Retrofitting code to allow merging

Sun, 26 Feb 2017 22:30:13 +0200

author
Iannis <ulalume3@yahoo.com>
date
Sun, 26 Feb 2017 22:30:13 +0200
changeset 65
33623b2e149c
parent 63
ed4ae866a95a
child 66
51b799247d67

Retrofitting code to allow merging

atmospheric_lidar/scripts/licel2scc_depol.py file | annotate | diff | comparison | revisions
--- a/atmospheric_lidar/scripts/licel2scc_depol.py	Sun Feb 26 22:13:32 2017 +0200
+++ b/atmospheric_lidar/scripts/licel2scc_depol.py	Sun Feb 26 22:30:13 2017 +0200
@@ -1,10 +1,12 @@
 """ Command line tool to convert Licel binary files to SCC NetCDF format.
 """
+import argparse
+import glob
+import importlib
+import logging
 import os
 import sys
-import glob
-import argparse
-import importlib
+
 
 from ..licel_depol import LicelCalibrationMeasurement
 
@@ -56,7 +58,8 @@
 
     The file should contain python code."""
     if not os.path.isfile(settings_path):
-        raise IOError("The provided settings path does not correspond to a file.")
+        logging.error("The provided settings path does not correspond to a file.")
+        sys.exit(1)
 
     dirname, basename = os.path.split(settings_path)
     sys.path.append(dirname)
@@ -68,7 +71,7 @@
 
 def main():
     # Define the command line argument
-    parser = argparse.ArgumentParser(description="A program to convert LICEL binary files to the SCC NetCDF format.")
+    parser = argparse.ArgumentParser(description="A program to convert Licel binary files to the SCC NetCDF format.")
     parser.add_argument("parameter_file", help="The path to a parameter file linking licel and SCC channels.")
     parser.add_argument("plus45_string", nargs='?', help="Search string for plus 45 degree files (default '*.*')", default="*.*")
     parser.add_argument("minus45_string", nargs='?', help="Search string for minus 45 degree files (default '*.*')", default="*.*")
@@ -85,28 +88,47 @@
     parser.add_argument("-p", "--pressure", type=float,
                         help="The pressure (in hPa) at lidar level, required if using US Standard atmosphere",
                         default="1020")
+    # Verbosity settings from http://stackoverflow.com/a/20663028
+    parser.add_argument('-d', '--debug', help="Print dubuging information.", action="store_const",
+                        dest="loglevel", const=logging.DEBUG, default=logging.INFO,
+                        )
+    parser.add_argument('-s', '--silent', help="Show only warning and error messages.", action="store_const",
+                        dest="loglevel", const=logging.WARNING
+                        )
+
     args = parser.parse_args()
 
+    # Get the logger with the appropriate level
+    logging.basicConfig(format='%(levelname)s: %(message)s', level=args.loglevel)
+    logger = logging.getLogger(__name__)
+
+    #coloredlogs.install(fmt='%(levelname)s: %(message)s', level=args.loglevel)
+
     # Get a list of files to convert
     plus45_files = glob.glob(args.plus45_string)
     minus45_files = glob.glob(args.minus45_string)
 
     if len(plus45_files)==0 or len(minus45_files)==0:
-        print "No files found when searching for %s and %s." % (plus45_files, minus45_files)
+        logger.error("No files found when searching for %s and %s." % (plus45_files, minus45_files))
         sys.exit(1)
 
     # Read the files
-    print "Reading {0} files from {1}".format(len(plus45_files), args.plus45_string)
-    print "Reading {0} files from {1}".format(len(minus45_files), args.minus45_string)
+    logger.info("Reading {0} files from {1}".format(len(plus45_files), args.plus45_string))
+    logger.info("Reading {0} files from {1}".format(len(minus45_files), args.minus45_string))
 
     CustomLidarMeasurement = create_custom_class(args.parameter_file, args.id_as_name, args.temperature,
                                                      args.pressure)
+
     measurement = CustomLidarMeasurement(plus45_files, minus45_files)
-    measurement = measurement.subset_by_scc_channels()
-        
-        # Save the netcdf
 
-    print "Saving netcdf."
+    try:
+        measurement = measurement.subset_by_scc_channels()
+    except ValueError as err:
+        logging.error(err)
+        sys.exit(1)
+
+    # Save the netcdf
+    logger.info("Saving netcdf")
     measurement.set_measurement_id(args.measurement_id, args.measurement_number)
     measurement.save_as_netcdf()
-    print "Created file ", measurement.scc_filename
+    logger.info("Created file %s" % measurement.scc_filename)

mercurial