Calculate SU: the number of Summer Days#
Example notebook that runs icclim.
The example calculates the number of summer days (SU indicator) for the dataset chosen by the user on C4I.
We assume to have the tas variable in netCDF files in a
./data/latest folder for model CMCC and for one member r1i1p1f1.The data can be downloaded using the metalink provided with this notebook.
The data described in a
.metalink file can be downloaded with tools such as aria2 or a browser plugin such as DownThemAll! If you wish to use a different dataset, you can use the climate 4 impact portal to search and select the data you wish to use and a metalink file to the ESGF data will be provided.The data is read using xarray and a plot of the time series over a specific region is generated, as well as an average spatial map. Several output types examples are shown.
To keep this example fast to run, the following period is considered: 2015-01-01 to 2019-12-31, and plots are shown over European region.
Installation and preparation of the needed modules#
[ ]:
[ ]:
Specification of the parameters#
[ ]:
# studied period
dt1 = datetime.datetime(2015, 1, 1, tzinfo=datetime.timezone.utc)
dt2 = datetime.datetime(2019, 12, 31, tzinfo=datetime.timezone.utc)
DATA_DIR = Path("./data/latest")
out_f = "su_icclim.nc"
filenames = [str(f) for f in DATA_DIR.glob("tas_day_CMCC*.nc")]
filenames
Compute Summer Days index (SU)#
Usually SU is computed on the maximum daily temperature (tasmax), but here we show that using var_name we can force icclim to use a different variable to compute indices, as long as its units is compatible.
[ ]:
icclim.index(
index_name="SU",
in_files=filenames,
var_name="tas",
slice_mode="JJA",
time_range=[dt1, dt2],
out_file=out_f,
logs_verbosity="HIGH",
)
Plot settings#
[ ]:
with xr.open_dataset(out_f, decode_times=False) as ds:
su_xr = ds
ds["time"] = xr.decode_cf(ds).time
# Select a single x,y combination from the data
longitude = su_xr["SU"]["lon"].sel(lon=3.5, method="nearest")
latitude = su_xr["SU"]["lat"].sel(lat=44.2, method="nearest")
[ ]:
su_xr.attrs["title"]
Note#
Notice that the title is not quite right in the resulting dataset.
SU assumes to be computed on tasmax, so its output title includes
maximum_air_temperature but here we used a air_temperature variable.Subset and Plot SU#
[ ]:
# Slice the data spatially using a single lat/lon point
one_point = su_xr["SU"].sel(lat=latitude, lon=longitude)
# Use xarray to create a quick time series plot
one_point.plot.line()
plt.show()
[ ]:
# You can clean up your plot as you wish using standard matplotlib approaches
f, ax = plt.subplots(figsize=(12, 6))
one_point.plot.line(
hue="lat",
marker="o",
ax=ax,
color="grey",
markerfacecolor="purple",
markeredgecolor="purple",
)
ax.set(title="Time Series For a Single Lat / Lon Location")
plt.show()
[ ]:
# Convert to dataframe -- then this can easily be exported to a csv
one_point_df = one_point.to_dataframe()
# View just the first 5 rows of the data
one_point_df.head()
# Export data to .csv file
[ ]:
# Time subsetting: this is just an example on how to do it
start_date = "2018-01-01"
end_date = "2019-12-31"
su = su_xr["SU"].sel(time=slice(start_date, end_date))
[ ]:
# Quickly plot the data using xarray.plot()
su.plot(x="lon", y="lat", col="time", col_wrap=1)
plt.suptitle("Two Time Steps of Summer Days", y=1.03)
plt.show()
[ ]:
# Set spatial extent and centre
central_lat = 47.0
central_lon = 1.0
extent = [-30, 30, 30, 56] # Western Europe
# Calculate time average
su_avg = su.mean(dim="time", keep_attrs=True)
# Set plot projection
map_proj = ccrs.AlbersEqualArea(
central_longitude=central_lon, central_latitude=central_lat
)
# Define plot
f, ax = plt.subplots(figsize=(14, 6), subplot_kw={"projection": map_proj})
# Plot data with proper colormap scale range
levels = np.arange(0, 90, 5)
p = su_avg.plot(levels=levels, cmap="RdBu_r", transform=ccrs.PlateCarree())
# Plot information
plt.suptitle("Two Time Steps of Europe Summer Days", y=1)
# Add the coastlines to axis and set extent
ax.coastlines()
ax.gridlines()
ax.set_extent(extent)
# Save plot as png
plt.savefig("c4i_su_icclim.png")
[ ]:
# Re-order longitude so that there is no blank line at 0 deg because 0 deg is within our spatial selection
su_avg.coords["lon"] = (su_avg.coords["lon"] + 180) % 360 - 180
su_avg = su_avg.sortby(su_avg.lon)
# Define plot
f, ax = plt.subplots(figsize=(14, 6), subplot_kw={"projection": map_proj})
# Define colorscale
levels = np.arange(0, 90, 15)
# Contours lines
p = su_avg.plot.contour(
levels=levels, colors="k", linewidths=0.5, transform=ccrs.PlateCarree()
)
# Contour filled colors
p = su_avg.plot.contourf(
levels=levels, cmap="RdBu_r", extend="both", transform=ccrs.PlateCarree()
)
# Plot information
plt.suptitle("Two Time Steps of Europe Summer Days", y=1)
# Add the coastlines to axis and set extent
ax.coastlines()
ax.gridlines()
ax.set_extent(extent)
# Save plot as png
plt.savefig("c4i_su_contours_icclim.png")
[ ]:
[ ]:
[ ]: