前言
基于一个需求,希望根据用户输入一个起报时间、一个预报时间、一个时间间隔,读取对应的wrf模式数据进行绘图。举个例子如下:
起报时间为:071000预报时常为:24h时间间隔为:3h一次
实现函数:datetime.datetime
主要实现代码如下所示:
import sysimport datetimedate = sys.argv[1]date = str(date)frst = sys.argv[2]step = sys.argv[3]path = r'/Users/WRF/outdata/'+ date #只能是已经存在的文件目录且有数据才可以进行读取start = datetime.datetime.strptime(date,'%Y%m%d%H').strftime("%Y-%m-%d_%H:%M:%S")end = (datetime.datetime.strptime(date,'%Y%m%d%H')+datetime.timedelta(hours=int(frst))).strftime("%Y-%m-%d_%H:%M:%S")intp = (datetime.datetime.strptime(date,'%Y%m%d%H')+datetime.timedelta(hours=int(step))).strftime("%Y-%m-%d_%H:%M:%S")fstart = path+'/wrfout_d01_'+start+'*'fintp = path+'/wrfout_d01_'+intp+'*'fend = path+'/wrfout_d01_'+end+'*'file = path+'/*'filestart = glob.glob(fstart)fileintp = glob.glob(fintp)fileend = glob.glob(fend)filelist = glob.glob(file)filelist.sort() rstart = np.array(np.where(np.array(filelist)==filestart))[0][0]rintp = np.array(np.where(np.array(filelist)==fileintp))[0][0]rend = np.array(np.where(np.array(filelist)==fileend))[0][0]fn = filelist[rstart:rend:rintp]
最终,这个fn中就是我们所需要的数据文件名,下面进行循环读取即可。
如果觉得《根据用户输入的参数自动读取对应时间内的wrf模式输出数据》对你有帮助,请点赞、收藏,并留下你的观点哦!