#!/data_systems/opt/bin/python3 """ send_cips_images_utils.py Created 2019-04 @author: Bill Barrett """ import re import argparse from datetime import date, timedelta import os.path from aimpi_path_server import AimPiPathServer YEAR_DAY_OF_YEAR = re.compile(r'(?P20[012]\d).*(?P[0-3]\d{2})') class SendCipsImages: """ Utilities common to sending all CIPS images """ def __init__(self, sys_args, definitive=True): """Read the json file containing the process information such as the perl script used, the partial text for the regular expression, the levels (if present), and the path prefix (if present) sys_args : list typically 'sys.argv' from the command line definitive : bool True if this is definitive data, false if preliminary """ parser = argparse.ArgumentParser(sys_args[0]) # The command line arguments currently expected are the following parser.add_argument('-cp', nargs='?', type=str, help='optional path for configuration files') parser.add_argument('-p', nargs='?', type=bool, help='optional boolean for whether or not this is production') parser.add_argument('-date', nargs='?', type=str, help='optional date in form yyyyddd') # Was a configuration file path specified? parsed_sys_argv = parser.parse_args() if parsed_sys_argv.cp: self.config_path = parsed_sys_argv.cp else: self.config_path = None print('configuration path: {}'.format(self.config_path)) # Is this a production or test system? if parsed_sys_argv.p: self.production = parsed_sys_argv.p else: self.production = False # Get the path to the configuration file self.config_file_path = AimPiPathServer( self.config_path, self.production).get_aimpi_config_file_path() print(self.config_file_path) if parsed_sys_argv.date: year_day_of_year = YEAR_DAY_OF_YEAR.match(parsed_sys_argv.date) assert year_day_of_year is not None, \ 'date "{}" is not in the format YYYYDDD'.format(parsed_sys_argv.date) self.year = year_day_of_year.group('year') self.day_of_year = year_day_of_year.group('day_of_year') else: if definitive: base_date = date.today() - timedelta(5) else: base_date = date.today() - timedelta(2) self.year = base_date.timetuple().tm_year self.day_of_year = base_date.timetuple().tm_yday print('year: {0}, day of year: {1}'.format(self.year, self.day_of_year)) self.definitive = definitive def get_search_directory(self, season, version, revision, is_raa=False): """ Get the the directory to search for files to attach to the outgoing email Parameters ---------- season : str the season in which the data was created version : str the data version revision : str the data revision is_raa : bool defaults to False. if true set the search path to an RAA directory Returns ------- : str the the directory to search for files to attach to the outgoing email """ search_dir = os.path.join('/aim/data/cips', season) if is_raa: search_dir = os.path.join(search_dir, 'raa', 'level_2b') return os.path.join(search_dir, 'ver_' + version, 'rev_' + revision)