背景
- 這項作業有極高的危險性,如非必要,切勿隨意執行。
- 理論上WRF與MCIP之間不應該存在時間批次的差異,亦即WRF中的批次定義,應延續到MCIP以及CMAQ的模擬,才能有最符合氣象場之空品模擬。
- 除了有整併全月模擬的需求(add_xtime)之外,在每日覆蓋的模擬作業中,如果要在既有的WRF執行成果上,重作過去的個案,此時就會遇到此一尷尬的情況。選項有2:
- 重做WRF。但面臨GFS數據已經不存在,需另行下載FNL重作。
- 容忍不同批次間風場銜接的系統差異,在未被覆蓋的WRFOUT之間進行時間軸的串連整併。
- 本程式是在[2022-08-20-CMAQ_fcst]失敗時的相應措施
acc_dm.py
程式IO
- 作業目錄位置:/nas2/cmaqruns/2022fcst/grid09/wrfout
- 引數:DM (DM=1 ~ 3)
- wrfout檔名:’wrfout_d0’+DM+’_‘+i,i=0 ~ 5
程式設計
- 以第0天0時為累積起始點,重設當天逐時累積值。
- 前一天最後1小時之值為隔天0時之起始值,重設當天逐時累積值。
- 除了具有累積特性的物理量之外(acc),也要累積XTIME及ITIMESTEP等2個時間步階相關參數。
- 全域屬性
- nc.SIMULATION_START_DATE
- nc.START_DATE
- nc.JULYR
- nc.JULDAY
- nc.TITLE
程式碼
#!/opt/anaconda3/envs/pyn_env/bin/python
import netCDF4
import datetime
import numpy as np
import sys,os
x='XTIME'
y='ITIMESTEP'
#accumulation variables
acc=['ACGRDFLX', 'ACSNOM', 'RAINC', 'RAINSH', 'RAINNC', 'SNOWNC', 'GRAUPELNC', 'HAILNC', 'ACHFX', 'ACLHF']
#note acc should be saved and restored(if needed) before following actions:
# for dm in 1 2 4;do
# for i in $(ls wrfout_d0${dm}*);do d=$(echo $i|cut -d'_' -f3)
# ncks -O -v Times,ACGRDFLX,ACSNOM,RAINC,RAINSH,RAINNC,SNOWNC,GRAUPELNC,HAILNC,ACHFX,ACLHF $i $d.nc;done
# ncrcat -O 2016*.nc acc_d0${dm}.nc
# done
#each run must begin with same day(last day of previous month)
DM=sys.argv[1] #DM=1~3
fnames=['wrfout_d0'+DM+'_'+i for i in '012345']
#'wrfout_d0'+DM+'_'+begd.strftime("%Y-%m-%d")+'_00:00:00'
nc = netCDF4.Dataset(fnames[0],'r+')
V=[list(filter(lambda x:nc.variables[x].ndim==j, [i for i in nc.variables])) for j in [1,2,3,4]]
v='Times'
if v not in V[1]:sys.exit('not a wrf system file')
nt=len(nc[v][:,0])
strT=[''.join([i.decode('utf-8') for i in nc[v][t,:]]) for t in range(nt)]
# print(datetime.datetime.strptime(a,'%Y-%m-%d_%H:00:00'))
min0=nc.variables[x][-1]+60
nc.SIMULATION_START_DATE=strT[0]
START_DATE=nc.SIMULATION_START_DATE
JULYR=nc.JULYR
JULDAY=nc.JULDAY
if JULYR%4==0:
JULDAY=min(366,JULDAY)
else:
JULDAY=min(365,JULDAY)
TITLE =nc.TITLE
# begin with zero accumulation
acmx={ac:np.zeros(shape=nc.variables[ac].shape[1:]) for ac in acc}
for ac in acc:
var=np.zeros(shape=nc.variables[ac].shape)
var[:,:,:]=np.array(nc.variables[ac][0,:,:])[None,:,:]
nc.variables[ac][:,:,:]-=var[:,:,:]
acmx[ac]=nc.variables[ac][-1,:,:]
nc.close()
for DM in range(1,6):
fname=fnames[DM]
nc = netCDF4.Dataset(fname,'r+')
nt=len(nc[v][:,0])
for ac in acc:
var=np.zeros(shape=nc.variables[ac].shape)
var[:,:,:]=acmx[ac][None,:,:]-np.array(nc.variables[ac][0,:,:])[None,:,:]
nc.variables[ac][:]+=var[:]
acmx[ac]=nc.variables[ac][-1,:,:]
nc.SIMULATION_START_DATE=START_DATE
nc.START_DATE =START_DATE
nc.JULYR =JULYR
nc.JULDAY =JULDAY
nc.TITLE =TITLE
for t in range(nt):
mins=min0+((DM-2)*24+t)*60
nc.variables[x][t]=float(mins)
nc.variables[x].units='minutes since '+START_DATE
nc.variables[x].description='minutes since '+START_DATE
nc.variables[y][t]=int(mins)
nc.close()