""" WRF后处理程序 最临近网格 20230201 byp """ import netCDF4 import wrf import os import glob import re import math import matplotlib.pyplot as plt import matplotlib import cartopy.crs as ccrs import cartopy import numpy as np from matplotlib.cm import get_cmap from PIL import Image # 读取数据###############################################################开始 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) # 最新文件 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) lat, lon = (20, 110) x, y = wrf.ll_to_xy(data_nc, lat, lon) # print(xy.values[0], '/', xy.values[1]) x, y = (wrf.to_np(x), wrf.to_np(y)) print(x, '/', y) lao = wrf.xy_to_ll(data_nc, x, y) print(wrf.to_np(lao)) idx = 1 uv = wrf.getvar(data_nc, 'uvmet10', timeidx=idx) sd = wrf.getvar(data_nc, 'wspd_wdir10', timeidx=idx) lats_lons = wrf.latlon_coords(uv) print(wrf.to_np(lats_lons[1][0][x])) print(wrf.to_np(lats_lons[0][y][0])) print(lats_lons[1][0][x].values) print(lats_lons[0][y][0].values) print(uv[0][y][x].values) u_t = wrf.to_np(uv[0][y][x]) v_t = wrf.to_np(uv[1][y][x]) wind_speed = math.sqrt(u_t ** 2 + v_t ** 2) print(wind_speed) print(wrf.to_np(sd[0][y][x])) # print(wrf.to_np(lats_lons[0][y][0])) # print(wrf.to_np(lats_lons[1][0][x])) # print(wrf.to_np(uv[0][y][x])) # print(wrf.to_np(uv[1][0][x]))