Beispiel #1
0
 def send(self):
     #is it possible to send a directory and a file in the same command?
     #let's assume it isn't
     if self.filesToSend is not None and self.directoryToSend is not None:
         raise SenderError('For PDL, you must choose files OR a directory to send, not both')
     for prop in self.required_properties:
         if prop not in self.properties.keys():
             raise SenderError('"%s" property must be supplied to send via PDL')
     jarfile = self.properties['jarfile']
     source = self.properties['source']
     ptype = self.properties['type']
     keyfile = self.properties['keyfile']
     configfile = self.properties['configfile']
     code = self.properties['code']
     if not os.path.isfile(jarfile):
         raise SenderError('Could not find Java jar file "%s".' % jarfile)
     if not os.path.isfile(configfile):
         raise SenderError('Could not find PDL config file "%s".' % configfile)
     if not os.path.isfile(keyfile):
         raise SenderError('Could not find PDL private key file "%s".' % keyfile)
     #build pdl command line from properties
     javabin = self.findjava()
     if javabin is None:
         raise SenderError('Could not find Java binary on system path.')
     basecmd = '%s -jar %s --send --source=%s --type=%s --privateKey=%s --configFile=%s --code=%s ' % (javabin,jarfile,source,ptype,keyfile,configfile,code)
     nuggets = []
     for key,value in self.properties.iteritems():
         if key in self.required_properties:
             continue
         if isinstance(value,int):
             vstr = '%i' % value
         elif isinstance(value,float):
             vstr = '%f' % value
         else:
             vstr = value
         nuggets.append('--%s=%s' % (key,vstr))
     cmd = basecmd + ' '.join(nuggets) + ' '
     nfiles = 0
     #pdl can be used to send information without sending any files
     if self.directoryToSend is None and self.filesToSend is None:
         #this is ok - PDL products can be defined completely on the command line
         retcode,stdout,stderr = getCommandOutput(cmd)
         if not retcode:
             raise SenderError('Could not send directory "%s" due to error "%s"' % (self.directoryToSend,stdout+stderr))
     if self.directoryToSend is not None:
         cmd = cmd + '--directory=%s ' % self.directoryToSend
         retcode,stdout,stderr = getCommandOutput(cmd)
         if not retcode:
             raise SenderError('Could not send directory "%s" due to error "%s"' % (self.directoryToSend,stdout+stderr))
         nfiles += len(os.walk(self.directoryToSend).next()[2])
     elif self.filesToSend is not None:
         for f in self.filesToSend:
             cmd = cmd + '--file=%s ' % f
             retcode,stdout,stderr = getCommandOutput(cmd)
             if not retcode:
                 raise SenderError('PDL command: "%s"\nCould not send file "%s" due to error "%s"' % (cmd,f,stdout+stderr))
             nfiles += 1
     return nfiles
Beispiel #2
0
def runSecondary(url,thisdir):
    cmd = os.path.join(thisdir,SECHAZ)
    cmd += ' -r %s' % url
    retcode,stdout,stderr = getCommandOutput(cmd)
    pdf = None
    png = None
    for line in stdout.split('\n'):
        if line.find('Saving map output') > -1:
            parts = line.split()
            tfile = parts[-1]
            if tfile.find('pdf') > -1:
                pdf = tfile
            if tfile.find('png') > -1:
                png = tfile
    return (pdf,png)
Beispiel #3
0
    def send(self):
        #we can really only support sending of one file and/or one directory, so error out
        #if someone has specified more than one of either.
        if len(self.files) > 1:
            raise ShakeMapException(
                'For PDL, you may only send one file at a time.')
        if len(self.directories) > 1:
            raise ShakeMapException(
                'For PDL, you may only send one directory at a time.')

        #make sure we have all the required properties
        for prop in self.required_properties:
            if prop not in list(self.properties.keys()):
                raise ShakeMapException(
                    '"%s" property must be supplied to send via PDL')

        #build pdl command line from properties
        self.properties['command'] = 'send'
        self.properties['status'] = 'UPDATE'
        if self.files:
            self.properties['file'] = self.files[0]
        else:
            self.properties['file'] = ''
        if self.directories:
            self.properties['directory'] = self.directories[0]
        else:
            self.properties['directory'] = ''
        cmd = self.pdlcmd
        for propkey, propvalue in self.properties.items():
            cmd = cmd.replace('[' + propkey.upper() + ']', propvalue)

        #call PDL on the command line
        retcode, stdout, stderr = getCommandOutput(cmd)
        if not retcode:
            fmt = 'Could not send product "%s" due to error "%s"'
            tpl = (code, stdout + stderr)
            raise ShakeMapException(fmt % tpl)

        #return the number of files we just sent
        nfiles = 0
        if self.properties['file']:
            nfiles += 1
        if self.properties['directory']:
            nfiles += len(os.listdir(self.properties['directory']))

        return nfiles
Beispiel #4
0
    def delete(self):
        for prop in self.required_properties:
            if prop not in list(self.properties.keys()):
                raise ShakeMapException(
                    '"%s" property must be supplied to send via PDL')

        #build pdl command line from properties
        self.properties['status'] = 'DELETE'
        self.properties['files'] = ''
        self.properties['directories'] = ''
        cmd = self.pdlcmd
        for propkey, propvalue in self.properties.items():
            cmd = cmd.replace('[' + propkey.upper() + ']', propvalue)

        retcode, stdout, stderr = getCommandOutput(cmd)
        if not retcode:
            fmt = 'Could not delete product "%s" due to error "%s"'
            tpl = (code, stdout + stderr)
            raise ShakeMapException(fmt % tpl)
Beispiel #5
0
def main(args):
    globaldict = getGlobalConfig()
    shakehome = globaldict['shakehome']
    popfile = globaldict['popfile']
    if shakehome is None:
        print 'Cannot find ShakeMap home folder on this system.'
        sys.exit(1)
    datadir = os.path.join(shakehome,'data',args.event)
    if not os.path.isdir(datadir):
        print 'Cannot find event %s on the system' % args.event
        sys.exit(1)

    #Make sure the timeoutput folder is there (can't put our time grids in output - that gets
    #wiped out every time shakemap runs
    outfolder = os.path.join(datadir,'timeoutput')
    if not os.path.isdir(outfolder):
        os.makedirs(outfolder)
        
    #now look for config file in top-level folder
    configfile = os.path.join(datadir,'alert.conf')
    if not os.path.isfile(configfile):
        print 'Cannot find alert config file for %s in the data directory' % args.event
        sys.exit(1)
    config = ConfigParser.ConfigParser()
    config.readfp(open(configfile))

    #get the bounds of the map so we can find cities
    xmin = float(config.get('MAP','xmin'))
    xmax = float(config.get('MAP','xmax'))
    ymin = float(config.get('MAP','ymin'))
    ymax = float(config.get('MAP','ymax'))
    
    citylist = getCityList(xmin,xmax,ymin,ymax,globaldict['cityfile'])
    
    #Get the MMI threshold below which alert times will NOT be saved
    mmithresh = float(config.get('MAP','mmithresh'))

    #get the array of epicenters
    lats = [float(p) for p in config.get('FAULT','lats').split()]
    lons = [float(p) for p in config.get('FAULT','lons').split()]

    #write out a new grind.conf file
    writeGrind(config,datadir)

    #instantiate our p/s travel time calculator
    calc = TravelTimeCalculator()

    #where is the grind binary?
    grindbin = os.path.join(shakehome,'bin','grind')

    #specify the event.xml file, get the depth of the event
    eventfile = os.path.join(datadir,'input','event.xml')
    root = parse(eventfile)
    eq = root.getElementsByTagName('earthquake')[0]
    depth = float(eq.getAttribute('depth'))
    root.unlink()

    #get the dimensionality of the grid file and of the pop grid we'll interpolate to
    gridfile = os.path.join(datadir,'output','grid.xml')
    if not os.path.isfile(gridfile):
        grindcmd = '%s -event %s' % (grindbin,args.event)
        res,stdout,stderr = getCommandOutput(grindcmd)
    mmigrid = ShakeGrid(gridfile,variable='MMI')
    popgrid = EsriGrid(popfile)
    popgrid.load(bounds=mmigrid.getRange())
    m,n = popgrid.griddata.shape
    
    #loop over all the event realizations
    timefiles = []
    timestack = np.zeros((m,n,len(lats)),dtype=np.float32)
    for i in range(0,len(lats)):
        print 'Calculating arrival times for scenario %i of %i' % (i+1,len(lats))
        lat = lats[i]
        lon = lons[i]
        if i == 0:
            lonoff = 0
            latoff = 0
        else:
            lonoff = -1* (lons[i] - lons[i-1])
            latoff = lats[i] - lats[i-1]
        #modify the event.xml file to have the new lat/lon epicenter
        sourcetext = getEventText(eventfile,lat,lon)
        f = open(eventfile,'wt')
        f.write(sourcetext)
        f.close()

        sdict = getSlowestStation(lat,lon,depth,calc)
        ptime = sdict['time']
        stationlat = sdict['lat']
        stationlon = sdict['lon']
        
        grindcmd = '%s -latoff %f -lonoff %f -event %s' % (grindbin,latoff,lonoff,args.event)
        res,stdout,stderr = getCommandOutput(grindcmd)
        if not res:
            print 'Grind command failed: "%s", "%s"' % (stdout,stderr)
            sys.exit(1)
            
        #Get the grid.xml output, do some time calculations
        mmigrid = ShakeGrid(gridfile,variable='MMI')
        timegrid = np.zeros((m,n),dtype=np.float32)
        
        for row in range(0,m):
            for col in range(0,n):
                mmilat,mmilon = mmigrid.getLatLon(row,col)
                distance = locations2degrees(lat,lon,mmilat,mmilon)
                tmp,stime = calc.getTravelTimes(distance,depth)
                timegrid[row,col] = stime - ptime

        #debugging
        f = plt.figure()
        plt.subplot(2,1,1)
        plt.imshow(mmigrid.griddata)
        plt.colorbar()
        plt.subplot(2,1,2)
        plt.imshow(timegrid)
        plt.colorbar()
        plt.savefig(os.path.join(outfolder,'timegrid.png'))
        plt.close(f)

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            exposure,timegrid = getTimeExposure(timegrid,mmigrid,popfile,mmithresh)
        print 'Population Warning Times for epicenter %.4f,%.4f' % (lat,lon)
        printExposure(exposure)
        expofile = os.path.join(outfolder,'expo%03i.json' % (i+1))
        f = open(expofile,'wt')
        f.write(json.dumps(exposure))
        f.close()
        timefile = os.path.join(outfolder,'timegrid%03i.flt' % (i+1))
        timefiles.append(timefile)
        metadict = {'epilat':lat,'epilon':lon,'eventid':args.event}
        saveTimeGrid(timefile,timegrid,mmigrid.geodict,metadict)
        timestack[:,:,i] = timegrid
        alertgrid = popgrid
        alertgrid.griddata = timegrid
        makeMap(alertgrid,'alertmap_%i' % i,outfolder,popfile,globaldict['popcolormap'],sdict,citylist,[lat],[lon])
        
        
    methods = config.get('MAP','output').split(',')
    for method in methods:
        if method == 'median':
            statgrid = np.median(timestack,axis=2)
        if method == 'mean':
            statgrid = np.nanmean(timestack,axis=2)
        if method == 'min':
            statgrid = np.nanmin(timestack,axis=2)
        if method == 'max':
            statgrid = np.nanmax(timestack,axis=2)
        timegrid = popgrid
        timegrid.griddata = statgrid
        makeMap(timegrid,method,outfolder,popfile,globaldict['popcolormap'],sdict,citylist,lats,lons)
Beispiel #6
0
 def send(self):
     #is it possible to send a directory and a file in the same command?
     #let's assume it isn't
     if self.filesToSend is not None and self.directoryToSend is not None:
         raise SenderError(
             'For PDL, you must choose files OR a directory to send, not both'
         )
     for prop in self.required_properties:
         if prop not in self.properties.keys():
             raise SenderError(
                 '"%s" property must be supplied to send via PDL')
     jarfile = self.properties['jarfile']
     source = self.properties['source']
     ptype = self.properties['type']
     keyfile = self.properties['keyfile']
     configfile = self.properties['configfile']
     code = self.properties['code']
     if not os.path.isfile(jarfile):
         raise SenderError('Could not find Java jar file "%s".' % jarfile)
     if not os.path.isfile(configfile):
         raise SenderError('Could not find PDL config file "%s".' %
                           configfile)
     if not os.path.isfile(keyfile):
         raise SenderError('Could not find PDL private key file "%s".' %
                           keyfile)
     #build pdl command line from properties
     javabin = self.findjava()
     if javabin is None:
         raise SenderError('Could not find Java binary on system path.')
     basecmd = '%s -jar %s --send --source=%s --type=%s --privateKey=%s --configFile=%s --code=%s ' % (
         javabin, jarfile, source, ptype, keyfile, configfile, code)
     nuggets = []
     for key, value in self.properties.iteritems():
         if key in self.required_properties:
             continue
         if isinstance(value, int):
             vstr = '%i' % value
         elif isinstance(value, float):
             vstr = '%f' % value
         else:
             vstr = value
         nuggets.append('--%s=%s' % (key, vstr))
     cmd = basecmd + ' '.join(nuggets) + ' '
     nfiles = 0
     #pdl can be used to send information without sending any files
     if self.directoryToSend is None and self.filesToSend is None:
         #this is ok - PDL products can be defined completely on the command line
         retcode, stdout, stderr = getCommandOutput(cmd)
         if not retcode:
             raise SenderError(
                 'Could not send directory "%s" due to error "%s"' %
                 (self.directoryToSend, stdout + stderr))
     if self.directoryToSend is not None:
         cmd = cmd + '--directory=%s ' % self.directoryToSend
         retcode, stdout, stderr = getCommandOutput(cmd)
         if not retcode:
             raise SenderError(
                 'Could not send directory "%s" due to error "%s"' %
                 (self.directoryToSend, stdout + stderr))
         nfiles += len(os.walk(self.directoryToSend).next()[2])
     elif self.filesToSend is not None:
         for f in self.filesToSend:
             cmd = cmd + '--file=%s ' % f
             retcode, stdout, stderr = getCommandOutput(cmd)
             if not retcode:
                 raise SenderError(
                     'PDL command: "%s"\nCould not send file "%s" due to error "%s"'
                     % (cmd, f, stdout + stderr))
             nfiles += 1
     return nfiles
Beispiel #7
0
if __name__ == '__main__':
    #make a data set
    data = np.arange(0,16).reshape(4,4).astype(np.int32)
    geodict = {'xmin':0.5,'xmax':3.5,'ymin':0.5,'ymax':3.5,'xdim':1.0,'ydim':1.0,'nrows':4,'ncols':4}
    gmtgrid = GMTGrid(data,geodict)

    #save that data set to a grid
    gmtgrid.save('gmt_from_python.grd')

    #use gmt to get the value at 1.5,1.5 (should be 9)
    f = open('track.xy','wt')
    f.write('1.5 1.5\n')
    f.close()
    cmd = 'gmt grdtrack -nn track.xy -Ggmt_from_python.grd'
    res,stdout,stderr = getCommandOutput(cmd)
    
    print stdout

    #now create an XY file from our grid
    f = open('from_python.xyz','wt')
    for i in range(0,geodict['nrows']):
        for j in range(0,geodict['ncols']):
            lat,lon = gmtgrid.getLatLon(i,j)
            value = gmtgrid.getValue(lat,lon)
            f.write('%.1f %.1f %i\n' % (lon,lat,value))
    f.close()

    #now create a grid file from our XY file
    cmd = 'gmt xyz2grd -R0.5/3.5/0.5/3.5 -I1.0/1.0 from_python.xyz -Gfrom_gmt.grd'
    res,stdout,stderr = getCommandOutput(cmd)
Beispiel #8
0
def main(args):
    eventid = args.id
    radius = args.radius
    #does the bayesloc folder exist?
    if not os.path.isdir(BAYESDIR):
        print FOLDER_ERROR
        sys.exit(1)
    bayesbin = os.path.join(BAYESDIR,'bin',BAYESBIN)
    ttimes = glob.glob(os.path.join(BAYESDIR,'ttimes','ak135.*'))
    if not os.path.isfile(bayesbin):
        print FOLDER_ERROR
        sys.exit(1)
    if not len(ttimes):
        print FOLDER_ERROR
        sys.exit(1)
    bayesdb = os.path.join(BAYESDIR,BAYESDB)
    # if startOver and os.path.isfile(bayesdb):
    #     os.remove(bayesdb)
    #does the database exist - if not, create it
    if not os.path.isfile(bayesdb):
        db = sqlite3.connect(bayesdb)
        cursor = db.cursor()
        createTables(db,cursor)
    else:
        db = sqlite3.connect(bayesdb)
        cursor = db.cursor()

    #Delete selected list of events
    if args.delete:
        nevents = deleteEvents(db,cursor,args.delete)
        print '%i events deleted from the database.' % nevents
        sys.exit(0)
        
    #Return some stats about the current database
    if args.stats:
        nevents,nstations,narrivals = getStats(cursor)
        print 'Your database contains information about:'
        print '\t%i events' % nevents
        print '\t%i stations' % nstations
        print '\t%i picks' % narrivals
        sys.exit(0)
        
    eventinfo = getPhaseData(eventid=eventid)
    if not len(eventinfo):
        print 'Could not find event %s in ComCat.  Returning.'
        sys.exit(1)

    #get the information about the input event
    eventinfo = eventinfo[0]
    eventlat = eventinfo.origins[0]['lat']
    eventlon = eventinfo.origins[0]['lon']
    eventtime = eventinfo.origins[0]['time']
    if eventtime < args.begindate or eventtime > args.enddate:
        fmt = 'Event %s (%s) is outside the time bounds you specified. %s to %s.  Exiting.' 
        print fmt % (eventinfo.eventcode,eventtime,args.begindate,args.enddate)
        sys.exit(1)

    tnow = datetime.utcnow()
    eventfolder = os.path.join(BAYESDIR,'events',eventid)
    if not os.path.isdir(eventfolder):
        os.makedirs(eventfolder)

    # eventlist1 = getEventData(radius=(eventlat,eventlon,0,radius),
    #                          starttime=args.begindate,
    #                          endtime=args.enddate,catalog='pde')
    eventlist = getEventData(radius=(eventlat,eventlon,0,radius),
                              starttime=args.begindate,
                              endtime=args.enddate,catalog='us')
    #eventlist = eventlist1 + eventlist2

    if args.count:
        fmt = 'There are %i events inside %.1f km radius around event %s (%.4f,%.4f)'
        print fmt % (len(eventlist),radius,eventid,eventlat,eventlon)
        sys.exit(0)
    
    #check to see if event has already been located - if so, stop, unless we're being forced
    if not args.force:
        sql = 'SELECT id,code,rlat,rlon,rdepth,rtime FROM event WHERE code="%s"' % eventid
        cursor.execute(sql)
        row = cursor.fetchone()
        if row is not None and row[2] is not None:
            print 'Event %s is already in the database.  Stopping.' % eventid
            sys.exit(0)
    
    priors = getEventPriors(eventlist,cursor)
    stations,arrivals,newevents = getProcessedData(eventlist,db,cursor,ndays=NWEEKS*7)
    fmt = 'In database: %i stations, %i arrivals.  %i events not in db.'
    #print fmt % (len(stations),len(arrivals),len(newevents))
    missing_stations = []
    for event in newevents:
        phasedata = getPhaseData(eventid=event)
        if phasedata is None:
            continue
        if not len(phasedata[0].magnitudes):
            continue
        newstations,newarrivals,ms = insertPhaseData(phasedata[0],db,cursor)
        stations = dict(stations.items() + newstations.items())
        arrivals += newarrivals
        missing_stations += ms

    print 'After searching online:'
    fmt = 'In database: %i stations, %i arrivals.  %i missing stations.'
    print fmt % (len(stations),len(arrivals),len(missing_stations))
    stafile = 'station.dat'
    stationfile = os.path.join(eventfolder,stafile)
    f = open(stationfile,'wt')
    f.write('sta_id lat lon elev\n')
    for stationcode,stationvals in stations.iteritems():
        slat,slon,elev = stationvals
        f.write('%s %.4f %.4f %.3f\n' % (stationcode,slat,slon,elev))
    f.close()

    arrfile = 'arrival.dat'
    arrivalfile = os.path.join(eventfolder,arrfile)
    f = open(arrivalfile,'wt')
    f.write('ev_id sta_id phase time\n')
    for arrival in arrivals:
        eid,scode,phase,time = arrival
        f.write('%i %s %s %.3f\n' % (eid,scode,phase,time))
    f.close()

    prifile = 'prior.dat' #??
    priorfile = os.path.join(eventfolder,prifile)
    f = open(priorfile,'wt')
    f.write('ev_id lat_mean lon_mean dist_sd depth_mean depth_sd time_mean time_sd\n')
    for prior in priors:
        evid,plat,plon,pdepth,ptime = prior
        f.write('%i %.4f %.4f 0.0 %.1f 0.0 %.3f 0.0\n' % (evid,plat,plon,pdepth,ptime))
    f.close()

    #write the config file
    configfile = os.path.join(eventfolder,'bayesloc.cfg')
    config = CONFIG.replace('BAYESLOC',BAYESLOC)
    config = config.replace('EVENTFOLDER',eventfolder)
    fcfg = open(configfile,'wt')
    fcfg.write(config)
    fcfg.close()

    #Run the BayesLoc program
    #change to the eventfolder
    cwd = os.getcwd()
    os.chdir(eventfolder)
    bayesbin = os.path.join(BAYESLOC,'bin','bayesloc')
    cmd = '%s %s' % (bayesbin,configfile)
    print 'Running command %s...' % cmd
    t1 = datetime.now()
    # process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    # for c in iter(lambda: process.stdout.read(1), ''):
    #     sys.stderr.write(c)
    res,stdout,stderr = getCommandOutput(cmd)
    t2 = datetime.now()
    if not res:
        print 'BayesLoc command "%s" failed.  \n%s\n%s.' % (cmd,stdout,stderr)
        sys.exit(1)
    else:
        dt = ((t2-t1).seconds)/60.0
        print 'BayesLoc command was successful - took %.1f minutes.' % dt
    os.chdir(cwd)

    resultfile = os.path.join(eventfolder,'output','origins_ned_stats.out')
    f = open(resultfile,'rt')
    f.readline()
    eventlist = []
    fieldlist = ['lat','lon','depth','time','rlat','rlon','rdepth','rtime','mag','nevents']
    nevents = len(priors) + len(newevents)
    for line in f.readlines():
        parts = line.split()
        eid = int(parts[0])
        lat = float(parts[1])
        lon = float(parts[2])
        depth = float(parts[3])
        time = UTCDateTime(float(parts[4])).datetime
        efmt = 'UPDATE event set rlat=%.4f,rlon=%.4f,rdepth=%.1f,rtime="%s",nevents=%i WHERE id=%i'
        equery = efmt % (lat,lon,depth,time,nevents,eid)
        cursor.execute(equery)
        db.commit()
        query = 'SELECT %s FROM event WHERE id=%i' % (','.join(fieldlist),eid)
        cursor.execute(query)
        row = cursor.fetchone()
        eventlist.append(dict(zip(fieldlist,row)))
    f.close()

    #make a map of all the relocated events
    fname = makeMap(eventlist,eventlat,eventlon,eventfolder)
    print 'Relocated events: %s' % fname
    
    #tell the user what happened with the relocation
    fmt = 'SELECT lat,lon,depth,time,rlat,rlon,rdepth,rtime,nevents FROM event WHERE code="%s"'
    query = fmt % (eventid)
    cursor.execute(query)
    row = cursor.fetchone()
    lat,lon,depth,time,rlat,rlon,rdepth,rtime,nevents = row
    time = UTCDateTime(time).datetime
    rtime = UTCDateTime(rtime).datetime
    if rtime >= time:
        dt = (rtime-time).seconds + ((rtime-time).microseconds)/float(1e6)
    else:
        dt = (time-rtime).seconds + ((time-rtime).microseconds)/float(1e6)
    dd,az1,az2 = gps2DistAzimuth(lat,lon,rlat,rlon)
    dd /= 1000.0
    print 'Event %s was relocated using %i events.' % (eventid,nevents)
    print 'Starting:  %s (%.4f,%.4f) %.1f km' % (time.strftime('%Y-%m-%d %H:%M:%S'),lat,lon,depth)
    print 'Relocated: %s (%.4f,%.4f) %.1f km' % (rtime.strftime('%Y-%m-%d %H:%M:%S'),rlat,rlon,rdepth)
    print '%.1f km (%.1f degrees), %.1f seconds' % (dd,az1,dt)
    cursor.close()
    db.close()