-
Notifications
You must be signed in to change notification settings - Fork 0
/
angles_geom.py
450 lines (388 loc) · 15.7 KB
/
angles_geom.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
from math import radians
from time import time
import numpy as np
import sunpos
from scipy.ndimage.filters import gaussian_filter1d
from scipy.stats import pearsonr, linregress
from general_utils.latlon import bounding_box
from general_utils.satellite_geom import (
sat_asat_r_vect2,
sat_hsat_r_vect2,
sat_sun_angle_r,
sat_sun_mirror_angle_r,
)
from general_utils.solar_geom_v5 import UTC2LAT_r
from general_utils.solar_geom_v5 import declin_r, sunposition_r
from general_utils.solar_geom_v5_test import parseDateTime
from read_metadata import read_satellite_step, read_satellite_longitude
from utils import *
def get_cos_zen(times, latitudes, longitudes):
return sunpos.evaluate(
times, np.flip(latitudes, 0), longitudes, ndim=2, n_cpus=2
).cosz
def get_zenith_angle(times, latitudes, longitudes):
return sunpos.evaluate(
times, np.flip(latitudes, 0), longitudes, ndim=2, n_cpus=2
).zenith
# def
def apply_gaussian_persistence(
persistence_array_1d, persistence_mask_1d, persistence_sigma, persistence_scope
):
"""
:param persistence_array_1d:
:param persistence_mask_1d:
:param persistence_sigma: standard deviation of time-expanding gaussian. persistance_sigma=0. => no expanding
:param persistence_scope: number of slots where applying persistence
:return: array of float between 0. and 1.
"""
persistence_sigma = float(persistence_sigma)
trunc = persistence_scope / persistence_sigma
return normalize(
gaussian_filter1d(
persistence_array_1d[~persistence_mask_1d],
sigma=persistence_sigma,
axis=0,
truncate=trunc,
),
normalization="max",
)
def look_like_cos_zen_1d(variable, cos_zen, under_bound, upper_bound, mask=None):
"""
:param variable: 1-d array representing the variable to test
:param cos_zen: 1-d array with cos of zenith angle
:param mask: mask associated with the variable
:param under_bound: hyper-parameter between -1 and 1 (eg: -1. or 0.93)
:param upper_bound: hyper-parameter between -1 and 1 (eg: -0.89 or 1.)
:return: integer 0-1
"""
if mask is None:
r, p = pearsonr(variable, cos_zen)
else:
# r, p = pearsonr(variable[~mask], cos_zen[~mask])
slope, intercept = linregress(cos_zen[~mask], variable[~mask])[0:2]
if upper_bound >= slope >= under_bound:
return 1
else:
return 0
def get_likelihood_variable_cos_zen(
variable,
cos_zen,
under_bound,
upper_bound,
nb_slots_per_day,
nb_slices_per_day=1,
mask=None,
persistence_sigma=0.0,
):
"""
:param variable: 1-d or 3-d array representing the variable to test
:param cos_zen: 1-d or 3-d array with cos of zenith angle
:param tolerance: hyper-parameter between -1 and 1 (eg: 0.93)
:param nb_slots_per_day
:param nb_slices_per_day
:param mask: mask associated with the variable
:param persistence_sigma: standard deviation of time-expanding gaussian. persistance_sigma=0. => no expanding
:return: matrix of likelihood (same shape as variable and cos_zen arrays)
"""
if len(variable.shape) == 1 and variable.shape == cos_zen.shape:
return look_like_cos_zen_1d(variable, cos_zen, under_bound, upper_bound, mask)
elif len(variable.shape) == 3 and variable.shape == cos_zen.shape:
to_return = np.zeros_like(cos_zen)
print("begin recognizing pattern")
t_begin_reco = time()
# https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.pearsonr.html
# computing of correlation need enough temporal information. If we have data on a too small window, ignore it
minimal_nb_unmasked_slots = 12
(nb_slots, nb_latitudes, nb_longitudes) = np.shape(variable)
nb_slots_per_step = int(nb_slots_per_day / nb_slices_per_day)
nb_steps = (
int(np.ceil(nb_slots / nb_slots_per_step)) + 1
) # +1 because first slot is not the darkest slot for every point
map_first_midnight = get_map_next_midnight(
cos_zen, nb_slots_per_day, current_midnight=0
)
persistence = persistence_sigma > 0
if persistence:
persistence_array = np.zeros(
(nb_steps, nb_latitudes, nb_longitudes), dtype=float
)
# complete persistence array
for lat in range(nb_latitudes):
for lon in range(nb_longitudes):
slot_beginning_slice = 0
slot_ending_slice = map_first_midnight[lat, lon] % nb_slots_per_step
step = 0
persistence_mask_1d = np.ones(nb_steps, dtype=bool)
while slot_beginning_slice < nb_slots:
slice_variable = variable[
slot_beginning_slice:slot_ending_slice, lat, lon
]
slice_cos_zen = cos_zen[
slot_beginning_slice:slot_ending_slice, lat, lon
]
slice_mask = mask[slot_beginning_slice:slot_ending_slice, lat, lon]
if slice_variable[~slice_mask].size > minimal_nb_unmasked_slots:
if persistence:
persistence_mask_1d[step] = False
persistence_array[step, lat, lon] = look_like_cos_zen_1d(
slice_variable,
slice_cos_zen,
under_bound,
upper_bound,
slice_mask,
)
else:
to_return[slot_beginning_slice:slot_ending_slice, lat, lon][
~slice_mask
] = look_like_cos_zen_1d(
slice_variable,
slice_cos_zen,
under_bound,
upper_bound,
slice_mask,
)
step += 1
slot_beginning_slice = slot_ending_slice
slot_ending_slice += nb_slots_per_step
if persistence:
if not np.all(persistence_mask_1d):
persistence_array[:, lat, lon][
~persistence_mask_1d
] = apply_gaussian_persistence(
persistence_array[:, lat, lon],
persistence_mask_1d,
persistence_sigma,
persistence_scope=nb_slices_per_day,
)
# TODO: add some spatial information
step = 0
slot_beginning_slice = 0
slot_ending_slice = map_first_midnight[lat, lon] % nb_slots_per_step
if not np.all(persistence_array[:, lat, lon] == 0):
while slot_beginning_slice < nb_slots:
slice_mask = mask[
slot_beginning_slice:slot_ending_slice, lat, lon
]
to_return[slot_beginning_slice:slot_ending_slice, lat, lon][
~slice_mask
] = persistence_array[step, lat, lon]
step += 1
slot_beginning_slice = slot_ending_slice
slot_ending_slice += nb_slots_per_step
print("time recognition", time() - t_begin_reco)
return to_return
else:
raise Exception("You have to give two 1-d or 3-d arrays of the same shape")
def get_next_midnight(mu_point, nb_slots_per_day, current_slot=0):
return np.maximum(
0,
current_slot
+ nb_slots_per_day
- 3
+ np.argmin(
mu_point[
current_slot
+ nb_slots_per_day
- 3 : current_slot
+ nb_slots_per_day
+ 3
]
),
)
def get_next_midday(mu_point, nb_slots_per_day, current_midday=0):
return np.maximum(
0,
current_midday
+ nb_slots_per_day
- 3
+ np.argmax(
mu_point[
current_midday
+ nb_slots_per_day
- 3 : current_midday
+ nb_slots_per_day
+ 3
]
),
)
def get_map_next_midnight(mu, nb_slots_per_day, current_midnight=0):
if current_midnight == 0: # means it is first iteration
return np.argmin(mu[0:nb_slots_per_day], axis=0)
else:
return np.maximum(
0,
current_midnight
+ nb_slots_per_day
- 3
+ np.argmin(
mu[
current_midnight
+ nb_slots_per_day
- 3 : current_midnight
+ nb_slots_per_day
+ 3
],
axis=0,
),
)
def get_map_next_midday(mu, nb_slots_per_day, current_slot=0):
return (
get_map_next_midnight(mu, nb_slots_per_day, current_slot) + nb_slots_per_day / 2
) % nb_slots_per_day
def get_map_next_sunrise(mu, nb_slots_per_day, current_dawn=0):
# derivative of cos-zen angle = 1
if current_dawn == 0: # means it is first iteration
derivative = mu[0:nb_slots_per_day] - np.roll(mu[0:nb_slots_per_day], shift=-1)
return np.argmax(derivative, axis=0)
else:
derivative = mu[
current_dawn + nb_slots_per_day - 3 : current_dawn + nb_slots_per_day + 3
] - np.roll(
mu[
current_dawn
+ nb_slots_per_day
- 3 : current_dawn
+ nb_slots_per_day
+ 3
],
shift=-1,
)
return np.maximum(
0, current_dawn + nb_slots_per_day - 3 + np.argmax(derivative, axis=0)
)
# should be useless, since these slots are supposed to be regularly distant from nb_slots_per_day
def get_map_next_sunset(mu, nb_slots_per_day, current_dawn=0):
# derivative of cos-zen angle = -1
if current_dawn == 0: # means it is first iteration
derivative = mu[0:nb_slots_per_day] - np.roll(mu[0:nb_slots_per_day], shift=-1)
return np.argmin(derivative, axis=0)
else:
derivative = mu[
current_dawn + nb_slots_per_day - 3 : current_dawn + nb_slots_per_day + 3
] - np.roll(
mu[
current_dawn
+ nb_slots_per_day
- 3 : current_dawn
+ nb_slots_per_day
+ 3
],
shift=-1,
)
return np.maximum(
0, current_dawn + nb_slots_per_day - 3 + np.argmin(derivative, axis=0)
)
def get_map_all_midnight_slots(mu, nb_slots_per_day):
nb_slots, nb_latitudes, nb_longitudes = np.shape(mu)
nb_days = nb_slots / nb_slots_per_day
current_slots = np.zeros((nb_days, nb_latitudes, nb_longitudes), dtype=int)
current_slots[0, :, :] = get_map_next_midnight(mu, nb_slots_per_day)
for lat in range(nb_latitudes):
for lon in range(nb_longitudes):
for day in range(1, nb_days):
current_slots[day, lat, lon] = get_next_midnight(
mu, nb_slots_per_day, current_slots[day - 1, lat, lon]
)
return current_slots
def compute_angle_glint(zen, sat, scat):
return 2 * np.cos(zen) * np.cos(sat) - np.cos(scat)
def simplified_declination_angle(day, year=None, method="tomas"):
# from THE EUROPEAN SOLAR RADIATION ATLASVol. 1: Fundamentals and maps
# can be used with scalars or arrays
assert method in ["julian", "tomas"], "method non implemented"
if method == "julian":
julian = day / 365.0 * 360
sin_delta = 0.3978 * np.sin(julian - 80.2 + 1.92 * np.sin(julian - 2.8))
return np.arcsin(sin_delta)
elif method == "tomas":
from general_utils.solar_geom_v5 import declin_r
return declin_r(year, day, 0)
def solar_altitude_angle(times, lats, lons, declin):
# can be used with scalars or arrays
lons, lats = np.asarray(lons), np.asarray(lats)
sin_altitude = np.empty((len(times), len(lats), len(lons)))
for t_ind, t in enumerate(times):
year, day, time = parseDateTime(t.isoformat())
for lon_ind, lon in enumerate(lons):
time_apparent = UTC2LAT_r(time, day, lon)
omega = 15 * (time_apparent - 12) # angle in degrees
for lat_ind, lat in enumerate(lons):
sin_altitude[t_ind, lat_ind, lon_ind] = np.sin(lat) * np.sin(
declin
) + np.cos(lat) * np.cos(declin) * np.cos(omega)
return np.arcsin(sin_altitude)
def solar_azimuth_angle():
cos_alpha = np.sin(lats) * np.sin(altitude) - np.sin(declin) / (
np.cos(lats) * np.cos(altitude)
)
sin_alpha = np.cos(declin) * np.sin(omega) / np.cos(altitude)
alpha = np.empty_like(cos_alpha)
alpha[sin_alpha < 0] = -np.arccos(cos_alpha)
alpha[sin_alpha >= 0] = np.arccos(cos_alpha)
return alpha
def calculate_satellite_geometry_point(lon_r, lat_r, asun_r, hsun_r, satlon_r):
"""
inputs in radians
:param satlon_r # satellite position longitude - sub-satellite point
:param asun_r # sun azimuth A0
:param hsun_r # sun elevation h0
:param lat_r # latitude
:param lon_r # longitude
:return:
"""
# satellite geometry
Asat_r = sat_asat_r_vect2(lon_r, lat_r, satlon_r) # [:, :, 0, 0]
Hsat_r = sat_hsat_r_vect2(lon_r, lat_r, satlon_r) # [:, :, 0, 0]
# sun - ground - satellite geometry
sun_sat_angle_r = sat_sun_angle_r(asun_r, hsun_r, Asat_r, Hsat_r)
# specular angle - give idea of possible mirroring effect
sun_sat_mirror_r = sat_sun_mirror_angle_r(asun_r, hsun_r, Asat_r, Hsat_r)
print(sun_sat_mirror_r)
return sun_sat_angle_r, sun_sat_mirror_r
if __name__ == "__main__":
beginning = 13517 + 60
nb_days = 2
ending = beginning + nb_days - 1
latitude_beginning = 40.0
latitude_ending = 45.0
longitude_beginning = 125.0
longitude_ending = 130.0
latitudes, longitudes = get_latitudes_longitudes(
latitude_beginning, latitude_ending, longitude_beginning, longitude_ending
)
times = get_times_utc(beginning, ending, read_satellite_step(), 1)
nb_slots_per_day = get_nb_slots_per_day(read_satellite_step(), 1)
res = 1 / 33.0
w = len(longitudes)
h = len(latitudes)
bb = bounding_box(
xmin=longitude_beginning,
xmax=longitude_ending,
ymin=latitude_beginning,
ymax=latitude_ending,
resolution=res,
width=w,
height=h,
)
coef = 2 * np.pi / 360.0
# for lon in lons_1d:
asun_r = np.empty((nb_days, nb_slots_per_day, h, w))
hsun_r = np.empty((nb_days, nb_slots_per_day, h, w))
for t_ind, t in enumerate(times):
date = t.isoformat()
year, day, time = parseDateTime(date)
dfb_index = int(t_ind / nb_slots_per_day)
slot_index = t_ind - nb_slots_per_day * dfb_index
for lon_ind, lon in enumerate(longitudes):
decl = declin_r(year, day, radians(lon))
time_LAT = UTC2LAT_r(time, day, radians(lon))
for lat_ind, lat in enumerate(latitudes):
(
asun_r[dfb_index, slot_index, lat_ind, lon_ind],
hsun_r[dfb_index, slot_index, lat_ind, lon_ind],
) = sunposition_r(decl, radians(lat), time_LAT)
satlon_r = np.empty(shape=(nb_days, nb_slots_per_day))
satlon_r[0, 0] = read_satellite_longitude()
lats_2d = bb.latitudes(array2d=True)
lons_2d = bb.longitudes(array2d=True)
calculate_satellite_geometry_point(lons_2d, lats_2d, asun_r, hsun_r, satlon_r)