Exemplo n.º 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 convert_sht_grid_to_xyz(inp):
    if isinstance(inp, str):
        try:
            grid = np.loadtxt(inp)
        except:
            gmi_misc.error('CAN NOT OPEN/WRONG FORMAT')
            exit()
    else:
        grid = inp

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

    X = np.zeros(lat_N * lon_N)
    Y = np.zeros(lat_N * lon_N)
    Z = np.zeros(lat_N * lon_N)
    k = 0
    for i in range(0, lon_N):
        for j in range(0, lat_N):
            if (0.0 + float(i) * 360.0 / float(lon_N) < 180.0):

                X[k] = 0.0 + float(i) * 360.0 / float(lon_N)
            else:
                X[k] = -360.0 + 0.0 + float(i) * 360.0 / float(lon_N)

            Y[k] = 90.0 - float(j) * 180.0 / float(lat_N)
            Z[k] = grid[j, i]
            k += 1

    return X, Y, Z
def switch_path(pth):
    old_cwd = os.getcwd()
    try:
        os.chdir(pth)
    except:
        gmi_misc.error('CAN NOT OPEN WORKING DIRECTORY ' + pth +
                       ', ABORTING...')
    return 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)
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")
Exemplo n.º 6
0
#**************** GET WORKING DIRECTORY ******************#
import os
old_cwd = os.getcwd()
gmi_misc.info('Current directory: ' + old_cwd)

WORKING_DIR = ''
import sys
if len(sys.argv) == 1:
    WORKING_DIR = ''

WORKING_DIR = sys.argv[1]

try:
    os.chdir(WORKING_DIR)
except:
    gmi_misc.error('CAN NOT OPEN WORKING DIRECTORY ' + WORKING_DIR +
                   ', 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
Exemplo n.º 7
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)
Exemplo n.º 10
0
    def start_main(self):

        if not os.path.exists(self.cfg.get('PATH', 'GMI_PATH')):
            gmi_misc.warning('Select new working folder path')
            return

        if not os.path.exists(self.cfg.get('PATH', 'GMI_MAGNETIZER')):
            gmi_misc.warning('Select new path to tessutil_magnetize_model')
            return

        if not os.path.exists(self.cfg.get('PATH', 'GMI_TESSBZ')):
            gmi_misc.warning('Select new path to tessbz')
            return

        HARDMODE = True

        if not HARDMODE:
            try:
                shutil.copyfile(
                    self.cfg.get('PATH', 'GMI_MAGNETIZER'),
                    self.cfg.get('PATH', 'GMI_PATH') +
                    '/tessutil_magnetize_model')
                os.chmod(
                    self.cfg.get('PATH', 'GMI_PATH') +
                    '/tessutil_magnetize_model', st.st_mode | stat.S_IEXEC)
            except:
                gmi_misc.warning('Could not copy executable ' +
                                 self.cfg.get('PATH', 'GMI_MAGNETIZER') +
                                 ' to the working folder ' +
                                 self.cfg.get('PATH', 'GMI_PATH'))

            try:
                shutil.copyfile(self.cfg.get('PATH', 'GMI_TESSBZ'),
                                self.cfg.get('PATH', 'GMI_PATH') + '/tessbz')
                os.chmod(
                    self.cfg.get('PATH', 'GMI_PATH') + '/tessbz',
                    st.st_mode | stat.S_IEXEC)
            except:
                gmi_misc.warning('Could not copy executable ' +
                                 self.cfg.get('PATH', 'GMI_TESSBZ') +
                                 ' to the working folder ' +
                                 self.cfg.get('PATH', 'GMI_PATH'))
        else:
            os.system(
                'cp ' +
                self.cfg.get('PATH', 'GMI_MAGNETIZER').replace(' ', '\ ') +
                ' ' + self.cfg.get('PATH', 'GMI_PATH').replace(' ', '\ ') +
                '/tessutil_magnetize_model')
            os.system('cp ' +
                      self.cfg.get('PATH', 'GMI_TESSBZ').replace(' ', '\ ') +
                      ' ' +
                      self.cfg.get('PATH', 'GMI_PATH').replace(' ', '\ ') +
                      '/tessbz')

            CHMOD = ''

            import sys

            if sys.platform == 'linux':
                CHMOD = 'chmod +x'

            elif sys.platform == 'darwin':
                CHMOD = 'chmod 755'
            else:
                gmi_misc.error('Unsupported operating system')

            os.system(CHMOD + ' ' +
                      (self.cfg.get('PATH', 'GMI_PATH')).replace(' ', '\ ') +
                      '/tessutil_magnetize_model')
            os.system(CHMOD + ' ' +
                      (self.cfg.get('PATH', 'GMI_PATH')).replace(' ', '\ ') +
                      '/tessbz')

        with open('.config.ini', 'w') as configfile:
            self.cfg.write(configfile)
        self.accept()
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)
Exemplo n.º 12
0
def read_config():
    import os
    import gmi_misc
    #gmi_misc.debug('Reading config file ' + os.path.abspath("mydir/myfile.txt"))

    import sys
    this = sys.modules[__name__]

    import configparser
    config = configparser.ConfigParser()

    try:
        config.readfp(open(r'input.txt'))
    except:
        gmi_misc.warning('no input.txt file in the directory, creating an empty one...')
        create_empty_config()
        config.readfp(open(r'input.txt'))
        
    #get project name
    try:
        this.PROJECT_NAME = str(config.get('Name', 'PROJECT_NAME'))
    except:
        gmi_misc.error('Necessary parameters in input.txt are missing! ABORTING') 


    try:
        this.MULTILAYER = True
        this.PATH_SURFACES = config.get('Global Tesseroid Model', 'PATH_SURFACES')
    except:
        gmi_misc.debug('Path to the list of surfaces in input.txt is missing! ABORTING')
        this.MULTILAYER = False
       

    if (this.MULTILAYER == False):
        try:
            this.TOP_SURFACE = config.get('Global Tesseroid Model', 'TOP_SURFACE')
            this.BOT_SURFACE = config.get('Global Tesseroid Model', 'BOT_SURFACE')
                
        except:
            gmi_misc.error('Necessary parameters in input.txt are missing! ABORTING')    
        

    try:
        #Global Tesseroid Model
        this.LON_MIN = float(config.get('Global Tesseroid Model', 'LON_MIN'))
        this.LON_MAX = float(config.get('Global Tesseroid Model', 'LON_MAX'))
        this.LAT_MIN = float(config.get('Global Tesseroid Model', 'LAT_MIN'))
        this.LAT_MAX = float(config.get('Global Tesseroid Model', 'LAT_MAX'))
        this.WIDTH = float(config.get('Global Tesseroid Model', 'WIDTH'))

        

        this.IGRF_DAY = int(config.get('Global Tesseroid Model', 'IGRF_DAY'))
        this.IGRF_MONTH = int(config.get('Global Tesseroid Model', 'IGRF_MONTH'))
        this.IGRF_YEAR = int(config.get('Global Tesseroid Model', 'IGRF_YEAR'))
        this.IGRF_COEFF_FILENAME = config.get('Global Tesseroid Model', 'IGRF_COEFF_FILENAME')

        #Global Grid
        this.GRID_LON_MIN = float(config.get('Global Grid', 'GRID_LON_MIN'))
        this.GRID_LON_MAX = float(config.get('Global Grid', 'GRID_LON_MAX'))
        this.GRID_LAT_MIN = float(config.get('Global Grid', 'GRID_LAT_MIN'))
        this.GRID_LAT_MAX = float(config.get('Global Grid', 'GRID_LAT_MAX'))

        this.GRID_ALT = float(config.get('Global Grid', 'GRID_ALT'))
        this.GRID_STEP = float(config.get('Global Grid', 'GRID_STEP'))

        #Spherical Harmonics
        this.N_MIN_CUTOFF = int(config.get('Spherical Harmonics', 'N_MIN_CUTOFF'))

        #Inversion
        this.OBSERVED_DATA = config.get('Inversion', 'OBSERVED_DATA')
        this.SUBTRACT_DATA = config.get('Inversion', 'SUBTRACT_DATA')
        this.INIT_SOLUTION = config.get('Inversion', 'INIT_SOLUTION')
        this.MAX_ITER = int(config.get('Inversion', 'MAX_ITER'))
        this.MULTIPLICATOR = float(config.get('Inversion', 'MULTIPLICATOR'))
    except:
        gmi_misc.error('Necessary parameters in input.txt are missing! ABORTING')
        

    try:
        #Tiles
        this.T_DO_TILES = True

        this.T_LON_MIN = float(config.get('Tiles', 'T_LON_MIN'))
        this.T_LON_MAX = float(config.get('Tiles', 'T_LON_MAX'))
        this.T_LAT_MIN = float(config.get('Tiles', 'T_LAT_MIN'))
        this.T_LAT_MAX = float(config.get('Tiles', 'T_LAT_MAX'))
        this.T_WIDTH = float(config.get('Tiles', 'T_WIDTH'))
        this.T_EDGE_EXT = float(config.get('Tiles', 'T_EDGE_EXT'))
        this.T_GRID_STEP = float(config.get('Tiles', 'T_GRID_STEP'))

    except:
        gmi_misc.debug('Tile inversion parameters are missing in input.txt')
        this.T_DO_TILES = False




    import platform
    import os
    oper_system = platform.system()

    this.TESSUTIL_MAGNETIZE_MODEL_FILENAME = './tessutil_magnetize_model'
    if os.path.isfile(this.TESSUTIL_MAGNETIZE_MODEL_FILENAME) == False:
        gmi_misc.error("CAN NOT FIND " + this.TESSUTIL_MAGNETIZE_MODEL_FILENAME)

    this.TESSBZ_FILENAME = './tessbz'
    if os.path.isfile(this.TESSBZ_FILENAME) == False:
        gmi_misc.error("CAN NOT FIND " + this.TESSBZ_FILENAME)

    return config
Exemplo n.º 13
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)
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)