-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpreprocess.py
318 lines (253 loc) · 10.3 KB
/
preprocess.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import sys
from pathlib import Path
import numpy as np
from typing import Optional, Union, List
sys.path.append("..")
from src.preprocess import (
VHIPreprocessor,
CHIRPSPreprocessor,
PlanetOSPreprocessor,
GLEAMPreprocessor,
S5Preprocessor,
ESACCIPreprocessor,
SRTMPreprocessor,
ERA5MonthlyMeanPreprocessor,
ERA5HourlyPreprocessor,
BokuNDVIPreprocessor,
KenyaASALMask,
ERA5LandPreprocessor,
ERA5LandMonthlyMeansPreprocessor,
)
from src.preprocess.admin_boundaries import KenyaAdminPreprocessor
from scripts.utils import get_data_path
def process_vci(subset_str: str = "kenya"):
data_path = get_data_path()
processor = VHIPreprocessor(get_data_path(), "VCI")
regrid_path = (
data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
processor.preprocess(
subset_str=subset_str, resample_time="M", upsampling=False, regrid=regrid_path
)
def process_precip_2018(subset_str: str = "kenya"):
data_path = get_data_path()
regrid_path = (
data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
processor = CHIRPSPreprocessor(data_path)
processor.preprocess(subset_str=subset_str, regrid=regrid_path, parallel=False)
def process_era5POS_2018(subset_str: str = "kenya"):
data_path = get_data_path()
regrid_path = (
data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
processor = PlanetOSPreprocessor(data_path)
processor.preprocess(
subset_str=subset_str,
regrid=regrid_path,
parallel=False,
resample_time="M",
upsampling=False,
)
def process_era5_land(
variables: Optional[Union[List, str]] = None,
subset_str: str = "kenya",
monmean: bool = True,
):
data_path = get_data_path()
# Check all the provided variables exist
if variables is None:
variables = [d.name for d in (data_path / "raw/reanalysis-era5-land").iterdir()]
assert (
variables != []
), f"Expecting to find some variables in: {(data_path / 'raw/reanalysis-era5-land')}"
else:
if isinstance(variables, str):
variables = [variables]
assert variables in [
d.name for d in (data_path / "raw/reanalysis-era5-land").iterdir()
], f"Expect to find {variables} in {(data_path / 'raw/reanalysis-era5-land')}"
else:
assert all(
np.isin(
variables,
[
d.name
for d in (data_path / "raw/reanalysis-era5-land").iterdir()
],
)
), f"Expected to find {variables}"
# regrid_path = data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
# assert regrid_path.exists(), f"{regrid_path} not available"
regrid_path = None
if monmean:
processor = ERA5LandMonthlyMeansPreprocessor(data_path)
else:
processor = ERA5LandPreprocessor(data_path)
for variable in variables:
processor.preprocess(
subset_str=subset_str,
regrid=None,
resample_time="M",
upsampling=False,
variable=variable,
)
def process_gleam(subset_str: str = "kenya"):
data_path = get_data_path()
regrid_path = (
data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
def process_gleam():
# if the working directory is alread ml_drought don't need ../data
if Path(".").absolute().as_posix().split("/")[-1] == "ml_drought":
data_path = Path("data")
else:
data_path = Path("../data")
regrid_path = (
data_path
/ "interim/reanalysis-era5-single-levels-monthly-means_preprocessed/data_kenya.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
processor = GLEAMPreprocessor(data_path)
processor.preprocess(
subset_str=subset_str, regrid=regrid_path, resample_time="M", upsampling=False
)
def process_seas5():
# if the working directory is alread ml_drought don't need ../data
if Path(".").absolute().as_posix().split("/")[-1] == "ml_drought":
data_path = Path("data")
else:
data_path = Path("../data")
regrid_path = (
data_path
/ "interim/reanalysis-era5-single-levels-monthly-means_preprocessed/data_kenya.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
datasets = [d.name for d in (data_path / "raw").iterdir() if "seasonal" in d.name]
for dataset in datasets:
variables = [v.name for v in (data_path / "raw" / dataset).glob("*")]
for variable in variables:
if variable == "total_precipitation":
processor = S5Preprocessor(data_path)
processor.preprocess(
subset_str="kenya",
regrid=regrid_path,
resample_time=None,
upsampling=False,
variable=variable,
)
def process_esa_cci_landcover():
if Path(".").absolute().as_posix().split("/")[-1] == "ml_drought":
data_path = Path("data")
else:
data_path = Path("../data")
regrid_path = (
data_path
/ "interim/reanalysis-era5-single-levels-monthly-means_preprocessed/data_kenya.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
processor = ESACCIPreprocessor(data_path)
processor.preprocess(subset_str=subset_str, regrid=regrid_path)
def preprocess_srtm(subset_str: str = "kenya"):
data_path = get_data_path()
regrid_path = (
data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
print(
"Warning: regridding with CDO using the VCI preprocessor data fails because"
"CDO reads the grid type as generic instead of latlon. This can be fixed "
"just by changing the grid type to latlon in the grid definition file."
)
processor = SRTMPreprocessor(data_path)
processor.preprocess(subset_str=subset_str, regrid=regrid_path)
def preprocess_kenya_boundaries(selection: str = "level_1"):
assert selection in [
f"level_{i}" for i in range(1, 6)
], f'selection must be one of {[f"level_{i}" for i in range(1,6)]}'
data_path = get_data_path()
regrid_path = data_path / "interim/chirps_preprocessed/data_kenya.nc"
assert regrid_path.exists(), f"{regrid_path} not available"
processor = KenyaAdminPreprocessor(data_path)
processor.preprocess(reference_nc_filepath=regrid_path, selection=selection)
def preprocess_asal_mask():
data_path = get_data_path()
regrid_path = data_path / "interim/chirps_preprocessed/data_kenya.nc"
assert regrid_path.exists(), f"{regrid_path} not available"
processor = KenyaASALMask(data_path)
processor.preprocess(reference_nc_filepath=regrid_path)
def preprocess_era5(subset_str: str = "kenya"):
data_path = get_data_path()
# regrid_path = data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
# assert regrid_path.exists(), f"{regrid_path} not available"
regrid_path = None
processor = ERA5MonthlyMeanPreprocessor(data_path)
processor.preprocess(subset_str=subset_str, regrid=regrid_path)
def preprocess_era5_hourly(subset_str: str = "kenya"):
data_path = get_data_path()
regrid_path = (
data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
processor = ERA5HourlyPreprocessor(data_path)
# W-MON is weekly each monday (the same as the NDVI data from Atzberger)
processor.preprocess(subset_str=subset_str, resample_time="W-MON")
# processor.merge_files(subset_str='W-MON')
def preprocess_boku_ndvi(subset_str: str = "kenya", regrid: bool = True):
data_path = get_data_path()
# downsample_first = whether to calculate VCI before or after time downsampling?
processor = BokuNDVIPreprocessor(data_path, downsample_first=False)
if regrid:
# regrid_path = (
# data_path / f"interim/reanalysis-era5-land_preprocessed/data_{subset_str}.nc"
# )
regrid_path = (
data_path / f"interim/reanalysis-era5-single-levels-monthly-means_preprocessed/data_{subset_str}.nc"
)
assert regrid_path.exists(), f"{regrid_path} not available"
else:
regrid_path = None
processor.preprocess(
subset_str=subset_str, resample_time="W-MON", regrid=regrid_path
)
def preprocess_s5_ouce():
if Path(".").absolute().as_posix().split("/")[-1] == "ml_drought":
data_path = Path("data")
else:
data_path = Path("../data")
variable = "total_precipitation"
daily_s5_dir = Path("/soge-home/data/model/seas5/1.0x1.0/daily")
s = S5Preprocessor(data_path, ouce_server=True)
s.preprocess(
variable=variable,
regrid=None,
resample_time=None,
**{"ouce_dir": daily_s5_dir, "infer": True},
)
if __name__ == "__main__":
subset_str = "kenya"
# preprocess_era5(subset_str=subset_str)
# process_era5_land(
# subset_str=subset_str,
# variables=[
# "volumetric_soil_water_layer_1",
# "potential_evaporation",
# ], # total_precipitation 2m_temperature evapotranspiration
# monmean=False,
# )
# process_vci(subset_str=subset_str)
# process_precip_2018(subset_str=subset_str)
# process_era5POS_2018(subset_str=subset_str)
# process_gleam(subset_str=subset_str)
# process_esa_cci_landcover(subset_str=subset_str)
# preprocess_srtm(subset_str=subset_str)
# preprocess_kenya_boundaries(selection="level_1")
# preprocess_kenya_boundaries(selection="level_2")
# preprocess_kenya_boundaries(selection="level_3")
# preprocess_era5_hourly(subset_str=subset_str)
preprocess_boku_ndvi(subset_str=subset_str)
# preprocess_asal_mask(subset_str=subset_str)