-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
386 lines (327 loc) · 11.5 KB
/
utils.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import numpy as np
from pandas import rolling_apply
from angles_geom import get_zenith_angle
from get_data import get_features
from read_metadata import read_satellite_name
from read_metadata import read_satellite_step
from read_netcdf import read_land_mask
from temperature_forecast import prepare_temperature_mask
from visualize import get_bbox
# THESE "typical inputs/outputs/etc" functions avoid to define the same values in every classes
def typical_input(seed=0):
sat_name = read_satellite_name()
if seed == 0:
if sat_name == "GOES16":
beginning = 13516 + 365 + 10 # +36
nb_days = 5
ending = beginning + nb_days - 1
latitude_beginning = 35.0 + 5
latitude_end = 40.0 + 5
longitude_beginning = -80.0
longitude_end = -75.0
elif sat_name == "H08":
beginning = 13525 + 180
nb_days = 3
ending = beginning + nb_days - 1
latitude_beginning = -10.0
latitude_end = -5.0
longitude_beginning = 110.0
longitude_end = 115.0
return (
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
)
else:
if sat_name == "GOES16":
beginning = 13516 + 365 + 10
nb_days = 6
ending = beginning + nb_days - 1
latitude_beginning = 35.0 + 1
latitude_end = 40.0 - 3
longitude_beginning = -115.0 + 35 + 1
longitude_end = -110.0 + 35 - 3
elif sat_name == "H08":
beginning = 13525 + 180
nb_days = 5
ending = beginning + nb_days - 1
latitude_beginning = 40.0 - 5
latitude_end = 45.0 - 5
longitude_beginning = 110.0 + 10 + 5
longitude_end = 115.0 + 10 + 5
return (
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
)
def typical_outputs(type_channels, output_level, seed=0):
(
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
) = typical_input(seed)
lats, lons = get_latitudes_longitudes(
latitude_beginning, latitude_end, longitude_beginning, longitude_end
)
return get_features(type_channels, lats, lons, beginning, ending, output_level)
def typical_angles(seed=0):
(
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
) = typical_input(seed)
lats, lons = get_latitudes_longitudes(
latitude_beginning, latitude_end, longitude_beginning, longitude_end
)
return get_zenith_angle(
get_times_utc(beginning, ending, read_satellite_step(), 1), lats, lons
)
def typical_land_mask(seed=0):
(
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
) = typical_input(seed)
lats, lons = get_latitudes_longitudes(
latitude_beginning, latitude_end, longitude_beginning, longitude_end
)
return read_land_mask(lats, lons)
def typical_temperatures_forecast(seed=0):
(
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
) = typical_input(seed)
lats, lons = get_latitudes_longitudes(
latitude_beginning, latitude_end, longitude_beginning, longitude_end
)
return prepare_temperature_mask(lats, lons, beginning, ending)
def typical_bbox(seed=0):
(
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
) = typical_input(seed)
return get_bbox(
latitude_beginning, latitude_end, longitude_beginning, longitude_end
)
def typical_time_step():
return read_satellite_step()
def array_to_one_label(arr, base=6):
arr = arr.flatten()
r = 0
for k, x in enumerate(arr):
r += x * (base**k)
return int(r)
def one_label_to_array(lab, shape, base=6):
(row, col) = shape
to_return = np.zeros(row * col, dtype=int)
k = to_return.size - 1
while k >= 0:
coef = lab / base**k
to_return[k] = coef
lab -= coef * (base**k)
k -= 1
return to_return.reshape(shape)
### compute rolling mean or median to smooth the albedo (for albedo-based cloud test) ###
def rounding_mean_list(list_1d, window):
cumsum = np.cumsum(np.insert(list_1d, 0, 0))
list_1d[window - 1 :] = (cumsum[window:] - cumsum[:-window]) / float(window)
return list_1d
def rounding_median_list(list_1d, window):
# not used practically because it is too resources-consuming
list_1d[window:] = rolling_apply(
list_1d, window=window, center=False, func=np.nanmedian
)[window:]
return list_1d
def apply_rolling_on_time(array, window=5, method="mean"):
"""
:param array:
:param window:
:param method:
:return:
"""
assert window % 2 == 1, "please give an uneven window width"
s = array.shape
assert len(s) in [1, 3], "dimension non valid"
assert method in ["mean", "median"], "pleas ask for an implemented method"
if method == "mean":
if len(s) == 1:
return rounding_mean_list(array, window)
if len(s) == 3:
lats, lons = s[1:3]
for lat in range(lats):
for lon in range(lons):
array[:, lat, lon] = rounding_mean_list(array[:, lat, lon], window)
else:
print("WARNING the median method is much slower than the mean")
if len(s) == 1:
return rounding_mean_list(array, window)
if len(s) == 3:
lats, lons = s[1:3]
for lat in range(lats):
for lon in range(lons):
array[:, lat, lon] = rounding_median_list(
array[:, lat, lon], window
)
return array
def looks_like_night(point, indexes_to_test):
# unused
for k in indexes_to_test:
if (point[k] + 1) != 0:
return False
return True
### utilities ###
def rc_to_latlon(r, c, size_tile=5):
if r >= 0 and c >= 0:
lat = 90 - size_tile * int(1 + r)
lon = -180 + size_tile * int(c)
return lat, lon
else:
raise AttributeError("rc not well formatted")
def latlon_to_rc(lat, lon, size_tile=5):
if lat % size_tile == 0:
lat += 1
if lon % size_tile == 0:
lon += 1
if -90 <= lat < 90 and -180 <= lon <= 175:
row = int(np.ceil((90.0 - 1.0 * lat) / size_tile))
col = int(np.ceil((180.0 + 1.0 * lon) / size_tile))
return row - 1, col - 1
else:
raise AttributeError("latlon not well formatted")
def get_latitudes_longitudes(
lat_start, lat_end, lon_start, lon_end, resolution=2.0 / 60
):
nb_lat = int((lat_end - lat_start) / resolution)
latitudes = np.linspace(lat_start, lat_end, nb_lat, endpoint=False)
nb_lon = int((lon_end - lon_start) / resolution)
longitudes = np.linspace(lon_start, lon_end, nb_lon, endpoint=False)
return latitudes, longitudes
def get_times_utc(dfb_beginning, dfb_ending, satellite_timestep, slot_step):
from datetime import datetime, timedelta
len_times = (
(1 + dfb_ending - dfb_beginning) * 60 * 24 / (satellite_timestep * slot_step)
)
origin_of_time = datetime(1980, 1, 1)
date_beginning = origin_of_time + timedelta(days=dfb_beginning)
times = [
date_beginning + timedelta(minutes=k * satellite_timestep * slot_step)
for k in range(len_times)
]
return times
def get_dfbs_slots(dfb_beginning, dfb_ending, satellite_timestep, slot_step):
dfbs = np.arange(dfb_beginning, dfb_ending + 1, step=1)
slots = np.arange(0, 60 * 24 / satellite_timestep, step=slot_step)
return dfbs, slots
def print_date_from_dfb(begin, ending):
from datetime import datetime, timedelta
d_beginning = datetime(1980, 1, 1) + timedelta(days=begin - 1, seconds=1)
d_ending = datetime(1980, 1, 1) + timedelta(days=ending + 1 - 1, seconds=-1)
print("Dates from ", str(d_beginning), " till ", str(d_ending))
return d_beginning, d_ending
def get_nb_slots_per_day(satellite_step, slot_step):
"""
:param satellite_step: the satellite characteristic time step between two slots (10 minutes for Himawari 8)
:param slot_step: the chosen sampling of slots. if slot_step = n, the sampled slots are s[0], s[n], s[2*n]...
:return: number of slots per day for this satellite and the chosen sampling step
"""
return int(24 * 60 / (satellite_step * slot_step))
def upper_divisor_slot_step(slot_step, nb_slots_per_day):
while (
nb_slots_per_day % slot_step != 0
): # increase slot step as long as its not a divisor of nb_slots_per_day
slot_step += 1
return slot_step
def normalize(array, mask=None, normalization="max", return_m_s=False):
# normalization: max, standard, 'reduced', 'gray-scale'
if normalization == "gray-scale":
if mask is None:
M = np.max(array)
m = np.min(array)
to_return = np.array(255 * (array - m) / (M - m), dtype=np.uint8), 0, 1
else:
M = np.max(array[~mask])
m = np.min(array[~mask])
to_return = np.zeros_like(array, dtype=np.uint8), 0, 1
to_return[0][~mask] = 255 * (array[~mask] - m) / (M - m)
elif normalization == "max":
if mask is None:
to_return = array / np.max(np.abs(array)), 0, 1
else:
to_return = array / np.max(array[~mask]), 0, 1
elif normalization == "centered":
if mask is None:
m = np.mean(array)
to_return = (array - m), m, 1
else:
m = np.mean(array[~mask])
array[~mask] = array[~mask] - m
to_return = array, m, 1
elif normalization == "reduced":
if mask is None:
s = np.sqrt(np.var(array))
to_return = array / s, 0, s
else:
s = np.sqrt(np.var(array[~mask]))
array[~mask] = array[~mask] / s
to_return = array, 0, s
elif normalization == "standard":
if mask is None:
m = np.mean(array)
s = np.sqrt(np.var(array))
to_return = (array - m) / s, m, s
else:
m = np.mean(array[~mask])
s = np.sqrt(np.var(array[~mask]))
array[~mask] = (array[~mask] - m) / s
to_return = array, m, s
else:
to_return = array, 0, 1
if return_m_s:
return to_return
else:
return to_return[0]
def get_centers(model, process):
# for gaussian mixture (not used now)
if process in ["gaussian", "bayesian"]:
return model.means_
elif process == "kmeans":
return model.cluster_centers_
else:
raise Exception("not implemented classifier")
def get_std(model, process, index):
# for gaussian mixture (not used now)
if process in ["gaussian", "bayesian"]:
return np.sqrt(model.covariances_[index, 0, 0])
elif process == "kmeans":
return 0
else:
raise Exception("not implemented classifier")
def save(path, to_be_saved):
from pickle import dump
dump(to_be_saved, open(path, "wb"))
def load(path):
from pickle import load
return load(open(path, "rb"))