-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli
executable file
·118 lines (100 loc) · 3.23 KB
/
cli
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!./venv/bin/python
from datetime import datetime
from datetime import timedelta
import sys
import logging
import os
import glob
import click
from Grids.Grids import Grids
from Grids.config import config
LOGGER = logging.getLogger(__name__)
FORMAT = "%(levelname)s - %(asctime)s - %(name)s - %(message)s"
logging.basicConfig(stream=sys.stderr, level=logging.INFO, format=FORMAT)
@click.group()
def cli():
pass
@cli.command("g2dss")
@click.option("--projects", default=None)
@click.option("--start", default=None)
@click.option("--end", default=None)
@click.option("--data_types", default=None)
@click.option("--dss_paths", default="both")
@click.option("--force", is_flag=True)
@click.option("--split", default=True)
def g2dss(projects, start, end, data_types, force, split, dss_paths):
fmt = "%Y%m%d"
if projects == "all":
projects = list(config.keys())
else:
projects = [s.strip() for s in projects.split(",")]
if data_types == "all":
data_types = ["QPE", "QTF", "QTE", "QPF"]
else:
data_types = [s.strip() for s in data_types.split(",")]
if not end:
end = datetime.now()
else:
end = datetime.strptime(end, fmt)
if not start:
start = end
else:
start = datetime.strptime(start, fmt)
delta = end - start
g = Grids()
for data_type in data_types:
for i in range(delta.days + 1):
date = (end - timedelta(days=i)).strftime(fmt)
try:
g.get_grid(
data_type=data_type,
date=date,
force=force,
split=split,
set_dataset=True,
)
except:
LOGGER.error(f"Fatal error for {data_type} {date}", exc_info=True)
continue
g.get_grid(
data_type=data_type,
date=date,
force=False,
split=False,
set_dataset=True,
)
g.warp()
for project in projects:
g.clip_to_dss(project=project, dss_paths=dss_paths)
@cli.command("blend")
@click.option("--projects", default=None)
@click.option("--lookback", default=10)
@click.option("--data_types", default=None)
@click.option("--force", is_flag=True)
def blend(projects, lookback, data_types, force):
lookback = int(lookback)
if projects == "all":
projects = list(config.keys())
else:
projects = [s.strip() for s in projects.split(",")]
if data_types == "all":
data_types = ["QP", "QT"]
else:
data_types = [s.strip() for s in data_types.split(",")]
for data_type in data_types:
g = Grids()
g.blend(data_type=data_type, lookback=lookback, force=force)
for project in projects:
project_pathname = os.path.join("data", f"NWD_{project}.blend.dss")
if os.path.exists(project_pathname):
os.remove(project_pathname)
g.clip_to_dss(project=project, dss_paths=[project_pathname])
g.dataset.close()
old_files = glob.glob("temp/*.nc")
for f in old_files:
try:
os.remove(f)
except:
continue
if __name__ == "__main__":
cli()