爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 4756|回复: 13

grib2转arl过程中遇到问题,求指点!

[复制链接]

新浪微博达人勋

发表于 2016-7-31 23:56:18 | 显示全部楼层 |阅读模式

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

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

x
  1. #--------------------------------------------------------
  2. # Author: Yaqiang Wang
  3. # Date: 2016-2-4
  4. # Purpose: Convert GRIB data to ARL data
  5. # Note: Sample
  6. #-----------------------------------------------------------
  7. #---- Set data folder
  8. datadir = 'D:/Temp'
  9. #---- Set output data file
  10. outfn = os.path.join(datadir, 'arl/fnl_20150218_06_00.grib2.arl')
  11. #if os.path.exists(outfn):
  12. # os.remove(outfn)
  13. #---- Read a GRIB data file
  14. infn = os.path.join(datadir, 'grib/fnl_20150218_06_00.grib2')
  15. print infn
  16. inf = addfile(infn)
  17. print 'GRIB data file has been opened...'
  18. #---- Set output ARL data file
  19. arlf = addfile(outfn, 'c', dtype='arl')
  20. #---- Set variable and level list
  21. gvar2d = ['Pressure_surface','Temperature_surface','u-component_of_wind_height_above_ground',\
  22. 'v-component_of_wind_height_above_ground']
  23. gvar3d = ['Geopotential_isobaric','Temperature_isobaric','Pressure_Vertical_velocity_isobaric',\
  24. 'u-component_of_wind_isobaric','v-component_of_wind_isobaric','Specific_humidity_isobaric']
  25. avar2d = ['PRSS','T02M','U10M','V10M']
  26. avar3d = ['HGTS','TEMP','WWND','UWND','VWND','SPHU']
  27. gv = inf['Geopotential_isobaric']
  28. nx = gv.dimlen(gv.ndim - 1)
  29. ny = gv.dimlen(gv.ndim - 2)
  30. levels = gv.dimvalue(gv.ndim - 3)[::-1]
  31. nz = len(levels)
  32. arlf.setlevels(levels)
  33. arlf.set2dvar(avar2d)
  34. for l in levels:
  35. arlf.set3dvar(avar3d)

  36. #---- Write ARL data file
  37. arlf.setx(gv.dimvalue(gv.ndim - 1))
  38. arlf.sety(gv.dimvalue(gv.ndim - 2))
  39. tNum = inf.timenum()
  40. fhour = 0
  41. for t in range(0, tNum):
  42. print 'Time index: ' + str(t)
  43. atime = inf.gettime(t)
  44. print atime.strftime('%Y-%m-%d %H:00')
  45. dhead = arlf.getdatahead(inf.proj, 'RSMC', 2, fhour)
  46. #Pre-write index record without checksum - will be over-write latter
  47. arlf.writeindexrec(atime, dhead)
  48. #Checksum list
  49. ksumlist = []
  50. # Write 2d variables
  51. ksums = []
  52. for avname,gvname in zip(avar2d, gvar2d):
  53. print avname + ' ' + gvname
  54. if avname == 'U10M' or avname == 'V10M':
  55. gdata = inf[gvname][t,0,:,:]
  56. else:
  57. gdata = inf[gvname][t,:,:]
  58. if avname == 'PRSS':
  59. gdata = gdata * 0.01
  60. ksum = arlf.writedatarec(atime, 0, avname, fhour, 99, gdata)
  61. ksums.append(ksum)
  62. ksumlist.append(ksums)
  63. # Write 3d variables
  64. for lidx in range(0, nz):
  65. ksums = []
  66. llidx = nz - lidx - 1
  67. print lidx
  68. print llidx
  69. for avname,gvname in zip(avar3d, gvar3d):
  70. print avname + ' ' + gvname
  71. gdata = inf[gvname][t,llidx,:,:]
  72. if avname == 'WWND':
  73. gdata = gdata * 0.01
  74. elif avname == 'SPHU':
  75. gdata = gdata * 1000.
  76. ksum = arlf.writedatarec(atime, lidx + 1, avname, fhour, 99, gdata)
  77. ksums.append(ksum)
  78. ksumlist.append(ksums)
  79. #Re-write index record with checksum
  80. arlf.writeindexrec(atime, dhead, ksumlist)
  81. fhour += 1
  82. arlf.close()
  83. print 'Finished!'
复制代码
王老师,我在进行grib2转arl过程中,使用您的脚本,出现如下错误,求指点!!!
>>> run script...
D:/Temp\grib/fnl_20150218_06_00.grib2
GRIB data file has been opened...
Traceback (most recent call last):
  File "<iostream>", line 28, in <module>
AttributeError: DimVariable instance has no attribute 'ndim'


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

新浪微博达人勋

发表于 2016-8-1 10:59:40 | 显示全部楼层
你先要看看你的数据里的变量名是什么,不一定和示例脚本中的一样
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-8-1 11:54:27 | 显示全部楼层
MeteoInfo 发表于 2016-8-1 10:59
你先要看看你的数据里的变量名是什么,不一定和示例脚本中的一样

王老师,昨天晚上我调试的时候看过文件名了!是一致的!附件的txt是用meteoinfo导出的文件信息!

grib2.txt

105.47 KB, 下载次数: 8, 下载积分: 金钱 -5

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

新浪微博达人勋

发表于 2016-8-1 12:03:14 | 显示全部楼层
长春市气象台 发表于 2016-8-1 11:54
王老师,昨天晚上我调试的时候看过文件名了!是一致的!附件的txt是用meteoinfo导出的文件信息!

你是“良辰”?数据里有“Geopotential_isobaric”变量吗?
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-8-1 12:11:57 | 显示全部楼层
MeteoInfo 发表于 2016-8-1 12:03
你是“良辰”?数据里有“Geopotential_isobaric”变量吗?

是我,王老师!我在单位值班,有的单位电脑!账号没切换!对不起,王老师,我看的不仔细,Geopotential_height_isobaric是这个名字!给王老师带来了无用的工作量!深表歉意!谢谢王老师!叩谢!!!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-8-1 15:25:16 | 显示全部楼层
MeteoInfo 发表于 2016-8-1 12:03
你是“良辰”?数据里有“Geopotential_isobaric”变量吗?

  1. <P> #--------------------------------------------------------
  2. # Author: Yaqiang Wang
  3. # Date: 2016-2-4
  4. # Purpose: Convert GRIB data to ARL data
  5. # Note: Sample
  6. #-----------------------------------------------------------
  7. #---- Set data folder
  8. datadir = 'D:/Temp'
  9. #---- Set output data file
  10. outfn = os.path.join(datadir, 'arl/fnl_20150218_06_00.grib2.arl')
  11. #if os.path.exists(outfn):
  12. #    os.remove(outfn)
  13. #---- Read a GRIB data file
  14. infn = os.path.join(datadir, 'grib/fnl_20150218_06_00.grib2')
  15. print infn
  16. inf = addfile(infn)
  17. print 'GRIB data file has been opened...'
  18. #---- Set output ARL data file
  19. arlf = addfile(outfn, 'c', dtype='arl')
  20. #---- Set variable and level list
  21. gvar2d = ['Pressure_surface','Temperature_surface','u-component_of_wind_height_above_ground',\
  22.     'v-component_of_wind_height_above_ground']
  23. gvar3d = ['Geopotential_height_isobaric','Temperature_isobaric',\
  24.     'u-component_of_wind_isobaric','v-component_of_wind_isobaric',\
  25.     'Vertical_velocity_pressure_isobaric','Relative_humidity_isobaric']
  26. avar2d = ['PRSS','T02M','U10M','V10M']
  27. avar3d = ['HGTS','TEMP','UWND','VWND','WWND','RELH']
  28. gv = inf['Geopotential_height_isobaric']
  29. nx = gv.dimlen(gv.ndim - 1)
  30. ny = gv.dimlen(gv.ndim - 2)
  31. levels = gv.dimvalue(gv.ndim - 3)[::-1]
  32. nz = len(levels)
  33. arlf.setlevels(levels)
  34. arlf.set2dvar(avar2d)
  35. for l in levels:
  36.     arlf.set3dvar(avar3d)</P>
  37. <P>#---- Write ARL data file
  38. arlf.setx(gv.dimvalue(gv.ndim - 1))
  39. arlf.sety(gv.dimvalue(gv.ndim - 2))
  40. tNum = inf.timenum()
  41. fhour = 0
  42. for t in range(0, tNum):
  43.     print 'Time index: ' + str(t)
  44.     atime = inf.gettime(t)
  45.     print atime.strftime('%Y-%m-%d %H:00')
  46.     dhead = arlf.getdatahead(inf.proj, 'RSMC', 2, fhour)
  47.     #Pre-write index record without checksum - will be over-write latter
  48.     arlf.writeindexrec(atime, dhead)
  49.     #Checksum list
  50.     ksumlist = []
  51.     # Write 2d variables
  52.     ksums = []
  53.     for avname,gvname in zip(avar2d, gvar2d):
  54.         print avname + ' ' + gvname
  55.         if avname == 'U10M' or avname == 'V10M':
  56.             gdata = inf[gvname][t,0,:,:]
  57.         else:
  58.             gdata = inf[gvname][t,:,:]
  59.         if avname == 'PRSS':
  60.             gdata = gdata * 0.01
  61.         ksum = arlf.writedatarec(atime, 0, avname, fhour, 99, gdata)
  62.         ksums.append(ksum)
  63.     ksumlist.append(ksums)
  64.     # Write 3d variables
  65.     for lidx in range(0, nz):
  66.         ksums = []
  67.         llidx = nz - lidx - 1
  68.         print lidx
  69.         print llidx
  70.         for avname,gvname in zip(avar3d, gvar3d):
  71.             print avname + ' ' + gvname
  72.             gdata = inf[gvname][t,llidx,:,:]   
  73.             if avname == 'WWND':
  74.                gdata = gdata * 0.01      
  75.             ksum = arlf.writedatarec(atime, lidx + 1, avname, fhour, 99, gdata)
  76.             ksums.append(ksum)
  77.         ksumlist.append(ksums)
  78.     #Re-write index record with checksum
  79.     arlf.writeindexrec(atime, dhead, ksumlist)
  80. fhour += 1
  81. arlf.close()
  82. print 'Finished!'</P>
复制代码
王老师,还要求助您!运行上面的脚本,出现下面的错误!我感觉是WWND Vertical_velocity_pressure_isobaric有问题,或是llidx = nz - lidx - 1货lidx有问题,或是缩进的问题!python是个小白,给老师添了好多麻烦!求指点~~~
另:如果我要加多个grib2数据怎么处理,就是t变量增加!
>>> run script...
D:/Temp\grib/fnl_20150218_06_00.grib2
GRIB data file has been opened...
Time index: 0
2015-02-18 06:00
PRSS Pressure_surface
T02M Temperature_surface
U10M u-component_of_wind_height_above_ground
V10M v-component_of_wind_height_above_ground
0
25
HGTS Geopotential_height_isobaric
TEMP Temperature_isobaric
UWND u-component_of_wind_isobaric
VWND v-component_of_wind_isobaric
WWND Vertical_velocity_pressure_isobaric
Index out of range!
Traceback (most recent call last):
  File "<iostream>", line 75, in <module>
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-8-1 15:40:28 | 显示全部楼层
良辰 发表于 2016-8-1 15:25
王老师,还要求助您!运行上面的脚本,出现下面的错误!我感觉是WWND Vertical_velocity_pressure_isobar ...

你看看你的数据里Vertical_velocity_pressure_isobaric有几层,是不是比其它变量层少一些。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-8-1 15:46:32 | 显示全部楼层
MeteoInfo 发表于 2016-8-1 15:40
你看看你的数据里Vertical_velocity_pressure_isobaric有几层,是不是比其它变量层少一些。

是的,其他要素26层!Vertical_velocity_pressure_isobaric有21层!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-8-1 15:52:30 | 显示全部楼层
良辰 发表于 2016-8-1 15:46
是的,其他要素26层!Vertical_velocity_pressure_isobaric有21层!

那只能在结果文件中写21层了。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-8-1 17:07:59 来自手机 | 显示全部楼层
好的,我改一下输入层次!谢谢
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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