Example #1
0
except IOError as err:
    print("WARNING: CAN NOT OPEN INITIAL SOLUTION FILE: {0}".format(err))
    exit()

A_sh_mul_x0 = np.matmul(A_sh, x0)
A_sh_mul_x0_sh_tools = gmi_misc.convert_result_into_shtools_format(
    A_sh_mul_x0, 'A_sh_mul_x0.coeff')

import gmi_gmt

gmi_gmt.plot_global_grid([1, 2], [1, 2], [1, 2], 0, 3, "polar")
sdgfsdfg

try:
    #raw_grid = gmi_misc.read_tess_output_global_grid_from_file(gmi_config.OBSERVED_DATA)
    raw_grid = gmi_misc.read_data_grid(gmi_config.OBSERVED_DATA)
    raw_grid = raw_grid * gmi_config.MULTIPLICATOR  #CHECK THIS!!!!
except IOError as err:
    print("CAN NOT OPEN OBSERVED DATAFILE: {0}".format(err))
    exit(-1)

print(raw_grid)

try:
    #raw_subtract_grid = gmi_misc.read_tess_output_global_grid_from_file(gmi_config.SUBTRACT_DATA)
    raw_subtract_grid = gmi_misc.read_data_grid(gmi_config.SUBTRACT_DATA)
    raw_subtract_grid = raw_subtract_grid * gmi_config.MULTIPLICATOR
except IOError as err:
    print("WARNING: CAN NOT OPEN SUBTRACTEBLE DATAFILE: {0}".format(err))
    raw_subtract_grid = raw_grid * 0.0
Example #2
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):

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