def test_evalresp_seed_identifiers_work(self): """ Asserts that the network, station, location and channel identifiers can be used to select difference responses. """ kwargs = {"filename": os.path.join(self.path, "RESP.OB.AAA._.BH_"), "t_samp": 0.1, "nfft": 1024, "units": "VEL", "date": UTCDateTime(2013, 1, 1), "network": "OP", "station": "AAA", "locid": "", "freq": False, "debug": False} # Get the response for the first channel kwargs["channel"] = "BHE" response_1 = evalresp(**kwargs) # Get the second one. Should be different. kwargs["channel"] = "BHN" response_2 = evalresp(**kwargs) # The only thing that changed was the channel code. This should change # the response. rel_diff = np.abs(response_2 - response_1).ptp() / \ max(np.abs(response_1).ptp(), np.abs(response_2).ptp()) self.assertGreater(rel_diff, 1E-3) # The RESP file only contains two channels. kwargs["channel"] = "BHZ" # suppress a C-level "no response found" warning with SuppressOutput(): self.assertRaises(ValueError, evalresp, **kwargs)
def test_evalresp_segfault(t_samp, filename, date, stat_id, chan_id, net_id, loc_id, units): out = None try: with CatchOutput() as out: evalresp( t_samp, 5, filename, date=date, station=stat_id, channel=chan_id, network=net_id, locid=loc_id, units=units, freq=True) except: if out and out.stderr and "are not supported" in out.stderr: sys.exit(99) else: sys.exit(1)
def test_evalresp_with_output_from_seed(self): """ The StationXML file has been converted to SEED with the help of a tool provided by IRIS: https://seiscode.iris.washington.edu/projects/stationxml-converter """ t_samp = 0.05 nfft = 16384 # Test for different output units. units = ["DISP", "VEL", "ACC"] filenames = ["IRIS_single_channel_with_response", "XM.05", "AU.MEEK"] for filename in filenames: xml_filename = os.path.join(self.data_dir, filename + os.path.extsep + "xml") seed_filename = os.path.join(self.data_dir, filename + os.path.extsep + "seed") p = Parser(seed_filename) # older systems don't like an end date in the year 2599 t_ = UTCDateTime(2030, 1, 1) if p.blockettes[50][0].end_effective_date > t_: p.blockettes[50][0].end_effective_date = None if p.blockettes[52][0].end_date > t_: p.blockettes[52][0].end_date = None resp_filename = p.getRESP()[0][-1] inv = read_inventory(xml_filename) network = inv[0].code station = inv[0][0].code location = inv[0][0][0].location_code channel = inv[0][0][0].code date = inv[0][0][0].start_date for unit in units: resp_filename.seek(0, 0) seed_response, seed_freq = evalresp(t_samp, nfft, resp_filename, date=date, station=station, channel=channel, network=network, locid=location, units=unit, freq=True) xml_response, xml_freq = \ inv[0][0][0].response.get_evalresp_response(t_samp, nfft, output=unit) self.assertTrue(np.allclose(seed_freq, xml_freq, rtol=1E-5)) self.assertTrue( np.allclose(seed_response, xml_response, rtol=1E-5))
def _get_response_from_RESP(self, tr): resp = evalresp(t_samp=self.delta, nfft=self.nfft, filename=self.metadata, date=tr.stats.starttime, station=self.station, channel=self.channel, network=self.network, locid=self.location, units="VEL", freq=False, debug=False) return resp
def _get_response_from_RESP(self, tr): resp = evalresp(t_samp=self.delta, nfft=self.nfft, filename=self.metadata, date=tr.stats.starttime, station=self.station, channel=self.channel, network=self.network, locid=self.location, units="VEL", freq=False, debug=False) return resp
def test_evalresp_segfault(t_samp, filename, date, stat_id, chan_id, net_id, loc_id, units): out = None try: with CatchOutput() as out: evalresp(t_samp, 5, filename, date=date, station=stat_id, channel=chan_id, network=net_id, locid=loc_id, units=units, freq=True) except: if out and out.stderr and "are not supported" in out.stderr: sys.exit(99) else: sys.exit(1)
def test_evalresp_with_output_from_seed(self): """ The StationXML file has been converted to SEED with the help of a tool provided by IRIS: https://seiscode.iris.washington.edu/projects/stationxml-converter """ t_samp = 0.05 nfft = 16384 # Test for different output units. units = ["DISP", "VEL", "ACC"] filenames = ["IRIS_single_channel_with_response", "XM.05", "AU.MEEK"] for filename in filenames: xml_filename = os.path.join(self.data_dir, filename + os.path.extsep + "xml") seed_filename = os.path.join(self.data_dir, filename + os.path.extsep + "seed") p = Parser(seed_filename) # older systems don't like an end date in the year 2599 t_ = UTCDateTime(2030, 1, 1) if p.blockettes[50][0].end_effective_date > t_: p.blockettes[50][0].end_effective_date = None if p.blockettes[52][0].end_date > t_: p.blockettes[52][0].end_date = None resp_filename = p.getRESP()[0][-1] inv = read_inventory(xml_filename) network = inv[0].code station = inv[0][0].code location = inv[0][0][0].location_code channel = inv[0][0][0].code date = inv[0][0][0].start_date for unit in units: resp_filename.seek(0, 0) seed_response, seed_freq = evalresp( t_samp, nfft, resp_filename, date=date, station=station, channel=channel, network=network, locid=location, units=unit, freq=True) xml_response, xml_freq = \ inv[0][0][0].response.get_evalresp_response(t_samp, nfft, output=unit) self.assertTrue(np.allclose(seed_freq, xml_freq, rtol=1E-5)) self.assertTrue(np.allclose(seed_response, xml_response, rtol=1E-5))
def getspectra(string): try: #if True: debug = True lenfft = 20000 resppath = '/APPS/metadata/RESPS/' stringTEMP = string.split('_') net = stringTEMP[0] sta = stringTEMP[1] loc = stringTEMP[2] chan = stringTEMP[3] year = stringTEMP[4] jday = stringTEMP[5] if debug: print 'Working on: ' + net + ' ' + sta + ' ' + loc + ' ' + chan stime = UTCDateTime(year + '-' + jday + "T00:00:00") st = client.timeseries(net, sta, loc, chan, stime, stime + 24*60*60) if debug: print(st) if len(st) == 1: power,freq = csd(st[0].data,st[0].data, NFFT = lenfft, noverlap = int(lenfft*.5), Fs = 1/st[0].stats.delta, scale_by_freq = True) power = power[1:].real freq = freq[1:] resp = evalresp(t_samp = st[0].stats.delta, nfft = lenfft, filename= resppath + 'RESP.' + net + '.' + \ sta + '.' + loc + '.' + chan, date = st[0].stats.starttime, station = sta, channel = chan, network = net, locid = loc, units = 'ACC') resp = resp[1:] power = 10.*np.log10(power/np.absolute(resp*np.conjugate(resp))) else: power =[] freq=[] if len(power) > 1: if debug: print 'Writing data for: ' + string if not os.path.exists('/TEST_ARCHIVE/PSDS/' + sta): os.makedirs('/TEST_ARCHIVE/PSDS/' + sta) if not os.path.exists('/TEST_ARCHIVE/PSDS/' + sta + '/' + year): os.makedirs('/TEST_ARCHIVE/PSDS/' + sta + '/' + year) fpsd = open('/TEST_ARCHIVE/PSDS/' + sta + '/' + year + '/PSD_' + string,'w') for p,f in zip(power,freq): fpsd.write(str(p) + ', ' + str(f) + '\n') fpsd.close() except: print 'Unable to process: ' + string return
def test_evalresp_seed_identifiers_work(self): """ Asserts that the network, station, location and channel identifiers can be used to select difference responses. """ kwargs = { "filename": os.path.join(self.path, "RESP.OB.AAA._.BH_"), "t_samp": 0.1, "nfft": 1024, "units": "VEL", "date": UTCDateTime(2013, 1, 1), "network": "OP", "station": "AAA", "locid": "", "freq": False, "debug": False } # Get the response for the first channel kwargs["channel"] = "BHE" response_1 = evalresp(**kwargs) # Get the second one. Should be different. kwargs["channel"] = "BHN" response_2 = evalresp(**kwargs) # The only thing that changed was the channel code. This should change # the response. rel_diff = np.abs(response_2 - response_1).ptp() / \ max(np.abs(response_1).ptp(), np.abs(response_2).ptp()) self.assertTrue(rel_diff > 1E-3) # The RESP file only contains two channels. kwargs["channel"] = "BHZ" with CatchOutput() as out: self.assertRaises(ValueError, evalresp, **kwargs) self.assertTrue("no response found for" in out.stderr.lower())
def test_evalrespBug395(self): """ Was a bug due to inconstistent numerical range """ resp = os.path.join(self.path, 'RESP.CH._.HHZ.gz') with NamedTemporaryFile() as fh: tmpfile = fh.name fh.write(gzip.open(resp).read()) samprate = 120.0 nfft = 56328 args = [1.0 / samprate, nfft, tmpfile, UTCDateTime(2012, 9, 4, 5, 12, 15, 863300)] kwargs = {'units': 'VEL', 'freq': True} _h, f = evalresp(*args, **kwargs) self.assertEquals(len(f), nfft // 2 + 1)
def test_evalresp_bug_395(self): """ Was a bug due to inconstistent numerical range """ resp = os.path.join(self.path, "RESP.CH._.HHZ.gz") with NamedTemporaryFile() as fh: tmpfile = fh.name with gzip.open(resp) as f: fh.write(f.read()) samprate = 120.0 nfft = 56328 args = [1.0 / samprate, nfft, tmpfile, UTCDateTime(2012, 9, 4, 5, 12, 15, 863300)] kwargs = {"units": "VEL", "freq": True} _h, f = evalresp(*args, **kwargs) self.assertEqual(len(f), nfft // 2 + 1)
def test_evalrespBug395(self): """ Was a bug due to inconstistent numerical range """ resp = os.path.join(self.path, 'RESP.CH._.HHZ.gz') with NamedTemporaryFile() as fh: tmpfile = fh.name fh.write(gzip.open(resp).read()) samprate = 120.0 nfft = 56328 args = [1.0 / samprate, nfft, tmpfile, UTCDateTime(2012, 9, 4, 5, 12, 15, 863300)] kwargs = {'units': 'VEL', 'freq': True} _h, f = evalresp(*args, **kwargs) self.assertEquals(len(f), nfft // 2 + 1)
def _get_response_from_parser(self, tr): parser = self.metadata resp_key = "RESP." + self.id for key, resp_file in parser.getRESP(): if key == resp_key: break else: msg = "Response for %s not found in Parser" % self.id raise ValueError(msg) resp_file.seek(0, 0) resp = evalresp(t_samp=self.delta, nfft=self.nfft, filename=resp_file, date=tr.stats.starttime, station=self.station, channel=self.channel, network=self.network, locid=self.location, units="VEL", freq=False, debug=False) return resp
def _get_response_from_parser(self, tr): parser = self.metadata resp_key = "RESP." + self.id for key, resp_file in parser.get_RESP(): if key == resp_key: break else: msg = "Response for %s not found in Parser" % self.id raise ValueError(msg) resp_file.seek(0, 0) resp = evalresp(t_samp=self.delta, nfft=self.nfft, filename=resp_file, date=tr.stats.starttime, station=self.station, channel=self.channel, network=self.network, locid=self.location, units="VEL", freq=False, debug=False) return resp
def test_evalresp_using_different_line_separator(self): """ The evalresp needs a file with correct line separator, so '\n' for POSIX, '\r' for Mac OS, or '\r\n' for Windows. Here we check that evalresp reads all three formats. This test only checks the parsing capabilities of evalresp, the number of fft points used (nfft) can therefore be chosen small. """ dt = UTCDateTime(2003, 11, 1, 0, 0, 0) nfft = 8 # Linux respf = os.path.join(self.path, 'RESP.NZ.CRLZ.10.HHZ') evalresp(0.01, nfft, respf, dt) # Mac respf = os.path.join(self.path, 'RESP.NZ.CRLZ.10.HHZ.mac') evalresp(0.01, nfft, respf, dt) # Windows respf = os.path.join(self.path, 'RESP.NZ.CRLZ.10.HHZ.windows') evalresp(0.01, nfft, respf, dt)
def test_evalrespUsingDifferentLineSeparator(self): """ The evalresp needs a file with correct line separator, so '\n' for POSIX, '\r' for Mac OS, or '\r\n' for Windows. Here we check that evalresp reads all three formats. This test only checks the parsing capabilities of evalresp, the number of fft points used (nfft) can therefore be chosen small. """ dt = UTCDateTime(2003, 11, 1, 0, 0, 0) nfft = 8 # linux respf = os.path.join(self.path, 'RESP.NZ.CRLZ.10.HHZ') evalresp(0.01, nfft, respf, dt) # mac respf = os.path.join(self.path, 'RESP.NZ.CRLZ.10.HHZ.mac') evalresp(0.01, nfft, respf, dt) # windows respf = os.path.join(self.path, 'RESP.NZ.CRLZ.10.HHZ.windows') evalresp(0.01, nfft, respf, dt)
power = power[1:] power = np.abs(power) ## Import metadata ## for tr in st.select(channel='LH*'): if net == 'XX': resppath = '/home/aalejandro/Pressure/RESP/RESP.' + tr.id else: resppath = '/APPS/metadata/RESPS/RESP.' + tr.id if debug: print(resppath) resp = evalresp(t_samp=tr.stats.delta, nfft=NFFT, filename=resppath, date=tr.stats.starttime, station=tr.stats.station, channel=tr.stats.channel, locid=tr.stats.location, network=tr.stats.network, units="ACC") st.filter('bandpass', freqmin=1. / Pmax, freqmax=1. / Pmin) st.taper(0.05) st.sort() sp = Parser() for tr in st.select(channel='LH*'): if net == 'XX': sp.read('/home/aalejandro/Pressure/RESP/RESP.' + net + '.' + sta + '.' + loc + '.' + chan) else:
plt.subplot(3, 1, 3) per, NLNM = get_nlnm() per, NHNM = get_nhnm() for tr in st: power, freq = csd(tr.data, tr.data, NFFT=lenfft, noverlap=int(lenfft * .5), Fs=1. / tr.stats.delta, scale_by_freq=True) power = np.abs(power[1:]) freq = freq[1:] resp = evalresp(t_samp=tr.stats.delta, nfft=lenfft, filename='/APPS/metadata/RESPS/RESP.' + tr.stats.network + '.' + tr.stats.station + '.' + tr.stats.location + '.' + tr.stats.channel, date=tr.stats.starttime, units='ACC') resp = resp[1:] power = 10. * np.log10(power / np.absolute(resp * np.conjugate(resp))) if tr.stats.location == '00': lab = 'STS-6A' else: lab = 'T-120' plt.semilogx(1. / freq, power, label=lab) plt.semilogx(per, NLNM, 'k', label="NHNM/NLNM") plt.semilogx(per, NHNM, 'k') plt.ylabel('Power (dB rel. 1 $m/(s^2)^2/Hz$)')
n, p, fre1 = selfnoise(stgood, length, overlap) fig = plt.figure(1, figsize=(18, 18)) plt.subplot(2, 1, 1) nm = (n['1'] + n['2'] + n['3']) / 3. for idx in range(1, 4): tr = stgood[idx - 1] resp = evalresp(t_samp=tr.stats.delta, nfft=length, filename='/APPS/metadata/RESPS/RESP.' + tr.id, date=tr.stats.starttime, station=tr.stats.station, channel=tr.stats.channel, network=tr.stats.network, locid=tr.stats.location, units='ACC') n[str(idx)] /= np.abs(resp[1:])**2 p[str(idx)] = 10. * np.log10(p[str(idx)] / np.abs(resp[1:])**2) p[str(idx)] = octavesmooth(p[str(idx)], 1. / 6.) n[str(idx)] = 10. * np.log10(n[str(idx)]) n[str(idx)] = octavesmooth(n[str(idx)], 1. / 6.) plt.semilogx(1. / fre1, p[str(idx)], label='PSD ' + (tr.id).replace('.', ' '), alpha=.7) plt.semilogx(1. / fre1,
print_info("Raw evalresp segfaults. XML response not " "attempted.") counter["evalresp_segfaults"] += 1 continue elif not output: filename.seek(0, 0) if debug: print "SEED" try: seed_response, seed_freq = evalresp( t_samp, nfft, filename, date=date, station=stat_id, channel=chan_id, network=net_id, locid=loc_id, units=unit, freq=True) except ValueError as e: seedresp_error = e else: with CatchOutput() as out: try: seed_response, seed_freq = evalresp( t_samp, nfft, filename, date=date,
def _find_resp(station, channel, network, time, delta, directory): """ Helper function to find the response information for a given station and \ channel at a given time and return a dictionary of poles and zeros, gain \ and sensitivity. :type station: str :param station: Station name (as in the response files) :type channel: str :param channel: Channel name (as in the response files) :type network: str :param network: Network to scan for, can be a wildcard :type time: datetime.datetime :param time: Date-time to look for repsonse information :type delta: float :param delta: Sample interval in seconds :type directory: str :param directory: Directory to scan for response information :returns: dict, response information """ import glob from obspy.signal.invsim import evalresp from obspy import UTCDateTime possible_respfiles = glob.glob(directory + '/RESP.' + network + '.' + station + '.*.' + channel) # GeoNet RESP naming possible_respfiles += glob.glob(directory + '/RESP.' + network + '.' + channel + '.' + station) # RDseed RESP naming possible_respfiles += glob.glob(directory + '/RESP.' + station + '.' + network) # WIZARD resp naming # GSE format, station needs to be 5 charectars padded with _, channel is 4 # characters padded with _ possible_respfiles += glob.glob(directory + '/' + station.ljust(5, '_') + channel[0:len(channel) - 1].ljust(3, '_') + channel[-1] + '.*_GSE') PAZ = [] seedresp = [] for respfile in possible_respfiles: print 'Reading response from: ' + respfile if respfile.split('/')[-1][0:4] == 'RESP': # Read from a resp file seedresp = { 'filename': respfile, 'date': UTCDateTime(time), 'units': 'DIS', 'network': network, 'station': station, 'channel': channel, 'location': '*' } try: # Attempt to evaluate the response for this information, if not # then this is not the correct response info! freq_resp, freqs = evalresp(delta, 100, seedresp['filename'], seedresp['date'], units=seedresp['units'], freq=True, network=seedresp['network'], station=seedresp['station'], channel=seedresp['channel']) except: print 'Issues with RESP file' seedresp = [] continue elif respfile[-3:] == 'GSE': PAZ, pazdate, pazstation, pazchannel, pazsensor =\ _GSE2_PAZ_read(respfile) # check that the date is good! if pazdate >= time and pazchannel != channel and\ pazstation != station: print 'Issue with GSE file' print 'date: '+str(pazdate)+' channel: '+pazchannel +\ ' station: '+pazstation PAZ = [] else: continue # Check that PAZ are for the correct station, channel and date if PAZ or seedresp: break if PAZ: return PAZ elif seedresp: return seedresp
print('work on station ' + station) tfiles = glob.glob(os.path.join(resp_dir, 'RESP.' + station + '*')) if len(tfiles) == 0: print('cannot find resp files for station ' + station) tfile = tfiles[0] comp = tfile.split('.')[2] #---extract the resp------ respz, freq = evalresp(dt, Nfft, tfile, tdate, station=station, channel=comp, network=network, locid='*', units='VEL', freq=True, debug=False) plt.subplot(211) plt.loglog(freq, np.absolute(respz)) invert_spectrum(respz, water_level) plt.subplot(212) plt.loglog(freq, np.absolute(respz)) #cos_win = cosine_sac_taper(freq, flimit=prefilt) #respz *=cos_win plt.show() output = os.path.join(resp_dir, 'resp.' + station + '.npy')
def single_comparison(): """ one by one comparison of the waveforms in the first path with the second path. """ client = Client() global input # identity of the waveforms (first and second paths) to be compared with each other identity_all = input['net'] + '.' + input['sta'] + '.' + \ input['loc'] + '.' + input['cha'] ls_first = glob.glob(os.path.join(input['first_path'], identity_all)) ls_second = glob.glob(os.path.join(input['second_path'], identity_all)) for i in range(0, len(ls_first)): try: tr1 = read(ls_first[i])[0] if input['phase'] != 'N': evsta_dist = util.locations2degrees(lat1 = tr1.stats.sac.evla, \ long1 = tr1.stats.sac.evlo, lat2 = tr1.stats.sac.stla, \ long2 = tr1.stats.sac.stlo) taup_tt = taup.getTravelTimes(delta = evsta_dist, depth = tr1.stats.sac.evdp) phase_exist = 'N' for tt_item in taup_tt: if tt_item['phase_name'] == input['phase']: print 'Requested phase:' print input['phase'] print '------' print tt_item['phase_name'] print 'exists in the waveform!' print '-----------------------' t_phase = tt_item['time'] phase_exist = 'Y' break if phase_exist != 'Y': continue # identity of the current waveform identity = tr1.stats.network + '.' + tr1.stats.station + '.' + \ tr1.stats.location + '.' + tr1.stats.channel # tr1: first path, tr2: second path, tr3: Raw data #tr3 = read(os.path.join(input['first_path'], '..', 'BH_RAW', identity))[0] if input['resp_paz'] == 'Y': response_file = os.path.join(input['first_path'], '..', 'Resp/RESP.' + identity) # Extract the PAZ info from response file paz = readRESP(response_file, unit = input['corr_unit']) poles = paz['poles'] zeros = paz['zeros'] scale_fac = paz['gain'] sensitivity = paz['sensitivity'] print paz # Convert Poles and Zeros (PAZ) to frequency response. h, f = pazToFreqResp(poles, zeros, scale_fac, \ 1./tr1.stats.sampling_rate, tr1.stats.npts*2, freq=True) # Use the evalresp library to extract # instrument response information from a SEED RESP-file. resp = invsim.evalresp(t_samp = 1./tr1.stats.sampling_rate, \ nfft = tr1.stats.npts*2, filename = response_file, \ date = tr1.stats.starttime, units = input['corr_unit'].upper()) # Keep the current identity in a new variable id_name = identity try: tr2 = read(os.path.join(input['second_path'], identity))[0] except Exception, error: # if it is not possible to read the identity in the second path # then change the network part of the identity based on # correction unit identity = input['corr_unit'] + '.' + tr1.stats.station + '.' + \ tr1.stats.location + '.' + tr1.stats.channel tr2 = read(os.path.join(input['second_path'], identity))[0] if input['resample'] != 'N': print 'WARNING: you are using resample!!!' tr1.resample(input['resample']) tr2.resample(input['resample']) if input['tw'] == 'Y': t_cut_1 = tr1.stats.starttime + t_phase - input['preset'] t_cut_2 = tr1.stats.starttime + t_phase + input['offset'] tr1.trim(starttime = t_cut_1, endtime = t_cut_2) t_cut_1 = tr2.stats.starttime + t_phase - input['preset'] t_cut_2 = tr2.stats.starttime + t_phase + input['offset'] tr2.trim(starttime = t_cut_1, endtime = t_cut_2) if input['hlfilter'] == 'Y': tr1.filter('lowpass', freq=input['hfreq'], corners=2) tr2.filter('lowpass', freq=input['hfreq'], corners=2) tr1.filter('highpass', freq=input['lfreq'], corners=2) tr2.filter('highpass', freq=input['lfreq'], corners=2) # normalization of all three waveforms to the # max(max(tr1), max(tr2), max(tr3)) to keep the scales #maxi = max(abs(tr1.data).max(), abs(tr2.data).max(), abs(tr3.data).max()) #maxi = max(abs(tr1.data).max(), abs(tr2.data).max()) #tr1_data = tr1.data/abs(maxi) #tr2_data = tr2.data/abs(maxi) #tr3_data = tr3.data/abs(maxi) tr1_data = tr1.data/abs(max(tr1.data)) tr2_data = tr2.data/abs(max(tr2.data)) #tr1_data = tr1.data #tr2_data = tr2.data*1e9 print max(tr1.data) print max(tr2.data) # create time arrays for tr1, tr2 and tr3 time_tr1 = np.arange(0, tr1.stats.npts/tr1.stats.sampling_rate, \ 1./tr1.stats.sampling_rate) time_tr2 = np.arange(0, tr2.stats.npts/tr2.stats.sampling_rate, \ 1./tr2.stats.sampling_rate) #time_tr3 = np.arange(0, tr3.stats.npts/tr3.stats.sampling_rate, \ # 1./tr3.stats.sampling_rate) # label for plotting label_tr1 = ls_first[i].split('/')[-2] label_tr2 = ls_second[i].split('/')[-2] label_tr3 = 'RAW' if input['resp_paz'] == 'Y': # start plotting plt.figure() plt.subplot2grid((3,4), (0,0), colspan=4, rowspan=2) #plt.subplot(211) plt.plot(time_tr1, tr1_data, color = 'blue', label = label_tr1, lw=3) plt.plot(time_tr2, tr2_data, color = 'red', label = label_tr2, lw=3) #plt.plot(time_tr3, tr3_data, color = 'black', ls = '--', label = label_tr3) plt.xlabel('Time (sec)', fontsize = 'xx-large', weight = 'bold') if input['corr_unit'] == 'dis': ylabel_str = 'Relative Displacement' elif input['corr_unit'] == 'vel': ylabel_str = 'Relative Vel' elif input['corr_unit'] == 'acc': ylabel_str = 'Relative Acc' plt.ylabel(ylabel_str, fontsize = 'xx-large', weight = 'bold') plt.xticks(fontsize = 'xx-large', weight = 'bold') plt.yticks(fontsize = 'xx-large', weight = 'bold') plt.legend(loc=1,prop={'size':20}) #-------------------Cross Correlation # 5 seconds as total length of samples to shift for cross correlation. cc_np = tr1.stats.sampling_rate * 3 np_shift, coeff = cross_correlation.xcorr(tr1, tr2, int(cc_np)) t_shift = float(np_shift)/tr1.stats.sampling_rate print "Cross Correlation:" print "Shift: " + str(t_shift) print "Coefficient: " + str(coeff) plt.title('Single Comparison' + '\n' + str(t_shift) + \ ' sec , coeff: ' + str(round(coeff, 5)) + \ '\n' + id_name, \ fontsize = 'xx-large', weight = 'bold') if input['resp_paz'] == 'Y': # ----------------------- #plt.subplot(223) plt.subplot2grid((3,4), (2,0), colspan=2) ''' plt.plot(np.log10(f), np.log10(abs(resp)/(sensitivity*sensitivity)), \ color = 'blue', label = 'RESP', lw=3) plt.plot(np.log10(f), np.log10(abs(h)/sensitivity), \ color = 'red', label = 'PAZ', lw=3) ''' plt.loglog(f, abs(resp)/(sensitivity*sensitivity), \ color = 'blue', label = 'RESP', lw=3) plt.loglog(f, abs(h)/sensitivity, \ color = 'red', label = 'PAZ', lw=3) #for j in [0.008, 0.012, 0.025, 0.5, 1, 2, 3, 4]: for j in [0]: plt.axvline(np.log10(j), linestyle = '--') #plt.xlabel('Frequency [Hz]\n(power of 10)', fontsize = 'xx-large', weight = 'bold') #plt.ylabel('Amplitude\n (power of 10)', fontsize = 'xx-large', weight = 'bold') plt.xlabel('Frequency [Hz]', fontsize = 'xx-large', weight = 'bold') plt.ylabel('Amplitude', fontsize = 'xx-large', weight = 'bold') plt.xticks(fontsize = 'xx-large', weight = 'bold') #plt.yticks = MaxNLocator(nbins=4) plt.yticks(fontsize = 'xx-large', weight = 'bold') plt.legend(loc=2,prop={'size':20}) # ----------------------- #plt.subplot(224) plt.subplot2grid((3,4), (2,2), colspan=2) #take negative of imaginary part phase_paz = np.unwrap(np.arctan2(h.imag, h.real)) phase_resp = np.unwrap(np.arctan2(resp.imag, resp.real)) #plt.plot(np.log10(f), phase_resp, color = 'blue', label = 'RESP', lw=3) #plt.plot(np.log10(f), phase_paz, color = 'red', label = 'PAZ', lw=3) plt.semilogx(f, phase_resp, color = 'blue', label = 'RESP', lw=3) plt.semilogx(f, phase_paz, color = 'red', label = 'PAZ', lw=3) #for j in [0.008, 0.012, 0.025, 0.5, 1, 2, 3, 4]: for j in [0.0]: plt.axvline(np.log10(j), linestyle = '--') #plt.xlabel('Frequency [Hz]\n(power of 10)', fontsize = 'xx-large', weight = 'bold') plt.xlabel('Frequency [Hz]', fontsize = 'xx-large', weight = 'bold') plt.ylabel('Phase [radian]', fontsize = 'xx-large', weight = 'bold') plt.xticks(fontsize = 'xx-large', weight = 'bold') plt.yticks(fontsize = 'xx-large', weight = 'bold') plt.legend(loc=3,prop={'size':20}) # title, centered above both subplots # make more room in between subplots for the ylabel of right plot plt.subplots_adjust(wspace=0.4, hspace=0.3) """ # ----------------------- plt.subplot(325) plt.plot(np.log10(f), np.log10(abs(resp)/(sensitivity*sensitivity)) - \ np.log10(abs(h)/sensitivity), \ color = 'black', label = 'RESP - PAZ') for j in [0.008, 0.012, 0.025, 0.5, 1, 2, 3, 4]: plt.axvline(np.log10(j), linestyle = '--') plt.xlabel('Frequency [Hz] (power of 10)') plt.ylabel('Amplitude (power of 10)') plt.legend() # ----------------------- plt.subplot(326) #take negative of imaginary part phase_paz = np.unwrap(np.arctan2(h.imag, h.real)) phase_resp = np.unwrap(np.arctan2(resp.imag, resp.real)) plt.plot(np.log10(f), np.log10(phase_resp) - np.log10(phase_paz), \ color = 'black', label = 'RESP - PAZ') for j in [0.008, 0.012, 0.025, 0.5, 1, 2, 3, 4]: plt.axvline(np.log10(j), linestyle = '--') plt.xlabel('Frequency [Hz] (power of 10)') plt.ylabel('Phase [radian] (power of 10)') plt.legend() # title, centered above both subplots # make more room in between subplots for the ylabel of right plot plt.subplots_adjust(wspace=0.3) """ plt.show() print str(i+1) + '/' + str(len(ls_first)) print ls_first[i] print '------------------' wait = raw_input(id_name) print '***************************' except Exception, error: print '##################' print error print '##################'
Fs=tr.stats.sampling_rate, NFFT=nfft, pad_to=nfft * 2, noverlap=olp, scale_by_freq=True, mode='psd') #still need to remove response specgram = specgram[1:, :] freq = freq[1:] # Grab the response resppath = '/APPS/metadata/RESPS/' # Return the response resp = evalresp(t_samp = tr.stats.delta, nfft = 2 * nfft, filename = resppath + 'RESP.' + net + '.' + \ sta + '.' + loc + '.' + chan, date = tr.stats.starttime, station = sta, channel = chan, network = net, locid = loc, units = 'ACC') # Remove the 0 frequency resp = resp[1:] # Correct for the response specgram = (specgram.T / (np.abs(resp * np.conjugate(resp)))).T specgram = 10. * np.log10(specgram) #for idx in range(len(specgram[1,:])): # specgram[:,idx] = smooth(specgram[:,idx],10) specs[str(tr.stats.channel)] = specgram print(specs) # We have calculated all the PSDs for the time period now we want to do things to them
def single_comparison(): """ one by one comparison of the waveforms in the first path with the second path. """ client = Client() global input # identity of the waveforms (first and second paths) to be compared with each other identity_all = input['net'] + '.' + input['sta'] + '.' + \ input['loc'] + '.' + input['cha'] ls_first = glob.glob(os.path.join(input['first_path'], identity_all)) ls_second = glob.glob(os.path.join(input['second_path'], identity_all)) for i in range(0, len(ls_first)): try: tr1 = read(ls_first[i])[0] if input['phase'] != 'N': evsta_dist = util.locations2degrees(lat1 = tr1.stats.sac.evla, \ long1 = tr1.stats.sac.evlo, lat2 = tr1.stats.sac.stla, \ long2 = tr1.stats.sac.stlo) taup_tt = taup.getTravelTimes(delta=evsta_dist, depth=tr1.stats.sac.evdp) phase_exist = 'N' for tt_item in taup_tt: if tt_item['phase_name'] == input['phase']: print 'Requested phase:' print input['phase'] print '------' print tt_item['phase_name'] print 'exists in the waveform!' print '-----------------------' t_phase = tt_item['time'] phase_exist = 'Y' break if phase_exist != 'Y': continue # identity of the current waveform identity = tr1.stats.network + '.' + tr1.stats.station + '.' + \ tr1.stats.location + '.' + tr1.stats.channel # tr1: first path, tr2: second path, tr3: Raw data #tr3 = read(os.path.join(input['first_path'], '..', 'BH_RAW', identity))[0] if input['resp_paz'] == 'Y': response_file = os.path.join(input['first_path'], '..', 'Resp/RESP.' + identity) # Extract the PAZ info from response file paz = readRESP(response_file, unit=input['corr_unit']) poles = paz['poles'] zeros = paz['zeros'] scale_fac = paz['gain'] sensitivity = paz['sensitivity'] print paz # Convert Poles and Zeros (PAZ) to frequency response. h, f = pazToFreqResp(poles, zeros, scale_fac, \ 1./tr1.stats.sampling_rate, tr1.stats.npts*2, freq=True) # Use the evalresp library to extract # instrument response information from a SEED RESP-file. resp = invsim.evalresp(t_samp = 1./tr1.stats.sampling_rate, \ nfft = tr1.stats.npts*2, filename = response_file, \ date = tr1.stats.starttime, units = input['corr_unit'].upper()) # Keep the current identity in a new variable id_name = identity try: tr2 = read(os.path.join(input['second_path'], identity))[0] except Exception, error: # if it is not possible to read the identity in the second path # then change the network part of the identity based on # correction unit identity = input['corr_unit'] + '.' + tr1.stats.station + '.' + \ tr1.stats.location + '.' + tr1.stats.channel tr2 = read(os.path.join(input['second_path'], identity))[0] if input['resample'] != 'N': print 'WARNING: you are using resample!!!' tr1.resample(input['resample']) tr2.resample(input['resample']) if input['tw'] == 'Y': t_cut_1 = tr1.stats.starttime + t_phase - input['preset'] t_cut_2 = tr1.stats.starttime + t_phase + input['offset'] tr1.trim(starttime=t_cut_1, endtime=t_cut_2) t_cut_1 = tr2.stats.starttime + t_phase - input['preset'] t_cut_2 = tr2.stats.starttime + t_phase + input['offset'] tr2.trim(starttime=t_cut_1, endtime=t_cut_2) if input['hlfilter'] == 'Y': tr1.filter('lowpass', freq=input['hfreq'], corners=2) tr2.filter('lowpass', freq=input['hfreq'], corners=2) tr1.filter('highpass', freq=input['lfreq'], corners=2) tr2.filter('highpass', freq=input['lfreq'], corners=2) # normalization of all three waveforms to the # max(max(tr1), max(tr2), max(tr3)) to keep the scales #maxi = max(abs(tr1.data).max(), abs(tr2.data).max(), abs(tr3.data).max()) #maxi = max(abs(tr1.data).max(), abs(tr2.data).max()) #tr1_data = tr1.data/abs(maxi) #tr2_data = tr2.data/abs(maxi) #tr3_data = tr3.data/abs(maxi) tr1_data = tr1.data / abs(max(tr1.data)) tr2_data = tr2.data / abs(max(tr2.data)) #tr1_data = tr1.data #tr2_data = tr2.data*1e9 print max(tr1.data) print max(tr2.data) # create time arrays for tr1, tr2 and tr3 time_tr1 = np.arange(0, tr1.stats.npts/tr1.stats.sampling_rate, \ 1./tr1.stats.sampling_rate) time_tr2 = np.arange(0, tr2.stats.npts/tr2.stats.sampling_rate, \ 1./tr2.stats.sampling_rate) #time_tr3 = np.arange(0, tr3.stats.npts/tr3.stats.sampling_rate, \ # 1./tr3.stats.sampling_rate) # label for plotting label_tr1 = ls_first[i].split('/')[-2] label_tr2 = ls_second[i].split('/')[-2] label_tr3 = 'RAW' if input['resp_paz'] == 'Y': # start plotting plt.figure() plt.subplot2grid((3, 4), (0, 0), colspan=4, rowspan=2) #plt.subplot(211) plt.plot(time_tr1, tr1_data, color='blue', label=label_tr1, lw=3) plt.plot(time_tr2, tr2_data, color='red', label=label_tr2, lw=3) #plt.plot(time_tr3, tr3_data, color = 'black', ls = '--', label = label_tr3) plt.xlabel('Time (sec)', fontsize='xx-large', weight='bold') if input['corr_unit'] == 'dis': ylabel_str = 'Relative Displacement' elif input['corr_unit'] == 'vel': ylabel_str = 'Relative Vel' elif input['corr_unit'] == 'acc': ylabel_str = 'Relative Acc' plt.ylabel(ylabel_str, fontsize='xx-large', weight='bold') plt.xticks(fontsize='xx-large', weight='bold') plt.yticks(fontsize='xx-large', weight='bold') plt.legend(loc=1, prop={'size': 20}) #-------------------Cross Correlation # 5 seconds as total length of samples to shift for cross correlation. cc_np = tr1.stats.sampling_rate * 3 np_shift, coeff = cross_correlation.xcorr(tr1, tr2, int(cc_np)) t_shift = float(np_shift) / tr1.stats.sampling_rate print "Cross Correlation:" print "Shift: " + str(t_shift) print "Coefficient: " + str(coeff) plt.title('Single Comparison' + '\n' + str(t_shift) + \ ' sec , coeff: ' + str(round(coeff, 5)) + \ '\n' + id_name, \ fontsize = 'xx-large', weight = 'bold') if input['resp_paz'] == 'Y': # ----------------------- #plt.subplot(223) plt.subplot2grid((3, 4), (2, 0), colspan=2) ''' plt.plot(np.log10(f), np.log10(abs(resp)/(sensitivity*sensitivity)), \ color = 'blue', label = 'RESP', lw=3) plt.plot(np.log10(f), np.log10(abs(h)/sensitivity), \ color = 'red', label = 'PAZ', lw=3) ''' plt.loglog(f, abs(resp)/(sensitivity*sensitivity), \ color = 'blue', label = 'RESP', lw=3) plt.loglog(f, abs(h)/sensitivity, \ color = 'red', label = 'PAZ', lw=3) #for j in [0.008, 0.012, 0.025, 0.5, 1, 2, 3, 4]: for j in [0]: plt.axvline(np.log10(j), linestyle='--') #plt.xlabel('Frequency [Hz]\n(power of 10)', fontsize = 'xx-large', weight = 'bold') #plt.ylabel('Amplitude\n (power of 10)', fontsize = 'xx-large', weight = 'bold') plt.xlabel('Frequency [Hz]', fontsize='xx-large', weight='bold') plt.ylabel('Amplitude', fontsize='xx-large', weight='bold') plt.xticks(fontsize='xx-large', weight='bold') #plt.yticks = MaxNLocator(nbins=4) plt.yticks(fontsize='xx-large', weight='bold') plt.legend(loc=2, prop={'size': 20}) # ----------------------- #plt.subplot(224) plt.subplot2grid((3, 4), (2, 2), colspan=2) #take negative of imaginary part phase_paz = np.unwrap(np.arctan2(h.imag, h.real)) phase_resp = np.unwrap(np.arctan2(resp.imag, resp.real)) #plt.plot(np.log10(f), phase_resp, color = 'blue', label = 'RESP', lw=3) #plt.plot(np.log10(f), phase_paz, color = 'red', label = 'PAZ', lw=3) plt.semilogx(f, phase_resp, color='blue', label='RESP', lw=3) plt.semilogx(f, phase_paz, color='red', label='PAZ', lw=3) #for j in [0.008, 0.012, 0.025, 0.5, 1, 2, 3, 4]: for j in [0.0]: plt.axvline(np.log10(j), linestyle='--') #plt.xlabel('Frequency [Hz]\n(power of 10)', fontsize = 'xx-large', weight = 'bold') plt.xlabel('Frequency [Hz]', fontsize='xx-large', weight='bold') plt.ylabel('Phase [radian]', fontsize='xx-large', weight='bold') plt.xticks(fontsize='xx-large', weight='bold') plt.yticks(fontsize='xx-large', weight='bold') plt.legend(loc=3, prop={'size': 20}) # title, centered above both subplots # make more room in between subplots for the ylabel of right plot plt.subplots_adjust(wspace=0.4, hspace=0.3) """ # ----------------------- plt.subplot(325) plt.plot(np.log10(f), np.log10(abs(resp)/(sensitivity*sensitivity)) - \ np.log10(abs(h)/sensitivity), \ color = 'black', label = 'RESP - PAZ') for j in [0.008, 0.012, 0.025, 0.5, 1, 2, 3, 4]: plt.axvline(np.log10(j), linestyle = '--') plt.xlabel('Frequency [Hz] (power of 10)') plt.ylabel('Amplitude (power of 10)') plt.legend() # ----------------------- plt.subplot(326) #take negative of imaginary part phase_paz = np.unwrap(np.arctan2(h.imag, h.real)) phase_resp = np.unwrap(np.arctan2(resp.imag, resp.real)) plt.plot(np.log10(f), np.log10(phase_resp) - np.log10(phase_paz), \ color = 'black', label = 'RESP - PAZ') for j in [0.008, 0.012, 0.025, 0.5, 1, 2, 3, 4]: plt.axvline(np.log10(j), linestyle = '--') plt.xlabel('Frequency [Hz] (power of 10)') plt.ylabel('Phase [radian] (power of 10)') plt.legend() # title, centered above both subplots # make more room in between subplots for the ylabel of right plot plt.subplots_adjust(wspace=0.3) """ plt.show() print str(i + 1) + '/' + str(len(ls_first)) print ls_first[i] print '------------------' wait = raw_input(id_name) print '***************************' except Exception, error: print '##################' print error print '##################'
output = test_for_segfault(t_samp, filename, date, stat_id, chan_id, net_id, loc_id, unit) if output == "segfault": print_info("Raw evalresp segfaults. XML response not " "attempted.") counter["evalresp_segfaults"] += 1 continue elif not output: filename.seek(0, 0) if debug: print "SEED" try: seed_response, seed_freq = evalresp( t_samp, nfft, filename, date=date, station=stat_id, channel=chan_id, network=net_id, locid=loc_id, units=unit, freq=True) except ValueError as e: seedresp_error = e else: with CatchOutput() as out: try: seed_response, seed_freq = evalresp( t_samp, nfft, filename, date=date, station=stat_id, channel=chan_id, network=net_id, locid=loc_id, units=unit, freq=True) except ValueError as e: seedresp_error = e elif output == "uniterror":
noverlap=int(0.25 * nfft), scale_by_freq=True, mode='psd') specgram = specgram[1:, :] freq = freq[1:] # Reading in responses - responses for the XX network isn't yet with IRIS if net2 == 'XX': resppath = '' else: resppath = '/APPS/metadata/RESPS/' # Removing response from seismic data traces resp = evalresp(t_samp = st[0].stats.delta, nfft = 2 * nfft, filename = resppath + 'RESP.' + net2 + '.' + \ sta2 + '.' + loc2 + '.' + chan2, date = st[0].stats.starttime, station = sta2, channel = chan2, network = net2, locid = loc2, units = 'ACC') resp = resp[1:] specgram = (specgram.T / (np.abs(resp * np.conjugate(resp)))).T specgram = 10. * np.log10(specgram) # Decimating wind speed to have a data point every minute tr.decimate(5) tr.decimate(2) tr.decimate(6) # Establishing the time (independent) variable time *= 1. / (60. * 60.) timews = np.asarray(range(tr.stats.npts)) / 60. # Creating the figure and labeling it, etc.
def _find_resp(station, channel, network, time, delta, directory): """ Helper function to find the response information for a given station and channel at a given time and return a dictionary of poles and zeros, gain and sensitivity. :type station: String :param station: Station name (as in the response files) :type channel: String :param channel: Channel name (as in the response files) :type network: String :param network: Network to scan for, can be a wildcard :type time: datetime.datetime :param time: Date-time to look for repsonse information :type delta: float :param delta: Sample interval in seconds :type directory: String :param directory: Directory to scan for response information :returns: Dictionary """ import glob from obspy.signal.invsim import evalresp from obspy import UTCDateTime possible_respfiles=glob.glob(directory+'/RESP.'+network+'.'+station+'.*.'+\ channel) # GeoNet RESP naming possible_respfiles+=glob.glob(directory+'/RESP.'+network+'.'+channel+'.'+\ station) # RDseed RESP naming possible_respfiles+=glob.glob(directory+'/RESP.'+station+'.'+network) # WIZARD resp naming # GSE format, station needs to be 5 charectars padded with _, channel is 4 # characters padded with _ possible_respfiles+=glob.glob(directory+'/'+station.ljust(5,'_')+\ channel[0:len(channel)-1].ljust(3,'_')+\ channel[-1]+'.*_GSE') PAZ=[] seedresp=[] for respfile in possible_respfiles: print 'Reading response from: '+respfile if respfile.split('/')[-1][0:4]=='RESP': # Read from a resp file seedresp={'filename': respfile, 'date': UTCDateTime(time), 'units': 'DIS', 'network': network, 'station': station, 'channel': channel, 'location': '*'} try: # Attempt to evaluate the response for this information, if not # then this is not the correct response info! freq_resp, freqs = evalresp(delta, 100, seedresp['filename'], seedresp['date'], units=seedresp['units'], freq=True, network=seedresp['network'], station=seedresp['station'], channel=seedresp['channel']) except: print 'Issues with RESP file' seedresp=[] continue elif respfile[-3:]=='GSE': PAZ, pazdate, pazstation, pazchannel, pazsensor =\ _GSE2_PAZ_read(respfile) # check that the date is good! if pazdate >= time and pazchannel != channel and\ pazstation != station: print 'Issue with GSE file' print 'date: '+str(pazdate)+' channel: '+pazchannel+\ ' station: '+pazstation PAZ=[] else: continue # Check that PAZ are for the correct station, channel and date if PAZ or seedresp: break if PAZ: return PAZ elif seedresp: return seedresp