def write_to_file(self, output_fn, molecule, write_coords_in_bohr=True): # Numeric formats specified in resp input specification # http://upjv.q4md-forcefieldtools.org/RED/resp/#other3 header_format = FortranRecordWriter('2I5') atoms_format = FortranRecordWriter('17X,3E16.7') esp_points_format = FortranRecordWriter('1X,4E16.7') with open(output_fn, 'x') as f: f.write( header_format.write([len(molecule), len(self.points)]) + "\n") for atom in molecule: if write_coords_in_bohr: coords = [ atom_coord / angstrom_per_bohr for atom_coord in atom.coords ] else: coords = atom.coords f.write(atoms_format.write(coords) + "\n") for point_coords, esp_val in zip(self.points, self.values): if write_coords_in_bohr: point_coords = [ point_coord / angstrom_per_bohr for point_coord in point_coords ] f.write( esp_points_format.write([esp_val] + point_coords) + "\n")
def default_hitran(ExoCrossline): """ Function formats line into default HITRAN format :param line: input line from file that comes from ExoMol to HITRAN format (ExoCross) :return: line in HITRAN format """ # Following lines get parameters molecule_id, iso_id = get_id(ExoCrossline) frequency = get_freq_exo(ExoCrossline) intensity = get_intensity(ExoCrossline) einstein_coeff = get_einstein_coeff(ExoCrossline) broadening_air, broadeing_self = get_broadening(ExoCrossline) energy = get_energy_exo(ExoCrossline) n_air = get_n_air(ExoCrossline) delta_air = get_delta_air(ExoCrossline) global_upper, global_lower = get_global_qn(ExoCrossline) local_upper, local_lower = get_local_qn(ExoCrossline) branch = get_branch_exo(ExoCrossline) upper_parity, lower_parity = get_parity_exo(ExoCrossline) error_indices1, error_indices2, error_indices3, error_indices4, error_indices5, error_indices6 = set_error( ) ref1, ref2, ref3, ref4, ref5, ref6 = set_ref() g_upper, g_lower = get_g(ExoCrossline) test1 = ' ' test2 = ' ' line_mixing_flag = ' ' newline = FortranRecordWriter( '(I2,I1,F12.6,E10.3,E10.3,F5.4,F5.3,F10.4,F4.2,F8.6,A15,A15,10X,A5,5X,A1,I3,A1,A5,6I1,6I2,A1,F7.1,F7.1)' ) line1 = molecule_id, iso_id, frequency, intensity, einstein_coeff, broadening_air, broadeing_self, energy, n_air,\ delta_air, global_upper,global_lower,test1,branch,local_lower,lower_parity,test2,error_indices1, error_indices2,\ error_indices3, error_indices4, error_indices5, error_indices6,ref1, ref2, ref3, ref4,\ ref5, ref6, line_mixing_flag, g_upper,g_lower # # string1 = str(newline.write(line1)) new_string = string1 + '\n' return new_string
def config_test_1(self): '''Set config after-creation of format''' ff = FortranRecordWriter('(2I10)') result = ff.write([0, 0, 0, 0]) self.assertEqual(result, ' 0 0\n 0 0') config.RECORD_SEPARATOR = '|' result = ff.write([0, 0, 0, 0]) self.assertEqual(result, ' 0 0| 0 0')
def _write_modified_respin(respin_type, molecule, ivary_list, charge, iuniq, fn_out, read_input_charges): with open(fn_out, 'w') as out: out.write(_get_respin_content(respin_type, read_input_charges)) numbers = FortranRecordWriter('2I5') # `charge, iuniq` line print(numbers.write([charge, iuniq]), file=out) for atom, ivary in zip(molecule, ivary_list): print(numbers.write([atom.atomic_no, ivary]), file=out) print(file=out)
def _read_top(self, fn, f, line): top_line_format_in_esp = FortranRecordWriter('2I5') if line == " ESP FILE - ATOMIC UNITS": return 'Gaussian' try: _a, _b = line.split() except ValueError: self.raiseInputFormatError(fn) if line == top_line_format_in_esp.write([int(_a), int(_b)]): return 'repESP' else: self.raiseInputFormatError(fn)
def fort_write(fobj, formatstr, values, debug=False): vals = list(flatten(values)) vals = [v for v in vals if v is not None] if debug: print('--- writing ---') try: print('file: ' + fobj.name) except AttributeError: print('file: console') print('fmt: ' + formatstr) print('values: ') print(vals) frw = FortranRecordWriter(formatstr) line = frw.write(vals) if fobj is None: print(line) else: fobj.write(line + '\n')
def __init__(self, format_string, strip_strings=True): """ Sets the format string and determines how we will read and write strings using this format """ self.format = format_string self.strip_strings = strip_strings # for ease of copying # Define a function that processes all arguments prior to adding them to # the returned list. By default, do nothing, but this allows us to # optionally strip whitespace from strings. self.process_method = lambda x: x if FortranFormat.strre.match(format_string): rematch = FortranFormat.strre.match(format_string) # replace our write() method with write_string to force left-justify self.type, self.write = str, self._write_string nitems, itemlen = rematch.groups() if nitems is None: self.nitems = 1 else: self.nitems = int(nitems) self.itemlen = int(itemlen) self.fmt = '%s' # See if we want to strip the strings if strip_strings: self.process_method = lambda x: x.strip() elif FortranFormat.intre.match(format_string): self.type = int rematch = FortranFormat.intre.match(format_string) nitems, itemlen = rematch.groups() if nitems is None: self.nitems = 1 else: self.nitems = int(nitems) self.itemlen = int(itemlen) self.fmt = '%%%dd' % self.itemlen elif FortranFormat.floatre.match(format_string): self.type = float rematch = FortranFormat.floatre.match(format_string) nitems, itemlen, num_decimals = rematch.groups() if nitems is None: self.nitems = 1 else: self.nitems = int(nitems) self.itemlen = int(itemlen) self.num_decimals = int(num_decimals) if 'F' in format_string.upper(): self.fmt = '%%%s.%sF' % (self.itemlen, self.num_decimals) else: self.fmt = '%%%s.%sE' % (self.itemlen, self.num_decimals) elif FortranFormat.floatre2.match(format_string): self.type = float rematch = FortranFormat.floatre2.match(format_string) nitems, itemlen, num_decimals = rematch.groups() if nitems is None: self.nitems = 1 else: self.nitems = int(nitems) self.itemlen = int(itemlen) self.num_decimals = int(num_decimals) if 'F' in format_string.upper(): self.fmt = '%%%s.%sF' % (self.itemlen, self.num_decimals) else: self.fmt = '%%%s.%sE' % (self.itemlen, self.num_decimals) else: # We tried... now just use the fortranformat package self._reader = FortranRecordReader(format_string) self._writer = FortranRecordWriter(format_string) self.write = self._write_ffwriter self.read = self._read_ffreader
def fortranWriteLine(data, stream, fformat): "Write `data` to `stream` according to Fortran format `fformat`." fmt = FortranRecordWriter(fformat) stream.write(fmt.write(data)) stream.write('\n') return
def config_test_2(self): '''RECORD_SEPARATOR = |''' config.RECORD_SEPARATOR = '|' result = FortranRecordWriter('(2I10)').write([0, 0, 0, 0]) self.assertEqual(result, ' 0 0| 0 0')
def config_test_1(self): '''Default RECORD_SEPARATOR ('\n')''' ff = FortranRecordWriter('(2I10)') result = ff.write([0, 0, 0, 0]) self.assertEqual(result, ' 0 0\n 0 0')
def reversion_output_test_4(self): line = FortranRecordWriter("('[', 3F4.1, ']')") result = """[ 1.0 1.0 1.0] [ 1.0 1.0 1.0]""" self.assertEqual(line.write([1.0, 1.0, 1.0, 1.0, 1.0, 1.0]), result)
def dump_charges_to_qout(molecule, charge_type, filename): line = FortranRecordWriter("8F10.6") charges = [atom.charges[charge_type] for atom in molecule] with open(filename, 'w') as f: print(line.write(charges), file=f)
emis7 = 0 emis8 = df_emissions.ix[idx, 'ETHYLBENZEN '] emis9 = df_emissions.ix[idx, 'FORMALDEHYD '] emis10 = df_emissions.ix[idx, 'LEAD '] emis11 = df_emissions.ix[idx, 'MANGANESE'] emis12 = df_emissions.ix[idx, 'NAPHTHALENE'] emis13 = df_emissions.ix[idx, ' NH3 '] emis14 = df_emissions.ix[idx, 'NOX '] emis15 = df_emissions.ix[idx, 'PAH'] emis16 = df_emissions.ix[idx, ' PM10'] emis17 = df_emissions.ix[idx, ' PM2_5 '] emis18 = df_emissions.ix[idx, 'SO2'] emis19 = df_emissions.ix[idx, ' TOLUENE '] emis20 = df_emissions.ix[idx, 'VOC'] emis21 = df_emissions.ix[idx, ' XYLENE'] line = FortranRecordWriter('(I2, A3, A15, 41X, A40, A10, 8X, I4, F6.2, I4, 10X, F9.2, 74X, I4, F9.5, F9.5, 1X, 20(E13.7, 39X), E13.7, 39X)') # line = FortranRecordWriter('(I2, A3, A15, 81X, A10, 8X, I4, F6.2, I4, 10X, F9.2, 74X, I4, F9.5, F9.5, 1X, E13.7, 39X, E13.7, 39X,E13.7, 39X, E13.7, 39X, E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X,E13.7, 39X, E13.7, 39X,E13.7, 39X)\n') lineOut = line.write([int(state_id), county_id, plant_id, plant_nm, scc_code, stkHeight, stkDia, stkGasTmp, stkGasVel, sic_code,\ latitude, longitude, emis1, emis2, emis3, emis4, emis5, emis6, emis7, emis8, emis9, emis10, emis11, \ emis12, emis13, emis14, emis15, emis16, emis17, emis18, emis19, emis20, emis21]) lineOut = lineOut + ' '*39 + '\n' outFile.write(lineOut) outFile.close() #%% outputFile = "C:/Users/vik/Documents/Projects/NARA/NARA_files_4_Biorefinery/pt_NARA_biorefinery_2xEmis_tpy.txt" outFile = open(outputFile, 'w') lineOut = '%s\n'*6%("#IDA", "#TYPE Point Source Inventory", "#COUNTRY US", "#YEAR 2011", "#DESC NARA Biorefinery Point Source Inventory with twice original emissions",