|
| 1 | +""" |
| 2 | +hypernets_processor cli |
| 3 | +""" |
| 4 | + |
| 5 | +from hypernets_processor.version import __version__ |
| 6 | +from hypernets_processor.utils.config import ( |
| 7 | + PROCESSOR_CONFIG_PATH, |
| 8 | + JOB_CONFIG_TEMPLATE_PATH, |
| 9 | + PROCESSOR_WATER_DEFAULTS_CONFIG_PATH, |
| 10 | + PROCESSOR_LAND_DEFAULTS_CONFIG_PATH |
| 11 | +) |
| 12 | +from hypernets_processor.utils.cli import configure_std_parser |
| 13 | +from hypernets_processor.utils.config import read_config_file |
| 14 | +from hypernets_processor.main.sequence_processor_main import main |
| 15 | +from datetime import datetime as dt |
| 16 | +import sys |
| 17 | +import os |
| 18 | + |
| 19 | + |
| 20 | +'''___Authorship___''' |
| 21 | +__author__ = "Sam Hunt" |
| 22 | +__created__ = "26/3/2020" |
| 23 | +__version__ = __version__ |
| 24 | +__maintainer__ = "Sam Hunt" |
| 25 | +__email__ = "sam.hunt@npl.co.uk" |
| 26 | +__status__ = "Development" |
| 27 | + |
| 28 | + |
| 29 | +def configure_parser(): |
| 30 | + """ |
| 31 | + Configure parser |
| 32 | +
|
| 33 | + :return: parser |
| 34 | + :rtype: argparse.ArgumentParser |
| 35 | + """ |
| 36 | + |
| 37 | + description = "Tool for processing Hypernets Land and Water Network hyperspectral field data" |
| 38 | + |
| 39 | + # Create standard parser |
| 40 | + parser = configure_std_parser(description=description) |
| 41 | + |
| 42 | + # Add specific arguments |
| 43 | + parser.add_argument("-i", "--input-directory", action="store", |
| 44 | + help="Directory of input data") |
| 45 | + parser.add_argument("-o", "--output-directory", action="store", |
| 46 | + help="Directory to write output data to") |
| 47 | + parser.add_argument("-n", "--network", action="store", choices=["land", "water"], |
| 48 | + help="Network to process file for") |
| 49 | + # parser.add_argument("--plot", action="store_true", |
| 50 | + # help="Generate plots of processed data") |
| 51 | + parser.add_argument("--write-all", action="store_true", |
| 52 | + help="Write all products at intermediate data processing levels before final product") |
| 53 | + parser.add_argument("-j", "--job-config", action="store", |
| 54 | + help="Use instead of above arguments to specify job with configuration file") |
| 55 | + return parser |
| 56 | + |
| 57 | + |
| 58 | +parser = configure_parser() |
| 59 | +parsed_args = parser.parse_args() |
| 60 | + |
| 61 | + |
| 62 | +if parsed_args.job_config and ( |
| 63 | + parsed_args.input_directory or parsed_args.output_directory |
| 64 | + or parsed_args.network or parsed_args.write_all |
| 65 | +): |
| 66 | + print("-j is mutually exclusive with other input arguments") |
| 67 | + sys.exit(2) |
| 68 | + |
| 69 | + |
| 70 | +def cli(): |
| 71 | + """ |
| 72 | + Command line interface to sequence_processor_main for ad-hoc job processing |
| 73 | + """ |
| 74 | + |
| 75 | + # If job config specified use |
| 76 | + if parsed_args.job_config: |
| 77 | + tmp_job = False |
| 78 | + job_config_path = parsed_args.job_config |
| 79 | + |
| 80 | + # Else build and write temporary job config from command line arguments |
| 81 | + else: |
| 82 | + tmp_job = True |
| 83 | + |
| 84 | + processor_defaults = PROCESSOR_WATER_DEFAULTS_CONFIG_PATH |
| 85 | + if parsed_args.network == "land": |
| 86 | + processor_defaults = PROCESSOR_LAND_DEFAULTS_CONFIG_PATH |
| 87 | + |
| 88 | + job_config = read_config_file([JOB_CONFIG_TEMPLATE_PATH, processor_defaults]) |
| 89 | + |
| 90 | + if parsed_args.input_directory is not None: |
| 91 | + job_config["Input"]["raw_data_directory"] = os.path.abspath(parsed_args.input_directory) |
| 92 | + else: |
| 93 | + print("-i required") |
| 94 | + sys.exit(2) |
| 95 | + |
| 96 | + if parsed_args.output_directory is not None: |
| 97 | + job_config["Output"]["archive_directory"] = os.path.abspath(parsed_args.output_directory) |
| 98 | + else: |
| 99 | + print("-o required") |
| 100 | + sys.exit(2) |
| 101 | + |
| 102 | + if parsed_args.write_all: |
| 103 | + for key in job_config["Output"].keys(): |
| 104 | + if key[:5] == "write": |
| 105 | + job_config["Output"][key] = "True" |
| 106 | + |
| 107 | + job_config["Log"]["log_path"] = os.path.abspath(parsed_args.log) if parsed_args.log is not None else "" |
| 108 | + job_config["Log"]["verbose"] = str(parsed_args.verbose) if parsed_args.verbose is not None else "" |
| 109 | + job_config["Log"]["quiet"] = str(parsed_args.quiet) if parsed_args.verbose is not None else "" |
| 110 | + |
| 111 | + job_config["Job"]["job_name"] = "run_" + dt.now().strftime("%Y%m%dT%H%M%S") |
| 112 | + home_directory = os.path.expanduser("~") |
| 113 | + job_config["Job"]["job_working_directory"] = os.path.join(home_directory, ".hypernets", "tmp") |
| 114 | + job_config_path = os.path.join( |
| 115 | + job_config["Job"]["job_working_directory"], |
| 116 | + job_config["Job"]["job_name"] + ".config" |
| 117 | + ) |
| 118 | + |
| 119 | + os.makedirs(job_config["Job"]["job_working_directory"], exist_ok=True) |
| 120 | + with open(job_config_path, "w") as f: |
| 121 | + job_config.write(f) |
| 122 | + |
| 123 | + # run main |
| 124 | + main(processor_config_path=PROCESSOR_CONFIG_PATH, job_config_path=job_config_path, to_archive=False) |
| 125 | + |
| 126 | + if tmp_job: |
| 127 | + os.remove(job_config_path) |
| 128 | + |
| 129 | + return None |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + pass |
0 commit comments