Esempio n. 1
0
 def getDate(value):
     if isinstance(value, datetime.datetime):
         return Core.Time(*(value.timetuple()[:6]))
     elif isinstance(value, str):
         value = parsers.parseDate(value)
         return Core.Time(*(value.timetuple()[:6]))
     return value
Esempio n. 2
0
    def parse(self, line):
        # parses a line of input and returns a Pick object
        line = line.strip().split()
        pickID, time, net, sta = line[0], line[1], line[2], line[3]

        # this will become the creation time
        now = Core.Time.GMT()
        time = Core.Time()
        time.fromString("2015-09-25T10:33:42", "%Y-%m-%dT%H:%M:%S")
        pick = DataModel.Pick.Create(pickID)
        wfid = DataModel.WaveformStreamID()
        wfid.setNetworkCode(net)
        wfid.setStationCode(sta)
        pick.setWaveformID(wfid)
        crea = DataModel.CreationInfo()
        crea.setAuthor("pick import script")
        crea.setAgencyID("TEST")
        crea.setCreationTime(now)
        crea.setModificationTime(now)
        pick.setCreationInfo(crea)
        pick.setEvaluationStatus(DataModel.REVIEWED)
        pick.setEvaluationMode(DataModel.MANUAL)
        pick.setTime(DataModel.TimeQuantity(time))
        pick.setCreationInfo(crea)
        return pick
Esempio n. 3
0
    def init(self):
        if not Client.Application.init(self): return False

        try:
            cstr = self.commandline().optionString("coord")
            tstr = self.commandline().optionString("time")
        except:
            sys.stderr.write(
                "must specify origin using '--coord lat,lon,dep --time time'\n"
            )
            return False

        self.origin = DataModel.Origin.Create()

        ci = DataModel.CreationInfo()
        ci.setAgencyID(self.agencyID())
        ci.setCreationTime(Core.Time.GMT())
        self.origin.setCreationInfo(ci)

        lat, lon, dep = map(float, cstr.split(","))
        self.origin.setLongitude(DataModel.RealQuantity(lon))
        self.origin.setLatitude(DataModel.RealQuantity(lat))
        self.origin.setDepth(DataModel.RealQuantity(dep))

        time = Core.Time()
        time.fromString(tstr.replace("/", "-") + ":0:0", "%F %T")
        self.origin.setTime(DataModel.TimeQuantity(time))

        return True
Esempio n. 4
0
def _parseTime(timestring):
    t = Core.Time()
    if t.fromString(timestring, "%F %T"):
        return t
    if t.fromString(timestring, "%FT%T"):
        return t
    if t.fromString(timestring, "%FT%TZ"):
        return t
    return None
Esempio n. 5
0
 def _overlaps(self, pstart, pend, cstart, cend):
     if cstart is None and cend is None: return True
     
     if cstart is None:
         cstart = Core.Time()
     
     if pend is not None:
         if pend > cstart:
             if cend is None or pstart < cend:
                 return True
     else:
         if cend is None or pstart < cend:
             return True
 
     return False
Esempio n. 6
0
    def __init__(self, appName, msgConn, req_id, req_type, user, header, label,
                 user_ip, client_ip):
        self.msgConn = msgConn
        self.arclinkRequest = DataModel.ArclinkRequest.Create()
        self.arclinkRequest.setCreated(Core.Time.GMT())
        self.arclinkRequest.setRequestID(req_id)
        self.arclinkRequest.setUserID(str(user))
        self.arclinkRequest.setClientID(appName)
        if user_ip: self.arclinkRequest.setUserIP(user_ip)
        if client_ip: self.arclinkRequest.setClientIP(client_ip)
        self.arclinkRequest.setType(req_type)
        self.arclinkRequest.setLabel(label)
        self.arclinkRequest.setHeader(header)

        self.averageTimeWindow = Core.TimeSpan(0.)
        self.totalLineCount = 0
        self.okLineCount = 0

        self.requestLines = []
        self.statusLines = []
Esempio n. 7
0
    def getDACache(self):
        if not self._daEnabled:
            return None

        now = Core.Time.GMT()
        # check if cache is still valid
        if self._daCache is None or now > self._daCache.validUntil():

            if self.query() is None or \
               not self.query().driver().isConnected():
                dbInt = IO.DatabaseInterface.Open(self.databaseURI())
                if dbInt is None:
                    Logging.error('failed to connect to database')
                    return self._daCache
                else:
                    self.setDatabase(dbInt)

            da = DataModel.DataAvailability()
            self.query().loadDataExtents(da)
            validUntil = now + Core.TimeSpan(self._daCacheDuration, 0)
            self._daCache = DataAvailabilityCache(self, da, validUntil)

        return self._daCache
Esempio n. 8
0
    def run(self):
        if not self._processCommandLineOptions():
            return False

        dbq = self.query()
        ep = DataModel.EventParameters()

        # If we got an event ID as command-line argument...
        if self._evid:
            # Retrieve event from DB
            evt = dbq.loadObject(DataModel.Event.TypeInfo(), self._evid)
            evt = DataModel.Event.Cast(evt)
            if evt is None:
                raise TypeError, "unknown event '" + self._evid + "'"
            # If start time was not specified, compute it from origin time.
            if self._startTime is None:
                orid = evt.preferredOriginID()
                org = dbq.loadObject(DataModel.Origin.TypeInfo(), orid)
                org = DataModel.Origin.Cast(org)
                t0 = org.time().value()
                self._startTime = t0 + Core.TimeSpan(-before)
                self._endTime = t0 + Core.TimeSpan(after)
                print >> sys.stderr, "time window: %s ... %s" % (
                    self._startTime, self._endTime)

            if not self.commandline().hasOption("no-origins"):
                # Loop over all origins of the event
                for org in dbq.getOrigins(self._evid):
                    org = DataModel.Origin.Cast(org)
                    # We only look for manual events.
                    if org.evaluationMode() != DataModel.MANUAL:
                        continue
                    self._orids.append(org.publicID())

        # FIRST the pick query loop, THEN the amplitude query loop!
        # NESTED QUERY LOOPS ARE NOT ALLOWED!!!
        picks = []
        for obj in dbq.getPicks(self._startTime, self._endTime):
            pick = DataModel.Pick.Cast(obj)
            if pick:
                if pick.evaluationMode(
                ) == DataModel.MANUAL and self.commandline().hasOption(
                        "no-manual-picks"):
                    continue
                if pick.waveformID().networkCode() in self._networkBlacklist:
                    continue
                picks.append(pick)
                ep.add(pick)
        print >> sys.stderr, "loaded %d picks                         " % ep.pickCount(
        )

        for i, pick in enumerate(picks):
            # amplitude query loop for each pick, see above comments.
            for obj in dbq.getAmplitudesForPick(pick.publicID()):
                ampl = DataModel.Amplitude.Cast(obj)
                if ampl:
                    ep.add(ampl)
            sys.stderr.write("loaded amplitudes for %d of %d picks\r" %
                             (i, len(picks)))
        print >> sys.stderr, "loaded %d amplitudes                    " % ep.amplitudeCount(
        )

        if not self.commandline().hasOption("no-origins"):
            for i, orid in enumerate(self._orids):
                # XXX There was occasionally a problem with:
                #   org = dbq.loadObject(DataModel.Origin.TypeInfo(), orid)
                #   org = DataModel.Origin.Cast(org)
                # NOTE when org was directly overwritten.
                # resulting in a segfault. The reason is not clear, but
                # is most probably in the Python wrapper. The the segfault
                # can be avoided by creating an intermediate object 'obj'.
                obj = dbq.loadObject(DataModel.Origin.TypeInfo(), orid)
                org = DataModel.Origin.Cast(obj)
                ep.add(org)
                sys.stderr.write("loaded %d of %d manual origins\r" %
                                 (i, len(self._orids)))
            print >> sys.stderr, "loaded %d manual origins                " % ep.originCount(
            )

        # finally dump event parameters as formatted XML archive to stdout
        ar = IO.XMLArchive()
        ar.setFormattedOutput(True)
        ar.create("-")
        ar.writeObject(ep)
        ar.close()
        del ep
        return True
Esempio n. 9
0
 def parseTime(self, timeStr):
     time = Core.Time()
     for fmt in TimeFormats:
         if time.fromString(timeStr, fmt):
             break
     return time
Esempio n. 10
0
    def _runBatchMode(self):
        ep = self._readEventParametersFromXML()

        # collect the objects
        objs = []
        while ep.pickCount() > 0:
            # FIXME: The cast hack forces the SC3 refcounter to be increased.
            pick = DataModel.Pick.Cast(ep.pick(0))
            ep.removePick(0)
            objs.append(pick)
        while ep.amplitudeCount() > 0:
            # FIXME: The cast hack forces the SC3 refcounter to be increased.
            ampl = DataModel.Amplitude.Cast(ep.amplitude(0))
            ep.removeAmplitude(0)
            objs.append(ampl)
        while ep.originCount() > 0:
            # FIXME: The cast hack forces the SC3 refcounter to be increased.
            origin = DataModel.Origin.Cast(ep.origin(0))
            ep.removeOrigin(0)
            objs.append(origin)
        del ep

        # DSU sort all objects by object creation time
        sortlist = []
        for obj in objs:
            # discard objects that have no creationInfo attribute
            try:
                t = obj.creationInfo().creationTime()
            except:
                continue

            if self._startTime is not None and t < self._startTime:
                continue
            if self._endTime is not None and t > self._endTime:
                continue
            sortlist.append((t, obj))
        sortlist.sort()

        time_of_1st_object, obj = sortlist[0]
        time_of_playback_start = Core.Time.GMT()

        ep = DataModel.EventParameters()
        pickampl = {}

        # go through the sorted list of object and process them sequentially
        for t, obj in sortlist:
            if self.isExitRequested(): return

            if self.speed:
                t = time_of_playback_start + Core.TimeSpan(
                    float(t - time_of_1st_object) / self.speed)
                while t > Core.Time.GMT():
                    time.sleep(0.1)

            if obj.ClassName() not in ["Pick", "Amplitude", "Origin"]:
                continue

            DataModel.Notifier.Enable()
            ep.add(obj)
            msg = DataModel.Notifier.GetMessage()
            if self.commandline().hasOption("test"):
                sys.stderr.write("Test mode - not sending %-10s %s\n" %
                                 (obj.ClassName(), obj.publicID()))
            else:
                if self.connection().send(msg):
                    sys.stderr.write("Sent %s %s\n" %
                                     (obj.ClassName(), obj.publicID()))
                else:
                    sys.stderr.write("Failed to send %-10s %s\n" %
                                     (obj.ClassName(), obj.publicID()))
            DataModel.Notifier.Disable()
            self.sync()

        return
Esempio n. 11
0
def datetime_to_seiscomp(dt):
    """Convert a python or obspy UTC datetime to a seiscomp Time."""
    if isinstance(dt, UTCDateTime):
        dt = dt.datetime
    return Core.Time(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
                     dt.microsecond)
Esempio n. 12
0
 def parseLogLine(self, line):
     'convert log lines to sc3 objects'
     if 'E:I: ' in line or 'E:I:F' in line:  # origin parameters
         #E:I:H: eventid ver evlat evlon dep mag time latu lonu depu magu timeu lk nTb nSb nT nS ave rms fitok splitok near statrig active inact nsta percnt prcntok mindist maxdist distok azspan Mok nSok Lok Tdif Tok Aok Ast alert_time
         timeStamp, Eid, ver, lat, lon, depth, mag, otTxt, latu, lonu, depu, magu, timeu, lk, nTb, nSb, nT, nS, ave, rms, fitok, splitok, near, statrig, active, inact, nsta, percnt, prcntok, mindist, maxdist, distok, azspan, Mok, nSok, Lok, Tdif, Tok, Aok, Ast, atimeTxt = line.strip(
         ).split()  # parse line
         creationTime = sccore.Time()  # creation time placeholder
         creationTime.fromString(
             atimeTxt, '%Y-%m-%dT%H:%M:%S.%f')  # get creation time
         ot = sccore.Time()  # origin time placeholder
         ot.fromString(otTxt, '%Y-%m-%dT%H:%M:%S.%f')  # get origin time
         self._origin = self.createOrigin(
             ot=ot,
             otu=timeu,
             lat=lat,
             lon=lon,
             depth=depth,
             mag=mag,
             latu=latu,
             lonu=lonu,
             depthu=depu,
             magu=magu,
             reported=Aok,
             creationTime=creationTime)  # add origin
         self.addOriginQuality(self._origin, nT, nS, azspan, maxdist,
                               mindist, rms)  # add origin quality
         self.eparams.add(self._origin)
         if int(Eid) > 0:  # if event has a positive id number
             self.addEvent(Eid,
                           origins=[self._origin])  #add origin to event
     if 'E:I:T: ' in line:  # Trigger
         #E:I:T:H: eventid ver update order sta chan net loc lat lon trigger_time log_taup taup_snr log_pd pd_snr log_pv pv_snr pa pa_snr assoc tpmag utpm pdmag updm uch ukm upd ups utp uts distkm azimuth [tterr plen sps toffset arrtime protime fndtime quetime sndtime e2time buftime alert]
         timeStamp, Eid, ver, update, order, sta, chn, net, loc, lat, lon, trigger_time, log_taup, taup_snr, log_pd, pd_snr, log_pv, pv_snr, pa, pa_snr, assoc, tpmag, utpm, pdmag, updm, uch, ukm, upd, ups, utp, uts, distkm, azimuth = line.strip(
         ).split()[:33]  # parse line
         pTime = sccore.Time()
         pTime.fromString(trigger_time,
                          '%Y-%m-%dT%H:%M:%S.%f')  # get pick time
         if loc == '--': loc = ''  # adjust location format
         creationTime = sccore.Time()
         creationTime.fromString(
             trigger_time.split('T')[0] + 'T' + timeStamp.split("|")[0],
             '%Y-%m-%dT%H:%M:%S.%f')  # get creation time
         pick = self.addPick(pTime,
                             net,
                             sta,
                             loc,
                             chn,
                             azimuth,
                             distkm,
                             latency=0,
                             creationTime=creationTime)  # add pick
         self.addArrival(self._origin, pick.publicID(), azimuth,
                         distkm)  # add arrival to current origin
         amp = self.addAmplitude(ampval=log_pd,
                                 amptype='log_pd',
                                 snr=pd_snr,
                                 pickid=pick.publicID(),
                                 timeref=pTime,
                                 starttime=0,
                                 endtime=update,
                                 creationTime=creationTime)
         self.addStationMagnitude(self._origin,
                                  magval=pdmag,
                                  amp=amp,
                                  weight=1)
         amp = self.addAmplitude(ampval=log_taup,
                                 amptype='log_taup',
                                 snr=taup_snr,
                                 pickid=pick.publicID(),
                                 timeref=pTime,
                                 starttime=0,
                                 endtime=update,
                                 creationTime=creationTime)
         self.addStationMagnitude(self._origin,
                                  magval=tpmag,
                                  amp=amp,
                                  weight=0)
Esempio n. 13
0
    return wrap
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
class RequestTrackerDB(object):

	def __init__(self, dbWrite, req, user):
		self.dbWrite = dbWrite
		self.arclinkRequest = DataModel.ArclinkRequest.Create()
		self.arclinkRequest.setCreated(Core.Time.FromString(str(req.reqDate), "%Y-%m-%d %H:%M:%S"))
		self.arclinkRequest.setRequestID(req.reqID)
		self.arclinkRequest.setUserID(user)
		self.arclinkRequest.setType(req.reqType)
		self.arclinkRequest.setLabel(str(req.reqLabel))
		self.arclinkRequest.setHeader(" ".join(req.constrains))

		self.averageTimeWindow = Core.TimeSpan(0.)
		self.totalLineCount = req.requestLineCount()
		#self.okLineCount = self.totalLineCount - req.errorLineCount()
		self.okLineCount = 0

		for line in req.getRequestLines():
			self.line_status(line)

		for line in req.getVolumeLines():
			self.volume_status(line)

		self.request_status("END", "") # FIXME


	@catch
	def send(self):