-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_scan.py
More file actions
254 lines (222 loc) · 7.33 KB
/
demo_scan.py
File metadata and controls
254 lines (222 loc) · 7.33 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Demonstrate how to build a fixer configuration automatically."""
# ruff: noqa: T201
from __future__ import annotations
from typing import TYPE_CHECKING, Any
import fixer_cmip7.fixes
import yaml
from fixer_cmip7.tests.test_fixes import create_test_dataset
if TYPE_CHECKING:
import fixer
import xarray as xr
def find_coord(
ds: xr.Dataset,
coordinate: fixer.fixes.Coordinate,
) -> xr.DataArray | None:
"""Find a coordinate in the dataset that matches the definition.
Parameters
----------
ds:
The dataset to search.
coordinate:
The coordinate definition to match.
Returns
-------
:
The matching coordinate, or None if no match is found.
"""
if (axis := coordinate.attrs.get("axis")) and axis in ds.cf.axes:
return ds[ds.cf.axes[axis]]
if (
standard_name := coordinate.attrs.get("standard_name")
) and standard_name in ds.cf.coordinates:
coord_names = ds.cf.coordinates[standard_name]
if len(coord_names) == 1:
return ds[coord_names[0]]
return None
def find_dim_coord(
ds: xr.Dataset,
coordinate: fixer.fixes.Coordinate,
) -> xr.DataArray | None:
"""Find a 1D coordinate in the dataset that matches the definition.
Parameters
----------
ds:
The dataset to search.
coordinate:
The coordinate definition to match.
Returns
-------
:
The matching coordinate, or None if no match is found.
"""
if not coordinate.dims or len(coordinate.dims) != 1:
return None
coord_name = None
if (axis := coordinate.attrs.get("axis")) and axis in ds.cf.axes:
coord_name = ds.cf.axes[axis]
elif (
standard_name := coordinate.attrs.get("standard_name")
) and standard_name in ds.cf.coordinates:
coord_names = [
coord_name
for coord_name in ds.cf.coordinates[standard_name]
if len(ds[coord_name].sizes) == 1
]
if len(coord_names) == 1:
coord_name = coord_names[0]
if coord_name is not None:
return ds[coord_name]
return None
def build_mapping( # noqa: C901
ds: xr.Dataset,
variable: fixer.fixes.Variable,
) -> tuple[dict[str, str], dict[str, str]]:
"""Return a mapping from variable definition to dataset variables.
Parameters
----------
ds:
The dataset to scan.
variable:
The variable definition.
Returns
-------
:
A fixer configuration for the reformat fix.
"""
dim_map = {}
variable_map = {}
for coord_def in variable.coords:
# Populate the dim_map.
if (coord := find_dim_coord(ds, coord_def)) is not None:
dims = list(coord.sizes)
if len(dims) == 1:
dim_name = dims[0]
if dim_name != coord_def.dims[0]:
dim_map[coord_def.dims[0]] = dim_name
# Populate the variable_map.
if (coord := find_coord(ds, coord_def)) is not None:
if coord.name != coord_def.name:
variable_map[coord_def.name] = coord.name
if (
coord_def.bounds
and (standard_name := coord.attrs.get("standard_name"))
and standard_name in ds.cf.bounds
):
bound_names = ds.cf.bounds[standard_name]
if len(bound_names) == 1:
if bound_names[0] != coord_def.bounds.name:
variable_map[coord_def.bounds.name] = bound_names[0]
bounds_dim_name = ds.cf.get_bounds_dim_name(standard_name)
def_bounds_dim_name = next(
iter(set(coord_def.bounds.dims) - set(coord_def.dims)),
)
if bounds_dim_name != def_bounds_dim_name:
dim_map[def_bounds_dim_name] = bounds_dim_name
# Add the main variable to the variable_map.
bound_names = {name for names in ds.cf.bounds.values() for name in names}
variables = [v for v in ds.data_vars if v not in bound_names]
if len(variables) == 1:
var_name = variables[0]
if var_name != variable.name:
variable_map[variable.name] = var_name
return dim_map, variable_map
def scan_flip_coords(
ds: xr.Dataset,
variable: fixer.fixes.Variable,
) -> list[dict[str, Any]]:
"""Scan a dataset and return a fixer configuration for the flip_coords fix.
Parameters
----------
ds:
The dataset to scan.
variable:
The variable definition.
Returns
-------
:
A list of fixer configurations for the flip_coords fix.
"""
result = []
for coord_def in variable.coords:
if (
coord_def.dims
and len(coord_def.dims) == 1
and "stored_direction" in coord_def.requirements
):
# Try to find the coordinate in the dataset.
coord_name = None
if (axis := coord_def.attrs.get("axis")) and axis in ds.cf.axes:
coord_name = ds.cf.axes[axis]
elif (
standard_name := coord_def.attrs.get("standard_name")
) and standard_name in ds.cf.coordinates:
coord_names = [
coord_name
for coord_name in ds.cf.coordinates[standard_name]
if len(ds[coord_name].sizes) == 1
]
if len(coord_names) == 1:
coord_name = coord_names[0]
if coord_name is None:
continue
# Check the direction of the coordinate.
coord = ds[coord_name]
coord_direction = (
"increasing"
if coord[0].values < coord[-1].values
else "decreasing"
)
expected_direction = coord_def.requirements["stored_direction"]
if coord_direction != expected_direction:
result.append(
{
"function": "fixer.fixes.flip_coords",
"coord": coord_name,
},
)
return result
def scan_cmip7(
ds: xr.Dataset,
realm: str,
branded_variable: str,
) -> list[dict[str, Any]]:
"""Scan a dataset and return a list of fixes.
Parameters
----------
ds:
The dataset to scan.
realm:
The CMIP7 realm.
branded_variable:
The branded variable name.
Returns
-------
:
A configurations for the fixes that should be applied to the dataset.
"""
variable = fixer_cmip7.fixes.CMIP7Variable.from_cmor_table(
table_id=realm,
entry=branded_variable,
)
dim_map, variable_map = build_mapping(ds, variable)
return [
{
"function": "fixer_cmip7.fixes.reformat",
"realm": realm,
"branded_variable": branded_variable,
"dim_map": dim_map,
"variable_map": variable_map,
},
*scan_flip_coords(ds, variable),
]
def main() -> None:
"""Scan a dataset for problems."""
ds = create_test_dataset()
print("Original:\n", ds)
print()
print("Scanning..")
fixer_configuration = scan_cmip7(ds, "atmos", "tas_tavg-h2m-hxy-u")
print("Found fixes:")
print(yaml.safe_dump(fixer_configuration, sort_keys=False))
if __name__ == "__main__":
main()