爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
12
返回列表 发新帖
楼主: wuwei2163

王老师,求助! 处理micaps数据中关于时间名的设置

[复制链接]

新浪微博达人勋

 楼主| 发表于 2014-3-18 14:09:20 | 显示全部楼层
本帖最后由 wuwei2163 于 2014-3-18 14:12 编辑

王老师,你看错了,你说的是第一个错误,我这是第二个错误啊,貌似没有定义文件名吧???
错误.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-3-18 14:23:34 | 显示全部楼层

图片被气象家园的logo给盖住了,看不清楚。
fileName改为FileName
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-3-18 14:55:39 | 显示全部楼层
王老师,在您的指导下,已经有点熟悉了,下面的这个错误是 定义数据类型时出错了,但不知道怎样改啊
错误.png
望王老师教我啊

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

新浪微博达人勋

发表于 2014-3-18 15:04:14 | 显示全部楼层
wuwei2163 发表于 2014-3-18 14:55
王老师,在您的指导下,已经有点熟悉了,下面的这个错误是 定义数据类型时出错了,但不知道怎样改啊

望王 ...

levels数组你是怎么定义赋值的?
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-3-18 16:36:54 | 显示全部楼层
本帖最后由 wuwei2163 于 2014-3-18 16:38 编辑
MeteoInfo 发表于 2014-3-18 15:04
levels数组你是怎么定义赋值的?

王老师,看你程序上定义的数组是Double,我levels上有7层,感觉定义整型和浮点型都可以的,但总是出错,现在被这个程序搞得头晕脑胀的,我把资料数据以及所有的代码贴出来,麻烦您看看,到底是哪里的问题???
# Author: Yaqiang Wang
# Date: 2012-12-11
# Purpose: Convert MICAPS 4 grid data to NetCDF data format
# Note: Sample
#-----------------------------------------------------------
import clr
clr.AddReferenceByPartialName("System.Windows.Forms")
clr.AddReferenceByPartialName("System.Drawing")
from System import *
from System.Windows.Forms import *
from System.Drawing import *
clr.AddReference("MeteoInfoC.dll")
from MeteoInfoC import *
from MeteoInfoC.Data import *
from MeteoInfoC.Data.MeteoData import *

import os.path
import datetime
import time

#---- Set data folder
dataDir = "y:\\ecmwf_thin\\R\\"

#---- Open a MICAPS 4 Data file to get the information
inDataInfo = MeteoDataInfo()
infile = dataDir + "1000\\14031708.000"
inDataInfo.OpenMICAPSData(infile)
undef = inDataInfo.MissingValue
lons = inDataInfo.GetX()
lats = inDataInfo.GetY()
lonNum = lons.Length
latNum = lats.Length
ls = [1000, 925, 850, 700, 500, 300, 100]
levelNum = len(ls)
levels = Array.CreateInstance(Double, levelNum)
i = 0
for l in ls:
        levels = l;
        i += 1

#---- Create netCDF data info object
outfilepath = "D:\\" + "test.nc"
outDataInfo = NetCDFDataInfo()
outDataInfo.FileName = outfilepath
outDataInfo.MissingValue = undef
outDataInfo.unlimdimid = 3

#---- Add dimensions: lon, lat, level, time
lonDim = outDataInfo.AddDimension("lon", lonNum)
latDim = outDataInfo.AddDimension("lat", latNum)
levelDim = outDataInfo.AddDimension("level", levelNum)
timeDim = outDataInfo.AddDimension("time", -1)

#---- Add variables
outDataInfo.AddVariable("lon", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([lonDim]))
outDataInfo.AddVariable("lat", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([latDim]))
outDataInfo.AddVariable("level", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([levelDim]))
outDataInfo.AddVariable("time", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([timeDim]))
outDataInfo.AddVariable("temadv", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([timeDim, levelDim, latDim, lonDim]))
#---- Add variable attributes
outDataInfo.AddVariableAttribute("lon", "units", "degrees_east")
outDataInfo.AddVariableAttribute("lon", "long_name", "longitude")
outDataInfo.AddVariableAttribute("lat", "units", "degrees_north")
outDataInfo.AddVariableAttribute("lat", "long_name", "latitude")
outDataInfo.AddVariableAttribute("level", "units", "level");
outDataInfo.AddVariableAttribute("level", "long_name", "level");
outDataInfo.AddVariableAttribute("level", "axis", "z");
outDataInfo.AddVariableAttribute("time", "units", "hours since 1800-1-1 00:00:00")
outDataInfo.AddVariableAttribute("time", "long_name", "time")
outDataInfo.AddVariableAttribute("temadv", "units", "No")
outDataInfo.AddVariableAttribute("temadv", "long_name", "TemperatureAdvection")
outDataInfo.AddVariableAttribute("temadv", "missing_value", undef)

#---- Add global attributes
outDataInfo.AddGlobalAttribute("title", "Temperature advection field data")
outDataInfo.AddGlobalAttribute("description", "script sample data")

#---- Create netCDF file
outDataInfo.CreateNCFile(outfilepath)

#---- Write lon, lat, level, time data
outDataInfo.WriteVar("lon", lons)
outDataInfo.WriteVar("lat", lats)
outDataInfo.WriteVar("level", levels)

timeArray = Array.CreateInstance(Double, 7)
times = []
st = datetime.datetime(1800, 1, 1, 0, 0, 0)
t = datetime.datetime(2014, 03, 17, 8, 0, 0)        
for i in range(0, 9):        
        times.append(t)
        delta = t - st
        hours = (delta.days * 86400 + delta.seconds) / 3600.0
        t = t + datetime.timedelta(hours=3)
        timeArray = hours
        print timeArray

startArray = Array.CreateInstance(Int, 1)
countArray = Array.CreateInstance(Int, 1)
startArray[0] = 0
countArray[0] = len(times)
outDataInfo.WriteVara("time", startArray, countArray, timeArray)

#---- Write data
tidx = 0
for t in times:
        lidx = 0
        for l in ls:
                infile = dataDir + str(l) + "\\" + t.strftime("%y%m%d%H") + ".000"
                print infile
                if os.path.isfile(infile):
                        print infile
                        inDataInfo.OpenMICAPSData(infile)
                        gdata = inDataInfo.GetGridData("var")
                        dataArray = gdata.ToOneDimData()        
                        startArray = Array.CreateInstance(int, 4)
                        countArray = Array.CreateInstance(int, 4)
                        startArray[0] = tidx
                        startArray[1] = lidx
                        startArray[2] = 0
                        startArray[3] = 0
                        countArray[0] = 1
                        countArray[1] = 1
                        countArray[2] = latDim.DimLength
                        countArray[3] = lonDim.DimLength
                        outDataInfo.WriteVara("temadv", startArray, countArray, dataArray)
                lidx += 1
        tidx += 1

#---- Close netCDF file
outDataInfo.CloseNCFile()

print 'Finished...!'

R.zip

413.06 KB, 下载次数: 1, 下载积分: 金钱 -5

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

新浪微博达人勋

发表于 2014-3-18 17:15:51 | 显示全部楼层
wuwei2163 发表于 2014-3-18 16:36
王老师,看你程序上定义的数组是Double,我levels上有7层,感觉定义整型和浮点型都可以的,但总是出错,现 ...

  1. #--------------------------------------------------------# Author: Yaqiang Wang
  2. # Date: 2014-3-18
  3. # Purpose: Convert MICAPS 4 grid data to NetCDF data format
  4. # Note: Sample
  5. #-----------------------------------------------------------
  6. import clr
  7. clr.AddReferenceByPartialName("System.Windows.Forms")
  8. clr.AddReferenceByPartialName("System.Drawing")
  9. from System import *
  10. from System.Windows.Forms import *
  11. from System.Drawing import *
  12. clr.AddReference("MeteoInfoC.dll")
  13. from MeteoInfoC import *
  14. from MeteoInfoC.Data import *
  15. from MeteoInfoC.Data.MeteoData import *

  16. import os.path
  17. import datetime
  18. import time

  19. #---- Set data folder
  20. dataDir = "D:\\Temp\\test\\R\\"

  21. #---- Open a MICAPS 4 Data file to get the information
  22. inDataInfo = MeteoDataInfo()
  23. infile = dataDir + "1000\\14031708.000.000"
  24. inDataInfo.OpenMICAPSData(infile)
  25. undef = inDataInfo.MissingValue
  26. lons = inDataInfo.GetX()
  27. lats = inDataInfo.GetY()
  28. lonNum = lons.Length
  29. latNum = lats.Length
  30. ls = [1000, 925, 850, 700, 500, 300, 100]
  31. levelNum = len(ls)
  32. levels = Array.CreateInstance(Double, levelNum)
  33. i = 0
  34. for l in ls:
  35.         levels = l;
  36.         i += 1

  37. #---- Create netCDF data info object
  38. outfilepath = dataDir + "test.nc"
  39. outDataInfo = NetCDFDataInfo()
  40. outDataInfo.FileName = outfilepath
  41. outDataInfo.MissingValue = undef
  42. outDataInfo.unlimdimid = 3

  43. #---- Add dimensions: lon, lat, level, time
  44. lonDim = outDataInfo.AddDimension("lon", lonNum)
  45. latDim = outDataInfo.AddDimension("lat", latNum)
  46. levelDim = outDataInfo.AddDimension("level", levelNum)
  47. timeDim = outDataInfo.AddDimension("time", -1)

  48. #---- Add variables
  49. outDataInfo.AddVariable("lon", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([lonDim]))
  50. outDataInfo.AddVariable("lat", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([latDim]))
  51. outDataInfo.AddVariable("level", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([levelDim]))
  52. outDataInfo.AddVariable("time", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([timeDim]))
  53. outDataInfo.AddVariable("temadv", NetCDF4.NcType.NC_DOUBLE, Array[Dimension]([timeDim, levelDim, latDim, lonDim]))
  54. #---- Add variable attributes
  55. outDataInfo.AddVariableAttribute("lon", "units", "degrees_east")
  56. outDataInfo.AddVariableAttribute("lon", "long_name", "longitude")
  57. outDataInfo.AddVariableAttribute("lat", "units", "degrees_north")
  58. outDataInfo.AddVariableAttribute("lat", "long_name", "latitude")
  59. outDataInfo.AddVariableAttribute("level", "units", "level");
  60. outDataInfo.AddVariableAttribute("level", "long_name", "level");
  61. outDataInfo.AddVariableAttribute("level", "axis", "z");
  62. outDataInfo.AddVariableAttribute("time", "units", "hours since 1800-1-1 00:00:00")
  63. outDataInfo.AddVariableAttribute("time", "long_name", "time")
  64. outDataInfo.AddVariableAttribute("temadv", "units", "No")
  65. outDataInfo.AddVariableAttribute("temadv", "long_name", "TemperatureAdvection")
  66. outDataInfo.AddVariableAttribute("temadv", "missing_value", undef)

  67. #---- Add global attributes
  68. outDataInfo.AddGlobalAttribute("title", "Temperature advection field data")
  69. outDataInfo.AddGlobalAttribute("description", "script sample data")

  70. #---- Create netCDF file
  71. outDataInfo.CreateNCFile(outfilepath)

  72. #---- Write lon, lat, level, time data
  73. outDataInfo.WriteVar("lon", lons)
  74. outDataInfo.WriteVar("lat", lats)
  75. outDataInfo.WriteVar("level", levels)

  76. tnum = 6
  77. timeArray = Array.CreateInstance(Double, tnum)
  78. times = []
  79. st = datetime.datetime(1800, 1, 1, 0, 0, 0)
  80. t = datetime.datetime(2014, 3, 17, 8, 0, 0)       
  81. for i in range(0, tnum):       
  82.         times.append(t)
  83.         delta = t - st
  84.         hours = (delta.days * 86400 + delta.seconds) / 3600.0
  85.         t = t + datetime.timedelta(hours=3)
  86.         timeArray = hours
  87.         print timeArray
  88.        
  89. startArray = Array.CreateInstance(int, 1)
  90. countArray = Array.CreateInstance(int, 1)
  91. startArray[0] = 0
  92. countArray[0] = len(times)
  93. outDataInfo.WriteVara("time", startArray, countArray, timeArray)

  94. #---- Write data
  95. tidx = 0
  96. st = times[0]
  97. for t in times:
  98.         lidx = 0       
  99.         for l in ls:
  100.                 tt = tidx * 3
  101.                 tstr = '.%03d'%tt
  102.                 infile = dataDir + str(l) + '\\' + st.strftime("%y%m%d%H") + tstr + tstr
  103.                 #print infile
  104.                 if os.path.isfile(infile):
  105.                         print infile
  106.                         inDataInfo.OpenMICAPSData(infile)
  107.                         gdata = inDataInfo.GetGridData("var")
  108.                         dataArray = gdata.ToOneDimData()       
  109.                         startArray = Array.CreateInstance(int, 4)
  110.                         countArray = Array.CreateInstance(int, 4)
  111.                         startArray[0] = tidx
  112.                         startArray[1] = lidx
  113.                         startArray[2] = 0
  114.                         startArray[3] = 0
  115.                         countArray[0] = 1
  116.                         countArray[1] = 1
  117.                         countArray[2] = latDim.DimLength
  118.                         countArray[3] = lonDim.DimLength
  119.                         outDataInfo.WriteVara("temadv", startArray, countArray, dataArray)
  120.                 lidx += 1
  121.         tidx += 1

  122. #---- Close netCDF file
  123. outDataInfo.CloseNCFile()

  124. print 'Finished...!'

评分

参与人数 1金钱 +8 体力 +100 收起 理由
lqouc + 8 + 100 太认真太贴心了,楷模啊

查看全部评分

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

新浪微博达人勋

 楼主| 发表于 2014-3-18 19:12:57 | 显示全部楼层
本帖最后由 wuwei2163 于 2014-3-18 19:14 编辑

王老师,谢谢您的耐心教导,根据你修改后的程序,可以运行处结果,但是出现了这样的两个错误:
第一个是 错误(1).png ,点击确定后,出现第二个 错误(2).png ,运行出的结果可以看出时间不对,而且结果大小只有2k多.......
论坛上也有人遇到这样的错误,更新了文件后据说可以运行结果,我先是更新个最新的MeteoInfoC.dll文件,结果还是同样的错误,然后我又卸掉了原来的版本,在论坛上下载了最新的1.1.3.0版本,还是不行的,王老师你看这是什么情况???

密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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