def checkStationAroundInitialCentroid(station, Config, StationMetaList): cfg = ConfigObj(dict=Config) initDist = cfg.Float('initialstationdistance') counter = 0 for i in StationMetaList: sdelta = loc2degrees(station, i) if sdelta < initDist: counter += 1 return counter
def kmean(Config, inputCentroid, FilterMeta, counter, Folder, Origin, flag): counter += 1 Logfile.add('COUNTER ' + str(counter) + ' CUTOFF ' + Config['cutoff']) cfg = ConfigObj(dict=Config) if counter == cfg.UInt('cutoff'): endcheck(inputCentroid, FilterMeta, Config, Folder, Origin, flag) sys.exit() scl = stationBelongToCluster(Config, inputCentroid, FilterMeta) #sys.exit() #print scl,len(scl) acounter = 1 for a in inputCentroid: #print a for i in scl: if acounter == i.member: delta = loc2degrees(i, a) if delta > cfg.Float('initialstationdistance'): i.member = -1 #print 'delete ',a,i,i.member,delta #b=scl.index(i) #del scl[b] #else: # print a,i,i.member,delta acounter += 1 #endfor #sys.exit() nsc = calculateClusterCentre(Config, scl) t = compareClusterCentre(inputCentroid, nsc, Config) Logfile.add('ITERATIONSTEP: ---> ' + str(counter) + ' <-----------------------------') while t != cfg.UInt('maxcluster'): Logfile.add('ANZAHL DER JO in KMEAN: ' + str(t)) kmean(Config, nsc, FilterMeta, counter, Folder, Origin, flag) endcheck(inputCentroid, FilterMeta, Config, Folder, Origin, flag) sys.exit()
def doCalc(flag, Config, WaveformDict, FilterMetaData, Gmint, Gmaxt, TTTGridMap, Folder, Origin, ntimes, switch, ev, arrayfolder, syn_in): ''' method for calculating semblance of one station array ''' Logfile.add('PROCESS %d %s' % (flag, ' Enters Semblance Calculation')) Logfile.add('MINT : %f MAXT: %f Traveltime' % (Gmint, Gmaxt)) cfg = ConfigObj(dict=Config) dimX = cfg.dimX() # ('dimx') dimY = cfg.dimY() # ('dimy') winlen = cfg.winlen() # ('winlen') step = cfg.step() # ('step') new_frequence = cfg.newFrequency() #('new_frequence') forerun = cfg.Int('forerun') duration = cfg.Int('duration') gridspacing = cfg.Float('gridspacing') nostat = len(WaveformDict) traveltimes = {} recordstarttime = '' minSampleCount = 999999999 ntimes = int((forerun + duration) / step) nsamp = int(winlen * new_frequence) nstep = int(step * new_frequence) from pyrocko import obspy_compat from pyrocko import orthodrome, model obspy_compat.plant() ############################################################################ calcStreamMap = WaveformDict stations = [] py_trs = [] for trace in calcStreamMap.iterkeys(): py_tr = obspy_compat.to_pyrocko_trace(calcStreamMap[trace]) py_trs.append(py_tr) for il in FilterMetaData: if str(il) == str(trace): szo = model.Station(lat=il.lat, lon=il.lon, station=il.sta, network=il.net, channels=py_tr.channel, elevation=il.ele, location=il.loc) stations.append(szo) #right number of stations? #==================================synthetic BeamForming======================================= if cfg.Bool('shift_by_phase_pws') == True: calcStreamMapshifted = calcStreamMap.copy() from obspy.core import stream stream = stream.Stream() for trace in calcStreamMapshifted.iterkeys(): stream.append(calcStreamMapshifted[trace]) pws_stack = PWS_stack([stream], weight=2, normalize=True) for tr in pws_stack: for trace in calcStreamMapshifted.iterkeys(): calcStreamMapshifted[trace] = tr calcStreamMap = calcStreamMapshifted if cfg.Bool('shift_by_phase_onset') == True: pjoin = os.path.join timeev = util.str_to_time(ev.time) trs_orgs = [] calcStreamMapshifted = calcStreamMap.copy() for trace in calcStreamMapshifted.iterkeys(): tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace]) trs_orgs.append(tr_org) timing = CakeTiming( phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20', fallback_time=100.) traces = trs_orgs event = model.Event(lat=float(ev.lat), lon=float(ev.lon), depth=ev.depth * 1000., time=timeev) directory = arrayfolder bf = BeamForming(stations, traces, normalize=True) shifted_traces = bf.process(event=event, timing=timing, fn_dump_center=pjoin( directory, 'array_center.pf'), fn_beam=pjoin(directory, 'beam.mseed')) i = 0 store_id = syn_in.store() engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()]) for trace in calcStreamMapshifted.iterkeys(): recordstarttime = calcStreamMapshifted[ trace].stats.starttime.timestamp recordendtime = calcStreamMapshifted[trace].stats.endtime.timestamp mod = shifted_traces[i] extracted = mod.chop(recordstarttime, recordendtime, inplace=False) shifted_obs_tr = obspy_compat.to_obspy_trace(extracted) calcStreamMapshifted[trace] = shifted_obs_tr i = i + 1 calcStreamMap = calcStreamMapshifted weight = 0. if cfg.Bool('weight_by_noise') == True: from noise_analyser import analyse pjoin = os.path.join timeev = util.str_to_time(ev.time) trs_orgs = [] calcStreamMapshifted = calcStreamMap.copy() for trace in calcStreamMapshifted.iterkeys(): tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace]) trs_orgs.append(tr_org) timing = CakeTiming( phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20', fallback_time=100.) traces = trs_orgs event = model.Event(lat=float(ev.lat), lon=float(ev.lon), depth=ev.depth * 1000., time=timeev) directory = arrayfolder bf = BeamForming(stations, traces, normalize=True) shifted_traces = bf.process(event=event, timing=timing, fn_dump_center=pjoin( directory, 'array_center.pf'), fn_beam=pjoin(directory, 'beam.mseed')) i = 0 store_id = syn_in.store() engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()]) weight = analyse(shifted_traces, engine, event, stations, 100., store_id, nwindows=1, check_events=True, phase_def='P') for trace in calcStreamMap.iterkeys(): recordstarttime = calcStreamMap[trace].stats.starttime d = calcStreamMap[trace].stats.starttime d = d.timestamp if calcStreamMap[trace].stats.npts < minSampleCount: minSampleCount = calcStreamMap[trace].stats.npts ############################################################################ traces = num.ndarray(shape=(len(calcStreamMap), minSampleCount), dtype=float) traveltime = num.ndarray(shape=(len(calcStreamMap), dimX * dimY), dtype=float) latv = num.ndarray(dimX * dimY, dtype=float) lonv = num.ndarray(dimX * dimY, dtype=float) ############################################################################ c = 0 streamCounter = 0 for key in calcStreamMap.iterkeys(): streamID = key c2 = 0 for o in calcStreamMap[key]: if c2 < minSampleCount: traces[c][c2] = o c2 += 1 for key in TTTGridMap.iterkeys(): if streamID == key: traveltimes[streamCounter] = TTTGridMap[key] else: "NEIN", streamID, key if not streamCounter in traveltimes: continue #hs : thread crashed before g = traveltimes[streamCounter] dimZ = g.dimZ mint = g.mint maxt = g.maxt Latul = g.Latul Lonul = g.Lonul Lator = g.Lator Lonor = g.Lonor gridElem = g.GridArray for x in range(dimX): for y in range(dimY): elem = gridElem[x, y] traveltime[c][x * dimY + y] = elem.tt latv[x * dimY + y] = elem.lat lonv[x * dimY + y] = elem.lon #endfor c += 1 streamCounter += 1 #endfor ############################## CALCULATE PARAMETER FOR SEMBLANCE CALCULATION ################## nsamp = winlen * new_frequence nstep = int(step * new_frequence) migpoints = dimX * dimY dimZ = 0 new_frequence = cfg.newFrequency() # ['new_frequence'] maxp = int(Config['ncore']) Logfile.add('PROCESS %d NTIMES: %d' % (flag, ntimes)) if False: print('nostat ', nostat, type(nostat)) print('nsamp ', nsamp, type(nsamp)) print('ntimes ', ntimes, type(ntimes)) print('nstep ', nstep, type(nstep)) print('dimX ', dimX, type(dimX)) print('dimY ', dimY, type(dimY)) print('mint ', Gmint, type(mint)) print('new_freq ', new_frequence, type(new_frequence)) print('minSampleCount ', minSampleCount, type(minSampleCount)) print('latv ', latv, type(latv)) print('traces', traces, type(traces)) print('traveltime', traveltime, type(traveltime)) t1 = time.time() traces_org = traces.reshape(1, nostat * minSampleCount) traveltime_org = traveltime.reshape(1, nostat * dimX * dimY) USE_C_CODE = True try: if USE_C_CODE: import Cm import CTrig start_time = time.time() k = Cm.otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint, new_frequence, minSampleCount, latv, lonv, traveltime_org, traces_org) print("--- %s seconds ---" % (time.time() - start_time)) else: start_time = time.time() k = otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint, new_frequence, minSampleCount, latv, lonv, traveltime_org, traces_org) #hs print("--- %s seconds ---" % (time.time() - start_time)) except: print "loaded tttgrid has probably wrong dimensions or stations, delete\ ttgrid or exchange" t2 = time.time() Logfile.add('%s took %0.3f s' % ('CALC:', (t2 - t1))) partSemb = k partSemb_data = partSemb.reshape(ntimes, migpoints) return partSemb_data
def doCalc_syn(flag, Config, WaveformDict, FilterMetaData, Gmint, Gmaxt, TTTGridMap, Folder, Origin, ntimes, switch, ev, arrayfolder, syn_in, parameter): ''' method for calculating semblance of one station array ''' Logfile.add('PROCESS %d %s' % (flag, ' Enters Semblance Calculation')) Logfile.add('MINT : %f MAXT: %f Traveltime' % (Gmint, Gmaxt)) cfg = ConfigObj(dict=Config) dimX = cfg.dimX() # ('dimx') dimY = cfg.dimY() # ('dimy') winlen = cfg.winlen() # ('winlen') step = cfg.step() # ('step') new_frequence = cfg.newFrequency() #('new_frequence') forerun = cfg.Int('forerun') duration = cfg.Int('duration') gridspacing = cfg.Float('gridspacing') nostat = len(WaveformDict) traveltimes = {} recordstarttime = '' minSampleCount = 999999999 ntimes = int((forerun + duration) / step) nsamp = int(winlen * new_frequence) nstep = int(step * new_frequence) from pyrocko import obspy_compat from pyrocko import orthodrome, model obspy_compat.plant() ############################################################################ calcStreamMap = WaveformDict stations = [] py_trs = [] for trace in calcStreamMap.iterkeys(): py_tr = obspy_compat.to_pyrocko_trace(calcStreamMap[trace]) py_trs.append(py_tr) for il in FilterMetaData: if str(il) == str(trace): szo = model.Station(lat=il.lat, lon=il.lon, station=il.sta, network=il.net, channels=py_tr.channel, elevation=il.ele, location=il.loc) stations.append(szo) #right number of stations? store_id = syn_in.store() engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()]) targets = [] for st in stations: target = Target(lat=st.lat, lon=st.lon, store_id=store_id, codes=(st.network, st.station, st.location, 'BHZ'), tmin=-1900, tmax=3900, interpolation='multilinear', quantity=cfg.quantity()) targets.append(target) if syn_in.nsources() == 1: if syn_in.use_specific_stf() is True: stf = syn_in.stf() exec(stf) else: stf = STF() if syn_in.source() == 'RectangularSource': source = RectangularSource( lat=float(syn_in.lat_0()), lon=float(syn_in.lon_0()), depth=syn_in.depth_syn_0() * 1000., strike=syn_in.strike_0(), dip=syn_in.dip_0(), rake=syn_in.rake_0(), width=syn_in.width_0() * 1000., length=syn_in.length_0() * 1000., nucleation_x=syn_in.nucleation_x_0(), slip=syn_in.slip_0(), nucleation_y=syn_in.nucleation_y_0(), stf=stf, time=util.str_to_time(syn_in.time_0())) if syn_in.source() == 'DCSource': source = DCSource(lat=float(syn_in.lat_0()), lon=float(syn_in.lon_0()), depth=syn_in.depth_syn_0() * 1000., strike=syn_in.strike_0(), dip=syn_in.dip_0(), rake=syn_in.rake_0(), stf=stf, time=util.str_to_time(syn_in.time_0()), magnitude=syn_in.magnitude_0()) else: sources = [] for i in range(syn_in.nsources()): if syn_in.use_specific_stf() is True: stf = syn_in.stf() exec(stf) else: stf = STF() if syn_in.source() == 'RectangularSource': sources.append( RectangularSource( lat=float(syn_in.lat_1(i)), lon=float(syn_in.lon_1(i)), depth=syn_in.depth_syn_1(i) * 1000., strike=syn_in.strike_1(i), dip=syn_in.dip_1(i), rake=syn_in.rake_1(i), width=syn_in.width_1(i) * 1000., length=syn_in.length_1(i) * 1000., nucleation_x=syn_in.nucleation_x_1(i), slip=syn_in.slip_1(i), nucleation_y=syn_in.nucleation_y_1(i), stf=stf, time=util.str_to_time(syn_in.time_1(i)))) if syn_in.source() == 'DCSource': sources.append( DCSource(lat=float(syn_in.lat_1(i)), lon=float(syn_in.lon_1(i)), depth=syn_in.depth_1(i) * 1000., strike=syn_in.strike_1(i), dip=syn_in.dip_1(i), rake=syn_in.rake_1(i), stf=stf, time=util.str_to_time(syn_in.time_1(i)), magnitude=syn_in.magnitude_1(i))) source = CombiSource(subsources=sources) response = engine.process(source, targets) synthetic_traces = response.pyrocko_traces() if cfg.Bool('synthetic_test_add_noise') is True: from noise_addition import add_noise trs_orgs = [] calcStreamMapsyn = calcStreamMap.copy() #from pyrocko import trace for tracex in calcStreamMapsyn.iterkeys(): for trl in synthetic_traces: if str(trl.name()[4:12]) == str(tracex[4:]): tr_org = obspy_compat.to_pyrocko_trace( calcStreamMapsyn[tracex]) tr_org.downsample_to(2.0) trs_orgs.append(tr_org) store_id = syn_in.store() engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()]) synthetic_traces = add_noise(trs_orgs, engine, source.pyrocko_event(), stations, store_id, phase_def='P') trs_org = [] trs_orgs = [] fobj = os.path.join(arrayfolder, 'shift.dat') xy = num.loadtxt(fobj, usecols=1, delimiter=',') calcStreamMapsyn = calcStreamMap.copy() #from pyrocko import trace for tracex in calcStreamMapsyn.iterkeys(): for trl in synthetic_traces: if str(trl.name()[4:12]) == str(tracex[4:]): mod = trl recordstarttime = calcStreamMapsyn[ tracex].stats.starttime.timestamp recordendtime = calcStreamMapsyn[ tracex].stats.endtime.timestamp tr_org = obspy_compat.to_pyrocko_trace( calcStreamMapsyn[tracex]) trs_orgs.append(tr_org) tr_org_add = mod.chop(recordstarttime, recordendtime, inplace=False) synthetic_obs_tr = obspy_compat.to_obspy_trace(tr_org_add) calcStreamMapsyn[tracex] = synthetic_obs_tr trs_org.append(tr_org_add) calcStreamMap = calcStreamMapsyn if cfg.Bool('shift_by_phase_pws') == True: calcStreamMapshifted = calcStreamMap.copy() from obspy.core import stream stream = stream.Stream() for trace in calcStreamMapshifted.iterkeys(): stream.append(calcStreamMapshifted[trace]) pws_stack = PWS_stack([stream], weight=2, normalize=True) for tr in pws_stack: for trace in calcStreamMapshifted.iterkeys(): calcStreamMapshifted[trace] = tr calcStreamMap = calcStreamMapshifted if cfg.Bool('shift_by_phase_onset') == True: pjoin = os.path.join timeev = util.str_to_time(ev.time) trs_orgs = [] calcStreamMapshifted = calcStreamMap.copy() for trace in calcStreamMapshifted.iterkeys(): tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace]) trs_orgs.append(tr_org) timing = CakeTiming( phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20', fallback_time=100.) traces = trs_orgs event = model.Event(lat=float(ev.lat), lon=float(ev.lon), depth=ev.depth * 1000., time=timeev) directory = arrayfolder bf = BeamForming(stations, traces, normalize=True) shifted_traces = bf.process(event=event, timing=timing, fn_dump_center=pjoin( directory, 'array_center.pf'), fn_beam=pjoin(directory, 'beam.mseed')) i = 0 store_id = syn_in.store() engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()]) for trace in calcStreamMapshifted.iterkeys(): recordstarttime = calcStreamMapshifted[ trace].stats.starttime.timestamp recordendtime = calcStreamMapshifted[trace].stats.endtime.timestamp mod = shifted_traces[i] extracted = mod.chop(recordstarttime, recordendtime, inplace=False) shifted_obs_tr = obspy_compat.to_obspy_trace(extracted) calcStreamMapshifted[trace] = shifted_obs_tr i = i + 1 calcStreamMap = calcStreamMapshifted weight = 0. if cfg.Bool('weight_by_noise') == True: from noise_analyser import analyse pjoin = os.path.join timeev = util.str_to_time(ev.time) trs_orgs = [] calcStreamMapshifted = calcStreamMap.copy() for trace in calcStreamMapshifted.iterkeys(): tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace]) trs_orgs.append(tr_org) timing = CakeTiming( phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20', fallback_time=100.) traces = trs_orgs event = model.Event(lat=float(ev.lat), lon=float(ev.lon), depth=ev.depth * 1000., time=timeev) directory = arrayfolder bf = BeamForming(stations, traces, normalize=True) shifted_traces = bf.process(event=event, timing=timing, fn_dump_center=pjoin( directory, 'array_center.pf'), fn_beam=pjoin(directory, 'beam.mseed')) i = 0 store_id = syn_in.store() engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()]) weight = analyse(shifted_traces, engine, event, stations, 100., store_id, nwindows=1, check_events=True, phase_def='P') for trace in calcStreamMap.iterkeys(): recordstarttime = calcStreamMap[trace].stats.starttime d = calcStreamMap[trace].stats.starttime d = d.timestamp if calcStreamMap[trace].stats.npts < minSampleCount: minSampleCount = calcStreamMap[trace].stats.npts ############################################################################ traces = num.ndarray(shape=(len(calcStreamMap), minSampleCount), dtype=float) traveltime = num.ndarray(shape=(len(calcStreamMap), dimX * dimY), dtype=float) latv = num.ndarray(dimX * dimY, dtype=float) lonv = num.ndarray(dimX * dimY, dtype=float) ############################################################################ c = 0 streamCounter = 0 for key in calcStreamMap.iterkeys(): streamID = key c2 = 0 for o in calcStreamMap[key]: if c2 < minSampleCount: traces[c][c2] = o c2 += 1 for key in TTTGridMap.iterkeys(): if streamID == key: traveltimes[streamCounter] = TTTGridMap[key] else: "NEIN", streamID, key if not streamCounter in traveltimes: continue #hs : thread crashed before g = traveltimes[streamCounter] dimZ = g.dimZ mint = g.mint maxt = g.maxt Latul = g.Latul Lonul = g.Lonul Lator = g.Lator Lonor = g.Lonor gridElem = g.GridArray for x in range(dimX): for y in range(dimY): elem = gridElem[x, y] traveltime[c][x * dimY + y] = elem.tt latv[x * dimY + y] = elem.lat lonv[x * dimY + y] = elem.lon #endfor c += 1 streamCounter += 1 #endfor ############################## CALCULATE PARAMETER FOR SEMBLANCE CALCULATION ################## nsamp = winlen * new_frequence nstep = int(step * new_frequence) migpoints = dimX * dimY dimZ = 0 new_frequence = cfg.newFrequency() # ['new_frequence'] maxp = int(Config['ncore']) Logfile.add('PROCESS %d NTIMES: %d' % (flag, ntimes)) if False: print('nostat ', nostat, type(nostat)) print('nsamp ', nsamp, type(nsamp)) print('ntimes ', ntimes, type(ntimes)) print('nstep ', nstep, type(nstep)) print('dimX ', dimX, type(dimX)) print('dimY ', dimY, type(dimY)) print('mint ', Gmint, type(mint)) print('new_freq ', new_frequence, type(new_frequence)) print('minSampleCount ', minSampleCount, type(minSampleCount)) print('latv ', latv, type(latv)) print('traces', traces, type(traces)) print('traveltime', traveltime, type(traveltime)) #==================================semblance calculation======================================== t1 = time.time() traces = traces.reshape(1, nostat * minSampleCount) traveltime = traveltime.reshape(1, nostat * dimX * dimY) USE_C_CODE = True try: if USE_C_CODE: import Cm import CTrig start_time = time.time() k = Cm.otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint, new_frequence, minSampleCount, latv, lonv, traveltime, traces) print("--- %s seconds ---" % (time.time() - start_time)) else: start_time = time.time() k = otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint, new_frequence, minSampleCount, latv, lonv, traveltime, traces) #hs print("--- %s seconds ---" % (time.time() - start_time)) except: print "loaded tttgrid has probably wrong dimensions or stations, delete\ ttgrid or exchange" t2 = time.time() partSemb = k partSemb_syn = partSemb.reshape(ntimes, migpoints) return partSemb_syn
def writeSembMatricesSingleArray(SembList, Config, Origin, arrayfolder, ntimes, switch): ''' method to write semblance matrizes from one processes to file for each timestep ''' logger.info('start write semblance matrices') cfg = ConfigObj(dict=Config) origin = OriginCfg(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) #endfor 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 for a, i in enumerate(SembList): #logger.info('timestep %d' % a) fobj = open( os.path.join(arrayfolder, '%s-%s_%03d.ASC' % (switch, Origin['depth'], 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') for j in range(migpoints): x = latv[j] y = lonv[j] z = origin.depth() # float(Origin['depth']) semb = i[j] fobj.write('%.2f %.2f %.2f %.20f\n' % (x, y, z, semb)) #endfor fobj.close()
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 collectSemb(SembList, Config, Origin, Folder, ntimes, arrays, switch, array_centers): ''' 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') if switch == 0: winlen = cfg.winlen() #('winlen') step = cfg.step() #('step') if switch == 1: winlen = cfg.winlen_f2() #('winlen') step = cfg.step_f2() #('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 origin = DataTypes.dictToLocation(Origin) i = 0 #for a in SembList: # tmp = num.zeros(num.shape(a)) azis = [] for a in SembList: x = array_centers[i][0] y = array_centers[i][1] delta = orthodrome.distance_accurate50m_numpy(x, y, origin.lat, origin.lon) #a = a*((1./delta**2)*1.e+15) tmp *= a #azis.append(toAzimuth(float(Origin['lat']), float(Origin['lon']),x, y)) i = i + 1 #min_coor = num.zeros([i,2]) #i = 0 #for a in SembList: # deltas = [] # x = array_centers[i][0] # y = array_centers[i][1] # for k in range(0,len(latv)): # delta = orthodrome.distance_accurate50m_numpy(x, y, latv[k], lonv[k]) # deltas.append(orthodrome.distance_accurate50m_numpy(x, y, latv[k], lonv[k])) # if delta <= num.min(deltas): # min_coor[i]= [latv[k], lonv[k]] # i = i+1 # array_overlap = num.average(min_coor, axis=0) # delta_center = orthodrome.distance_accurate50m_numpy(array_overlap[0], array_overlap[1], origin.lat, origin.lon) # print(array_overlap) # print(delta_center) # diff_center_lat = origin.lat-array_overlap[0] # diff_center_lon = origin.lon-array_overlap[1] # print(diff_center_lat) # print(diff_center_lon) #for a in SembList: #if num.mean(a)>0: # tmp *= a 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 = arrays folder = Folder['semb'] fobjsembmax = open(os.path.join(folder, 'sembmax_%s.txt' % (switch)), 'w') norm = num.max(num.max(tmp, axis=1)) max_p = 0. sum_i = 0. for a, i in enumerate(tmp): if a < 1: sum_i *= i for a, i in enumerate(tmp): if a < 1: max = num.max(sum_i[:]) for j in range(migpoints): if i[j] > num.max(i[:]) * 0.9 and i[j] > max_p: latvmax = latv[j] lonvmax = lonv[j] max_p = i[j] # delta_lat = origin.lat-latvmax # delta_lon = origin.lon-lonvmax #for a, i in enumerate(tmp): # max_pos = [l for l, k in enumerate(i) if k == i.max()][0] # delta_lat = origin.lat-latv[max_pos] # delta_lon = origin.lon-lonv[max_pos] for j in range(migpoints): latv[j] = latv[j] #+delta_lat lonv[j] = lonv[j] #+delta_lon # latv.append(latv[j]-delta_lat) # lonv.append(lonv[j]-delta_lon) #nix = [] #for a, i in enumerate(tmp): # for j in range(migpoints): # if i[j]/norm > num.max(sum_i/norm)*0.4: # if j in nix: # pass # else: # latv[j] = latv[j]+delta_lat # lonv[j] = lonv[j]+delta_lon # nix.append(j) #if i[j]/norm > num.max(sum_i/norm)*0.4: # print('yes') # delta_lat = origin.lat-latv[j] # delta_lon = origin.lon-lonv[j] # print delta_lat, delta_lon, latvmax, lonvmax # print latv[j], lonv[j], origin.lat, origin.lon # ix = num.where(latv[j]+delta_lat)[0][0] # iy = num.where(lonv[j]+delta_lon)[0][0] # lat = latv[j].copy() # lon = lonv[j].copy() # latv[j] = latv[ix] ## lonv[j] = lonv[iy] # lonv[iy] # #latv[j] = latv[j]+delta_lat #lonv[j] = lonv[j]+delta_lon # print latv[j], lonv[j] # for a, i in enumerate(tmp): logger.info('timestep %d' % a) print(a) fobj = open( os.path.join(folder, '%s-%s_%03d.ASC' % (switch, Origin['depth'], 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 uncert = num.std(i) #maybe not std? for j in range(migpoints): x = latv[j] #+delta_lat y = lonv[j] #+delta_lon # if i[j]/norm > num.max(i[:]/norm)*0.1: # delta_lat = origin.lat-latv[max_pos] # delta_lon = origin.lon-lonv[max_pos] # print delta_lat, delta_lon, latv[max_pos], lonv[max_pos] # print latv[j], lonv[j], origin.lat, origin.lon # x = latv[j]+delta_lat # y = lonv[j]+delta_lon # print x, y semb = i[j] / norm fobj.write('%.2f %.2f %.20f\n' % (x, y, semb)) # xd= latv[j]-delta_lat # yd= lonv[j]-delta_lon # sembd = 0. # fobj.write('%.2f %.2f %.20f\n' %(xd,yd,sembd)) if semb > sembmax: sembmax = semb # search for maximum and position of maximum on semblance grid for given time step sembmaxX = x sembmaxY = y delta = orthodrome.distance_accurate50m_numpy(x, y, origin.lat, origin.lon) azi = toAzimuth(float(Origin['lat']), float(Origin['lon']), float(sembmaxX), float(sembmaxY)) sembmaxvaluev[a] = sembmax sembmaxlatv[a] = sembmaxX sembmaxlonv[a] = sembmaxY fobjsembmax.write('%d %.3f %.3f %.30f %.30f %d %03f %f %03f\n' % (a * step, sembmaxX, sembmaxY, sembmax, uncert, usedarrays, delta, float(azi), delta * 119.19)) fobj.close() fobjsembmax.close() trigger.writeSembMaxValue(sembmaxvaluev, sembmaxlatv, sembmaxlonv, ntimes, Config, Folder) inspect_semb = cfg.Bool('inspect_semb') if inspect_semb is True: trigger.semblancestalta(sembmaxvaluev, sembmaxlatv, sembmaxlonv) return sembmaxvaluev
def doCalc(flag, Config, WaveformDict, FilterMetaData, Gmint, Gmaxt, TTTGridMap, Folder, Origin, ntimes): ''' method for calculating semblance of one station array ''' Logfile.add('PROCESS %d %s' % (flag, ' Enters Semblance Calculation')) Logfile.add('MINT : %f MAXT: %f Traveltime' % (Gmint, Gmaxt)) cfg = ConfigObj(dict=Config) dimX = cfg.dimX() # ('dimx') dimY = cfg.dimY() # ('dimy') winlen = cfg.winlen() # ('winlen') step = cfg.step() # ('step') new_frequence = cfg.newFrequency() # ('new_frequence') forerun = cfg.Int('forerun') duration = cfg.Int('duration') gridspacing = cfg.Float('gridspacing') nostat = len(WaveformDict) traveltimes = {} recordstarttime = '' minSampleCount = 999999999 ntimes = int((forerun + duration) / step) nsamp = int(winlen * new_frequence) nstep = int(step * new_frequence) #for i in WaveformDict.iterkeys(): # print i,WaveformDict[i] ############################################################################ calcStreamMap = WaveformDict for trace in calcStreamMap.iterkeys(): recordstarttime = calcStreamMap[trace].stats.starttime d = calcStreamMap[trace].stats.starttime d = d.timestamp if calcStreamMap[trace].stats.npts < minSampleCount: minSampleCount = calcStreamMap[trace].stats.npts ############################################################################ traces = np.ndarray(shape=(len(calcStreamMap), minSampleCount), dtype=float) traveltime = np.ndarray(shape=(len(calcStreamMap), dimX * dimY), dtype=float) latv = np.ndarray(dimX * dimY, dtype=float) lonv = np.ndarray(dimX * dimY, dtype=float) ############################################################################ #traces = np.ndarray (nostat*minSampleCount,dtype=float) #traveltimes = np.ndarray (nostat*dimX*dimY,dtype=float) #latv = np.ndarray (dimX*dimY,dtype=float) #lonv = np.ndarray (dimX*dimY,dtype=float) #print 'minSC: ',minSampleCount,' LCSM: ',len(calcStreamMap) c = 0 streamCounter = 0 for key in calcStreamMap.iterkeys(): streamID = key c2 = 0 #print streamID, len(calcStreamMap[key]),minSampleCount for o in calcStreamMap[key]: if c2 < minSampleCount: traces[c][c2] = o #print 'C: ',c,' C2: ',c2,' TRACES:',traces[c][c2] c2 += 1 #endfor for key in TTTGridMap.iterkeys(): if streamID == key: traveltimes[streamCounter] = TTTGridMap[key] else: "NEIN", streamID, key #endfor if not streamCounter in traveltimes: continue #hs : thread crashed before g = traveltimes[streamCounter] dimZ = g.dimZ mint = g.mint maxt = g.maxt Latul = g.Latul Lonul = g.Lonul Lator = g.Lator Lonor = g.Lonor gridElem = g.GridArray for x in range(dimX): for y in range(dimY): elem = gridElem[x, y] traveltime[c][x * dimY + y] = elem.tt latv[x * dimY + y] = elem.lat lonv[x * dimY + y] = elem.lon #endfor c += 1 streamCounter += 1 #endfor ############################## CALCULATE PARAMETER FOR SEMBLANCE CALCULATION ################## nsamp = winlen * new_frequence nstep = int(step * new_frequence) migpoints = dimX * dimY dimZ = 0 new_frequence = cfg.newFrequency() # ['new_frequence'] maxp = int(Config['ncore']) #maxp = 20 #hs Logfile.add('PROCESS %d NTIMES: %d' % (flag, ntimes)) #k = Csemblance.semb(flag,nostat,nsamp,ntimes,nstep,Gmint,Gmaxt,Lonul,Latul,minSampleCount, # dimZ,dimX,dimY,new_frequence,ntimesstart,ntimesend,winlen,step,gridspacing, # latv,lonv,traveltime,traces,backveclen) #k = sembPar.semb (flag,nostat,nsamp,ntimes,nstep,Gmint,Gmaxt,Lonul,Latul,minSampleCount,dimZ, # dimX,dimY,new_frequence,ntimesstart,ntimesend,winlen,step,gridspacing,latv, # lonv,traveltime,traces,backveclen) if False: print('nostat ', nostat, type(nostat)) print('nsamp ', nsamp, type(nsamp)) print('ntimes ', ntimes, type(ntimes)) print('nstep ', nstep, type(nstep)) print('dimX ', dimX, type(dimX)) print('dimY ', dimY, type(dimY)) print('mint ', Gmint, type(mint)) print('new_freq ', new_frequence, type(new_frequence)) print('minSampleCount ', minSampleCount, type(minSampleCount)) print('latv ', latv, type(latv)) print('traces', traces, type(traces)) print('traveltime', traveltime, type(traveltime)) traveltime = traveltime.reshape(1, nostat * dimX * dimY) traces = traces.reshape(1, nostat * minSampleCount) #print 'traveltime2',traveltime,type(traveltime) t1 = time.time() if USE_C_CODE: k = Cm.otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint, new_frequence, minSampleCount, latv, lonv, traveltime, traces) else: k = otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint, new_frequence, minSampleCount, latv, lonv, traveltime, traces) #hs t2 = time.time() Logfile.add('%s took %0.3f s' % ('CALC:', (t2 - t1))) #print 'K',k,len(k),' MUST ',ntimes*dimX*dimY,' RES ',k[1] partSemb = k #partSemb = partSemb.reshape (1,migpoints) partSemb = partSemb.reshape(ntimes, migpoints) #print 'PARTSEMB FLAG: ',partSemb,type(partSemb),partSemb.ndim return partSemb
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 doCalc_syn (flag,Config,WaveformDict,FilterMetaData,Gmint,Gmaxt,TTTGridMap, Folder,Origin, ntimes, switch, ev,arrayfolder, syn_in, parameter): ''' method for calculating semblance of one station array ''' Logfile.add ('PROCESS %d %s' % (flag,' Enters Semblance Calculation') ) Logfile.add ('MINT : %f MAXT: %f Traveltime' % (Gmint,Gmaxt)) cfg = ConfigObj (dict=Config) dimX = cfg.dimX() # ('dimx') dimY = cfg.dimY() # ('dimy') winlen = cfg.winlen () # ('winlen') step = cfg.step() # ('step') new_frequence = cfg.newFrequency() #('new_frequence') forerun= cfg.Int('forerun') duration= cfg.Int('duration') gridspacing = cfg.Float('gridspacing') nostat = len (WaveformDict) traveltimes = {} recordstarttime = '' minSampleCount = 999999999 ntimes = int ((forerun + duration)/step) nsamp = int (winlen * new_frequence) nstep = int (step * new_frequence) from pyrocko import obspy_compat from pyrocko import orthodrome, model obspy_compat.plant() ############################################################################ calcStreamMap = WaveformDict stations = [] py_trs = [] for trace in calcStreamMap.iterkeys(): py_tr = obspy_compat.to_pyrocko_trace(calcStreamMap[trace]) py_trs.append(py_tr) for il in FilterMetaData: if str(il) == str(trace): szo = model.Station(lat=il.lat, lon=il.lon, station=il.sta, network=il.net, channels=py_tr.channel, elevation=il.ele, location=il.loc) stations.append(szo) #right number of stations? store_id = syn_in.store() engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()]) targets = [] for st in stations: target = Target( lat=st.lat, lon=st.lon, store_id=store_id, codes=(st.network, st.station, st.location, 'BHZ'), tmin=-1900, tmax=3900, interpolation='multilinear', quantity=cfg.quantity()) targets.append(target) if syn_in.nsources() == 1: if syn_in.use_specific_stf() is True: stf = syn_in.stf() exec(stf) else: stf = STF() if syn_in.source() == 'RectangularSource': source = RectangularSource( lat=float(syn_in.lat_0()), lon=float(syn_in.lon_0()), depth=syn_in.depth_syn_0()*1000., strike=syn_in.strike_0(), dip=syn_in.dip_0(), rake=syn_in.rake_0(), width=syn_in.width_0()*1000., length=syn_in.length_0()*1000., nucleation_x=syn_in.nucleation_x_0(), slip=syn_in.slip_0(), nucleation_y=syn_in.nucleation_y_0(), stf=stf, time=util.str_to_time(syn_in.time_0())) if syn_in.source() == 'DCSource': source = DCSource( lat=float(syn_in.lat_0()), lon=float(syn_in.lon_0()), depth=syn_in.depth_syn_0()*1000., strike=syn_in.strike_0(), dip=syn_in.dip_0(), rake=syn_in.rake_0(), stf=stf, time=util.str_to_time(syn_in.time_0()), magnitude=syn_in.magnitude_0()) else: sources = [] for i in range(syn_in.nsources()): if syn_in.use_specific_stf() is True: stf = syn_in.stf() exec(stf) else: stf = STF() if syn_in.source() == 'RectangularSource': sources.append(RectangularSource( lat=float(syn_in.lat_1(i)), lon=float(syn_in.lon_1(i)), depth=syn_in.depth_syn_1(i)*1000., strike=syn_in.strike_1(i), dip=syn_in.dip_1(i), rake=syn_in.rake_1(i), width=syn_in.width_1(i)*1000., length=syn_in.length_1(i)*1000., nucleation_x=syn_in.nucleation_x_1(i), slip=syn_in.slip_1(i), nucleation_y=syn_in.nucleation_y_1(i), stf=stf, time=util.str_to_time(syn_in.time_1(i)))) if syn_in.source() == 'DCSource': sources.append(DCSource( lat=float(syn_in.lat_1(i)), lon=float(syn_in.lon_1(i)), depth=syn_in.depth_1(i)*1000., strike=syn_in.strike_1(i), dip=syn_in.dip_1(i), rake=syn_in.rake_1(i), stf=stf, time=util.str_to_time(syn_in.time_1(i)), magnitude=syn_in.magnitude_1(i))) source = CombiSource(subsources=sources) response = engine.process(source, targets) synthetic_traces = response.pyrocko_traces() if cfg.Bool('synthetic_test_add_noise') is True: from noise_addition import add_noise trs_orgs = [] calcStreamMapsyn = calcStreamMap.copy() #from pyrocko import trace for tracex in calcStreamMapsyn.iterkeys(): for trl in synthetic_traces: if str(trl.name()[4:12])== str(tracex[4:]): tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapsyn[tracex]) tr_org.downsample_to(2.0) trs_orgs.append(tr_org) store_id = syn_in.store() engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()]) synthetic_traces = add_noise(trs_orgs, engine, source.pyrocko_event(), stations, store_id, phase_def='P') trs_org = [] trs_orgs = [] fobj = os.path.join(arrayfolder, 'shift.dat') xy = num.loadtxt(fobj, usecols=1, delimiter=',') calcStreamMapsyn = calcStreamMap.copy() #from pyrocko import trace for tracex in calcStreamMapsyn.iterkeys(): for trl in synthetic_traces: if str(trl.name()[4:12])== str(tracex[4:]): mod = trl recordstarttime = calcStreamMapsyn[tracex].stats.starttime.timestamp recordendtime = calcStreamMapsyn[tracex].stats.endtime.timestamp tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapsyn[tracex]) trs_orgs.append(tr_org) tr_org_add = mod.chop(recordstarttime, recordendtime, inplace=False) synthetic_obs_tr = obspy_compat.to_obspy_trace(tr_org_add) calcStreamMapsyn[tracex] = synthetic_obs_tr trs_org.append(tr_org_add) calcStreamMap = calcStreamMapsyn
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)