# HG changeset patch # User Ioannis # Date 1537868594 -10800 # Node ID 03b470b0a05f5ced1d05021bc9a64f2f0adcb430 # Parent 1e18b2a416ad3e16304770a38ea2033d1b674ca4 Improvment on telecover. diff -r 1e18b2a416ad -r 03b470b0a05f atmospheric_lidar/scripts/licel2tc.py --- a/atmospheric_lidar/scripts/licel2tc.py Fri Sep 14 16:37:20 2018 +0300 +++ b/atmospheric_lidar/scripts/licel2tc.py Tue Sep 25 12:43:14 2018 +0300 @@ -2,7 +2,6 @@ """ import argparse import glob -import importlib import logging import os import sys @@ -20,7 +19,6 @@ class TelecoverDataset(object): - def __init__(self, filenames, settings_path, licel_timezone='UTC'): """ Create a telecover dataset object. @@ -64,14 +62,20 @@ def read_files(self): for filename in self.filenames: - current_file = LicelFile(filename, use_id_as_name=True, licel_timezone=self.licel_timezone) + # Load only header + current_file = LicelFile(filename, use_id_as_name=True, licel_timezone=self.licel_timezone, import_now=False) + current_file.import_header_only() # Populate the dictionary linking sites with files. site = current_file.site - if current_file.site in self.file_sites.keys(): - self.file_sites[site].append(current_file) - else: - self.file_sites[site] = [current_file, ] + + if site in self.settings['sectors'].values(): + current_file.import_file() # Import full data + if current_file.site in self.file_sites.keys(): + + self.file_sites[site].append(current_file) + else: + self.file_sites[site] = [current_file, ] def check_files_ok(self): """ Check if the available files are enough to create the ASCII files. """ @@ -83,10 +87,10 @@ sector_name)) len_sector = len(self.file_sites[site_name]) - if len_sector> 1: + if len_sector > 1: logger.info('More than one files ({0}) found in sector {1} (site name {2})'.format(len_sector, - sector_name, - site_name)) + sector_name, + site_name)) # Check that all files have the correct channels channels = self.settings['channels'].keys() @@ -115,21 +119,27 @@ date, z = self._get_file_metadata() file_paths = [] + + logger.info('Creating {0} files.'.format(len(self.settings['channels']))) + for channel_id, channel_settings in self.settings['channels'].items(): output_filename = '{call_sign}_tc_{date}_{name}.dat'.format(call_sign=self.settings['call_sign'], date=date.strftime('%Y%m%dT%H'), name=channel_settings['short_name']) + output_path = os.path.join(output_dir, output_filename) + logger.info("Output file path: {0}".format(output_path)) + header_txt = ("{s[call_sign]} ({s[site]})\r\n" "{s[system_name]}\r\n" "{name}\r\n" "{date}\r\n" "range, {sectors}").format(s=self.settings, - name=channel_settings['name'], - date=date.strftime('%d.%m.%Y, %H'), - sectors=', '.join(self.settings['sector_order'])) + name=channel_settings['name'], + date=date.strftime('%d.%m.%Y, %H'), + sectors=', '.join(self.settings['sector_order'])) # Get altitude data sectors = self.settings['sector_order'] @@ -146,6 +156,9 @@ data = np.array(data).T[:bin_max, :] np.savetxt(output_path, data, fmt='%.5e', delimiter=',', newline='\r\n', header=header_txt, comments='') + + logger.info("File saved.") + file_paths.append(output_path) return file_paths @@ -172,7 +185,7 @@ """ settings = self.settings['channels'][channel.channel_name] - trigger_delay = settings.get('trigger_dealy', 0) + trigger_delay = settings.get('trigger_delay', 0) background_min = settings['background_min'] background_max = settings['background_max'] @@ -208,30 +221,21 @@ return data_rc -def convert_to_telecover(files, dark_pattern, measurement_id, measurement_number): + +def convert_to_telecover(file_paths, settings_path, licel_timezone, output_dir): """ Convert files to SCC. """ - measurement = CustomLidarMeasurement(files) - # Get a list of files containing dark measurements - if dark_pattern != "": - dark_files = glob.glob(dark_pattern) - if dark_files: - logger.debug("Using %s as dark measurements files!" % ', '.join(dark_files)) - measurement.dark_measurement = CustomLidarMeasurement(dark_files) - else: - logger.warning( - 'No dark measurement files found when searching for %s. Will not use any dark measurements.' % dark_pattern) try: - measurement = measurement.subset_by_scc_channels() - except ValueError as err: + dataset = TelecoverDataset(file_paths, settings_path, licel_timezone) + except Exception as err: logger.error(err) sys.exit(1) - # Save the netcdf - logger.info("Saving netcdf") - measurement.set_measurement_id(measurement_id, measurement_number) - measurement.save_as_SCC_netcdf() - logger.info("Created file %s" % measurement.scc_filename) + try: + dataset.save_ascii(output_dir) + except Exception as err: + logger.error(err) + sys.exit(2) def main(): @@ -242,23 +246,11 @@ parser.add_argument("files", help="Location of licel files. Use relative path and filename wildcards. (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("-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") - parser.add_argument('-D', '--dark_measurements', - help="Location of files containing dark measurements. Use relative path and filename wildcars, see 'files' parameter for example.", - default="", dest="dark_files" - ) + parser.add_argument('-o', '--output', help='Output directory.', default='.') + parser.add_argument('--licel_timezone', help="String describing the timezone according to the tz database.", default="UTC", dest="licel_timezone", ) - # 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, @@ -280,7 +272,8 @@ sys.exit(0) # Get a list of files to process - files = glob.glob(args.files) + logger.debug("Search path: {0}".format(os.path.expanduser(args.files))) + files = glob.glob(os.path.expanduser(args.files)) # If not files found, exit if len(files) == 0: @@ -288,6 +281,10 @@ sys.exit(1) # If everything OK, proceed - logger.info("Found {0} files matching {1}".format(len(files), os.path.abspath(args.files))) + logger.info("Found {0} files matching {1}".format(len(files), args.files)) + + convert_to_telecover(files, args.settings_file, args.licel_timezone, args.output) - logger.error('Nothing implemented yet!') + +if __name__ == "__main__": + main() diff -r 1e18b2a416ad -r 03b470b0a05f setup.py --- a/setup.py Fri Sep 14 16:37:20 2018 +0300 +++ b/setup.py Tue Sep 25 12:43:14 2018 +0300 @@ -58,6 +58,7 @@ ], entry_points={ 'console_scripts': ['licel2scc = atmospheric_lidar.scripts.licel2scc:main', - 'licel2scc-depol = atmospheric_lidar.scripts.licel2scc_depol:main'], + 'licel2scc-depol = atmospheric_lidar.scripts.licel2scc_depol:main', + 'licel2tc = atmospheric_lidar.scripts.licel2tc:main'], }, )