|
| 1 | +"""Console script for daops.""" |
| 2 | + |
| 3 | +__author__ = """Alan Iwi""" |
| 4 | +__contact__ = 'alan.iwi@stfc.ac.uk' |
| 5 | +__copyright__ = "Copyright 2023 United Kingdom Research and Innovation" |
| 6 | +__license__ = "BSD - see LICENSE file in top-level package directory" |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import argparse |
| 11 | +import dateutil.parser |
| 12 | +import configparser |
| 13 | + |
| 14 | +from daops.ops.subset import subset |
| 15 | +from roocs_utils.utils.file_utils import FileMapper |
| 16 | + |
| 17 | +def parse_args(): |
| 18 | + |
| 19 | + parser = argparse.ArgumentParser() |
| 20 | + sub_parsers = parser.add_subparsers() |
| 21 | + sub_parsers.required = True |
| 22 | + |
| 23 | + parser_subset = sub_parsers.add_parser('subset', help='subset data') |
| 24 | + parser_subset.add_argument('--area', '-a', type=str, |
| 25 | + help=('area in format w,s,e,n. Hint: if w is negative, include an "=" sign ' |
| 26 | + 'e.g. --area=-10,...')) |
| 27 | + parser_subset.add_argument('--time', '-t', type=str, metavar='time_window', |
| 28 | + help='time window e.g. 1999-01-01T00:00:00/2100-12-30T00:00:00') |
| 29 | + parser_subset.add_argument('--time-components', '-c', type=str, |
| 30 | + help="time components e.g. month:dec,jan,feb or 'year:1970,1980|month:01,02,03'") |
| 31 | + parser_subset.add_argument('--levels', '-l', type=str, |
| 32 | + help=('comma-separated list of levels (e.g. 500,1000,2000) ' |
| 33 | + 'or slash-separated range (e.g. 50/2000 for 50 to 2000)')) |
| 34 | + parser_subset.add_argument('--output-format', '-f', type=str, metavar='format', |
| 35 | + choices=('netcdf', 'nc', 'zarr'), default='netcdf') |
| 36 | + parser_subset.add_argument('--file-namer', '-F', type=str, |
| 37 | + choices=('simple', 'standard'), default='standard') |
| 38 | + parser_subset.add_argument('--output-dir', '-d', type=str, metavar='output_directory', required=True) |
| 39 | + parser_subset.add_argument('collection', type=str, nargs='+', default=list) |
| 40 | + |
| 41 | + return parser.parse_args() |
| 42 | + |
| 43 | + |
| 44 | +def get_params(args): |
| 45 | + |
| 46 | + collection = args.collection if len(args.collection) == 1 else FileMapper(args.collection) |
| 47 | + |
| 48 | + return {'collection': collection, |
| 49 | + 'time': args.time, |
| 50 | + 'time_components': args.time_components, |
| 51 | + 'area': args.area, |
| 52 | + 'level': args.levels, |
| 53 | + 'output_type': args.output_format, |
| 54 | + 'output_dir': args.output_dir, |
| 55 | + 'file_namer': args.file_namer, |
| 56 | + 'apply_fixes': False |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | +def check_env(): |
| 61 | + """ |
| 62 | + Check that ROOCS_CONFIG points to a valid config file |
| 63 | + (although for certain types of invalid file, in fact main is never called, |
| 64 | + so exit might not always be graceful in these cases). |
| 65 | + Call this after get_params() so that 'help' still works even if this is not set. |
| 66 | + """ |
| 67 | + config_env_var = 'ROOCS_CONFIG' |
| 68 | + c = configparser.ConfigParser() |
| 69 | + try: |
| 70 | + ret = c.read(os.environ[config_env_var]) |
| 71 | + except (KeyError, configparser.Error): |
| 72 | + ret = None |
| 73 | + if not ret: |
| 74 | + print(f'Environment variable {config_env_var} must contain the path name of a config file in ini format') |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + args = parse_args() |
| 80 | + params = get_params(args) |
| 81 | + check_env() |
| 82 | + ret = subset(**params) |
| 83 | + for uri in ret.file_uris: |
| 84 | + print(uri) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + sys.exit(main()) # pragma: no cover |
0 commit comments