Source code for ceda_datapoint.core.cloud

__author__ = "Daniel Westwood"
__contact__ = "daniel.westwood@stfc.ac.uk"
__copyright__ = "Copyright 2024 United Kingdom Research and Innovation"

import json
import logging
import os
from typing import Any, Union

import fsspec
import requests
import rioxarray as rxr
import xarray as xr

from ceda_datapoint.aliases import CEDA_DAP_PREFIX, LATLON_ALIASES, PAIRS_ONLY
from ceda_datapoint.core.asset import BasicAsset, DataPointMapper
from ceda_datapoint.mixins import UIMixin
from ceda_datapoint.utils import hash_id, logstream

try:
    import cf
    from cf import FieldList
except ImportError:
    cf = None
    FieldList = None

logger = logging.getLogger(__name__)
logger.addHandler(logstream)
logger.propagate = False


def _decode_polygon(spatial_dims: list, coordinates: list) -> dict:
    """
    Decode GeoJSON Polygon to xarray Selection.
    """
    selects = {}
    for i, dim in enumerate(spatial_dims):
        corners = [c[i] for c in coordinates]
        dim_range = slice(min(corners), max(corners))
        selects[dim] = dim_range
    return selects


def _decode_datetime_xr(datetime):
    """
    Decode pystac datetime to xarray select.
    """
    dt = datetime.split("/")
    if len(dt) > 1:
        return slice(dt[0], dt[1])
    elif len(dt) == 1:
        return dt[0]
    else:
        return dt


def _decode_datetime_cf(datetime):
    """
    Decode pystac datetime to cf-python subspace.
    """
    if cf is None:
        raise ImportError('Package "cf-python" not installed')

    if isinstance(datetime, str):
        dt = datetime.split("/")

    # Determine if a range or a single value for datetime and
    # provide the appropriate cf query objcet to provide to the
    # subspace method.
    if len(dt) == 2:
        return cf.wi(cf.dt(dt[0]), cf.dt(dt[1]))
    elif len(dt) == 1:
        return cf.isclose(cf.dt(dt[0]))
    else:
        raise ValueError(f"Can't decode datetime specification: {datetime}")


def _find_spatial_dims(ds) -> Union[list, None]:
    """Determine the names of the spatial dimensions."""
    lat = next((d for d in ds.dims if d.lower() in LATLON_ALIASES["latitude"]), None)
    lon = next((d for d in ds.dims if d.lower() in LATLON_ALIASES["longitude"]), None)

    warn_skip_spatial = False
    if lat in PAIRS_ONLY:
        if lon != PAIRS_ONLY[-PAIRS_ONLY.index(lat)]:
            warn_skip_spatial = True

    if lat is None or lon is None:
        warn_skip_spatial = True

    if warn_skip_spatial:
        logger.warning(
            "Spatial AOI skipped - could not identify spatial dimensions. "
            "Accepted latitude aliases are: "
            f"{sorted(LATLON_ALIASES['latitude'])}; "
            "accepted longitude aliases are: "
            f"{sorted(LATLON_ALIASES['longitude'])}."
        )
        return None

    # Determine lat/lon ordering in dataset.
    dims = list(ds.dims)
    if dims.index(lat) < dims.index(lon):
        return [lat, lon]
    else:
        return [lon, lat]


def _convert_lat_lon_names(selection_args):
    """Convert latitude/longitude dimension aliases to canonical names."""
    formatted_selection_args = {}
    for name, value in selection_args.items():
        key = str(name).strip().lower()
        lat_names = LATLON_ALIASES["latitude"]
        lon_names = LATLON_ALIASES["longitude"]

        if key in lat_names:
            formatted_selection_args["latitude"] = value
        elif key in lon_names:
            formatted_selection_args["longitude"] = value
        else:
            raise ValueError(
                f"Unrecognised spatial dimension {name!r}. "
                "Expected a latitude alias "
                f"{sorted(lat_names)} or a longitude alias "
                f"{sorted(lon_names)}."
            )
    if formatted_selection_args["latitude"] in PAIRS_ONLY:
        lo = formatted_selection_args["longitude"]
        la = formatted_selection_args["latitude"]
        if lo != PAIRS_ONLY[-PAIRS_ONLY.index(la)]:
            raise ValueError(
                f"Mismatching lat/lon dimension names: {la}, {lo}",
                f' - pairing is enforced on the labels: "{PAIRS_ONLY}"',
            )

    return formatted_selection_args


[docs] class DataPointCloudProduct(BasicAsset): """ Object for storing and manipulating a single cloud product i.e Kerchunk/Zarr/CFA. """ def __init__( self, asset_stac: dict, id: str = None, cloudf: str = None, order: int = None, mode: str = "xarray", meta: dict = None, stac_attrs: dict = None, properties: dict = None, mapper: DataPointMapper = None, data_selection: dict = None, ) -> None: """ Initialise a single cloud product object. The cloud product has identical properties and attributes to the parent item, but now represents a single reference dataset. :param asset_stac: (dict) The asset as presented in the stac index. :param id: (str) Identifier for this cloud product. :param cf: (str) Cloud format type. :param order: (int) Unused property relating to priority. :param mode: (str) Method to use for opening dataset. :param meta: (dict) DataPoint metadata relating to parent objects. :param stac_attrs: (dict) Attributes of the item outside the ``properties``. :param properties: (dict) Properties of the item in the ``properties`` field. """ if mode not in ("xarray", "cf"): raise NotImplementedError( f'Bad mode value {mode}: only "xarray" and "cf" are valid modes.' ) self.mode = mode super().__init__( asset_stac, id=id, meta=meta, stac_attrs=stac_attrs, properties=properties, mapper=mapper, ) self._order = order self._cloud_format = cloudf self._data_selection = data_selection or {} self._meta["cloud_format"] = cloudf self.visibility = "all" self._set_visibility() def __str__(self) -> str: return f"<DataPointCloudProduct: {self._id} (Format: {self._cloud_format})>" def __repr__(self) -> str: """Representation of this class using the meta components""" repr = super().__repr__().split("\n") repr.append("Attributes:") if self._properties is None: repr.append(" <empty>") return "\n".join(repr) for k, v in self._properties.items(): repr.append(f" - {k}: {v}") return "\n".join(repr) @property def cloud_format(self) -> str: """Read-only property""" return self._cloud_format
[docs] @classmethod def help(cls) -> None: """Display public methods for this object.""" print("DataPointCloudProduct Help:") print(" > product.info() - Get information about this cloud product.") print( " > product.open_dataset() - Open the dataset for this cloud product (in xarray)" ) # print(' > product.open_asset() - Overrides BasicAsset get method') super(DataPointCloudProduct, cls).help(additionals=["href", "cloud_format"])
[docs] def info(self) -> None: """Display information about this object""" print(self.__repr__())
[docs] def open_asset( self, local_only: bool = False, prepare_data: bool = True, **kwargs ) -> Any: """ Override for basic asset get function. :param local_only: (bool) Switch to using local-only files - DataPoint will convert all hrefs and internal Kerchunk links to use local paths. Note that local_only is only available for the 'xarray' mode. :param prepare_data: (bool) Apply dataset selections after opening. """ return self.open_dataset(local_only=local_only, prepare_data=prepare_data)
[docs] def open_dataset( self, mode: str = "xarray", local_only: bool = False, prepare_data: bool = True, **kwargs, ) -> xr.Dataset | FieldList: """ Open the dataset for this product. Specific methods to open cloud formats are private since the method should be determined by internal values not user input. :param mode: (str) Library to use for opening datasets. Can be either 'xarray', returning a `xr.Dataset` object, or 'cf', returning a `cf.FieldList` object. :param local_only: (bool) Switch to using local-only files - DataPoint will convert all hrefs and internal Kerchunk links to use local paths. Note that local_only is only available for the 'xarray' mode. :param prepare_data: (bool) Apply dataset selections after opening. """ if mode != "xarray": raise NotImplementedError( "Non-xarray opening is not implemented for cloud products yet." ) if not self._cloud_format: raise ValueError("No cloud format given for this dataset") if self.href is None: raise ValueError('Cloud assets with no "href" are not supported') if self.visibility == "local-only" and not local_only: raise ValueError( "Href not reachable via https, please use `local_only=True` " "to open this dataset." ) if local_only and mode == "cf": raise ValueError("'local_only' is not supported in 'cf' mode.") if mode == "xarray": return self.open_dataset_with_xr( local_only=local_only, prepare_data=prepare_data, **kwargs, ) elif mode == "cf": return self.open_dataset_with_cf( local_only=local_only, prepare_data=prepare_data, **kwargs, ) else: raise NotImplementedError( f'Only "xarray" and "cf" are valid modes. Got mode of: {mode}' )
[docs] def open_dataset_with_xr( self, local_only: bool = False, prepare_data: bool = True, **kwargs ) -> xr.Dataset: """ Open the dataset for this product with xarray. Returns a `xr.Dataset` object representing the dataset. :param local_only: (bool) Switch to using local-only files - DataPoint will convert all hrefs and internal Kerchunk links to use local paths. :param prepare_data: (bool) Apply dataset selections after opening. """ try: if self._cloud_format == "kerchunk": ds = self._open_kerchunk(local_only=local_only, **kwargs) elif self._cloud_format == "CFA": ds = self._open_cfa(**kwargs) elif self._cloud_format == "zarr": ds = self._open_zarr(**kwargs) elif self._cloud_format == "cog": ds = self._open_cog(**kwargs) else: raise ValueError( 'Cloud format not recognised - must be one of ("kerchunk", "CFA", "zarr", "cog")' ) if not prepare_data: return ds return self._prepare_dataset_xr(ds) except ValueError as err: raise err except FileNotFoundError: raise FileNotFoundError( "The requested resource could not be located: " f"{self.href}" )
[docs] def open_dataset_with_cf( self, local_only: bool = False, prepare_data: bool = True, **kwargs ) -> FieldList: """ Open the dataset for this product with cf-python. Returns a `cf.FieldList` object of one or more `cf.Field` objects representing the dataset. :param local_only: (bool) Switch to using local-only files - DataPoint will convert all hrefs and internal Kerchunk links to use local paths. :param prepare_data: (bool) Apply dataset selections ('subspaces' in 'cf-python' terminology) after opening. """ # Import internally to avoid a hard dependency - have set in # packaging (pyproject.toml) as 'cf-output' install extra category if cf is None: raise ImportError('Package "cf-python" not installed') if self._cloud_format == "zarr": # Open Zarr to cf fl = cf.read(self) if self._cloud_format == "kerchunk": fl = self._open_kerchunk_cf(local_only=local_only) elif self._cloud_format == "CFA": # Open CFA fl = cf.read(self.href, cfa="field") else: # Not supporting the 'cog' case here since this isn't # supported by cf-python raise ValueError( 'Cloud format not recognised - must be one of ("kerchunk", "CFA", ' '"zarr") for opening in the "cf" mode.' ) if not prepare_data: return fl return self._prepare_dataset_cf(fl)
def _open_kerchunk_cf(self, local_only: bool = False): # Parse kwargs mapper_kwargs = {} mapper_kwargs = ( self._mapper.get("mapper_kwargs", self._asset_stac) or mapper_kwargs ) open_zarr_kwargs = self._mapper.get("open_zarr_kwargs", self._asset_stac) or {} # Local or remote HREF href = self.href if local_only: href = _fetch_kerchunk_make_local(href) # Important: need BOTH of these to be set True else get # (a)sync inconsistency issues. zarr_kwargs = { "remote_options": {"asynchronous": True}, "asynchronous": True, } | open_zarr_kwargs # Create the filesystem and mapping object fs = fsspec.filesystem( "reference", fo=href, remote_protocol="https", **zarr_kwargs ) kerchunk = fs.get_mapper() # Finally read from the filesystem mapper return cf.read(kerchunk) def _open_kerchunk( self, local_only: bool = False, mapper_kwargs: dict = None, **kwargs, ) -> xr.Dataset: """ Open a kerchunk dataset in xarray :param local_only: (bool) Switch to using local-only files - DataPoint will convert all hrefs and internal Kerchunk links to use local paths. :param mapper_kwargs: (dict) Kwargs to provide to Kerchunk's fsspec mapper. """ mapper_kwargs = mapper_kwargs or {} href = self.href mapper_kwargs = ( self._mapper.get("mapper_kwargs", self._asset_stac) or mapper_kwargs ) open_zarr_kwargs = self._mapper.get("open_zarr_kwargs", self._asset_stac) or {} if local_only: href = _fetch_kerchunk_make_local(href) return xr.open_dataset(href, engine="kerchunk", **open_zarr_kwargs) def _open_cfa( self, cfa_options: dict = None, **kwargs, ) -> xr.Dataset: """ Open a CFA dataset in xarray :param cfa_options: (dict) Configuration options to pass to the CFA engine """ cfa_options = cfa_options or {} href = self.href open_xarray_kwargs = ( self._mapper.get("open_xarray_kwargs", self._asset_stac) or {} ) | kwargs return xr.open_dataset( href, engine="CFA", cfa_options=cfa_options, **open_xarray_kwargs ) def _open_zarr( self, **kwargs, ) -> xr.Dataset: open_zarr_kwargs = ( self._mapper.get("open_zarr_kwargs", self._asset_stac) or {} ) | kwargs return xr.open_dataset(self.href, engine="zarr", **open_zarr_kwargs) def _open_cog( self, **kwargs, ) -> xr.Dataset: open_cog_kwargs = ( self._mapper.get("open_cog_kwargs", self._asset_stac) or {} ) | kwargs return rxr.open_rasterio(self.href, **open_cog_kwargs) def _prepare_dataset_xr( self, ds: xr.Dataset, ) -> Union[xr.Dataset, xr.DataArray]: """Perform any dataset selections here.""" intersects = self._meta["search_terms"].get("intersects", None) # Intersection applies to spatial data datetime = self._meta["search_terms"].get("datetime", None) # Datetime can only be applied to 'time' dimension. query = self._meta["search_terms"].get("query", []) vq = None for ql in query: if "variables" in ql: vq = ql.split("=")[-1] variables = self._data_selection.get("variables", None) or vq sel = self._data_selection.get("sel", None) isel = self._data_selection.get("isel", None) spatial_dims = None if intersects: # Order spatial dims correctly based on bbox spatial_dims = _find_spatial_dims(ds) if spatial_dims is not None: if intersects["type"] == "Polygon": if len(intersects["coordinates"]) == 1: coords = intersects["coordinates"][0] else: coords = intersects["coordinates"] select = _decode_polygon(spatial_dims, coords) ds = ds.sel(**select) else: logger.warning( "Unsupported intersection type for Single Search Selection - " "AOI not applied." ) if datetime is not None: if "time" not in ds: logger.warning( "Datetime selection could not be applied - ", 'no "time" dimension present.', ) else: if isinstance(datetime, str): time_sel = _decode_datetime_xr(datetime) else: time_sel = slice(datetime[0], datetime[1]) ds = ds.sel(time=time_sel) if variables is not None: if isinstance(variables, str): variables = [variables] keep_vars = [] all_vars = list(ds.variables) for v in variables: if v not in ds: logger.warning( "Variable selection could not be applied - ", f'no "{v}" variable present.', ) else: keep_vars.append(v) if len(keep_vars) == 0: raise ValueError( f"No variables kept in current selection - {variables}" ) drop_vars = list(set(all_vars).difference(set(keep_vars))) ds = ds.drop_vars(drop_vars) if sel is not None: ds = ds.sel(**sel) if isel is not None: ds = ds.isel(**isel) return ds def _prepare_dataset_cf( self, fl: FieldList, ) -> FieldList: """Perform any dataset subspaces here.""" if cf is None: raise ImportError('Package "cf-python" not installed') # Get relevant search terms intersects = self._meta["search_terms"].get("intersects", None) datetime = self._meta["search_terms"].get("datetime", None) query = self._meta["search_terms"].get("query", []) # Get data selection choices vq = None for ql in query: if "variables" in ql: vq = ql.split("=")[-1] variables = self._data_selection.get("variables", None) or vq sel = self._data_selection.get("sel", None) isel = self._data_selection.get("isel", None) # Filter the FieldList to the requested variables fl_selection = fl if variables is not None: for field in fl_selection: field.dump() # 'variables' input is a list so unpack as arguments # to select_by_ncvar fl_selection = fl.select_by_ncvar(*variables) # Returns empty fieldlist if no fields captured by selection if not fl_selection: logger.warning( "Variable selection could not be applied: " f"no such variables in FieldList: {variables}" ) else: logger.warning("No variable selection provided.") # Create output FieldList out = cf.FieldList() # Apply collapses on the fields invididually, but at the end # we aggregate in case we can simplify. for field in fl_selection: # Handle any empty or size one axes - see cf.Field.squeeze # (ncas-cms.github.io/cf-python/method/cf.Field.squeeze.html) # for details field.squeeze(inplace=True) # Apply polygon selection on spatial dimensions # Check for horizontal X coordinate and horizontal X coordinate x = field.dimension_coordinate("X", default=None) y = field.dimension_coordinate("Y", default=None) if y is not None and x is not None and intersects is not None: if intersects.get("type", None) == "Polygon": coords = intersects["coordinates"] if len(coords) == 1: coords = coords[0] lons = [c[0] for c in coords] lats = [c[1] for c in coords] # The X case may only work assuming the field has # appropriate cyclicity for the longitude, but the # cyclicity should be determined automatically by # cf-python and hence it should be OK. field = field.subspace( X=cf.wi(min(lons), max(lons)), Y=cf.wi(min(lats), max(lats)), ) else: logger.warning( "Unsupported intersection type for Single Search " "Selection - AOI not applied." ) # Apply datetime selection if requested if datetime is not None: if not field.coordinate("time"): logger.warning( "Time selection could not be applied to " f"{field}: field has no time coordinate." ) # Needs decoding from 'start/end' format dt_query = _decode_datetime_cf(datetime) field = field.subspace(T=dt_query) # Apply spatial subspaces if sel is not None: # Note sel args are directly compatible with cf-python # subspace method sel = _convert_lat_lon_names(sel) field = field.subspace(**sel) if isel is not None: isel = _convert_lat_lon_names(isel) # Process isel args -> cf-python subspacing via indices indices = field.indices(**isel) field = field[indices] out.append(field) # Re-aggregate to potentially simplify the result return cf.aggregate(out) def _set_visibility(self) -> None: """Determine if this product is reachable""" if self.href.startswith("/"): # Check local path self.visibility = "local-only" if not os.path.isfile(self.href): self.visibility = "unreachable" return # Check remote link check_ref = self.href if self._cloud_format == "zarr": check_ref = f"{self.href}/.zmetadata" status = requests.head(check_ref) if status.status_code != 200: # Check local link self.visibility = "local-only" local_ref = self.href.replace("https://dap.ceda.ac.uk", "") if not os.path.isfile(local_ref): self.visibility = "unreachable" return return
[docs] class DataPointCluster(UIMixin): """ A set of non-combined datasets opened using the DataPointSearch ``to_dataset()`` method. Has some additional properties over a list of datasets.""" def __init__( self, products: list, parent_id: str = None, meta: dict = None, local_only: bool = False, data_selection: Union[dict, None] = None, show_unreachable: bool = False, ) -> None: """Initialise a cluster of datasets from a set of assets. :param products: (list) A list of DataPoint cloud product objects. :param parent_id: (str) ID of the parent search/item object. :param meta: (dict) Metadata about the parent object. :param local_only: (bool) Switch to using local-only files - DataPoint will convert all hrefs and internal Kerchunk links to use local paths. :param show_unreachable: (bool) Show the hidden assets that DataPoint has determined are currently unreachable. """ self._id = f"{parent_id}-{hash_id(parent_id)}" self._local_only = local_only self._data_selection = data_selection self.show_unreachable = show_unreachable meta = meta or {} self._products = {} for p in products: if isinstance(p, DataPointCluster): for sub_p in p.products: self._products[sub_p.id] = sub_p elif p is not None: self._products[p.id] = p self._meta = meta self._meta["products"] = len(products) def __str__(self) -> str: """String representation of this class""" return f"<DataPointCluster: {self._id} (Datasets: {len(self._products)})>" def __getitem__(self, index): """ Index this object to obtain a DataPointCloudProduct by ID or position in the cluster. """ if isinstance(index, int): index = list(self._products.keys())[index] if index not in self._products: raise IndexError(f'"{index}" not found in available products.') return self._products[index] @property def products(self) -> list[DataPointCloudProduct]: """List of products contained within this cluster""" return [ v for v in self._products.values() if v.visibility != "unreachable" or self.show_unreachable ]
[docs] @classmethod def help(cls) -> None: """Helper function - lists methods that can be utilised for this class""" print("DataPointCluster Help:") print(" > cluster.info() - basic cluster information") print(" > cluster.open_dataset(index/id) - open a specific dataset in xarray") super(DataPointCluster, cls).help(additionals=["products"])
[docs] def info(self) -> None: """Information about this object instance.""" print(self.__repr__())
def __repr__(self) -> str: """Notebooks representation of this class""" repr = super().__repr__().split("\n") repr.append("Products:") for p in self._products.values(): if p.visibility != "all": repr.append(f" - {p.id}: {p.cloud_format} ({p.visibility})") else: repr.append(f" - {p.id}: {p.cloud_format}") return "\n".join(repr)
[docs] def open_dataset( self, id: str, mode: str = "xarray", local_only: bool = False, **kwargs, ) -> xr.Dataset: """ Open a dataset from within this cluster's cloud products. A dataset can be indexed either by id or position within this cluster's set of datasets. :param id: (str) The ID or index of the dataset in the resulting cluster. :param mode: (str) Library to use for opening datasets. Can be either 'xarray', returning a `xr.Dataset` object, or 'cf', returning a `cf.FieldList` object. :param local_only: (bool) Switch to using local-only files - DataPoint will convert all hrefs and internal Kerchunk links to use local paths. Note that local_only is only available for the 'xarray' mode. """ if mode not in ("xarray", "cf"): raise NotImplementedError( f'Only "xarray" and "cf" are valid modes. Got mode of: {mode}' ) local_only = local_only or self._local_only if local_only and mode == "cf": raise ValueError("'local_only' is not supported in 'cf' mode.") if isinstance(id, int): id = list(self._products.keys())[id] if id not in self._products: logger.warning(f'"{id}" not found in available datasets.') return None product = self._products[id] if mode == "xarray": return product.open_dataset_with_xr(local_only=local_only, **kwargs) elif mode == "cf": return product.open_dataset_with_cf(local_only=local_only, **kwargs)
def open_datasets(self): raise NotImplementedError('"Combine" feature has not yet been implemented')
def _zarr_kwargs_default(add_kwargs: dict = None) -> dict: """Add any default kwargs for specific requests.""" add_kwargs = add_kwargs or {} defaults = { "consolidated": False, } return defaults | add_kwargs def _fetch_kerchunk_make_local(href: str) -> dict: """ Fetch a kerchunk file, open as json content and do find/replace to access local files only. """ href_local = href.replace(CEDA_DAP_PREFIX, "") if not os.path.isfile(href_local): attempts = 0 success = False while attempts < 3 and not success: resp = requests.get(href) if resp.status_code == 200: success = True attempts += 1 if attempts >= 3 and not success: raise ValueError( f"File {href}: Download unsuccessful - " "could not download the file successfully (tried 3 times)" ) refs = json.loads(resp.text) else: with open(href_local) as f: refs = json.load(f) for key, v in refs["refs"].items(): if isinstance(v, list) and len(v) == 3: url = v[0] if isinstance(url, str) and url.startswith(CEDA_DAP_PREFIX + "/"): # Map URL to local path local_path = url.replace(CEDA_DAP_PREFIX + "/", "/") # Normalize path local_path = os.path.abspath(local_path) # Validate existence (critical for local_only mode) if not os.path.exists(local_path): raise FileNotFoundError( f"Missing local file for kerchunk reference: " f"{url} expected at: {local_path}" ) # Convert to proper file URI refs["refs"][key][0] = f"file://{local_path}" return refs