def parseargs(): """ Read and process the command line arguments. Returns: -------- args: Namespace object Object with the command line arguments. Modification History: --------------------- 2013 madison Initial implementation. 2014-03-06 patricio Updated from optparse to argparse. Added documentation and config_file option. [email protected] """ # Parser to process a configuration file: cparser = argparse.ArgumentParser(description=__doc__, add_help=False, formatter_class=argparse.RawDescriptionHelpFormatter) # Add config file option: cparser.add_argument("-c", "--config_file", help="Configuration filename (string).", metavar="FILE") # remaining_argv contains all other command-line-arguments: args, remaining_argv = cparser.parse_known_args() # Get parameters from configuration file (if exists): if args.config_file: if not os.path.isfile(args.config_file): ut.printexit("Configuration file '%s' does not exist."%args.config_file) config = ConfigParser.SafeConfigParser() config.read([args.config_file]) if "Parameters" not in config.sections(): ut.printexit("Invalid configuration file: '%s'."%args.config_file) defaults = dict(config.items("Parameters")) # Store these arguments as lists: if "db_list" in defaults: defaults["db_list"] = defaults["db_list"].split() if "part_list" in defaults: defaults["part_list"] = defaults["part_list"].split() if "dbtype" in defaults: defaults["dbtype"] = defaults["dbtype"].split() else: defaults = {} # Inherit options from cparser: parser = argparse.ArgumentParser(parents=[cparser]) # General Options: parser.add_argument("-v", "--verbose-level", action="store", help="Verbosity level (integer) [default: %(default)s].", dest="verb", type=int, default=2) parser.add_argument("-q", "--quiet", action="store_false", help="Set verbosity level to 0.", dest="verb") # Database Options: group = parser.add_argument_group("Database Options") group.add_argument("-o", "--output", action = "store", help="Output filename (string) [default: '%(default)s'].", dest= "output", default = "output.tli") group.add_argument("-d", "--database", action="append", help="Path (string) to the input line-transition " "database file(s).", dest="db_list") group.add_argument("-p", "--partition", action="append", help="Path (string) to the auxiliary partition-function " "file(s).", dest="part_list") group.add_argument("-t", "--dbtype", action="append", help="Database type (string). 'ps' for Partridge & " "Schwenke's H2O; 'hit' for HITRAN and HITEMP; or " "'ts' for Schwenke's TiO.", choices=('ps', 'hit', 'ts'), dest="dbtype") # Wavelength Options: group = parser.add_argument_group("Wavelength Options") group.add_argument("-i", "--wl-init", action="store", help="Initial wavelength (microns) [default: " "%(default)s].", dest="iwav", type=float, default=1.0) group.add_argument("-f", "--wl-final", action="store", help="Final wavelength (microns) [default: %(default)s].", dest="fwav", type=float, default=2.0) parser.set_defaults(**defaults) args = parser.parse_args(remaining_argv) return args
def parseargs(): """ Read and process the command line arguments. Returns: -------- args: Namespace object Object with the command line arguments. Modification History: --------------------- 2013 madison Initial implementation. 2014-03-06 patricio Updated from optparse to argparse. Added documentation and config_file option. [email protected] 2017-10-27 mhimes Added multiprocessing. Added ncores (number of cores) and defn (HITRAN molecules definition file) config args """ # Parser to process a configuration file: cparser = argparse.ArgumentParser(description=__doc__, add_help=False, formatter_class=argparse.RawDescriptionHelpFormatter) # Add config file option: cparser.add_argument("-c", "--config_file", help="Configuration filename (string).", metavar="FILE") # remaining_argv contains all other command-line-arguments: args, remaining_argv = cparser.parse_known_args() # Get parameters from configuration file (if exists): if args.config_file: if not os.path.isfile(args.config_file): ut.printexit("Configuration file '{:s}' does not exist.". format(args.config_file)) config = ConfigParser.SafeConfigParser() config.read([args.config_file]) if "Parameters" not in config.sections(): ut.printexit("Invalid configuration file: '{:s}'.". format(args.config_file)) defaults = dict(config.items("Parameters")) # Store these arguments as lists: if "db_list" in defaults: defaults["db_list"] = defaults["db_list"].split() if "part_list" in defaults: defaults["part_list"] = defaults["part_list"].split() if "dbtype" in defaults: defaults["dbtype"] = defaults["dbtype"].split() else: defaults = {} # Inherit options from cparser: parser = argparse.ArgumentParser(parents=[cparser]) # General Options: parser.add_argument("-v", "--verbose-level", action="store", help="Verbosity level (integer) [default: %(default)s].", dest="verb", type=int, default=2) parser.add_argument("-q", "--quiet", action="store_false", help="Set verbosity level to 0.", dest="verb") parser.add_argument("-N", "--ncores", action="store", help="Maximum number of cores", dest="ncores", type=int, default=1) # Database Options: group = parser.add_argument_group("Database Options") group.add_argument("-o", "--output", action = "store", help="Output filename (string) [default: '%(default)s'].", dest= "output", default = "output.tli") group.add_argument("-d", "--database", action="append", help="Path (string) to the input line-transition " "database file(s).", dest="db_list") group.add_argument("-p", "--partition", action="append", help="Path (string) to the auxiliary partition-function " "file(s).", dest="part_list") group.add_argument("-t", "--dbtype", action="append", help="Database type (string). 'ps' for Partridge & " "Schwenke's H2O; 'hit' for HITRAN and HITEMP; " "'ts' for Schwenke's TiO, or 'vo' for Plez's VO.", choices=('ps', 'hit', 'ts', 'vo'), dest="dbtype") # HITRAN definitions file group.add_argument("-n", "--defn", action="store", help="Path/to/file of HITRAN definitions file", dest="defn") # Wavelength Options: group = parser.add_argument_group("Wavelength Options") group.add_argument("-i", "--wl-init", action="store", help="Initial wavelength (microns) [default: " "%(default)s].", dest="iwav", type=float, default=1.0) group.add_argument("-f", "--wl-final", action="store", help="Final wavelength (microns) [default: %(default)s].", dest="fwav", type=float, default=2.0) parser.set_defaults(**defaults) args = parser.parse_args(remaining_argv) return args
# Number of files: Nfiles = len(dblist) # Driver routine to read the databases: driver = [] for i in np.arange(Nfiles): if dbtype[i] == "ps": driver.append(ps.pands(dblist[i], pflist[i])) elif dbtype[i] == "hit": driver.append(hit.hitran(dblist[i], pflist[i])) elif dbtype[i] == "ts": driver.append(ts.tioschwenke(dblist[i], pflist[i])) elif dbtype[i] == "vo": driver.append(vo.voplez(dblist[i], pflist[i])) else: ut.printexit("Unknown Database type (%d): '%s'"%(i+1, dbtype[i])) ut.lrprint(verbose-10, "File %d, database name: '%s'"%(i+1, driver[i].name)) ut.lrprint(verbose, "Beginning to write the TLI file: '%s'"%outputfile) # Open output file: TLIout = open(outputfile, "wb") # Get the machine endian type (big/little): endianness = sys.byteorder # Hardcoded implementation of lineread's "magic number" check for endianness # derived from binary encoding the letters TLI into binary location # and checking the order if endianness == 'big': magic = '\xab\xb3\xb6\xff' if endianness == 'little':
# Number of files: Nfiles = len(dblist) # Driver routine to read the databases: driver = [] for i in np.arange(Nfiles): if dbtype[i] == "ps": driver.append(ps.pands(dblist[i], pflist[i])) elif dbtype[i] == "hit": driver.append(hit.hitran(dblist[i], pflist[i], defn)) elif dbtype[i] == "ts": driver.append(ts.tioschwenke(dblist[i], pflist[i])) elif dbtype[i] == "vo": driver.append(vo.voplez(dblist[i], pflist[i])) else: ut.printexit("Unknown Database type ({:d}): '{:s}'". format(i+1, dbtype[i])) ut.lrprint(verbose-10, "File {:d}, database name: '{:s}'". format(i+1, driver[i].name)) ut.lrprint(verbose, "Beginning to write the TLI file: '{:s}'". format(outputfile)) # Open output file: TLIout = open(outputfile, "wb") # Get the machine endian type (big/little): endianness = sys.byteorder # Hardcoded implementation of lineread's "magic number" check for endianness # derived from binary encoding the letters TLI into binary location # and checking the order if endianness == 'big':