-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathobs4mips.py
More file actions
131 lines (108 loc) · 3.77 KB
/
obs4mips.py
File metadata and controls
131 lines (108 loc) · 3.77 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
import os.path
import pathlib
from pathlib import Path
from typing import Any
import pandas as pd
import xarray as xr
from ref_sample_data.data_request.base import IntakeESGFDataRequest
from ref_sample_data.data_request.cmip6 import prefix_to_filename
from ref_sample_data.resample import decimate_curvilinear, decimate_rectilinear
class Obs4MIPsRequest(IntakeESGFDataRequest):
"""
Represents a Obs4MIPs dataset request
"""
obs4mips_path_items = (
"activity_id",
"institution_id",
"source_id",
"variable_id",
"grid_label",
)
obs4mips_filename_paths = (
"variable_id",
"source_id",
"grid_label",
)
def __init__(self, facets: dict[str, Any], remove_ensembles: bool, time_span: tuple[str, str] | None):
self.avail_facets = [
"activity_id",
"institution_id",
"source_id",
"frequency",
"variable_id",
"grid_label",
"version",
"data_node",
]
self.facets = facets
self.remove_ensembles = remove_ensembles
self.time_span = time_span
super().__init__(remove_ensembles, time_span)
self.obs4mips_path_items = [
"activity_id",
"institution_id",
"source_id",
"variable_id",
"grid_label",
]
self.obs4mips_filename_paths = [
"variable_id",
"source_id",
"grid_label",
]
assert all(key in self.avail_facets for key in self.obs4mips_path_items), "Error message"
assert all(key in self.avail_facets for key in self.obs4mips_filename_paths), "Error message"
def decimate_dataset(self, dataset: xr.Dataset) -> xr.Dataset | None:
"""
Downscale the dataset to a smaller size.
Parameters
----------
dataset
The dataset to downscale
time_span
The time span to extract from a dataset
Returns
-------
xr.Dataset
The downscaled dataset
"""
has_latlon = "lat" in dataset.dims and "lon" in dataset.dims
has_ij = "i" in dataset.dims and "j" in dataset.dims
if has_latlon:
assert len(dataset.lat.dims) == 1 and len(dataset.lon.dims) == 1
result = decimate_rectilinear(dataset)
elif has_ij:
# 2d curvilinear grid (generally ocean variables)
result = decimate_curvilinear(dataset)
else:
raise ValueError("Cannot decimate this grid: too many dimensions")
if "time" in dataset.dims and self.time_span is not None:
result = result.sel(time=slice(*self.time_span))
if result.time.size == 0:
result = None
return result
def generate_filename(self, metadata: pd.Series, ds: xr.Dataset, ds_filename: pathlib.Path) -> Path:
"""
Create the output filename for the dataset.
Parameters
----------
ds
Loaded dataset
ds_filename
Filename of the dataset
Returns
-------
The output filename
"""
output_path = (
Path(os.path.join(*[metadata[item] for item in self.obs4mips_path_items]))
/ f"v{metadata['version']}"
)
if ds_filename.name.split("_")[0] == ds.variable_id:
filename_prefix = "_".join([metadata[item] for item in self.obs4mips_filename_paths])
else:
filename_prefix = ds_filename.name.split("_")[0] + "_"
filename_prefix += "_".join(
[metadata[item] for item in self.obs4mips_filename_paths if item != "variable_id"]
)
return output_path / prefix_to_filename(ds, filename_prefix)