-
Notifications
You must be signed in to change notification settings - Fork 3
/
prediction_funcs.py
executable file
·293 lines (223 loc) · 8.75 KB
/
prediction_funcs.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
"""
This module contains functions to do neural network CMA/CPH/CTP/CTT/MLAY
predictions based on SEVIRI measurements.
Author: Daniel Philipp (DWD, 2022)
"""
import time
import os
import logging
import helperfuncs as hf
import seviri_ml_core
fmt = '%(levelname)s : %(filename)s : %(message)s'
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO,
format=fmt)
# set backend name from environment variable
backend = hf.get_backend_name(os.environ.get('SEVIRI_ML_BACKEND'))
# read nn_driver.txt
opts = hf.get_driver_opts(backend)
# configure theano compilation
if backend == 'THEANO':
Tconfig = hf.ConfigTheano(opts)
def predict_cma(vis006, vis008, nir016, ir039, ir062, ir073, ir087,
ir108, ir120, ir134, lsm, skt, solzen=None, satzen=None,
undo_true_refl=False, correct_vis_cal_nasa_to_impf=0,
make_binary=True, make_uncertainty=True):
""" Run cloud mask (CMA) prediction. """
logger.info('---------- RUNNING CMA ANN ----------')
v = 'CMA'
# put data into structure
data = seviri_ml_core.InputData(
vis006, vis008, nir016, ir039, ir062, ir073,
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen
)
cldmask = None
results = []
# create a processor instance
proc = seviri_ml_core.ProcessorCMA(
data, undo_true_refl, correct_vis_cal_nasa_to_impf,
cldmask, v, opts
)
# run prediction
start = time.time()
prediction = proc.get_prediction()
logger.info("Time for prediction CMA: {:.3f}".format(time.time() - start))
results.append(prediction)
if make_binary:
# apply threshold
binary = proc.get_binary()
results.append(binary)
if make_uncertainty:
# run uncertainty calculation
uncertainty = proc.get_uncertainty()
results.append(uncertainty)
return results
def predict_cph(vis006, vis008, nir016, ir039, ir062, ir073, ir087,
ir108, ir120, ir134, lsm, skt, solzen=None, satzen=None,
undo_true_refl=False, correct_vis_cal_nasa_to_impf=0,
cldmask=None, make_binary=True, make_uncertainty=True):
""" Run cloud phase (CPH) prediction. """
logger.info('---------- RUNNING CPH ANN ----------')
v = 'CPH'
# put data into structure
data = seviri_ml_core.InputData(
vis006, vis008, nir016, ir039, ir062, ir073,
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen
)
# create a processor instance
proc = seviri_ml_core.ProcessorCPH(
data, undo_true_refl, correct_vis_cal_nasa_to_impf,
cldmask, v, opts
)
results = []
# run prediction
start = time.time()
prediction = proc.get_prediction()
logger.info("Time for prediction CPH: {:.3f}".format(time.time() - start))
results.append(prediction)
if make_binary:
# apply threshold
binary = proc.get_binary()
results.append(binary)
if make_uncertainty:
# run uncertainty calculation
uncertainty = proc.get_uncertainty()
results.append(uncertainty)
return results
def predict_ctp(vis006, vis008, nir016, ir039, ir062, ir073, ir087,
ir108, ir120, ir134, lsm, skt, solzen=None, satzen=None,
undo_true_refl=False, correct_vis_cal_nasa_to_impf=0,
cldmask=None, make_uncertainty=True):
""" Run cloud top pressure (CTP) prediction. """
logger.info('---------- RUNNING CTP ANN ----------')
v = 'CTP'
# put data into structure
data = seviri_ml_core.InputData(
vis006, vis008, nir016, ir039, ir062, ir073,
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen
)
# create a processor instance
proc = seviri_ml_core.ProcessorCTP(
data, undo_true_refl, correct_vis_cal_nasa_to_impf,
cldmask, v, opts
)
results = []
# run prediction
start = time.time()
prediction = proc.get_prediction()
logger.info("Time for prediction CTP: {:.3f}".format(time.time() - start))
results.append(prediction)
if make_uncertainty:
# run uncertainty calculation
start = time.time()
uncertainty = proc.get_uncertainty()
logger.info('Time for calculating uncertainty: '
'{:.3f}'.format(time.time() - start))
results.append(uncertainty)
return results
def predict_ctt(vis006, vis008, nir016, ir039, ir062, ir073, ir087,
ir108, ir120, ir134, lsm, skt, solzen=None, satzen=None,
undo_true_refl=False, correct_vis_cal_nasa_to_impf=0,
cldmask=None, make_uncertainty=True):
""" Run cloud top temperature (CTT) prediction. """
logger.info('---------- RUNNING CTT ANN ----------')
v = 'CTT'
# put data into structure
data = seviri_ml_core.InputData(
vis006, vis008, nir016, ir039, ir062, ir073,
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen
)
# create a processor instance
proc = seviri_ml_core.ProcessorCTT(
data, undo_true_refl, correct_vis_cal_nasa_to_impf,
cldmask, v, opts
)
results = []
# run prediction
start = time.time()
prediction = proc.get_prediction()
logger.info("Time for prediction CTT: {:.3f}".format(time.time() - start))
results.append(prediction)
if make_uncertainty:
# run uncertainty calculation
start = time.time()
uncertainty = proc.get_uncertainty()
logger.info('Time for calculating uncertainty: '
'{:.3f}'.format(time.time() - start))
results.append(uncertainty)
return results
def predict_cbh(ir108, ir120, ir134, solzen=None, satzen=None,
cldmask=None, make_uncertainty=True):
"""
Main function that calls the neural network for CTT prediction.
Input:
- ir108 (2d numpy array): SEVIRI IR 10.8 um (Ch 9)
- ir120 (2d numpy array): SEVIRI IR 12.0 um (Ch 10)
- ir134 (2d numpy array): SEVIRI IR 13.4 um (Ch 11)
- satzen (2d numpy array): Satellite zenith angle
- solzen (2d numpy array): Solar zenith angle
- cldmask (2d numpy array or None): External cloud mask.
- make_uncertainty (bool): Generate uncertainties using DQR
Return:
- prediction (list): NN output list
[CBH, CBH 1-sigma uncertainty]
"""
logger.info('---------- RUNNING CBH ANN ----------')
v = 'CBH'
# put data into structure
data = seviri_ml_core.InputData(
ir108=ir108, ir120=ir120, ir134=ir134,
solzen=solzen, satzen=satzen)
undo_true_refl = False
correct_vis_cal_nasa_to_impf = False
# create a processor instance
proc = seviri_ml_core.ProcessorCBH(
data, undo_true_refl, correct_vis_cal_nasa_to_impf,
cldmask, v, opts
)
results = []
# run prediction
start = time.time()
prediction = proc.get_prediction()
logger.info("Time for prediction CBH: {:.3f}".format(time.time() - start))
results.append(prediction)
if make_uncertainty:
# run uncertainty calculation
start = time.time()
uncertainty = proc.get_uncertainty()
logger.info('Time for calculating uncertainty: '
'{:.3f}'.format(time.time() - start))
results.append(uncertainty)
return results
def predict_mlay(vis006, vis008, nir016, ir039, ir062, ir073, ir087,
ir108, ir120, ir134, lsm, skt, solzen=None, satzen=None,
undo_true_refl=False, correct_vis_cal_nasa_to_impf=0,
cldmask=None, make_binary=True, make_uncertainty=True):
""" Run multilayer flag (MLAY) prediction. """
logger.info('---------- RUNNING MLAY ANN ----------')
v = 'MLAY'
# put data into structure
data = seviri_ml_core.InputData(
vis006, vis008, nir016, ir039, ir062, ir073,
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen
)
# create a processor instance
proc = seviri_ml_core.ProcessorMLAY(
data, undo_true_refl, correct_vis_cal_nasa_to_impf,
cldmask, v, opts
)
results = []
# run prediction
start = time.time()
prediction = proc.get_prediction()
logger.info("Time for prediction MLAY: {:.3f}".format(time.time() - start))
results.append(prediction)
if make_binary:
# apply threshold
binary = proc.get_binary()
results.append(binary)
if make_uncertainty:
# run uncertainty calculation
uncertainty = proc.get_uncertainty()
results.append(uncertainty)
return results