Skip to content

Commit 155390f

Browse files
committed
Add support for several grids in same realm
1 parent f7f7034 commit 155390f

3 files changed

Lines changed: 75 additions & 9 deletions

File tree

dr2xml/Xwrite.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ def write_xios_file_def(filename, svars_per_table, year, dummies, skipped_vars_p
464464
set_config_variable("domain_defs", OrderedDict())
465465
# Add xml_file_definition
466466
xml_file_definition = DR2XMLElement(tag="file_definition")
467-
_, hgrid, _, _, _ = internal_dict['grids'][internal_dict["select_grid_choice"]][context]
468467
files_list = determine_files_list(svars_per_table, enddate, year, debug)
468+
_, hgrid, _, _, _ = internal_dict['grids'][internal_dict["select_grid_choice"]][context]["default"]
469469
for file_dict in files_list:
470470
write_xios_file_def_for_svars_list(hgrid=hgrid, xml_file_definition=xml_file_definition, dummies=dummies,
471471
skipped_vars_per_table=skipped_vars_per_table,
@@ -751,6 +751,7 @@ def get_grid_info(sv, grid, table):
751751
grid_choice = internal_dict["grid_choice"]
752752
context_index = get_config_variable("context_index")
753753
if grid in ["", ]:
754+
internal_grids = internal_dict["grids"][grid_choice][context]
754755
# either native or close-to-native
755756
if sv.type in ["dev", ]:
756757
target_grid = sv.description.split('|')[1]
@@ -767,10 +768,10 @@ def get_grid_info(sv, grid, table):
767768
grids_dev[sv.label][grid_choice][context]
768769
else:
769770
grid_label, target_hgrid_id, zgrid_id, grid_resolution, grid_description = \
770-
internal_dict["grids"][grid_choice][context]
771+
internal_grids.get(sv.label, internal_grids["default"])
771772
else:
772773
grid_label, target_hgrid_id, zgrid_id, grid_resolution, grid_description = \
773-
internal_dict["grids"][grid_choice][context]
774+
internal_grids.get(sv.label, internal_grids["default"])
774775
else:
775776
if grid in ['cfsites', ]:
776777
target_hgrid_id = cfsites_domain_id

dr2xml/projects/dr2xml.json

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -731,14 +731,38 @@
731731
}
732732
]
733733
},
734+
"variables_per_grid_type": {
735+
"help": "List of variables associated with a grid type",
736+
"values": [
737+
{
738+
"type": "value",
739+
"origin": "laboratory",
740+
"keys": ["variables_per_grid_type"]
741+
},
742+
{}
743+
],
744+
"target_type": "dict"
745+
},
734746
"grids": {
735747
"help": "Grids : per model resolution and per context :\\n- CMIP6 qualifier (i.e. 'gn' or 'gr') for the main grid chosen (because you may choose has main production grid a regular one, when the native grid is e.g. unstructured)\\n- Xios id for the production grid (if it is not the native grid)\\n- Xios id for the latitude axis used for zonal means (mist match latitudes for grid above)\\n- resolution of the production grid (using CMIP6 conventions)\\n- grid description",
736748
"fatal": true,
737749
"values": [
738750
{
739-
"type": "value",
740-
"origin": "laboratory",
741-
"keys": ["grids"]
751+
"type": "func",
752+
"origin": "functions_file",
753+
"keys": ["format_grids"],
754+
"options": {
755+
"grids": {
756+
"type": "value",
757+
"origin": "laboratory",
758+
"keys": ["grids"]
759+
},
760+
"variables_per_grid_type": {
761+
"type": "value",
762+
"origin": "internal",
763+
"keys": ["variables_per_grid_type"]
764+
}
765+
}
742766
}
743767
]
744768
},
@@ -1354,9 +1378,10 @@
13541378
"keys": ["excluded_dimensions"]
13551379
}
13561380
],
1357-
"not_values": [null]
1381+
"not_values": [[]]
13581382
}
1359-
]
1383+
],
1384+
"target_type": "list"
13601385
},
13611386
"select_excluded_expgroups": {
13621387
"help": "Excluded experiments groups for variable selection.",

dr2xml/projects/dr2xml_func.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
from __future__ import print_function, division, absolute_import, unicode_literals
99

10+
import copy
11+
from collections import defaultdict
12+
1013
from utilities.logger import get_logger
1114

1215

@@ -64,4 +67,41 @@ def sort_mips(mips):
6467
rep = rep | mips[grid]
6568
else:
6669
rep = mips
67-
return sorted(list(rep))
70+
return sorted(list(rep))
71+
72+
73+
def format_grids(grids, variables_per_grid_type):
74+
logger = get_logger()
75+
if not isinstance(grids, dict) or any([not isinstance(grids[elt], dict) for elt in grids]):
76+
logger.error("Grids must be defined as a dict of resolution, realm and grid_type")
77+
raise ValueError("Grids must be defined as a dict of resolution, realm and grid_type")
78+
else:
79+
issues = list()
80+
for (resolution, dict_resolution) in grids.items():
81+
for (realm, dict_realm) in dict_resolution.items():
82+
if isinstance(dict_realm, list) and len(dict_realm) == 5:
83+
new_dict_realm = dict(default=dict_realm)
84+
grids[resolution][realm] = copy.deepcopy(new_dict_realm)
85+
elif isinstance(dict_realm, dict) and \
86+
all([isinstance(elt, dict) and len(elt) == 5 for elt in dict_realm.values()]) and \
87+
"default" in dict_realm:
88+
pass
89+
else:
90+
issues.append((resolution, realm))
91+
if len(issues) > 0:
92+
logger.error("Issues in grids definition for the following (resolution, realm) couples:\n%s" % issues)
93+
raise ValueError("Issues in grids definition for the following (resolution, realm) couples:\n%s" % issues)
94+
else:
95+
rep = defaultdict(lambda: defaultdict(dict))
96+
for (resolution, dict_resolution) in grids.items():
97+
for (realm, dict_realm) in dict_resolution.items():
98+
rep[resolution][realm]["default"] = copy.deepcopy(grids[resolution][realm]["default"])
99+
for (grid_type, grid_type_list) in dict_realm.items():
100+
grid_def = copy.deepcopy(grids[resolution][realm][grid_type])
101+
if grid_type in grids[resolution][realm]:
102+
for var in grid_type_list:
103+
rep[resolution][realm][var] = grid_def
104+
else:
105+
logger.warning("Could not find grid_type %s for resolution %s and realm %s, use default." %
106+
(grid_type, resolution, realm))
107+
return rep

0 commit comments

Comments
 (0)