- 积分
- 5377
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2013-3-12
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2017-5-18 11:24:43
|
显示全部楼层
#-------------------------------------------------------- # Author: Yaqiang Wang
# Date: 2015-12-2
# Purpose: Convert WRF out netCDF data to ARL data
# Note: Sample
#-----------------------------------------------------------
#---- Set data folder
datadir = 'K:/wrfout'
#---- Set output data file
outfn = os.path.join(datadir, '20120728.arl')
if os.path.exists(outfn):
os.remove(outfn)
#---- Read a netCDF data file
infn = os.path.join(datadir, 'wrfout_d01_2012-07-28.nc')
print infn
inf = addfile(infn)
print 'NetCDF file has been opened...'
#---- Set output ARL data file
arlf = addfile(outfn, 'c', dtype='arl')
#---- Set variable and level list
wvar2d = ['HGT','PSFC','PBLH','UST','SWDOWN','HFX','LH','T2','U10','V10','RAINNC']
wvar3d = ['P','T','U','V','W','QVAPOR']
avar2d = ['SHGT','PRSS','PBLH','USTR','DSWF','SHTF','LHTF','T02M','U10M','V10M','TPPA']
avar3d = ['PRES','TEMP','UWND','VWND','WWND','SPHU']
wv = inf['P']
nx = wv.dimlen(wv.ndim - 1)
ny = wv.dimlen(wv.ndim - 2)
levels = wv.dimvalue(wv.ndim - 3)
nz = len(levels)
arlf.setlevels(levels)
arlf.set2dvar(avar2d)
for l in levels:
arlf.set3dvar(avar3d)
#---- Constant for poisson's equation
cp = 1004.0 # J/kg/K; specific heat
grav = 9.81 # m/s**2; gravity
rdry = 287.04 # J/kg/K; gas constant
rovcp = rdry / cp # constant for poisson's equation
#---- Write ARL data file
arlf.setx(wv.dimvalue(wv.ndim - 1))
arlf.sety(wv.dimvalue(wv.ndim - 2))
tNum = inf.timenum()
fhour = 0
for t in range(0, tNum):
print 'Time index: ' + str(t)
atime = inf.gettime(t)
print atime.strftime('%Y-%m-%d %H:00')
dhead = arlf.getdatahead(inf.proj, 'AWRF', 1, fhour)
#Pre-write index record without checksum - will be over-write latter
arlf.writeindexrec(atime, dhead)
#Checksum list
ksumlist = []
# Write 2d variables
ksums = []
for avname,wvname in zip(avar2d, wvar2d):
#print avname + ' ' + wvname
gdata = inf[wvname][t,:,:]
if avname == 'PRSS':
gdata = gdata * 0.01
elif avname == 'TPPA':
gdata = gdata * 0.001
ksum = arlf.writedatarec(atime, 0, avname, fhour, 99, gdata)
ksums.append(ksum)
ksumlist.append(ksums)
# Write 3d variables
for lidx in range(0, nz):
ksums = []
#print lidx
pp = inf['P'][t,lidx,:,:]
pb = inf['PB'][t,lidx,:,:]
pres = pp + pb
uwnd = inf['U'][t,lidx,:,:]
vwnd = inf['V'][t,lidx,:,:]
temp = inf['T'][t,lidx,:,:]
#potential to ambient temperature
temp = (temp + 300.) * (pres / 100000.) ** rovcp
sphu = inf['QVAPOR'][t,lidx,:,:]
wwnd = inf['W'][t,lidx+1,:,:]
#convert vertical velocity from m/s to hPa/s using omega = -W g rho
wwnd = -wwnd * grav * pres * 0.01 / (rdry * temp * (1.0 + 0.6077 * sphu))
pres = pres * 0.01
ksum = arlf.writedatarec(atime, lidx + 1, 'PRES', fhour, 99, pres)
ksums.append(ksum)
ksum = arlf.writedatarec(atime, lidx + 1, 'TEMP', fhour, 99, temp)
ksums.append(ksum)
ksum = arlf.writedatarec(atime, lidx + 1, 'UWND', fhour, 99, uwnd[:,:nx])
ksums.append(ksum)
ksum = arlf.writedatarec(atime, lidx + 1, 'VWND', fhour, 99, vwnd[:ny,:])
ksums.append(ksum)
ksum = arlf.writedatarec(atime, lidx + 1, 'WWND', fhour, 99, wwnd)
ksums.append(ksum)
ksum = arlf.writedatarec(atime, lidx + 1, 'SPHU', fhour, 99, sphu)
ksums.append(ksum)
ksumlist.append(ksums)
#Re-write index record with checksum
arlf.writeindexrec(atime, dhead, ksumlist)
fhour += 6
arlf.close()
print 'Finished!'
;这个程序只要改改路径和 fhour += 多少就行了 |
|