-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_qw_multifreq.py
executable file
·403 lines (316 loc) · 15.9 KB
/
prepare_qw_multifreq.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
import numpy as np
import os
import dill
import sys
import argparse
import time
from termcolor import colored
sys.path.append('../atmrad')
from gpu.atmosphere import Atmosphere, avg
from gpu.weight_funcs import krho
from cpu.core.static.weight_funcs import kw
import gpu.core.math as math
# radiometry_angle = 51.*np.pi/180
radiometry_angle = 0.
frequencies = np.linspace(18.0, 27.2, 47, endpoint=True)
T_cosmic = 2.72548
def createArgParser():
_p = argparse.ArgumentParser()
_p.add_argument('-P', '--path_to_dump_dir', default='./dump/summer/2019/',
help='Where to get data from?')
_p.add_argument('-R', '--regularization', default=17.25,
help='Regularization coefficient')
_p.add_argument('--lm', action='store_true', default=False,
help='Compute QRETRLM, WRETRLM')
_p.add_argument('--ms', action='store_true', default=False,
help='Compute QRETRMS, WRETRMS')
_p.add_argument('--tcl', default=0, help="Average effective cloud temperature, Cels.")
_p.add_argument('--qretrlmname', default='qretrlm_multifreq')
_p.add_argument('--wretrlmname', default='wretrlm_multifreq')
_p.add_argument('--qretrmsname', default='qretrms_multifreq')
_p.add_argument('--wretrmsname', default='wretrms_multifreq')
return _p
def dump(_obj, _name, _dump_options):
if not os.path.exists('dump'):
os.makedirs('dump')
print(colored('{}...'.format(_name), 'blue'))
if 'numpy' in _dump_options:
np.save(os.path.join('dump', '{}.npy'.format(_name)), _obj)
print(colored('...numpy', 'green'))
if 'dill' in _dump_options:
with open(os.path.join('dump', '{}.dump'.format(_name)), 'wb') as _dump:
dill.dump(_obj, _dump, recurse=True)
print(colored('...dill', 'green'))
if __name__ == '__main__':
parser = createArgParser()
ns = parser.parse_args(sys.argv[1:])
dump_dir = ns.path_to_dump_dir
tcl = float(ns.tcl)
r = float(ns.regularization)
if not r:
print(colored('No regularization!\n', 'yellow'))
else:
print(colored('Regularization coeff.: {:.3f}\n'.format(r), 'yellow'))
print(colored('\nDUMP dir is {}'.format(dump_dir), 'blue'))
TS = np.load(os.path.join(dump_dir, 'ts.npy'))
# DT = np.load(os.path.join(dump_dir, 'dt.npy'))
BT = np.load(os.path.join(dump_dir, 'btc1.npy'))
LM = np.load(os.path.join(dump_dir, 'lm.npy'))
MS = np.load(os.path.join(dump_dir, 'ms.npy'))
# ID = np.load(os.path.join(dump_dir, 'id.npy'))
print('\nРазмерности и типы входных данных')
print(
'TS ', TS.shape, TS.dtype, '\n',
# 'DT ', DT.shape, DT.dtype, '\n',
# 'ID ', ID.shape, ID.dtype, '\n',
'BT ', BT.shape, BT.dtype, '\n',
'LM ', LM.shape, LM.dtype, '\n',
'MS ', MS.shape, MS.dtype, '\n',
)
batch_size = 0
while batch_size < 1:
print('\nВведите размер одного батча')
try:
batch_size = int(input())
except Exception as e:
print(e)
continue
if batch_size < 1:
print('Размер батча не может быть меньше 1')
continue
print()
with open('Dolgoprudnyj_gridded.dump', 'rb') as _dump:
meteosonde_data = dill.load(_dump)
# channels = {}
# for i, f in enumerate(frequencies):
# channels[np.round(f, decimals=1)] = i
print("Средняя эффективная температура облака t_cl = {}".format(tcl))
K_W_STD = np.asarray([kw(nu, t=tcl) for nu in frequencies])
_, _, _, alt = meteosonde_data[tuple(MS[0])]
QSTD, QREAL = np.asarray([]), np.asarray([])
QRETRLM, WRETRLM = np.asarray([]), np.asarray([])
QRETRMS, WRETRMS = np.asarray([]), np.asarray([])
progress = 0.
n_batches = len(TS) // batch_size
for n in range(n_batches):
indexes = list(range(len(TS)))[n * batch_size: n * batch_size + batch_size]
start = time.time()
T0, P0, rho0 = [], [], []
T, P, rho_rel = [], [], []
for i in indexes:
T0_, P0_, rho0_, *_ = LM[i]
T_, P_, rho_rel_, _ = meteosonde_data[tuple(MS[i])]
T0.append(T0_)
P0.append(P0_)
rho0.append(rho0_)
T.append(T_)
P.append(P_)
rho_rel.append(rho_rel_)
T0, P0, rho0 = np.asarray(T0), np.asarray(P0), np.asarray(rho0) # (batch_size, )
stdAtm = Atmosphere.Standard(T0=T0, P0=P0, rho0=rho0, H=15, dh=15. / 1000)
# данные уже загружены на GPU
del T0
del P0
del rho0
# Z x batch_size -> batch_size x 1 x Z
stdAtm.temperature = math.move_axis(math.as_tensor([math.transpose(stdAtm.temperature)]), 0, 1)
stdAtm.pressure = math.move_axis(math.as_tensor([math.transpose(stdAtm.pressure)]), 0, 1)
stdAtm.absolute_humidity = math.move_axis(math.as_tensor([math.transpose(stdAtm.absolute_humidity)]), 0, 1)
stdAtm.liquid_water = math.move_axis(math.as_tensor([math.transpose(stdAtm.liquid_water)]), 0, 1)
QSTD = np.hstack((QSTD, np.asarray(stdAtm.Q)[:, 0]))
# batch_size x Z -> batch_size x 1 x Z
T, P, rho_rel = np.moveaxis(np.asarray([T]), 0, 1), np.moveaxis(np.asarray([P]), 0, 1), \
np.moveaxis(np.asarray([rho_rel]), 0, 1)
realAtm = Atmosphere(T, P, RelativeHumidity=rho_rel, altitudes=alt)
del T
del P
del rho_rel
QREAL = np.hstack((QREAL, np.asarray(realAtm.Q)[:, 0]))
if not r:
if int(ns.lm):
# MULTI FREQ LOCALMETEO
brt = math.as_tensor([[BT[i, :]] for i in indexes]) # под углом | (batch_size, 1, 47)
t_avg_down_std = math.as_tensor(
[avg.downward.T(stdAtm, nu, radiometry_angle) for nu in frequencies]
) # под углом | (47, batch_size, 1)
t_avg_down_std = math.move_axis(t_avg_down_std, 0, -1) # (batch_size, 1, 47)
tau_o_std = math.as_tensor(
[stdAtm.opacity.oxygen(nu) for nu in frequencies]
) # в зените | (47, batch_size, 1)
tau_o_std = math.move_axis(tau_o_std, 0, -1) # (batch_size, 1, 47)
tau_e_std = math.log(
(t_avg_down_std - T_cosmic) / (t_avg_down_std - brt)
) # (batch_size, 1, 47) - в зените
k_rho_std = math.as_tensor(
[krho(stdAtm, nu) for nu in frequencies]
) # в зените
k_rho_std = math.move_axis(k_rho_std, 0, -1) # (batch_size, 1, 47)
k_w_std = math.move_axis(math.as_tensor([[K_W_STD[:]] * len(indexes)]), 0, 1) # (batch_size, 1, 47)
M_std = math.as_tensor([k_rho_std, k_w_std]) # (2, batch_size, 1, 47)
M_std = math.move_axis(M_std, 0, -1) # (batch_size, 1, 47, 2)
right_std = math.move_axis(math.as_tensor([tau_e_std - tau_o_std]), 0, -1) # (batch_size, 1, 47, 1)
sol_std = math.linalg_lstsq(M_std, right_std) # (batch_size, 1, 2, 1)
QRETRLM = np.hstack((QRETRLM, np.array(sol_std[:, 0, 0, 0])))
WRETRLM = np.hstack((WRETRLM, np.array(sol_std[:, 0, 1, 0])))
del brt
del t_avg_down_std
del tau_o_std
del tau_e_std
del k_rho_std
del k_w_std
del M_std
del right_std
del sol_std
if int(ns.ms):
# MULTI FREQ METEOSONDE
brt = math.as_tensor([[BT[i, :]] for i in indexes]) # под углом | (batch_size, 1, 47)
t_avg_down_real = math.as_tensor(
[avg.downward.T(realAtm, nu, radiometry_angle) for nu in frequencies]
) # под углом | (47, batch_size, 1)
t_avg_down_real = math.move_axis(t_avg_down_real, 0, -1) # (batch_size, 1, 47)
tau_o_real = math.as_tensor(
[realAtm.opacity.oxygen(nu) for nu in frequencies]
) # в зените | (47, batch_size, 1)
tau_o_real = math.move_axis(tau_o_real, 0, -1) # (batch_size, 1, 47)
tau_e_real = math.log(
(t_avg_down_real - T_cosmic) / (t_avg_down_real - brt)
) # (batch_size, 1, 47) - в зените
k_rho_real = math.as_tensor(
[krho(realAtm, nu) for nu in frequencies]
) # в зените
k_rho_real = math.move_axis(k_rho_real, 0, -1) # (batch_size, 1, 47)
k_w_std = math.move_axis(math.as_tensor([[K_W_STD[:]] * len(indexes)]), 0, 1) # (batch_size, 1, 47)
M_real = math.as_tensor([k_rho_real, k_w_std]) # (2, batch_size, 1, 47)
M_real = math.move_axis(M_real, 0, -1) # (batch_size, 1, 47, 2)
right_real = math.move_axis(math.as_tensor([tau_e_real - tau_o_real]), 0, -1) # (batch_size, 1, 47, 1)
sol_real = math.linalg_lstsq(M_real, right_real) # (batch_size, 1, 2, 1)
QRETRMS = np.hstack((QRETRMS, np.array(sol_real[:, 0, 0, 0])))
WRETRMS = np.hstack((WRETRMS, np.array(sol_real[:, 0, 1, 0])))
del brt
del t_avg_down_real
del tau_o_real
del tau_e_real
del k_rho_real
del k_w_std
del M_real
del right_real
del sol_real
else:
r = math.as_tensor(r)
if int(ns.lm):
# MULTI FREQ LOCALMETEO WITH REGULARIZATION
brt = math.as_tensor([[BT[i, :]] for i in indexes]) # под углом | (batch_size, 1, 47)
t_avg_down_std = math.as_tensor(
[avg.downward.T(stdAtm, nu, radiometry_angle) for nu in frequencies]
) # под углом | (47, batch_size, 1)
t_avg_down_std = math.move_axis(t_avg_down_std, 0, -1) # (batch_size, 1, 47)
tau_o_std = math.as_tensor(
[stdAtm.opacity.oxygen(nu) for nu in frequencies]
) # в зените | (47, batch_size, 1)
tau_o_std = math.move_axis(tau_o_std, 0, -1) # (batch_size, 1, 47)
tau_e_std = math.log(
(t_avg_down_std - T_cosmic) / (t_avg_down_std - brt)
) # (batch_size, 1, 47) - в зените
k_rho_std = math.as_tensor(
[krho(stdAtm, nu) for nu in frequencies]
) # в зените
k_rho_std = math.move_axis(k_rho_std, 0, -1) # (batch_size, 1, 47)
k_w_std = math.move_axis(math.as_tensor([[K_W_STD[:]] * len(indexes)]), 0, 1) # (batch_size, 1, 47)
a = math.sum_(tau_e_std * k_w_std, axis=-1) - math.sum_(tau_o_std * k_w_std, axis=-1) # (batch_size, 1)
b = math.sum_(k_rho_std * k_w_std, axis=-1) / math.sum_(k_rho_std * k_rho_std, axis=-1)
c = math.sum_(tau_e_std * k_rho_std, axis=-1) - math.sum_(tau_o_std * k_rho_std, axis=-1)
d = c / math.sum_(k_rho_std * k_rho_std, axis=-1) # (batch_size, 1)
p = math.sum_(k_rho_std * k_w_std, axis=-1) # (batch_size, 1)
s = math.sum_(k_w_std * k_w_std, axis=-1) # (batch_size, 1)
u = b * p - s # (batch_size, 1)
w = (u * math.lambertw(-math.exp((a - b * c) * r / u) * r * r / u) - a * r + b * c * r) / (r * u)
q = d - b * w # (batch_size, 1)
QRETRLM = np.hstack((QRETRLM, np.array(q[:, 0])))
WRETRLM = np.hstack((WRETRLM, np.array(w[:, 0])))
del brt
del t_avg_down_std
del tau_o_std
del tau_e_std
del k_rho_std
del k_w_std
del a
del b
del c
del d
del p
del s
del u
del q
del w
if int(ns.ms):
# MULTI FREQ METEOSONDE WITH REGULARIZATION
brt = math.as_tensor([[BT[i, :]] for i in indexes]) # под углом | (batch_size, 1, 47)
t_avg_down_real = math.as_tensor(
[avg.downward.T(realAtm, nu, radiometry_angle) for nu in frequencies]
) # под углом | (47, batch_size, 1)
t_avg_down_real = math.move_axis(t_avg_down_real, 0, -1) # (batch_size, 1, 47)
tau_o_real = math.as_tensor(
[realAtm.opacity.oxygen(nu) for nu in frequencies]
) # в зените | (47, batch_size, 1)
tau_o_real = math.move_axis(tau_o_real, 0, -1) # (batch_size, 1, 47)
tau_e_real = math.log(
(t_avg_down_real - T_cosmic) / (t_avg_down_real - brt)
) # (batch_size, 1, 47) - в зените
k_rho_real = math.as_tensor(
[krho(realAtm, nu) for nu in frequencies]
) # в зените
k_rho_real = math.move_axis(k_rho_real, 0, -1) # (batch_size, 1, 47)
k_w_std = math.move_axis(math.as_tensor([[K_W_STD[:]] * len(indexes)]), 0, 1) # (batch_size, 1, 47)
a = math.sum_(tau_e_real * k_w_std, axis=-1) - math.sum_(tau_o_real * k_w_std, axis=-1)
b = math.sum_(k_rho_real * k_w_std, axis=-1) / math.sum_(k_rho_real * k_rho_real, axis=-1)
c = math.sum_(tau_e_real * k_rho_real, axis=-1) - math.sum_(tau_o_real * k_rho_real, axis=-1)
d = c / math.sum_(k_rho_real * k_rho_real, axis=-1) # (batch_size, 1)
p = math.sum_(k_rho_real * k_w_std, axis=-1) # (batch_size, 1)
s = math.sum_(k_w_std * k_w_std, axis=-1) # (batch_size, 1)
u = b * p - s # (batch_size, 1)
w = (u * math.lambertw(-math.exp((a - b * c) * r / u) * r * r / u) - a * r + b * c * r) / (r * u)
q = d - b * w # (batch_size, 1)
QRETRMS = np.hstack((QRETRMS, np.array(q[:, 0])))
WRETRMS = np.hstack((WRETRMS, np.array(w[:, 0])))
del brt
del t_avg_down_real
del tau_o_real
del tau_e_real
del k_rho_real
del k_w_std
del a
del b
del c
del d
del p
del s
del u
del q
del w
del stdAtm
del realAtm
# if int(ns.lm):
# np.save(os.path.join(dump_dir, '{}'.format(ns.qretrlmname)), QRETRLM)
# np.save(os.path.join(dump_dir, '{}'.format(ns.wretrlmname)), WRETRLM)
# if int(ns.ms):
# np.save(os.path.join(dump_dir, '{}'.format(ns.qretrmsname)), QRETRMS)
# np.save(os.path.join(dump_dir, '{}'.format(ns.wretrmsname)), WRETRMS)
progress += len(indexes)
end = time.time() - start
print(colored('Total progress: {:.5f}% \t\t Batch no. {} out of {}\t\t Time spent per batch: {:.4f}'.format(
progress / len(TS) * 100.,
n + 1, n_batches,
end),
'green')
)
print('\nСохраняем...')
QSTD = np.asarray(QSTD)
QREAL = np.asarray(QREAL)
# np.save(os.path.join(dump_dir, 'qstd.npy'), QSTD)
# np.save(os.path.join(dump_dir, 'qreal.npy'), QREAL)
if int(ns.lm):
np.save(os.path.join(dump_dir, '{}'.format(ns.qretrlmname)), QRETRLM)
np.save(os.path.join(dump_dir, '{}'.format(ns.wretrlmname)), WRETRLM)
if int(ns.ms):
np.save(os.path.join(dump_dir, '{}'.format(ns.qretrmsname)), QRETRMS)
np.save(os.path.join(dump_dir, '{}'.format(ns.wretrmsname)), WRETRMS)