- 积分
- 55952
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 MeteoInfo 于 2015-2-2 14:33 编辑
写NetCDF格式数据的基本步骤是:定义各种维(经度、纬度、高度、时间),定义变量(包括维变量和数据变量)以及变量的属性,定义全局属性,将各维变量的数据写入文件,将数据变量按照一定规则写入文件。
这里演示将多个MICAPS第4类数据文件写入一个NetCDF文件中,MICAPS第4类数据是简单的二维格点数据,通过读取不同高度、不同时间的数据文件形成4维数据,写入一个NetCDF文件。所需的MICAPS示例数据见此贴:MICAPS第四类格式数据转换的请教
http://bbs.06climate.com/forum.php?mod=viewthread&tid=11611&fromuid=106
需要更新MeteoInfo的最新文件(见置顶帖子)。
写出来的NC数据经过测试和原始MICAPS数据是吻合的。
详细脚本代码如下:
- #--------------------------------------------------------
- # 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...!'
|
评分
-
查看全部评分
|