def getShakeMapParams(self,config,shakefile,geodict): #get the shakemap params the user wants smparams = config.get('SHAKEMAP','variables').split(',') #load the shakemap intset = set(SM_TERMS).intersection(smparams) if not len(intset): print 'The allowed ShakeMap variables are: "%s". Your config file has "%s".' % (str(SM_TERMS),str(smparams)) sys.exit(1) shakedict = {} shakemap = None if 'PGA' in smparams: shakemap = ShakeGrid(shakefile,variable='PGA') shakemap.interpolateToGrid(geodict) tmpgrid = gmt.GMTGrid() tmpgrid.loadFromGrid(shakemap) shakedict['PGA'] = tmpgrid if 'PGV' in smparams: shakemap = ShakeGrid(shakefile,variable='PGV') shakemap.interpolateToGrid(geodict) tmpgrid = gmt.GMTGrid() tmpgrid.loadFromGrid(shakemap) shakedict['PGV'] = tmpgrid if 'MMI' in smparams: shakemap = ShakeGrid(shakefile,variable='MMI') shakemap.interpolateToGrid(geodict) tmpgrid = gmt.GMTGrid() tmpgrid.loadFromGrid(shakemap) shakedict['MMI'] = tmpgrid if 'MW' in smparams: if shakemap is None: shakemap = ShakeGrid(shakefile,variable='MMI') attdict = shakemap.getAttributes() shakedict['MW'] = attdict['event']['magnitude'] return (shakedict)
def main(args): #read in global config file configfile = os.path.join(os.path.expanduser('~'),'.lsprocess','lsprocess.cfg') hasconfig = os.path.isfile(configfile) if not hasconfig: print() print('No config file "%s" found.' % configfile) print() sys.exit(1) global_grids,outfolder = readConfig(configfile) #returns a dictionary just like global_config above #read in event specific grid file try: covdict,predictors,ename = parseEvent(args.eventfile) except Exception as msg: print('There is something wrong with your event file. See errors below.') print(msg) sys.exit(1) #construct output folder from global/event configs outfolder = os.path.join(outfolder,ename) if not os.path.isdir(outfolder): os.mkdir(outfolder) #look for bounding box and resolution in event config file, or get from shakemap bbox = None shakemap = ShakeGrid(predictors['shakemap'][0],'MMI') if 'bbox' in covdict: bbox = covdict['bbox'] else: #bbox = shakemap.getRange() #default to the bounding box of the coverage data with fiona.open(covdict['filename']) as src: tbbox = src.bounds bbox = (tbbox[0],tbbox[2],tbbox[1],tbbox[3]) if 'resolution' in covdict: resolution = covdict['resolution'] else: resolution = shakemap.getGeoDict()['xdim'] #get input coverage projection from event config OR from .prj file #projstr = covdict['projstr'] #get format of coverage, check against list of supported fiona formats, read in data #we'll do other support later #if necessary, project coverage into lat/lon #skip projection for now as well #determine what the grid shape and (potentially) new bbox is given bbox and resolution nrows,ncols,bbox = getShape(bbox,resolution) #if the coverage dataset is larger than the ShakeMap, we need to make sure our output grid #is contained by the shakemap for interpolation purposes. shakebounds = shakemap.getRange() shakexdim,shakeydim = (shakemap.geodict['xdim'],shakemap.geodict['ydim']) xmin = max(bbox[0],shakebounds[0]+shakexdim*2) xmax = min(bbox[1],shakebounds[1]-shakexdim*2) ymin = max(bbox[2],shakebounds[2]+shakeydim*2) ymax = min(bbox[3],shakebounds[3]-shakeydim*2) geodict = {'xdim':resolution,'ydim':resolution, 'xmin':xmin,'xmax':xmax, 'ymin':ymin,'ymax':ymax, 'nrows':nrows,'ncols':ncols} #rasterize projected coverage defined bounding box and resolution shpfile = covdict['filename'] print('Creating coverage grid...') covgrid = makeCoverageGrid(shpfile,geodict) outgridfile = os.path.join(outfolder,'coverage.grd') print('Saving coverage to %s...' % outgridfile) covgrid.save(outgridfile) #make a grid of lat,lon values row = np.arange(0,nrows) col = np.arange(0,ncols) rows = repmat(row,ncols,1).T cols = repmat(col,nrows,1) lat,lon = covgrid.getLatLon(rows,cols) #create a list of arrays that we'll dump out to a text file when done vardict = {} vardict['coverage'] = covgrid.griddata.flatten() vardict['lat'] = lat.flatten() vardict['lon'] = lon.flatten() #subset shakemap and global grids using defined bounding box and resolution shakefile = predictors['shakemap'][0] variables = predictors['shakemap'][1] for var in variables: shakemap = ShakeGrid(shakefile,var.upper()) shakemap.interpolateToGrid(geodict) gmtshake = GMTGrid() gmtshake.geodict = shakemap.geodict gmtshake.griddata = shakemap.griddata outshakefile = os.path.join(outfolder,'%s.grd' % var) print('Saving %s to %s...' % (var,outshakefile)) gmtshake.save(outshakefile) vardict[var] = gmtshake.griddata.flatten() #write netcdf versions of coverage, shakemap, and global grids to output folder for gridname,gridfile in global_grids.items(): if not os.path.isfile(gridfile): pass try: grid = sampleGrid(gridfile,geodict) except Exception as e: print('There was an error while sampling the "%s" grid "%s". - "%s"' % (gridname,gridfile,str(e))) outgridfile = os.path.join(outfolder,gridname+'.grd') print('Saving %s to %s...' % (gridname,outgridfile)) grid.save(outgridfile) vardict[gridname] = grid.griddata.flatten() #create text file with columns of data for all predictor variables firstcols = ['lat','lon','coverage'] outmat = np.zeros((nrows*ncols,len(vardict))) for i in range(0,len(firstcols)): col = firstcols[i] outmat[:,i] = vardict[col] colidx = i+1 colnames = [] for col,column in vardict.items(): if col in firstcols: continue outmat[:,colidx] = vardict[col] colnames.append(col) colidx += 1 colnames = firstcols + colnames m,n = outmat.shape datfile = os.path.join(outfolder,'%s.dat' % ename) print('Saving all variables to data file %s...' % datfile) f = open(datfile,'wt') f.write(','.join(colnames)+'\n') for i in range(0,m): line = ','.join('%.4f' % col for col in outmat[i,:]) f.write(line+'\n') f.close()
rows = repmat(row,ncols,1).T cols = repmat(col,nrows,1) lat,lon = covgrid.getLatLon(rows,cols) #create a list of arrays that we'll dump out to a text file when done vardict = {} vardict['coverage'] = covgrid.griddata.flatten() vardict['lat'] = lat.flatten() vardict['lon'] = lon.flatten() #subset shakemap and global grids using defined bounding box and resolution shakefile = predictors['shakemap'][0] variables = predictors['shakemap'][1] for var in variables: shakemap = ShakeGrid(shakefile,var.upper()) shakemap.interpolateToGrid(geodict) gmtshake = GMTGrid() gmtshake.geodict = shakemap.geodict gmtshake.griddata = shakemap.griddata outshakefile = os.path.join(outfolder,'%s.grd' % var) print 'Saving %s to %s...' % (var,outshakefile) gmtshake.save(outshakefile) vardict[var] = gmtshake.griddata.flatten() #write netcdf versions of coverage, shakemap, and global grids to output folder for gridname,gridfile in global_grids.iteritems(): if not os.path.isfile(gridfile): pass try: grid = sampleGrid(gridfile,geodict) except Exception,e: