Example #1
0
def main(dr, tc=True):
    import gmi_misc

    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())

    print("HASH TEST")

    import gmi_config
    gmi_config.read_config()

    #**************** HASH TESTS    **************************#
    import gmi_hash

    stages = [0, 0, 0]
    stages, dict = gmi_hash.read_dict('checksums.npy', runcheck=tc)
    #**************** ----------    **************************#

    os.chdir(old_cwd)
    return stages, dict
    def EnableResultPlotting(self):
        import gmi_config
        import gmi_gmt
        import os
        old_cwd = switch_path(self.GMI_PATH)

        config = gmi_config.read_config()
        try:
            current_opt = str(self.listWidget_ResultsList.currentItem().text())
        except:
            current_opt = '_none_'
            gmi_misc.debug(
                'bug to fix with restarting inversion win new paramters')

        self.listWidget_ResultsList.repaint()

        print(current_opt)

        if (os.path.exists(current_opt)):
            gmi_misc.info(str(current_opt) + ' is selected for plotting')
            #self.pushButton_Plotresult.setEnabled(True)
            self.graphicsView_ResultPlotView.setEnabled(True)
            #self.pushButton_Plotresult.repaint()
        else:
            #self.pushButton_Plotresult.setDisabled(True)
            self.graphicsView_ResultPlotView.setDisabled(True)
            #self.pushButton_Plotresult.repaint()

        switch_path_back(old_cwd)

        self.PlotResult()
    def EnablePlotting(self):
        import gmi_config
        import gmi_gmt
        import os
        old_cwd = switch_path(self.GMI_PATH)

        config = gmi_config.read_config()
        current_opt = str(self.comboBox_GridList.currentText())

        for sect in config.sections():
            if (config.has_option(sect, current_opt)):
                if (os.path.exists(config.get(sect, current_opt))):
                    gmi_misc.info(
                        str(sect) + '.' + str(current_opt) + ' (' +
                        config.get(sect, current_opt) +
                        ') is selected for plotting')
                    self.pushButton_Plot.setEnabled(True)
                    self.pushButton_Spectrum.setEnabled(True)
                    self.pushButton_ZoomIn.setEnabled(True)
                    self.pushButton_ZoomOut.setEnabled(True)
                    self.graphicsView_PlotView.setEnabled(True)
                    self.pushButton_Plot.repaint()
                    self.pushButton_Spectrum.repaint()
                    break

                else:
                    gmi_misc.warning(
                        str(sect) + '.' + str(current_opt) + ' (' +
                        config.get(sect, current_opt) +
                        ') IS EMPTY/DOES NOT EXIST!')
                    self.pushButton_Plot.setDisabled(True)
                    self.pushButton_Spectrum.setDisabled(True)
                    self.pushButton_ZoomIn.setDisabled(True)
                    self.pushButton_ZoomOut.setDisabled(True)
                    self.graphicsView_PlotView.setDisabled(True)
                    self.pushButton_Plot.repaint()
                    self.pushButton_Spectrum.repaint()
                    break

        switch_path_back(old_cwd)
    def ReadWorkingDirectory(self):
        import configparser
        import gmi_config

        old_cwd = switch_path(self.GMI_PATH)
        config = gmi_config.read_config()

        with open(self.GMI_PATH + '/input.txt', 'r') as config_file:
            buf = config_file.read()
            self.textEdit_ConfigEditor.setText(buf)

        self.comboBox_GridList.clear()
        self.comboBox_GridList.addItem('TOP_SURFACE')
        self.comboBox_GridList.addItem('BOT_SURFACE')
        self.comboBox_GridList.addItem('OBSERVED_DATA')
        self.comboBox_GridList.addItem('SUBTRACT_DATA')
        self.comboBox_GridList.addItem('INIT_SOLUTION')
        #self.comboBox_GridList.addItem(config[])

        switch_path_back(old_cwd)

        self.working_directory_opened = True

        self.InitResultsList()
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")
Example #6
0
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)*******#
    CREATE_VIM_MODEL = True
    #**************** ---------------------------------*******#

    import gmi_misc
    #**************** PRINT HEADER ***************************#
    gmi_misc.print_header()
    gmi_misc.message("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()

    #*********************************************************#

    gmi_misc.message('Path to surfaces: ' + gmi_config.PATH_SURFACES)

    import glob, re
    surfaces_filenames = glob.glob(gmi_config.PATH_SURFACES + '/*')
    try:
        surfaces_filenames.sort(
            key=lambda f: int(re.sub('\D', '', f))
        )  #good initial sort but doesnt sort numerically very well
        sorted(surfaces_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 surfaces: ' + str(surfaces_filenames))

    for li, this_surf, next_surf in zip(range(len(surfaces_filenames) - 1),
                                        surfaces_filenames[0:-1],
                                        surfaces_filenames[1:]):
        gmi_misc.message('Layer ' + str(li) + ': upper surface ' + this_surf +
                         ', lower surface: ' + next_surf)

        Z_bot = gmi_misc.read_surf_grid(next_surf)
        Z_top = gmi_misc.read_surf_grid(this_surf)

        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('layer' + str(li),
                                1.0 * gmi_config.MULTIPLICATOR, X, Y, Z_top,
                                Z_bot)

    #**************** WRITE MD5 PARAMS **************#

    #/fix it
    '''
    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 read_dict(dictname, runcheck=True):


    import gmi_misc
    import gmi_config

    gmi_config.read_config()

    import numpy as np

    stages = [0, 0, 0]
    try:
        read_dictionary = np.load(dictname,allow_pickle='TRUE').item()
    except:
        gmi_misc.warning("NO SCRIPTS WERE EXECUTED!")
        return stages, None

    import hashlib

    gmi_misc.info("Reading checksum file: "+ dictname)


    #STAGE 1 CHECKSUMS **************************************
    if runcheck:
        print(u'1. [ ] Tesseroid model', end='\r', flush=True)
    if (len(read_dictionary['stage1']) != 0):
        if runcheck:
            md5_stage = _gethashoffile('model.magtess')
            if md5_stage != read_dictionary['stage1']:
                print(u'1. [' + URING + u'] Tesseroid model: checksum in dictionary does not match the one of ' + CBLUE + 'model.magtess' + CEND)
                stages[0] = -1
            else:
                print(u'1. [' + UCHECK + u'] Tesseroid model: correct checksum')
                stages[0] = 1
        else:
            stages[0] = 1
    else:
        if runcheck:
            print(u'1. [' + UCROSS + u'] Tesseroid model: no checksum ')
        stages[0] = 0

    #STAGE 2 CHECKSUMS **************************************
    if runcheck:
        print(u'2. [ ] Effect of each tesseroid', end='\r', flush=True)
    if (len(read_dictionary['stage2']) != 0):
        if runcheck:
            md5_stage = _gethashofdirs('model', verbose=0)
            if md5_stage != read_dictionary['stage2']:
                print(u'2. [' + URING + u'] Effect of each tesseroid: checksum in dictionary does not match the one of the folder ' + CBLUE + 'model' + CEND)
                stages[1] = -1
            else:
                print(u'2. [' + UCHECK + u'] Effect of each tesseroid: correct checksum')
                stages[1] = 1
        else:
            stages[1] = 1

    else:
        if runcheck:
            print(u'2. [' + UCROSS + u'] Effect of each tesseroid: no checksum ')
        stages[1] = 0

    if runcheck:
        print(u'3. [ ] Design matrix', end='\r', flush=True)
    if (len(read_dictionary['stage3']) != 0):
        if runcheck:
            SHAhash = hashlib.md5()

            try:
                f1 = open('design_matrix_shcoeff.npy', 'rb')

                i = 0
                i_step = 6500
                while 1:
                    if i == 0:
                            print(u'3. [\] Design matrix', end='\r', flush=True)
                    elif i == i_step:
                            print(u'3. [|] Design matrix', end='\r', flush=True)
                    elif i == i_step*2:
                            print(u'3. [/] Design matrix', end='\r', flush=True)
                    elif i == i_step*3:
                            print(u'3. [-] Design matrix', end='\r', flush=True)
                    elif i == i_step*4:
                            print(u'3. [\] Design matrix', end='\r', flush=True)
                            i = -1
                    else:
                            pass

                    buf = f1.read(4096)
                    if not buf : break
                    SHAhash.update(hashlib.md5(buf).hexdigest().encode('utf-8'))

                    i += 1

                f1.close()

                i = 0
                f2 = open('design_matrix_ufilt_shcoeff.npy', 'rb')
                while 1:
                    if i == 0:
                            print(u'3. [\] Design matrix', end='\r', flush=True)
                    elif i == i_step:
                            print(u'3. [|] Design matrix', end='\r', flush=True)
                    elif i == i_step*2:
                            print(u'3. [/] Design matrix', end='\r', flush=True)
                    elif i == i_step*3:
                            print(u'3. [-] Design matrix', end='\r', flush=True)
                    elif i == i_step*4:
                            print(u'3. [\] Design matrix', end='\r', flush=True)
                            i = -1
                    else:
                            pass

                    buf = f2.read(4096)
                    if not buf : break
                    SHAhash.update(hashlib.md5(buf).hexdigest().encode('utf-8'))

                    i += 1


                f2.close()

                md5_stage = SHAhash.hexdigest()

            except:
                md5_stage = 'abc'


            if md5_stage != read_dictionary['stage3']:
                print(u'3. [' + URING + u'] Design matrix: checksum in dictionary does not match the one of the folder ' + CBLUE + 'model' + CEND)
                stages[2] = -1
            else:
                print(u'3. [' + UCHECK + u'] Design matrix: correct checksum ')
                stages[2] = 1

        else:
            stages[2] = 1
    else:
        if runcheck:
            print(u'3. [' + UCROSS + u'] Design matrix: no checksum ')
        stages[2] = 0

    return stages, read_dictionary
    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)
Example #12
0
def plot_global_grid(grid_array, surf, name, minv, maxv, cs, text_val, text_unit, type_p):
    import gmi_config
    gmi_config.read_config()

    #type_p:
    #0 with polar
    #1 without polar
    #2 zone

    if minv > maxv:
        gmi_misc.warning('minv IS BIGGER THAN maxv, swapping')
        return

    actual_maxv = grid_array.flatten().max()
    actual_minv = grid_array.flatten().min()

    lat_N = len(grid_array[:, 0])
    lon_N = len(grid_array[0, :])

    tempfname = 'temp.xyz'


    with open(tempfname, 'w') as tempf:
        for i in range(0, lon_N):
            for j in range(0, lat_N):
                if surf == False:
                    tempf.write(str(-180.0+i*360.0/float(lon_N)) + ' ' +  str(-90.0+j*180.0/float(lat_N)) + ' ' + str(grid_array[j, i]) + '\n')
                else:
                    tempf.write(str(-180.0+i*360.0/float(lon_N)) + ' ' +  str(-90.0+j*180.0/float(lat_N)) + ' ' + str(grid_array[j, i]) + '\n')

    lon_step = 360.0 / float(lon_N)
    lat_step = 180.0 / float(lat_N)

    gmt_command = 'gmt'
    import platform
    gmi_misc.info('Operating system: ' + platform.system())
    if platform.system() == "Linux":
        gmt_command = 'gmt'
    elif platform.system() == "Darwin":
        #gmt_command = '/Applications/GMT-5.3.3.app/Contents/Resources/bin/gmt'
        gmt_command = '/usr/local/Cellar/gmt/6.0.0_5/bin/gmt'
        #gmt_command = 'gmt'
    else:
        pass


    def gmt(com):
        cmd = gmt_command + ' ' + com + '\n'
        gmi_misc.info(cmd)
        os.system(cmd)
        return

    gmt('gmtset PS_MEDIA=a2')
    gmt('gmtset ANNOT_FONT_SIZE_PRIMARY=14p')

    if cs == 'polar':
        gmt('gmtset COLOR_BACKGROUND=blue')
        gmt('gmtset COLOR_FOREGROUND=red')

    elif cs == 'haxby':
        gmt('gmtset COLOR_BACKGROUND=black')
        gmt('gmtset COLOR_FOREGROUND=white')
    else:
        pass

    proj='W0/6i'

    cticks = 6

    if type_p == 0 or type_p == 1:
        gmt('nearneighbor {} -Rd -I{}d -S{}d -G{}.grd'.format(tempfname, np.minimum(lon_step, lat_step), 2.0*np.minimum(lon_step, lat_step), _trunc_fn(tempfname)) )
    else:
        gmt('nearneighbor {} -Rd{}/{}/{}/{} -I{}d -S{}d -G{}.grd'.format(tempfname, gmi_config.T_LON_MIN, gmi_config.T_LON_MAX, gmi_config.T_LAT_MIN, gmi_config.T_LAT_MAX, np.minimum(lon_step, lat_step), 2.0*np.minimum(lon_step, lat_step), _trunc_fn(tempfname)) )

    diff = abs(maxv-minv)
    i = _ident_pont_pos(diff)
    cstep = 0.5*float((int(diff * 10.0**i) // cticks)) * 10.0**(-i)
    tstep = cstep*2.0

    gmt('makecpt -C{} -T{}/{}/{} > {}.cpt'.format(cs, minv, maxv, cstep, _trunc_fn(tempfname)))


    gmt('grdimage {}.grd -Rd -J{} -C{}.cpt -P -K > {}.ps'.format(_trunc_fn(tempfname), proj ,_trunc_fn(tempfname), _trunc_fn(tempfname)) )


    gmt('pscoast -Rd -J{} -Ba90g30f5/a30g30f5WeSn -V -Dc -Wthin -O -K >> {}.ps'.format( proj , _trunc_fn(tempfname)))

    if type_p == 0:

        gmt('grdimage {}.grd -Rd-180/180/-90/-60 -Js0/-90/7i/30 -C{}.cpt -Y3.2i  -O -K  >> {}.ps'.format(_trunc_fn(tempfname), _trunc_fn(tempfname), _trunc_fn(tempfname)) )
        gmt('pscoast -Rd-180/180/-90/-60 -Js0/-90/7i/30 -Ba90g30f5/a30g30f5WeSn -V -Dc -Wthin -O -K >> {}.ps'.format(_trunc_fn(tempfname)))

        gmt('grdimage {}.grd -Rd-180/180/60/90 -Js0/90/2.3i/30 -C{}.cpt -X3.8i  -O -K  >> {}.ps'.format(_trunc_fn(tempfname), _trunc_fn(tempfname), _trunc_fn(tempfname)) )
        gmt('pscoast -Rd-180/180/60/90 -Js0/90/2.3i/30 -Ba90g30f5/a30g30f5WeSn -V -Dc -Wthin -O -K >> {}.ps'.format(_trunc_fn(tempfname)))

    fb_add = ''
    if ((actual_minv < minv) or (actual_maxv > maxv)):
        fb_add = '+e'
        if actual_minv < minv:
            fb_add = fb_add + 'b'
        if actual_maxv > maxv:
            fb_add = fb_add + 'f'

    if type_p == 0:
        gmt('psscale -Dx1i/-0.4i+w2.0i/0.5c{}  -C{}.cpt -I0 -B{}:"{}":/:"{}": -X-2.3i -Y0.5i -O >> {}.ps'.format(fb_add, _trunc_fn(tempfname), tstep, text_val, text_unit, (_trunc_fn(tempfname))))

    elif type_p == 1:
        gmt('psscale -Dx1i/-0.4i+w4i/0.5c+h{}  -C{}.cpt -I0 -B{}:"{}":/:"{}": -O >> {}.ps'.format(fb_add, _trunc_fn(tempfname), tstep, text_val, text_unit, (_trunc_fn(tempfname))))
    else:
        gmt('psscale -Dx1i/-0.4i+w4i/0.5c+h{}  -C{}.cpt -I0 -B{}:"{}":/:"{}": -O >> {}.ps'.format(fb_add, _trunc_fn(tempfname), tstep, text_val, text_unit, (_trunc_fn(tempfname))))

    gmt('ps2raster -A+r -Tg {}.ps'.format(_trunc_fn(tempfname)))

    show = False

    if(show == True):

        import matplotlib.image as mpimg
        img=mpimg.imread(_trunc_fn(tempfname) + '.png')
        imgplot = plt.imshow(img)
        plt.title(name)
        plt.show()
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)
    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 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)
import gmi_misc

gmi_misc.print_header()
print("Get average susceptibilities for ocean/cont")

import gmi_config
gmi_config.read_config()

import numpy as np

x0 = np.loadtxt('apriori_VIM/2degx2deg_apriori_hemant_crust1.0_wolfgang.x0.x0')
print(str(len(x0)))

land_mask = gmi_misc.read_suscept_global_grid_from_file(
    'cont_ocean_mask/cont_ocean_CRUST_mask.xyz')
#x0 = land_mask*0.0

land = []
ocean = []
for i in range(len(x0)):
    if land_mask[i] < 0.5:
        land.append(x0[i])
    else:
        ocean.append(x0[i])
print('1')

av_land = np.sum(land) / len(land)
av_ocean = np.sum(ocean) / len(ocean)

print(av_land)
print(av_ocean)
Example #18
0
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)