<?xml version="1.0" encoding="UTF-8"?>
<sod>
    <eventArm>
        <fdsnEvent>
            <originTimeRange>
                <startTime>
                    <year>2003</year>
                    <month>1</month>
                    <day>1</day>
                </startTime>
                <endTime>
                    <year>2003</year>
                    <month>1</month>
                    <day>16</day>
                </endTime>
            </originTimeRange>
            <magnitudeRange>
                <min>5</min>
            </magnitudeRange>
        </fdsnEvent>
        <removeEventDuplicate/>
        <originScript type="jython">
           result = preferredOrigin.magnitudeList.get(0).value > 5.5
        </originScript>
        <printlineEventProcess/>
    </eventArm>
    <networkArm>
        <fdsnStation>
        </fdsnStation>
        <networkAND>
            <networkOR>	
                <networkCode>IU</networkCode>
                <networkCode>II</networkCode>
            </networkOR>	
            <networkScript type="jython" >
                if networkAttr.get_code() == 'II' or networkAttr.get_code() == 'IU':
                  print networkAttr.get_code()
                  result = True
                else:
                  result = False
            </networkScript>
        </networkAND>
        <stationAND>
            <stationScript type="jython">
            from edu.sc.seis.fissuresUtil.chooser import ClockUtil
            from edu.sc.seis.sod.status import Pass, Fail
            if station.getEndDate().after(ClockUtil.now()):
                result = util.pass("Station is alive now")
            else:
                result = util.fail("Station is no longer active")
            </stationScript>
        </stationAND>
        <siteCode>00</siteCode>
        <channelOR>
           <channelCode>BHN</channelCode>
           <channelCode>BHE</channelCode>
           <channelCode>BHZ</channelCode>
           <channelAND>
               <channelCode>LHZ</channelCode>
               <channelScript type="jython">
                   from edu.sc.seis.fissuresUtil.chooser import ClockUtil
                   otherChans = networkSource.getChannels(channel.station)
                   result = util.pass("No BHZ, only LHZ is alive now")
                   for other in otherChans:
                       if other.code == "BHZ" and other.getEndDate().after(ClockUtil.now()):
                           # active BHZ, so reject LHZ
                           result = util.fail( "Use BHZ instead of LHZ for "+channel.network.code+"."+channel.station.code)
               </channelScript>
           </channelAND>
        </channelOR>
        <printlineChannelProcess/>
    </networkArm>
    <waveformVectorArm>
        <eventStationScript  type="jython" >
        from edu.sc.seis.TauP import SphericalCoords, SeismicPhase
        from edu.iris.Fissures import Location
        from edu.iris.Fissures.model import BoxAreaImpl
        from edu.sc.seis.fissuresUtil.bag import AreaUtil
        minLatitude = 0;
        maxLatitude = 40;
        minLongitude = 100;
        maxLongitude = 160;
        result = False
        box = BoxAreaImpl(minLatitude, maxLatitude, minLongitude, maxLongitude)
        PP = SeismicPhase('PP','iasp91',event.getPreferred().getLocation().depth.value)
        dist = SphericalCoords.distance(event.getPreferred().getLocation().latitude, event.getPreferred().getLocation().longitude, station.getFloatLatitude(), station.getFloatLongitude())
        arrival = PP.getEarliestArrival(dist)
        if arrival != None:
           piercePoints = arrival.getPierce()
           for timeDist in piercePoints:
              if timeDist.getDistDeg() != 0 and timeDist.getDepth() == 0:
                 azimuth = SphericalCoords.azimuth(event.getPreferred().getLocation().latitude, event.getPreferred().getLocation().longitude, station.getFloatLatitude(), station.getFloatLongitude())
                 bounceLat = SphericalCoords.latFor(station.getFloatLatitude(), station.getFloatLongitude(), timeDist.getDistDeg(), azimuth)
                 bounceLon = SphericalCoords.lonFor(station.getFloatLatitude(), station.getFloatLongitude(), timeDist.getDistDeg(), azimuth)
                 result = AreaUtil.inArea(box, bounceLat, bounceLon)
                 break
        </eventStationScript>
        <eventVectorScript type="jython">
        for channel in channelGroup.getChannels():
           if channel.getOrientationCode() == 'N' and abs(channel.getAzimuth()) > 5:
               # reject N channels that are more than 5 degrees from north
               result = util.fail('channel not really North, %f'%(channel.getAzimuth(),))
        </eventVectorScript>
        <phaseRequest>
            <model>prem</model>
            <beginPhase>ttp</beginPhase>
            <beginOffset>
                <unit>SECOND</unit>
                <value>-120</value>
            </beginOffset>
            <endPhase>tts</endPhase>
            <endOffset>
                <unit>SECOND</unit>
                <value>360</value>
            </endOffset>
        </phaseRequest>
        <vectorRequestScript type="jython">
            from edu.iris.Fissures.model import MicroSecondDate, UnitImpl
            for rfarray in request:
                for rf in rfarray:
                   width = rf.end.subtract(rf.begin)
                   if width.getValue(UnitImpl.SECOND) &lt; 500:
                       result = util.fail("only "+width.getValue(UnitImpl.SECOND)+" seconds")
                   else:
                       result = True
        </vectorRequestScript>
        <vectorAvailableDataScript type="jython">
             result = True
             if len(request) != len(available):
                 result = False
             else:
                 for i in range(len(request)):
                     if len(request[i]) != len(available[i]):
                         result = False
                         break
        </vectorAvailableDataScript>
        <vectorScript type="jython" >
        from jarray import array
        from bag import rmean, taper, rtrend, stat
        from edu.iris.Fissures.seismogramDC import LocalSeismogramImpl
        from edu.iris.Fissures.network import ChannelIdUtil
        print "Hello Sod "
        out = []
        for perChanSeis in seismograms:
            seisout = []
            out.append(seisout)
            for s in perChanSeis:
                s = rmean(s)
                s = taper(rtrend(s))
                print ChannelIdUtil.toStringNoDates(s.getChannelID())
                print
                print stat(s)
                seisout.append(s)
        result = util.waveformVectorResult(True, out[0], out[1], out[2])
       </vectorScript>
        <printlineSeismogramProcess/>
    </waveformVectorArm>
</sod>
