""" WRF后处理程序 uv10 js 20230129 byp """ import netCDF4 import wrf import os import glob import re import math # 读取数据###############################################################开始 dir_name = "../wrfout/" dir_output = '../output/txt/' list_of_files = glob.glob(dir_name+'*') latest_file = max(list_of_files, key=os.path.getctime) # 最新文件 # print(latest_file) mat = re.search(r"(\d{4}-\d{1,2}-\d{1,2}_\d{1,2})", latest_file) # 正则匹配时间 dir_time_file = 'wrfout_d01_'+latest_file[mat.span()[0]:] dir_time = latest_file[mat.span()[0]:mat.span()[1]].replace('-', '').replace('_', '') data_nc = netCDF4.Dataset(dir_name+dir_time_file) print(dir_time) dir_output = dir_output+dir_time if not os.path.exists(dir_output): os.mkdir(dir_output) dir_output = dir_output+'/UV10' os.mkdir(dir_output) idx = 0 while idx < 73: # u = wrf.getvar(data_nc, 'U10', timeidx=idx) # v = wrf.getvar(data_nc, 'V10', timeidx=idx) uv = wrf.getvar(data_nc, 'uvmet10', timeidx=idx) sd = wrf.getvar(data_nc, 'wspd_wdir10', timeidx=idx) # sd10 = wrf.getvar(data_nc, 'uvmet10_wspd_wdir', timeidx=idx) u = uv.values[0] v = uv.values[1] wspd = sd.values[0] idx = idx + 1 # Get the lat/lon coordinates lats, lons = wrf.latlon_coords(sd) lats = lats.values lons = lons.values # 时间 Start_Date = str(data_nc.START_DATE).replace('_', '\/\/') Start_Date_idx = str(uv.coords['Time'].values)[0:19].replace('T', '\/\/') Image_Name_idx = str(uv.coords['Time'].values)[0:19].replace(':', '_') Image_Name_idx = Image_Name_idx.replace('-', '_') Image_Name_idx = Image_Name_idx.replace('T', '_') print(Image_Name_idx) file_name = dir_output + "/UV10_" + Image_Name_idx + ".js" f = open(file_name, "w") f.write('var d = [') i = 0 while i < 69: k = 0 while k < 69: f.write('[' + str(lons[i][k]) + ', ' + str(lats[i][k]) + ', ' + str(u[i][k]) + ', ' + str(v[i][k]) + ', ' + str( wspd[i][k]) + '], ') k = k + 1 i = i + 1 f.write(']') f.close()