def test_evalrespBug395(self): """ Was a bug due to inconstistent numerical range """ resp = os.path.join(self.path, 'RESP.CH._.HHZ.gz') fh = NamedTemporaryFile() tmpfile = fh.name fh.write(gzip.open(resp).read()) fh.close() samprate = 120.0 nfft = 56328 args = [1/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) os.unlink(tmpfile)
def test_evalrespBug395(self): """ Was a bug due to inconstistent numerical range """ resp = os.path.join(self.path, 'RESP.CH._.HHZ.gz') fh = NamedTemporaryFile() tmpfile = fh.name fh.write(gzip.open(resp).read()) fh.close() samprate = 120.0 nfft = 56328 args = [ 1 / 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) os.unlink(tmpfile)
def evalresp(t_samp, nfft, filename, date, station='*', channel='*', network='*', locid='*', units="VEL", start_stage=-1, stop_stage=0, freq=False, debug=False): """ Use the evalresp library to extract instrument response information from a SEED RESP-file. To restrict the response to the instrument the start and stop stages can be specified here. :type t_samp: float :param t_samp: Sampling interval in seconds :type nfft: int :param nfft: Number of FFT points of signal which needs correction :type filename: str (or open file like object) :param filename: SEED RESP-filename or open file like object with RESP information. Any object that provides a read() method will be considered to be a file like object. :type date: UTCDateTime :param date: Date of interest :type station: str :param station: Station id :type channel: str :param channel: Channel id :type network: str :param network: Network id :type locid: str :param locid: Location id :type units: str :param units: Units to return response in. Can be either DIS, VEL or ACC :type start_stage: int :param start_stage: integer stage numbers of start stage (<0 causes default evalresp bahaviour). :type stop_stage: int :param stop_stage: integer stage numbers of stop stage :type debug: bool :param debug: Verbose output to stdout. Disabled by default. :rtype: numpy.ndarray complex128 :return: Frequency response from SEED RESP-file of length nfft """ if isinstance(filename, basestring): with open(filename, 'rb') as fh: data = fh.read() elif hasattr(filename, 'read'): data = filename.read() # evalresp needs files with correct line separators depending on OS fh = NamedTemporaryFile() #with NamedTemporaryFile() as fh: if 1: tempfile = fh.name fh.write(os.linesep.join(data.splitlines())) fh.close() fy = 1 / (t_samp * 2.0) # start at zero to get zero for offset/ DC of fft freqs = np.linspace(0, fy, nfft // 2 + 1) start_stage_c = C.c_int(start_stage) stop_stage_c = C.c_int(stop_stage) stdio_flag = C.c_int(0) sta = C.create_string_buffer(station) cha = C.create_string_buffer(channel) net = C.create_string_buffer(network) locid = C.create_string_buffer(locid) unts = C.create_string_buffer(units) if debug: vbs = C.create_string_buffer("-v") else: vbs = C.create_string_buffer("") rtyp = C.create_string_buffer("CS") datime = C.create_string_buffer(date.formatSEED()) fn = C.create_string_buffer(tempfile) nfreqs = C.c_int(freqs.shape[0]) res = clibevresp.evresp(sta, cha, net, locid, datime, unts, fn, freqs, nfreqs, rtyp, vbs, start_stage_c, stop_stage_c, stdio_flag, C.c_int(0)) # optimizing performance, see # http://wiki.python.org/moin/PythonSpeed/PerformanceTips nfreqs, rfreqs, rvec = res[0].nfreqs, res[0].freqs, res[0].rvec h = np.empty(nfreqs, dtype='complex128') f = np.empty(nfreqs, dtype='float64') for i in xrange(nfreqs): h[i] = rvec[i].real + rvec[i].imag * 1j f[i] = rfreqs[i] clibevresp.free_response(res) del nfreqs, rfreqs, rvec, res if freq: return h, f return h
def evalresp(t_samp, nfft, filename, date, station='*', channel='*', network='*', locid='*', units="VEL", freq=False, debug=False): """ Use the evalresp library to extract instrument response information from a SEED RESP-file. :type t_samp: float :param t_samp: Sampling interval in seconds :type nfft: int :param nfft: Number of FFT points of signal which needs correction :type filename: str :param filename: SEED RESP-filename or content of RESP file :type date: UTCDateTime :param date: Date of interest :type station: str :param station: Station id :type channel: str :param channel: Channel id :type network: str :param network: Network id :type locid: str :param locid: Location id :type units: str :param units: Units to return response in. Can be either DIS, VEL or ACC :type debug: bool :param debug: Verbose output to stdout. Disabled by default. :rtype: numpy.ndarray complex128 :return: Frequency response from SEED RESP-file of length nfft """ # evalresp needs files with correct line separators depending on OS data = open(filename, 'rb').read() fh = NamedTemporaryFile() tempfile = fh.name fh.write(os.linesep.join(data.splitlines())) fh.close() fy = 1 / (t_samp * 2.0) # start at zero to get zero for offset/ DC of fft freqs = np.linspace(0, fy, nfft // 2 + 1) start_stage = C.c_int(-1) stop_stage = C.c_int(0) stdio_flag = C.c_int(0) sta = C.create_string_buffer(station) cha = C.create_string_buffer(channel) net = C.create_string_buffer(network) locid = C.create_string_buffer(locid) unts = C.create_string_buffer(units) if debug: vbs = C.create_string_buffer("-v") else: vbs = C.create_string_buffer("") rtyp = C.create_string_buffer("CS") datime = C.create_string_buffer("%d,%3d" % (date.year, date.julday)) fn = C.create_string_buffer(tempfile) nfreqs = C.c_int(freqs.shape[0]) res = clibevresp.evresp(sta, cha, net, locid, datime, unts, fn, freqs, nfreqs, rtyp, vbs, start_stage, stop_stage, stdio_flag, C.c_int(0)) # optimizing performance, see # http://wiki.python.org/moin/PythonSpeed/PerformanceTips nfreqs, rfreqs, rvec = res[0].nfreqs, res[0].freqs, res[0].rvec h = np.empty(nfreqs, dtype='complex128') f = np.empty(nfreqs, dtype='float64') for i in xrange(nfreqs): h[i] = rvec[i].real + rvec[i].imag * 1j f[i] = rfreqs[i] clibevresp.free_response(res) del nfreqs, rfreqs, rvec, res # delete temporary file try: os.remove(tempfile) except: pass if freq: return h, f return h