Skip to content

Commit 06b3b41

Browse files
committed
Added scrit to plot soil moisture saturation
1 parent ce7fa9e commit 06b3b41

File tree

5 files changed

+125
-148
lines changed

5 files changed

+125
-148
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ plot_winds10m.ncl
2020
test.run
2121
utils.pyc
2222
plot_meteogram.ncl
23+
plot_soil_moisture.ncl

copy_data.run

+5-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ${python} plot_meteogram.py Hamburg Pisa Rome Milano Naples Brocken
6262

6363
ncftpput -R -v altervista icon_italy/meteograms meteogram_*
6464

65-
scripts=("plot_winter.py" "plot_cape.py" "plot_convergence.py" "plot_gph_t_500.py" "plot_gph_t_850.py" "plot_gph_thetae_850.py" "plot_hsnow.py" "plot_jetstream.py" "plot_pres_t2m_winds10m.py" "plot_rain_acc.py" "plot_rain_clouds.py" "plot_winds10m.py" "plot_vorticity.py")
65+
scripts=("plot_winter.py" "plot_cape.py" "plot_convergence.py" "plot_gph_t_500.py" "plot_gph_t_850.py" "plot_gph_thetae_850.py" "plot_hsnow.py" "plot_jetstream.py" "plot_pres_t2m_winds10m.py" "plot_rain_acc.py" "plot_rain_clouds.py" "plot_winds10m.py" "plot_vorticity.py" "plot_soil_moisture.py")
6666
projections=("euratl" "it" "de")
6767

6868
${parallel} -j 8 ${python} ::: "${scripts[@]}" ::: "${projections[@]}"
@@ -83,6 +83,7 @@ ncftpput -R -v altervista icon_forecasts/t_v_pres t_v_pres_*
8383
ncftpput -R -v altervista icon_forecasts/precip_acc precip_acc_*
8484
ncftpput -R -v altervista icon_forecasts/vort_850 vort_850_*
8585
ncftpput -R -v altervista icon_forecasts/winter winter_*
86+
ncftpput -R -v altervista icon_forecasts/soil_moisture soil_moisture_*
8687

8788
ncftpput -R -v altervista icon_italy/t_v_pres it/t_v_pres_*
8889
ncftpput -R -v altervista icon_italy/gph_t_850 it/gph_t_850_*
@@ -96,10 +97,10 @@ ncftpput -R -v altervista icon_italy/gph_thetae_850 it/gph_thetae_850_*
9697
#ncftpput -R -v altervista icon_italy/thetae_winds thetae_winds_*
9798
#ncftpput -R -v altervista icon_italy/vort_850 it/vort_850_*
9899
ncftpput -R -v altervista icon_italy/precip_acc it/precip_acc_*
99-
#ncftpput -R -v altervista icon_italy/soil_moisture it/soil_moisture_*
100100
ncftpput -R -v altervista icon_italy/winds_jet it/winds_jet_*
101101
ncftpput -R -v altervista icon_italy/vort_850 it/vort_850_*
102102
ncftpput -R -v altervista icon_italy/winter it/winter_*
103+
ncftpput -R -v altervista icon_italy/soil_moisture it/soil_moisture_*
103104

104105
ncftpput -R -v altervista icon_de/precip_clouds de/precip_clouds_*
105106
ncftpput -R -v altervista icon_de/hsnow de/hsnow_*
@@ -115,6 +116,8 @@ ncftpput -R -v altervista icon_de/precip_acc de/precip_acc_*
115116
ncftpput -R -v altervista icon_de/winds_jet de/winds_jet_*
116117
ncftpput -R -v altervista icon_de/vort_850 de/vort_850_*
117118
ncftpput -R -v altervista icon_de/winter de/winter_*
119+
ncftpput -R -v altervista icon_de/soil_moisture de/soil_moisture_*
120+
118121

119122
#Remove images locally
120123
rm *.png

plot_soil_moisture.ncl

-146
This file was deleted.

plot_soil_moisture.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
debug = False
2+
if not debug:
3+
import matplotlib
4+
matplotlib.use('Agg')
5+
6+
import matplotlib.pyplot as plt
7+
import xarray as xr
8+
import metpy.calc as mpcalc
9+
from metpy.units import units
10+
from glob import glob
11+
import numpy as np
12+
import pandas as pd
13+
from multiprocessing import Pool
14+
from functools import partial
15+
import os
16+
from utils import *
17+
import sys
18+
19+
# The one employed for the figure name when exported
20+
variable_name = 'soil_moisture'
21+
22+
print('Starting script to plot '+variable_name)
23+
24+
# Get the projection as system argument from the call so that we can
25+
# span multiple instances of this script outside
26+
if not sys.argv[1:]:
27+
print('Projection not defined, falling back to default (euratl, it, de)')
28+
projections = ['euratl','it','de']
29+
else:
30+
projections=sys.argv[1:]
31+
32+
def main():
33+
"""In the main function we basically read the files and prepare the variables to be plotted.
34+
This is not included in utils.py as it can change from case to case."""
35+
file = glob(input_file)
36+
print('Using file '+file[0])
37+
dset = xr.open_dataset(file[0])
38+
dset = dset.metpy.parse_cf()
39+
40+
saturation = xr.open_dataset('/home/mpim/m300382/icon_forecasts/soil_saturation.nc')['soil_saturation']
41+
42+
# Convert to normal soil moisture units
43+
depths = dset['W_SO'].depth.values
44+
depths[0]=depths[0]*2
45+
rho_w=1000.
46+
47+
w_so = dset['W_SO'].copy() # Otherwise it overwrites it
48+
49+
for i, depth in enumerate(depths):
50+
w_so[:,i,:,:] = dset['W_SO'][:,i,:,:]/(depth*rho_w)
51+
52+
# Expand the saturation matrix to have
53+
saturation = np.repeat(saturation.values[None,:,:], w_so.depth.shape[0], axis=0)
54+
saturation = np.repeat(saturation[None,:,:,:], w_so.time.shape[0], axis=0)
55+
w_so_sat = (w_so / saturation)*100.
56+
57+
# Fix weird points with ice/rock
58+
w_so_sat = w_so_sat.where(w_so!=0,0.)
59+
60+
lon, lat = get_coordinates(dset)
61+
lon2d, lat2d = np.meshgrid(lon, lat)
62+
63+
time = pd.to_datetime(dset.time.values)
64+
cum_hour=np.array((time-time[0]) / pd.Timedelta('1 hour')).astype("int")
65+
66+
levels_sm = np.arange(0, 100, 10.)
67+
68+
cmap = plt.get_cmap('terrain_r')
69+
70+
for projection in projections:# This works regardless if projections is either single value or array
71+
fig = plt.figure(figsize=(figsize_x, figsize_y))
72+
ax = plt.gca()
73+
m, x, y =get_projection(lon2d, lat2d, projection, labels=True)
74+
75+
# All the arguments that need to be passed to the plotting function
76+
args=dict(m=m, x=x, y=y, ax=ax, cmap=cmap,
77+
w_so_sat=w_so_sat, levels_sm=levels_sm,
78+
time=time, projection=projection, cum_hour=cum_hour)
79+
80+
print('Pre-processing finished, launching plotting scripts')
81+
if debug:
82+
plot_files(time[1:2], **args)
83+
else:
84+
# Parallelize the plotting by dividing into chunks and processes
85+
dates = chunks(time, chunks_size)
86+
plot_files_param=partial(plot_files, **args)
87+
p = Pool(processes)
88+
p.map(plot_files_param, dates)
89+
90+
def plot_files(dates, **args):
91+
# Using args we don't have to change the prototype function if we want to add other parameters!
92+
first = True
93+
for date in dates:
94+
# Find index in the original array to subset when plotting
95+
i = np.argmin(np.abs(date - args['time']))
96+
# Build the name of the output image
97+
filename = subfolder_images[args['projection']]+'/'+variable_name+'_%s.png' % args['cum_hour'][i]#date.strftime('%Y%m%d%H')#
98+
99+
cs = args['ax'].contourf(args['x'], args['y'], args['w_so_sat'][i,0], extend='both', cmap=args['cmap'],
100+
levels=args['levels_sm'])
101+
102+
an_fc = annotation_forecast(args['ax'],args['time'][i])
103+
an_var = annotation(args['ax'], 'Soil Moisture saturation' ,loc='lower left', fontsize=6)
104+
an_run = annotation_run(args['ax'], args['time'])
105+
106+
if first:
107+
plt.colorbar(cs, orientation='horizontal', label='Saturation [%]', pad=0.03, fraction=0.04)
108+
109+
if debug:
110+
plt.show(block=True)
111+
else:
112+
plt.savefig(filename, **options_savefig)
113+
114+
remove_collections([cs, an_fc, an_var, an_run])
115+
116+
first = False
117+
118+
if __name__ == "__main__":
119+
main()

soil_saturation.nc

5.52 MB
Binary file not shown.

0 commit comments

Comments
 (0)