metradar 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,203 +0,0 @@
1
- '''
2
- 本程序将极坐标格式的ppi数据转换为格点数据,并保存为nc格式
3
-
4
- '''
5
-
6
- # %%
7
- import numpy as np
8
- import xarray as xr
9
- from decode_fmt_pyart import read_cnrad_fmt
10
- import os
11
- import time
12
- import shutil
13
-
14
- def do_ppi(params):
15
-
16
- # sweep_slice,radar,outname
17
- sweep_slice = params['sweep_slice']
18
- target_reso = params['out_reso']
19
- # print(sweep_slice)
20
- radar = params['radar']
21
-
22
- ori_reso = radar.range['meters_between_gates']
23
- # target_reso = 500
24
- reso_list = list(np.arange(0,11,1)*ori_reso)
25
- # 从reso_list中找到最接近target_reso的值
26
- out_scale = np.argmin(np.abs(np.array(reso_list)-target_reso))
27
- # out_scale = int(target_reso/ori_reso)
28
- target_reso = ori_reso * out_scale
29
-
30
- # sweep_slice = slice(0,384,None)
31
- sweep_azimuths = radar.azimuth['data'][sweep_slice]
32
- elv = np.mean(radar.elevation['data'][sweep_slice])
33
-
34
- # 输出的距离库只保留一半
35
- ngates = int(len(radar.gate_x['data'][sweep_slice][0])/2) #//2*2
36
- grid_x = np.arange(-1*ngates,ngates+1,out_scale)
37
- grid_y = np.arange(-1*ngates,ngates+1,out_scale)
38
- total_gates = len(grid_x)
39
-
40
- aa = np.meshgrid(grid_x,grid_y)
41
- azi_grid = np.arctan2(aa[0],aa[1])*180/np.pi
42
- azi_grid[azi_grid<0]+=360
43
-
44
- azi_reso = 360/len(sweep_azimuths)
45
-
46
- # 求方位角索引
47
- new_azi = azi_grid.flatten()
48
- t = new_azi-sweep_azimuths[0]
49
- t[t<0]+=360
50
- ray_number = np.round(t/azi_reso,0).astype(int)
51
- ray_number[ray_number==len(sweep_azimuths)]=0
52
- ray_number = np.reshape(ray_number,(total_gates,total_gates))
53
-
54
-
55
- # 求距离索引
56
- dis_grid = np.sqrt(aa[0]**2 + aa[1]**2)
57
- dis_grid = np.round(dis_grid.flatten(),0).astype(int)
58
- dis_grid = np.reshape(dis_grid,(total_gates,total_gates))
59
-
60
-
61
-
62
- sweepdata = radar.fields['reflectivity']['data'][sweep_slice]
63
- # 对数据进行截断,在径向方向上
64
- sweepdata = sweepdata[:,0:ngates]
65
- # if np.max(sweepdata.data.flatten()) < -30:
66
- # # 表示该层无有效数据
67
- # return None
68
- # data_grid = np.zeros((total_gates,total_gates)) - 9999
69
- data_grid = np.zeros((total_gates,total_gates))
70
- new_data = data_grid.flatten()
71
- new_spdata = sweepdata.flatten()
72
-
73
- pos_out = [i+j*total_gates for i in range(total_gates) for j in range(total_gates) if dis_grid[i,j] < ngates]
74
- d_out = [rn*ngates+dg for rn,dg in zip(ray_number.flatten(),dis_grid.flatten()) if dg < ngates]
75
-
76
- new_data[pos_out]=new_spdata[d_out]
77
- new_data[new_data<-10]=np.nan
78
- new_data[new_data>=95]=np.nan
79
- new_data = new_data *2 + 64
80
- new_data[np.isnan(new_data)]=255
81
- new_data = new_data.astype(np.uint8)
82
- data_grid = np.reshape(new_data,(total_gates,total_gates))
83
- outdic={'data':data_grid.transpose(),'elv':elv,'tol_gates':total_gates,
84
- 'out_reso':target_reso,'datatype':'uint8','scale_factor':2,'add_offset':64,
85
- 'missing_value':255}
86
- print('ele: ' + str(elv) + ' done!')
87
-
88
- del data_grid,new_data,new_spdata,pos_out,d_out,sweepdata
89
- return outdic
90
-
91
- def do_radar(param):
92
- pass
93
- try:
94
- filename = param['filename']
95
- outname = param['outname']
96
- out_reso = param['out_reso']
97
- if 'bcopy' in param.keys():
98
- bcopy = param['bcopy']
99
- else:
100
- bcopy = 0
101
- if 'outpath2' in param.keys():
102
- outpath2 = param['outpath2']
103
- else:
104
- outpath2 = ''
105
- print('outpath2 not set!')
106
- print('start trans ' + filename)
107
-
108
- # 读取雷达数据
109
- radar = read_cnrad_fmt(filename)
110
-
111
- if 'reflectivity' in radar.fields.keys():
112
- print('ref in data!')
113
-
114
-
115
- start = time.time()
116
-
117
- all_data = []
118
- elvs = []
119
-
120
- nnf=0
121
- pre_ele=-999
122
- for sweep_slice in radar.iter_slice():
123
- # if nnf ==2:
124
- # break
125
- params={'sweep_slice':sweep_slice,'radar':radar,'out_reso':out_reso,}
126
- cur_ele = np.mean(radar.elevation['data'][sweep_slice])
127
- if abs(cur_ele - pre_ele) < 0.2:
128
- # print('该仰角已处理过!')
129
- # 实际上是跳过了最低仰角的两层径向速度
130
- continue
131
-
132
- outdic = do_ppi(params)
133
- if outdic is None:
134
- continue
135
- all_data.append(outdic['data'])
136
- elvs.append(outdic['elv'])
137
- pre_ele = cur_ele
138
- nnf +=1
139
- end = time.time()
140
-
141
-
142
- #构建xarray数据
143
- total_gates = outdic['tol_gates']
144
- if total_gates % 2 == 0:
145
- out_grid = np.arange(int(-total_gates/2),int(total_gates/2))
146
- else:
147
- out_grid = np.arange(int(-(total_gates-1)/2),int((total_gates-1)/2)+1)
148
-
149
- if len(all_data) == 0:
150
- print('no data in ' + filename)
151
-
152
- return
153
- all_data = np.array(all_data)
154
- all_data = all_data.astype(np.uint8)
155
- data = xr.DataArray(np.array(all_data),coords=[elvs,out_grid,out_grid],dims=['z','x','y'],name='ref')
156
- data.attrs['units'] = 'dBZ'
157
- data.attrs['standard_name'] = 'equivalent_reflectivity_factor'
158
- data.attrs['long_name'] = 'equivalent_reflectivity_factor'
159
- data.attrs['radar_lat'] = radar.latitude['data'][0]
160
- data.attrs['radar_lon'] = radar.longitude['data'][0]
161
- data.attrs['radar_altitude'] = radar.altitude['data'][0]
162
- data.attrs['grid_num'] = total_gates
163
- data.attrs['grid_reso'] = outdic['out_reso']
164
- data.attrs['elevation'] = elvs
165
- data.attrs['obs_range'] = int(outdic['out_reso'] * (total_gates-1)/2)
166
- data.attrs['distance_unit'] = 'meter'
167
- data.attrs['missing_value'] = 255
168
- data.attrs['scale_factor'] = 2
169
- data.attrs['add_offset'] = 64
170
- data.attrs['datatype'] = 'uint8'
171
- data.attrs['decode_method'] = 'dbz = (data - add_offset) / scale_factor'
172
-
173
- if not os.path.exists(os.path.dirname(outname)):
174
- os.makedirs(os.path.dirname(outname))
175
-
176
- data.to_netcdf(outname,encoding={'ref': {'zlib': True,'dtype':'uint8'},})
177
- print(outname + ' saved!')
178
-
179
- # copy to outpath2
180
- if bcopy == 1:
181
- if not os.path.exists(outpath2):
182
- os.makedirs(outpath2)
183
- shutil.copy(outname,outpath2 + os.sep + os.path.basename(outname))
184
- print(outpath2 + os.sep + os.path.basename(outname) + ' copied!')
185
-
186
-
187
- except Exception as e:
188
- print(e)
189
-
190
- if __name__ == "__main__":
191
- rootpath = 'testdata/FMT/'
192
- filename = 'Z_RADR_I_Z9200_20240831130000_O_DOR_SAD_CAP_FMT.bin.bz2'
193
-
194
- outname = '/Users/wenjianzhu/Downloads/polargrid/' + filename + '_new.nc'
195
- params={'filename':rootpath + filename,'outname':outname,'out_reso':500,'bcopy':0}
196
-
197
-
198
- st = time.time()
199
- do_radar(params)
200
- et = time.time()
201
- print(et-st)
202
-
203
-
@@ -1,18 +0,0 @@
1
- Metadata-Version: 2.2
2
- Name: metradar
3
- Version: 0.1.2
4
- Summary: radar data processing
5
- Author-email: Wenjian Zhu <kevin2075@163.com>
6
- Classifier: Programming Language :: Python :: 3
7
- Classifier: License :: OSI Approved :: MIT License
8
- Classifier: Operating System :: OS Independent
9
- Requires-Python: >=3.10
10
- Description-Content-Type: text/markdown
11
- Requires-Dist: numpy>=1.18
12
- Requires-Dist: requests>=2.24.0
13
- Requires-Dist: arm_pyart>=1.19.5
14
- Requires-Dist: cartopy>=0.24.0
15
- Requires-Dist: openpyxl>=3.1.5
16
- Requires-Dist: pydda==1.2.0
17
- Requires-Dist: tqdm>=4.67.0
18
- Requires-Dist: wradlib>=2.4.0