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')
Esempio n. 2
0
 def __init__(self, name, fmt, default, type_, docstring):
     self.name = name
     if fmt is None: self.fmt = DummyRecordWriter()
     else: self.fmt = FortranRecordWriter(fmt)
     self.type_ = type_
     self.__doc__ = docstring
     # Duplicate None if element is multivalued
     if default is None and isinstance(type_, tuple):
         default = (None,)*len(type_)
     self.default = default
Esempio n. 3
0
    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")
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
0
    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)
Esempio n. 7
0
    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)
Esempio n. 8
0
def main():

    # T [GeV] partitions for piecewise function
    T = np.linspace(0.005, 0.800, 1000)
    Tlo  = T[T < 0.155]
    #Tlo = T[T < 0.110]
    Tmid = T[(T >= 0.155) & (T < 0.18)]
    #Tmid = T[(T >= 0.110) & (T < 0.130)]
    Thi  = T[T >= 0.18]
    #Thi = T[T >= 0.130]
    
    # evaluate trace anomaly (e-3p)/T**4 on temperature intervals
    e3p_T4_lo  = urqmd_e3p_T4(Tlo)
    z = np.linspace(0, 1, Tmid.size)
    s = 6*z**5 - 15*z**4 + 10*z**3
    e3p_T4_mid = (1-s)*urqmd_e3p_T4(Tmid) + s*s95_e3p_T4(Tmid)
    e3p_T4_hi  = s95_e3p_T4(Thi)
    e3p_T4 = np.concatenate([e3p_T4_lo, e3p_T4_mid, e3p_T4_hi])

    # (e-3p)/T**4, p/T**4, e/T**4 and s/T**3
    p_T4_interp = Spline(T, e3p_T4/T).antiderivative()
    e_T4  = e3p_T4  + 3*p_T4_interp(T)
    s_T3 = e_T4  + p_T4_interp(T)

    # e [GeV/fm**3], p [GeV/fm**3], s [1/fm**3]
    e = e_T4*T**4/hbarc**3
    p = p_T4_interp(T)*T**4/hbarc**3
    s = s_T3*T**3/hbarc**3

    # express as functions of energy density
    p_interp = Spline(e, p)
    s_interp = Spline(e, s) 
    T_interp = Spline(e, T) 
 
    # e output mesh: GeV/fm**3
    #e_table = np.arange(1,311000,2)*1e-3
    e_table = np.exp(np.linspace(-6.9078,6.476972,1000)) 
    p_table = p_interp(e_table)
    s_table = s_interp(e_table)
    T_table = T_interp(e_table)

    # print to file in fortran format
    line = FortranRecordWriter('(4E15.6)')
    with open('s95.dat','w') as wf:
        for i in range(len(e_table)):
            wf.write(line.write([e_table[i], p_table[i], s_table[i], T_table[i]])+"\n")
Esempio n. 9
0
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')
Esempio n. 10
0
    def __init__(self, format, fields, fixed_fields=(), name=None,
                 post_read_hook=None):
        self._fields = fields
        self._fixed_fields = tuple(fixed_fields)
        self._reader = FortranRecordReader(format)
        self._writer = FortranRecordWriter(format)
        self.name = name
        self.post_read_hook = post_read_hook

        self.data = {}
        for f in fields:
            if f is not None:
                self.data[f] = None
Esempio n. 11
0
    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")
Esempio n. 12
0
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
Esempio n. 13
0
def generate_fort_2(polytropic_index, jmax, kmax, jout, kout, log_central_density, iteration, mass_star, xcut,
                    xwidth, xnorm, type):
    """
    Generate fort.2 model file for CHYMERA Code
    :param polytropic_index: Polytropic index of star
    :param jmax: radial grid size
    :param kmax: vertical grid size
    :param jout: radial size of star+disk. negative for just the disk
    :param kout: polar radius of star
    :param log_central_density: log of central density
    :param iteration: iteration parameter for model
    :param mass_star: point mass in center of star
    :param xcut: width of star in model
    :param xwidth: percentage of mass in the disk
    :param xnorm: TODO: Figure these x things out
    :param type: Currently either 'RWI' for Rossby Wave Instability, anything else for general disk model
    :return: A model file for input into the CHYMERA computational fluid dynamics code to run
    """
    jmax2 = jmax + 2
    jmax1 = jmax + 1
    if type == 'RWI':
        print('Using Rossby Wave equations for disk')
        denny = [[0 for x in range(jmax2)] for x in range(jmax2)]
        anggy = [[0 for x in range(jmax1)] for x in range(jmax1)]

        constants_array = []
        header_line = ""
        with open("fort.2", "r") as example_file:
            header_line = example_file.readline()
            print(header_line)
        constants_array = header_line.split(" ")
        # Remove the first empty parts that come from the '3X' Fortran Formatting
        del constants_array[0]
        del constants_array[0]
        del constants_array[0]
        del constants_array[0]
        print(constants_array)
        # Convert to floats
        for element in range(len(constants_array)):
            constants_array[element] = float(constants_array[element])
        print(constants_array)
        '''
        Format of first line in fort.2 file
        PINDEX,CON2,RRR2,OMCEN,DENCEN,TOVERW,ROF3N,ZOF3N,
&        A1NEWZ,JREQ,KZPOL
        '''

        # Get the density array
        for column in range(jmax2):
            for row in range(jmax2):
                denny[row][column] = surface_density_profile(amplitude, row + 1, r_nought, delta_r, h, column + 1, alpha,
                                                             constants_array[0], jout, constants_array[6],
                                                             constants_array[7])

        # Get angular momentum array
        for column in range(jmax1):
            for row in range(jmax1):
                anggy[row][column] = angular_momentum(row + 1, constants_array[6], column + 1, constants_array[7],
                                                      denny[row][column], g, mass_star, h=h)
        print("Length of Anggy: " + str(len(anggy)))

        with open("temp", 'w') as model_file:
            model_file.write(header_line)
            fortran_writer = FortranRecordWriter('8(1PE10.3,2X)')
            # Write header line
            # Fortran saves out arrays column first, so first row in file would be the first entry in each row in array
            # Each line is composed of 8 floating point with 22 spaces with 15 after the decimal place, then two spaces
            # Writing density array to fort.2
            temp_denny = []
            for column in range(jmax2):
                for row in range(jmax2):
                    temp_denny.append(denny[column][row])
                    ''' if len(temp_denny) == 8:
                        writer.writerow(temp_denny)
                        temp_denny = []
                    if row == jmax1 and column == jmax1:
                        writer.writerow(temp_denny)
                        temp_denny = []'''
            output_text = fortran_writer.write(temp_denny)
            output_text += "\n"
            model_file.write(output_text)
            # Writing the angular momentum array to fort.2
            # Repeat what was done for the denny array
            temp_anggy = []
            for column in range(jmax1):
                for row in range(jmax1):
                    temp_anggy.append(anggy[column][row])
            print("Length of Temp Anggy: " + str(len(temp_anggy)))
            output_text = fortran_writer.write(temp_anggy)
            output_text += "\n"
            model_file.write(output_text)
            # TODO Input coordinates of points into RWI equations and output them with 8 density points per line
            # TODO Then for specific angular momentum, same thing
    else:
        print('Using normal equations for disk')
 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')
Esempio n. 15
0
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)
Esempio n. 16
0
    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
Esempio n. 17
0
    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
Esempio n. 18
0
class DataCard:
    """ Class to implement a line of generalized ATP/Fortran style input records
        format is a format string suitable for the fortranformat module.
        fields is a list of field names for indexing the data dict. Field names
            will usually be strings, but could be integers or floats in the
            case of matching fixed_fields.
        fixed_fields is an iterable of field indices that have fixed values.
            The expected value for the field should be the field name in the
            fields  list.
        post_read_hook is an optional parameter indicating a function to be
            called after reading lines into the DataCard.
        Data in the line is internally represented using a dict.

        format and fields should not be changed after initialization.

        Reads only one line, but should be passed an interable of lines.
    """

    def __init__(self, format, fields, fixed_fields=(), name=None,
                 post_read_hook=None):
        self._fields = fields
        self._fixed_fields = tuple(fixed_fields)
        self._reader = FortranRecordReader(format)
        self._writer = FortranRecordWriter(format)
        self.name = name
        self.post_read_hook = post_read_hook

        self.data = {}
        for f in fields:
            if f is not None:
                self.data[f] = None

    def read(self, lines):
        """ Read in datalines with validation prior to populating data. """
        if not self.match(lines):
            # This should raise an exception and will help
            # identify where in the stack the exception occured.
            tmp = copy.deepcopy(self)
            tmp._read(lines)
        self._read(lines)


    def _read(self, lines):
        line = lines[0]
        data = self._reader.read(line)
        for f in self._fixed_fields:
            if data[f] != self._fields[f]:
                raise ValueError('Fixed field with wrong value: ' + data[f] +
                                 '/' + self._fields[f])

        for f, d in zip(self._fields, data):
            if f is not None:
                self.data[f] = d

        if self.post_read_hook is not None:
            self.post_read_hook(self)

        return self

    def write(self):
        data = [self.data[f] if f is not None else None for f in self._fields]
        return self._writer.write(data)

    def match(self, lines):
        """ Checks if text lines match record type. Does not modify card data.
        """
        tmp = copy.deepcopy(self)
        try:
            tmp._read(lines)
        except ValueError:
            return False

        return True

    def num_lines(self):
        return 1
Esempio n. 19
0
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)
Esempio n. 20
0
class ValueDescriptor:

    def __init__(self, name, fmt, default, type_, docstring):
        self.name = name
        if fmt is None: self.fmt = DummyRecordWriter()
        else: self.fmt = FortranRecordWriter(fmt)
        self.type_ = type_
        self.__doc__ = docstring
        # Duplicate None if element is multivalued
        if default is None and isinstance(type_, tuple):
            default = (None,)*len(type_)
        self.default = default

    def __get__(self, instance, owner):
        """Return with formatting applied."""
        if self.name in instance._values:
            value = instance._values[self.name] 
        else:
            value = self.default
        if self.ismultivalue:
            return "".join(map(self._str_value, value))
        return self._str_value(value)

    def __set__(self, instance, value):
        self._check_type(value)
        instance._values[self.name] = value

    def _check_type(self, value):
        """Typecheck for values. Every type accepts None."""
        def isok(v, t):
            return (v is None
                    or (isinstance(t, set) and v in t)
                    or (isinstance(t, range) and t.start <= v < t.stop)
                    or isinstance(v, t)
                    )
        # All types must match for a tuple
        if self.ismultivalue:
            matches = all(isok(v, t) for v, t in zip(value, self.type_))
        else:
            matches = isok(value, self.type_)
        if not matches:
            err = "Value {} is not of type {} or None.".format(
                    value, self.type_)
            raise TypeError(err)

    @property
    def ismultivalue(self):
        return isinstance(self.type_, tuple)

    def _str_value(self, value):
        if value is None:
            return " "*self._format_width()
        return self.fmt.write([value])

    def _format_width(self):
        """With of the Fortran format.

        Necessary for blank fields. Implementation is more of a guess than
        anything but good enough for this application.
        """
        total = 0
        for item in self.fmt._eds:
            repeat = getattr(item, "num_chars", None)
            if repeat is None: repeat = getattr(item, "repeat", 1)
            if repeat is None: repeat = 1
            total = total + repeat*getattr(item, "width", 1)
        return total
Esempio n. 21
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)
Esempio n. 22
0
class FortranFormat(object):
    """
    Processes Fortran format strings according to the Fortran specification for
    such formats. This object handles reading and writing data with any valid
    Fortran format. It does this by using the `fortranformat` project
    [https://bitbucket.org/brendanarnold/py-fortranformat].

    However, while `fortranformat` is very general and adheres well to the
    standard, it is very slow. As a result, simple, common format strings have
    been optimized and processes reads and writes between 3 and 5 times faster.
    The format strings (case-insensitive) of the following form (where # can be
    replaced by any number) are optimized:
        - #E#.#
        - #D#.#
        - #F#.#
        - #(F#.#)
        - #a#
        - #I#

    Parameters
    ----------
    format_string : str
        The Fortran Format string to process
    strip_strings : bool=True
        If True, strings are stripped before being processed by stripping
        (only) trailing whitespace
    """

    strre = re.compile(r'(\d+)?a(\d+)$', re.I)
    intre = re.compile(r'(\d+)?i(\d+)$', re.I)
    floatre = re.compile(r'(\d+)?[edf](\d+)\.(\d+)$', re.I)
    floatre2 = re.compile(r'(\d+)?\([edf](\d+)\.(\d+)\)$', re.I)

    #===================================================

    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 __copy__(self):
        return type(self)(self.format, self.strip_strings)

    #===================================================

    def __str__(self):
        return self.format

    #===================================================

    def write(self, items, dest):
        """
        Writes an iterable of data (or a single item) to the passed file-like
        object

        Parameters
        ----------
        items : iterable or single float/str/int
            These are the objects to write in this format. The types of each
            item should match the type specified in this Format for that
            argument
        dest : file or file-like
            This is the file to write the data to. It must have a `write` method
            or an AttributeError will be raised

        Notes
        -----
        This method may be replaced with _write_string (for #a#-style formats)
        or _write_ffwriter in the class initializer if no optimization is
        provided for this format, but the call signatures and behavior are the
        same for each of those functions.
        """
        if hasattr(items, '__iter__') and not isinstance(items, string_types):
            mod = self.nitems - 1
            for i, item in enumerate(items):
                dest.write(self.fmt % item)
                if i % self.nitems == mod:
                    dest.write('\n')
            if i % self.nitems != mod:
                dest.write('\n')
        else:
            dest.write(self.fmt % item)
            dest.write('\n')

    #===================================================

    def _write_string(self, items, dest):
        """ Writes a list/tuple of strings """
        if hasattr(items, '__iter__') and not isinstance(items, string_types):
            mod = self.nitems - 1
            for i, item in enumerate(items):
                dest.write((self.fmt % item).ljust(self.itemlen))
                if i % self.nitems == mod:
                    dest.write('\n')
            if i % self.nitems != mod:
                dest.write('\n')
        else:
            dest.write((self.fmt % item).ljust(self.itemlen))
            dest.write('\n')

    #===================================================

    def _read_nostrip(self, line):
        """
        Reads the line and returns converted data. Special-cased for flags that
        may contain 'blank' data. ugh.
        """
        line = line.rstrip('\n')
        nitems = int(ceil(len(line) / self.itemlen))
        ret = [0 for i in range(nitems)]
        start, end = 0, self.itemlen
        for i in range(nitems):
            ret[i] = self.process_method(self.type(line[start:end]))
            start = end
            end += self.itemlen
        return ret

    #===================================================

    def read(self, line):
        """ Reads the line and returns the converted data """
        line = line.rstrip()
        nitems = int(ceil(len(line) / self.itemlen))
        ret = [0 for i in range(nitems)]
        start, end = 0, self.itemlen
        for i in range(nitems):
            ret[i] = self.process_method(self.type(line[start:end]))
            start = end
            end += self.itemlen
        return ret

    #===================================================

    def _read_ffreader(self, line):
        """ Reads the line and returns the converted data """
        return self._reader.read(line.rstrip())

    #===================================================

    def _write_ffwriter(self, items, dest):
        dest.write('%s\n' % self._writer.write(items))
Esempio n. 23
0
File: hrb.py Progetto: dpo/pyorder
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
Esempio n. 24
0
class FortranFormat(object):
    """
    Processes Fortran format strings according to the Fortran specification for
    such formats. This object handles reading and writing data with any valid
    Fortran format. It does this by using the `fortranformat` project
    [https://bitbucket.org/brendanarnold/py-fortranformat].

    However, while `fortranformat` is very general and adheres well to the
    standard, it is very slow. As a result, simple, common format strings have
    been optimized and processes reads and writes between 3 and 5 times faster.
    The format strings (case-insensitive) of the following form (where # can be
    replaced by any number) are optimized:
        - #E#.#
        - #D#.#
        - #F#.#
        - #(F#.#)
        - #a#
        - #I#

    Parameters
    ----------
    format_string : str
        The Fortran Format string to process
    strip_strings : bool=True
        If True, strings are stripped before being processed by stripping
        (only) trailing whitespace
    """

    strre = re.compile(r'(\d+)?a(\d+)$', re.I)
    intre = re.compile(r'(\d+)?i(\d+)$', re.I)
    floatre = re.compile(r'(\d+)?[edf](\d+)\.(\d+)$', re.I)
    floatre2 = re.compile(r'(\d+)?\([edf](\d+)\.(\d+)\)$', re.I)

    #===================================================

    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 __copy__(self):
        return type(self)(self.format, self.strip_strings)

    #===================================================

    def __str__(self):
        return self.format

    #===================================================

    def write(self, items, dest):
        """
        Writes an iterable of data (or a single item) to the passed file-like
        object

        Parameters
        ----------
        items : iterable or single float/str/int
            These are the objects to write in this format. The types of each
            item should match the type specified in this Format for that
            argument
        dest : file or file-like
            This is the file to write the data to. It must have a `write` method
            or an AttributeError will be raised

        Notes
        -----
        This method may be replaced with _write_string (for #a#-style formats)
        or _write_ffwriter in the class initializer if no optimization is
        provided for this format, but the call signatures and behavior are the
        same for each of those functions.
        """
        if hasattr(items, '__iter__') and not isinstance(items, basestring):
            mod = self.nitems - 1
            for i, item in enumerate(items):
                dest.write(self.fmt % item)
                if i % self.nitems == mod:
                    dest.write('\n')
            if i % self.nitems != mod:
                dest.write('\n')
        else:
            dest.write(self.fmt % item)
            dest.write('\n')

    #===================================================

    def _write_string(self, items, dest):
        """ Writes a list/tuple of strings """
        if hasattr(items, '__iter__') and not isinstance(items, basestring):
            mod = self.nitems - 1
            for i, item in enumerate(items):
                dest.write((self.fmt % item).ljust(self.itemlen))
                if i % self.nitems == mod:
                    dest.write('\n')
            if i % self.nitems != mod:
                dest.write('\n')
        else:
            dest.write((self.fmt % item).ljust(self.itemlen))
            dest.write('\n')

    #===================================================

    def _read_nostrip(self, line):
        """
        Reads the line and returns converted data. Special-cased for flags that
        may contain 'blank' data. ugh.
        """
        line = line.rstrip('\n')
        nitems = int(ceil(len(line) / self.itemlen))
        ret = [0 for i in xrange(nitems)]
        start, end = 0, self.itemlen
        for i in xrange(nitems):
            ret[i] = self.process_method(self.type(line[start:end]))
            start = end
            end += self.itemlen
        return ret

    #===================================================

    def read(self, line):
        """ Reads the line and returns the converted data """
        line = line.rstrip()
        nitems = int(ceil(len(line) / self.itemlen))
        ret = [0 for i in xrange(nitems)]
        start, end = 0, self.itemlen
        for i in xrange(nitems):
            ret[i] = self.process_method(self.type(line[start:end]))
            start = end
            end += self.itemlen
        return ret

    #===================================================

    def _read_ffreader(self, line):
        """ Reads the line and returns the converted data """
        return self._reader.read(line.rstrip())

    #===================================================

    def _write_ffwriter(self, items, dest):
        dest.write('%s\n' % self._writer.write(items))
 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 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_exo(ExoCrossline)

    einstein_coeff = get_einstein_coeff(ExoCrossline)

    broadening_air, broadeing_self = get_broadening(ExoCrossline)

    energy = get_exo_energy(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(
        2, 5, 0, 0, 0, 0, 0)

    ref1, ref2, ref3, ref4, ref5, ref6 = set_ref(10, 4, 0, 0, 0, 0)

    g_upper, g_lower = get_g(ExoCrossline)

    space1 = ' '
    space2 = ' '

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

    # Set up line with chosen parameters (order and type are important)

    line1 = molecule_id, iso_id, frequency, intensity, einstein_coeff, broadening_air, broadeing_self, energy, n_air,\
            delta_air, global_upper,global_lower,space1,branch,local_lower,lower_parity,space2,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
    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",