def __update_data(self): emsg, flag_error = "", False ss = "" try: for option in self.omap: ss = option.name if not option.checkbox.isChecked(): value = None else: value = option.get_value() self.f.__setattr__(option.name, value) ss = "" if self.f.llzero is not None and self.f.llfin is not None: if self.f.llzero >= self.f.llfin: raise RuntimeError("llzero must be lower than llfin") except Exception as E: flag_error = True if ss: emsg = "Option <em>--%s</em>: %s" % (ss, str(E)) else: emsg = str(E) a99.get_python_logger().exception("Updating Options object") self._flag_valid = not flag_error self.__set_error_text(emsg)
def on_retry_failed(self): try: self.rm.retry_failed() except Exception as e: MSG = "Could not retry failed" a99.get_python_logger().exception(MSG) a99.show_error("%s: %s" % (MSG, str(e)))
def main(fns_input, fn_output): ffmol = [] for fn in fns_input: fmol = pyfant.FileMolecules() try: fmol.load(fn) print("File '{}': {} molecule{}".format( fn, len(fmol), "" if len(fmol) == 1 else "s")) ffmol.append(fmol) except: a99.get_python_logger().exception("Skipping file '{}'".format(fn)) n = len(ffmol) fout = pyfant.FileMolecules() fout.titm = "Merge of {} file{}: {}".format( n, "s" if n != 1 else "", ", ".join([os.path.split(fmol.filename)[1] for fmol in ffmol])) for fmol in ffmol: fout.molecules.extend(fmol.molecules) print("Number of molecules in output: {}".format(len(fout))) if fn_output is None: fn_output = a99.new_filename(_PREFIX, ".dat") fout.save_as(fn_output) print("Saved file '{}'".format(fn_output))
def on_tableWidget_cellDoubleClicked(self, row=0, col=0): try: self.__explore_directory() except Exception as e: MSG = "Could explore directory" a99.get_python_logger().exception(MSG) a99.show_error("%s: %s" % (MSG, str(e)))
def _convert_clicked(self): self.add_log("===BEGIN===") try: errors = self._validate() if len(errors) == 0: conv, errors = self._get_conv(errors) if len(errors) == 0: lines = self._get_lines() if lines is None: errors.append("Molecular lines not specified") if len(errors) == 0: fobj, log = conv.make_file_molecules(lines) self._report_conversion(fobj, log) if log.flag_ok: fobj.save_as(self.w_out.value) self.add_log("File '{}' generated successfully".format( self.w_out.value)) else: self.add_log_error( "Cannot convert:\n - " + ("\n - ".join(errors)), True) except Exception as e: a99.get_python_logger().exception("Conversion failed") self.add_log_error("Conversion failed: {}".format(a99.str_exc(e)), True) self.add_log("===END===")
def copy_star(src_dir=None, starname=None): """ Copies files such as main.dat and abonds.dat from PFANT/data/some_directory into local directory Args: src_dir: absolute path to directory containing PFANT star data files FileMain and FileAbonds (and optionally FileDissoc files, which will be also copied if present) starname: if passed, will ignore src_dir and make it from starname instead, considering starname as a subdirectory of PFANT/data/ """ star_classes = [pyfant.FileMain, pyfant.FileDissoc, pyfant.FileAbonds] if starname is not None: src_dir = os.path.join(pyfant.get_pfant_data_path(), starname) if src_dir is None and starname is None: raise ValueError("Supply either src_dir or starname") if not os.path.isdir(src_dir): raise ValueError("'{}' is not a valid directory".format(src_dir)) a99.get_python_logger().debug("Will look inside directory %s" % src_dir) # makes list of files to analyse types = ('*.dat', '*.mod') ff = [] for type_ in types: ff.extend(glob.glob(os.path.join(src_dir, type_))) copy_or_skip_files(ff)
def copy_or_skip_files(ff, dest_dir="."): """Copies a series of files, skipping those which already exist. Args: ff: list of full paths to files to be copied dest_dir: destination directory """ for f in ff: name = os.path.split(f)[1] flag_skip = False a99.get_python_logger().info("Considering file '%s' ..." % name) if os.path.isfile(name): _print_skipped("file exists in local directory") flag_skip = True else: obj = f311.load_with_classes( f, [pyfant.FileMain, pyfant.FileAbonds, pyfant.FileDissoc]) if obj is not None: pass else: _print_skipped("neither main, abonds, nor dissoc file") flag_skip = True if not flag_skip: try: shutil.copy(f, dest_dir) a99.get_python_logger().info(" ... file copied") except Exception as e: a99.print_error("Error copying file: %s" % str(e))
def __run(self): """Called both from run() and run_from_combo().""" fn_output = self._get_fn_output() # Deletes output file if it already exists so that we don't have TRAPRB failing because of this if os.path.isfile(fn_output): os.unlink(fn_output) # Logging if self.flag_log_file: log_path = "traprb.log" if self.flag_log_console: stdout_ = a99.LogTwo(log_path) else: stdout_ = open(log_path, "w") else: if self.flag_log_console: stdout_ = subprocess.STD_OUTPUT_HANDLE else: stdout_ = None try: self._flag_running = True self.__popen = subprocess.run([self.exe_path], stdout=PIPE, input=bytes("{}\n{}\n".format(self.fn_input, fn_output), "ascii")) if stdout_ is not None: stdout_.write(self.__popen.stdout.decode("ascii")) if self.__popen.returncode != 0: raise pyfant.FailedError("%s failed (returncode=%s)" % (self.__class__.__name__.lower(), self.__popen.returncode)) except Exception as e: flag_re_raise = True if self.__popen: if isinstance(e, pyfant.FailedError) and self._flag_killed: # dismisses error if explicitly killed flag_re_raise = False elif isinstance(e, IOError) and self.__popen.returncode == 0: # Sometimes a IOError is raised even if Fortran executes # successfully, so the error is dismissed a99.get_python_logger().warning("Harmless error in TRAPRB") flag_re_raise = False if flag_re_raise: self._error_message = e.__class__.__name__+": "+str(e) self._flag_error = True raise finally: self._flag_finished = True self._flag_running = False if self.__popen is not None: self.__returncode = self.__popen.returncode if stdout_ is not None: stdout_.close()
def _calculate(self): flag_fcf = self.checkbox_fcf.isChecked() if flag_fcf: if self.fcfs is None or len(self.fcfs) == 0: self.add_log_error("FCFs not available", True) try: l_text = [] self.status("Calculating...") molconsts = copy.deepcopy(self.molconsts) molconsts.None_to_zero() for vl in range(10): for v2l in range(10): l_text.extend( a99.format_box("vl, v2l = ({}, {})".format(vl, v2l))) factor = 1. if flag_fcf: try: factor = self.fcfs[(vl, v2l)] except KeyError: l_text.append("Franck-Condon Factor not available") continue rows, header = [], None for J in range(40): mtools = pyfant.kovacs_toolbox(molconsts, flag_normalize=True) mtools.populate(vl, v2l, J) if header is None: branches = [key[3] for key in mtools.dict_sj] header = ["J"] + branches + ["Sum"] vv = mtools.dict_sj.values() total = sum([ x * factor for x in vv if x != pyfant.NO_LINE_STRENGTH ]) rows.append([J + .5] + [ "{:.5e}".format(x * factor) if x != pyfant.NO_LINE_STRENGTH else "-" for x in vv ] + ["{:.9g}".format(total)]) l_text.append(tabulate.tabulate(rows, header)) l_text.append("") text = "\n".join(l_text) self.textEdit.setPlainText(text) self.status("") except Exception as E: self.add_log_error(a99.str_exc(E), True) a99.get_python_logger().exception("Error calculating HLF")
def run(self): """Runs executable. Blocking routine. Only returns when executable finishes running. """ assert not self._flag_running, "Already running" assert not self._flag_finished, "Already finished" a99.get_python_logger().debug("Running %s '%s'" % (self.__class__.__name__.lower(), self.name)) self.__run()
def on_collect_errors(self): # TODO **duplicate code** export a_XExplorer's one and use because the latter is more complete try: k = a99.ErrorCollector() k.collect_errors(".") w = f311.XHTML(self, k.get_html(), "Errors in '.' and subdirectories") w.show() except Exception as e: MSG = "Could not collect errors" a99.get_python_logger().exception(MSG) a99.show_error("%s: %s" % (MSG, str(e)))
def MolLinesEditor_cell_changed(self, row, column, value): """Called by the molecular lines editor to notify that a value has changed.""" try: attr_name = SOL_ATTR_NAMES[column] v = self.sol.__getattribute__(attr_name) if v[row] != value: v[row] = value self.flag_changed = True self.plot_lines() self.update_window_title() except Exception as e: a99.get_python_logger().exception("XFileMolecules.MolLinesEditor_cell_changed() raised")
def _do_save_as(self, filename): with open(filename, "w") as h: a99.write_lf(h, str(len(self.molecules))) a99.write_lf(h, self.titm) a99.write_lf(h, " ".join([str(x.nv) for x in self.molecules])) for i_m, m in enumerate(self.molecules): a99.get_python_logger().info( "Saving '{}': molecule {}/{}".format( filename, i_m + 1, len(self.molecules))) # # Assembles "titulo" # ## Transitions ltrans = [] for sol in m: if sol.vl is None or sol.v2l is None: break ltrans.append([sol.vl, sol.v2l]) new_titulo = "{} # {} # {}".\ format(m.description, " ".join([s.strip() for s in m.symbols]), "|".join(["{:.0f},{:.0f}".format(*t) for t in ltrans])) # - mled change, incorporate shit a99.write_lf(h, new_titulo) a99.write_lf( h, (" ".join(["%.10g"] * 9)) % (m.fe, m.do, m.mm, m.am, m.bm, m.ua, m.ub, m.te, m.cro)) a99.write_lf(h, "") a99.write_lf(h, str(m.s)) a99.write_lf(h, " ".join(["{:g}".format(x.qqv) for x in m.sol])) a99.write_lf(h, " ".join(["{:.2f}".format(x.ggv) for x in m.sol])) a99.write_lf(h, " ".join(["{:.5f}".format(x.bbv) for x in m.sol])) a99.write_lf(h, " ".join(["{:.2f}".format(x.ddv) for x in m.sol])) a99.write_lf(h, " ".join(["{:g}".format(x.fact) for x in m.sol])) num_sol = len(m.sol) for i, s in enumerate(m.sol): num_lines = len( s) # number of lines for current set-of-lines for j in range(num_lines): numlin = 0 if j < num_lines - 1 else 9 if i == num_sol - 1 else 1 a99.write_lf( h, "%.10g %.10g %.10g %s %d" % (s.lmbdam[j], s.sj[j], s.jj[j], s.branch[j], numlin))
def _draw(self): fig = self.figure fig.clear() plt.figure(fig.number) try: a99.format_BLB() # Spectrum sp = self._spectrum if sp is not None: setup = f311.PlotSpectrumSetup() plt.plot(sp.x, sp.y, label=str(sp.title), color=_COLOR_SPECTRUM, linestyle=_LINESTYLE_SPECTRUM, linewidth=_LINEWIDTH) plt.xlabel(setup.fmt_xlabel.format(sp)) xmin, xmax, ymin, ymax, xspan, yspan = f311.calc_max_min([sp]) ymin, ymax = min(0., ymin), max(1., ymax) _T = 0.02 # fraction of extra space on left, right, top, bottom of graphics plt.xlim([xmin - xspan * _T, xmax + xspan * _T]) plt.ylim([ymin - yspan * _T, ymax + yspan * _T]) # Continuum if self._points is not None: xx, yy = zip(*self._points) plt.plot(xx, yy, c=_COLOR_LINE, linestyle=_LINESTYLE_CONTINUUM, linewidth=_LINEWIDTH) plt.plot(xx, yy, c=_COLOR_MARKER, linestyle="None", marker=_MARKER) try: fig.tight_layout() except: a99.get_python_logger().exception("WContinuum._draw()") except: a99.get_python_logger().exception("WContinuum._draw()") finally: self.canvas.draw()
def insert_molecule_from_nist(moldb, formula, flag_replace=False, flag_do_what_i_can=True): """ Retrieves NIST data and inserts new molecule into DB, or updates states if molecule exists :param moldb: FileMolDB object :param formula: str, e.g., "OH" :param flag_replace: applies to states (not molecule entry); passed on to insert_states_from_nist() :param flag_do_what_i_can: if set, will insert molecule even if cannot download molecular constants from NIST :return: None """ conn = moldb.get_conn() cursor = conn.cursor() assert isinstance(conn, sqlite3.Connection) assert isinstance(cursor, sqlite3.Cursor) id_molecule = None data = None name = "?name?" n = conn.execute("select count(*) from molecule where formula = ?", (formula, )).fetchone()["count(*)"] if n > 0: id_molecule = conn.execute("select id from molecule where formula = ?", (formula, )) else: try: data, header, name = pyfant.get_nist_webbook_constants( formula, flag_unicode=True, flag_parse_state=True) except: if flag_do_what_i_can: a99.get_python_logger().exception( "Could not get NIST constants for formula '{}'".format( formula)) else: raise if id_molecule is None: cursor.execute("insert into molecule (formula, name) values (?, ?)", (formula, name)) id_molecule = cursor.lastrowid conn.commit() if data is not None: insert_states_from_nist(moldb, id_molecule, data, flag_replace) conn.commit()
def on_run_multi(self): import pyfant as pf errors = self._check_single_setup() if not self.multi_editor.f: errors.append("abundances X FWHM's configuration not set") else: # forces validation because validity is linked to FileAbonds managed in other tab self.multi_editor.f.validate() if not self.multi_editor.flag_valid: errors.append("error(s) in abundances X FWHM's configuration") if self.checkbox_multi_custom_id.isChecked(): s = self.__get_multi_custom_session_id() if len(s) == 0: errors.append("Please inform custom session id.") elif len( errors ) == 0: # will only offer to remove directory if everything is ok so far dirname = pyfant.get_custom_multisession_dirname(s) if os.path.isdir(dirname): r = QMessageBox.question( self, "Directory exists", "Directory '%s' already exists.\n\nWould you like to remove it?" % dirname, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if r == QMessageBox.Yes: try: shutil.rmtree(dirname) except Exception as e: errors.append(str(e)) else: return if len(errors) == 0: try: self.setEnabled(False) self.__submit_multi() self._manager_form.show() except Exception as e: errors.append(str(e)) a99.get_python_logger().exception("Cannot submit multi-job") finally: self.setEnabled(True) if len(errors) > 0: a99.show_error("Cannot submit multi-job:\n - " + ("\n - ".join(errors)))
def configure(self, sequence): """Series of configuration actions to take before Runnable can run. Runnable can be re-run (useful if fails), so configure() will know if it has been called before and skip most operations if so. """ if not self.__flag_configured_before: self.__logger = a99.get_python_logger() self.__sid.make_id() if self.__flag_output_to_dir: self.__rename_outputs(sequence) if FOR_PFANT in sequence: self.__opt.fn_progress = self.__sid.join_with_session_dir( "progress.txt") # this is for pfant # Always gotta open logs if self.__flag_log_file: log_path = self.__sid.join_with_session_dir("fortran.log") if self.__flag_log_console: stdout_ = a99.LogTwo(log_path) else: stdout_ = open(log_path, "w") else: if self.__flag_log_console: stdout_ = subprocess.STD_OUTPUT_HANDLE else: stdout_ = None self.__popen_text_dest = stdout_ self.__create_data_files() self.__flag_configured_before = True
def __init__(self, file_main, file_abonds, options, file_abxfwhm, custom_id=None): import pyfant as pf pyfant.Runnable.__init__(self) assert isinstance(file_main, pyfant.FileMain) assert isinstance(file_abonds, pyfant.FileAbonds) assert isinstance(options, pyfant.FileOptions) assert isinstance(file_abxfwhm, pyfant.FileAbXFwhm) self.__file_main = file_main self.__file_abonds = file_abonds self.__options = options self.__file_abxfwhm = file_abxfwhm self.__custom_id = custom_id # # Protected variables # ExecutableStatus instance self.__status = MultiRunnableStatus(self) # # Private variables self.__logger = a99.get_python_logger() self.__sid = pyfant.SID(_multi_id_maker()) self.__runnable_manager = None
def create_or_replace_or_skip_links(ff, dest_dir="."): """Creates a series of links given a list of target filepaths. Args: ff: list of full path to files dest_dir: destination directory It skips files of types FileMain, FileAbonds, FileDissoc, FileToH """ for f in ff: name = os.path.split(f)[1] ptd = os.path.join(dest_dir, name) # path to destination flag_skip = False a99.get_python_logger().info(("Considering file '%s' ..." % name)) if os.path.isfile(ptd) and not os.path.islink(ptd): _print_skipped("file exists in local directory") flag_skip = True else: obj = f311.load_with_classes(f, [ pyfant.FileMain, pyfant.FileAbonds, pyfant.FileDissoc, pyfant.FileToH ]) if obj is not None: _print_skipped("detected type %s" % obj.__class__.__name__) flag_skip = True else: obj = f311.load_with_classes(f, [pyfant.FileModBin]) if obj is not None: if len(obj) == 1: _print_skipped("%s of only one record" % obj.__class__.__name__) flag_skip = True if not flag_skip: try: if os.path.islink(ptd): os.remove(ptd) s_action = "replaced existing" else: s_action = "created" a99.create_symlink(f, ptd) a99.get_python_logger().info(" ... %s link" % s_action) except Exception as e: a99.print_error("Error creating link: %s" % str(e))
def setup_inputs(dest_dir='.', star='sun-asplund-2009', common='common', h=True, atoms=True, molecules=True, opa=True): """ Sets up input data for spectral synthesis. Args: dest_dir='.': directory where files and links will be created star='sun-asplund-2009': directory (relative to PFANT/data) for stellar data common='common': directory (relative to PFANT/data) for star-independent data files h=True: whether to look for hmap.dat atoms=True: whether to look for atoms.dat molecules=True: whether to look for molecules.dat opa=True: whether to look for grid.moo """ import pyfant as pf logger = a99.get_python_logger() dd = pyfant.get_pfant_path("data") # Functions that return full path, given a filename, to ... fd = lambda filename: os.path.join(dest_dir, filename ) # ... Destination directory fs = lambda filename: os.path.join(dd, star, filename ) # ... Stellar data directory fc = lambda filename: os.path.join(dd, common, filename ) # ... Common data directory # ## main.dat not present if not os.path.isfile(fd("main.dat")): zz_mnbp = [ "main.dat", "abonds.dat", "modeles.mod" ] # files that must not be present if main.dat is not present for z in zz_mnbp: if os.path.isfile(fd(z)): raise RuntimeError( "Found file '%s' in local directory." "If 'main.dat' is not present, files %s must also not exist." % (z, zz_mnbp[1:])) # ## Stellar data... zz = ["main.dat", "abonds.dat"] copy_or_skip_files([fs(z) for z in zz], dest_dir=dest_dir) # ## Common data... zz = ["absoru2.dat", "partit.dat", "grid.mod"] if opa: zz.append("grid.moo") if h: zz.append("hmap.dat") if atoms: zz.append("atoms.dat") if molecules: zz.append("molecules.dat") create_or_replace_or_skip_links([fc(z) for z in zz], dest_dir=dest_dir)
def _file_changed(self): f = None try: f = self._load_lines() except Exception as e: self._flines = None self._speciess = [] msg = "Error reading contents of file '{}': '{}'".format( self.w_file.value, a99.str_exc(e)) self.add_log_error(msg, True) a99.get_python_logger().exception(msg) # Tries to restore iso without much compromise self._set_species(self.w_conv.f.obj["species"]) self.w_conv.f.obj["species"] = self._get_species() self.w_file.flag_valid = f is not None self.changed.emit()
def link_to_data(src_dir=None): """Creates symbolic links to files '*.dat', '*.mod', '*.moo' within src_dir, skipping certain types Args: src_dir: directory containing files to be linked to. If omitted, it defaults to PFANT/data/common """ if src_dir is None: src_dir = pyfant.get_pfant_path('data', 'common') a99.get_python_logger().debug("Will look inside directory %s" % src_dir) # makes list of files to analyse types = ('*.dat', '*.mod', '*.moo') ff = [] for type_ in types: ff.extend(glob.glob(os.path.join(src_dir, type_))) create_or_replace_or_skip_links(ff)
def run(self): self.__logger = a99.get_python_logger() # this was leaving file open after finished add_file_handler(self.__logger, "python.log") self.__logger.debug("\\o/ %s is alive \\o/" % (self.name)) misses = 0 flag_sleep = False while True: if self.flag_exit: break with self.lock: if self.runnable: self.flag_idle = False try: self.runnable.run() if self.manager.flag_auto_clean and self.runnable.flag_success: self.runnable.load_result() self.runnable.sid.clean() except pyfant.FailedError as E: # If runnable fails, the current behaviour is use ErrorCollector and # critical-log the error if found something; otherwise dir_ = self.runnable.conf.sid.dir k = a99.ErrorCollector(flag_warnings=False) k.collect_errors(dir_) template = "Caught in rm.py::_Runner.run() --- {} failed! See below:\n" \ "{}" msg_fortran = "\n".join( ["! {}".format(x) for x in k.get_plain_text()]) msg = template.format(self.runnable.__class__.__name__, msg_fortran) self.__logger.critical(msg) # # if isinstance(E, IOError): # printOpenFiles() # # self.__logger.exception("%s failed" % self.runnable.__class__.__name__) # print "EXITING SO THAT YOU CAN SEE THE ERROR" # self.manager.exit() # raise self.manager._finish(self) self.runnable = None self.flag_idle = True else: misses += 1 if misses >= 34: flag_sleep = True misses = 0 if flag_sleep: T = 0.1 time.sleep(T) flag_sleep = False
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
def on_submit(self): flag_ok = True errors = self._check_single_setup() if len(errors) == 0: # more error checking if self.checkbox_custom_id.isChecked(): s = self.__get_custom_session_id() if len(s) == 0: errors.append("Please inform custom session id.") elif len( errors ) == 0: # will only offer to remove directory if everything is ok so far dirname = _get_custom_dirname(s) if os.path.isdir(dirname): r = QMessageBox.question( self, "Directory exists", "Directory '%s' already exists.\n\n" "Would you like to remove it?" % dirname, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if r == QMessageBox.Yes: try: shutil.rmtree(dirname) except Exception as e: errors.append(str(e)) else: return if len(errors) == 0: try: self._manager_form.show() self._manager_form.raise_() self._manager_form.activateWindow() self.__submit_job() except Exception as e: errors.append(str(e)) a99.get_python_logger().exception("Cannot submit job") if len(errors) > 0: a99.show_error("Cannot submit job:\n - " + ("\n - ".join(errors)))
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
def __init__(self, *args, max_simultaneous=None, flag_auto_clean=False, flag_verbose=False, flag_exit_if_fail=False, **kwargs): self.__max_simultaneous = max_simultaneous self.__flag_auto_clean = flag_auto_clean self.__flag_verbose = flag_verbose self.__flag_exit_if_fail = flag_exit_if_fail if self.__max_simultaneous is None: self.__max_simultaneous = multiprocessing.cpu_count() QObject.__init__(self) threading.Thread.__init__(self, *args, **kwargs) self.__logger = a99.get_python_logger() # counts finished self.__num_finished = 0 # counts failed self.__num_failed = 0 # all runnables self.__runnables = [] # FIFO stack containing indexes of __runnables to run self.__idxs_to_run = collections.deque() # flag to exit as soon as possible self.__flag_exit = False # set to True if explicitly cancelled through calling cancel() self.__flag_cancelled = False # indicates whether the runnable manages is paused. # When paused, it will not delegate new tasks to the runners. self.__flag_paused = False # Runner threads self.__runners = [] # # Locks self.__lock = Lock() # self.__lock = MyLock("RM Lock", True) # # Statistics # time the thread has started self.__time_started = None # time the last runnable has finished self.__time_finished = None # average time to run each runnable self.__time_per_runnable = 0 for i in range(self.__max_simultaneous): t = _Runner(self) self.__runners.append(t)
def populate_hitrandb(db): """Populates database with HITRAN data Populates a sqlite3 database represented by a FileHitranDB object with information downloaded from the HITRAN website Args: db: FileHitranDB instance .. todo:: This routine is never used """ import pyfant assert isinstance(db, pyfant.FileHitranDB) conn = db.get_conn() try: mols, _ = pyfant.get_hitran_molecules() conn.executemany("insert into molecule values (?,?,?)", mols) a99.get_python_logger().info("Inserted {} molecules".format(len(mols))) for mol in mols: isos, _ = pyfant.get_hitran_isotopologues(mol[0]) for iso in isos: try: conn.execute("insert into isotopologue values(?,?,?,?,?)", [iso[0], mol[0]] + iso[1:]) except: a99.get_python_logger().exception("Tried to insert this: {}".format(iso)) raise a99.get_python_logger().info("Inserted {} isotopologues for molecule '{}' ({})". format(len(isos), *mol[1:3])) finally: conn.commit() conn.close() conn.close()
a99.logging_level = logging.INFO a99.flag_log_file = True if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__, formatter_class=a99.SmartFormatter) names = pyfant.Conf().opt.get_names() # option names for name in names: # name = name.replace('_', '-') parser.add_argument("--" + name, type=str, help='') args = parser.parse_args() # Configuration for Python logging messages. logger = a99.get_python_logger() c = pyfant.Combo() c.conf.flag_log_file = True # Configuration for Fortran messages c.conf.flag_log_console = True # " c.conf.flag_output_to_dir = False # Will generate outputs in current directory for name in names: x = args.__getattribute__(name) if x is not None: c.conf.opt.__setattr__(name, x) c.run() logger.info("Session directory: %s" % c.conf.sid.dir)
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])