-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathget_slice.py
132 lines (114 loc) · 5.77 KB
/
get_slice.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
# %%
from time import time
import pandas as pd
import xarray as xr
import zarr
# from numcodecs import blosc
#
# blosc.set_nthreads(16)
# %%
def get_nadir_slice(path, **slice_args):
dt_start = pd.to_datetime(slice_args.get('time_min', "2012-10-01"))
dt_end = pd.to_datetime(slice_args.get('time_max', "2013-10-01"))
groups = [f"{dt.year}/{dt.month}" for dt in
pd.date_range(start=dt_start.date().replace(day=1), end=dt_end, freq='MS')]
ts = (dt_start - pd.to_datetime("2012-10-01")).total_seconds()
te = (dt_end - pd.to_datetime("2012-10-01")).total_seconds()
dses = []
for group in groups:
with xr.open_zarr(zarr.DirectoryStore(path),
group=group, decode_times=False, consolidated=True,
synchronizer=zarr.ProcessSynchronizer(f'data/nadir.sync')) as ds:
units, reference_date = ds.time.attrs['units'].split('since')
ts = (dt_start - pd.to_datetime(reference_date)).to_timedelta64() / pd.to_timedelta(1, unit=units.strip())
te = (dt_end - pd.to_datetime(reference_date)) / pd.to_timedelta(1, unit=units.strip())
dses.append(
ds
.pipe(lambda ds: ds.isel(time=(ds.time < te) & (ds.time >= ts))).compute()
.pipe(xr.decode_cf)
.pipe(lambda ds: ds.isel(time=(ds.lat > slice_args.get('lat_min', 0))))
.pipe(lambda ds: ds.isel(time=(ds.lat < slice_args.get('lat_max', 360))))
.pipe(lambda ds: ds.isel(time=(ds.lon < slice_args.get('lon_max', 360))))
.pipe(lambda ds: ds.isel(time=(ds.lon > slice_args.get('lon_min', 0)))).compute()
)
if len(dses) == 0:
print(
f"no data found for {slice_args} {groups} {pd.date_range(start=dt_start.replace(day=1), end=dt_end, freq='MS')}")
return None
return xr.concat(
dses,
dim="time"
)
def get_swot_slice(path, drop_vars=('model_index',),
**slice_args):
dt_start = pd.to_datetime(slice_args.get('time_min', "2012-10-01"))
dt_end = pd.to_datetime(slice_args.get('time_max', "2013-10-01"))
groups = [f"{dt.year}/{dt.month}" for dt in
pd.date_range(start=dt_start.date().replace(day=1), end=dt_end, freq='MS')]
dses = []
for group in groups:
with xr.open_zarr(zarr.DirectoryStore(path), drop_variables=drop_vars, group=group,
decode_times=False,
consolidated=True,
synchronizer=zarr.ProcessSynchronizer(f'data/swot.sync')) as ds:
units, reference_date = ds.time.attrs['units'].split('since')
ts = (dt_start - pd.to_datetime(reference_date)).to_timedelta64().astype(float)
te = (dt_end - pd.to_datetime(reference_date)).to_timedelta64().astype(float)
dses.append(
ds
.pipe(lambda ds: ds.isel(time=(ds.time < te) & (ds.time >= ts))).compute()
.pipe(xr.decode_cf)
.pipe(lambda ds: ds.isel(time=(ds.lat_nadir > slice_args.get('lat_min', 0))))
.pipe(lambda ds: ds.isel(time=(ds.lat_nadir < slice_args.get('lat_max', 360))))
.pipe(lambda ds: ds.isel(time=(ds.lon_nadir < slice_args.get('lon_max', 360))))
.pipe(lambda ds: ds.isel(time=(ds.lon_nadir > slice_args.get('lon_min', 0)))).compute()
)
if len(dses) == 0:
print(
f"no data found for {slice_args} {groups} {pd.date_range(start=dt_start.replace(day=1), end=dt_end, freq='MS')}")
return None
return xr.concat(
dses,
dim="time"
)
def get_oi_slice(path, **slice_args):
with xr.open_dataset(path) as ds:
return (
ds
.pipe(lambda ds: ds.sel(time=slice(slice_args.get('time_min', "2012-10-01"),
slice_args.get('time_max', "2013-10-01"))))
.pipe(lambda ds: ds.isel(y=(ds.lat > slice_args.get('lat_min', 0))))
.pipe(lambda ds: ds.isel(y=(ds.lat < slice_args.get('lat_max', 360))))
.pipe(lambda ds: ds.isel(x=(ds.lon < slice_args.get('lon_max', 360))))
.pipe(lambda ds: ds.isel(x=(ds.lon > slice_args.get('lon_min', 0))))
).compute()
def get_natl_slice(path, **slice_args):
with xr.open_dataset(path) as ds:
return (
ds.pipe(lambda ds: ds.assign(time=ds.time.assign_attrs({'units': 'days since 2012-10-01'})))
.pipe(xr.decode_cf)
.pipe(lambda ds: ds.sel(time=slice(slice_args.get('time_min', "2012-10-01"),
slice_args.get('time_max', "2013-10-01"))))
.pipe(lambda ds: ds.isel(y=(ds.lat > slice_args.get('lat_min', 0))))
.pipe(lambda ds: ds.isel(y=(ds.lat < slice_args.get('lat_max', 360))))
.pipe(lambda ds: ds.isel(x=(ds.lon < slice_args.get('lon_max', 360))))
.pipe(lambda ds: ds.isel(x=(ds.lon > slice_args.get('lon_min', 0))))
).compute()
if __name__ == '__main__':
slice_args = {
"time_min": "2013-08-23",
"time_max": "2013-09-03",
"lat_min": 30,
"lat_max": 40,
"lon_min": 295,
"lon_max": 305,
}
t0 = time()
batch = {
**{f'nadir_{name}': get_nadir_slice(f'data/zarr/nadir/{name}', **slice_args) for name in
['swot', 'en', 'tpn', 'g2', 'j1']},
'swot': get_swot_slice(f'data/zarr/swot', **slice_args),
'oi': get_oi_slice('data/raw/DUACS-OI_maps/ssh_model/ssh_sla_boost_NATL60_en_j1_tpn_g2.nc', **slice_args),
'natl': get_natl_slice('data/raw/NATL60_regular_grid/1_10/natl60CH_H.nc', **slice_args),
}
print(time() - t0)