- 积分
- 55975
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2012-12-19 11:43:39
|
显示全部楼层
njzqxt 发表于 2012-12-19 11:33
王老师,能不能把10楼转换的 py 脚本文件贴上来,我对照一下呢?谢谢了! - #--------------------------------------------------------
- # 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 = "D:\\Temp\\micaps\\ttadv\"
- #---- Open a MICAPS 4 Data file to get the information
- inDataInfo = MeteoDataInfo()
- infile = dataDir + "1000\\12121408.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, 400, 300, 200, 150, 100]
- levelNum = len(ls)
- levels = Array.CreateInstance(Double, levelNum)
- i = 0
- for l in ls:
- levels[i] = 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(2012, 12, 12, 8, 0, 0)
- for i in range(0, 8):
- times.append(t)
- delta = t - st
- hours = (delta.days * 86400 + delta.seconds) / 3600.0
- t = t + datetime.timedelta(hours=12)
- timeArray[i] = hours
- print timeArray[i]
-
- 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...!'
复制代码 |
|