/
conversion.py
executable file
·350 lines (279 loc) · 11.3 KB
/
conversion.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#################
import numpy as np
import itertools
#################
"""
Module containing class Conversion. Can also be executed as a main function.
Created on Mon Dec 21 14:41:11 2015
@author: fvanden
Revision history: 21.12.2015 - Created
03.01.2016 - digitalTodB function added
TO DO: add other conversions
27.01.2016 - log and lin transformations added
conversion no longer daughterclass of compute
createKeys and findKey added
08.02.2016 - createKeys extended so that Rhohv = RHO, V = Rvel
and Sw = W
12.08.2016 - Isztar's method added to refl2Rain
20.10.2017 - added gwt conversion
"""
## -------------------------------------------------------------------------- ##
class Conversion():
"""
class Conversion: contains all kinds of conversions performed on data and
keys within the ProfileLab toolbox. i.e. Converts the diffent types of
polarimetric keys and reflectivity rainrate relations.
Parameters
----------
None : depends on the many functions
Attributes
----------
self.conventions : list of strings, the types of polarimetric conventions
we work with
self.convertKeys : dict, lookup table for conversions between conventions
"""
## ------------------------------------------------------------------ ##
## Constructors/Destructors ##
## ------------------------------------------------------------------ ##
def __init__(self):
"""
Initiates mother class Compute. As such, also inherits Data and
coordinate Grid.
"""
self.conventions = ['MXPOL','MCH','LogLin']
self.convertKeys = self.createKeys()
def __del__(self):
pass
## ------------------------------------------------------------------ ##
## Methods ##
## ------------------------------------------------------------------ ##
# public:
def Refl2Rain(self, reflectivity, method = 'Zh', A = 200, b = 5/8):
"""
"""
if method == 'Zh':
rainrate = (self.logToLin(reflectivity, 'Z') / A)**(b)
if method == 'Isztar':
x = reflectivity
logRR = -2.3 +0.17*x -5.1e-3*x**2 +9.8e-5*x**3 -6e-7*x**4
dBR = logRR * 10
rainrate = dBR
rainrate = self.logToLin(rainrate, 'Z')
return rainrate
def Rain2Refl(self, rainrate, method = 'Zh', A = 200, b = 5/8):
"""
"""
if method == 'Zh':
reflectivity = self.linToLog(rainrate**(1/(b))*A, 'Z')
return reflectivity
def logToLin(self, data, polvar):
"""
Transforms logarithmic data into linear data, with a different
transformation depending on type of polarimetric variable.
Parameters
----------
data : list or numpy array, polarimetric data to be converted
polvar : str, name of the polarimetric variable type for the data
Returns
-------
varLin : list or numpy array : data converted into linear values
"""
polvar = self.findKey('LogLin', polvar)
data = np.asarray(data)
if(polvar in ['Ph','Pv','SNRh','SNRv','Zh','Zv','MZh','MZv']):
varLin=10**(0.1*data)
elif(polvar=='Sw'):
varLin=data**2
else:
varLin = data
return varLin
def linToLog(self, data, polvar):
"""
Transforms linear data to logarithmic data, with a different
transformation depending on type of polarimetric variable.
Parameters
----------
data : list or numpy array, polarimetric data to be converted
polvar : str, name of the polarimetric variable type for the data
Returns
-------
varLin : list or numpy array : data converted into linear values
"""
polvar = self.findKey('LogLin', polvar)
data = np.asarray(data)
if(polvar in ['Ph','Pv','SNRh','SNRv','Zh','Zv','MZh','MZv']):
varLog = 10*np.log10(data)
elif(polvar=='Sw'):
varLog = np.sqrt(data)
else:
varLog = data
return varLog
def digitalTodB(self, Data, polvar):
"""
Digital number to dB conversion. Automatically converts between MXPOL
and MCH conventions.
Parameters
----------
Data : dict, with instrumentnames in the keys and data values
polvar : str, polarimetric variable name.
Returns
-------
Data, dict with instrumentnames in keys and converted data
"""
for i in range(0,len(Data)):
for key in Data:
if 'AL' or 'LD' or 'PM' in key.split('_')[0]:
if polvar == 'Z':
data = Data[key]
data = np.asarray(data)
data = data.astype(float)
output=np.zeros(data.shape)
output[data!=0]=(data[data!=0])*0.5
output[data==0]=float('nan')
Data[key] = output
elif polvar == 'ZDR':
pass
else:
pass
return Data
def findKey(self, convention, polvar):
"""
Finds the correct variable name for a given convention (MXPOL, MCH) and
a given variable name which was spelled with a different case or
according to a different convention. For example, MXPOL convention uses
'Z' for the reflectivity variable, but if a user inserted 'Zh' this
function will convert it to 'Z'.
Parameters
----------
convention : str, for now, MCH, MXPOL or LogLin (for logarithmic/linear
conversions)
polvar : str, key of polarimetric variable to be converted
Returns
-------
mykey : str, polarimertric variable key as used within the ProfileLab
toolbox context
"""
for key, value in self.convertKeys[convention].items():
mykey = polvar
if polvar in value:
mykey = key
break
return mykey
def gwtConv(self, gwtcode):
"""
Converts GWT26 code into flow direction and cyclonicity
Parameters
----------
gwtcode : int, gwt number from MeteoSwiss observations or
2-tuple of int organised as: (flow direction (int), cyclonicity
(float))
Returns
-------
output : the converted code
See Also
--------
For GWT26 code:
http://www.meteoschweiz.admin.ch/content/dam/meteoswiss/en/Ungebundene-Seiten/Publikationen/Fachberichte/doc/ab235.pdf
The flow direction/cyclonicity code is of my own invention and is
organised as follows:
directions are given by numbers 1 to 8, starting with 1 for North and
in clockwise direction ending with 8 for NorthWest. Cyclonicity is
given by a floating number: 1 for cyclonic, 0 for anticyclonic and
0.5 for indifferent.
"""
conversion_table = {
'nan' : 'nan',
1 : (7, 1.),
2 : (6,1.),
3 : (8,1.),
4 : (1,1.),
5 : (2,1.),
6 : (3, 1.),
7 : (4,1.),
8 : (5,1.),
9 : (7,0.),
10 : (6,0.),
11 : (8,0.),
12 : (1,0.),
13 : (2,0.),
14 : (3,0.),
15 : (4,0.),
16 : (5,0.),
17 : (7,.5),
18 : (6,.5),
19 : (8,.5),
20 : (1,.5),
21 : (2,.5),
22 : (3,.5),
23 : (4,.5),
24 : (5,.5),
25 : (0,1.),
26 : (0,.5),
}
if isinstance(gwtcode, int):
output = conversion_table[gwtcode]
elif np.isnan(gwtcode):
output = np.nan
elif isinstance(gwtcode, tuple):
for key, value in conversion_table.items():
if value == gwtcode:
output = key
else:
print("not a valid input, try integer or tuple")
output = []
return output
# private:
def createKeys(self):
"""
Creates dictionary for key conversion
"""
MCH = ['Z','ZV','ZDR','PHIDP','V', 'W', 'RHO', 'MPH', 'CLUT', 'STA1', 'STA2', 'WBN']
MXPOL = ['Zh','Zdr', 'Phidp', 'RVel','Sw','Rhohv', 'Zv', 'Clut']
LogLin = ['Ph','Pv','SNRh','SNRv','Zh','Zv','MZh','MZv','Sw']
convertkeys = {}
for conv in self.conventions:
convertkeys[conv] = {}
if conv == 'MXPOL':
variables = MXPOL
elif conv == 'MCH':
variables = MCH
elif conv == 'LogLin':
variables = LogLin
else:
# add other conventions here
pass
for var in variables:
convertkeys[conv][var] = []
convertkeys[conv][var] = list((map(''.join, itertools.product(*zip(var.upper(), var.lower())))))
if var == 'Zh':
convertkeys[conv][var].extend('z')
convertkeys[conv][var].extend('Z')
elif var == 'Z':
s = 'Zh'
convertkeys[conv][var].extend(list((map(''.join, itertools.product(*zip(s.upper(), s.lower()))))))
elif var == 'RHO' or var == 'Rhohv':
if conv == 'MCH':
s = 'Rhohv'
else:
s = 'RHO'
convertkeys[conv][var].extend(list((map(''.join, itertools.product(*zip(s.upper(), s.lower()))))))
elif var == 'Sw' or var == 'W':
if conv == 'MCH':
s = 'Sw'
else:
s = 'W'
convertkeys[conv][var].extend(list((map(''.join, itertools.product(*zip(s.upper(), s.lower()))))))
elif var == 'RVel' or var == 'V':
if conv == 'MCH':
s = 'RVel'
else:
s = 'V'
convertkeys[conv][var].extend(list((map(''.join, itertools.product(*zip(s.upper(), s.lower()))))))
else:
pass
return convertkeys
## -------------------------------------------------------------------------- ##
if __name__ == '__main__':
test = Conversion()