{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import cdsapi" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2025-01-21 10:17:29,931 INFO [2024-09-28T00:00:00] **Welcome to the New Climate Data Store (CDS)!** This new system is in its early days of full operations and still undergoing enhancements and fine tuning. Some disruptions are to be expected. Your \n", "[feedback](https://jira.ecmwf.int/plugins/servlet/desk/portal/1/create/202) is key to improve the user experience on the new CDS for the benefit of everyone. Thank you.\n", "2025-01-21 10:17:29,932 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics.\n", "2025-01-21 10:17:29,933 INFO [2024-09-16T00:00:00] Remember that you need to have an ECMWF account to use the new CDS. **Your old CDS credentials will not work in new CDS!**\n", "2025-01-21 10:17:29,933 WARNING [2024-06-16T00:00:00] CDS API syntax is changed and some keys or parameter names may have also changed. To avoid requests failing, please use the \"Show API request code\" tool on the dataset Download Form to check you are using the correct syntax for your API request.\n" ] } ], "source": [ "client = cdsapi.Client()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "grid = [0.75, 0.75]\n", "area = [90, -180+0.75/2, -90, 180]\n", "year = ['{}'.format(y) for y in range(1979, 2025)]\n", "month = ['{:02d}'.format(m) for m in range(1, 13)]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "dl_dir = 'tmp/'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Invariant" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2025-01-21 10:23:01,417 INFO Request ID is 154c2994-9ff9-4941-bf4a-983e335ee10f\n", "2025-01-21 10:23:01,526 INFO status has been updated to accepted\n", "2025-01-21 10:23:15,417 INFO status has been updated to successful\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "5922a90b4ae2639fa1b8f3f8ee440dfd.nc: 0%| | 0.00/434k [00:00<?, ?B/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "2025-01-21 10:23:17,901 INFO Request ID is 7b145c45-70ed-4873-8b3a-95c36878d2e6\n", "2025-01-21 10:23:18,257 INFO status has been updated to accepted\n", "2025-01-21 10:23:32,237 INFO status has been updated to successful\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "5922a90b4ae2639fa1b8f3f8ee440dfd.nc: 0%| | 0.00/434k [00:00<?, ?B/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "+++ Done!\n" ] } ], "source": [ "filename = 'ERA5_LowRes_Invariant.nc'\n", "dataset = \"reanalysis-era5-single-levels-monthly-means\"\n", "request = {\n", " \"product_type\": [\"monthly_averaged_reanalysis\"],\n", " \"variable\": [\n", " \"geopotential\",\n", " \"land_sea_mask\"\n", " ],\n", " \"grid\": grid,\n", " \"area\": area,\n", " \"year\": [\"1979\"],\n", " \"month\": [\"01\"],\n", " \"time\": [\"00:00\"],\n", " \"data_format\": \"netcdf\",\n", " \"download_format\": \"unarchived\",\n", "}\n", "\n", "client.retrieve(dataset, request).download(dl_dir + filename)\n", "print('+++ Done!')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# Unfortunate necessary cleaning\n", "with xr.open_dataset(dl_dir + filename) as ds:\n", " # ds = ds.rename({\"valid_time\": \"time\"}).drop_vars([\"expver\", \"number\"]) #, errors=\"ignore\")\n", " \n", " ds = ds.isel(valid_time=0).drop_vars([\"expver\", \"number\", \"valid_time\"])\n", " ds['orography'] = ds['z'] / 9.80665\n", " ds['orography'].attrs.update(long_name='Orography', units='m', standard_name='orography')\n", " ds = ds.drop_vars([\"z\"])\n", " ds['lsm'].attrs = {k: v for k, v in ds['lsm'].attrs.items() if not k.startswith(\"GRIB_\")}\n", " \n", " ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith(\"GRIB_\")}\n", " ds.to_netcdf(filename)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Temp and precip " ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2025-01-21 10:54:18,233 INFO Request ID is d067a552-8c4c-4323-99a9-520a58ba6527\n", "2025-01-21 10:54:18,319 INFO status has been updated to accepted\n", "2025-01-21 10:54:39,641 INFO status has been updated to running\n", "2025-01-21 10:56:12,626 WARNING Structural differences in grib fields detected when opening in xarray. Opening the grib file safely, however this may result in files with non-intuitive filenames.\n", "2025-01-21 10:57:10,562 INFO status has been updated to successful\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "f6090c8ca0f20f3acad182b5cc0df08f.nc: 0%| | 0.00/96.1M [00:00<?, ?B/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "+++ Done!\n" ] } ], "source": [ "filename = 'ERA5_LowRes_Monthly_t2m.nc'\n", "\n", "dataset = \"reanalysis-era5-single-levels-monthly-means\"\n", "request = {\n", " \"product_type\": [\"monthly_averaged_reanalysis\"],\n", " \"variable\": [\"2m_temperature\"],\n", " \"grid\": grid,\n", " \"area\": area,\n", " \"year\": year,\n", " \"month\": month,\n", " \"time\": [\"00:00\"],\n", " \"data_format\": \"netcdf\",\n", " \"download_format\": \"unarchived\",\n", "}\n", "\n", "client.retrieve(dataset, request).download(dl_dir + filename)\n", "print('+++ Done!')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "# Unfortunate necessary cleaning\n", "with xr.open_dataset(dl_dir + filename) as ds:\n", " ds = ds.rename({\"valid_time\": \"time\"}).drop_vars([\"expver\", \"number\"]) \n", " \n", " ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith(\"GRIB_\")}\n", " for var in ds.variables.keys():\n", " ds[var].attrs = {k: v for k, v in ds[var].attrs.items() if not k.startswith(\"GRIB_\")}\n", " \n", " ds.to_netcdf(filename)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2025-01-21 11:02:06,637 INFO Request ID is 6533f982-8975-4013-b179-a450df5b0097\n", "2025-01-21 11:02:06,714 INFO status has been updated to accepted\n", "2025-01-21 11:02:20,592 INFO status has been updated to running\n", "2025-01-21 11:03:23,478 WARNING Structural differences in grib fields detected when opening in xarray. Opening the grib file safely, however this may result in files with non-intuitive filenames.\n", "2025-01-21 11:03:23,480 INFO status has been updated to successful\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "e45b7c37af7a73ef9b416b35ce1c1f59.nc: 0%| | 0.00/97.1M [00:00<?, ?B/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "+++ Done!\n" ] } ], "source": [ "filename = 'ERA5_LowRes_Monthly_tp.nc'\n", "\n", "dataset = \"reanalysis-era5-single-levels-monthly-means\"\n", "request = {\n", " \"product_type\": [\"monthly_averaged_reanalysis\"],\n", " \"variable\": [\"total_precipitation\"],\n", " \"grid\": grid,\n", " \"area\": area,\n", " \"year\": year,\n", " \"month\": month,\n", " \"time\": [\"00:00\"],\n", " \"data_format\": \"netcdf\",\n", " \"download_format\": \"unarchived\",\n", "}\n", "\n", "client.retrieve(dataset, request).download(dl_dir + filename)\n", "print('+++ Done!')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "# Unfortunate necessary cleaning\n", "with xr.open_dataset(dl_dir + filename) as ds:\n", " ds = ds.rename({\"valid_time\": \"time\"}).drop_vars([\"expver\", \"number\"]) \n", " \n", " ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith(\"GRIB_\")}\n", " for var in ds.variables.keys():\n", " ds[var].attrs = {k: v for k, v in ds[var].attrs.items() if not k.startswith(\"GRIB_\")}\n", " \n", " ds.to_netcdf(filename)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.8" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "nbTranslate": { "displayLangs": [ "*" ], "hotkey": "alt-t", "langInMainMenu": true, "sourceLang": "en", "targetLang": "fr", "useGoogleTranslate": true }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }