Exemple #1
0
    def read_tape6(self, directory, filename):
        '''read_tape6 does currently not take any input parameters.
        It uses the directory and filename specified in the beginning of the function.
        They should point to a .tp6 modtran output file with MODTRAN format.
        The function returns the line numbers from the beginning and end of a block of data,
        as well as a list of parameters, which label each row of data in the .tp7 file'''

        modtran_functions = ModtranFunctions()

        results_directory = directory
        results_filename = filename
        tape6_directory = os.path.join(results_directory, results_filename)

        beginning_data = []
        end_data = []
        '''searches for keywords in tape 7 and determines the lines in which data begins and stops'''

        with open(tape6_directory, 'r') as tape6:
            searchlines = tape6.readlines()
        for i, line in enumerate(searchlines):
            if 'FREQ' in line:
                list_parameters = line.split(' ')
                modtran_functions.remove_from_list(list_parameters, '')
                if 'CM-1' in searchlines[i + 1]:
                    beginning_data.append(i + 2)
                else:
                    beginning_data.append(i + 1)
            elif 'WAVELENGTH' in line:
                list_parameters = line.split(' ')
                modtran_functions.remove_from_list(list_parameters, '')
                # needs to be verified, if 'NM' is the correct term used in tp7 files
                if 'NM' in searchlines[i + 1]:
                    beginning_data.append(i + 2)
                else:
                    beginning_data.append(i + 1)
            elif '-9999.' in line:
                end_data.append(i - 1)

        if len(beginning_data) != len(end_data):
            print(
                'reading tape6: number of starting points is not equal to number of endpoints'
            )

        #print (beginning_data)
        #print (end_data)
        #print (list_parameters)
        return ({
            'beginning_data': beginning_data,
            'end_data': end_data,
            'list_parameters': list_parameters
        })
    def get_list_parameters(self, file_directory, filename, file_extension):

        modtran_functions = ModtranFunctions()

        filename = filename + file_extension
        universal_directory = os.path.join(file_directory, filename)

        beginning_data = []
        end_data = []
        '''searches for keywords in tape 7 and determines the lines in which data begins and stops'''

        with open(universal_directory, 'r') as universal:
            searchlines = universal.readlines()
        for i, line in enumerate(searchlines):
            if 'FREQ' in line:
                list_parameters = line.split(' ')
                print(list_parameters)
                modtran_functions.remove_from_list(list_parameters, '')
                if 'CM-1' in searchlines[i + 1]:
                    beginning_data.append(i + 2)
                else:
                    beginning_data.append(i + 1)
            elif 'WAVLEN (NM)' in line:
                list_parameters = line.split(' ')
                modtran_functions.remove_from_list(list_parameters, '')
                if 'NM' in searchlines[i + 1]:
                    beginning_data.append(i + 2)
                else:
                    beginning_data.append(i + 1)
            elif '-9999.' in line:
                end_data.append(i - 1)

        if len(beginning_data) != len(end_data):
            print(
                'reading .7sc: number of starting points is not equal to number of endpoints'
            )

        list_parameters2 = []
        for string in list_parameters:
            tmp = string.strip('\n')
            list_parameters2.append(tmp)

        print(list(enumerate(list_parameters2)))
    def read_seven_sc(self, seven_sc_directory, filename):
        '''read_seven_sc does currently not take any input parameters.
        It uses the directory and filename specified in the beginning of the function.
        They should point to a .tp7 modtran output file with common format.
        The function returns the line numbers from the beginning and end of a block of data,
        as well as a list of parameters, which label each row of data in the .tp7 file'''

        modtran_functions = ModtranFunctions()

        #results_directory = r'C:\Users\ried_st\OneDrive\Austausch\Programming\TEST'
        #results_filename = 'ASCIIwrite.tp7'
        filename = filename + '.7sc'
        seven_sc_directory = os.path.join(seven_sc_directory, filename)

        beginning_data = []
        end_data = []
        '''searches for keywords in tape 7 and determines the lines in which data begins and stops'''

        with open(seven_sc_directory, 'r') as seven_sc:
            searchlines = seven_sc.readlines()
        for i, line in enumerate(searchlines):
            if 'FREQ' in line:
                list_parameters = line.split(' ')
                modtran_functions.remove_from_list(list_parameters, '')
                if 'CM-1' in searchlines[i + 1]:
                    beginning_data.append(i + 2)
                else:
                    beginning_data.append(i + 1)
            elif 'WAVLEN (NM)' in line:
                list_parameters = line.split(' ')
                modtran_functions.remove_from_list(list_parameters, '')
                if 'NM' in searchlines[i + 1]:
                    beginning_data.append(i + 2)
                else:
                    beginning_data.append(i + 1)
            elif '-9999.' in line:
                end_data.append(i - 1)

        if len(beginning_data) != len(end_data):
            print(
                'reading .7sc: number of starting points is not equal to number of endpoints'
            )

        list_parameters2 = []
        for string in list_parameters:
            tmp = string.strip('\n')
            list_parameters2.append(tmp)

        complete_data = []

        for i in range(0, len(beginning_data)):
            #if tape5 contains multiple input blocks, there are also multiple output blocks. len(beginning_data) is the number of output blocks
            block = []
            for j in range(0, 2):
                #each parameter row need to be extracted separately

                tmp = []
                for k in range(beginning_data[i], end_data[i] + 1):
                    tmp2 = searchlines[k].split(' ')
                    modtran_functions.remove_from_list(tmp2, '')
                    tmp4 = []
                    for string in tmp2:
                        tmp3 = string.strip('\n')
                        tmp4.append(tmp3)

                    tmp.append(tmp4[j])

                block.append(tmp)

            complete_data.append(block)

        #print (beginning_data)
        #print (end_data)
        #print (list_parameters)
        return ([beginning_data, end_data, list_parameters2, complete_data])
Exemple #4
0
    def read_tape7(self, tape7_directory, filename, write_to_separate_files):
        '''Input:
        - tape7_directory: the path directory where the .tp7 is located
        - filename: filename of the .tp7 file
        The function returns the line numbers from the beginning and end of a block of data,
        as well as a list of parameters, which label each row of data in the .tp7 file
        
        The output is a list with the form: [beginning_data, end_data, list_parameters2, complete_data]
        complete_data contains all the data from the file in a matrix. The first index accesses the block,
        the second one the line inside a block (according to list_parameters, which can be accessed by Tape7_reader.get_list_parameters()
        
        Example:
        tape7 = Tape7.read_tape7(r'C:\\Users\\ried_st\\Desktop\\Temporal eclipse workspace\\PG_Alte Modtran Rechnungen\\neu gerechnet\\20160217_1', '20160217_1', 'y')
        tape7[3][0][2]

        would return the third line of the first block of complete_data'''

        modtran_functions = ModtranFunctions()

        #results_directory = r'C:\Users\ried_st\OneDrive\Austausch\Programming\TEST'
        #results_filename = 'ASCIIwrite.tp7'
        filename_ext = filename + '.tp7'
        tape7_path = os.path.join(tape7_directory, filename_ext)

        beginning_data = []
        end_data = []
        '''searches for keywords in tape 7 and determines the lines in which data begins and stops'''

        with open(tape7_path, 'r') as tape7:
            searchlines = tape7.readlines()
        for i, line in enumerate(searchlines):
            if 'FREQ' in line:
                list_parameters = line.split(' ')
                modtran_functions.remove_from_list(list_parameters, '')
                if 'CM-1' in searchlines[i + 1]:
                    beginning_data.append(i + 2)
                else:
                    beginning_data.append(i + 1)
            elif 'WAVLEN (NM)' in line:
                list_parameters = line.split(' ')
                modtran_functions.remove_from_list(list_parameters, '')
                if 'NM' in searchlines[i + 1]:
                    beginning_data.append(i + 2)
                else:
                    beginning_data.append(i + 1)
            elif '-9999.' in line:
                end_data.append(i - 1)

        if len(beginning_data) != len(end_data):
            print(
                'reading tape 7: number of starting points is not equal to number of endpoints'
            )

        list_parameters2 = []
        for string in list_parameters:
            tmp = string.strip('\n')
            list_parameters2.append(tmp)

        complete_data = []

        for i in range(0, len(beginning_data)):
            '''if tape7 contains multiple input blocks, there are also multiple output blocks. len(beginning_data) is the number of output blocks'''
            block = []
            for j in range(0, len(list_parameters)):
                '''each parameter row needs to be extracted separately'''

                tmp = []
                for k in range(beginning_data[i], end_data[i] + 1):
                    '''iterates over all lines of one row in one block and writes the values into '''
                    tmp2 = searchlines[k].split(' ')
                    modtran_functions.remove_from_list(tmp2, '')
                    tmp4 = []
                    for string in tmp2:
                        tmp3 = string.strip('\n')
                        tmp4.append(tmp3)

                    tmp.append(tmp4[j])

                block.append(tmp)

            complete_data.append(block)

        #print (beginning_data)
        #print (end_data)
        #print (list_parameters)
        if write_to_separate_files == 'y':
            with open(tape7_path, 'r') as tape7:
                searchlines = tape7.readlines()
            for i in range(0, len(beginning_data)):
                data_write = searchlines[beginning_data[i - 1] -
                                         1:end_data[i - 1] + 1]

                new_file = open(
                    tape7_directory + '\\' + filename + '_' + str(i) + '.dat',
                    'w')
                new_file.writelines(data_write)
                new_file.close()

#             new_tape5 = open(input_directory + '/' + filename + '.dat', 'w')
#             new_tape5.writelines(output_file)
#             new_tape5.close()
        return ([beginning_data, end_data, list_parameters2, complete_data])