Skip to content
Snippets Groups Projects
Commit 8f343de0 authored by Fabian Wachsmann's avatar Fabian Wachsmann
Browse files
parents 37b70176 28f77f87
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Count Annual Summer Days for a particular Location
%% Cell type:markdown id: tags:
In this Use Case you will learn how to access a data file from DKRZ's CMIP6 archive and calculate the annual summer of summer days for a particular location.\
In this Use Case you will learn how to access a data file from DKRZ's CMIP6 archive and count the annual number of summer days for a particular location.\
\
You will be using:
- [Intake](https://github.com/intake/intake) for finding the data the data
- [Xarray](http://xarray.pydata.org/en/stable/) for loading and processing the data
- [hvPlot](https://hvplot.holoviz.org/index.html) for visualizing the data
%% Cell type:code id: tags:
``` python
# get formating done automatically according to style `black`
%load_ext lab_black
#%load_ext lab_black
```
%% Cell type:code id: tags:
``` python
import intake
import requests
import folium
import xarray as xr
import matplotlib
from IPython.display import display
from ipywidgets import widgets
from geopy.geocoders import Nominatim
import numpy as np
import pandas as pd
import hvplot.pandas
```
%% Output
%% Cell type:markdown id: tags:
## Choose Location and Year
If ambiguous the more likely location will be chosen
<a id='selection'></a>
%% Cell type:code id: tags:
``` python
place = widgets.Text(description="Enter place:")
display(place)
x = range(1850, 2014)
year = widgets.Dropdown(
options=x,
description="Select year: ",
disabled=False,
)
display(year)
```
%% Output
%% Cell type:markdown id: tags:
### Check if your Selection is correct
%% Cell type:code id: tags:
``` python
geolocator = Nominatim(user_agent="any_agent")
location = geolocator.geocode(place.value)
m = folium.Map(location=[location.latitude, location.longitude])
tooltip = location.latitude, location.longitude
folium.Marker([location.latitude, location.longitude], tooltip=tooltip).add_to(m)
display(m)
print(location.address)
print((location.latitude, location.longitude))
print("Chosen year: " + str(year.value))
```
%% Cell type:markdown id: tags:
## Load Intake Catalogue
The catalog files are uploaded to the cloud. There is a gsv.gz file and a json file under
https://swiftbrowser.dkrz.de/public/dkrz_a44962e3ba914c309a7421573a6949a6/intake-esm/
We load the catalog descriptor with intake:
%% Cell type:code id: tags:
``` python
# col_url = "https://swift.dkrz.de/v1/dkrz_a44962e3ba914c309a7421573a6949a6/intake-esm/mistral-cmip6.json"
# or
col_url = "/work/ik1017/Catalogs/mistral-cmip6.json"
col = intake.open_esm_datastore(col_url)
```
%% Cell type:markdown id: tags:
Let's see what is in the intake catalog. The underlying data base is given as a panda dataframe which we can access with 'col.df'. col.df.head() shows us the first rows of the table:
%% Cell type:code id: tags:
``` python
col.df.head()
```
%% Cell type:markdown id: tags:
## Browse Intake Catalogue and open Variable
%% Cell type:code id: tags:
``` python
query = dict(
source_id="MPI-ESM1-2-HR",
variable_id="tasmax",
table_id="day",
experiment_id="historical",
member_id="r10i1p1f1",
)
cat = col.search(**query)
```
%% Cell type:markdown id: tags:
show query
%% Cell type:code id: tags:
``` python
cat.df
```
%% Cell type:markdown id: tags:
open selected year
%% Cell type:code id: tags:
``` python
query_result_df = cat.df
query_result_df["start_year"] = query_result_df["time_range"].str[0:4].astype(int)
query_result_df["end_year"] = query_result_df["time_range"].str[9:13].astype(int)
query_result_df["selection"] = (year.value - query_result_df["start_year"] >= 0) & (
year.value - query_result_df["end_year"] <= 0
)
query_result_df["path"].where(query_result_df["selection"] == True)
selected_path_index = query_result_df.loc[query_result_df["selection"] == True][
"path"
].index[0]
selected_path = query_result_df["path"][selected_path_index]
ds_tasmax = xr.open_dataset(selected_path)
tasmax_xr = ds_tasmax["tasmax"]
time_start = str(year.value) + "-01-01T12:00:00.000000000"
time_end = str(year.value) + "-12-31T12:00:00.000000000"
tasmax_year_xr = tasmax_xr.loc[time_start:time_end, :, :]
```
%% Cell type:code id: tags:
``` python
selected_path
```
%% Cell type:code id: tags:
``` python
tasmax_xr
```
%% Cell type:markdown id: tags:
## Compare Model Grid Cell with chosen Location
%% Cell type:code id: tags:
``` python
# Find nearest model coordinate
# First, find the index of the grid point nearest a specific lat/lon.
abslat = np.abs(tasmax_year_xr["lat"] - location.latitude)
abslon = np.abs(tasmax_year_xr["lon"] - location.longitude)
c = np.maximum(abslon, abslat)
([xloc], [yloc]) = np.where(c == np.min(c))
```
%% Cell type:markdown id: tags:
Draw Map
%% Cell type:code id: tags:
``` python
# draw map
m = folium.Map(location=[location.latitude, location.longitude], zoom_start=8)
tooltip = location.latitude, location.longitude
folium.Marker(
[tasmax_year_xr["lat"][yloc], tasmax_year_xr["lon"][xloc]],
tooltip=tooltip,
popup="Model Grid Cell Center",
).add_to(m)
folium.Marker(
[location.latitude, location.longitude],
tooltip=tooltip,
popup="Location selected by You",
).add_to(m)
# Define rectangle
rect_lat1 = (tasmax_year_xr["lat"][yloc - 1] + tasmax_year_xr["lat"][yloc]) / 2
rect_lon1 = (tasmax_year_xr["lon"][xloc - 1] + tasmax_year_xr["lon"][xloc]) / 2
rect_lat2 = (tasmax_year_xr["lat"][yloc + 1] + tasmax_year_xr["lat"][yloc]) / 2
rect_lon2 = (tasmax_year_xr["lon"][xloc + 1] + tasmax_year_xr["lon"][xloc]) / 2
#
folium.Rectangle(
bounds=[[rect_lat1, rect_lon1], [rect_lat2, rect_lon2]],
color="#ff7800",
fill=True,
fill_color="#ffff00",
fill_opacity=0.2,
).add_to(m)
m
```
%% Cell type:markdown id: tags:
Draw Map
%% Cell type:markdown id: tags:
## Draw Temperature Time Series and count Summer days
%% Cell type:code id: tags:
``` python
tasmax_year_place_xr = tasmax_year_xr[:, yloc, xloc] - 273.15
tasmax_year_place_df = pd.DataFrame(index = tasmax_year_place_xr['time'].values, columns = ['Temperature', 'Summer Day Threshold'])
#
tasmax_year_place_df.loc[:, 'Actual Temperature'] = tasmax_year_place_xr.values
tasmax_year_place_df.loc[:, 'Summer Day Threshold'] = 25
#test
```
%% Cell type:code id: tags:
``` python
tasmax_year_place_df.hvplot.line(y=['Actual Temperature', 'Summer Day Threshold'],
value_label='Temperature in °C', legend='bottom', title='Daily maximum Temperature near Surface for ' +place.value, height=500, width=620)
```
%% Cell type:code id: tags:
``` python
no_summer_days = tasmax_year_place_xr[tasmax_year_place_xr > 25].size
print("In " +str(year.value) +" " +place.value +" had " + str(no_summer_days)+" summer days.")
```
%% Cell type:markdown id: tags:
[Try another location and year](#selection)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment