def read_ModEM(self, datafile=None, respfile=None): if ((datafile is None) and (respfile is None)): print "Please provide data and/or response file" # check data type compatible with provided input files self._check_data_type(datafile, respfile) # determine whether to automatically choose data type if self.data_type is None: find_datatype=True else: find_datatype=False # read files if datafile is not None: mdObj=mtmn.Data() mdObj.read_data_file(datafile) if self.workdir is None: self.workdir=op.dirname(datafile) if find_datatype: self.data_type='data' self.period=mdObj.period_list if respfile is not None: mrObj=mtmn.Data() mrObj.read_data_file(respfile) if self.workdir is None: self.workdir=op.dirname(respfile) if find_datatype: if self.data_type == 'data': self.data_type='residual' else: self.data_type='response' self.period=mrObj.period_list # get period index and period for plotting self.get_plot_period_index() # get mt_dict containing data, responses, or residual depending on # data_type if self.data_type == 'data': self.mt_dict=mdObj.mt_dict elif self.data_type == 'response': self.mt_dict=mrObj.mt_dict elif self.data_type == 'residual': self.mt_dict={} for key in mdObj.mt_dict.keys(): self.mt_dict[key]=mtpt.ResidualPhaseTensor(pt_object1=mdObj.mt_dict[key], pt_object2=mrObj.mt_dict[key])
def _compute_residual_pt(self): """ compute residual phase tensor so the result is something useful to plot """ log_path = os.path.dirname(os.path.dirname(self.fn_list1[0])) log_fn = os.path.join(log_path, 'Residual_PT.log') logfid = file(log_fn, 'w') self._get_freq_list() freq_dict = dict([(np.round(key, 5), value) for value, key in enumerate(self.freq_list)]) num_freq = self.freq_list.shape[0] num_station = len(self.mt_list1) #make a structured array to put stuff into for easier manipulation self.rpt_array = np.zeros(num_station, dtype=[('station', '|S10'), ('freq', (np.float, num_freq)), ('lat', np.float), ('lon', np.float), ('elev', np.float), ('offset', np.float), ('phimin', (np.float, num_freq)), ('phimax', (np.float, num_freq)), ('skew', (np.float, num_freq)), ('azimuth', (np.float, num_freq)), ('geometric_mean', (np.float, num_freq))]) self.residual_pt_list = [] for mm, mt1 in enumerate(self.mt_list1): station_find = False fdict1 = dict([(np.round(ff, 5), ii) for ii, ff in enumerate(mt1.freq)]) for mt2 in self.mt_list2: if mt2.station == mt1.station: logfid.write('{0}{1}{0}\n'.format('=' * 30, mt1.station)) fdict2 = dict([(np.round(ff, 5), ii) for ii, ff in enumerate(mt2.freq)]) #need to make sure only matched frequencies are compared index_1 = [] index_2 = [] for key1 in sorted(fdict1.keys()): try: index_2.append(fdict2[key1]) index_1.append(fdict1[key1]) logfid.write('-' * 20 + '\n') logfid.write('found {0} in both\n'.format(key1)) logfid.write('Index 1={0}, Index 2={1}\n'.format( fdict1[key1], fdict2[key1])) except KeyError: 'Did not find {0:.4e} Hz in {1}'.format( key1, mt2.fn) #need to sort the index list, otherwise weird things happen index_1.sort() index_2.sort() #create new Z objects that have similar frequencies new_z1 = mtpl.mtz.Z(z_array=mt1.z[index_1], zerr_array=mt1.z_err[index_1], freq=mt1.freq[index_1]) new_z2 = mtpl.mtz.Z(z_array=mt2.z[index_2], zerr_array=mt2.z_err[index_2], freq=mt2.freq[index_2]) #make new phase tensor objects pt1 = mtpt.PhaseTensor(z_object=new_z1) pt2 = mtpt.PhaseTensor(z_object=new_z2) #compute residual phase tensor rpt = mtpt.ResidualPhaseTensor(pt1, pt2) rpt.compute_residual_pt(pt1, pt2) #add some attributes to residual phase tensor object rpt.station = mt1.station rpt.lat = mt1.lat rpt.lon = mt1.lon #append to list for manipulating later self.residual_pt_list.append(rpt) #be sure to tell the program you found the station station_find = True #put stuff into an array because we cannot set values of #rpt, need this for filtering. st_1, st_2 = self.station_id self.rpt_array[mm]['station'] = mt1.station[st_1:st_2] self.rpt_array[mm]['lat'] = mt1.lat self.rpt_array[mm]['lon'] = mt1.lon self.rpt_array[mm]['elev'] = mt1.elev self.rpt_array[mm]['freq'][:] = self.freq_list rpt_fdict = dict([(np.round(key, 5), value) for value, key in enumerate(rpt.freq)]) for f_index, freq in enumerate(rpt.freq): aa = freq_dict[np.round(freq, 5)] try: rr = rpt_fdict[np.round(freq, 5)] try: self.rpt_array[mm]['phimin'][aa] = \ rpt.residual_pt.phimin[0][rr] self.rpt_array[mm]['phimax'][aa] = \ rpt.residual_pt.phimax[0][rr] self.rpt_array[mm]['skew'][aa] = \ rpt.residual_pt.beta[0][rr] self.rpt_array[mm]['azimuth'][aa] = \ rpt.residual_pt.azimuth[0][rr] self.rpt_array[mm]['geometric_mean'][aa] = \ np.sqrt(abs(rpt.residual_pt.phimin[0][rr]* rpt.residual_pt.phimax[0][rr])) logfid.write('Freq={0:.5f} '.format(freq)) logfid.write('Freq_list_index={0} '.format( np.where(self.freq_list == freq)[0][0])) logfid.write('rpt_array_index={0} '.format(aa)) logfid.write('rpt_dict={0} '.format(rr)) logfid.write('rpt.freq_index={0} '.format( np.where(rpt.freq == freq)[0][0])) logfid.write('Phi_max={0:2f} '.format( rpt.residual_pt.phimax[0][rr])) logfid.write('Phi_min={0:2f} '.format( rpt.residual_pt.phimin[0][rr])) logfid.write('Skew={0:2f} '.format( rpt.residual_pt.beta[0][rr])) logfid.write('Azimuth={0:2f}\n'.format( rpt.residual_pt.azimuth[0][rr])) except IndexError: print '-' * 50 print mt1.station print 'freq_index for 1: {0}'.format(f_index) print 'freq looking for: {0}'.format(freq) print 'index in big : {0}'.format(aa) print 'index in 1 : {0} '.format(rr) print 'len_1 = {0}, len_2 = {1}'.format( len(mt2.freq), len(mt1.freq)) print 'len rpt_freq = {0}'.format(len( rpt.freq)) except KeyError: print 'Station {0} does not have {1:.5f}Hz'.format( mt1.station, freq) break else: pass if station_find == False: print 'Did not find {0} from list 1 in list 2'.format( mt1.station) # from the data get the relative offsets and sort the data by them self._get_offsets() logfid.close()
def _get_pt(self): """ put pt parameters into something useful for plotting """ ns = len(self.data_obj.mt_dict.keys()) nf = len(self.data_obj.period_list) data_pt_arr = np.zeros( (nf, ns), dtype=[('phimin', np.float), ('phimax', np.float), ('skew', np.float), ('azimuth', np.float), ('east', np.float), ('north', np.float), ('lon', np.float), ('lat', np.float), ('station', 'S10')]) if self.resp_fn is not None: model_pt_arr = np.zeros( (nf, ns), dtype=[('phimin', np.float), ('phimax', np.float), ('skew', np.float), ('azimuth', np.float), ('east', np.float), ('north', np.float), ('lon', np.float), ('lat', np.float), ('station', 'S10')]) res_pt_arr = np.zeros( (nf, ns), dtype=[('phimin', np.float), ('phimax', np.float), ('skew', np.float), ('azimuth', np.float), ('east', np.float), ('north', np.float), ('lon', np.float), ('lat', np.float), ('geometric_mean', np.float), ('station', 'S10')]) for ii, key in enumerate(self.data_obj.mt_dict.keys()): east = self.data_obj.mt_dict[key].grid_east / self.dscale north = self.data_obj.mt_dict[key].grid_north / self.dscale lon = self.data_obj.mt_dict[key].lon lat = self.data_obj.mt_dict[key].lat dpt = self.data_obj.mt_dict[key].pt data_pt_arr[:, ii]['east'] = east data_pt_arr[:, ii]['north'] = north data_pt_arr[:, ii]['lon'] = lon data_pt_arr[:, ii]['lat'] = lat data_pt_arr[:, ii]['phimin'] = dpt.phimin data_pt_arr[:, ii]['phimax'] = dpt.phimax data_pt_arr[:, ii]['azimuth'] = dpt.azimuth data_pt_arr[:, ii]['skew'] = dpt.beta data_pt_arr[:, ii]['station'] = self.data_obj.mt_dict[key].station if self.resp_fn is not None: mpt = self.resp_obj.mt_dict[key].pt try: rpt = mtpt.ResidualPhaseTensor(pt_object1=dpt, pt_object2=mpt) rpt = rpt.residual_pt res_pt_arr[:, ii]['east'] = east res_pt_arr[:, ii]['north'] = north res_pt_arr[:, ii]['lon'] = lon res_pt_arr[:, ii]['lat'] = lat res_pt_arr[:, ii]['phimin'] = rpt.phimin res_pt_arr[:, ii]['phimax'] = rpt.phimax res_pt_arr[:, ii]['azimuth'] = rpt.azimuth res_pt_arr[:, ii]['skew'] = rpt.beta res_pt_arr[:, ii]['station'] = self.data_obj.mt_dict[ key].station res_pt_arr[:, ii]['geometric_mean'] = np.sqrt( abs(rpt.phimin[0] * rpt.phimax[0])) except mtex.MTpyError_PT: print key, dpt.pt.shape, mpt.pt.shape model_pt_arr[:, ii]['east'] = east model_pt_arr[:, ii]['north'] = north model_pt_arr[:, ii]['lon'] = lon model_pt_arr[:, ii]['lat'] = lat model_pt_arr[:, ii]['phimin'] = mpt.phimin model_pt_arr[:, ii]['phimax'] = mpt.phimax model_pt_arr[:, ii]['azimuth'] = mpt.azimuth model_pt_arr[:, ii]['skew'] = mpt.beta model_pt_arr[:, ii]['station'] = self.data_obj.mt_dict[ key].station # make these attributes self.pt_data_arr = data_pt_arr if self.resp_fn is not None: self.pt_resp_arr = model_pt_arr self.pt_resid_arr = res_pt_arr
import mtpy.analysis.pt as mtpt dir_path_01 = r"c:\Users\jrpeacock\Documents\Test_Data\Tongario\original" dir_path_02 = r"c:\Users\jrpeacock\Documents\Test_Data\Tongario\repeat" line_list = [] for station in ([3]): #, 4, 13, 24, 30, 62, 70]): fn_01 = os.path.join(dir_path_01, 'TNG-0{0:02}.edi'.format(station)) fn_02 = os.path.join(dir_path_02, 'TNG-3{0:02}.edi'.format(station)) mt_01 = mt.MT() mt_01.read_mt_file(fn_01) mt_02 = mt.MT() mt_02.read_mt_file(fn_02) # get frequencies where no change is expected no_change = np.where(1. / mt_01.Z.freq < 2) rpt = mtpt.ResidualPhaseTensor(mt_01.pt, mt_02.pt) line_list.append('-' * 72) line_list.append('# *** TNG-0{0:02} ***'.format(station)) line_list.append('-' * 72) for ff, pt_det in zip(rpt.freq, rpt.residual_pt.pt_err): line_list.append('{0:.6f},{1:.3f}'.format(1. / ff, abs(np.median(pt_det) * 100))) #with open(r"c:\Users\jrpeacock\Documents\Test_Data\Tongario\pt_det_err.txt", 'w') as fid: # fid.write('\n'.join(line_list))