-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_decadal_fix.py
executable file
·201 lines (173 loc) · 6.23 KB
/
generate_decadal_fix.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
"""
To use this script:
In dachar directory:
python generate_decadal_fix.py -f /path/to/file.json -d CMIP6.DCPP.MOHC.HadGEM3-GC31-MM.dcppA-hindcast.s2004-r3i1p1f2.Amon.pr.gn.v20200417
will generate a fix template at '/path/to/file.json' for the dataset id "CMIP6.DCPP.MOHC.HadGEM3-GC31-MM.dcppA-hindcast.s2004-r3i1p1f2.Amon.pr.gn.v20200417"
The year will be taken from the ds id if possible for the start date of the experiment, and will be YYYY-11-01T00:00:00
"""
import argparse
import copy
import json
import os
import re
import sys
from datetime import datetime
import numpy as np
import xarray as xr
from roocs_utils.project_utils import dsid_to_datapath
def arg_parse():
parser = argparse.ArgumentParser()
parser.add_argument(
"-f",
"--fpath",
type=str,
required=True,
help="Path to write the JSON fix file to.",
)
parser.add_argument(
"-d",
"--dsid",
type=str,
required=True,
help="Dataset ID to create the fix template for.",
)
return parser.parse_args()
decadal_template = {
"dataset_id": "",
"fixes": [
{
"fix_id": "VarAttrFix",
"operands": {"var_id": "time", "attrs": {"long_name": "valid_time"}},
"source": {
"name": "ceda",
"version": "",
"comments": "",
"url": "https://github.com/cp4cds/c3s34g_master/tree/master/Decadal",
},
},
{
"fix_id": "GlobalAttrFix",
"operands": {
"attrs": {
"forcing_description": "derive: daops.fix_utils.decadal_utils.get_decadal_model_attr_from_dict: forcing_description",
"physics_description": "derive: daops.fix_utils.decadal_utils.get_decadal_model_attr_from_dict: physics_description",
"initialization_description": "derive: daops.fix_utils.decadal_utils.get_decadal_model_attr_from_dict: initialization_description",
"startdate": "derive: daops.fix_utils.decadal_utils.get_sub_experiment_id",
"sub_experiment_id": "derive: daops.fix_utils.decadal_utils.get_sub_experiment_id",
}
},
"source": {
"name": "ceda",
"version": "",
"comments": "",
"url": "https://github.com/cp4cds/c3s34g_master/tree/master/Decadal",
},
},
{
"fix_id": "AddScalarCoordFix",
"operands": {
"var_id": "reftime",
"value": "derive: daops.fix_utils.decadal_utils.get_reftime",
"dtype": "datetime64[ns]",
"attrs": {
"long_name": "Start date of the forecast",
"standard_name": "forecast_reference_time",
},
"encoding": {
"dtype": "int32",
"units": "days since 1850-01-01",
"calendar": "derive: daops.fix_utils.decadal_utils.get_time_calendar",
},
},
"source": {
"name": "ceda",
"version": "",
"comments": "",
"url": "https://github.com/cp4cds/c3s34g_master/tree/master/Decadal",
},
},
{
"fix_id": "AddCoordFix",
"operands": {
"var_id": "leadtime",
"value": "derive: daops.fix_utils.decadal_utils.get_lead_times",
"dim": ["time"],
"dtype": "float64",
"attrs": {
"long_name": "Time elapsed since the start of the forecast",
"standard_name": "forecast_period",
"units": "days",
},
"encoding": {"dtype": "double"},
},
"source": {
"name": "ceda",
"version": "",
"comments": "",
"url": "https://github.com/cp4cds/c3s34g_master/tree/master/Decadal",
},
},
{
"fix_id": "AddDataVarFix",
"operands": {
"var_id": "realization",
"value": "1",
"dtype": "int32",
"attrs": {
"long_name": "realization",
"comment": "For more information on the ripf, refer to the variant_label, initialization_description, physics_description and forcing_description global attributes",
},
},
"source": {
"name": "ceda",
"version": "",
"comments": "",
"url": "https://github.com/cp4cds/c3s34g_master/tree/master/Decadal",
},
},
{
"fix_id": "RemoveCoordAttrFix",
"operands": {
"var_ids": "derive: daops.fix_utils.decadal_utils.get_decadal_bnds_list"
},
"source": {
"name": "ceda",
"version": "",
"comments": "",
"url": "https://github.com/cp4cds/c3s34g_master/tree/master/Decadal",
},
},
],
}
def get_decadal_template():
"Takes a copy of the global template, and returns it."
tmpl = copy.deepcopy(decadal_template)
return tmpl
def get_ds_ids(fpath):
ds_ids = []
with open(fpath, "r") as f:
for line in f:
ds_id = line.rstrip()
ds_ids.append(ds_id)
return ds_ids
def main():
args = arg_parse()
fpath = args.fpath
ds_id = args.dsid
data_path = dsid_to_datapath(ds_id)
# put into template
dt = get_decadal_template()
dt["dataset_id"] = ds_id
pathname = os.path.dirname(sys.argv[0])
if ds_id in get_ds_ids(os.path.join(pathname, "further_info_url-dsets.txt")):
global_attrs = dt["fixes"][1].get("operands").get("attrs")
global_attrs.update(
further_info_url="derive: daops.fix_utils.decadal_utils.fix_further_info_url"
)
dt["fixes"][1]["operands"]["attrs"] = global_attrs
# save the template as a json file
with open(fpath, "w") as fp:
json.dump(dt, fp, indent=4)
if __name__ == "__main__":
main()