def interpolate_grid(self, pad_east=None, pad_north=None, cell_size=None): """ interpolate the irregular model grid onto a regular grid. """ model_obj = ws.WSModel() model_obj.model_fn = self.model_fn model_obj.read_model_file() self.grid_z = model_obj.grid_z.copy() if cell_size is not None: self.cell_size_east = cell_size self.cell_size_north = cell_size else: self.cell_size_east = np.median(model_obj.nodes_east) self.cell_size_north = np.median(model_obj.nodes_north) if pad_east is not None: self.pad_east = pad_east if self.pad_east is None: self.pad_east = np.where(model_obj.nodes_east[0:10] > self.cell_size_east*1.1)[0][-1] if pad_north is not None: self.pad_north = pad_north if self.pad_north is None: self.pad_north = np.where(model_obj.nodes_north[0:10] > self.cell_size_north*1.1)[0][-1] new_east = np.arange(model_obj.grid_east[self.pad_east], model_obj.grid_east[-self.pad_east-1], self.cell_size_east) new_north = np.arange(model_obj.grid_north[self.pad_north], model_obj.grid_north[-self.pad_north-1], self.cell_size_north) model_n, model_e = np.broadcast_arrays(model_obj.grid_north[:, None], model_obj.grid_east[None, :]) new_res_arr = np.zeros((new_north.shape[0], new_east.shape[0], model_obj.grid_z.shape[0])) for z_index in range(model_obj.grid_z.shape[0]): res = model_obj.res_model[:, :, z_index] new_res_arr[:, :, z_index] = interpolate.griddata( (model_n.ravel(), model_e.ravel()), res.ravel(), (new_north[:, None], new_east[None, :])) self.res_array = new_res_arr
def _get_model(self): """ get model to put into array """ model_obj = ws.WSModel() model_obj.model_fn = self.model_fn model_obj.read_model_file() self.cell_size_east = np.median(model_obj.nodes_east) self.cell_size_north = np.median(model_obj.nodes_north) self.pad_east = np.where( model_obj.nodes_east[0:10] > self.cell_size_east * 1.1)[0][-1] self.pad_north = np.where( model_obj.nodes_north[0:10] > self.cell_size_north * 1.1)[0][-1] self.grid_z = model_obj.grid_z.copy() self.res_array = model_obj.res_model[self.pad_north:-self.pad_north, self.pad_east:-self.pad_east, :]
data_fn = os.path.join(dir_path, 'mshn_modem_data_ef05.dat') mfn_tipper = os.path.join(dir_path, r"mshn_tip_err03_NLCG_050.rho") mfn_ws_sm2 = os.path.join(dir_path, r"mshn_sm2_fine_model.05") mfn_ws_sm3 = os.path.join(dir_path, r"mshn_sm3_fine_model.04") mfn_all_sm500 = os.path.join(dir_path, r"mshn_err05_cov03_NLCG_063.rho") #difference between modem and ws grids d_east = 0. d_north = 0. # get all models into useable objects modem_data = modem.Data() modem_data.read_data_file(data_fn) ws_sm2 = ws.WSModel(mfn_ws_sm2) ws_sm3 = ws.WSModel(mfn_ws_sm2) modem_all = modem.Model() modem_all.read_model_file(mfn_all_sm500) modem_tip = modem.Model() modem_tip.read_model_file(mfn_tipper) #--> interpolate on to the base model # smooth over the ws models because their resistivities are so low and # the models are coarse. nr_ws_sm2 = interp_grid(ws_sm2, modem_all, shift_east=d_east, shift_north=d_north, smooth_kernel=None, pad=5) nr_ws_sm3 = interp_grid(ws_sm3, modem_all, shift_east=d_east,
return new_res # ============================================================================== # interpolate all models onto the same grid # ============================================================================== dfn = r"/mnt/hgfs/jpeacock/Documents/Geothermal/TorC/torc_modem_data_ef07_edit.dat" mfn_ws = r"/home/jpeacock/Documents/wsinv3d/torc/inv_01/torc_sm.ws" mfn_tipper = ( r"/mnt/hgfs/jpeacock/Documents/Geothermal/TorC/torc_sm50_tip_cov4_NLCG_012.rho" ) modem_data = modem.Data() modem_data.read_data_file(dfn) model_ws = ws.WSModel(model_fn=mfn_ws) modem_tip = modem.Model() modem_tip.read_model_file(mfn_tipper) # interpolate onto same grid interp_res = interp_grid(model_ws, modem_tip, pad=5) # --> average all as a geometric mean avg_res = (interp_res * modem_tip.res_model)**(1.0 / 2) # avg_res = interp_res x, y = np.meshgrid(modem_tip.grid_east, modem_tip.grid_north) ws_x, ws_y = np.meshgrid(model_ws.grid_east, model_ws.grid_north) kk = 5 kwargs = {"cmap": "jet_r", "vmin": -1, "vmax": 4}
model_fn = r"c:\MinGW32-xy\Peacock\ModEM\WS_StartingModel_03b\Modular_NLCG_012.rho" data_fn = r"c:\MinGW32-xy\Peacock\ModEM\WS_StartingModel_03b\mb_data.dat" md = modem.Data() md.read_data_file(data_fn) mod_model = modem.Model() mod_model.read_model_file(model_fn) new_mod_model = modem.Model() wsd = ws.WSData() wsd.read_data_file(ws_data_fn) wsm = ws.WSModel() wsm.model_fn = ws_model_fn wsm.read_model_file() new_mod_model.station_locations = md.coord_array.copy() new_mod_model.cell_size_east = 500 new_mod_model.cell_size_north = 500 new_mod_model.n_layers = 43 new_mod_model.z1_layer = 10 new_mod_model.pad_east = 12 new_mod_model.pad_north = 12 new_mod_model.pad_stretch_h = 1.5 new_mod_model.z_bottom = 200000 new_mod_model.z_target_depth = 40000 new_mod_model.make_mesh()
r"/home/jpeacock/Documents/wsinv3d/LV/geothermal_deep_02/WS_Station_Locations.txt" ) # difference between modem and ws grids # shift_east = 3700 # shift_north = -3700 shift_east = 4500 shift_north = 1700 modem_data = modem.Data() modem_data.read_data_file(modem_data_fn) modem_mod = modem.Model() modem_mod.read_model_file(modem_model_fn) ws_mod = ws.WSModel(ws_model_fn) ws_mod.read_model_file() ws_data = ws.WSData() ws_data.read_data_file(data_fn=ws_data_fn, station_fn=ws_station_fn) print "Start Time = {0}".format(time.ctime()) pad = 8 north, east = np.broadcast_arrays(ws_mod.grid_north[:, None], ws_mod.grid_east[None, :]) # 2) do a 2D interpolation for each layer, much faster new_res = np.zeros(( modem_mod.grid_north.shape[0], modem_mod.grid_east.shape[0],
def interpolate_model_grid(old_model_fn, new_model_fn, save_path=None, new_fn_basename=None, pad=3, nan_rho=100, shift_east=0, shift_north=0): """ interpolate an old model onto a new model Arguments ------------------ **old_model_fn** : string full path to the old model file, or the file that contains the original model that will be interpolated onto a new grid, new_model_fn **new_model_fn** : string full path to the new model file to interpolate old_model_fn on to. **save_path** : string directory path to save new interpolated model file *default* is os.path.dirname(new_model_fn) **new_fn_basename** : string filename given to the new interpolated model *default* is os.path.basename(new_model_fn+'interp') **pad** : int number of cells which to pad outer values of the grid. Say new_model_fn is larger than old_model_fn, then there will be Nan where the model don't match up, pad will extend values from the given number of cells from the edge of new_model_fn. **nan_rho** : float if there are Nan in the new_model, they will be given this value. *default* is 100 Ohm-m **shift_east** : float shift east of new_model grid relative to the old model grid in meters. *Default* is 0 **shift_north** : float shift north of new_model grid relative to the old model grid in meters. *Default* is 0 """ print 'Interpolating {0} into {1}'.format(old_model_fn, new_model_fn) # check to see if the old model is modem or ws if old_model_fn[-4:] == '.rho': old_mod = modem.Model() old_mod.read_model_file(old_model_fn) else: old_mod = ws.WSModel(old_model_fn) old_mod.read_model_file() # check to see if the new model is modem or ws if new_model_fn[-4:] == '.rho': new_mod = modem.Model() new_mod.read_model_file(new_model_fn) new_ext = '.rho' new_fn_type = 'modem' else: try: new_mod = ws.WSModel(new_model_fn) new_mod.read_model_file() except ValueError: new_mod = ws.WSMesh() new_mod.read_initial_file(new_model_fn) new_ext = '' new_fn_type = 'ws' if save_path is None: save_path = os.path.dirname(new_model_fn) if new_fn_basename is None: fn = os.path.basename(new_model_fn) ext_find = fn.find('.') if ext_find == -1: ext_find = len(fn) - 1 new_fn_basename = '{0}_interp{1}'.format(fn[0:ext_find], new_ext) print 'Start Time = {0}'.format(time.ctime()) # make a grid of old model old_north, old_east = np.broadcast_arrays(old_mod.grid_north[:-1, None], old_mod.grid_east[None, :-1]) #2) do a 2D interpolation for each layer, much faster # make a new array of zeros to put values with shape of new model new_res = np.zeros((new_mod.nodes_north.shape[0], new_mod.nodes_east.shape[0], new_mod.nodes_z.shape[0])) for zz in range(new_mod.nodes_z.shape[0]): try: old_zz = np.where(old_mod.grid_z >= new_mod.grid_z[zz])[0][0] if old_zz >= old_mod.nodes_z.size - 1: old_zz = old_mod.nodes_z.size - 1 except IndexError: old_zz = -1 print 'New depth={0:.2f}; old depth={1:.2f}'.format( new_mod.grid_z[zz], old_mod.grid_z[old_zz]) new_res[:, :, zz] = spi.griddata( (old_north.ravel(), old_east.ravel()), old_mod.res_model[:, :, old_zz].ravel(), (new_mod.grid_north[:-1, None] + shift_north, new_mod.grid_east[None, :-1] + shift_east), method='linear') new_res[0:pad, pad:-pad, zz] = new_res[pad, pad:-pad, zz] new_res[-pad:, pad:-pad, zz] = new_res[-pad - 1, pad:-pad, zz] new_res[:, 0:pad, zz] = new_res[:, pad, zz].repeat(pad).reshape(new_res[:, 0:pad, zz].shape) new_res[:, -pad:, zz] = new_res[:, -pad - 1, zz].repeat(pad).reshape(new_res[:, -pad:, zz].shape) # new_res[np.where(np.nan_to_num(new_res) == 0.0)] = 100.0 if new_fn_type == 'modem': new_mod.write_model_file(save_path=save_path, model_fn_basename=new_fn_basename, res_model=new_res) else: nfid = file(os.path.join(save_path, new_fn_basename), 'w') nfid.write('# interpolated starting model for ws written by mtpy\n') nfid.write('{0} {1} {2} {3}\n'.format(new_mod.nodes_north.shape[0], new_mod.nodes_east.shape[0], new_mod.nodes_z.shape[0], 0)) # write S--> N block for ii, n_node in enumerate(new_mod.nodes_north): nfid.write('{0:>12.1f}'.format(abs(n_node))) if ii != 0 and np.remainder(ii + 1, 5) == 0: nfid.write('\n') elif ii == new_mod.nodes_north.shape[0] - 1: nfid.write('\n') # write W--> E block for jj, e_node in enumerate(new_mod.nodes_east): nfid.write('{0:>12.1f}'.format(abs(e_node))) if jj != 0 and np.remainder(jj + 1, 5) == 0: nfid.write('\n') elif jj == new_mod.nodes_east.shape[0] - 1: nfid.write('\n') # write top--> bottom block for kk, z_node in enumerate(new_mod.nodes_z): nfid.write('{0:>12.1f}'.format(abs(z_node))) if kk != 0 and np.remainder(kk + 1, 5) == 0: nfid.write('\n') elif kk == new_mod.nodes_z.shape[0] - 1: nfid.write('\n') for kk in range(new_mod.nodes_z.shape[0]): for jj in range(new_mod.nodes_east.shape[0]): for ii in range(new_mod.nodes_north.shape[0]): nfid.write('{0:.4e}\n'.format( new_res[(new_mod.nodes_north.shape[0] - 1) - ii, jj, kk])) nfid.close() print 'Wrote file to {0}'.format( os.path.join(save_path, new_fn_basename)) # new_mesh = ws.WSMesh() # new_mesh.write_initial_file(nodes_north=new_mod.nodes_north, # nodes_east=new_mod.nodes_east, # nodes_z=new_mod.nodes_z,/home/jpeacock/Documents/ModEM/LV/geo_err12/lv_geo_err03_cov5_NLCG_065.res # res_model=new_res, # save_path=save_path) print 'End Time = {0}'.format(time.ctime()) return os.path.join(save_path, new_fn_basename)
import mtpy.modeling.ws3dinv as ws import matplotlib.pyplot as plt import os import numpy as np model1_fn = r"/home/jpeacock/Documents/wsinv3d/LV/sm_modem_inv1/lv_sm_modem_inv1_fine_model.03" model2_fn = r"/home/jpeacock/Documents/wsinv3d/LV/sm_modem_inv2/lv_sm_modem_inv2_fine_model.07" dfn = r"/home/jpeacock/Documents/wsinv3d/LV/sm_modem_inv2/WS_data_sm_modem_inv1_8_small.dat" sfn = r"/home/jpeacock/Documents/wsinv3d/LV/WS_Station_Locations.txt" ws_data = ws.WSData() ws_data.read_data_file(data_fn=dfn, station_fn=sfn) m1 = ws.WSModel(model1_fn) m2 = ws.WSModel(model2_fn) new_model = np.sqrt(m1.res_model * m2.res_model) omfid = file(m1.model_fn, 'r') mlines = omfid.readlines() omfid.close() mfid = file(r"/home/jpeacock/Documents/wsinv3d/LV/lv_sm_avg.ws", 'w') mfid.writelines(mlines[0:26]) for kk in range(m1.grid_z.shape[0]): for jj in range(m1.grid_east.shape[0]): for ii in range(m1.grid_north.shape[0]): res_num = new_model[(m1.grid_north.shape[0] - 1) - ii, jj, kk] mfid.write('{0:12.5e}\n'.format(res_num))
r"/home/jpeacock/Documents/ModEM/LV/sm_mb_tipper/lv_mb_tipper_NLCG_043.rho" ) mfn_sm_mb = r"/home/jpeacock/Documents/ModEM/LV/sm_mb_inv1/lv_mb_sm_err12_NLCG_071.rho" # difference between modem and ws grids d_east = -7500.0 d_north = 500.0 # get all models into useable objects modem_data = modem.Data() modem_data.read_data_file(data_fn) base_model = modem.Model() base_model.read_model_file(mfn_avg_sm1) ws_hs1 = ws.WSModel(mfn_ws_hs1) ws_sm1 = ws.WSModel(mfn_ws_sm1) ws_sm2 = ws.WSModel(mfn_ws_sm2) modem_nt = modem.Model() modem_nt.read_model_file(mfn_no_tipper) modem_tip = modem.Model() modem_tip.read_model_file(mfn_tipper) modem_sm_mb = modem.Model() modem_sm_mb.read_model_file(mfn_mb_sm) modem_sm3 = modem.Model() modem_sm3.read_model_file(mfn_sm3)