-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostpro_icon_plot_VARS_diurnal.py
More file actions
143 lines (110 loc) · 5.78 KB
/
postpro_icon_plot_VARS_diurnal.py
File metadata and controls
143 lines (110 loc) · 5.78 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
import numpy as np
from netCDF4 import Dataset
import xarray as xr
import pandas as pd
from glob import glob
import os
import os.path
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib import colors
# Output directory
save_to_path =os.path.abspath('/hpc/uwork/extjroqu/bacy_plots/icon-nwp0_results/plots/daily_weight')
# It opens files, grid and extpar
varname = 'LHFL_S' # Choose the variable to work with ishf lhtfl
vn = "diurnal" # # "day" diurnal
timec = "diurnal" # "daily" diurnal
dr_data = "/hpc/uwork/extjroqu/bacy_plots/icon-nwp0_results/mean_data_cdo_hour/"
dr_extra = "/hpc/uhome/extjroqu/bacy_data/"
gridfile = Dataset(dr_extra+"griddir/icon_grid_9999_R13B07_L.nc") #3km eu domain
clon, clat = np.rad2deg( gridfile.variables["clon"]) , np.rad2deg( gridfile.variables["clat"]) #[::-1] )
if varname == "LHFL_S":
name_var = "lhtfl"
elif varname == "SHFL_S":
name_var = "ishf"
else:
print("No var")
# Different datasets in a list
dset2_lh = xr.open_dataset(dr_data+"mean_fc06_"+str(varname)+"_"+str(vn)+"_irri.nc")
dset1_lh = xr.open_dataset(dr_data+"mean_sat03_"+str(varname)+"_"+str(vn)+"_irri.nc")
dset3_lh = xr.open_dataset(dr_data+"mean_24h_11_"+str(varname)+"_"+str(vn)+"_irri.nc")
dset4_lh = xr.open_dataset(dr_data+"mean_24h_05_"+str(varname)+"_"+str(vn)+"_irri.nc")
dset5_lh = xr.open_dataset(dr_data+"mean_24h_02_"+str(varname)+"_"+str(vn)+"_irri.nc")
dset6_lh = xr.open_dataset(dr_data+"mean_ctrl_"+str(varname)+"_"+str(vn)+"_irri.nc")
dstd2_lh = xr.open_dataset(dr_data+"std_fc06_"+str(varname)+"_"+str(vn)+"_irri.nc")
dstd1_lh = xr.open_dataset(dr_data+"std_sat03_"+str(varname)+"_"+str(vn)+"_irri.nc")
dstd3_lh = xr.open_dataset(dr_data+"std_24h_11_"+str(varname)+"_"+str(vn)+"_irri.nc")
dstd4_lh = xr.open_dataset(dr_data+"std_24h_05_"+str(varname)+"_"+str(vn)+"_irri.nc")
dstd5_lh = xr.open_dataset(dr_data+"std_24h_02_"+str(varname)+"_"+str(vn)+"_irri.nc")
dstd6_lh = xr.open_dataset(dr_data+"std_ctrl_"+str(varname)+"_"+str(vn)+"_irri.nc")
# All in datasets together
#dsets_m = xr.concat(dsets, dim='time')
exp_size = 6
exp_coords = np.linspace(1, exp_size, exp_size)
time_x = dset1_lh.step.size #if time is 14 weeks
time_coords = dset1_lh.step.values #if time is 14 weeks
lh_mean = xr.zeros_like(xr.DataArray(np.empty([exp_size,time_x]), coords=([('exp', exp_coords),('time',time_coords)])))
lh_mean['time'] = dset1_lh.coords['step'].values
ds_list = [dset1_lh, dset2_lh, dset3_lh, dset4_lh, dset5_lh,dset6_lh]
for d in range(exp_size):
lh_mean[d,:] = ds_list[d][name_var]
lh_mean.attrs['units'] = dset1_lh[name_var].units
# Set var
var = lh_mean
var.attrs['GRIB_shortName'] = varname
var.attrs['units'] = dset1_lh[name_var].units
#df_w = pd.DataFrame(var, index=['SAT','FC','11.1 mm','6.7 mm','2.6 mm','CTRL'],
df_w = pd.DataFrame(var, index=['SAT','FC','MIT','MSP','MFR','CTRL'],
columns=["00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"])
# std df
lh_std = xr.zeros_like(xr.DataArray(np.empty([exp_size,time_x]), coords=([('exp', exp_coords),('time',time_coords)])))
lh_std['time'] = dset1_lh.coords['step'].values
ds_list = [dstd1_lh, dstd2_lh, dstd3_lh, dstd4_lh, dstd5_lh,dstd6_lh]
for d in range(exp_size):
lh_std[d,:] = ds_list[d][name_var]
lh_std.attrs['units'] = dset1_lh[name_var].units
# Set var
var_std = lh_std
var_std.attrs['GRIB_shortName'] = varname
var_std.attrs['units'] = dset1_lh[name_var].units
#df_w = pd.DataFrame(var, index=['SAT','FC','11.1 mm','6.7 mm','2.6 mm','CTRL'],
df_std = pd.DataFrame(var_std, index=['SAT','FC','MIT','MSP','MFR','CTRL'],
columns=["00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"])
####################### i_var has lon lat with irrigation grids #######################
# Plot the data
df_w_transposed = df_w.T
df_std_transposed = df_std.T
# Specify custom colors, second option colorblind friendly
#custom_colors = ['red', 'blue', 'purple','green', 'orange']
custom_colors = ["#D55E00", "#0072B2", "#984ea3","#009E73", "#E69F00","black"]
fig, ax = plt.subplots(figsize=(6, 4))
for column, color in zip(df_w_transposed.columns, custom_colors):
plt.plot(df_w_transposed.index, df_w_transposed[column], label=column, color=color)
for column, color in zip(df_std_transposed.columns, custom_colors):
ax.fill_between(df_std_transposed.index,
df_w_transposed[column] - df_std_transposed[column],
df_w_transposed[column] + df_std_transposed[column],
color=color, alpha=0.2)
plt.legend(bbox_to_anchor =(0, 0),loc='lower left')
plt.xticks(df_w_transposed.index[::2], rotation=45, ha='right', fontsize=11)
plt.ylabel(str(var.units), fontsize=11)
plt.tight_layout() #Remove excess of white space
plt.savefig(os.path.join(save_to_path,"Sim_merged_"+str(varname)+"_"+str(timec)+"_std.png"),dpi=300)
plt.close()
print("DONE plot irr mean for all exp "+str(timec)+" "+str(varname))
print("Variable MAX for "+str(varname)+": ",var.max(dim='time').values)
print("Variable MIN for "+str(varname)+": ",var.min(dim='time').values)
print("Variable MEAN for "+str(varname)+": ",var.mean(dim='time').values)
# No std in the plot
fig, ax = plt.subplots(figsize=(6, 4))
for column, color in zip(df_w_transposed.columns, custom_colors):
plt.plot(df_w_transposed.index, df_w_transposed[column], label=column, color=color)
plt.legend(bbox_to_anchor =(0, 0),loc='lower left')
plt.xticks(df_w_transposed.index[::2], rotation=45, ha='right', fontsize=11)
plt.ylabel(str(var.units), fontsize=11)
plt.tight_layout() #Remove excess of white space
plt.savefig(os.path.join(save_to_path,"Sim_merged_"+str(varname)+"_"+str(timec)+".png"),dpi=300)
plt.close()