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,386 +0,0 @@
1
- #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
- '''
4
- @File : construct_aws_refvpr_mainprog.py
5
- @Time : 2023/08/19 20:58:59
6
- @Author : Wenjian Zhu
7
- @Version : 1.0
8
- @Email : kevin2075@163.com
9
- '''
10
-
11
- from retrieve_cmadaas import cmadaas_obs_by_time_range_and_id,cmadaas_get_radar_vol,cmadaas_sounding_by_time,cmadaas_get_radar_3dref
12
- import pandas as pd
13
- import geo_transforms_pyart as geotrans
14
- import math
15
- import numpy as np
16
- import os
17
- import configparser
18
- from make_mosaic_mp_archive import MAKE_RADAR_MOSAIC
19
- from multiprocessing import freeze_support
20
- from datetime import datetime,timedelta
21
- import xarray as xr
22
- import numpy.ma as MA
23
- import matplotlib.pyplot as plt
24
- from parse_pal import parse_pro
25
- from matplotlib.ticker import (MultipleLocator)
26
- from matplotlib import font_manager
27
- from comm_func import geopotential_to_height
28
- from trans_new_mosaic_nc import trans_bin_nc_batch
29
- # 一键生成VPR数据和自动站数据,并绘制图形
30
- #数据来源:天擎
31
-
32
-
33
- if __name__ == "__main__":
34
- freeze_support()
35
- print('entering program......')
36
- start_time = '20230703190000'
37
- # end_time = '20230703200000'
38
- end_time = '20230704020000'
39
- time_range = "[%s,%s]"%(start_time, end_time)
40
-
41
- # 读取自动站雨量数据
42
- # A7606, A7607, A7055, A7617
43
- staid = 'A7617'
44
- outpath = '/Users/wenjianzhu/Downloads/testvpr'
45
- outpath_fmt = outpath + os.sep + 'fmt'
46
- outpath_tlogp = outpath + os.sep + 'tlogp'
47
- outpath_sta = './output/VPR'
48
- outpath_pic = './output/VPR'
49
- outpath_ref = './output/VPR'
50
- outname_sta= '%s.csv'%staid
51
- outname_ref = 'p3_vpr_%s_datan3d.nc'%staid
52
- outpath_mosaic = outpath + os.sep + 'datan3d'
53
-
54
- if not os.path.exists(outpath):
55
- os.makedirs(outpath)
56
- if not os.path.exists(outpath_fmt):
57
- os.makedirs(outpath_fmt)
58
- if not os.path.exists(outpath_tlogp):
59
- os.makedirs(outpath_tlogp)
60
-
61
- #第一步,从天擎下载自动站数据
62
- elements='Station_Name,Station_Id_C,Station_Id_d,lat,lon,Datetime,PRE,PRE_1h'
63
-
64
- data_code = 'SURF_CHN_MUL_MIN'
65
-
66
- # 读取数据
67
- data = cmadaas_obs_by_time_range_and_id(time_range=time_range, data_code = data_code,elements=elements,sta_ids = staid)
68
- if data is None:
69
- print('required data is None, please check the site number!')
70
-
71
- sta_lat = data['lat'][0]
72
- sta_lon = data['lon'][0]
73
- sta_name = data['Station_Name'][0]
74
-
75
- # data.to_csv(outpath_sta + os.sep + outname_sta,index=False)
76
- data2 = data.set_index('Datetime')
77
- # data2['PRE_1h'][0]=0
78
-
79
-
80
- diff_rain =[]
81
- diff_rain.append(0)
82
-
83
-
84
- # PRE_1h表示当前小时的累计雨量
85
- # pre_5min表示当前5分钟的累计雨量
86
- # pre_1min表示当前1分钟的累计雨量
87
- # accsum表示从起始时刻到当前时刻的累计雨量
88
- # 时间均为UTC时间
89
-
90
- # diff()为求差,mode()为获取众数
91
- # steps 为数据的时间间隔,单位为分钟
92
- steps = int(data['Datetime'].diff().mode()[0].seconds/60)
93
- difrain_name = 'pre_%dmin'%steps
94
- for nn in np.arange(0,data2.shape[0]-1):
95
-
96
- if data2['PRE_1h'][nn+1] >= data2['PRE_1h'][nn]:
97
- diff_rain.append(round(data2['PRE_1h'][nn+1]-data2['PRE_1h'][nn],2))
98
- else:
99
- diff_rain.append(round(data2['PRE_1h'][nn+1],2))
100
- newpd = pd.DataFrame(diff_rain,columns=[difrain_name],index=data2.index)
101
- # 如果间隔是1分钟,那么还需要求出5分钟的雨量
102
- if steps == 1:
103
- newpd['pre_5min'] = newpd[difrain_name].rolling(5).sum()
104
- newpd['pre_5min'] = newpd['pre_5min'].round(2)
105
- else:
106
- pass
107
- newindex = [tt for tt in newpd.index if tt.minute%5==0]
108
- newpd['accsum'] = newpd[difrain_name].cumsum()
109
- newpd['accsum'] = newpd['accsum'].round(2)
110
- newpd['PRE_1h'] = data2['PRE_1h']
111
- newpd['PRE'] = data2['PRE']
112
- newpd['lat'] = data2['lat']
113
- newpd['lon'] = data2['lon']
114
- newpd['staname']=data2['Station_Name']
115
- newpd.to_csv(outpath_sta + os.sep + outname_sta,index=True)
116
-
117
- # 第二步,从离自动站最近的探空站中获取探空数据,提取零度层高度和-20度层高度
118
- # 读取探空站信息,获取经纬度、站号等信息
119
- # tlogpinfo = pd.read_csv('../common/stationinfo_new/china_tlogp_stations_m3.txt',delimiter=r"\s+",skiprows=3,names=['stanum','lon','lat','alt','num2'])
120
-
121
- # tmpdata['stanum'].values[0] int
122
- # staid_tlogp = str(tmpdata['stanum'].values[0])
123
- tlogpdata = cmadaas_sounding_by_time(times='20230704000000')
124
- allstas = np.unique(tlogpdata['Station_Id_C'])
125
- alllons=[]
126
- alllats=[]
127
- #获取所有站点的经纬度信息
128
- for ts in allstas:
129
- tmpd = tlogpdata[tlogpdata['Station_Id_C']==ts]
130
- curlat = tmpd['Lat'].values[0]
131
- curlon = tmpd['Lon'].values[0]
132
- alllons.append(curlon)
133
- alllats.append(curlat)
134
-
135
- x,y= geotrans.geographic_to_cartesian_aeqd(lon=alllons,lat=alllats,lon_0=sta_lon,lat_0=sta_lat)
136
- dis = [math.sqrt(pow(x[k],2) + pow(y[k],2))/1000 for k in range(len(x))]
137
- flag = np.array(dis) <= min(dis)+0.1
138
- tmpts = allstas[flag]
139
- tmpdata = tlogpdata[tlogpdata['Station_Id_C']==tmpts[0]]
140
- tmpdata = tmpdata.sort_values(by='PRS_HWC',ascending=False)
141
- tmpdata.dropna(subset=['PRS_HWC'],inplace=True)
142
- tmpdata.to_csv(outpath_tlogp + os.sep + 'tlogp_%s.csv'%tmpts[0],index=False)
143
- #查找离零度层和-20度层最近的高度
144
- flag = np.array(abs(tmpdata['TEM'])) <= abs(tmpdata['TEM']).min()+0.01
145
- z0 = tmpdata['GPH'][flag].values[0]
146
- flag = np.array(abs(tmpdata['TEM']+20)) <= abs(tmpdata['TEM']+20).min()+0.01
147
- z20 = tmpdata['GPH'][flag].values[0]
148
-
149
- z0 = geopotential_to_height(z0*9.80665)
150
- z20 = geopotential_to_height(z20*9.80665)
151
-
152
-
153
- # 第三步,从天擎读取雷达三维拼图数据
154
- data_code = 'RADA_L3_MST_PRE_3REF_QC'
155
- allpath,allfiles=cmadaas_get_radar_3dref(starttime=start_time, endtime=end_time, data_code = data_code,outdir=outpath_mosaic)
156
-
157
- # 下载完成之后,进行批量转换
158
- if len(allpath) > 0:
159
- # 表示都是bin文件,尚未整理成nc以及进一步拼接位3d数据
160
- # 需要进行转换
161
- for cpath in np.unique(allpath):
162
- trans_bin_nc_batch(str(cpath),str(cpath))
163
- # 转换完成后,删除.bin文件
164
- files = os.listdir(str(cpath))
165
- # for filename in files:
166
- # if not filename.endswith('.nc'):
167
- # os.remove(str(cpath)+os.sep+filename)
168
-
169
- # 转nc结束后,将多层nc文件进行拼接成3d文件
170
- files = os.listdir(str(cpath))
171
- files=sorted(files)
172
- validts = []
173
- if len(files)==0:
174
- print('no valid files')
175
- continue
176
- for filename in files:
177
- if not filename.endswith('.nc'):
178
- print('not nc file: %s'%(filename))
179
- continue
180
- validts.append(filename[9:24])
181
- if len(validts)==0:
182
- print('no valid times in %s'%(str(cpath)))
183
- continue
184
- validts = np.unique(validts)
185
-
186
- for vts in validts:
187
- validfiles = []
188
- file_paths = []
189
- for filename in files:
190
- if filename.find(vts) >= 0 and filename.endswith('.nc'):
191
- validfiles.append(filename)
192
- file_paths.append(str(cpath)+os.sep+filename)
193
- if len(validfiles) == 0:
194
- print('no valid files for time %s'%vts)
195
- continue
196
- # xdata = xr.open_dataset(str(cpath)+os.sep+validfiles[0])
197
- # ds = xr.open_mfdataset(file_paths,engine='netcdf4',combine='nested')
198
- allref=[]
199
- ds0 = xr.open_dataset(file_paths[0])
200
- lon = ds0['lon'].values
201
- lat = ds0['lat'].values
202
- hgts=[]
203
- maxv = ds0['ref'].attrs['maxv']
204
- minv = ds0['ref'].attrs['minv']
205
- for file in file_paths:
206
- cds = xr.open_dataset(file)
207
- hgts.append(cds['lev'].values[0])
208
- # allds.append(cds)
209
- allref.append(cds['ref'].values[0])
210
-
211
- lev_coord = ('lev', hgts, {'long_name':'height', 'units':'m', '_CoordinateAxisType':'Height'})
212
- lon_coord = ('lon', lon, {
213
- 'long_name':'longitude', 'units':'degrees_east', '_CoordinateAxisType':'Lon'})
214
- lat_coord = ('lat', lat, {
215
- 'long_name':'latitude', 'units':'degrees_north', '_CoordinateAxisType':'Lat'})
216
-
217
- # create xarray
218
- varattrs = {'long_name': 'Refelectivity',
219
- 'short_name': 'ref', 'units': 'dBZ',
220
- 'maxv':maxv,
221
- 'minv':minv}
222
- allref = np.reshape(allref, (len(hgts), len(lat), len(lon)))
223
- data = xr.Dataset({'ref':(['lev','lat', 'lon'], allref, varattrs)},coords={ 'lev':lev_coord,'lat':lat_coord, 'lon':lon_coord})
224
-
225
- # add attributes
226
- data.attrs['Conventions'] = "CF-1.6"
227
- data.attrs['Origin'] = 'cmadaas'
228
- data.attrs['author'] = 'ZWJ'
229
- outname_3d = str(cpath)+os.sep+'ACHN_CAP_'+vts+'_3d.nc'
230
- data.to_netcdf(outname_3d,encoding={'ref': {'zlib': True}})
231
- print(outname_3d + ' done!')
232
- pass
233
-
234
- # 第六步,绘制图形
235
- # 根据自动站数据和三维拼图数据构建VPR并绘制图形
236
-
237
- # 从三维拼图数据中获取格点的时间序列
238
- data = pd.read_csv(outpath_sta+os.sep+outname_sta,index_col=0)
239
- sta_lat = data['lat'][0]
240
- sta_lon = data['lon'][0]
241
- sta_name = data['staname'][0]
242
- startt = datetime.strptime(start_time,'%Y%m%d%H%M%S')
243
- endt = datetime.strptime(end_time,'%Y%m%d%H%M%S')
244
- curt = startt
245
- allref=[]
246
- grd_height = None
247
- file_times=[]
248
- while curt <= endt:
249
-
250
- # curname = 'mosaic_' + curt.strftime('%Y%m%d%H%M') + '.nc'
251
- curname = 'ACHN_CAP_' + curt.strftime('%Y%m%d_') + curt.strftime('%H%M00') + '_3d.nc'
252
- # curmosaic_path = outpath_mosaic + os.sep + curt.strftime('%Y') + os.sep + curt.strftime('%Y%m%d')
253
- curmosaic_path = outpath_mosaic
254
- if not os.path.exists(curmosaic_path + os.sep + curname):
255
- print('file not exist: %s'%(curmosaic_path + os.sep + curname))
256
- curt += timedelta(minutes=6)
257
- continue
258
-
259
- ref = xr.open_dataset(curmosaic_path + os.sep + curname)
260
-
261
- ref2 = ref.sel(lon=sta_lon,lat=sta_lat,method='nearest')['ref'].values
262
- allref.append(ref2)
263
- file_times.append(curt)
264
- curt += timedelta(minutes=6)
265
- ref.close()
266
- allref = np.array(allref)
267
- # allref = allref.reshape(allref.shape[0],allref.shape[2])
268
-
269
- alldata = MA.masked_array(allref, mask=allref==-9999)
270
- grd_height = ref['lev'].values
271
- xrdata = xr.Dataset({
272
- 'dbz':(['z', 'time'], alldata.T, {'long name':'time-height dBZ'})},
273
- coords={'z':grd_height, 'time':file_times},
274
- attrs={'lat':sta_lat, 'lon':sta_lon})
275
-
276
- xrdata.to_netcdf(outpath_ref + os.sep + outname_ref)
277
- print('ref data success!')
278
-
279
- # 画图
280
-
281
- oridf = pd.read_csv(outpath_sta+os.sep+outname_sta)#,index_col=0
282
-
283
- oridf['Datetime'] = pd.to_datetime(oridf['Datetime'], format="%Y-%m-%d %H:%M:%S")
284
- oridf = oridf.set_index('Datetime')
285
- newindex = [tt for tt in oridf.index if tt.minute%5==0]
286
- df = oridf.loc[newindex]
287
- # df.set_index('staname',inplace=True)
288
- df.reset_index(inplace=True)
289
-
290
- #将缺失的时间进行插值处理
291
- helper = pd.DataFrame({'Datetime': pd.date_range(start=df['Datetime'].min(), end=df['Datetime'].max(),freq='300s')})
292
- newdf = pd.merge(df, helper, on='Datetime', how='outer').sort_values('Datetime')
293
- newdf['accsum'] = newdf['accsum'].interpolate(method='linear')
294
-
295
- # ref_colorfile='gr2_colors/default_BR_PUP1.pal'
296
- ref_colorfile = 'gr2_colors/BR_WDTB_Bright.pal'
297
- outdic= parse_pro(ref_colorfile)
298
- cmap=outdic['cmap']
299
- norm=outdic['norm']
300
- units=outdic['units']
301
- # from pyart.graph import common
302
- # cmapname = 'pyart_NWSRef'
303
- # cmap = common.parse_cmap(cmapname, 'reflectivity')
304
-
305
- data = xr.open_dataset(outpath_ref + os.sep + outname_ref)
306
-
307
- data = data.rolling(z=2, time=2, min_periods=1, center=True).mean()
308
-
309
- font_path = 'simsun.ttc'
310
- font_manager.fontManager.addfont(font_path)
311
- prop = font_manager.FontProperties(fname=font_path)
312
-
313
- plt.rcParams["font.size"] = 12
314
- plt.rcParams["font.sans-serif"] = prop.get_name()
315
- fig = plt.figure(figsize=(10,6))
316
- ax1 = fig.add_axes([0.1,0.18,0.8,0.3])#位置[左,下,右,上]
317
- ax3 = fig.add_axes([0.1,0.64,0.8,0.3])#位置[左,下,右,上]
318
- ax2 = ax3.twinx()
319
- ax1.minorticks_on()
320
-
321
-
322
- cb_ax = fig.add_axes([0.25, 0.08, 0.5, 0.03])
323
-
324
- # data.dbz.plot(ax=ax1,cmap=cmap,norm=norm,)
325
- grid_y,grid_x=np.meshgrid(np.arange(0,data.dbz.shape[1]),data.z.values,)
326
- pcm = ax1.pcolormesh(grid_y,grid_x,data.dbz,cmap=cmap,norm=norm)
327
-
328
- z0h = ax1.plot(np.arange(0,data.dbz.shape[1]),np.ones(data.dbz.shape[1])*z0,'y--',linewidth=2,label='0度层高度')
329
- z20h=ax1.plot(np.arange(0,data.dbz.shape[1]),np.ones(data.dbz.shape[1])*z20,'r--',linewidth=2,label='-20度层高度')
330
-
331
-
332
- ax1.legend(loc='upper right')
333
-
334
- ax1.tick_params(axis='x',which='minor',length=3,width=0.8,)
335
- ax1.tick_params(axis='x',which='major',length=5,width=1,)
336
- cb = plt.colorbar(mappable=pcm,cax=cb_ax,extend='both',ticks=np.arange(-10,85,5),orientation='horizontal')
337
- cb.set_label('反射率因子 dBZ',fontsize=12)
338
- ah1=ax2.plot(newdf['accsum'].values,'r',linewidth=2,label='累计雨量')
339
- ah2=ax3.bar(np.arange(0,newdf.shape[0]),newdf['pre_5min'],label='5分钟雨量')
340
- ax2.legend(loc='upper right')
341
- ax3.legend(loc='upper left')
342
- ax3.minorticks_on()
343
- ax3.tick_params(axis='x',which='minor',length=3,width=0.8,)
344
- ax3.tick_params(axis='x',which='major',length=5,width=1,)
345
- ax3.xaxis.set_minor_locator(MultipleLocator(1))
346
- ax1.set_xlim([0,data.dbz.shape[1]+1])
347
- ax2.set_xlim([0,newdf.shape[0]])
348
- ax1.grid('y')
349
- ax2.grid('y')
350
-
351
- ax1.set_ylabel('距离地面的高度(米)')
352
- ax1.set_title('单点回波强度时间-高度图(%.2f°N,%.2f°E)'%(data.lat,data.lon),fontsize=12)
353
- ax2.set_ylabel('累计雨量(毫米)',color='r')
354
- ax3.set_ylabel('5分钟雨量(毫米)',color=[59/255,117/255,175/255])
355
-
356
- tmpdate = pd.to_datetime(data.time)+ timedelta(hours=8)
357
- dates1 = [date.strftime('%H:%M') for date in tmpdate]
358
- dates1[0] = tmpdate.min().strftime('%H:%M \n%Y-%m-%d')
359
-
360
- datestr = dates1[::5]
361
- tmpd = tmpdate[::5]
362
- datestr[-1] = tmpd.max().strftime('%H:%M \n%Y-%m-%d')
363
-
364
- ax1.set_xticks(np.arange(0,data.time.shape[0],5))
365
- ax1.set_xticklabels(datestr)
366
-
367
- ax1.set_ylim([0,20000])
368
- ax1.set_yticks(np.arange(0,20000,2500))
369
-
370
- tmpdate = pd.to_datetime(newdf['Datetime'])+ timedelta(hours=8)
371
- dates2 = [date.strftime('%H:%M') for date in tmpdate]
372
- dates2[0] = tmpdate.min().strftime('%H:%M \n%Y-%m-%d')
373
- dates2[-1] = tmpdate.max().strftime('%H:%M \n%Y-%m-%d')
374
-
375
- ax2.set_xticks(np.arange(0,newdf.shape[0],6))
376
-
377
- ax2.set_xticklabels(dates2[::6])
378
- ax3.set_xlabel('时间(时:分,北京时)')
379
- ax2.set_title('5分钟雨量(柱)、自动站累积雨量(红实线), 站号:%s,%s(%.2f°N,%.2f°E)'%(staid,sta_name,sta_lat,sta_lon),fontsize=12)
380
- # plt.figtext(0.73,0.02,'国家气象中心-天气预报技术研发室制作',fontsize=10)
381
- plt.savefig(outpath_pic + os.sep + outname_ref.replace('.nc','.svg'))#,dpi=600
382
- print(outpath_pic + os.sep + outname_ref.replace('.nc','.svg') + ' saved!')
383
- # plt.show()
384
-
385
-
386
-
@@ -1,306 +0,0 @@
1
- #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
- '''
4
- @File : construct_aws_refvpr_mainprog.py
5
- @Time : 2023/08/19 20:58:59
6
- @Author : Wenjian Zhu
7
- @Version : 1.0
8
- @Email : kevin2075@163.com
9
- '''
10
-
11
- from retrieve_cmadaas import cmadaas_obs_by_time_range_and_id,cmadaas_get_radar_vol,cmadaas_sounding_by_time,cmadaas_get_radar_3dref
12
- import pandas as pd
13
- import geo_transforms_pyart as geotrans
14
- import math
15
- import numpy as np
16
- import os
17
- import configparser
18
- from make_mosaic_mp_archive import MAKE_RADAR_MOSAIC
19
- from multiprocessing import freeze_support
20
- from datetime import datetime,timedelta
21
- import xarray as xr
22
- import numpy.ma as MA
23
- import matplotlib.pyplot as plt
24
- from parse_pal import parse_pro
25
- from matplotlib.ticker import (MultipleLocator)
26
- from matplotlib import font_manager
27
- from comm_func import geopotential_to_height
28
- from trans_new_mosaic_nc import trans_bin_nc_batch,trans_bin_nc_batch2
29
- from read_swan import decode_swan
30
- # 一键生成VPR数据和自动站数据,并绘制图形
31
- #数据来源:天擎
32
-
33
-
34
- if __name__ == "__main__":
35
- freeze_support()
36
- start_time = '20230703190000'
37
- # end_time = '20230703200000'
38
- end_time = '20230704020000'
39
- time_range = "[%s,%s]"%(start_time, end_time)
40
-
41
- # 读取自动站雨量数据
42
- # A7606, A7607, A7055, A7617
43
- staid = 'A7617'
44
- outpath = '/Users/wenjianzhu/Downloads/testvpr'
45
- outpath_fmt = outpath + os.sep + 'fmt'
46
- outpath_tlogp = outpath + os.sep + 'tlogp'
47
- outpath_sta = './output/VPR'
48
- outpath_pic = './output/VPR'
49
- outpath_ref = './output/VPR'
50
- outname_sta= '%s.csv'%staid
51
- outname_ref = 'p3_vpr_%s_swan.nc'%staid
52
- # outpath_mosaic = outpath + os.sep + 'mosaic'
53
- outpath_mosaic = outpath + os.sep + 'swan'
54
- if not os.path.exists(outpath):
55
- os.makedirs(outpath)
56
- if not os.path.exists(outpath_fmt):
57
- os.makedirs(outpath_fmt)
58
- if not os.path.exists(outpath_tlogp):
59
- os.makedirs(outpath_tlogp)
60
-
61
- #第一步,从天擎下载自动站数据
62
-
63
- elements='Station_Name,Station_Id_C,Station_Id_d,lat,lon,Datetime,PRE,PRE_1h'
64
-
65
- data_code = 'SURF_CHN_MUL_MIN'
66
-
67
- # 读取数据
68
- data = cmadaas_obs_by_time_range_and_id(time_range=time_range, data_code = data_code,elements=elements,sta_ids = staid)
69
- if data is None:
70
- print('required data is None, please check the site number!')
71
-
72
- sta_lat = data['lat'][0]
73
- sta_lon = data['lon'][0]
74
- sta_name = data['Station_Name'][0]
75
-
76
- # data.to_csv(outpath_sta + os.sep + outname_sta,index=False)
77
- data2 = data.set_index('Datetime')
78
- # data2['PRE_1h'][0]=0
79
-
80
-
81
- diff_rain =[]
82
- diff_rain.append(0)
83
-
84
-
85
- # PRE_1h表示当前小时的累计雨量
86
- # pre_5min表示当前5分钟的累计雨量
87
- # pre_1min表示当前1分钟的累计雨量
88
- # accsum表示从起始时刻到当前时刻的累计雨量
89
- # 时间均为UTC时间
90
-
91
- # diff()为求差,mode()为获取众数
92
- # steps 为数据的时间间隔,单位为分钟
93
- steps = int(data['Datetime'].diff().mode()[0].seconds/60)
94
- difrain_name = 'pre_%dmin'%steps
95
- for nn in np.arange(0,data2.shape[0]-1):
96
-
97
- if data2['PRE_1h'][nn+1] >= data2['PRE_1h'][nn]:
98
- diff_rain.append(round(data2['PRE_1h'][nn+1]-data2['PRE_1h'][nn],2))
99
- else:
100
- diff_rain.append(round(data2['PRE_1h'][nn+1],2))
101
- newpd = pd.DataFrame(diff_rain,columns=[difrain_name],index=data2.index)
102
- # 如果间隔是1分钟,那么还需要求出5分钟的雨量
103
- if steps == 1:
104
- newpd['pre_5min'] = newpd[difrain_name].rolling(5).sum()
105
- newpd['pre_5min'] = newpd['pre_5min'].round(2)
106
- else:
107
- pass
108
- newindex = [tt for tt in newpd.index if tt.minute%5==0]
109
- newpd['accsum'] = newpd[difrain_name].cumsum()
110
- newpd['accsum'] = newpd['accsum'].round(2)
111
- newpd['PRE_1h'] = data2['PRE_1h']
112
- newpd['PRE'] = data2['PRE']
113
- newpd['lat'] = data2['lat']
114
- newpd['lon'] = data2['lon']
115
- newpd['staname']=data2['Station_Name']
116
- newpd.to_csv(outpath_sta + os.sep + outname_sta,index=True)
117
-
118
- # 第二步,从离自动站最近的探空站中获取探空数据,提取零度层高度和-20度层高度
119
- # 读取探空站信息,获取经纬度、站号等信息
120
- # tlogpinfo = pd.read_csv('../common/stationinfo_new/china_tlogp_stations_m3.txt',delimiter=r"\s+",skiprows=3,names=['stanum','lon','lat','alt','num2'])
121
-
122
- # tmpdata['stanum'].values[0] int
123
- # staid_tlogp = str(tmpdata['stanum'].values[0])
124
- tlogpdata = cmadaas_sounding_by_time(times='20230704000000')
125
- allstas = np.unique(tlogpdata['Station_Id_C'])
126
- alllons=[]
127
- alllats=[]
128
- #获取所有站点的经纬度信息
129
- for ts in allstas:
130
- tmpd = tlogpdata[tlogpdata['Station_Id_C']==ts]
131
- curlat = tmpd['Lat'].values[0]
132
- curlon = tmpd['Lon'].values[0]
133
- alllons.append(curlon)
134
- alllats.append(curlat)
135
-
136
- x,y= geotrans.geographic_to_cartesian_aeqd(lon=alllons,lat=alllats,lon_0=sta_lon,lat_0=sta_lat)
137
- dis = [math.sqrt(pow(x[k],2) + pow(y[k],2))/1000 for k in range(len(x))]
138
- flag = np.array(dis) <= min(dis)+0.1
139
- tmpts = allstas[flag]
140
- tmpdata = tlogpdata[tlogpdata['Station_Id_C']==tmpts[0]]
141
- tmpdata = tmpdata.sort_values(by='PRS_HWC',ascending=False)
142
- tmpdata.dropna(subset=['PRS_HWC'],inplace=True)
143
- tmpdata.to_csv(outpath_tlogp + os.sep + 'tlogp_%s.csv'%tmpts[0],index=False)
144
- #查找离零度层和-20度层最近的高度
145
- flag = np.array(abs(tmpdata['TEM'])) <= abs(tmpdata['TEM']).min()+0.01
146
- z0 = tmpdata['GPH'][flag].values[0]
147
- flag = np.array(abs(tmpdata['TEM']+20)) <= abs(tmpdata['TEM']+20).min()+0.01
148
- z20 = tmpdata['GPH'][flag].values[0]
149
-
150
- z0 = geopotential_to_height(z0*9.80665)
151
- z20 = geopotential_to_height(z20*9.80665)
152
-
153
- # 第三步,从本地文件读取swan数据
154
-
155
- # 第六步,绘制图形
156
- # 根据自动站数据和三维拼图数据构建VPR并绘制图形
157
-
158
- # 从三维拼图数据中获取格点的时间序列
159
- data = pd.read_csv(outpath_sta+os.sep+outname_sta,index_col=0)
160
- sta_lat = data['lat'][0]
161
- sta_lon = data['lon'][0]
162
- sta_name = data['staname'][0]
163
- startt = datetime.strptime(start_time,'%Y%m%d%H%M%S')
164
- endt = datetime.strptime(end_time,'%Y%m%d%H%M%S')
165
- curt = startt
166
- allref=[]
167
- grd_height = None
168
- file_times=[]
169
- while curt <= endt:
170
-
171
- # curname = 'mosaic_' + curt.strftime('%Y%m%d%H%M') + '.nc'
172
- curname = 'Z_OTHE_RADAMOSAIC_' + curt.strftime('%Y%m%d%H%M00') + '.bin.bz2'
173
- curmosaic_path = outpath_mosaic
174
- if not os.path.exists(curmosaic_path + os.sep + curname):
175
- print('file not exist: %s'%(curmosaic_path + os.sep + curname))
176
- curt += timedelta(minutes=6)
177
- continue
178
-
179
- ref = decode_swan(curmosaic_path,curname,scale=[0.5,-64])
180
- ref2 = ref.sel(lon=sta_lon,lat=sta_lat,method='nearest')['data'].values
181
- allref.append(ref2)
182
- file_times.append(curt)
183
-
184
- grd_height = ref['level'].values*1000
185
- grd_height = grd_height.astype('int')
186
- ref.close()
187
- curt += timedelta(minutes=6)
188
- print(curname + ' added!')
189
- allref = np.array(allref)
190
- allref = allref.reshape(allref.shape[0],allref.shape[2])
191
-
192
- alldata = MA.masked_array(allref, mask=allref==-9999)
193
-
194
- xrdata = xr.Dataset({
195
- 'dbz':(['z', 'time'], alldata.T, {'long name':'time-height dBZ'})},
196
- coords={'z':grd_height, 'time':file_times},
197
- attrs={'lat':sta_lat, 'lon':sta_lon})
198
-
199
- xrdata.to_netcdf(outpath_ref + os.sep + outname_ref)
200
- print('ref data success!')
201
-
202
- # 画图
203
-
204
- oridf = pd.read_csv(outpath_sta+os.sep+outname_sta)#,index_col=0
205
-
206
- oridf['Datetime'] = pd.to_datetime(oridf['Datetime'], format="%Y-%m-%d %H:%M:%S")
207
- oridf = oridf.set_index('Datetime')
208
- newindex = [tt for tt in oridf.index if tt.minute%5==0]
209
- df = oridf.loc[newindex]
210
- # df.set_index('staname',inplace=True)
211
- df.reset_index(inplace=True)
212
-
213
- #将缺失的时间进行插值处理
214
- helper = pd.DataFrame({'Datetime': pd.date_range(start=df['Datetime'].min(), end=df['Datetime'].max(),freq='300s')})
215
- newdf = pd.merge(df, helper, on='Datetime', how='outer').sort_values('Datetime')
216
- newdf['accsum'] = newdf['accsum'].interpolate(method='linear')
217
-
218
- # ref_colorfile='gr2_colors/default_BR_PUP1.pal'
219
- ref_colorfile = 'gr2_colors/BR_WDTB_Bright.pal'
220
- outdic= parse_pro(ref_colorfile)
221
- cmap=outdic['cmap']
222
- norm=outdic['norm']
223
- units=outdic['units']
224
- # from pyart.graph import common
225
- # cmapname = 'pyart_NWSRef'
226
- # cmap = common.parse_cmap(cmapname, 'reflectivity')
227
-
228
- data = xr.open_dataset(outpath_ref + os.sep + outname_ref)
229
-
230
- data = data.rolling(z=2, time=2, min_periods=1, center=True).mean()
231
-
232
- font_path = 'simsun.ttc'
233
- font_manager.fontManager.addfont(font_path)
234
- prop = font_manager.FontProperties(fname=font_path)
235
-
236
- plt.rcParams["font.size"] = 12
237
- plt.rcParams["font.sans-serif"] = prop.get_name()
238
- fig = plt.figure(figsize=(10,6))
239
- ax1 = fig.add_axes([0.1,0.18,0.8,0.3])#位置[左,下,右,上]
240
- ax3 = fig.add_axes([0.1,0.64,0.8,0.3])#位置[左,下,右,上]
241
- ax2 = ax3.twinx()
242
- ax1.minorticks_on()
243
-
244
-
245
- cb_ax = fig.add_axes([0.25, 0.08, 0.5, 0.03])
246
-
247
- # data.dbz.plot(ax=ax1,cmap=cmap,norm=norm,)
248
- grid_y,grid_x=np.meshgrid(np.arange(0,data.dbz.shape[1]),data.z.values,)
249
- pcm = ax1.pcolormesh(grid_y,grid_x,data.dbz,cmap=cmap,norm=norm)
250
-
251
- z0h = ax1.plot(np.arange(0,data.dbz.shape[1]),np.ones(data.dbz.shape[1])*z0,'y--',linewidth=2,label='0度层高度')
252
- z20h=ax1.plot(np.arange(0,data.dbz.shape[1]),np.ones(data.dbz.shape[1])*z20,'r--',linewidth=2,label='-20度层高度')
253
-
254
-
255
- ax1.legend(loc='upper right')
256
-
257
- ax1.tick_params(axis='x',which='minor',length=3,width=0.8,)
258
- ax1.tick_params(axis='x',which='major',length=5,width=1,)
259
- cb = plt.colorbar(mappable=pcm,cax=cb_ax,extend='both',ticks=np.arange(-10,85,5),orientation='horizontal')
260
- cb.set_label('反射率因子 dBZ',fontsize=12)
261
- ah1=ax2.plot(newdf['accsum'].values,'r',linewidth=2,label='累计雨量')
262
- ah2=ax3.bar(np.arange(0,newdf.shape[0]),newdf['pre_5min'],label='5分钟雨量')
263
- ax2.legend(loc='upper right')
264
- ax3.legend(loc='upper left')
265
- ax3.minorticks_on()
266
- ax3.tick_params(axis='x',which='minor',length=3,width=0.8,)
267
- ax3.tick_params(axis='x',which='major',length=5,width=1,)
268
- ax3.xaxis.set_minor_locator(MultipleLocator(1))
269
- ax1.set_xlim([0,data.dbz.shape[1]+1])
270
- ax2.set_xlim([0,newdf.shape[0]])
271
- ax1.grid('y')
272
- ax2.grid('y')
273
-
274
- ax1.set_ylabel('距离地面的高度(米)')
275
- ax1.set_title('单点回波强度时间-高度图(%.2f°N,%.2f°E)'%(data.lat,data.lon),fontsize=12)
276
- ax2.set_ylabel('累计雨量(毫米)',color='r')
277
- ax3.set_ylabel('5分钟雨量(毫米)',color=[59/255,117/255,175/255])
278
-
279
- tmpdate = pd.to_datetime(data.time)+ timedelta(hours=8)
280
- dates1 = [date.strftime('%H:%M') for date in tmpdate]
281
- dates1[0] = tmpdate.min().strftime('%H:%M \n%Y-%m-%d')
282
-
283
- datestr = dates1[::5]
284
- tmpd = tmpdate[::5]
285
- datestr[-1] = tmpd.max().strftime('%H:%M \n%Y-%m-%d')
286
-
287
- ax1.set_xticks(np.arange(0,data.time.shape[0],5))
288
- ax1.set_xticklabels(datestr)
289
-
290
- ax1.set_ylim([0,20000])
291
- ax1.set_yticks(np.arange(0,20000,2500))
292
-
293
- tmpdate = pd.to_datetime(newdf['Datetime'])+ timedelta(hours=8)
294
- dates2 = [date.strftime('%H:%M') for date in tmpdate]
295
- dates2[0] = tmpdate.min().strftime('%H:%M \n%Y-%m-%d')
296
- dates2[-1] = tmpdate.max().strftime('%H:%M \n%Y-%m-%d')
297
-
298
- ax2.set_xticks(np.arange(0,newdf.shape[0],6))
299
-
300
- ax2.set_xticklabels(dates2[::6])
301
- ax3.set_xlabel('时间(时:分,北京时)')
302
- ax2.set_title('5分钟雨量(柱)、自动站累积雨量(红实线), 站号:%s,%s(%.2f°N,%.2f°E)'%(staid,sta_name,sta_lat,sta_lon),fontsize=12)
303
- # plt.figtext(0.73,0.02,'国家气象中心-天气预报技术研发室制作',fontsize=10)
304
- # plt.savefig(outpath_pic + os.sep + outname_ref.replace('.nc','.png'))#,dpi=600
305
- # print(outpath_pic + os.sep + outname_ref.replace('.nc','.png') + ' saved!')
306
- plt.show()