爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 37108|回复: 65

MeteoInfoLab脚本示例:绘制雷达反射率图

  [复制链接]

新浪微博达人勋

发表于 2018-3-30 10:14:48 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 MeteoInfo 于 2019-5-16 14:22 编辑

CINRAD SA雷达数据读取和绘图,程序里有具体标注。

  1. def degree_1km(lon, lat):
  2.     '''
  3.     Get degree per kilometer at a location.   
  4.     '''
  5.     dis_km = distance([lon,lon+1], [lat,lat], islonlat=True) / 1000
  6.     return 1 / dis_km

  7. #Location of radar (Longitude/Latitude)
  8. lon = 120.9586
  9. lat = 31.0747

  10. #Get degree per kilometer at the location
  11. deg_1km = degree_1km(lon, lat)

  12. #Read data
  13. fn = 'D:/Temp/binary/Z_RADR_I_Z9002_20180304192700_O_DOR_SA_CAP.bin'
  14. f = addfile(fn)
  15. scan = 1
  16. rf = f['Reflectivity'][scan,:,:]
  17. azi = f['azimuthR'][scan,:]
  18. dis = f['distanceR'][:]/1000.0
  19. ele = f['elevationR'][scan,:]

  20. #Get x/y (kilometers) coordinates of data
  21. e = radians(ele)
  22. azi = 90 - azi
  23. a = radians(azi)
  24. nr = rf.shape[0]
  25. nd = rf.shape[1]
  26. x = zeros((nr + 1, nd))
  27. y = zeros((nr + 1, nd))
  28. for j in xrange (len(e)):
  29.     x[j,:] = dis * cos(e[j]) * cos(a[j])
  30.     y[j,:] = dis * cos(e[j]) * sin(a[j])
  31. x[nr,:] = x[0,:]
  32. y[nr,:] = y[0,:]
  33. rf1 = zeros((nr + 1, nd))
  34. rf1[:nr,:nd] = rf
  35. rf1[nr,:] = rf[0,:]

  36. #km to degree
  37. x = x * deg_1km + lon
  38. y = y * deg_1km + lat

  39. #Plot        
  40. ax = axesm(bgcolor='b')
  41. #Add map layers
  42. geoshow('cn_province', edgecolor=None, facecolor=[230,230,230])
  43. geoshow('cn_province', edgecolor=[80,80,80])
  44. city = geoshow('cn_cities', facecolor='r', size=8)
  45. city.addlabels('NAME', fontname=u'黑体', fontsize=16, yoffset=18)
  46. #Plot radar reflectivity
  47. levs=[5,10,15,20,25,30,35,40,45,50,55,60,65,70]
  48. cols=[(255,255,255,0),(102,255,255),(0,162,232),(86,225,250),(3,207,14),\
  49.     (26,152,7),(255,242,0),(217,172,113),(255,147,74),(255,0,0),\
  50.     (204,0,0),(155,0,0),(236,21,236),(130,11,130),(184,108,208)]
  51. layer=pcolorm(x, y, rf1, levs, colors=cols, zorder=1)
  52. colorbar(layer, shrink=0.8, label='dBZ', labelloc='top')
  53. #Plot circles
  54. rr = array([50, 100, 150, 200])
  55. for r in rr:
  56.     rd = r * deg_1km
  57.     ax.add_circle((lon, lat), rd, edgecolor='r')
  58. geoshow([lat,lat], [lon-rd,lon+rd], color='r')
  59. geoshow([lat+rd,lat-rd], [lon,lon], color='r')
  60. #Set plot
  61. xlim(lon - rd, lon + rd)
  62. ylim(lat - rd, lat + rd)
  63. xaxis(tickin=False)
  64. xaxis(tickvisible=False, location='top')
  65. yaxis(tickin=False)
  66. yaxis(tickvisible=False, location='right')
  67. yticks(arange(29, 34, 1))
  68. title('Radar reflectivity')


radar_ref.png

上述脚本中x, y坐标都是2维数组,形成的是非矩形网格,用pcolor/pcolorm函数形成的是多边形图集,也就是说每个非矩形网格是一个多边形(Polygon),这样做虽然无需进行数据插值,但网格太多的话会影响绘图速度。另一种数据处理方式是先将非矩形网格点(当作散点对待)插值为矩形网格,然后再用imshow/imshowm函数生成图像进行显示。插值会需要一些时间,但图像的显示会快很多。数据插值处理的脚本:
  1. def degree_1km(lon, lat):
  2.     '''
  3.     Get degree per kilometer at a location.   
  4.     '''
  5.     dis_km = distance([lon,lon+1], [lat,lat], islonlat=True) / 1000
  6.     return 1 / dis_km

  7. #Location of radar (Longitude/Latitude)
  8. lon = 120.9586
  9. lat = 31.0747

  10. #Get degree per kilometer at the location
  11. deg_1km = degree_1km(lon, lat)

  12. #Read data
  13. fn = 'D:/Temp/binary/Z_RADR_I_Z9002_20180304192700_O_DOR_SA_CAP.bin'
  14. f=addfile(fn)
  15. scan = 1
  16. rf=f['Reflectivity'][scan,:,:]
  17. azi=f['azimuthR'][scan,:]
  18. dis=f['distanceR'][:]/1000.0
  19. ele=f['elevationR'][scan,:]

  20. #Get x/y (kilometers) coordinates of data
  21. e=radians(ele)
  22. azi = 90 - azi
  23. a=radians(azi)
  24. nr = rf.shape[0]
  25. nd = rf.shape[1]
  26. x=zeros(rf.shape)
  27. y=zeros(rf.shape)
  28. for j in xrange (len(e)):
  29.     x[j,:] = dis * cos(e[j]) * cos(a[j])
  30.     y[j,:] = dis * cos(e[j]) * sin(a[j])

  31. #km to degree
  32. x = x * deg_1km + lon
  33. y = y * deg_1km + lat

  34. #Interpolate to griddata
  35. rf,x,y = griddata((x,y), rf, method='nearest')
  36. #rf,x,y = griddata((x, y), rf, method='idw', pointnum=20)
  37. #rf,x,y = griddata((x, y), rf, method='idw', radius=0.1)

  38. #Plot        
  39. ax = axesm(bgcolor='b')
  40. #Add map layers
  41. geoshow('cn_province', edgecolor=None, facecolor=[230,230,230])
  42. geoshow('cn_province', edgecolor=[80,80,80])
  43. city = geoshow('cn_cities', facecolor='r', size=8)
  44. city.addlabels('NAME', fontname=u'黑体', fontsize=16, yoffset=18)
  45. #Plot radar reflectivity
  46. levs=[5,10,15,20,25,30,35,40,45,50,55,60,65,70]
  47. cols=[(255,255,255,0),(102,255,255),(0,162,232),(86,225,250),(3,207,14),\
  48.     (26,152,7),(255,242,0),(217,172,113),(255,147,74),(255,0,0),\
  49.     (204,0,0),(155,0,0),(236,21,236),(130,11,130),(184,108,208)]
  50. layer = imshowm(x, y, rf, levs, colors=cols, zorder=1)
  51. colorbar(layer, shrink=0.8, label='dBZ', labelloc='top')
  52. #Plot circles
  53. rr = array([50, 100, 150, 200])
  54. for r in rr:
  55.     rd = r * deg_1km
  56.     ax.add_circle((lon, lat), rd, edgecolor='r')
  57. geoshow([lat,lat], [lon-rd,lon+rd], color='r')
  58. geoshow([lat+rd,lat-rd], [lon,lon], color='r')
  59. #Set plot
  60. xlim(lon - rd, lon + rd)
  61. ylim(lat - rd, lat + rd)
  62. xaxis(tickin=False)
  63. xaxis(tickvisible=False, location='top')
  64. yaxis(tickin=False)
  65. yaxis(tickvisible=False, location='right')
  66. yticks(arange(29, 34, 1))
  67. title('Radar reflectivity')
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-3-30 11:14:08 | 显示全部楼层
来了,顶!!!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2018-3-30 11:46:22 | 显示全部楼层
赞!!!{:5_213:}{:5_213:}{:5_213:}
请问有径向速度没{:5_213:}{:5_213:}
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-3-30 12:20:49 | 显示全部楼层
本帖最后由 chongzika 于 2018-3-30 12:25 编辑

王老师我用的1.4.8版本的报错,是版本问题么?
1
Running Jython script...
mipylib is loaded...
Traceback (most recent call last):
  File "mete.py", line 30, in <module>
    x[i,:] = dis*cos(e)*cos(a)
  File "/data/c03n02/cliu/software/meteinfo/MeteoInfo-1.4.8/MeteoInfo/pylib/mipylib/numeric/miarray.py", line 221, in __setitem__
    r = ArrayMath.setSection(self.array, ranges, value)
TypeError: setSection(): 3rd arg can't be coerced to ucar.ma2.Array, java.lang.Number

密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-3-30 12:42:10 | 显示全部楼层
chongzika 发表于 2018-3-30 12:20
王老师我用的1.4.8版本的报错,是版本问题么?
1
Running Jython script...

需要用目前最新的版本
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-3-30 15:05:12 | 显示全部楼层
MeteoInfo 发表于 2018-3-30 12:42
需要用目前最新的版本
  1. #import os.path
  2. #import datetime
  3. import os
  4. def degree_1km(lon, lat):
  5.     '''
  6.     Get degree per kilometer at a location.   
  7.     '''
  8.     dis_km = distance([lon,lon+1], [lat,lat], islonlat=True) / 1000
  9.     return 1 / dis_km
  10. def file_name(file_dir):
  11.     for root, dirs, files in os.walk(file_dir):
  12.         return files
  13. datadir = 'data/'
  14. fns = []
  15. fns=file_name(datadir)

  16. #Location of radar (Longitude/Latitude)
  17. lon = 118.46
  18. lat = 32.03

  19. #Get degree per kilometer at the location
  20. deg_1km = degree_1km(lon, lat)

  21. #Read data
  22. #fn = 'Z_RADR_I_Z9250_20170610084000_O_DOR_SA_CAP.bin'
  23. #for i in xrange (len(fns)):
  24. for i in xrange (8):
  25.     fn=os.path.join(datadir,fns[i])
  26.     print(fn)
  27.     f=addfile(fn)
  28.     rf=f['Reflectivity'][1,:,:]
  29.     print(rf)
  30.     azi=f['azimuthR'][0,:]
  31.     dis=f['distanceR'][:]/1000.0
  32.     ele=f['elevationR'][0,:]
  33.    
  34.     #Get x/y (kilometers) coordinates of data
  35.     e=radians(ele)
  36.     azi = 45 - azi
  37.     a=radians(azi)
  38.     x=zeros(rf.shape)
  39.     y=zeros(rf.shape)
  40.     for i in xrange (len(e)):
  41.         x[i,:] = dis*cos(e[i])*cos(a[i])
  42.         y[i,:] = dis*cos(e[i])*sin(a[i])
  43.    
  44.     #km to degree
  45.     x = x * deg_1km + lon
  46.     y = y * deg_1km + lat
  47.    
  48.     #Plot        
  49.     ax = axesm(bgcolor='b')
  50.     #Add map layers
  51.     geoshow('cn_province', edgecolor=None, facecolor=[230,230,230])
  52.     geoshow('cn_province', edgecolor=[80,80,80])
  53.     city = geoshow('cn_cities', facecolor='r', size=8)
  54.     city.addlabels('NAME', fontname=u'黑体', fontsize=16, yoffset=15)
  55.     #Plot radar reflectivity
  56.     levs=[5,10,15,20,25,30,35,40,45,50,55,60,65,70]
  57.     cols=[(255,255,255,0),(102,255,255),(0,162,232),(86,225,250),(3,207,14),\
  58.         (26,152,7),(255,242,0),(217,172,113),(255,147,74),(255,0,0),\
  59.         (204,0,0),(155,0,0),(236,21,236),(130,11,130),(184,108,208)]
  60.     layer=pcolorm(x, y, rf, levs, colors=cols, order=1)
  61.     colorbar(layer, shrink=0.8, label='dBZ', labelloc='top')
  62.     #Plot circles
  63.     rr = array([50, 100, 150, 200])
  64.     for r in rr:
  65.         rd = r * deg_1km
  66.         ax.add_circle((lon, lat), rd, edgecolor='r')
  67.     geoshow([lat,lat], [lon-rd,lon+rd], color='r')
  68.     geoshow([lat+rd,lat-rd], [lon,lon], color='r')
  69.     #Set plot
  70.     xlim(lon - rd, lon + rd)
  71.     ylim(lat - rd, lat + rd)
  72.     xaxis(tickin=False)
  73.     xaxis(tickvisible=False, location='top')
  74.     yaxis(tickin=False)
  75.     yaxis(tickvisible=False, location='right')
  76.     yticks(arange(29, 34, 1))
  77.     title('Radar reflectivity')
  78.     #savefig('Z_RADR_2017-06-10_06_00.eps')
  79.     savefig(fn[19:27]+'png',600,400)
复制代码
王老师,您好,我照着您说的对应雷达数据的脚本写了个循环,一下子处理200多个文件,但一般到处理10个左右就不行了内存。每次还需要释放内存么?我不太懂这个,具体错误请看
data/Z_RADR_I_Z9250_20170610003600_O_DOR_SA_CAP.bin
array([[-33.0, 35.5, 46.5, 45.0, 44.0, 40.5, 35.0, 34.5, 33.5, 35.0, 33.5, 31.5, 39.0, 30.5, 26.5, 35.0, 38.0, 34.0, 27.0, 19.5, 24.5, 30.5, 24.0, 23.5, 21.0, 20.0, 21.5, 21.5, 26.5, 26.5, 23.5, 30.0, 38.5, 46.0, 47.0, 45.0, 44.5, 46.5, 45.5, 44.0, 46.0, 39.0, 36.5, 35.5, 30.5, 31.5, 37.0, 43.0, 40.5, 45.0, 42.0, 34.5, 34.5, 34.5, 34.0, 34.5, 38.0, 34.5, 37.0, 38.0, 38.5, 41.5, 33.5, 27.5, 24.0, 28.5, 28.5, 31.5, 28.5, 30.5, 27.5, 29.5, 31.5, 30.5, 34.5, 37.0, 42.0, 43.5, 39.5, 41.5, 44.0, 38.5, 40.0, 40.0, 37.0, 40.0, 36.0, 39.5, 42.0, 44.0, 42.5, 41.0, 40.5, 40.5, 42.5, 41.5, 43.5, 45.0, 44.5, 45.5, 44.5, 46.5, 43.5, 39.0, 41.5, 41.0, 39.5, 41.5, 39.5, 38.5, 40.5, 39.0, 40.5, 37.0, 35.0, 38.5, 43.0, 39.0, 36.0, 31.0, 30.5, 35.0, 38.0, 38.5, 40.0, 39.5, 41.0, 43.0, 41.5, 40.5, 41.0, 44.0, 44.0, 44.5, 42.5, 45.5, 43.5, 42.0, 42.5, 40.0, 39.0, 42.0, 45.5, 45.5, 45.5, 45.0, 45.0, 48.0, 47.5, 45.5, 43.5, 39.5, 39.5, 40.5, 40.5, 40.0, 36.5, 34.0, 32.5, 34.0, 34.0, 33.5, 32.5, 34.0, 34.0, 34.0, 30.5, 32.0, 30.5, 29.5, 29.0, 28.0, 29.0, 27.0, 27.0, 26.5, 27.5, 27.0, 24.5, 24.5, 23.5, 23.0, 23.0, 22.5, 23.0, 26.0, 29.0, 30.0, 27.5, 26.5, 24.5, 27.0, 28.0, 31.0, 27.5, 27.5, 24.5, 27.0, 28.5, 26.0, 27.0, ...]])
Traceback (most recent call last):
  File "meteuse.py", line 63, in <module>
    layer=pcolorm(x, y, rf, levs, colors=cols, order=1)
  File "/data/c03n02/cliu/software/meteinfo/MeteoInfo-1.4.8R7/MeteoInfo/pylib/mipylib/plotlib/miplot.py", line 5140, in pcolorm
    return gca.pcolor(*args, **kwargs)
  File "/data/c03n02/cliu/software/meteinfo/MeteoInfo-1.4.8R7/MeteoInfo/pylib/mipylib/plotlib/axes.py", line 540, in pcolor
    layer = ArrayUtil.meshLayer(x.asarray(), y.asarray(), a.asarray(), ls, lonlim)
        at org.meteoinfo.shape.PolygonShape.updatePolygons(PolygonShape.java:338)
        at org.meteoinfo.shape.PolygonShape.setPoints(PolygonShape.java:188)
        at org.meteoinfo.data.ArrayUtil.meshLayer(ArrayUtil.java:1907)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
java.lang.OutOfMemoryError: java.lang.OutOfMemoryError: GC overhead limit exceeded


密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-3-30 16:05:29 | 显示全部楼层
chongzika 发表于 2018-3-30 15:05
王老师,您好,我照着您说的对应雷达数据的脚本写了个循环,一下子处理200多个文件,但一般到处理10个左 ...

在51行后面加上cla()试试。cla是清除之前的axes
#Plot
cla()
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-3-30 18:20:42 | 显示全部楼层
MeteoInfo 发表于 2018-3-30 16:05
在51行后面加上cla()试试。cla是清除之前的axes
#Plot
cla()

可以了,多谢王老师
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-3-30 18:51:38 | 显示全部楼层
MeteoInfo 发表于 2018-3-30 16:05
在51行后面加上cla()试试。cla是清除之前的axes
#Plot
cla()

顺便问一下王老师,图中的汉字是自动绘制的,能不能改成汉语拼音呢
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-3-31 10:51:17 来自手机 | 显示全部楼层
厉害了           
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表