-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gpm_multi.py
executable file
·163 lines (121 loc) · 4.95 KB
/
test_gpm_multi.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
from collections import OrderedDict
import itertools
import time
import math
import h5py
from netCDF4 import Dataset
import scipy
import sys
def main():
sr_pars = {"trmm": {
"zt": 402500., # orbital height of TRMM (post boost) APPROXIMATION!
"dr": 250., # gate spacing of TRMM
}, "gpm": {
"zt": 407000., # orbital height of GPM APPROXIMATION!
"dr": 125., # gate spacing of GPM
}}
bw_sr = 0.71 # SR beam width
platf = "gpm" # SR platform/product: one out of ["gpm", "trmm"]
zt = sr_pars[platf]["zt"] # SR orbit height (meters)
dr_sr = sr_pars[platf]["dr"] # SR gate length (meters)
fileName = "2A.GPM.Ku.V720170308.20180502-S014128-E021127.V05A.RT-H5"
sr_data = read_gpm(fileName)
def read_gpm(filename, bbox=None):
"""Reads GPM files for matching with GR
Parameters
----------
filename : string
path of the GPM file
bbox : dict
dictionary with bounding box coordinates (lon, lat),
defaults to None
Returns
-------
gpm_data : dict
dictionary of gpm data
Examples
--------
See :ref:`/notebooks/match3d/wradlib_match_workflow.ipynb`.
"""
pr_data = Dataset(filename, mode="r")
lon = pr_data['NS'].variables['Longitude']
lat = pr_data['NS'].variables['Latitude']
if bbox is not None:
poly = [[bbox['left'], bbox['bottom']],
[bbox['left'], bbox['top']],
[bbox['right'], bbox['top']],
[bbox['right'], bbox['bottom']],
[bbox['left'], bbox['bottom']]]
mask = get_clip_mask(np.dstack((lon[:], lat[:])), poly)
else:
mask = np.ones_like(lon, dtype=bool, subok=False)
mask = np.nonzero(np.count_nonzero(mask, axis=1))
lon = lon[mask]
lat = lat[mask]
year = pr_data['NS']['ScanTime'].variables['Year'][mask]
month = pr_data['NS']['ScanTime'].variables['Month'][mask]
dayofmonth = pr_data['NS']['ScanTime'].variables['DayOfMonth'][mask]
# dayofyear = pr_data['NS']['ScanTime'].variables['DayOfYear'][mask]
hour = pr_data['NS']['ScanTime'].variables['Hour'][mask]
minute = pr_data['NS']['ScanTime'].variables['Minute'][mask]
second = pr_data['NS']['ScanTime'].variables['Second'][mask]
# secondofday = pr_data['NS']['ScanTime'].variables['SecondOfDay'][mask]
millisecond = pr_data['NS']['ScanTime'].variables['MilliSecond'][mask]
date_array = zip(year, month, dayofmonth,
hour, minute, second,
millisecond.astype(np.int32) * 1000)
pr_time = np.array(
[dt.datetime(d[0], d[1], d[2], d[3], d[4], d[5], d[6]) for d in
date_array])
sfc = pr_data['NS']['PRE'].variables['landSurfaceType'][mask]
pflag = pr_data['NS']['PRE'].variables['flagPrecip'][mask]
# bbflag = pr_data['NS']['CSF'].variables['flagBB'][mask]
zbb = pr_data['NS']['CSF'].variables['heightBB'][mask]
# print(zbb.dtype)
bbwidth = pr_data['NS']['CSF'].variables['widthBB'][mask]
qbb = pr_data['NS']['CSF'].variables['qualityBB'][mask]
qtype = pr_data['NS']['CSF'].variables['qualityTypePrecip'][mask]
ptype = pr_data['NS']['CSF'].variables['typePrecip'][mask]
quality = pr_data['NS']['scanStatus'].variables['dataQuality'][mask]
refl = pr_data['NS']['SLV'].variables['zFactorCorrected'][mask]
# print(pr_data['NS']['SLV'].variables['zFactorCorrected'])
zenith = pr_data['NS']['PRE'].variables['localZenithAngle'][mask]
pr_data.close()
# Check for bad data
if max(quality) != 0:
raise ValueError('GPM contains Bad Data')
pflag = pflag.astype(np.int8)
# Determine the dimensions
ndim = refl.ndim
if ndim != 3:
raise ValueError('GPM Dimensions do not match! '
'Needed 3, given {0}'.format(ndim))
tmp = refl.shape
nscan = tmp[0]
nray = tmp[1]
nbin = tmp[2]
# Reverse direction along the beam
refl = np.flip(refl, axis=-1)
# Change pflag=1 to pflag=2 to be consistent with 'Rain certain' in TRMM
pflag[pflag == 1] = 2
# Simplify the precipitation types
ptype = (ptype / 1e7).astype(np.int16)
# Simplify the surface types
imiss = (sfc == -9999)
sfc = (sfc / 1e2).astype(np.int16) + 1
sfc[imiss] = 0
# Set a quality indicator for the BB and precip type data
# TODO: Why is the `quality` variable overwritten?
quality = np.zeros((nscan, nray), dtype=np.uint8)
i1 = ((qbb == 0) | (qbb == 1)) & (qtype == 1)
quality[i1] = 1
i2 = ((qbb > 1) | (qtype > 2))
quality[i2] = 2
gpm_data = {}
gpm_data.update({'nscan': nscan, 'nray': nray, 'nbin': nbin,
'date': pr_time, 'lon': lon, 'lat': lat,
'pflag': pflag, 'ptype': ptype, 'zbb': zbb,
'bbwidth': bbwidth, 'sfc': sfc, 'quality': quality,
'refl': refl, 'zenith': zenith})
return gpm_data
main()