Commit 0a95fda6 authored by Marco Kulüke's avatar Marco Kulüke
Browse files

calc day max with xarray

parent 34861fc1
......@@ -66,8 +66,8 @@
" '18:00','19:00','20:00',\n",
" '21:00','22:00','23:00'\n",
" ],\n",
" 'area' : '-40/-25/-40/-25',#'-40/-50/-25/-15', # North, West, South, East. Default: global\n",
" 'grid': ['3', '3'],\n",
" 'area' : '-40.5/-25.3/-40.5/-25.3',#'-40/-50/-25/-15', # North, West, South, East. Default: global\n",
"# 'grid': ['3', '3'],\n",
" \"format\": \"netcdf\",\n",
" }, \"download.nc\"\n",
")"
......@@ -88,14 +88,12 @@
"metadata": {},
"outputs": [],
"source": [
"t_xr['longitude']"
"t_xr['latitude']"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"t_xr.hvplot.quadmesh(\n",
" 'longitude', 'latitude', 't2m', projection=ccrs.Orthographic(-45, -20),\n",
......@@ -119,10 +117,8 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Find nearest model coordinate\n",
"location_latitude = -40.\n",
......@@ -149,7 +145,7 @@
"metadata": {},
"outputs": [],
"source": [
"t_xr = t_xr[:, yloc, xloc] -273.15"
"t_xr = t_xr -273.15"
]
},
{
......@@ -179,6 +175,15 @@
"tasmax_s = t_pd.resample('D').max()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tasmax_s"
]
},
{
"cell_type": "code",
"execution_count": null,
......
No preview for this file type
......@@ -45,7 +45,8 @@
"from geopy.geocoders import Nominatim\n",
"import numpy as np\n",
"import pandas as pd\n",
"import hvplot.pandas"
"import hvplot.pandas\n",
"import cdsapi"
]
},
{
......@@ -102,6 +103,157 @@
"print(\"Chosen year: \" + str(year.value))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download ERA5 Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"area_string = str(location.latitude) +'/' +str(location.longitude) +'/' +str(location.latitude) +'/' +str(location.longitude)\n",
"year_string = str(year.value)\n",
"year_string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"area_string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c = cdsapi.Client()\n",
"\n",
"c.retrieve(\n",
" 'reanalysis-era5-single-levels',\n",
" {\n",
" 'variable': '2m_temperature',\n",
" 'product_type': 'reanalysis',\n",
" 'year': year_string,\n",
" 'month':[\n",
" '01','02','03',\n",
" '04','05','06',\n",
" '07','08','09',\n",
" '10','11','12'\n",
" ],\n",
" 'day':[\n",
" '01','02','03',\n",
" '04','05','06',\n",
" '07','08','09',\n",
" '10','11','12',\n",
" '13','14','15',\n",
" '16','17','18',\n",
" '19','20','21',\n",
" '22','23','24',\n",
" '25','26','27',\n",
" '28','29','30',\n",
" '31'\n",
" ],\n",
" 'time':[\n",
" '00:00','01:00','02:00',\n",
" '03:00','04:00','05:00',\n",
" '06:00','07:00','08:00',\n",
" '09:00','10:00','11:00',\n",
" '12:00','13:00','14:00',\n",
" '15:00','16:00','17:00',\n",
" '18:00','19:00','20:00',\n",
" '21:00','22:00','23:00'\n",
" ],\n",
" 'area' : '50.0/8.0/55.0/12.0',#area_string, # North/ West/ South/ East. Default: global\n",
"# 'grid': ['3', '3'],\n",
" \"format\": \"netcdf\",\n",
" }, \"tmp_grid_box_size.nc\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'53.550341/10.000654/53.550341/10.000654'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_xr = xr.open_dataset(\"tmp_grid_box_size.nc\")\n",
"test_xr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Resample ERA5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t_xr = xr.open_dataset(\"tmp.nc\")['t2m']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t_xr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# alternative approach\n",
"\n",
"tdaymax_xr = t_xr.groupby('time.dayofyear').max()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t_s = pd.Series(t_xr[:,0,0].values, index=t_xr['time'].values)\n",
"tdaymax_s = t_s.resample('D').max()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tdaymax_xr[:, 0, 0].values -tdaymax_s.values"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -282,6 +434,11 @@
" tooltip=tooltip,\n",
" popup=\"Location selected by You\",\n",
").add_to(m)\n",
"folium.Marker(\n",
" [t_xr['latitude'], t_xr['longitude']],\n",
" tooltip=tooltip,\n",
" popup=\"Location selected by ERA5\",\n",
").add_to(m)\n",
"\n",
"\n",
"# Define rectangle\n",
......@@ -324,10 +481,11 @@
"tasmax_year_place_xr = tasmax_year_xr[:, yloc, xloc] - 273.15\n",
"tasmax_year_place_df = pd.DataFrame(index = tasmax_year_place_xr['time'].values, columns = ['Temperature', 'Summer Day Threshold'])\n",
"#\n",
"tasmax_year_place_df.loc[:, 'Actual Temperature'] = tasmax_year_place_xr.values\n",
"tasmax_year_place_df.loc[:, 'Model Temperature'] = tasmax_year_place_xr.values\n",
"tasmax_year_place_df.loc[:, 'Reanalysis Temperature'] = tdaymax_s.values -273.15\n",
"tasmax_year_place_df.loc[:, 'Summer Day Threshold'] = 25\n",
"\n",
"tasmax_year_place_df.hvplot.line(y=['Actual Temperature', 'Summer Day Threshold'], \n",
"tasmax_year_place_df.hvplot.line(y=['Model Temperature', 'Reanalysis Temperature', 'Summer Day Threshold'], \n",
" value_label='Temperature in °C', legend='bottom', title='Daily maximum Temperature near Surface for ' +place.value, height=500, width=620)"
]
},
......@@ -337,8 +495,10 @@
"metadata": {},
"outputs": [],
"source": [
"no_summer_days = tasmax_year_place_xr[tasmax_year_place_xr > 25].size\n",
"print(\"In \" +str(year.value) +\" \" +place.value +\" had \" + str(no_summer_days)+\" summer days.\")"
"no_summer_days_model = tasmax_year_place_xr[tasmax_year_place_xr > 25].size\n",
"no_summer_days_reanalysis = tdaymax_s[tdaymax_s -273.15 > 25].size\n",
"print(\"Model: In \" +str(year.value) +\" \" +place.value +\" had \" + str(no_summer_days_model)+\" summer days.\")\n",
"print(\"Reanalysis: In \" +str(year.value) +\" \" +place.value +\" had \" + str(no_summer_days_reanalysis)+\" summer days.\")"
]
},
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment