def filterStations(StationList, Config, Origin, network): F = [] cfg = ConfigObj(dict=Config) minDist, maxDist = cfg.FloatRange('mindist', 'maxdist') origin = Location(Origin['lat'], Origin['lon']) Logfile.red('Filter stations with configured parameters...') print 'nr networks = ', len(network) print 'nr stations = ', len(StationList) for j in network: #print j for i in StationList: if fnmatch.fnmatch(i.getcmpName(), j): pos = Location(i.lat, i.lon) sdelta = loc2degrees(origin, pos) if sdelta > minDist and sdelta < maxDist: s = Station(i.net, i.sta, i.loc, i.comp, i.lat, i.lon, i.ele, i.dip, i.azi, i.gain) if s not in F: F.append(s) Logfile.red('%d STATIONS LEFT IN LIST' % len(F)) return F
def collectSembweighted(SembList, Config, Origin, Folder, ntimes, arrays, switch, weights): ''' method to collect semblance matrizes from all processes and write them to file for each timestep ''' Logfile.add('start collect in collectSemb') cfg = ConfigObj(dict=Config) origin = ConfigObj(dict=Origin) dimX = cfg.dimX() # ('dimx') dimY = cfg.dimY() # ('dimy') winlen = cfg.winlen() # ('winlen') step = cfg.step() # ('step') latv = [] lonv = [] gridspacing = cfg.Float('gridspacing') migpoints = dimX * dimY o_lat = origin.lat() # float (Origin['lat']) o_lon = origin.lon() # float (Origin['lon']) oLatul = 0 oLonul = 0 z = 0 for i in xrange(dimX): oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing if z == 0 and i == 0: Latul = oLatul o = 0 for j in xrange(dimY): oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing if o == 0 and j == 0: Lonul = oLonul latv.append(oLatul) lonv.append(oLonul) tmp = 1 for a, w in zip(SembList, weights): tmp *= a #sys.exit() sembmaxvaluev = num.ndarray(ntimes, dtype=float) sembmaxlatv = num.ndarray(ntimes, dtype=float) sembmaxlonv = num.ndarray(ntimes, dtype=float) rc = UTCDateTime(Origin['time']) rcs = '%s-%s-%s_%02d:%02d:%02d' % (rc.day, rc.month, rc.year, rc.hour, rc.minute, rc.second) d = rc.timestamp usedarrays = 5 folder = Folder['semb'] fobjsembmax = open(os.path.join(folder, 'sembmax_%s.txt' % (switch)), 'w') for a, i in enumerate(tmp): logger.info('timestep %d' % a) fobj = open( os.path.join( folder, '%s-%s_%03d._weighted_semblance.ASC' % (switch, Origin['depth'], a)), 'w') #fobj = open (os.path.join (folder, '%03d.ASC' % a),'w') fobj.write('# %s , %s\n' % (d, rcs)) fobj.write('# step %ds| ntimes %d| winlen: %ds\n' % (step, ntimes, winlen)) fobj.write('# \n') fobj.write('# southwestlat: %.2f dlat: %f nlat: %f \n' % (Latul, gridspacing, dimX)) fobj.write('# southwestlon: %.2f dlon: %f nlon: %f \n' % (Lonul, gridspacing, dimY)) fobj.write('# ddepth: 0 ndepth: 1 \n') sembmax = 0 sembmaxX = 0 sembmaxY = 0 origin = DataTypes.dictToLocation(Origin) uncert = num.std(i) #maybe not std? for j in range(migpoints): x = latv[j] y = lonv[j] semb = i[j] fobj.write('%.2f %.2f %.20f\n' % (x, y, semb)) if semb > sembmax: sembmax = semb # search for maximum and position of maximum on semblance grid for given time step sembmaxX = x sembmaxY = y delta = loc2degrees(Location(sembmaxX, sembmaxY), origin) azi = toAzimuth(float(Origin['lat']), float(Origin['lon']), float(sembmaxX), float(sembmaxY)) sembmaxvaluev[a] = sembmax sembmaxlatv[a] = sembmaxX sembmaxlonv[a] = sembmaxY fobjsembmax.write('%d %.2f %.2f %.20f %.20f %d %03f %f %03f\n' % (a * step, sembmaxX, sembmaxY, sembmax, uncert, usedarrays, delta, float(azi), delta * 119.19)) fobj.close() fobjsembmax.close() durationpath = os.path.join(folder, "duration.txt") trigger.writeSembMaxValue(sembmaxvaluev, sembmaxlatv, sembmaxlonv, ntimes, Config, Folder) print 'DD2: ', durationpath trigger.semblancestalta(sembmaxvaluev, sembmaxlatv, sembmaxlonv)
def calcTTTAdvTauP(Config, station, Origin, flag, Xcorrshift=None, Refshift=None): cfg = ConfigObj(dict=Config) dimX = cfg.Int('dimx') dimY = cfg.Int('dimy') gridspacing = cfg.Float('gridspacing') o_lat = float(Origin['lat']) o_lon = float(Origin['lon']) o_depth = float(Origin['depth']) oLator = o_lat + dimX / 2 oLonor = o_lon + dimY / 2 oLatul = 0 oLonul = 0 TTTGridMap = {} LMINMAX = [] GridArray = {} locStation = Location(station.lat, station.lon) sdelta = loc2degrees(Location(o_lat, o_lon), locStation) Logfile.add('TTT PROCESS %d STATION: %s --> DELTA: %f' % (flag, station.getName(), sdelta)) inputpath = str(flag) + '-' + station.getName() + ".input" outputpath = str(flag) + '-' + station.getName() + ".output" errorpath = str(flag) + '-' + station.getName() + '.error' fobjinput = open(inputpath, 'w') fobjinput.write('s\n') fobjinput.write(('%s %s\n') % (station.lat, station.lon)) fobjinput.write('h\n') fobjinput.write(('%s\n') % (o_depth)) for i in xrange(dimX): oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing for j in xrange(dimY): oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing fobjinput.write('e\n') fobjinput.write(('%s %s\n') % (oLatul, oLonul)) #endfor fobjinput.close() cmd = ('taup_time -ph P -mod ak135 -time -o %s < %s > %s') % ( outputpath, inputpath, errorpath) #os.system(cmd) p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p.wait() L = [] output = open(outputpath, 'r') 'OUTPUT: ', outputpath for k in output: k = k.split() if len(k) == 1: tt = k[0].replace('\n', '') tt = float(tt) - float(Xcorrshift[station.getName()].shift) L.append(tt) #endfor output.close() z = 0 for i in xrange(dimX): oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing if z == 0 and i == 0: Latul = oLatul o = 0 for j in xrange(dimY): oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing if o == 0 and j == 0: Lonul = oLonul de = loc2degrees(Location(oLatul, oLonul), locStation) time = L[i * dimX + j] GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, time, de) LMINMAX.append(time) #endfor mint = float(min(LMINMAX)) maxt = float(max(LMINMAX)) k = MinTMaxT(mint, maxt) TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul, oLator, oLonor, GridArray) tttname = str(flag) + '-ttt.pkl' Basic.dumpToFile(tttname, TTTGridMap) Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k) try: os.remove(inputpath) os.remove(outputpath) os.remove(errorpath) except: Logfile.exception('cannot delete files')
def calcTTTAdv(Config, station, Origin, flag, arrayname, Xcorrshift=None, Refshift=None): phasename = ('%sphase') % (os.path.basename(arrayname)) cfg = ConfigObj(dict=Config) dimX = cfg.Int('dimx') dimY = cfg.Int('dimy') gridspacing = cfg.Float('gridspacing') o_lat = float(Origin['lat']) o_lon = float(Origin['lon']) o_depth = float(Origin['depth']) oLator = o_lat + dimX / 2 oLonor = o_lon + dimY / 2 oLatul = 0 oLonul = 0 #mint= 100000 #maxt=-100000 TTTGridMap = {} LMINMAX = [] GridArray = {} locStation = Location(station.lat, station.lon) sdelta = loc2degrees(Location(o_lat, o_lon), locStation) tt = obs_TravelTimes(sdelta, o_depth) for r in tt: if r['phase_name'] is Config[phasename] or r['phase_name'] == ( '%sdiff') % (Config[phasename]): station.takeoff = r['take-off angle'] ph = r['phase_name'] #endfor #15.12.2015 : rauskommentiert #Logfile.add ('STATIONINFO ' + str(station) + ' ' + str(station.takeoff) + ' ' + str(ph), # 'PROCESS %d STATION: %s --> DELTA: %f' % (flag,station,sdelta), ' ') z = 0 for i in xrange(dimX): oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing if z == 0 and i == 0: Latul = oLatul o = 0 for j in xrange(dimY): ttime = 0 oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing if o == 0 and j == 0: Lonul = oLonul de = loc2degrees(Location(oLatul, oLonul), locStation) tt = obs_TravelTimes(de, o_depth) for k in tt: if k['phase_name'] == Config[phasename] or k['phase_name'] == ( '%sdiff') % (Config[phasename]): ttime = k['time'] if int(Config['xcorr']) == 1: ttime = ttime - float( Xcorrshift[station.getName()].shift) - Refshift GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime, de) LMINMAX.append(ttime) #endfor if ttime == 0: print '\033[31mAvailable phases for station %s in range %f deegree\033[0m' % ( station, de) print '\033[31m' + '|'.join( [str(item['phase_name']) for item in tt]) + '\033[0m' print '\033[31myou tried phase %s\033[0m' % (Config[phasename]) raise Exception("\033[31mILLEGAL: phase definition\033[0m") #endif #endfor #endfor mint = min(LMINMAX) maxt = max(LMINMAX) # maxt = time TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul, oLator, oLonor, GridArray) k = MinTMaxT(mint, maxt) Basic.dumpToFile(str(flag) + '-ttt.pkl', TTTGridMap) Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k) Basic.dumpToFile('station-' + str(flag) + '.pkl', station)
def calcTTTAdv(Config, station, Origin, flag, arrayname, Xcorrshift, Refshift, phase): cfg = ConfigObj(dict=Config) if cfg.Bool('correct_shifts_empirical') is True: dimX = cfg.Int('dimx_emp') dimY = cfg.Int('dimy_emp') else: dimX = cfg.Int('dimx') dimY = cfg.Int('dimy') gridspacing = cfg.Float('gridspacing') traveltime_model = cfg.Str('traveltime_model') o_lat = float(Origin['lat']) o_lon = float(Origin['lon']) o_depth = float(Origin['depth']) oLator = o_lat + dimX / 2 oLonor = o_lon + dimY / 2 oLatul = 0 oLonul = 0 o_dip = 80. plane = False TTTGridMap = {} LMINMAX = [] GridArray = {} locStation = Location(station.lat, station.lon) sdelta = loc2degrees(Location(o_lat, o_lon), locStation) Phase = cake.PhaseDef(phase) model = cake.load_model('../data/' + traveltime_model) z = 0 if plane is True: depth = np.linspace(0., 40., num=dimY) for i in xrange(70): oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing if z == 0 and i == 0: Latul = oLatul o = 0 start_time = time.clock() for j in xrange(40): oLonul = o_lon - ( (dimY - 1) / 2) * gridspacing + j * gridspacing / np.cos(o_dip) if o == 0 and j == 0: Lonul = oLonul de = loc2degrees(Location(oLatul, oLonul), locStation) arrivals = model.arrivals([de, de], phases=Phase, zstart=depth[j] * km, zstop=0.) try: ttime = arrivals[0].t except Exception: try: arrivals = model.arrivals([de, de], phases=Phase, zstart=depth[j] * km - 2.5, zstop=depth[j] * km + 2.5, refine=True) ttime = arrivals[0].t except Exception: tt = obs_TravelTimes(de, o_depth) for k in tt: if k['phase_name'] == 'P' or k['phase_name'] == ( '%sdiff') % (Config[phasename]): ttime = k['time'] print("Something wrong with phase arrival, too large\ distances choosen?") GridArray[(i, j)] = GridElem(oLatul, oLonul, depth[j], ttime, de) LMINMAX.append(ttime) GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime, de) LMINMAX.append(ttime) if ttime == 0: raise Exception("\033[31mILLEGAL: phase definition\033[0m") else: for i in xrange(dimX): oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing if z == 0 and i == 0: Latul = oLatul o = 0 for j in xrange(dimY): oLonul = o_lon - ( (dimY - 1) / 2) * gridspacing + j * gridspacing if o == 0 and j == 0: Lonul = oLonul de = loc2degrees(Location(oLatul, oLonul), locStation) arrivals = model.arrivals([de, de], phases=Phase, zstart=o_depth * km) try: ttime = arrivals[0].t except: try: arrivals = model.arrivals([de, de], phases=Phase, zstart=o_depth * km, zstop=o_depth * km, refine=True) ttime = arrivals[0].t except: arrivals = model.arrivals([de, de], phases=Phase, zstart=o_depth * km - 2.5, zstop=o_depth * km + 2.5, refine=True) ttime = arrivals[0].t GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime, de) LMINMAX.append(ttime) GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime, de) LMINMAX.append(ttime) if ttime == 0: raise Exception("\033[31mILLEGAL: phase definition\033[0m") mint = min(LMINMAX) maxt = max(LMINMAX) TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul, oLator, oLonor, GridArray) k = MinTMaxT(mint, maxt) Basic.dumpToFile(str(flag) + '-ttt.pkl', TTTGridMap) Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k) Basic.dumpToFile('station-' + str(flag) + '.pkl', station)
def calcTTTAdv(Config, station, Origin, flag, arrayname, Xcorrshift=None, Refshift=None): phasename = ('%sphase') % (os.path.basename(arrayname)) cfg = ConfigObj(dict=Config) dimX = cfg.Int('dimx') dimY = cfg.Int('dimy') gridspacing = cfg.Float('gridspacing') o_lat = float(Origin['lat']) o_lon = float(Origin['lon']) o_depth = float(Origin['depth']) oLator = o_lat + dimX / 2 oLonor = o_lon + dimY / 2 oLatul = 0 oLonul = 0 o_dip = 80. plane = False TTTGridMap = {} LMINMAX = [] GridArray = {} locStation = Location(station.lat, station.lon) sdelta = loc2degrees(Location(o_lat, o_lon), locStation) Phase = cake.PhaseDef(Config[phasename]) model = cake.load_model() z = 0 if plane is True: depth = np.linspace(0., 40., num=dimY) for i in xrange(70): oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing if z == 0 and i == 0: Latul = oLatul o = 0 start_time = time.clock() for j in xrange(40): oLonul = o_lon - ( (dimY - 1) / 2) * gridspacing + j * gridspacing / np.cos(o_dip) if o == 0 and j == 0: Lonul = oLonul de = loc2degrees(Location(oLatul, oLonul), locStation) arrivals = model.arrivals([de, de], phases=Phase, zstart=depth[j] * km, zstop=0.) try: ttime = arrivals[0].t except: try: arrivals = model.arrivals([de, de], phases=Phase, zstart=depth[j] * km - 2.5, zstop=depth[j] * km + 2.5, refine=True) ttime = arrivals[0].t except: tt = obs_TravelTimes(de, o_depth) for k in tt: if k['phase_name'] == 'P' or k['phase_name'] == ( '%sdiff') % (Config[phasename]): ttime = k['time'] print "Something wrong with phase arrival too large\ distances choosen?" GridArray[(i, j)] = GridElem(oLatul, oLonul, depth[j], ttime, de) LMINMAX.append(ttime) if int(Config['xcorr']) == 1: ttime = ttime-float(Xcorrshift[station.getName()].shift)\ - Refshift GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime, de) LMINMAX.append(ttime) if ttime == 0: print '\033[31mAvailable phases for station %s in range %f deegree\033[0m' % ( station, de) print '\033[31m' + '|'.join( [str(item['phase_name']) for item in tt]) + '\033[0m' print '\033[31myou tried phase %s\033[0m' % ( Config[phasename]) raise Exception("\033[31mILLEGAL: phase definition\033[0m") else: for i in xrange(dimX): oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing if z == 0 and i == 0: Latul = oLatul o = 0 for j in xrange(dimY): oLonul = o_lon - ( (dimY - 1) / 2) * gridspacing + j * gridspacing if o == 0 and j == 0: Lonul = oLonul de = loc2degrees(Location(oLatul, oLonul), locStation) arrivals = model.arrivals([de, de], phases=Phase, zstart=o_depth * km) try: ttime = arrivals[0].t except: try: arrivals = model.arrivals([de, de], phases=Phase, zstart=o_depth * km - 2.5, zstop=o_depth * km - 2.5, refine=True) ttime = arrivals[0].t except: arrivals = model.arrivals([de, de], phases=Phase, zstart=o_depth * km - 2.5, zstop=0., refine=True) ttime = arrivals[0].t GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime, de) LMINMAX.append(ttime) if int(Config['xcorr']) == 1: ttime = ttime-float(Xcorrshift[station.getName()].shift)\ - Refshift GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime, de) LMINMAX.append(ttime) if ttime == 0: print '\033[31mAvailable phases for station %s in range %f deegree\033[0m' % ( station, de) print '\033[31m' + '|'.join( [str(item['phase_name']) for item in tt]) + '\033[0m' print '\033[31myou tried phase %s\033[0m' % ( Config[phasename]) raise Exception("\033[31mILLEGAL: phase definition\033[0m") mint = min(LMINMAX) maxt = max(LMINMAX) TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul, oLator, oLonor, GridArray) k = MinTMaxT(mint, maxt) Basic.dumpToFile(str(flag) + '-ttt.pkl', TTTGridMap) Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k) Basic.dumpToFile('station-' + str(flag) + '.pkl', station)