|
| 1 | +"""PyStemmusScope directories utilities. |
| 2 | +
|
| 3 | +Module designed to manage input directories and data for running the model |
| 4 | +and storing outputs. |
| 5 | +""" |
| 6 | +import logging |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import time |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +def read_config(path_to_config_file): |
| 16 | + """Read config from given config file. |
| 17 | +
|
| 18 | + Load paths from config file and save them into dict. |
| 19 | +
|
| 20 | + Args: |
| 21 | + path_to_config_file: Path to the config file. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + Dictionary containing paths to work directory and all sub-directories. |
| 25 | + """ |
| 26 | + config = {} |
| 27 | + with open(path_to_config_file, "r", encoding="utf8") as f: |
| 28 | + for line in f: |
| 29 | + (key, val) = line.split("=") |
| 30 | + config[key] = val.rstrip('\n') |
| 31 | + |
| 32 | + return config |
| 33 | + |
| 34 | +def create_io_dir(forcing_filename, config): |
| 35 | + """Create input directory and copy required files. |
| 36 | +
|
| 37 | + Work flow executor to create work directory and all sub-directories. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + Path (string) to input, output directory and config file for every station/forcing. |
| 41 | + """ |
| 42 | + # get start time with the format Y-M-D-HM |
| 43 | + timestamp = time.strftime('%Y-%m-%d-%H%M') |
| 44 | + station_name = forcing_filename.split('_')[0] |
| 45 | + # create input directory |
| 46 | + input_dir = Path(f"{config['WorkDir']}/input/{station_name}_{timestamp}") |
| 47 | + Path(input_dir).mkdir(parents=True, exist_ok=True) |
| 48 | + message = f"Prepare work directory {input_dir} for the station: {station_name}" |
| 49 | + logger.info("%s", message) |
| 50 | + # copy model parameters to work directory |
| 51 | + _copy_data(input_dir, config) |
| 52 | + input_dir = str(input_dir) |
| 53 | + |
| 54 | + # create output directory |
| 55 | + output_dir = Path(f"{config['WorkDir']}/output/{station_name}_{timestamp}") |
| 56 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 57 | + message = f"Prepare work directory {output_dir} for the station: {station_name}" |
| 58 | + logger.info("%s", message) |
| 59 | + output_dir = str(output_dir) |
| 60 | + |
| 61 | + # update config file for ForcingFileName and InputPath |
| 62 | + config_file_path = _update_config_file(forcing_filename, input_dir, output_dir, |
| 63 | + config, station_name, timestamp) |
| 64 | + |
| 65 | + return input_dir, output_dir, config_file_path |
| 66 | + |
| 67 | +def _copy_data(input_dir, config): |
| 68 | + """Copy required data to the work directory. |
| 69 | +
|
| 70 | + Create sub-directories inside the work directory and copy data. |
| 71 | +
|
| 72 | + Args: |
| 73 | + input_dir: Path to the input directory. |
| 74 | + config: Dictionary containing all the paths. |
| 75 | + """ |
| 76 | + folder_list_vegetation = ["directional", "fluspect_parameters", "leafangles", |
| 77 | + "radiationdata", "soil_spectrum"] |
| 78 | + for folder in folder_list_vegetation: |
| 79 | + os.makedirs(input_dir / folder, exist_ok=True) |
| 80 | + shutil.copytree(str(config[folder]), str(input_dir / folder), dirs_exist_ok=True) |
| 81 | + |
| 82 | + # copy input_data.xlsx |
| 83 | + shutil.copy(str(config["input_data"]), str(input_dir)) |
| 84 | + |
| 85 | +def _update_config_file(nc_file, input_dir, output_dir, config, station_name, timestamp): #pylint: disable=too-many-arguments |
| 86 | + """Update config file for each station. |
| 87 | +
|
| 88 | + Create config file for each forcing/station under the work directory. |
| 89 | +
|
| 90 | + Args: |
| 91 | + ncfile: Name of forcing file. |
| 92 | + input_dir: Path to the input directory. |
| 93 | + output_dir: Path to the output directory. |
| 94 | + config: Dictionary containing all the paths. |
| 95 | + station_name: Station name inferred from forcing file. |
| 96 | + timestamp: Timestamp when creating the config file. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + Path to updated config file. |
| 100 | + """ |
| 101 | + config_file_path = Path(input_dir, f"{station_name}_{timestamp}_config.txt") |
| 102 | + with open(config_file_path, 'w', encoding="utf8") as f: |
| 103 | + for key, value in config.items(): |
| 104 | + if key == "ForcingFileName": |
| 105 | + update_entry = f"{key}={nc_file}\n" |
| 106 | + elif key == "InputPath": |
| 107 | + update_entry = f"{key}={str(input_dir)}/\n" |
| 108 | + elif key == "OutputPath": |
| 109 | + update_entry = f"{key}={str(output_dir)}/\n" |
| 110 | + else: |
| 111 | + update_entry = f"{key}={value}\n" |
| 112 | + |
| 113 | + f.write(update_entry) |
| 114 | + |
| 115 | + return str(config_file_path) |
0 commit comments