-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpreprocess.py
More file actions
156 lines (120 loc) · 5.28 KB
/
preprocess.py
File metadata and controls
156 lines (120 loc) · 5.28 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
from pathlib import Path
import sys
sys.path.append('..')
from src.preprocess import (VHIPreprocessor, CHIRPSPreprocesser,
PlanetOSPreprocessor, GLEAMPreprocessor,
ESACCIPreprocessor, SRTMPreprocessor,
ERA5MonthlyMeanPreprocessor)
from src.preprocess.admin_boundaries import KenyaAdminPreprocessor
def process_vci_2018():
# 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')
processor = VHIPreprocessor(data_path, 'VCI')
processor.preprocess(subset_str='kenya',
resample_time='M', upsampling=False)
def process_precip_2018():
# 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/VCI_preprocessed/data_kenya.nc'
assert regrid_path.exists(), f'{regrid_path} not available'
processor = CHIRPSPreprocesser(data_path)
processor.preprocess(subset_str='kenya',
regrid=regrid_path,
parallel=False)
def process_era5POS_2018():
# 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/VCI_preprocessed/data_kenya.nc'
assert regrid_path.exists(), f'{regrid_path} not available'
processor = PlanetOSPreprocessor(data_path)
processor.preprocess(subset_str='kenya', regrid=regrid_path,
parallel=False, resample_time='M', upsampling=False)
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/VCI_preprocessed/data_kenya.nc'
assert regrid_path.exists(), f'{regrid_path} not available'
processor = GLEAMPreprocessor(data_path)
processor.preprocess(subset_str='kenya', regrid=regrid_path,
resample_time='M', upsampling=False)
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/VCI_preprocessed/data_kenya.nc'
assert regrid_path.exists(), f'{regrid_path} not available'
processor = ESACCIPreprocessor(data_path)
processor.preprocess(subset_str='kenya', regrid=regrid_path)
def preprocess_srtm():
if Path('.').absolute().as_posix().split('/')[-1] == 'ml_drought':
data_path = Path('data')
else:
data_path = Path('../data')
regrid_path = data_path / 'interim/VCI_preprocessed/data_kenya.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='kenya', 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)]}'
if Path('.').absolute().as_posix().split('/')[-1] == 'ml_drought':
data_path = Path('data')
else:
data_path = Path('../data')
regrid_path = data_path / 'interim/chirps_preprocessed/chirps_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_era5():
if Path('.').absolute().as_posix().split('/')[-1] == 'ml_drought':
data_path = Path('data')
else:
data_path = Path('../data')
regrid_path = data_path / 'interim/VCI_preprocessed/data_kenya.nc'
assert regrid_path.exists(), f'{regrid_path} not available'
processor = ERA5MonthlyMeanPreprocessor(data_path)
processor.preprocess(subset_str='kenya', regrid=regrid_path)
def preprocess_icdc():
from src.preprocess.icdc import (
ModisNDVIPreprocessor,
ModisLSTPreprocessor,
)
if Path('.').absolute().as_posix().split('/')[-1] == 'ml_drought':
data_path = Path('data')
else:
data_path = Path('../data')
processor = ModisNDVIPreprocessor(data_path)
processor.preprocess(
subset_str='ethiopia_safe',
)
processor = ModisLSTPreprocessor(data_path)
processor.preprocess(
subset_str='ethiopia_safe'
)
if __name__ == '__main__':
# process_vci_2018()
# process_precip_2018()
# process_era5POS_2018()
# process_gleam()
# process_esa_cci_landcover()
# preprocess_srtm()
# preprocess_era5()
preprocess_icdc()