1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| import numpy as np import xarray as xr import pandas as pd from netCDF4 import Dataset import matplotlib.pyplot as plt from matplotlib.cm import get_cmap
from wrf import (to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords, extract_vars, xy_to_ll, ll_to_xy)
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all"
SMALL_SIZE = 8 MEDIUM_SIZE = 10 BIGGER_SIZE = 24
plt.rcParams["figure.figsize"] = (10,8) plt.rc('font', size=BIGGER_SIZE) plt.rc('axes', titlesize=BIGGER_SIZE) plt.rc('axes', labelsize=BIGGER_SIZE) plt.rc('xtick', labelsize=BIGGER_SIZE) plt.rc('ytick', labelsize=BIGGER_SIZE) plt.rc('legend', fontsize=BIGGER_SIZE) plt.rc('figure', titlesize=BIGGER_SIZE)
plt.rcParams.update({'font.family':'sans-serif'}) plt.rcParams['font.sans-serif'] = ['DejaVu Sans']
rad2deg = 57.2957795
def get_station_info(statfile): stats = pd.read_csv(statfile, sep=',') stats = stats[['WMO_id','lat','lon']] stats = stats.dropna(axis=0) stats = stats.astype({'WMO_id':int}) return (stats)
def read_model_data(domloc, wrfdir, dom, statf, sdate, edate, utc): import glob from netCDF4 import Dataset wrfoutfiles = sorted(glob.glob('{0}/wrfout_{1}_*'.format(wrfdir,dom))) wrflist = [ Dataset(f) for f in wrfoutfiles ] from wrf import getvar, ALL_TIMES, interplevel, extract_times, to_np, ll_to_xy, extract_times wrftimes = extract_times(wrflist, timeidx=ALL_TIMES, method='cat')
times = [ str(s).split(':')[0] for s in wrftimes ] print(times) sdate = (datetime.strptime(sdate,'%Y-%m-%d %H:%M:%S')+timedelta(hours=-utc)).strftime('%Y-%m-%dT%H') edate = (datetime.strptime(edate,'%Y-%m-%d %H:%M:%S')+timedelta(hours=-utc)).strftime('%Y-%m-%dT%H') st = times.index(sdate) et = times.index(edate) + 1 print ('{0}: {1} - {2}'.format(domloc, st, et))
stats = get_station_info(statf) stats['ij'] = stats.apply(lambda row: np.array(ll_to_xy(wrflist[0], row.lat, row.lon).values), axis=1) stats['i'] = stats.apply(lambda row: row.ij[0], axis=1) stats['j'] = stats.apply(lambda row: row.ij[1], axis=1)
varlist = ["T2","Q2", "U10","V10"] df = pd.DataFrame() for varname in varlist: for tt in range(st,et): stats['varname'] = varname var = getvar(wrflist, varname, timeidx=tt, method="cat") if varname == 'T2': var = var - 273.15 stats['datetime'] = var.coords['Time'].values stats['value'] = stats.apply(lambda row: to_np(var[row.i, row.j]), axis=1) df =df.append(stats[['datetime','WMO_id','varname','value']]) df['datetime'] = df.apply(lambda row: (datetime.strptime(str(row.datetime),'%Y-%m-%d %H:%M:%S')+timedelta(hours=utc)).strftime("%Y-%m-%d %H:%M:%S"), axis=1) df['model'] = domloc return (df)
station = get_station_info("./station.txt") station
domloc = "ucm" wrfdir = "/xxx/wrf/" dom = "d02" statf = "./station.txt" sdate = "2023-03-31 21:00:00" edate = "2023-04-01 03:00:00" utc = 0
df = read_model_data(domloc, wrfdir, dom, statf, sdate, edate, utc) df
select_var = df[(df["varname"] == "T2") & (df["WMO_id"] == 1)] select_var
plt.plot(pd.to_datetime(select_var.datetime), select_var.value, 'ro--') plt.title("T2m") plt.ylabel("T2m (deg C)") plt.xlabel("Time (UTC)") plt.legend()
plt.xticks(rotation=45) plt.grid() plt.show() plt.close()
|