コード例 #1
0
    def _do_load_h(self, h, filename, num_lines=0):
        r = 0  # counts rows of file
        ii = 0
        try:
            self.lines = []
            while True:
                s = h.readline().strip("\n")
                if len(s) == 0:
                    break
                """
                KuruczMolLineOld1 = namedtuple("KuruczMolLineOld1",
                              ["lambda_", "J2l", "Jl", "state2l", "v2l", "spin2l", "statel", "vl",
                               "spinl", ])

                """

                line = KuruczMolLineOld1(
                    float(s[0:9]),
                    float(s[9:15]),
                    float(s[15:21]),
                    s[22:23],
                    int(s[23:25]),
                    int(s[26:27]),
                    s[28:29],
                    int(s[29:31]),
                    int(s[32:33]),
                )

                self.lines.append(line)
                r += 1
                ii += 1
                if ii == _PROGRESS_INDICATOR_PERIOD:
                    a99.get_python_logger().info("Loading '{}': {}".format(
                        filename, a99.format_progress(r, num_lines)))
                    ii = 0

        except Exception as e:
            raise RuntimeError("Error around %d%s row of file '%s': \"%s\"" %
                               (r + 1, a99.ordinal_suffix(r + 1), filename,
                                a99.str_exc(e))) from e
コード例 #2
0
    def _do_load_h(self, h, filename, num_lines=0):
        r = 0  # counts rows of file
        ii = 0
        try:
            self.lines = []
            while True:
                s = h.readline().strip("\n")
                if len(s) == 0:
                    break

                line = KuruczMolLineOld(
                    float(s[0:10]),
                    float(s[10:15]),
                    float(s[15:20]),
                    int(s[20:22]),
                    int(s[22:24]),
                    s[24:25],
                    int(s[25:27]),
                    s[27:28],
                    int(s[28:29]),
                    s[32:33],
                    int(s[33:35]),
                    s[35:36],
                    int(s[36:37]),
                )

                self.lines.append(line)
                r += 1
                ii += 1
                if ii == _PROGRESS_INDICATOR_PERIOD:
                    a99.get_python_logger().info("Loading '{}': {}".format(
                        filename, a99.format_progress(r, num_lines)))
                    ii = 0

        except Exception as e:
            raise RuntimeError("Error around %d%s row of file '%s': \"%s\"" %
                               (r + 1, a99.ordinal_suffix(r + 1), filename,
                                a99.str_exc(e))) from e
コード例 #3
0
ファイル: filemolecules.py プロジェクト: trevisanj/pyfant
    def _do_load(self, filename):
        """Clears internal lists and loads from file."""

        with open(filename, "r") as h:
            r = 0  # counts rows of file
            try:
                number = int(h.readline())  # not used (see below)
                r += 1
                self.titm = a99.readline_strip(h)
                r += 1

                nv = a99.int_vector(
                    h)  # number of transitions=sets-of-lines for each molecule
                r += 1
                # Uses length of nv vector to know how many molecules to read (ignores "number")
                num_mol = len(nv)

                for im in range(num_mol):
                    nvi = nv[im]

                    m = Molecule()
                    self.molecules.append(m)

                    m.titulo = a99.readline_strip(h)
                    a99.get_python_logger().debug(
                        'Reading %d%s molecule \'%s\'' %
                        (im + 1, a99.ordinal_suffix(im + 1), m.titulo))

                    parts = [s.strip() for s in m.titulo.split("#")]
                    m.description = parts[0]
                    if len(parts) > 1:
                        # Found 'structure' in m.titulo
                        m.symbols = [
                            basic.adjust_atomic_symbol(s) for s in [
                                s.strip() for s in parts[1].split(" ")
                                if len(s.strip()) > 0
                            ]
                        ]
                    else:
                        # Will try to guess molecule by m.titulo's contents
                        temp = basic.description_to_symbols(parts[0])
                        m.symbols = temp or []
                    transitions = []
                    if len(parts) > 2:
                        numbers = [
                            int(float(x))
                            for x in re.findall('([0-9.]+)', parts[2])
                        ]
                        transitions = list(zip(numbers[0::2], numbers[1::2]))

                    r += 1
                    m.fe, m.do, m.mm, m.am, m.bm, m.ua, m.ub, m.te, m.cro = a99.float_vector(
                        h)
                    r += 1

                    h.readline()  # Skips line which is blank in file
                    # In readers.f90 the variables are ise, a0, a1, a2 a3, a4, als
                    # but the pfant does not use them.
                    r += 1

                    m.s = float(h.readline())
                    r += 1

                    # These vectors must have nvi elements
                    s_v, r_inc = a99.multirow_str_vector(h, nvi, r)
                    r += r_inc
                    qqv = list(map(float, s_v))
                    s_v, r_inc = a99.multirow_str_vector(h, nvi, r)
                    r += r_inc
                    ggv = list(map(float, s_v))
                    s_v, r_inc = a99.multirow_str_vector(h, nvi, r)
                    r += r_inc
                    bbv = list(map(float, s_v))
                    s_v, r_inc = a99.multirow_str_vector(h, nvi, r)
                    r += r_inc
                    ddv = list(map(float, s_v))
                    s_v, r_inc = a99.multirow_str_vector(h, nvi, r)
                    r += r_inc
                    fact = list(map(float, s_v))
                    for name in ["qqv", "ggv", "bbv", "ddv", "fact"]:
                        v = eval(name)
                        if len(v) != nvi:
                            raise RuntimeError(
                                'Attribute %s of molecule #%d must be a vector with %d elements (has %d)'
                                % (name, im + 1, nvi, len(v)))

                    # creates sets of lines and appends to molecule
                    for isol, (q, g, b, d,
                               f) in enumerate(zip(qqv, ggv, bbv, ddv, fact)):
                        o = SetOfLines()
                        o.qqv = q
                        o.ggv = g
                        o.bbv = b
                        o.ddv = d
                        o.fact = f
                        if isol < len(transitions):
                            o.vl, o.v2l = transitions[isol]
                        m.sol.append(o)

                    # Now reads lines
                    sol_iter = iter(
                        m.sol
                    )  # iterator to change the current set-of-lines with the "numlin" flag
                    o = next(sol_iter)  # current set-of-lines
                    # o.lmbdam, o.sj, o.jj, o.branch = [], [], [], []
                    while True:
                        # Someone added "*" signs as a 6th column of some lines
                        # which was causing my reading to crash.
                        # Therefore I read the line and discard beyond the 5th column before
                        # converting to float
                        temp = a99.str_vector(h)
                        lmbdam = float(temp[0])
                        sj = float(temp[1])
                        jj = float(temp[2])
                        # Alphanumeric now iz = int(temp[3])
                        iz = temp[3]
                        numlin = int(temp[4])

                        r += 1

                        o.lmbdam.append(lmbdam)
                        o.sj.append(sj)
                        o.jj.append(jj)
                        o.branch.append(iz)

                        if numlin > 0:
                            if numlin == 9:
                                break
                            o = next(sol_iter)

                    a99.get_python_logger().info("Loading '{}': {}".format(
                        filename, a99.format_progress(im + 1, num_mol)))

                    if im + 1 == num_mol:
                        break

                    # im += 1
            except Exception as e:
                raise type(e)(("Error around %d%s row of file '%s'" %
                               (r + 1, a99.ordinal_suffix(r + 1), filename)) +
                              ": " + str(e)).with_traceback(sys.exc_info()[2])
コード例 #4
0
    def _do_load(self, filename):
        def strip(s):
            return s.decode("ascii").strip()

        def I(s):
            return s.decode("ascii")  # Identity

        my_struct = struct.Struct(
            "14s 13s 12s 4s 6s 6s 3s 12s 4s 6s 6s 3s 13s 2x 3s 1x 1s 1x 1s 6s 2x"
        )
        func_map = [
            float, float, float, int, float, float, int, float, int, float,
            float, int, float, strip, I, I, strip
        ]

        filesize = os.path.getsize(filename)
        num_lines = float(filesize) / 120

        if num_lines != int(num_lines):
            raise RuntimeError(
                "Fractionary number of lines: {}, not a FilePlezTiO".format(
                    num_lines))
        num_lines = int(num_lines)

        with open(filename, "rb") as h:
            try:
                r = 0  # counts rows of file
                ii = 0  # progress feedback auxiliary variable
                while True:
                    s = h.readline()

                    # print(s)
                    # break
                    if len(s) == 0:
                        break  # # EOF: blank line or references section

                    # Filtering part

                    gf = float(s[14:27])
                    J2l = float(s[43:49])
                    vl = int(s[70:74])
                    v2l = int(s[49:53])

                    # gf = float(bits[1])
                    # J2l = float(bits[4])
                    # vl = float(bits[8])
                    # v2l = float(bits[3])

                    # gf = args[1]
                    # J2l = args[4]
                    # vl = args[8]
                    # v2l = args[3]

                    if not (gf >= self.min_gf and J2l <= self.max_J2l
                            and vl <= self.max_vl and v2l <= self.max_v2l):
                        r += 1
                        continue

                    bits = my_struct.unpack_from(s)

                    args = [func(x) for func, x in zip(func_map, bits)]
                    line = PlezTiOLine(*args)
                    self.lines.append(line)
                    r += 1

                    # args = [func(x) for func, x in zip(func_map, bits)]
                    # line = PlezTiOLine(*bits)
                    # self.lines.append(line)

                    #           1         2         3         4         5         6         7         8         9        10        11
                    # 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
                    #
                    #    lambda_air(A)   gf         Elow(cm-1)  vl  Jl    Nl  syml  Eup(cm-1) vu   Ju    Nu  symu  gamrad    mol trans branch
                    #      3164.8716 0.769183E-11   3459.6228   0   5.0   5.0  0  35047.3396  15   6.0   6.0  0 1.821557E+07|'TiO a f  R    '
                    # 12345678901234                                                                                        | 12345678901234
                    # 0             1234567890123                                                                           |
                    #               1            123456789012                                                               |
                    #                            2           1234                                                           |
                    #                                        3   123456                                                     |
                    #                                            4     123456                                               |
                    #                                                  5     123                                            |
                    #                                                        6  123456789012                                |
                    #                                                           7           1234                            |
                    #                                                                       8   123456                      |
                    #                                                                           9     123456                |
                    #                                                                                 10    123             |
                    #                                                                                       11 1234567890123|
                    #                                                                                          12           2x
                    #                                                                                                         123 1 1 123456
                    #                                                                                                         13  141516

                    ii += 1
                    if ii > 12345:
                        a99.get_python_logger().info("Loading '{}': {}".format(
                            filename, a99.format_progress(r + 1, num_lines)))
                        ii = 0

            except Exception as e:
                raise type(e)(("Error around %d%s row of file '%s'" %
                               (r + 1, a99.ordinal_suffix(r + 1), filename)) +
                              ": " + str(e)).with_traceback(sys.exc_info()[2])
コード例 #5
0
    def _do_load_h(self, h, filename, num_lines=0):
        r = 0  # counts rows of file
        ii = 0
        try:
            self.lines = []
            while True:
                s = h.readline().strip("\n")
                if len(s) == 0:
                    break

                # Kurucz: "negative energies are predicted or extrapolated"
                # (http: // kurucz.harvard.edu / linelists.html)
                E2l = float(s[22:32])
                if E2l < 0:
                    E2l = -E2l
                El = float(s[37:48])
                if El < 0:
                    El = -El

                try:
                    spin2l = int(s[57:58])
                except ValueError:
                    spin2l = 0

                try:
                    spinl = int(s[65:66])
                except ValueError:
                    spinl = 0

                line = KuruczMolLine(
                    float(s[0:10]) * 10,
                    float(s[10:17]),
                    float(s[17:22]),
                    E2l,
                    float(s[32:37]),
                    El,
                    int(s[48:50]),
                    int(s[50:52]),
                    s[53:54],
                    int(s[54:56]),
                    s[56:57],
                    spin2l,
                    s[61:62],
                    int(s[62:64]),
                    s[64:65],
                    spinl,
                    int(s[68:70]),
                )

                self.lines.append(line)
                r += 1
                ii += 1
                if ii == _PROGRESS_INDICATOR_PERIOD:
                    a99.get_python_logger().info("Loading '{}': {}".format(
                        filename, a99.format_progress(r, num_lines)))
                    ii = 0

        except Exception as e:
            # f = type(e)(("Error around %d%s row of file '%s'" %
            #              (r + 1, a99.ordinal_suffix(r + 1), filename)) + ": " + str(
            #     e)).with_traceback(sys.exc_info()[2])
            raise RuntimeError("Error around %d%s row of file '%s': \"%s\"" %
                               (r + 1, a99.ordinal_suffix(r + 1), filename,
                                a99.str_exc(e))) from e
コード例 #6
0
ファイル: tune-zinf.py プロジェクト: trevisanj/pyfant
            combo.conf.opt.pas = PAS
            combo.conf.flag_output_to_dir = True
            combo.conf.opt.zinf = args.max
            combo.conf.opt.no_molecules = True
            combo.conf.opt.opa = False
            combo.conf.opt.no_h = True
            # Note that half of the line (needs to)/(will be) calculated
            combo.conf.opt.llzero = line.lambda_ - args.max
            combo.conf.opt.llfin = line.lambda_
            combos.append(combo)
            ll.append(a)

            i += 1
            ii += 1
            if ii == 1000:
                print(a99.format_progress(i, n))
                ii = 0

    # # Runs pfant
    logger.info("Running pfant's...")

    # FLAG_CLEAN = True
    # rm = RunnableManager(flag_auto_clean=FLAG_CLEAN)

    rm = pyfant.RunnableManager()
    rm.add_runnables(combos)
    app = a99.get_QApplication([])
    form = pyfant.XRunnableManager(None, rm)
    form.show()
    # it is good to start the manager as late as possible, otherwise
    # the program will hang if, for example, the form fails to be created.