- 积分
- 55960
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 MeteoInfo 于 2015-12-3 23:56 编辑
HYSPLIT模式需要特定格式(ARL)的气象数据来驱动,这里给出了将wrf模式输出的netCDf格式数据转为ARL格式的示例脚本。ARL格式中变量的名称和单位都是固定的,很多变量需要经过单位转换再写入ARL数据文件中。脚本里有详细的注释。
脚本程序:
- #-------------------------------------------------------- # Author: Yaqiang Wang
- # Date: 2015-12-2
- # Purpose: Convert WRF out netCDF data to ARL data
- # Note: Sample
- #-----------------------------------------------------------
- #---- Set data folder
- datadir = 'E:/Temp/Yaqiang'
- #---- Set output data file
- outfn = os.path.join(datadir, 'test_wrfout1.arl')
- if os.path.exists(outfn):
- os.remove(outfn)
- #---- Read a netCDF data file
- infn = os.path.join(datadir, 'wrfout_d01_2009-06-08_00_00_00')
- 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!'
转换后的数据和HYSPLIT官方提供的arw2arl程序转换的结果是一致的。
|
|