- 积分
- 55955
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 MeteoInfo 于 2017-4-3 11:11 编辑
convert2nc(infn, outfn)函数用来将GrADS数据转为netCDF格式数据(目前只支持等经纬度投影数据),第一个参数为欲转换的GrADS数据文件名,第二个参数为转换后的netCDF数据文件名。
脚本程序:
- infn = 'D:/Temp/grads/model.ctl' #Input GrADS file
- outfn = 'D:/Temp/grads/model.nc' #Output netCDF file
- convert2nc(infn, outfn)
也可以不用convert2nc函数,更加灵活的转换:
- #--------------------------------------------------------
- # Author: Yaqiang Wang
- # Date: 2016-4-12
- # Purpose: Convert GrADS data to netCDF data
- # Note: Sample
- #-----------------------------------------------------------
- infn = 'D:/Temp/grads/model.ctl' #Input GrADS file
- outfn = 'D:/Temp/grads/model.nc' #Output netCDF file
- #Open GrADS file
- f = addfile(infn)
- #New netCDF file
- ncfile = addfile(outfn, 'c')
- #Add dimensions
- dims = []
- for dim in f.dimensions():
- dims.append(ncfile.adddim(dim.getShortName(), dim.getLength()))
- xdim = f.finddim('X')
- ydim = f.finddim('Y')
- tdim = f.finddim('T')
- xnum = xdim.getLength()
- ynum = ydim.getLength()
- tnum = tdim.getLength()
- #Add global attributes
- ncfile.addgroupattr('Conventions', 'CF-1.6')
- for attr in f.attributes():
- ncfile.addgroupattr(attr.getName(), attr.getValues())
- #Add dimension variables
- dimvars = []
- for dim in dims:
- dname = dim.getShortName()
- if dname == 'T':
- var = ncfile.addvar('time', 'int', [dim])
- var.addattr('units', 'hours since 1900-01-01 00:00:0.0')
- var.addattr('long_name', 'Time')
- var.addattr('standard_name', 'time')
- var.addattr('axis', dname)
- tvar = var
- elif dname == 'Z':
- var = ncfile.addvar('level', 'float', [dim])
- var.addattr('axis', dname)
- else:
- var = ncfile.addvar(dim.getShortName(), 'float', [dim])
- if 'Z' in dname:
- var.addattr('axis', 'Z')
- else:
- var.addattr('axis', dname)
- dimvars.append(var)
- #Add variables
- variables = []
- for var in f.variables():
- print 'Variable: ' + var.getShortName()
- vdims = []
- for vdim in var.getDimensions():
- for dim in dims:
- if vdim.getShortName() == dim.getShortName():
- vdims.append(dim)
- #print vdims
- nvar = ncfile.addvar(var.getShortName(), var.getDataType(), vdims)
- nvar.addattr('fill_value', -9999.0)
- for attr in var.getAttributes():
- nvar.addattr(attr.getName(), attr.getValues())
- variables.append(nvar)
- #Create netCDF file
- ncfile.create()
- #Write variable data
- for dimvar, dim in zip(dimvars, f.dimensions()):
- if dim.getShortName() != 'T':
- ncfile.write(dimvar, array(dim.getDimValue()))
- sst = datetime.datetime(1900,1,1)
- for t in range(0, tnum):
- st = f.gettime(t)
- print st.strftime('%Y-%m-%d %H:00')
- hours = (st - sst).total_seconds() // 3600
- origin = [t]
- ncfile.write(tvar, array([hours]), origin=origin)
- for var in variables:
- print 'Variable: ' + var.name
- if var.ndim == 3:
- data = f[str(var.name)][t,:,:]
- data[data==nan] = -9999.0
- origin = [t, 0, 0]
- shape = [1, ynum, xnum]
- data = data.reshape(shape)
- ncfile.write(var, data, origin=origin)
- else:
- znum = var.dims[1].getLength()
- for z in range(0, znum):
- data = f[str(var.name)][t,z,:,:]
- data[data==nan] = -9999.0
- origin = [t, z, 0, 0]
- shape = [1, 1, ynum, xnum]
- data = data.reshape(shape)
- ncfile.write(var, data, origin=origin)
- #Close netCDF file
- ncfile.close()
- print 'Finished!'
|
|