- 积分
- 55955
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
发表于 2014-3-18 17:15:51
|
显示全部楼层
wuwei2163 发表于 2014-3-18 16:36
王老师,看你程序上定义的数组是Double,我levels上有7层,感觉定义整型和浮点型都可以的,但总是出错,现 ...
- #--------------------------------------------------------# Author: Yaqiang Wang
- # Date: 2014-3-18
- # 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 = "D:\\Temp\\test\\R\\"
- #---- Open a MICAPS 4 Data file to get the information
- inDataInfo = MeteoDataInfo()
- infile = dataDir + "1000\\14031708.000.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 = dataDir + "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)
- tnum = 6
- timeArray = Array.CreateInstance(Double, tnum)
- times = []
- st = datetime.datetime(1800, 1, 1, 0, 0, 0)
- t = datetime.datetime(2014, 3, 17, 8, 0, 0)
- for i in range(0, tnum):
- 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
- st = times[0]
- for t in times:
- lidx = 0
- for l in ls:
- tt = tidx * 3
- tstr = '.%03d'%tt
- infile = dataDir + str(l) + '\\' + st.strftime("%y%m%d%H") + tstr + tstr
- #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...!'
|
评分
-
查看全部评分
|