Downloading, Reading and Working with Copernicus WEKEO Soil Water Index 12.5 km

14.1 Overview

14.2 Imports

import os

import cartopy.crs as ccrs
import hvplot.pandas  # noqa
import xarray as xr
from dotenv import dotenv_values
from hda import Client, Configuration
conf = Configuration(
    user=dotenv_values(".env")["USER_WEKEO"],
    password=dotenv_values(".env")["PASS_WEKEO"],
)
hda_client = Client(config=conf)
query = {
    "dataset_id": "EO:CLMS:DAT:CLMS_GLOBAL_SWI_12.5KM_V3_TIME-SERIES_NETCDF",
    "bbox": [
        30.315105942117828,
        -27.488474233587713,
        41.07238227721744,
        -10.203047702100717,
    ],
    "itemsPerPage": 200,
    "startIndex": 0,
}

matches = hda_client.search(query)

print(matches)
SearchResults[items=3,volume=312MB]
%%capture

local_path = "cgls_swi_12_5"

if not os.path.isdir(local_path):
    os.mkdir(local_path)

matches.download(download_dir=local_path)
def _preprocess(ds: xr.Dataset):
    return ds.SWI_010


df = xr.open_mfdataset(
    "cgls_swi_12_5/*.nc",
    combine="nested",
    parallel=True,
    chunks=-1,
    preprocess=_preprocess,
).to_dataframe()
df
lon lat SWI_010
locations time
565647 2007-01-01 12:00:00 30.104193 -10.060086 NaN
2007-01-02 12:00:00 30.104193 -10.060086 80.5
2007-01-03 12:00:00 30.104193 -10.060086 80.5
2007-01-04 12:00:00 30.104193 -10.060086 80.5
2007-01-05 12:00:00 30.104193 -10.060086 79.0
... ... ... ... ...
1621559 2024-12-27 12:00:00 31.977373 -29.931433 NaN
2024-12-28 12:00:00 31.977373 -29.931433 NaN
2024-12-29 12:00:00 31.977373 -29.931433 NaN
2024-12-30 12:00:00 31.977373 -29.931433 NaN
2024-12-31 12:00:00 31.977373 -29.931433 NaN

94804925 rows × 3 columns

%run ./src/ssm_cmap.py

df.hvplot.points(
    x="lon",
    y="lat",
    c="SWI_010",
    groupby="time",
    x_sampling=0.16,
    y_sampling=0.16,
    rasterize=True,
    crs=ccrs.PlateCarree(),
    tiles=True,
    cmap=SSM_CMAP,  # noqa
    clim=(0, 100),
    frame_width=500,
    clabel="Soil Water Index",
)