Skip to content

Commit 01ddcb6

Browse files
committedDec 3, 2024
updated with new data access
1 parent f9ed51b commit 01ddcb6

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed
 

‎glofas-bangladesh-floods.ipynb

+51-41
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"The tutorial is structured as follows:\n",
3232
"\n",
3333
"0. (Install and import the required python packages)\n",
34-
"1. Download the data of interest from the C3S Climate Data Store (CDS);\n",
35-
"1. Explore the data with xarray, apply mask using the auxilary data, and produce demonstrative map plots;\n",
36-
"1. Compute and plot time-series of the June 2022 river discharge against the 10-year period;\n",
37-
"1. Compute and display a map of the number of days in June 2022 that the river discharge exceeded the\n",
34+
"1. Download the data of interest from the Early warning Data Store (EWDS);;\n",
35+
"2. Explore the data with xarray, apply mask using the auxilary data, and produce demonstrative map plots;\n",
36+
"3. Compute and plot time-series of the June 2022 river discharge against the 10-year period;\n",
37+
"4. Compute and display a map of the number of days in June 2022 that the river discharge exceeded the\n",
3838
" 90th percentile of the 10-year period.\n",
3939
"\n",
4040
"The steps and methods here serve as demonstrative examples and could be applied to other events and datasets,\n",
@@ -84,11 +84,11 @@
8484
"source": [
8585
"## Setup your environment\n",
8686
"\n",
87-
"Before we begin we must prepare our environment. This includes installing the Application Programming Interface (API) of the CDS, and importing the various python libraries that we will need.\n",
87+
"Before we begin we must prepare our environment. This includes installing the Application Programming Interface (API) of the EWDS, and importing the various python libraries that we will need.\n",
8888
"\n",
89-
"### Install CDS API\n",
89+
"### Install EWDS API\n",
9090
"\n",
91-
"To install the CDS API, run the following command. We use an exclamation mark to pass the command to the shell (not to the Python interpreter)."
91+
"To install the EWDS API, run the following command. We use an exclamation mark to pass the command to the shell (not to the Python interpreter)."
9292
]
9393
},
9494
{
@@ -97,7 +97,7 @@
9797
"metadata": {},
9898
"outputs": [],
9999
"source": [
100-
"!pip -q install cdsapi\n",
100+
"!pip -q install cdsapi #'cdsapi>=0.7.2'\n",
101101
"\n",
102102
"# Other requirements:\n",
103103
"!pip -q install cfgrib ecmwflibs\n",
@@ -112,14 +112,25 @@
112112
"source": [
113113
"### Import libraries\n",
114114
"\n",
115-
"We will be working with data in NetCDF format. To best handle this data we will use libraries for working with multidimensional arrays, in particular Xarray. We will also need libraries for plotting and viewing data, in this case we will use Matplotlib and Cartopy."
115+
"We will be working with data in Grib and Grib format. To best handle this data we will use libraries for working with multidimensional arrays, in particular Xarray. We will also need libraries for plotting and viewing data, in this case we will use Matplotlib and Cartopy."
116116
]
117117
},
118118
{
119119
"cell_type": "code",
120-
"execution_count": 2,
120+
"execution_count": null,
121121
"metadata": {},
122-
"outputs": [],
122+
"outputs": [
123+
{
124+
"ename": "",
125+
"evalue": "",
126+
"output_type": "error",
127+
"traceback": [
128+
"\u001b[1;31mRunning cells with 'Python 3.12.3' requires the ipykernel package.\n",
129+
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
130+
"\u001b[1;31mCommand: '/bin/python3.12 -m pip install ipykernel -U --user --force-reinstall'"
131+
]
132+
}
133+
],
123134
"source": [
124135
"import os\n",
125136
"\n",
@@ -148,11 +159,11 @@
148159
"metadata": {},
149160
"source": [
150161
"## Search and download the data\n",
151-
"### Enter your CDS API key\n",
162+
"### Enter your EWDS API key\n",
152163
"\n",
153-
"We will request data from the Climate Data Store (CDS) programmatically with the help of the CDS API.\n",
164+
"We will request data from the Early Warning Data Store (EWDS) programmatically with the help of the EWDS API.\n",
154165
"\n",
155-
"If you have not set up your CDS API credentials with a `~/.cdsapirc` file, it is possible to provide the credentials when initialising the `cdsapi.Client`. To do this we must define the two variables needed to establish a connection: `URL` and `KEY` . The `URL` for the cds api is common and you do not need to modify that field. The `KEY` is string of characters made up of your your personal User ID and CDS API key. To obtain these, first register or login to the CDS (https://cds-beta.climate.copernicus.eu/), then visit https://cds-beta.climate.copernicus.eu/how-to-api and copy the string of characters listed after \"key:\". Replace the `#########` below with this string.\n",
166+
"If you have not set up your EWDS API credentials with a `~/.cdsapirc` file, it is possible to provide the credentials when initialising the `cdsapi.Client`. To do this we must define the two variables needed to establish a connection: `URL` and `KEY` . The `URL` for the EWDS api is common and you do not need to modify that field. The `KEY` is string of characters made up of your your personal User ID and EWDS API key. To obtain these, first register or login to the EWDS (https://ewds.climate.copernicus.eu/), then visit https://ewds.climate.copernicus.eu/how-to-api and copy the string of characters listed after \"key:\". Replace the `#########` below with this string.\n",
156167
"\n",
157168
"NOTE: If you have set up your cdsapi key using a ~/.cdsapirc you do not need to add your key below."
158169
]
@@ -166,7 +177,7 @@
166177
"if os.path.isfile(\"~/.cdsapirc\"):\n",
167178
" cdsapi_kwargs = {}\n",
168179
"else:\n",
169-
" URL = 'https://cds-beta.climate.copernicus.eu/api'\n",
180+
" URL = 'https://ewds.climate.copernicus.eu/api'\n",
170181
" KEY = '##################################'\n",
171182
" cdsapi_kwargs = {\n",
172183
" 'url': URL,\n",
@@ -200,7 +211,7 @@
200211
"### Find the river discharge data we want\n",
201212
"\n",
202213
"The historical GLOFAS data can be found in \n",
203-
"[CDS catalogue entry](https://cds.climate.copernicus.eu/cdsapp#!/dataset/cems-glofas-historical).\n",
214+
"[EWDS catalogue entry](https://ewds.climate.copernicus.eu/datasets).\n",
204215
"\n",
205216
"We are going to look at the Bangladesh flood in June 2022. For our analysis we are going to compare June 2022\n",
206217
"against all the June data from the 10-year reference period: 2012 to 2021. This period has been chosen for\n",
@@ -220,16 +231,17 @@
220231
"- **Product type**: Consolidated\n",
221232
"- **Variable**: River discharge in the last 24 hours'\n",
222233
"- **Year**: 2012 to 2022\n",
223-
"- **Month**: June\n",
234+
"- **Month**: 06\n",
224235
"- **Day**: All days\n",
225-
"- **Area**: [30, 85, 20, 95,] = A bounding box for Bangladesh\n",
226-
"- **Format**: Grib\n",
236+
"- **Area**: [30, 85, 20, 95] = A bounding box for Bangladesh\n",
237+
"- **data_format**: Grib\n",
238+
"- **download_format**: unarchived\n",
227239
"\n",
228240
"At the end of the download form, select **\"Show API request\"**. This will reveal a block of code, which you can simply copy and paste into a cell of your Jupyter Notebook (see cells below).\n",
229241
"\n",
230242
"#### Download data\n",
231243
"\n",
232-
"... having copied the API request into the cell below, running this will retrieve and download the data you requested into your local directory. However, before you run the cell below, the **terms and conditions** of this particular dataset need to have been accepted in the CDS. The option to view and accept these conditions is given at the end of the download form, just above the **\"Show API request\"** option."
244+
"... having copied the API request into the cell below, running this will retrieve and download the data you requested into your local directory. However, before you run the cell below, the **terms and conditions** of this particular dataset need to have been accepted in the EWDS. The option to view and accept these conditions is given at the end of the download form, just above the **\"Show API request\"** option."
233245
]
234246
},
235247
{
@@ -240,23 +252,21 @@
240252
"source": [
241253
"download_file = f\"{DATADIR}/glofas-2012_2022.grib\"\n",
242254
"if not os.path.isfile(download_file):\n",
243-
" c = cdsapi.Client(\n",
244-
" **cdsapi_kwargs\n",
245-
" )\n",
246-
" c.retrieve(\n",
247-
" 'cems-glofas-historical',\n",
248-
" {\n",
249-
" 'system_version': 'version_4_0',\n",
250-
" 'hydrological_model': 'lisflood',\n",
251-
" 'product_type': 'consolidated',\n",
252-
" 'variable': 'river_discharge_in_the_last_24_hours',\n",
253-
" 'hyear': [f\"{year}\" for year in range(2012, 2023)],\n",
254-
" 'hmonth': 'june',\n",
255-
" 'hday': [f\"{day:02d}\" for day in range(1,31)],\n",
256-
" 'format': 'grib',\n",
257-
" 'area': [30, 85, 20, 95,],\n",
258-
" },\n",
259-
" ).download(download_file)"
255+
" c = cdsapi.Client()\n",
256+
" DATASET=\"cems-glofas-historical\"\n",
257+
" REQUEST= {\n",
258+
" \"system_version\": [\"version_4_0\"],\n",
259+
" \"hydrological_model\": [\"lisflood\"],\n",
260+
" \"product_type\": [\"consolidated\"],\n",
261+
" \"variable\": [\"river_discharge_in_the_last_24_hours\"],\n",
262+
" \"hyear\": [f\"{year}\" for year in range(2012, 2023)],\n",
263+
" \"hmonth\": [\"06\"],\n",
264+
" \"hday\": [f\"{day:02d}\" for day in range(1,31)],\n",
265+
" \"data_format\": \"grib2\",\n",
266+
" \"download_format\": \"unarchived\",\n",
267+
" \"area\": [30, 85, 20, 95]\n",
268+
" }\n",
269+
" c.retrieve(DATASET,REQUEST).download(download_file)"
260270
]
261271
},
262272
{
@@ -716,7 +726,7 @@
716726
}
717727
],
718728
"source": [
719-
"glofas_data = xr.open_dataset(download_file)\n",
729+
"glofas_data = xr.open_dataset(download_file, engine=\"cfgrib\")\n",
720730
"glofas_data"
721731
]
722732
},
@@ -1273,7 +1283,7 @@
12731283
"upstream_area_file = os.path.join(DATADIR, upstream_area_fname)\n",
12741284
"# If we have not already downloaded the file, we download it\n",
12751285
"if not os.path.isfile(upstream_area_file):\n",
1276-
" u_version=2 # file version\n",
1286+
" u_version=4 # file version\n",
12771287
" upstream_data_url = (\n",
12781288
" f\"https://confluence.ecmwf.int/download/attachments/242067380/{upstream_area_file}?\"\n",
12791289
" f\"version={u_version}&modificationDate=1668604690076&api=v2&download=true\"\n",
@@ -2066,7 +2076,7 @@
20662076
],
20672077
"metadata": {
20682078
"kernelspec": {
2069-
"display_name": "Python 3 (ipykernel)",
2079+
"display_name": "Python 3.11.8-01",
20702080
"language": "python",
20712081
"name": "python3"
20722082
},
@@ -2080,7 +2090,7 @@
20802090
"name": "python",
20812091
"nbconvert_exporter": "python",
20822092
"pygments_lexer": "ipython3",
2083-
"version": "3.10.9"
2093+
"version": "3.11.8"
20842094
},
20852095
"vscode": {
20862096
"interpreter": {

0 commit comments

Comments
 (0)
Please sign in to comment.