- 积分
- 3268
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2018-9-22
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2022-2-27 20:59:35
|
显示全部楼层
已解决,原因是垂直层颠倒了,手动颠倒后就能用了,顺便调整了各个变量的单位转换,能运行的代码如下,希望能给以后转换MERRA2的朋友参考
import os
datadir = '/Users/luoyh23/hysplit/data2arl/mer2arl'
prefix = 'MERRA2_100.tavg'
suffix = '.nc4'
yyyy = 1980
mon = 6
dd = 22
#转换事件发生前10天的数据
for day in range(dd-10,dd+1):
mm='%02d' % mon
daystr='%02d' % day
cdate = str(yyyy)+mm+daystr
outfn = os.path.join(datadir, 'mer2arl'+cdate+'.arl')
infn1 = os.path.join(datadir, prefix+'3_3d_asm_Nv.'+cdate+suffix)
infn2 = os.path.join(datadir, prefix+'1_2d_flx_Nx.'+cdate+suffix)
infn3 = os.path.join(datadir, prefix+'1_2d_slv_Nx.'+cdate+suffix)
infn4 = os.path.join(datadir, prefix+'1_2d_int_Nx.'+cdate+suffix)
inf1 = addfile(infn1)
inf2 = addfile(infn2)
inf3 = addfile(infn3)
inf4 = addfile(infn4)
arlf = addfile(outfn, 'c', dtype='arl')
conv2d = [0.102, 0.01, 0.06, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
wvar2d = ['PHIS','PS','PRECTOTCORR','PBLH','USTAR','Z0M','SWNETSRF','HFLUX','EFLUX','T2M','U10M','V10M']
avar2d = ['SHGT','PRSS','PRT3','PBLH','USTR','RGHS','DSWF','SHTF','LHTF','T02M','U10M','V10M']
conv3d = [0.01, 1.0, 1.0, 1.0, 0.01, 1.0]
wvar3d = ['PL','U','V','OMEGA','T','QV']
avar3d = ['PRES','UWND','VWND','WWND','TEMP','SPHU']
wv = inf1['PL']
nx = wv.dimlen(wv.ndim - 1)
ny = wv.dimlen(wv.ndim - 2)
levels = wv.dimvalue(wv.ndim - 3)[::-1]
nz = len(levels)
arlf.setlevels(levels)
arlf.set2dvar(avar2d)
for l in levels:
arlf.set3dvar(avar3d)
arlf.setx(wv.dimvalue(wv.ndim - 1))
arlf.sety(wv.dimvalue(wv.ndim - 2))
tNum1 = inf1.timenum()
tNum2 = inf2.timenum()
tNum3 = inf3.timenum()
tNum4 = inf4.timenum()
fhour = 3
#loop through each time step
for t in range(0, tNum1):
#print 'Time index: ' + str(t)
datamin = 90 + t*180
atime1 = inf1.gettime(t)
dhead = arlf.getdatahead(inf1.proj,'MER2', 2, 0)
arlf.writeindexrec(atime1, dhead)
#hourly data for flux vatiables
for k in range(0,tNum2):
atime2 = inf2.gettime(k)
if atime1==atime2 or atime2 > atime1:
iflx = k
break
#hourly data for 2d variables
for k in range(0,tNum3):
atime3 = inf3.gettime(k)
if atime1==atime3 or atime3 > atime1:
islv=k
iint=k
break
ksumlist = []
ksums = []
#extract 2d variables
for coeff,avname,wvname in zip(conv2d,avar2d, wvar2d):
if wvname in ['PS','T2M','U10M','V10M']:
gdata = coeff*inf3[wvname][islv,:,:]
elif wvname in ['SWNETSRF','HFLUX']:
gdata = coeff*inf4[wvname][iint,:,:]
elif wvname in ['PBLH','USTAR','Z0M','EFLUX']:
gdata = coeff*inf2[wvname][iflx,:,:]
elif wvname == 'PHIS':
gdata = coeff*inf1[wvname][t,:,:]
#special for PRT3
if wvname == 'PRECTOTCORR':
gdata = coeff*(inf2[wvname][iflx,:,:]+inf2[wvname][iflx-1,:,:]+inf2[wvname][iflx+1,:,:])/3.
ksum = arlf.writedatarec(atime1, 0, avname, fhour, 99, gdata)
ksums.append(ksum)
ksumlist.append(ksums)
#write 3d variables
for lidx0 in range(0, nz):
ksums = []
lidx = nz - lidx0 -1
for coeff,avname,wvname in zip(conv3d,avar3d, wvar3d):
gdata = coeff*inf1[wvname][t,lidx,:,:]
ksum = arlf.writedatarec(atime1, lidx + 1, avname, fhour, 99, gdata)
ksums.append(ksum)
ksumlist.append(ksums)
arlf.writeindexrec(atime1, dhead, ksumlist)
fhour += 1
arlf.close()
print 'Finished!'
|
|