- 积分
- 55946
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
这里给出一个将GrADS数据文件转为netCDF数据文件的脚本示例程序,其它格式数据转netCDF可以参考:
- #-----------------------------------------------------
- # Author: Yaqiang Wang
- # Date: 2015-3-12
- # Purpose: Convert CUACE grads data to netCDF (CUACE/Dust)
- # Note: Sample
- #-----------------------------------------------------
- print 'Loading classes...'
- from org.meteoinfo.data import GridData
- from org.meteoinfo.data import DataMath
- from org.meteoinfo.data.meteodata import MeteoDataInfo
- from org.meteoinfo.geoprocess.analysis import ResampleMethods
- from org.meteoinfo.data.meteodata.netcdf import NetCDFDataInfo
- from org.meteoinfo.projection import ProjectionInfo
- from org.meteoinfo.projection import ProjectionNames
- from org.meteoinfo.projection import KnownCoordinateSystems
- from ucar.nc2 import NetcdfFileWriter
- from ucar.nc2 import Attribute
- from ucar.ma2 import DataType
- from ucar.ma2 import Array
- import os.path
- import jarray
- import datetime
- from java.util import Date
- from java.text import SimpleDateFormat
- #Set date
- year = 2014
- month = 4
- day = 23
- hour = 0
- sdate = datetime.datetime(year, month, day, hour)
- print sdate
- #Set directory
- dataDir = 'U:/data/cuace_dust/dust_example/2014_case'
- outDir = dataDir
- infn = os.path.join(dataDir, 'dust_post_'+ sdate.strftime('%Y%m%d%H') + '.ctl')
- outfn = os.path.join(dataDir, 'WMO_SDS-WAS_Asian_Center_Model_Forecasting_CUACE-Dust_CMA_' \
- + sdate.strftime('%Y-%m-%d') + '.nc')
- #Set output X/Y coordinates and projection
- toProjInfo = KnownCoordinateSystems.geographic.world.WGS1984
- sx = 70.0
- xnum = 161
- sy = 20
- ynum = 71
- delta = 0.5
- xlist = []
- ylist = []
- for i in range(0, xnum):
- xlist.append(sx + delta * i)
- for i in range(0, ynum):
- ylist.append(sy + delta * i)
- X = jarray.array(xlist, 'd')
- Y = jarray.array(ylist, 'd')
- #Read GrADS data file
- print 'Open GrADS data file...'
- mdi = MeteoDataInfo()
- mdi.openGrADSData(infn)
- dataInfo = mdi.getDataInfo()
- dataInfo.setBigEndian(True)
- fromProjInfo = mdi.getProjectionInfo()
- tnum = dataInfo.getTimeNum()
- mvalue = dataInfo.getMissingValue()
- #Set output nc data file
- print 'Create output NC file: ' + outfn
- ncfile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, outfn)
- #Add dimensions
- print 'Add dimensions...'
- xDim = ncfile.addDimension(None, 'lon', xnum)
- yDim = ncfile.addDimension(None, 'lat', ynum)
- tDim = ncfile.addDimension(None, 'time', tnum)
- #Add global attributes
- print 'Add global attributes...'
- ncfile.addGroupAttribute(None, Attribute('Conventions', 'CF-1.6'))
- ncfile.addGroupAttribute(None, Attribute('Title', 'Sand and dust storm forecasting'))
- ncfile.addGroupAttribute(None, Attribute('Model', 'CUACE/Dust'))
- ncfile.addGroupAttribute(None, Attribute('Center', 'WMO SDS-WAS Asian Center'))
- ncfile.addGroupAttribute(None, Attribute('Agency', 'China Meteorological Administration'))
- #Add variables
- xvar = ncfile.addVariable(None, 'lon', DataType.FLOAT, [xDim])
- xvar.addAttribute(Attribute('units', 'degrees_east'))
- xvar.addAttribute(Attribute('long_name', 'Longitude'))
- xvar.addAttribute(Attribute('standard_name', 'longitude'))
- xvar.addAttribute(Attribute('axis', 'X'))
- yvar = ncfile.addVariable(None, 'lat', DataType.FLOAT, [yDim])
- yvar.addAttribute(Attribute('units', 'degrees_north'))
- yvar.addAttribute(Attribute('long_name', 'Latitude'))
- yvar.addAttribute(Attribute('standard_name', 'latitude'))
- yvar.addAttribute(Attribute('axis', 'Y'))
- tvar = ncfile.addVariable(None, 'time', DataType.INT, [tDim])
- tvar.addAttribute(Attribute('units', 'hours since 1900-01-01 00:00:0.0'))
- tvar.addAttribute(Attribute('long_name', 'Time'))
- tvar.addAttribute(Attribute('standart_name', 'time'))
- tvar.addAttribute(Attribute('axis', 'T'))
- #Data variables
- vnames = ['load','con','dry','wet','aod']
- varlist = []
- var = ncfile.addVariable(None, 'LOAD_DUST', DataType.FLOAT, [tDim, yDim, xDim])
- var.addAttribute(Attribute('long_name', 'Dust load'))
- var.addAttribute(Attribute('units', 'kg/m2'))
- var.addAttribute(Attribute('missing_value', -9999.0))
- varlist.append(var)
- var = ncfile.addVariable(None, 'SCONC_DUST', DataType.FLOAT, [tDim, yDim, xDim])
- var.addAttribute(Attribute('long_name', 'Surface dust concentration'))
- var.addAttribute(Attribute('units', 'ug/m3'))
- var.addAttribute(Attribute('missing_value', -9999.0))
- varlist.append(var)
- var = ncfile.addVariable(None, 'DDEPO_DUST', DataType.FLOAT, [tDim, yDim, xDim])
- var.addAttribute(Attribute('long_name', '3-hour accumulated dry deposition'))
- var.addAttribute(Attribute('units', 'kg/m2'))
- var.addAttribute(Attribute('missing_value', -9999.0))
- varlist.append(var)
- var = ncfile.addVariable(None, 'WDEPO_DUST', DataType.FLOAT, [tDim, yDim, xDim])
- var.addAttribute(Attribute('long_name', '3-hour accumulated wet deposition'))
- var.addAttribute(Attribute('units', 'kg/m2'))
- var.addAttribute(Attribute('missing_value', -9999.0))
- varlist.append(var)
- var = ncfile.addVariable(None, 'AOD550_DUST', DataType.FLOAT, [tDim, yDim, xDim])
- var.addAttribute(Attribute('long_name', 'Dust optical depth at 550nm'))
- var.addAttribute(Attribute('units', '-'))
- var.addAttribute(Attribute('missing_value', -9999.0))
- varlist.append(var)
- #Write nc file
- ncfile.create()
- #Write x,y,z,t variables
- print 'Write x variable...'
- shape = jarray.array([xnum], 'i')
- data = Array.factory(DataType.FLOAT, shape)
- for i in range(0,xnum):
- data.set(i, X)
- ncfile.write(xvar, data)
- print 'Write y variable...'
- shape = jarray.array([ynum], 'i')
- data = Array.factory(DataType.FLOAT, shape)
- for i in range(0,ynum):
- data.set(i, Y)
- ncfile.write(yvar, data)
- print 'Write time variable...'
- format = SimpleDateFormat('yyyy-MM-dd')
- sdate = format.parse('1900-01-01')
- tvalues = dataInfo.getTimeValues(sdate, 'hours')
- shape = jarray.array([tnum], 'i')
- data = Array.factory(DataType.INT, shape)
- for i in range(0,tnum):
- data.set(i, tvalues)
- ncfile.write(tvar, data)
- #Write data variables
- print 'Write data variable...'
- for vname, var in zip(vnames, varlist):
- for t in range(0, tnum):
- print 'Time: ' + str(t + 1)
- mdi.setTimeIndex(t)
- gData = mdi.getGridData(vname)
- ngData = gData.project(fromProjInfo, toProjInfo, X, Y, ResampleMethods.Bilinear)
- origin = jarray.array([t, 0, 0], 'i')
- ncfile.write(var, origin, NetCDFDataInfo.gridToArray3D(ngData))
- #Close nc file
- ncfile.flush()
- ncfile.close()
- print 'Finished'
|
|