df=read_csv(fname)#if 'VI' in df.columns:sys.exit('VI already in file: '+fname)
col0=list(df.columns)col=[i[:3].lower()foriindf.columnsiflen(i)>=3]#in case of TWD97
if'lat'notincolormin(df[df.columns[0]])>360.:x,y=np.array(df[df.columns[0]])-Xcent,np.array(df[df.columns[1]])-Ycentlon,lat=pnyc(x,y,inverse=True)else:lon,lat=np.array(df[df.columns[0]]),np.array(df[df.columns[1]])x,y=pnyc(lon,lat,inverse=False)#UTC time
if'Title'incol0:ttl=np.array(df.Title)idx=np.where(ttl==0)[0]ntrj=len(idx)ends=[idx[i+1]foriinrange(ntrj-1)]+[len(df)]lngs=[ends[i]-idx[i]foriinrange(ntrj)]dd=[]dates=[bdate+datetime.timedelta(hours=t*BF-8)fortinrange(max(lngs))]forninrange(ntrj):dd+=[dates[i]foriinrange(lngs[n])]df['date']=ddnTail=ends[0]-1else:dates=[bdate+datetime.timedelta(hours=t*BF-8)fortinrange(len(df))]df['date']=datesnTail=len(df)
讀取wrfout檔案中的內容
fdate=dates[0].strftime('%Y-%m-%d')ifbh_wrf>0anddates[0].hour<bh_wrf:fdate=(dates[0]+datetime.timedelta(days=-1)).strftime('%Y-%m-%d')fn=path+head+fdate+tailnc=netCDF4.Dataset(fn,'r')V=[list(filter(lambdax:nc.variables[x].ndim==j,[iforiinnc.variables]))forjin[1,2,3,4]]if'PBLH'notinV[2]:sys.exit('PBLH not found in nc file: '+fn)forsinV[2]:exec(s+'=nc.variables["'+s+'"][:]')WS=np.sqrt(U10*U10+V10*V10)PBLH[np.where(np.isnan(PBLH))[:]]=35.PBLH[np.where(PBLH<35.)[:]]=35.VI=WS*PBLHnt,nrow,ncol=VI.shapestrT=[''.join([i.decode('utf-8')foriinnc.variables['Times'][t,:]])fortinrange(nt)]Times=[datetime.datetime.strptime(a,'%Y-%m-%d_%H:00:00')forainstrT]long,latg=XLONG[0,:,:].flatten(),XLAT[0,:,:].flatten()
ifdd==len(df)-1:continue#no need to openfile any more
ifbh_wrf==now.hourornow==dates[nTail]:ifnow==dates[nTail]:fdate=dates[0].strftime('%Y-%m-%d')ifbh_wrf>0anddates[0].hour<bh_wrf:fdate=(dates[0]+datetime.timedelta(days=-1)).strftime('%Y-%m-%d')fn=path+head+fdate+tailnc=netCDF4.Dataset(fn,'r')forsinV[2]:ifsinnc.variables:exec(s+'=nc.variables["'+s+'"][:]')WS=np.sqrt(U10*U10+V10*V10)PBLH[np.where(np.isnan(PBLH))[:]]=35.PBLH[np.where(PBLH<35.)[:]]=35.VI=WS*PBLHstrT=[''.join([i.decode('utf-8')foriinnc.variables['Times'][t,:]])fortinrange(nt)]Times=[datetime.datetime.strptime(a,'%Y-%m-%d_%H:00:00')forainstrT]