-
Notifications
You must be signed in to change notification settings - Fork 8
/
pp_ns_data.py
executable file
·94 lines (77 loc) · 2.66 KB
/
pp_ns_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# coding: utf-8
"""Process NS UM output by interpolating selected fields to common grid."""
# Commonly used standard library tools
import argparse
from datetime import timedelta
from pathlib import Path
from time import time
import warnings
# My packages and local scripts
from aeolus.core import Run
from commons import (
DT_FMT,
NS_CYCLE_TIMES,
NS_MODEL_TYPES,
)
import mypaths
from proc_um_output import process_cubes
from utils import create_logger
# Global definitions and styles
warnings.filterwarnings("ignore")
SCRIPT = Path(__file__).name
def parse_args(args=None):
"""Argument parser."""
ap = argparse.ArgumentParser(
SCRIPT,
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
ap.add_argument(
"-p", "--planet", type=str, required=True, help="Planet configuration"
)
ap.add_argument("-r", "--run", type=str, required=True, help="Run key")
return ap.parse_args(args)
def main(args=None):
"""Main entry point of the script."""
# Parse command-line arguments
args = parse_args(args)
planet = args.planet
L.info(f"planet = {planet}")
run_key = args.run
L.info(f"run_key = {run_key}")
subdir = f"{planet}_{run_key}"
label = f"{planet}_{run_key}"
L.info(f"label = {label}")
for model_type, model_specs in NS_MODEL_TYPES.items():
L.info(f"model_type = {model_type}")
label = f"{planet}_{run_key}_{model_type}"
for i in range(NS_CYCLE_TIMES[subdir]["ndays"]):
_cycle = (NS_CYCLE_TIMES[subdir]["start"] + timedelta(days=i)).strftime(
DT_FMT
)
L.info(f"_cycle = {_cycle}")
fpath = mypaths.nsdir / subdir / _cycle / model_specs["path"]
L.info(f"fpath = {fpath}")
# Create a subdirectory for processed data
outdir = fpath.parent / "_processed"
outdir.mkdir(parents=True, exist_ok=True)
# Initialise a `Run` by loading data from the selected files
run = Run(
files=fpath,
name=label,
planet=planet,
model_type=model_type,
timestep=model_specs["timestep"],
)
# Regrid & interpolate data
run.proc_data(process_cubes, timestep=run.timestep)
# Write the data to a netCDF file
fname_out = outdir / f"{run.name}_{_cycle}.nc"
run.to_netcdf(fname_out)
L.success(f"Saved to {fname_out}")
if __name__ == "__main__":
t0 = time()
L = create_logger(Path(__file__))
main()
L.info(f"Execution time: {time() - t0:.1f}s")