"""
This module provides functions to easily access ragged-array datasets.
"""

import xarray as xr


def gdp1h() -> xr.Dataset:
    """Returns the NOAA Global Drifter Program (GDP) hourly dataset as an Xarray
    dataset.

    The data is accessed from a public AWS S3 bucket accessible at
    https://registry.opendata.aws/noaa-oar-hourly-gdp/. This dataset includes
    corrections and additional metadata since the original submission of the
    dataset to NCEI (accessible via https://doi.org/10.25921/x46c-3620). We
    recommend using this dataset over the one distributed via NCEI.

    Returns
    -------
    xarray.Dataset
        Hourly GDP dataset as a ragged array

    Examples
    --------
    >>> from clouddrift.datasets import gdp1h
    >>> gdp1h()
    <xarray.Dataset>
    Dimensions:                (traj: 17324, obs: 165754333)
    Coordinates:
        ids                    (obs) int64 ...
        lat                    (obs) float32 ...
        lon                    (obs) float32 ...
        time                   (obs) datetime64[ns] ...
    Dimensions without coordinates: traj, obs
    Data variables: (12/55)
        BuoyTypeManufacturer   (traj) |S20 ...
        BuoyTypeSensorArray    (traj) |S20 ...
        CurrentProgram         (traj) float64 ...
        DeployingCountry       (traj) |S20 ...
        DeployingShip          (traj) |S20 ...
        DeploymentComments     (traj) |S20 ...
        ...                     ...
        sst1                   (obs) float64 ...
        sst2                   (obs) float64 ...
        typebuoy               (traj) |S10 ...
        typedeath              (traj) int8 ...
        ve                     (obs) float32 ...
        vn                     (obs) float32 ...
    Attributes: (12/16)
        Conventions:       CF-1.6
        acknowledgement:   Elipot, Shane; Sykulski, Adam; Lumpkin, Rick; Centurio...
        contributor_name:  NOAA Global Drifter Program
        contributor_role:  Data Acquisition Center
        date_created:      2022-12-09T06:02:29.684949
        doi:               10.25921/x46c-3620
        ...                ...
        processing_level:  Level 2 QC by GDP drifter DAC
        publisher_email:   aoml.dftr@noaa.gov
        publisher_name:    GDP Drifter DAC
        publisher_url:     https://www.aoml.noaa.gov/phod/gdp
        summary:           Global Drifter Program hourly data
        title:             Global Drifter Program hourly drifting buoy collection

    See Also
    --------
    :func:`gdp6h`
    """
    url = "https://noaa-oar-hourly-gdp-pds.s3.amazonaws.com/latest/gdp_v2.00.zarr"
    return xr.open_dataset(url, engine="zarr")


def gdp6h() -> xr.Dataset:
    """Returns the NOAA Global Drifter Program (GDP) 6-hourly dataset as an
    Xarray dataset.

    The data is accessed from a public HTTPS server at NOAA's Atlantic
    Oceanographic and Meteorological Laboratory (AOML) accessible at
    https://www.aoml.noaa.gov/phod/gdp/index.php.

    Returns
    -------
    xarray.Dataset
        6-hourly GDP dataset as a ragged array

    Examples
    --------
    >>> from clouddrift.datasets import gdp6h
    >>> gdp6h()
    <xarray.Dataset>
    Dimensions:                (traj: 26843, obs: 44544647)
    Coordinates:
        ids                    (obs) int64 ...
        time                   (obs) datetime64[ns] ...
        lon                    (obs) float32 ...
        lat                    (obs) float32 ...
    Dimensions without coordinates: traj, obs
    Data variables: (12/44)
        ID                     (traj) int64 ...
        rowsize                (traj) int32 ...
        WMO                    (traj) int32 ...
        expno                  (traj) int32 ...
        deploy_date            (traj) datetime64[ns] ...
        deploy_lat             (traj) float32 ...
        ...                     ...
        vn                     (obs) float32 ...
        temp                   (obs) float32 ...
        err_lat                (obs) float32 ...
        err_lon                (obs) float32 ...
        err_temp               (obs) float32 ...
        drogue_status          (obs) bool ...
    Attributes: (12/16)
        title:             Global Drifter Program six-hourly drifting buoy collec...
        history:           Last update July 2022.  Metadata from dirall.dat and d...
        Conventions:       CF-1.6
        date_created:      2022-12-08T18:44:27.784441
        publisher_name:    GDP Drifter DAC
        publisher_email:   aoml.dftr@noaa.gov
        ...                ...
        contributor_name:  NOAA Global Drifter Program
        contributor_role:  Data Acquisition Center
        institution:       NOAA Atlantic Oceanographic and Meteorological Laboratory
        acknowledgement:   Lumpkin, Rick; Centurioni, Luca (2019). NOAA Global Dr...
        summary:           Global Drifter Program six-hourly data
        doi:               10.25921/7ntx-z961

    See Also
    --------
    :func:`gdp1h`
    """
    url = "https://www.aoml.noaa.gov/ftp/pub/phod/buoydata/gdp_jul22_ragged_6h.nc#mode=bytes"
    return xr.open_dataset(url)
