def quadrupol_convertfile_miniseed2ts(infile, outfile,combine,invert): try: dummystream = read(infile) except: raise MTpyError_inputarguments('no valid miniSeed file') no_traces = len(dummystream) if no_traces < 4: print 'found only {0} traces in file - cannot combine all traces' lo_outfn = [] lo_tuples = [] for trace in range(no_traces): station, channel, location, network, samplingrate, t0, nsamples, data =\ readfile_obspy(infile,trace) channel = channel.lower() ts_tuple = [station.upper(), channel.lower(), samplingrate,t0, int(nsamples)] ts_tuple.append(data) lo_tuples.append(ts_tuple) if combine is True: lo_newtuples = [] northtup = None southtup = None for tup in lo_tuples: if tup[1].lower()[-1] in ['n']: northtup = tup continue if tup[1].lower()[-1] in ['s']: southtup = tup continue if (northtup is not None) and (southtup is not None): newtup = [northtup[0], 'ex',northtup[2],northtup[3],northtup[4]] if invert is True: data = 0.5*(northtup[5] - southtup[5]) else: data = 0.5*(northtup[5] + southtup[5]) newtup.append(data) newtup = tuple(newtup) elif (northtup is not None): newtup = northtup else: newtup = southtup lo_newtuples.append(newtup) easttup = None westtup = None for tup in lo_tuples: if tup[1].lower()[-1] in ['e']: easttup = tup continue if tup[1].lower()[-1] in ['w']: westtup = tup continue if (easttup is not None) and (westtup is not None): newtup = [easttup[0], 'ey',easttup[2],easttup[3],easttup[4]] if invert is True: data = 0.5*(easttup[5] - westtup[5]) else: data = 0.5*(easttup[5] + westtup[5]) newtup.append(data) newtup = tuple(newtup) elif (easttup is not None): newtup = easttup else: newtup = westtup lo_newtuples.append(newtup) else: lo_newtuples = [] for tup in lo_tuples: if tup[1].lower()[-1] in ['s','w']: if invert is True: newtup = tuple([tup[:4],-tup[4]] ) else: newtup = tup else: newtup = tup lo_newtuples.append(newtup) for tup in lo_newtuples: outfilebase = op.splitext(op.abspath(outfile))[0] newoutfile = '{0}.{1}'.format(outfilebase,tup[1]) outfilename = MTfh.write_ts_file_from_tuple(newoutfile,tup) #print 'wrote file {0}'.format(outfilename) lo_outfn.append(outfilename) return lo_outfn
def convertfile_miniseed2ts(infile, outfile, unit=None, lat = None, lon = None, elev = None): try: dummystream = read(infile) except: raise MTpyError_inputarguments('infile is not miniSeed') no_traces = len(dummystream) lo_outfn = [] for trace in range(no_traces): station, channel, location, network, samplingrate, t0, nsamples, data =\ readfile_obspy(infile,trace) channel = channel.lower() if channel[-1] == 'e': channel = channel[:-1]+ 'y' if channel[-1] == 'n': channel = channel[:-1] +'x' ts_tuple = [station.upper(), channel.lower(), samplingrate,t0, nsamples] if unit is not None: try: unit = unit.lower() except: unit = None # if unit is None: # ts_tuple.append('unknown') if lat is not None: try: lat = MTfT._assert_position_format('lat', lat) except: lat = None # if lat is None: # ts_tuple.append(0.) if lon is not None: try: lon = MTfT._assert_position_format('lon', lon) except: lon = None # if lon is None: # ts_tuple.append(0.) if elev is not None: try: elev = MTfT._assert_position_format('elev', elev) except: elev = None # if elev is None: # ts_tuple.append(0.) ts_tuple.append(data) if outfile.lower().endswith('mseed'): outfilebase = op.splitext(op.abspath(outfile))[0] newoutfile = '{0}.{1}'.format(outfilebase,ts_tuple[1]) else: newoutfile = outfile outfilename = MTfh.write_ts_file_from_tuple(newoutfile,tuple(ts_tuple)) outfilename = newoutfile lo_outfn.append(outfilename) return lo_outfn
def quadrupol_convertfile_miniseed2ts(infile, outfile, combine, invert): try: dummystream = read(infile) except: raise MTpyError_inputarguments('no valid miniSeed file') no_traces = len(dummystream) if no_traces < 4: print 'found only {0} traces in file - cannot combine all traces' lo_outfn = [] lo_tuples = [] for trace in range(no_traces): station, channel, location, network, samplingrate, t0, nsamples, data =\ readfile_obspy(infile,trace) channel = channel.lower() ts_tuple = [ station.upper(), channel.lower(), samplingrate, t0, int(nsamples) ] ts_tuple.append(data) lo_tuples.append(ts_tuple) if combine is True: lo_newtuples = [] northtup = None southtup = None for tup in lo_tuples: if tup[1].lower()[-1] in ['n']: northtup = tup continue if tup[1].lower()[-1] in ['s']: southtup = tup continue if (northtup is not None) and (southtup is not None): newtup = [northtup[0], 'ex', northtup[2], northtup[3], northtup[4]] if invert is True: data = 0.5 * (northtup[5] - southtup[5]) else: data = 0.5 * (northtup[5] + southtup[5]) newtup.append(data) newtup = tuple(newtup) elif (northtup is not None): newtup = northtup else: newtup = southtup lo_newtuples.append(newtup) easttup = None westtup = None for tup in lo_tuples: if tup[1].lower()[-1] in ['e']: easttup = tup continue if tup[1].lower()[-1] in ['w']: westtup = tup continue if (easttup is not None) and (westtup is not None): newtup = [easttup[0], 'ey', easttup[2], easttup[3], easttup[4]] if invert is True: data = 0.5 * (easttup[5] - westtup[5]) else: data = 0.5 * (easttup[5] + westtup[5]) newtup.append(data) newtup = tuple(newtup) elif (easttup is not None): newtup = easttup else: newtup = westtup lo_newtuples.append(newtup) else: lo_newtuples = [] for tup in lo_tuples: if tup[1].lower()[-1] in ['s', 'w']: if invert is True: newtup = tuple([tup[:4], -tup[4]]) else: newtup = tup else: newtup = tup lo_newtuples.append(newtup) for tup in lo_newtuples: outfilebase = op.splitext(op.abspath(outfile))[0] newoutfile = '{0}.{1}'.format(outfilebase, tup[1]) outfilename = MTfh.write_ts_file_from_tuple(newoutfile, tup) #print 'wrote file {0}'.format(outfilename) lo_outfn.append(outfilename) return lo_outfn
def write_ascii_mt_file(self, save_fn=None, save_station='mb', fmt='%.8e', ex=1, ey=1, notch_dict=None): """ write an mtpy time series data file Arguments: ----------- **save_fn** : full path to save file, if None file is saved as: station_YYYYMMDD_hhmmss_df.component ex. mt01_20130206_120000_256.HX **save_station** : string prefix string to add to station number as only integers can be input into metadata of the zen boxes. ex. mb001 **fmt** : string format format of data numbers output to ascii file. *default* is '%.8e' for 8 significan figures in scientific notation. Output: -------- **fn_mt_ascii** : full path to saved file """ try: self.start_date except AttributeError: self.read_3d() time_series = self.convert_counts() if save_fn is None: svfn_directory = os.path.join(os.path.dirname(self.fn), 'TS') if not os.path.exists(svfn_directory): os.mkdir(svfn_directory) svfn_date = ''.join(self.start_date.split('-')) svfn_time = ''.join(self.start_time.split(':')) svfn_station = save_station + self.rx_stn save_fn = os.path.join( svfn_directory, '{0}_{1}_{2}_{3}.{4}'.format(svfn_station, svfn_date, svfn_time, int(self.df), self.ch_cmp.upper())) #calibrate electric channels if self.ch_cmp == 'ex': time_series /= ex elif self.ch_cmp == 'ey': time_series /= ey #apply notch filter if desired if notch_dict is not None: self.apply_adaptive_notch_filter(notch_dict) print 'Filtered notches: ' for nfilt in self.filt_list: if type(nfilt[0]) != str: print '{0}{1:.2f} Hz'.format(' ' * 4, nfilt[0]) header_tuple = (save_station + self.rx_stn, self.ch_cmp, self.df, time.mktime(time.strptime(self.start_dt, datetime_fmt)), time_series.shape[0], 'mV', np.median(self.lat), np.median(self.lon), 0.0, time_series) self.fn_mt_ascii = mtfh.write_ts_file_from_tuple(save_fn, header_tuple, fmt=fmt) print 'Wrote mtpy timeseries file to {0}'.format(self.fn_mt_ascii)
def convertfile_miniseed2ts(infile, outfile, unit=None, lat=None, lon=None, elev=None): try: dummystream = read(infile) except: raise MTpyError_inputarguments('infile is not miniSeed') no_traces = len(dummystream) lo_outfn = [] for trace in range(no_traces): station, channel, location, network, samplingrate, t0, nsamples, data =\ readfile_obspy(infile,trace) channel = channel.lower() if channel[-1] == 'e': channel = channel[:-1] + 'y' if channel[-1] == 'n': channel = channel[:-1] + 'x' ts_tuple = [ station.upper(), channel.lower(), samplingrate, t0, nsamples ] if unit is not None: try: unit = unit.lower() except: unit = None # if unit is None: # ts_tuple.append('unknown') if lat is not None: try: lat = MTfT._assert_position_format('lat', lat) except: lat = None # if lat is None: # ts_tuple.append(0.) if lon is not None: try: lon = MTfT._assert_position_format('lon', lon) except: lon = None # if lon is None: # ts_tuple.append(0.) if elev is not None: try: elev = MTfT._assert_position_format('elev', elev) except: elev = None # if elev is None: # ts_tuple.append(0.) ts_tuple.append(data) if outfile.lower().endswith('mseed'): outfilebase = op.splitext(op.abspath(outfile))[0] newoutfile = '{0}.{1}'.format(outfilebase, ts_tuple[1]) else: newoutfile = outfile outfilename = MTfh.write_ts_file_from_tuple(newoutfile, tuple(ts_tuple)) outfilename = newoutfile lo_outfn.append(outfilename) return lo_outfn