#!/usr/bin/env python# *-* coding: utf-8 *-*"""Transfer complex resistivity models between two FE grids."""importos# import shapelyfromoptparseimportOptionParserfromshapely.geometryimportPolygonimportcrtomo.gridasCRGridimportnumpyasnp
[docs]defhandle_cmd_options():parser=OptionParser()parser.add_option("--old",dest="old_dir",type="string",help="directory containing the old grid (elem.dat and "+"elec.dat files), (default: old/)",default="old")parser.add_option("--new",dest="new_dir",type="string",help="directory containing the new grid (elem.dat and "+"elec.dat AND rho.dat files), (default: new/)",default="new")parser.add_option("-o","--output",dest="output",help="Output rho file (default: rho_new.dat)",metavar="FILE",default="rho_new.dat")(options,args)=parser.parse_args()returnoptions
[docs]def_load_grid(directory):ifnotos.path.isdir(directory):raiseIOError('directory not found: {0}'.format(directory))forfilenamein(directory+os.sep+'elem.dat',directory+os.sep+'elec.dat',):ifnotos.path.isfile(filename):raiseIOError('filename not found: {0}'.format(filename))cells=[]grid=CRGrid.crt_grid()grid.load_elem_file(directory+os.sep+'elem.dat')grid.load_elec_file(directory+os.sep+'elec.dat')gx=grid.grid['x']gz=grid.grid['z']forx,zinzip(gx,gz):# find all cell that touch this cellcoords=[(xi,zi)forxi,ziinzip(x,z)]p2=Polygon(coords)cells.append(p2)rhofile=directory+os.sep+'rho.dat'ifos.path.isfile(rhofile):rho=np.loadtxt(rhofile,skiprows=1)else:rho=Nonereturngx,gz,cells,rho
[docs]def_almost_equal(a,b):"""Check if the two numbers are almost equal """# arbitrary small number!!!threshold=1e-9diff=np.abs(a-b)return(diff<threshold)
[docs]defmain():options=handle_cmd_options()gx_old,gz_old,cells_old,rho_old=_load_grid(options.old_dir)gx_new,gz_new,cells_new,_=_load_grid(options.new_dir)print('finished preparing grids')rho_new_raw=[]fornew_cell_id,cell_newinenumerate(cells_new):print('working on cell {0} from {1}'.format(new_cell_id,len(cells_new)))# find all cells in the old grid that touch the new one# check using the areaarea=cell_new.areatouching_cells={}forindex,cell_oldinenumerate(cells_old):diff=cell_new.intersection(cell_old)ifdiff.area>0:# store area fractiontouching_cells[index]=diff.area/areaarea_found=np.sum([xforxintouching_cells.items()])if_almost_equal(area_found,1.0):# we found all cells toching the new cellbreak# set the new rho values for this cell# strategy 1: largest winsiftouching_cells:largest_id=max(touching_cells,key=touching_cells.get)rho_new_raw.append(rho_old[largest_id,:])else:rho_new_raw.append([np.nan,np.nan])rho_new=np.array(rho_new_raw)withopen(options.output,'wb')asfid:fid.write(bytes('{0}\n'.format(rho_new.shape[0]),'utf-8',))np.savetxt(fid,rho_new)