-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostpro_icon_plot_VARS_bowen.py
More file actions
132 lines (98 loc) · 5.32 KB
/
postpro_icon_plot_VARS_bowen.py
File metadata and controls
132 lines (98 loc) · 5.32 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
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 = 'Bowen_ratio'
varname2 = 'LHFL_S'
name_d = "Sim_Hourly_Mean" # "Sim_Mean" Sim_Hourly_Mean
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] )
# Different datasets in a list
dset2_lh = xr.open_dataset(dr_data+"mean_fc06_"+str(varname2)+"_"+str(vn)+"_irri.nc")
dset1_lh = xr.open_dataset(dr_data+"mean_sat03_"+str(varname2)+"_"+str(vn)+"_irri.nc")
dset3_lh = xr.open_dataset(dr_data+"mean_24h_11_"+str(varname2)+"_"+str(vn)+"_irri.nc")
dset4_lh = xr.open_dataset(dr_data+"mean_24h_05_"+str(varname2)+"_"+str(vn)+"_irri.nc")
dset5_lh = xr.open_dataset(dr_data+"mean_24h_02_"+str(varname2)+"_"+str(vn)+"_irri.nc")
dset6_lh = xr.open_dataset(dr_data+"mean_ctrl_"+str(varname2)+"_"+str(vn)+"_irri.nc")
dset2_sh = xr.open_dataset(dr_data+"mean_fc06_SHFL_S_"+str(vn)+"_irri.nc")
dset1_sh = xr.open_dataset(dr_data+"mean_sat03_SHFL_S_"+str(vn)+"_irri.nc")
dset3_sh = xr.open_dataset(dr_data+"mean_24h_11_SHFL_S_"+str(vn)+"_irri.nc")
dset4_sh = xr.open_dataset(dr_data+"mean_24h_05_SHFL_S_"+str(vn)+"_irri.nc")
dset5_sh = xr.open_dataset(dr_data+"mean_24h_02_SHFL_S_"+str(vn)+"_irri.nc")
dset6_sh = xr.open_dataset(dr_data+"mean_ctrl_SHFL_S_"+str(vn)+"_irri.nc")
# All in datasets together
exp_size = 6
exp_coords = np.linspace(1, exp_size, exp_size)
time_x = dset1_sh.step.size #if time is 14 weeks
time_coords = dset1_sh.step.values #if time is 14 weeks
sh_mean = xr.zeros_like(xr.DataArray(np.empty([exp_size,time_x]), coords=([('exp', exp_coords),('time',time_coords)])))
sh_mean['time'] = dset1_sh.coords['step'].values
ds_list = [dset1_sh, dset2_sh, dset3_sh, dset4_sh, dset5_sh,dset6_sh]
for d in range(exp_size):
sh_mean[d,:] = ds_list[d]['ishf']
sh_mean.attrs['units'] = dset1_sh['ishf'].units
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]['lhtfl']
lh_mean.attrs['units'] = dset1_lh['lhtfl'].units
# Calculate bowen ratio
var = np.true_divide(sh_mean,lh_mean)
var.attrs['GRIB_shortName'] = varname
var.attrs['units'] = dset1_lh['lhtfl'].units
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"])
####################### i_var has lon lat with irrigation grids #######################
# Plot the data
df_w_transposed = df_w.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=(10, 4))
fig, ax = plt.subplots(figsize=(6, 4)) # It looks better like this
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)
ax.axhline(y=1, color='grey', linestyle='--', label='_nolegend_')
plt.legend(bbox_to_anchor =(1, 1),loc='upper right')
plt.xticks(df_w_transposed.index[::2], rotation=45, ha='right', fontsize=11)
plt.ylabel(str(var.GRIB_shortName), 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()
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)
### Selecting some hours of the day ###
df_selected = df_w.loc[:, "04":"20"]
df_w_transposed = df_selected.T
custom_colors = ["#D55E00", "#0072B2", "#984ea3","#009E73", "#E69F00","black"]
fig, ax = plt.subplots(figsize=(10, 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)
ax.axhline(y=1, color='grey', linestyle='--', label='_nolegend_')
plt.legend(bbox_to_anchor =(1, 1),loc='upper right')
plt.xticks(df_w_transposed.index[::2], rotation=45, ha='right', fontsize=11)
plt.ylabel(str(var.GRIB_shortName), fontsize=11)
plt.tight_layout() #Remove excess of white space
plt.savefig(os.path.join(save_to_path,"Sim_merged_"+str(varname)+"_"+str(timec)+"_short.png"),dpi=300)
# Saving the df
df_combined = df_w.T
df_combined.to_csv(os.path.join(save_to_path,'bowen_'+varname+'_diurnal_all.txt'), sep="\t")