{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Masking data-cubes using geometry objects" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# If first time running, uncomment the line below to install any additional dependencies\n", "# !bash requirements-for-notebooks.sh" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "from earthkit.transforms import aggregate as ek_aggregate\n", "from earthkit import data as ek_data\n", "\n", "from earthkit.data.testing import earthkit_remote_test_data_file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load some test data\n", "\n", "All `earthkit-transforms` methods can be called with `earthkit-data` objects (Readers and Wrappers) or with the \n", "pre-loaded `xarray` or `geopandas` objects.\n", "\n", "In this example we will use hourly ERA5 2m temperature data on a 0.5x0.5 spatial grid for the year 2015 as\n", "our physical data; and we will use the NUTS geometries which are stored in a geojson file.\n", "\n", "First we lazily load the ERA5 data and NUTS geometries from our test-data repository.\n", "\n", "Note the data is only downloaded when\n", "we use it, e.g. at the `.to_xarray` line, additionally, the download is cached so the next time you run this\n", "cell you will not need to re-download the file (unless it has been a very long time since you have run the\n", "code, please see tutorials in `earthkit-data` for more details in cache management)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset> Size: 5MB\n",
"Dimensions: (number: 1, time: 24, step: 1, surface: 1, latitude: 201,\n",
" longitude: 281)\n",
"Coordinates:\n",
" * number (number) int64 8B 0\n",
" * time (time) datetime64[ns] 192B 2015-01-01 ... 2015-01-01T23:00:00\n",
" * step (step) timedelta64[ns] 8B 00:00:00\n",
" * surface (surface) float64 8B 0.0\n",
" * latitude (latitude) float64 2kB 80.0 79.75 79.5 79.25 ... 30.5 30.25 30.0\n",
" * longitude (longitude) float64 2kB -10.0 -9.75 -9.5 ... 59.5 59.75 60.0\n",
" valid_time (time, step) datetime64[ns] 192B ...\n",
"Data variables:\n",
" t2m (number, time, step, surface, latitude, longitude) float32 5MB ...\n",
"Attributes:\n",
" GRIB_edition: 1\n",
" GRIB_centre: ecmf\n",
" GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts\n",
" GRIB_subCentre: 0\n",
" Conventions: CF-1.7\n",
" institution: European Centre for Medium-Range Weather Forecasts\n",
" history: 2024-10-15T13:59 GRIB to CDM+CF via cfgrib-0.9.1...| \n", " | id | \n", "NUTS_ID | \n", "LEVL_CODE | \n", "CNTR_CODE | \n", "NAME_LATN | \n", "NUTS_NAME | \n", "MOUNT_TYPE | \n", "URBN_TYPE | \n", "COAST_TYPE | \n", "FID | \n", "geometry | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "DK | \n", "DK | \n", "0 | \n", "DK | \n", "Danmark | \n", "Danmark | \n", "0 | \n", "0 | \n", "0 | \n", "DK | \n", "MULTIPOLYGON (((15.1629 55.0937, 15.094 54.996... | \n", "
| 1 | \n", "RS | \n", "RS | \n", "0 | \n", "RS | \n", "Serbia | \n", "Srbija/Сpбија | \n", "0 | \n", "0 | \n", "0 | \n", "RS | \n", "POLYGON ((21.4792 45.193, 21.3585 44.8216, 22.... | \n", "
| 2 | \n", "EE | \n", "EE | \n", "0 | \n", "EE | \n", "Eesti | \n", "Eesti | \n", "0 | \n", "0 | \n", "0 | \n", "EE | \n", "MULTIPOLYGON (((27.357 58.7871, 27.6449 57.981... | \n", "
| 3 | \n", "EL | \n", "EL | \n", "0 | \n", "EL | \n", "Elláda | \n", "Ελλάδα | \n", "0 | \n", "0 | \n", "0 | \n", "EL | \n", "MULTIPOLYGON (((28.0777 36.1182, 27.8606 35.92... | \n", "
| 4 | \n", "ES | \n", "ES | \n", "0 | \n", "ES | \n", "España | \n", "España | \n", "0 | \n", "0 | \n", "0 | \n", "ES | \n", "MULTIPOLYGON (((4.391 39.8617, 4.1907 39.7981,... | \n", "
<xarray.Dataset> Size: 5MB\n",
"Dimensions: (number: 1, time: 24, step: 1, surface: 1, latitude: 201,\n",
" longitude: 281)\n",
"Coordinates:\n",
" * number (number) int64 8B 0\n",
" * time (time) datetime64[ns] 192B 2015-01-01 ... 2015-01-01T23:00:00\n",
" * step (step) timedelta64[ns] 8B 00:00:00\n",
" * surface (surface) float64 8B 0.0\n",
" * latitude (latitude) float64 2kB 80.0 79.75 79.5 79.25 ... 30.5 30.25 30.0\n",
" * longitude (longitude) float64 2kB -10.0 -9.75 -9.5 ... 59.5 59.75 60.0\n",
" valid_time (time, step) datetime64[ns] 192B ...\n",
"Data variables:\n",
" t2m (number, time, step, surface, latitude, longitude) float32 5MB ...\n",
"Attributes:\n",
" GRIB_edition: 1\n",
" GRIB_centre: ecmf\n",
" GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts\n",
" GRIB_subCentre: 0\n",
" Conventions: CF-1.7\n",
" institution: European Centre for Medium-Range Weather Forecasts\n",
" history: 2024-10-15T13:59 GRIB to CDM+CF via cfgrib-0.9.1...<xarray.Dataset> Size: 201MB\n",
"Dimensions: (index: 37, number: 1, time: 24, step: 1, surface: 1,\n",
" latitude: 201, longitude: 281)\n",
"Coordinates:\n",
" * number (number) int64 8B 0\n",
" * time (time) datetime64[ns] 192B 2015-01-01 ... 2015-01-01T23:00:00\n",
" * step (step) timedelta64[ns] 8B 00:00:00\n",
" * surface (surface) float64 8B 0.0\n",
" * latitude (latitude) float64 2kB 80.0 79.75 79.5 79.25 ... 30.5 30.25 30.0\n",
" * longitude (longitude) float64 2kB -10.0 -9.75 -9.5 ... 59.5 59.75 60.0\n",
" valid_time (time, step) datetime64[ns] 192B dask.array<chunksize=(24, 1), meta=np.ndarray>\n",
" * index (index) int64 296B 0 1 2 3 4 5 6 7 8 ... 29 30 31 32 33 34 35 36\n",
"Data variables:\n",
" t2m (index, number, time, step, surface, latitude, longitude) float32 201MB dask.array<chunksize=(1, 1, 24, 1, 1, 201, 281), meta=np.ndarray>\n",
"Attributes:\n",
" GRIB_edition: 1\n",
" GRIB_centre: ecmf\n",
" GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts\n",
" GRIB_subCentre: 0\n",
" Conventions: CF-1.7\n",
" institution: European Centre for Medium-Range Weather Forecasts\n",
" history: 2024-10-15T13:59 GRIB to CDM+CF via cfgrib-0.9.1...<xarray.Dataset> Size: 201MB\n",
"Dimensions: (FID: 37, number: 1, time: 24, step: 1, surface: 1,\n",
" latitude: 201, longitude: 281)\n",
"Coordinates:\n",
" * number (number) int64 8B 0\n",
" * time (time) datetime64[ns] 192B 2015-01-01 ... 2015-01-01T23:00:00\n",
" * step (step) timedelta64[ns] 8B 00:00:00\n",
" * surface (surface) float64 8B 0.0\n",
" * latitude (latitude) float64 2kB 80.0 79.75 79.5 79.25 ... 30.5 30.25 30.0\n",
" * longitude (longitude) float64 2kB -10.0 -9.75 -9.5 ... 59.5 59.75 60.0\n",
" valid_time (time, step) datetime64[ns] 192B dask.array<chunksize=(24, 1), meta=np.ndarray>\n",
" * FID (FID) object 296B 'DK' 'RS' 'EE' 'EL' ... 'CY' 'CZ' 'DE' 'NO'\n",
"Data variables:\n",
" t2m (FID, number, time, step, surface, latitude, longitude) float32 201MB dask.array<chunksize=(1, 1, 24, 1, 1, 201, 281), meta=np.ndarray>\n",
"Attributes:\n",
" GRIB_edition: 1\n",
" GRIB_centre: ecmf\n",
" GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts\n",
" GRIB_subCentre: 0\n",
" Conventions: CF-1.7\n",
" institution: European Centre for Medium-Range Weather Forecasts\n",
" history: 2024-10-15T13:59 GRIB to CDM+CF via cfgrib-0.9.1...