def main(dr): #**************** TESTING PARAMS (WOULD BE REMOVED)*******# CREATE_VIM_MODEL = True #**************** ---------------------------------*******# import gmi_misc #**************** PRINT HEADER ***************************# gmi_misc.print_header() print("Script no. 1: Creation of a tesseroid model") #**************** ------------ ***************************# #**************** GET WORKING DIRECTORY ******************# import os old_cwd = os.getcwd() gmi_misc.info('Current directory: ' + old_cwd) try: os.chdir(dr) except: gmi_misc.error('CAN NOT OPEN WORKING DIRECTORY ' + dr + ', ABORTING...') gmi_misc.message('Working directory: ' + os.getcwd()) #**************** --------------------- ******************# #**************** read parameters from file **************# import gmi_config gmi_config.read_config() #**************** ------------------------- **************# result_folder = gmi_misc.init_result_folder() import numpy as np n_lon, n_lat, X, Y = gmi_misc.create_tess_cpoint_grid() #*********************************************************# Z_bot = gmi_misc.read_surf_grid(gmi_config.BOT_SURFACE) Z_top = gmi_misc.read_surf_grid(gmi_config.TOP_SURFACE) if gmi_config.MULTIPLICATOR != 1.0: gmi_misc.warning( "NOTE: SUSCEPTIBILITY OF EACH TESSEROID IS MULTIPLIED BY " + str(gmi_config.MULTIPLICATOR)) _create_tess_model_file('model', 1.0 * gmi_config.MULTIPLICATOR, X, Y, Z_top, Z_bot) if CREATE_VIM_MODEL: if ('.vim' in gmi_config.INIT_SOLUTION) or ('.vis' in gmi_config.INIT_SOLUTION): sus_grid = gmi_misc.read_sus_grid(gmi_config.INIT_SOLUTION) dm1, dm2, x0 = gmi_misc.convert_surf_grid_to_xyz(sus_grid) _create_tess_model_file(result_folder + '/model_with_x0', x0, X, Y, Z_top, Z_bot) _create_tess_model_file(result_folder + '/model_with_x0_mult', x0 * gmi_config.MULTIPLICATOR, X, Y, Z_top, Z_bot) np.savetxt(result_folder + '/init_solution.x0', x0) gmi_misc.write_sus_grid_to_file( x0, result_folder + 'init_solution.xyz') #**************** WRITE MD5 PARAMS **************# import hashlib file_name = 'model.magtess' with open(file_name, 'r') as file_to_check: # read contents of the file data = file_to_check.read() # pipe contents of the file through md5_returned = hashlib.md5(data.encode('utf-8')).hexdigest() #Save dictionary = { 'stage1': md5_returned, 'stage2': '', 'stage3': '', 'stage4': '', } np.save('checksums.npy', dictionary) #**************** ---------------- **************# #**************** RETURN BACK TO INITIAL PATH ***# os.chdir(old_cwd)
def main(dr): import gmi_misc #**************** PRINT HEADER ***************************# gmi_misc.print_header() print("Script no. 4: Inversion") #**************** ------------ ***************************# #**************** GET WORKING DIRECTORY ******************# import os old_cwd = os.getcwd() gmi_misc.info('Current directory: ' + old_cwd) try: os.chdir(dr) except: gmi_misc.error('CAN NOT OPEN WORKING DIRECTORY ' + dr + ', ABORTING...') gmi_misc.info('WORKING DIRECTORY: ' + os.getcwd()) #**************** --------------------- ******************# #**************** read parameters from file **************# import gmi_config gmi_config.read_config() #**************** ------------------------- **************# #************ check if previous stages were launched *****# ''' import gmi_hash stages = [0,0,0] stages, dictionary = gmi_hash.read_dict('checksums.npy') err = 0 if stages[0] == -1: gmi_misc.warning('model.magtess was changed after the run of Script 1, restart Script no. 1 first! ABORTING...') err += 1 elif stages[0] == 0: gmi_misc.warning('model.magtess was changed after the run of Script 1, restart Script no. 1 first! ABORTING...') err += 1 else: pass if stages[1] == -1: gmi_misc.warning('Folder model was changed after the run of Script 2, restart Script no. 2 first! ABORTING...') err += 1 elif stages[1] == 0: gmi_misc.warning('Folder model was changed after the run of Script 2, restart Script no. 2 first! ABORTING...') err += 1 else: pass if stages[2] == -1: gmi_misc.warning('Design matrix was changed after the run of Script 3, restart Script no. 3 first! ABORTING...') err += 1 elif stages[2] == 0: gmi_misc.warning('Design matrix was changed after the run of Script 3, restart Script no. 3 first! ABORTING...') err += 1 else: pass if err > 0: gmi_misc.error('CHECKSUM FAILED, ABORTING!') ''' #**************** --------------------- ******************# import matplotlib.pyplot as plt import pyshtools import numpy as np from scipy.interpolate import griddata import convert_shtools_grids gmi_misc.warning("INPUT GRID IS MULTIPLIED BY " + str(gmi_config.MULTIPLICATOR)) ##READ DESIGN MATRIX A = np.load('design_matrix_shcoeff.npy') A_alldeg = np.load('design_matrix_ufilt_shcoeff.npy') import scipy.io A = np.transpose(A) A_alldeg = np.transpose(A_alldeg) ##READ INITIAL SOLUTION #read initial solution sus_grid = gmi_misc.read_sus_grid(gmi_config.INIT_SOLUTION) dm1, dm2, x0 = gmi_misc.convert_surf_grid_to_xyz(sus_grid) d_ideal = np.matmul(A, x0) ##READ OBSERVED GRID obs_grid = gmi_misc.read_data_grid( gmi_config.OBSERVED_DATA) #* gmi_config.MULTIPLICATOR ##READ SUBTRACTABLE FIELD try: sub_grid = gmi_misc.read_data_grid( gmi_config.SUBTRACT_DATA) * gmi_config.MULTIPLICATOR except IOError as err: print("CAN NOT OPEN SUBTRACTEBLE DATAFILE: {0}".format(err)) sub_grid = obs_grid * 0.0 ##REMOVE SUBTRACTABLE FIELD obs_grid = obs_grid - sub_grid def _save_powerspectrum(specname, shc): spectrum = shc.spectrum() deg = shc.degrees() with open(specname, 'w') as f: for i in range(len(deg)): f.write(str(deg[i]) + ' ' + str(spectrum[i]) + '\n') ##CONVERT OBSERVED GRID INTO SHCOEFF obs_sht_grid = pyshtools.SHGrid.from_array(obs_grid) obs_sht_shcoeff = obs_sht_grid.expand(normalization='schmidt') obs_sht_shcoeff.to_file("obs_sht_shcoeff.sht_shcoeff") ##save unfiltered input grid obs_sht_grid = obs_sht_shcoeff.expand(grid='DH2') glon, glat, gval = convert_shtools_grids.convert_sht_grid_to_xyz( obs_sht_grid.to_array()) gmi_misc.write_xyz_grid_to_file(glon, glat, gval, 'obs_grid.xyz') _save_powerspectrum('obs_sht_shcoeff.spec', obs_sht_shcoeff) ##FILTER OBSERVED GRID INTO SHCOEFF obs_sht_shcoeff_trunc = gmi_misc.remove_lw_sh_coeff( obs_sht_shcoeff, gmi_config.N_MIN_CUTOFF) obs_sht_shcoeff_trunc.to_file("obs_sht_shcoeff_trunc.sht_shcoeff") ##save filtered input grid obs_sht_grid_filt = obs_sht_shcoeff_trunc.expand(grid='DH2') glon, glat, gval = convert_shtools_grids.convert_sht_grid_to_xyz( obs_sht_grid_filt.to_array()) gmi_misc.write_xyz_grid_to_file(glon, glat, gval, 'obs_grid_filt.xyz') _save_powerspectrum('obs_sht_shcoeff_trunc.spec', obs_sht_shcoeff_trunc) d = gmi_misc.read_coeffs_from_text_file( "obs_sht_shcoeff_trunc.sht_shcoeff", gmi_config.N_MIN_CUTOFF) n_coeff = len(d) #SOLVING import gmi_inv_methods print("d_ideal (np.matmul(A, x0)) = " + str(d_ideal)) print("d = " + str(d)) print("|d_ideal - d| = " + str(np.linalg.norm(d_ideal - d))) h = gmi_inv_methods.Projected_Gradient(A, d, x0) print("x0 = " + str(x0)) print("h = " + str(h)) print("|x0 - h| = " + str(np.linalg.norm(x0 - h))) d_res = np.matmul(A_alldeg, h) res_sht_shcoeff = gmi_misc.convert_result_into_shtools_format( d_res, 'res_sht_shcoeff.sht_shcoeff') res_sht_grid = res_sht_shcoeff.expand(grid='DH2') glon, glat, gval = convert_shtools_grids.convert_sht_grid_to_xyz( res_sht_grid.to_array()) gmi_misc.write_xyz_grid_to_file(glon, glat, gval, 'res_grid.xyz') _save_powerspectrum('res_sht_shcoeff.spec', res_sht_shcoeff) res_sht_shcoeff_trunc = gmi_misc.remove_lw_sh_coeff( res_sht_shcoeff, gmi_config.N_MIN_CUTOFF) res_sht_shcoeff_trunc.to_file("res_sht_shcoeff_trunc.sht_shcoeff") res_sht_grid_filt = res_sht_shcoeff_trunc.expand(grid='DH2') glon, glat, gval = convert_shtools_grids.convert_sht_grid_to_xyz( res_sht_grid_filt.to_array()) gmi_misc.write_xyz_grid_to_file(glon, glat, gval, 'res_grid_filt.xyz') _save_powerspectrum('res_sht_shcoeff_trunc.spec', res_sht_shcoeff_trunc) ###################### gmi_misc.write_sus_grid_to_file(h, 'res.xyz') gmi_misc.write_sus_grid_to_file( x0 - h, 'x0-res.xyz', ) gmi_misc.write_sus_grid_to_file(x0, 'x0.xyz') #save result result_folder = gmi_misc.init_result_folder() import shutil shutil.copyfile('input.txt', './' + result_folder + '/' + 'input.txt') shutil.copyfile('x0-res.xyz', './' + result_folder + '/' + 'x0-res.xyz') shutil.copyfile('x0.xyz', './' + result_folder + '/' + 'x0.xyz') shutil.copyfile('res.xyz', './' + result_folder + '/' + 'res.xyz') shutil.copyfile('obs_grid_filt.xyz', './' + result_folder + '/' + 'obs_grid_filt.xyz') shutil.copyfile('obs_sht_shcoeff_trunc.spec', './' + result_folder + '/' + 'obs_sht_shcoeff_trunc.spec') shutil.copyfile( 'obs_sht_shcoeff_trunc.sht_shcoeff', './' + result_folder + '/' + 'obs_sht_shcoeff_trunc.sht_shcoeff') shutil.copyfile('obs_grid.xyz', './' + result_folder + '/' + 'obs_grid.xyz') shutil.copyfile('obs_sht_shcoeff.spec', './' + result_folder + '/' + 'obs_sht_shcoeff.spec') shutil.copyfile('obs_sht_shcoeff.sht_shcoeff', './' + result_folder + '/' + 'obs_sht_shcoeff.sht_shcoeff') shutil.copyfile('res_grid_filt.xyz', './' + result_folder + '/' + 'res_grid_filt.xyz') shutil.copyfile('res_sht_shcoeff_trunc.spec', './' + result_folder + '/' + 'res_sht_shcoeff_trunc.spec') shutil.copyfile( 'res_sht_shcoeff_trunc.sht_shcoeff', './' + result_folder + '/' + 'res_sht_shcoeff_trunc.sht_shcoeff') shutil.copyfile('res_grid.xyz', './' + result_folder + '/' + 'res_grid.xyz') shutil.copyfile('res_sht_shcoeff.spec', './' + result_folder + '/' + 'res_sht_shcoeff.spec') shutil.copyfile('res_sht_shcoeff.sht_shcoeff', './' + result_folder + '/' + 'res_sht_shcoeff.sht_shcoeff') shutil.copyfile('nlssubprob.dat', './' + result_folder + '/' + 'nlssubprob.dat') try: shutil.copyfile('video_log.mp4', './' + result_folder + '/' + 'video_log.mp4') except: print('could not cave video') #**************** RETURN BACK TO INITIAL PATH ***# os.chdir(old_cwd)
def main(dr): import gmi_misc #**************** PRINT HEADER ***************************# gmi_misc.print_header() print("Grid reading test") #**************** ------------ ***************************# #**************** GET WORKING DIRECTORY ******************# import os old_cwd = os.getcwd() gmi_misc.info('Current directory: ' + old_cwd) try: os.chdir(dr) except: gmi_misc.error('CAN NOT OPEN WORKING DIRECTORY ' + dr + ', ABORTING...') gmi_misc.info('WORKING DIRECTORY: ' + os.getcwd()) #**************** --------------------- ******************# #**************** read parameters from file **************# import gmi_config gmi_config.read_config() #**************** ------------------------- **************# #BODY*****************************************************# import matplotlib.pyplot as plt import pyshtools import numpy as np from scipy.interpolate import griddata #read design matrices try: A = np.load('design_matrix_shcoeff.npy') A = np.transpose(A) except: print("CAN NOT OPEN SH COEFF DESIGN MATRIX") exit(-1) #read initial solution sus_grid = gmi_misc.read_sus_grid(gmi_config.INIT_SOLUTION) dm1, dm2, x0 = gmi_misc.convert_surf_grid_to_xyz(sus_grid) obs_matmul = np.matmul(A, x0) #read PRECALCULATED observed grid all degrees try: raw_grid = gmi_misc.read_data_grid(gmi_config.OBSERVED_DATA) except IOError as err: print("CAN NOT OPEN OBSERVED DATAFILE: {0}".format(err)) exit(-1) shtools_inp_grid = pyshtools.SHGrid.from_array(raw_grid) sht_obs_tessfc_alldegrees = shtools_inp_grid.expand( normalization='schmidt') sht_obs_tessfc = gmi_misc.remove_lw_sh_coeff(sht_obs_tessfc_alldegrees, gmi_config.N_MIN_CUTOFF) sht_obs_tessfc.to_file("test_sht_obs_tessfc.coeff") obs_tessfc = gmi_misc.read_coeffs_from_text_file( "test_sht_obs_tessfc.coeff", gmi_config.N_MIN_CUTOFF) obs_diff = (obs_matmul - obs_tessfc) print(obs_diff) print(np.linalg.norm(obs_diff)) #*********************************************************# #**************** RETURN BACK TO INITIAL PATH ***# os.chdir(old_cwd)
def plot(self): import gmi_config import gmi_gmt old_cwd = switch_path(self.GMI_PATH) self.graphicsScene_PlotScene.clear() #new thing config = gmi_config.read_config() fname = '' pname = '' uname = '' units = '' colorsch = '' min = 0 max = 0 surf = False current_plot = str(self.comboBox_GridList.currentText()) if current_plot == 'TOP_SURFACE': fname = config.get('Global Tesseroid Model', 'TOP_SURFACE') pname = 'Top Surface' colorsch = 'haxby' uname = 'Depth' units = 'kM' min = -10 max = 15 surf = True grid = gmi_misc.read_surf_grid(fname) grid = grid / 1000.0 elif current_plot == 'BOT_SURFACE': fname = config.get('Global Tesseroid Model', 'BOT_SURFACE') pname = 'Top Surface' colorsch = 'haxby' uname = 'Depth' units = 'kM' min = -70 max = -5 surf = True grid = gmi_misc.read_surf_grid(fname) grid = grid / 1000.0 elif current_plot == 'OBSERVED_DATA': fname = config.get('Inversion', 'OBSERVED_DATA') pname = 'Observed Field' colorsch = 'polar' uname = 'Magnetic field' units = 'nT' min = -12 max = 12 surf = False grid = gmi_misc.read_surf_grid(fname) elif current_plot == 'SUBTRACT_DATA': fname = config.get('Inversion', 'SUBTRACT_DATA') pname = 'Field to be Removed from Observed' colorsch = 'polar' uname = 'Magnetic field' units = 'nT' min = -12 max = 12 surf = False grid = gmi_misc.read_surf_grid(fname) elif current_plot == 'INIT_SOLUTION': x0_name = config.get('Inversion', 'INIT_SOLUTION') pname = 'Initial solution' colorsch = 'haxby' uname = 'Susceptibility' units = 'SI' min = 0 max = 0.1 grid = gmi_misc.read_sus_grid(x0_name) #if float(config.get('Inversion', 'MULTIPLICATOR')) != 1.0: # max = max * float(config.get('Inversion', 'MULTIPLICATOR')) # #units = 'SI'.format(float(config.get('Inversion', 'MULTIPLICATOR'))) else: pass gmi_gmt.plot_global_grid(grid, surf, pname, min, max, colorsch, uname, units, 1) plot_pixmap = QtGui.QPixmap('temp.png') self.graphicsScene_PlotScene.addPixmap( plot_pixmap.scaledToHeight( self.graphicsView_PlotView.geometry().height() * self.zoomfactor)) #self.graphicsScene_PlotScene.setSceneRect(self.graphicsView_PlotView.geometry().x(), self.graphicsView_PlotView.geometry().y(), self.graphicsView_PlotView.geometry().width(), self.graphicsView_PlotView.geometry().height()) self.graphicsView_PlotView.setScene(self.graphicsScene_PlotScene) self.graphicsView_PlotView.show() output_folder = gmi_misc.init_result_folder() import shutil shutil.copyfile('temp.png', './' + output_folder + '/' + current_plot + '.png') switch_path_back(old_cwd)
def spec(self): import gmi_config import gmi_gmt old_cwd = switch_path(self.GMI_PATH) self.graphicsScene_PlotScene.clear() #new thing config = gmi_config.read_config() plot_cutoff = False current_plot = str(self.comboBox_GridList.currentText()) if current_plot == 'TOP_SURFACE': fname = config.get('Global Tesseroid Model', 'TOP_SURFACE') grid = gmi_misc.read_surf_grid(fname) norm = 'unnorm' units = 'kM' grid = grid / 1000.0 elif current_plot == 'BOT_SURFACE': fname = config.get('Global Tesseroid Model', 'BOT_SURFACE') grid = gmi_misc.read_surf_grid(fname) norm = 'unnorm' units = 'kM' grid = grid / 1000.0 elif current_plot == 'OBSERVED_DATA': fname = config.get('Inversion', 'OBSERVED_DATA') norm = 'schmidt' units = 'nT' plot_cutoff = True grid = gmi_misc.read_data_grid(fname) elif current_plot == 'SUBTRACT_DATA': fname = config.get('Inversion', 'SUBTRACT_DATA') norm = 'schmidt' units = 'nT' plot_cutoff = True grid = gmi_misc.read_data_grid(fname) elif current_plot == 'INIT_SOLUTION': x0_name = config.get('Inversion', 'INIT_SOLUTION') norm = 'unnorm' units = 'SI' grid = gmi_misc.read_sus_grid(x0_name) else: pass import matplotlib.pyplot as plt import matplotlib.patches as mpatches import pyshtools grid_sht = pyshtools.SHGrid.from_array(grid) shc_sht = grid_sht.expand(normalization=norm) spectrum = shc_sht.spectrum() deg = shc_sht.degrees() print(deg) #plotting plt.plot(deg[1:], np.log10(spectrum)[1:], '-', lw=0.6) a_yticks = np.array([1, 0.1, 0.01, 0.001, 0.0001]) plt.yticks(np.log10(a_yticks), a_yticks.astype(str)) a_xticks = np.append(np.array([1]), np.arange(10, max(deg), 10)) if plot_cutoff: a_xticks = np.append( a_xticks, np.array( [int(config.get('Spherical Harmonics', 'N_MIN_CUTOFF'))])) plt.xticks(a_xticks, a_xticks.astype(str)) plt.title(current_plot + ' power spectrum') plt.xlabel('SH degree') plt.ylabel('Power [' + units + '^2]') plt.grid() plt.savefig('temp.png') plt.clf() plt.close() plot_pixmap = QtGui.QPixmap('temp.png') self.graphicsScene_PlotScene.addPixmap( plot_pixmap.scaledToHeight( self.graphicsView_PlotView.geometry().height() * 0.95)) #self.graphicsScene_PlotScene.setSceneRect(self.graphicsView_PlotView.geometry().x(), self.graphicsView_PlotView.geometry().y(), self.graphicsView_PlotView.geometry().width(), self.graphicsView_PlotView.geometry().height()) self.graphicsView_PlotView.setScene(self.graphicsScene_PlotScene) self.graphicsView_PlotView.show() output_folder = gmi_misc.init_result_folder() import shutil shutil.copyfile( 'temp.png', './' + output_folder + '/' + current_plot + '_spec.png') switch_path_back(old_cwd)
def PlotResult(self): import gmi_config import gmi_gmt old_cwd = switch_path(self.GMI_PATH) self.graphicsScene_ResultPlotScene.clear() #new thing config = gmi_config.read_config() output_folder = gmi_misc.init_result_folder() fname = str(self.listWidget_ResultsList.currentItem().text()) indrem = 3 if '.dat' in fname: current_plot = output_folder + '/' + fname dat = np.loadtxt(current_plot) import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot(dat[:, 0], dat[:, 1]) ax.set(xlabel='i', ylabel='val', title='.dat file plot') ax.grid() fig.savefig("temp.png") #plt.show() plt.clf() plt.close() indrem = 3 elif '.spec' in fname: current_plot = output_folder + '/' + fname dat = np.loadtxt(current_plot) import matplotlib.pyplot as plt plt.plot(dat[1:, 0], np.log10(dat[:, 1])[1:], '-', lw=0.6) a_yticks = np.array([1, 0.1, 0.01, 0.001, 0.0001]) plt.yticks(np.log10(a_yticks), a_yticks.astype(str)) a_xticks = np.append(np.array([1]), np.arange(10, np.ndarray.max(dat[:, 0]), 10)) a_xticks = np.append( a_xticks, np.array( [int(config.get('Spherical Harmonics', 'N_MIN_CUTOFF'))])) plt.xticks(a_xticks, a_xticks.astype(str)) plt.title(current_plot + ' power spectrum') plt.xlabel('SH degree') plt.ylabel('Power [SI^2]') plt.grid() plt.savefig('temp.png') plt.clf() plt.close() indrem = 4 elif '.sht_shcoeff' in fname: import pyshtools import matplotlib.pyplot as plt urrent_plot = output_folder + '/' + fname sht_shcoeff = pyshtools.SHCoeffs.from_file(fname) sht_shcoeff.plot_spectrum2d() plt.savefig('temp.png') plt.clf() plt.close() indrem = 11 else: current_plot = output_folder + '/' + fname surf = False if 'grid' in fname: pname = fname colorsch = 'polar' uname = 'Magnetic Field' units = 'nT' min = -12 max = 12 grid = gmi_misc.read_surf_grid(current_plot) else: pname = fname colorsch = 'haxby' uname = 'Susceptibility' units = 'SI' min = 0 max = 0.1 grid = gmi_misc.read_sus_grid(current_plot) indrem = 3 if gmi_config.T_DO_TILES: type_p = 2 else: type_p = 1 gmi_gmt.plot_global_grid(grid, surf, pname, min, max, colorsch, uname, units, type_p) result_pixmap = QtGui.QPixmap('temp.png') self.graphicsScene_ResultPlotScene.addPixmap( result_pixmap.scaledToHeight( self.graphicsView_ResultPlotView.geometry().height() * 0.95)) self.graphicsView_ResultPlotView.setScene( self.graphicsScene_ResultPlotScene) self.graphicsView_ResultPlotView.show() import shutil shutil.copyfile( 'temp.png', './' + output_folder + '/' + self.listWidget_ResultsList.currentItem().text()[:-indrem] + 'png') switch_path_back(old_cwd)