# HG changeset patch # User Ioannis # Date 1487229725 -7200 # Node ID 673843d7f9d8b32169ca0887790dbb2c65d4c1ed # Parent 51e0df09a5da4b170bb4db37dac21929f0223be9 Adding command line script. Rename of convertion script folder diff -r 51e0df09a5da -r 673843d7f9d8 atmospheric_lidar/scripts/__init__.py diff -r 51e0df09a5da -r 673843d7f9d8 atmospheric_lidar/scripts/licel2scc.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/atmospheric_lidar/scripts/licel2scc.py Thu Feb 16 09:22:05 2017 +0200 @@ -0,0 +1,108 @@ +""" Command line tool to convert Licel binary files to SCC NetCDF format. +""" +import os +import sys +import glob +import argparse +import importlib + +from ..licel import LicelLidarMeasurement + + +def create_custom_class(custom_netcdf_parameter_path, use_id_as_name=False, temperature=25., pressure=1020.): + """ This funtion creates a custom LicelLidarMeasurement subclass, + based on the input provided by the users. + + Parameters + ---------- + custom_netcdf_parameter_path: str + The path to the custom channels parameters. + use_id_as_name: bool + Defines if channels names are descriptive or transient digitizer IDs. + temperature: float + The ground temperature in degrees C (default 25.0). + pressure: float + The ground pressure in hPa (default: 1020.0). + + Returns + ------- + CustomLidarMeasurement: + A custom sub-class of LicelLidarMeasurement + """ + + custom_netcdf_parameters = read_settings_file(custom_netcdf_parameter_path) + + class CustomLidarMeasurement(LicelLidarMeasurement): + extra_netcdf_parameters = custom_netcdf_parameters + + def __init__(self, filelist=None): + super(CustomLidarMeasurement, self).__init__(filelist, use_id_as_name) + + def get_PT(self): + ''' Sets the pressure and temperature at station level. This is used if molecular_calc parameter is + set to 0 (i.e. use US Standard atmosphere). + + The results are stored in the info dictionary. + ''' + + self.info['Temperature'] = temperature + self.info['Pressure'] = pressure + + return CustomLidarMeasurement + + +def read_settings_file(settings_path): + """ Read the settings file. + + 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.") + + dirname, basename = os.path.split(settings_path) + sys.path.append(dirname) + + module_name, _ = os.path.splitext(basename) + settings = importlib.import_module(module_name) + return settings + + +def main(): + # Define the command line argument + 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("directory", nargs='?', help="Directory containing licel files (default '.')", default='.') + parser.add_argument("search_string", nargs='?', help="Search string for files in directory (default '*.*')", default="*.*") + parser.add_argument("-i", '--id_as_name', + help="Use transient digitizer ids as channel names, instead of descriptive names", + action="store_true") + parser.add_argument("-m", "--measurement_id", help="The new measurement id", default=None) + parser.add_argument("-n", "--measurement_number", + help="The measurement number for the date from 00 to 99. Used if no id is provided", + default="00") + parser.add_argument("-t", "--temperature", type=float, + help="The temperature (in C) at lidar level, required if using US Standard atmosphere", + default="25") + parser.add_argument("-p", "--pressure", type=float, + help="The pressure (in hPa) at lidar level, required if using US Standard atmosphere", + default="1020") + args = parser.parse_args() + + # Get a list of files to convert + search_str = os.path.join(args.directory, args.search_string) + files = glob.glob(search_str) + + print(args.parameter_file) + if files: + # Read the files + print "Reading {0} files from {1}".format(len(files), args.directory) + CustomLidarMeasurement = create_custom_class(args.parameter_file, args.id_as_name, args.temperature, + args.pressure) + measurement = CustomLidarMeasurement(files) + + # Save the netcdf + print "Saving netcdf." + measurement.set_measurement_id(args.measurement_id, args.measurement_number) + measurement.save_as_netcdf() + print "Created file ", measurement.scc_filename + else: + print "No files found when searching for ", search_str diff -r 51e0df09a5da -r 673843d7f9d8 example_scripts/convert_ipral.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example_scripts/convert_ipral.py Thu Feb 16 09:22:05 2017 +0200 @@ -0,0 +1,74 @@ +""" Sample script to convert licel files to SCC netcdf format. + +The script assumes the following things: + +1. You have already installed the atmospheric_lidar module (e.g. using pip). +2. You have create a class in a file "ipral" describing your system (described the readme file). + If you want to use it for a different system you need to change the script to use your own class + +Run the script using: python convert_ipral.py + +Examples +-------- +# This will read all files starting with "l" from "my_dir". +# It will guess the measurement ID based on the date of the files, and will assume measurement number 00. +# For example, the new measurment id could be 20120101mb00 +python convert_ipral.py my_dir l*. + +# This will use the measurement id you defined. +python convert_ipral.py my_dir l*. -m 20120204mb32 # It will create the file 20120204mb32.nc + +Help string +----------- +# To get this, run: python convert_ipral.py -h +usage: convert_ipral.py [-h] [-m MEASUREMENT_ID] [-n MEASUREMENT_NUMBER] + [directory] [searchstring] + +positional arguments: + directory Directory with licel files. + searchstring Processing system id. + +optional arguments: + -h, --help show this help message and exit + -m MEASUREMENT_ID, --measurement_id MEASUREMENT_ID + The new measurement id + -n MEASUREMENT_NUMBER, --measurement_number MEASUREMENT_NUMBER + The measurement number for the date, if no id is + provided + +""" + +import glob +import os +import argparse + +from atmospheric_lidar import ipral + + +if __name__ == "__main__": + + # Define the command line arguments. + parser = argparse.ArgumentParser() + parser.add_argument("directory", nargs='?', help="Directory with licel files.", default='.') + parser.add_argument("searchstring", nargs='?', help="Processing system id.", default="*.*") + parser.add_argument("-m", "--measurement_id", help="The new measurement id", default=None) + parser.add_argument("-n", "--measurement_number", help="The measurement number for the date, if no id is provided", default="00") + args = parser.parse_args() + + + # Get a list of files to convert + search_str = os.path.join(args.directory, args.searchstring) + files = glob.glob(search_str) + + if files: + # Read the files + print "Reading {0} files from {1}".format(len(files), args.directory) + measurement = ipral.IpralLidarMeasurement(files) + + #Save the netcdf + print "Saving netcdf." + measurement.set_measurement_id(args.measurement_id, args.measurement_number) + measurement.save_as_netcdf() + print "Created file ", measurement.scc_filename + else: + print "No files found when searching for ", search_str \ No newline at end of file diff -r 51e0df09a5da -r 673843d7f9d8 example_scripts/convert_lilas.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example_scripts/convert_lilas.py Thu Feb 16 09:22:05 2017 +0200 @@ -0,0 +1,74 @@ +""" Sample script to convert licel files to SCC netcdf format. + +The script assumes the following things: + +1. You have already installed the atmospheric_lidar module (e.g. using pip). +2. You have create a class in a file "lilas" describing your system (described the readme file). + If you want to use it for a different system you need to change the script to use your own class + +Run the script using: python convert_lilas.py + +Examples +-------- +# This will read all files starting with "l" from "my_dir". +# It will guess the measurement ID based on the date of the files, and will assume measurement number 00. +# For example, the new measurment id could be 20120101mb00 +python convert_lilas.py my_dir l*. + +# This will use the measurement id you defined. +python convert_lilas.py my_dir l*. -m 20120204mb32 # It will create the file 20120204mb32.nc + +Help string +----------- +# To get this, run: python convert_lilas.py -h +usage: convert_lilas.py [-h] [-m MEASUREMENT_ID] [-n MEASUREMENT_NUMBER] + [directory] [searchstring] + +positional arguments: + directory Directory with licel files. + searchstring Processing system id. + +optional arguments: + -h, --help show this help message and exit + -m MEASUREMENT_ID, --measurement_id MEASUREMENT_ID + The new measurement id + -n MEASUREMENT_NUMBER, --measurement_number MEASUREMENT_NUMBER + The measurement number for the date, if no id is + provided + +""" + +import glob +import os +import argparse + +from atmospheric_lidar import lilas + + +if __name__ == "__main__": + + # Define the command line arguments. + parser = argparse.ArgumentParser() + parser.add_argument("directory", nargs='?', help="Directory with licel files.", default='.') + parser.add_argument("searchstring", nargs='?', help="Processing system id.", default="*.*") + parser.add_argument("-m", "--measurement_id", help="The new measurement id", default=None) + parser.add_argument("-n", "--measurement_number", help="The measurement number for the date, if no id is provided", default="00") + args = parser.parse_args() + + + # Get a list of files to convert + search_str = os.path.join(args.directory, args.searchstring) + files = glob.glob(search_str) + + if files: + # Read the files + print "Reading {0} files from {1}".format(len(files), args.directory) + measurement = lilas.LilasLidarMeasurement(files) + + #Save the netcdf + print "Saving netcdf." + measurement.set_measurement_id(args.measurement_id, args.measurement_number) + measurement.save_as_netcdf() + print "Created file ", measurement.scc_filename + else: + print "No files found when searching for ", search_str \ No newline at end of file