- 积分
- 6313
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2013-1-15
- 最后登录
- 1970-1-1
|
发表于 2014-3-17 17:01:39
|
显示全部楼层
本帖最后由 wuwei2163 于 2014-3-17 17:15 编辑
MeteoInfo 发表于 2012-12-19 11:43
王老师,你好!最近我也遇到相似的问题,研究这个帖子也很久了,由于我的数据上时间和上面的不太一样,关于时间的设置还是不会改的,请求王老师帮帮忙,比较急用!!!
这是你之前编写的处理micaps多层次多时间数据的程序,请你根据我数据的时间告诉我怎么改时间设置的,麻烦您了
#--------------------------------------------------------
# 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\\R\\"
#---- Open a MICAPS 4 Data file to get the information
inDataInfo = MeteoDataInfo()
infile = dataDir + "1000\\14031708.000"
inDataInfo.OpenMICAPSData(infile)
undef = inDataInfo.UNDEF
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.UNDEF = 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, 8)
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=12)
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...!'
|
|