""" WRF后处理程序 uv10 js 20230129 byp """ import netCDF4 import wrf import os import glob import re import math # 读取数据###############################################################开始 dir_name = "./data/WRF/" 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) idx = idx + 1 # Get the lat/lon coordinates lats, lons = wrf.latlon_coords(u) # 时间 Start_Date = str(data_nc.START_DATE).replace('_', '\/\/') Start_Date_idx = str(u.coords['Time'].values)[0:19].replace('T', '\/\/') Image_Name_idx = str(u.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 < 68: lons_t = lons.values[0:1][0][i] lats_t = lats.values[i][0] u_t = u.values[0:1][0][i] v_t = v.values[0:1][0][i] # 计算风速 wind_speed = math.sqrt(u_t ** 2 + v_t ** 2) # # f.write(lons.values[0:1][0][i]+'|'+lats.values[0:1][i][1]+'|') f.write('[' + str(lons_t) + ', ' + str(lats_t) + ', ' + str(u_t) + ', ' + str(v_t) + ', ' + str( wind_speed) + '], ') i = i + 1 if i == 68: f.write('[' + str(lons_t) + ', ' + str(lats_t) + ', ' + str(u_t) + ', ' + str(v_t) + ', ' + str( wind_speed) + ']') f.write(']') f.close()