def _create_tess_model_file(fname, suscept, x_grid, y_grid, z_topg, z_botg): import numpy as np import os import gmi_config gmi_config.read_config() import gmi_misc nlat, nlon = x_grid.shape if gmi_config.T_DO_TILES: width = gmi_config.T_WIDTH minlon = gmi_config.T_LON_MIN - gmi_config.T_EDGE_EXT maxlon = gmi_config.T_LON_MAX + gmi_config.T_EDGE_EXT minlat = gmi_config.T_LAT_MIN - gmi_config.T_EDGE_EXT maxlat = gmi_config.T_LAT_MAX + gmi_config.T_EDGE_EXT else: width = gmi_config.WIDTH minlon = gmi_config.LON_MIN maxlon = gmi_config.LON_MAX minlat = gmi_config.LAT_MIN maxlat = gmi_config.LAT_MAX with open(fname + '.tess', 'w') as tessfile: k = 0 for i in range(nlat - 1, -1, -1): for j in range(nlon): if isinstance(suscept, np.ndarray): sus_curr = suscept[k] elif isinstance(suscept, float): sus_curr = suscept else: pass if gmi_misc.check_if_in_boundary(x_grid[i, j], y_grid[i, j], minlon, maxlon, minlat, maxlat): string = str(x_grid[i, j] - width / 2.0) + ' ' + str( x_grid[i, j] + width / 2.0) + ' ' + str( y_grid[i, j] - width / 2.0) + ' ' + str( y_grid[i, j] + width / 2.0) + ' ' + str( z_topg[i, j]) + ' ' + str( z_botg[i, j]) + ' 1.0 ' + str(sus_curr) tessfile.write(string + '\n') k = k + 1 if not os.path.isfile(gmi_config.IGRF_COEFF_FILENAME): gmi_misc.warning( 'main field SH model ' + gmi_config.IGRF_COEFF_FILENAME + ' is missing, downloading IGRF13 from https://www.ngdc.noaa.gov/IAGA/vmod/geomag70_linux.tar.gz as a substitute! Check your config file' ) _download_igrf() gmi_config.IGRF_COEFF_FILENAME = 'geomag70_linux/IGRF13.COF' os.system(gmi_config.TESSUTIL_MAGNETIZE_MODEL_FILENAME + ' ' + gmi_config.IGRF_COEFF_FILENAME + ' ' + fname + '.tess ' + str(gmi_config.IGRF_DAY) + ' ' + str(gmi_config.IGRF_MONTH) + ' ' + str(gmi_config.IGRF_YEAR) + ' ' + fname + '.magtess') if os.path.isfile(fname + '.tess'): gmi_misc.ok("Magnetic tesseroid model " + '\033[1m' + fname + '.tess' + '\033[0m' + " is created") else: gmi_misc.error("model.magtess WAS NOT CREATED, CHECK IF " + '\033[1m' + gmi_config.TESSUTIL_MAGNETIZE_MODEL_FILENAME + '\033[0m' + " IS WORKING PROPERLY")
def main(dr): #**************** TESTING PARAMS (WOULD BE REMOVED)*******# TRUNCATE = True #**************** ---------------------------------*******# import gmi_misc #**************** PRINT HEADER ***************************# gmi_misc.print_header() print ("Script no. 3: Creation of design matrices") #**************** ------------ ***************************# #**************** 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') if __name__ == '__main__': err = 0 if stages[0] == -1: err += 1 gmi_misc.warning('model.magtess was changed after the run of Script 1, restart Script no. 1 first! ABORTING...') elif stages[0] == 0: err += 1 gmi_misc.warning('model.magtess was changed after the run of Script 1, restart Script no. 1 first! ABORTING...') else: pass if stages[1] == -1: err += 1 gmi_misc.warning('Folder model was changed after the run of Script 2, restart Script no. 2 first! ABORTING...') elif stages[1] == 0: err += 1 gmi_misc.warning('Folder model was changed after the run of Script 2, restart Script no. 2 first! ABORTING...') else: pass if err > 0: gmi_misc.error('CHECKSUM FAILED, ABORTING!') ''' #**************** --------------------- ******************# #**************** CREATE DESIGN MATRICES *****************# import os import glob import pyshtools import numpy as np import glob, re models_filenames = glob.glob('layer*.magtess') try: models_filenames.sort(key=lambda f: int(re.sub('\D', '', f))) #good initial sort but doesnt sort numerically very well sorted(models_filenames) #sort numerically in ascending order except: gmi_misc.error('CHECK FILENAMES IN LAYERS FOLDER - THERE SHOULD BE INTEGER NUMBERS IN FILENAMES TO INDICATE THE ORDER OF SURFACES') gmi_misc.message('All layers: ' + str(models_filenames)) for li,this_model in zip(range(len(models_filenames)), models_filenames): this_model_noext = this_model.split('.')[0] os.chdir(this_model_noext) coefflist = glob.glob("*.coeff") os.chdir('..') n_tess = len(coefflist) if n_tess == 0: gmi_misc.error("NO CALCULATED SH MODELS OF EACH TESSEROID'S MAGNETIC FIELD") exit(-1) if gmi_config.MULTIPLICATOR != 1.0: gmi_misc.warning("NOTE: SUSCEPTIBILITY OF EACH TESSEROID IS MULTIPLIED BY "+ str(gmi_config.MULTIPLICATOR)) coeff_filename = this_model_noext + '/tess_n' + str(0) + '.coeff' b = gmi_misc.read_coeffs_from_text_file(coeff_filename, gmi_config.N_MIN_CUTOFF) n_vals = len(b) gmi_misc.message('Assemblying design matrices...') from tqdm import tqdm A = np.zeros((n_tess, n_vals)) A_ufilt = np.zeros((n_tess, n_vals)) #if __name__ == '__main__': for i in tqdm(range(n_tess)): coeff_filename = this_model_noext + '/tess_n' + str(i) + '.coeff' b = gmi_misc.read_coeffs_from_text_file(coeff_filename, gmi_config.N_MIN_CUTOFF) b_ufilt = gmi_misc.read_coeffs_from_text_file(coeff_filename, 0) A[i, :] = b[:] A_ufilt[i, :] = b_ufilt[:] #**************** SAVE MATRICES *****************# if((len(models_filenames)) == 1): np.save('design_matrix_shcoeff', A) np.save('design_matrix_ufilt_shcoeff', A_ufilt) else: np.save(this_model_noext + '_design_matrix_shcoeff', A) np.save(this_model_noext + '_design_matrix_ufilt_shcoeff', A_ufilt) #**************** ------------- *****************# gmi_misc.ok('...done') #**************** WRITE MD5 PARAMS **************# ''' import hashlib SHAhash = hashlib.md5() f1 = open('design_matrix_shcoeff.npy', 'rb') while 1: # Read file in as little chunks buf = f1.read(4096) if not buf : break SHAhash.update(hashlib.md5(buf).hexdigest().encode('utf-8')) f1.close() f2 = open('design_matrix_ufilt_shcoeff.npy', 'rb') while 1: # Read file in as little chunks buf = f2.read(4096) if not buf : break SHAhash.update(hashlib.md5(buf).hexdigest().encode('utf-8')) f2.close() dictionary['stage3'] = SHAhash.hexdigest() dictionary['stage4'] = '' np.save('checksums.npy', dictionary) ''' #**************** ---------------- **************# #**************** RETURN BACK TO INITIAL PATH ***# os.chdir(old_cwd)
def main(dr): import numpy as np import gmi_misc #**************** PRINT HEADER ***************************# gmi_misc.print_header() print ("Script no. 2: Calculation of the magnetic field of each tesseroid in the 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.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') if __name__ == '__main__': 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...') elif stages[0] == 0: gmi_misc.warning('model.magtess was changed after the run of Script 1, restart Script no. 1 first! ABORTING...') else: pass if err > 0: gmi_misc.error('CHECKSUM FAILED, ABORTING!') #**************** --------------------- ******************# #**************** CREATE CALCULATION GRID ****************# gmi_misc.create_calc_grid('grid.txt') #**************** --------------------- ******************# #**************** OPEN TESSEROID MODEL *******************# try: mag_tesseroids = np.loadtxt('model.magtess', delimiter=" ") except IOError as err: gmi_misc.error("CAN NOT OPEN TESSEROID MODEL: {0}".format(err)) exit(-1) #print mag_tesseroids n_tess = len(mag_tesseroids) print ('Number of tesseroids in the model: ' + str(n_tess)) #**************** --------------------- ******************# from tqdm import tqdm import pyshtools coeff_info = pyshtools.SHCoeffs.from_zeros(1) import os if os.path.exists('model'): if len(os.listdir('model') ) > 0: if __name__ == '__main__': gmi_misc.warning("MODEL FOLDER ALREADY EXIST! DO YOU WANT TO RECALCULATE?") if gmi_misc.ask() == True: pass else: gmi_misc.error("MODEL FOLDER WAS NOT OVERWRITEN, ABORTING!") else: os.mkdir('model') if gmi_config.MULTIPLICATOR != 1.0: gmi_misc.warning("NOTE: SUSCEPTIBILITY OF EACH TESSEROID IS MULTIPLIED BY " + str(gmi_config.MULTIPLICATOR)) n_cpu = os.cpu_count() gmi_misc.message('Number of processors: '+ str(n_cpu)) gmi_misc.message('Calculating effects of each tesseroid...') bar = tqdm(range(n_tess)) i = 0 while i < n_tess: curr_max_j = 0 for j in range(0, n_cpu, 1): if (i+j) < n_tess: with open('dummy_' + str(j) + '.magtess', 'w') as tessfile: string = str(mag_tesseroids[i+j, 0]) + ' ' + str(mag_tesseroids[i+j, 1]) + ' ' + str(mag_tesseroids[i+j, 2]) + ' ' + str(mag_tesseroids[i+j, 3]) + ' ' + str(mag_tesseroids[i+j, 4]) + ' ' + str(mag_tesseroids[i+j, 5]) + ' ' + str(mag_tesseroids[i+j, 6]) + ' ' + str(mag_tesseroids[i+j, 7]) + ' ' + str(mag_tesseroids[i+j, 8]) + ' ' + str(mag_tesseroids[i+j, 9]) + ' ' + str(mag_tesseroids[i+j, 10]) tessfile.write(string + '\n') curr_max_j = curr_max_j + 1 command_bz = "" + gmi_config.TESSBZ_FILENAME + " dummy_" + str(0) + ".magtess < grid.txt > " + "out_" + str(0) + "_Bz.txt" for j in range(1, n_cpu, 1): if (i+j) < n_tess: command_bz = command_bz + ' | ' command_bz = command_bz + "" + gmi_config.TESSBZ_FILENAME + " dummy_" + str(j) + ".magtess < grid.txt > " + "out_" + str(j) + "_Bz.txt" command = command_bz + '\n' os.system(command) for j in range(0, n_cpu, 1): if (i+j) < n_tess: raw_grid = gmi_misc.read_data_grid("out_" + str(j) + "_Bz.txt") shtools_inp_grid = pyshtools.SHGrid.from_array(raw_grid) #get SH coefficients shtools_coeff = shtools_inp_grid.expand(normalization='schmidt') coeff_info = shtools_coeff shtools_coeff.to_file('model/tess_n' + str(i) + '.coeff') os.remove("out_" + str(j) + "_Bz.txt") os.remove('dummy_' + str(j) + '.magtess') i = i + 1 bar.update(curr_max_j) gmi_misc.ok('...done') gmi_misc.message('Properties of SHCoeffs:') gmi_misc.message(str(coeff_info.info())) gmi_misc.message("Max degree: " + str(coeff_info.lmax)) #**************** WRITE MD5 PARAMS **************# dictionary['stage2'] = gmi_hash._gethashofdirs('model', verbose=1) dictionary['stage3'] = '' dictionary['stage4'] = '' np.save('checksums.npy', dictionary) #**************** ---------------- **************# #**************** RETURN BACK TO INITIAL PATH ***# os.chdir(old_cwd)
def main(dr): #**************** TESTING PARAMS (WOULD BE REMOVED)*******# TRUNCATE = True #**************** ---------------------------------*******# import gmi_misc #**************** PRINT HEADER ***************************# gmi_misc.print_header() print("Script no. 3: Creation of design matrices") #**************** ------------ ***************************# #**************** 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') if __name__ == '__main__': err = 0 if stages[0] == -1: err += 1 gmi_misc.warning( 'model.magtess was changed after the run of Script 1, restart Script no. 1 first! ABORTING...' ) elif stages[0] == 0: err += 1 gmi_misc.warning( 'model.magtess was changed after the run of Script 1, restart Script no. 1 first! ABORTING...' ) else: pass if stages[1] == -1: err += 1 gmi_misc.warning( 'Folder model was changed after the run of Script 2, restart Script no. 2 first! ABORTING...' ) elif stages[1] == 0: err += 1 gmi_misc.warning( 'Folder model was changed after the run of Script 2, restart Script no. 2 first! ABORTING...' ) else: pass if err > 0: gmi_misc.error('CHECKSUM FAILED, ABORTING!') #**************** --------------------- ******************# #**************** CREATE DESIGN MATRICES *****************# import os import glob os.chdir('model') coefflist = glob.glob("*.coeff") os.chdir('..') n_tess = len(coefflist) if n_tess == 0: gmi_misc.error( "NO CALCULATED SH MODELS OF EACH TESSEROID'S MAGNETIC FIELD") exit(-1) if gmi_config.MULTIPLICATOR != 1.0: gmi_misc.warning( "NOTE: SUSCEPTIBILITY OF EACH TESSEROID IS MULTIPLIED BY " + str(gmi_config.MULTIPLICATOR)) import pyshtools import numpy as np coeff_filename = 'model/tess_n' + str(0) + '.coeff' b = gmi_misc.read_coeffs_from_text_file(coeff_filename, gmi_config.N_MIN_CUTOFF) n_vals = len(b) gmi_misc.message('Assemblying design matrices...') from tqdm import tqdm A = np.zeros((n_tess, n_vals)) A_ufilt = np.zeros((n_tess, n_vals)) #if __name__ == '__main__': for i in tqdm(range(n_tess)): coeff_filename = 'model/tess_n' + str(i) + '.coeff' b = gmi_misc.read_coeffs_from_text_file(coeff_filename, gmi_config.N_MIN_CUTOFF) b_ufilt = gmi_misc.read_coeffs_from_text_file(coeff_filename, 0) A[i, :] = b[:] A_ufilt[i, :] = b_ufilt[:] ''' else: from PyQt5 import QtWidgets app = QtWidgets.QApplication.instance() if app is None: # if it does not exist then a QApplication is created app = QtWidgets.QApplication([]) from progress_bar import ProgressBar pb = ProgressBar() for i in range(n_tess): coeff_filename = 'model/tess_n' + str(i) + '.coeff' b = gmi_misc.read_coeffs_from_text_file(coeff_filename, gmi_config.N_MIN_CUTOFF) b_ufilt = gmi_misc.read_coeffs_from_text_file(coeff_filename, 0) A[i, :] = b[:] A_ufilt[i, :] = b_ufilt[:] pb.setValue(((i + 1) / n_tess) * 100) app.processEvents() pb.close() ''' gmi_misc.ok('...done') #**************** SAVE MATRICES *****************# np.save('design_matrix_shcoeff', A) np.save('design_matrix_ufilt_shcoeff', A_ufilt) #**************** ------------- *****************# #**************** WRITE MD5 PARAMS **************# import hashlib SHAhash = hashlib.md5() f1 = open('design_matrix_shcoeff.npy', 'rb') while 1: # Read file in as little chunks buf = f1.read(4096) if not buf: break SHAhash.update(hashlib.md5(buf).hexdigest().encode('utf-8')) f1.close() f2 = open('design_matrix_ufilt_shcoeff.npy', 'rb') while 1: # Read file in as little chunks buf = f2.read(4096) if not buf: break SHAhash.update(hashlib.md5(buf).hexdigest().encode('utf-8')) f2.close() dictionary['stage3'] = SHAhash.hexdigest() dictionary['stage4'] = '' np.save('checksums.npy', dictionary) #**************** ---------------- **************# #**************** RETURN BACK TO INITIAL PATH ***# os.chdir(old_cwd)