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)
示例#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)
示例#3
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)*******#
    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):

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