-
Notifications
You must be signed in to change notification settings - Fork 49
add BGC-Argo+ dataset support to ArgoFloat.open_dataset()
#623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
RaphaelBajon
wants to merge
5
commits into
euroargodev:master
Choose a base branch
from
RaphaelBajon:feature/bgcargo-plus-store
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf8a72b
BGCArgo+ dataset inclusion, test and doc.
RaphaelBajon 7121ba6
update ftp path depending on the dataset version + add version check
RaphaelBajon f05830c
hardcoded fix issue/h5py for prof->point point->prof
RaphaelBajon 9aff879
disclose the AI model
RaphaelBajon 819c9d2
Merge branch 'master' into feature/bgcargo-plus-store
RaphaelBajon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| """ | ||
| BGC-Argo+ dataset store for :class:`argopy.ArgoFloat`. | ||
|
|
||
| The BGC-Argo+ dataset (https://www.bgc-argo-plus.info) is a quality-controlled, | ||
| outlier-removed version of BGC-Argo float data curated at SOEST / University of | ||
| Hawaiʻi at Mānoa (Bushinsky et al, 2025, Bushinsky et al, submitted). Individual float files are served on the SOEST FTP server:: | ||
|
|
||
| ftp://ftp.soest.hawaii.edu/bgc_argo_plus/outliers_removed/ | ||
|
|
||
| Usage | ||
| ----- | ||
| The typical access path is through :class:`argopy.ArgoFloat`:: | ||
|
|
||
| from argopy import ArgoFloat | ||
| ds = ArgoFloat(6903091).open_dataset('BGCArgoPlus') | ||
|
|
||
| You can also use the store directly:: | ||
|
|
||
| from argopy.stores.float.bgcargo_plus import BGCArgoPlusStore | ||
| store = BGCArgoPlusStore(6903091) | ||
| ds = store.open_dataset() | ||
| url = store.url | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| import xarray as xr | ||
|
|
||
| import numpy as np | ||
|
|
||
| from ...stores.implementations.ftp import ftpstore | ||
| from ...utils import check_wmo | ||
|
|
||
|
|
||
| def _decode_bytes_dataset(ds: xr.Dataset) -> xr.Dataset: | ||
| """Decode byte-string variables to str. | ||
|
|
||
| h5py returns fixed-length HDF5 string variables as numpy bytes dtype | ||
| (``'|S...'``) instead of str. This normalises the whole dataset at load | ||
| time so that all downstream argopy code can use ordinary string operations. | ||
| """ | ||
| updates = {} | ||
| for name, var in ds.data_vars.items(): | ||
| if var.dtype.kind == 'S': # fixed-length bytes, e.g. dtype='|S64' | ||
| decoded = np.char.decode(var.values, 'utf-8') | ||
| updates[name] = var.copy(data=decoded) | ||
| elif var.dtype.kind == 'O': # object array — may contain variable-length bytes | ||
| first = next( | ||
| ( | ||
| x for x in var.values.flat | ||
| if x is not None and not (isinstance(x, float) and np.isnan(x)) | ||
| ), | ||
| None, | ||
| ) | ||
| if isinstance(first, bytes): | ||
| decoded = np.vectorize( | ||
| lambda x: x.decode('utf-8') if isinstance(x, bytes) else x | ||
| )(var.values) | ||
| updates[name] = var.copy(data=decoded) | ||
| if updates: | ||
| ds = ds.assign(updates) | ||
|
|
||
| # PLATFORM_NUMBER is stored as a numeric string in HDF5 (e.g. '6903091') | ||
| # but argopy's uid() arithmetic requires it as an integer. | ||
| if 'PLATFORM_NUMBER' in ds.data_vars: | ||
| try: | ||
| pn = ds['PLATFORM_NUMBER'] | ||
| ds = ds.assign({ | ||
| 'PLATFORM_NUMBER': pn.copy( | ||
| data=np.char.strip(pn.values.astype(str)).astype(np.int64) | ||
| ) | ||
| }) | ||
| except (ValueError, TypeError): | ||
| pass # leave as-is if conversion fails (e.g. non-numeric WMO) | ||
|
|
||
| return ds | ||
|
|
||
| log = logging.getLogger("argopy.stores.BGCArgoPlusStore") | ||
|
|
||
| #: Root of the BGC-Argo+ FTP tree (no trailing slash) | ||
| BGCARGO_PLUS_FTP_HOST = "ftp.soest.hawaii.edu" | ||
|
|
||
| #: Path template on the FTP server. | ||
| #: ``{version}`` can be overridden via :attr:`BGCArgoPlusStore.version`. | ||
| BGCARGO_PLUS_PATH_TEMPLATE = ( | ||
| "/bgc_argo_plus/outliers_removed/{version}/{wmo}_Sprof_BGCArgoPlus.nc" # for versions v0.1_2025_12 and earlier | ||
| # "/bgc_argo_plus/outliers_removed/{version}/Individual_Floats/{wmo}_Sprof_BGCArgoPlus.nc" # for version v0.1_2026_04 and later, this is managed in the url function | ||
| ) | ||
|
|
||
| #: Default version tag that maps to the latest production release on the FTP. | ||
| BGCARGO_PLUS_DEFAULT_VERSION = "v0.1_2026_04" | ||
| SUPPORTED_VERSIONS = {BGCARGO_PLUS_DEFAULT_VERSION, 'v0.1_2025_12', 'v0.0_2025_09'} | ||
|
|
||
|
|
||
| def bgcargo_plus_url(wmo: int, version: str = BGCARGO_PLUS_DEFAULT_VERSION) -> str: | ||
| """Return the FTP URL for a BGC-Argo+ float file. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| wmo : int | ||
| Float WMO number. | ||
| version : str, optional | ||
| Dataset version string, e.g. ``"v0.1_2026_04"``. | ||
|
|
||
| Returns | ||
| ------- | ||
| str | ||
| Full FTP URL | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> from argopy.stores.float.bgcargo_plus import bgcargo_plus_url | ||
| >>> bgcargo_plus_url(6903091) | ||
| 'ftp://ftp.soest.hawaii.edu/bgc_argo_plus/outliers_removed/v0.1_2026_04/6903091_Sprof_BGCArgoPlus.nc' | ||
| """ | ||
| path = BGCARGO_PLUS_PATH_TEMPLATE.format(wmo=wmo, version=version) if version != 'v0.1_2026_04' else BGCARGO_PLUS_PATH_TEMPLATE.format(wmo=wmo, version=version+"/Individual_Floats") | ||
| return f"ftp://{BGCARGO_PLUS_FTP_HOST}{path}" | ||
|
|
||
|
|
||
| class BGCArgoPlusStore: | ||
| """Store that fetches BGC-Argo+ individual-float files from SOEST FTP. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| wmo : int or str | ||
| Float WMO number. | ||
| version : str, optional | ||
| BGC-Argo+ dataset version, default :data:`BGCARGO_PLUS_DEFAULT_VERSION`. | ||
| cache : bool, optional | ||
| Cache downloaded files locally (passed to :class:`ftpstore`). | ||
| cachedir : str, optional | ||
| Local cache directory (passed to :class:`ftpstore`). | ||
| timeout : int, optional | ||
| FTP connection timeout in seconds (passed to :class:`ftpstore`). | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> from argopy.stores.float.bgcargo_plus import BGCArgoPlusStore | ||
| >>> store = BGCArgoPlusStore(6903091) | ||
| >>> ds = store.open_dataset() | ||
| >>> store.url | ||
| 'ftp://ftp.soest.hawaii.edu/bgc_argo_plus/...' | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| wmo: int | str, | ||
| version: str = BGCARGO_PLUS_DEFAULT_VERSION, | ||
| cache: bool = False, | ||
| cachedir: str = "", | ||
| timeout: int = 0, | ||
| ): | ||
| self.WMO = check_wmo(wmo)[0] | ||
| self.version = version | ||
| self.cache = cache | ||
| self.cachedir = cachedir | ||
| self.timeout = timeout | ||
|
|
||
| if self.version not in SUPPORTED_VERSIONS: | ||
| raise ValueError( | ||
| f"Unsupported BGC-Argo+ version '{self.version}'. " | ||
| f"Supported versions: {sorted(SUPPORTED_VERSIONS)}" | ||
| ) | ||
|
|
||
| # Build the FTP store pointing at the SOEST server | ||
| ftp_root = f"{BGCARGO_PLUS_FTP_HOST}" | ||
| self._fs = ftpstore( | ||
| host=ftp_root, | ||
| cache=self.cache, | ||
| cachedir=self.cachedir if self.cachedir else None, | ||
| timeout=self.timeout if self.timeout else None, | ||
| ) | ||
|
|
||
| @property | ||
| def url(self) -> str: | ||
| """Full FTP URL of this float's BGC-Argo+ file.""" | ||
| return bgcargo_plus_url(self.WMO, version=self.version) | ||
|
|
||
| def open_dataset(self, **kwargs) -> xr.Dataset: | ||
| """Download and open the BGC-Argo+ netCDF file for this float. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| **kwargs | ||
| Additional keyword arguments forwarded to | ||
| :meth:`argopy.stores.ftpstore.open_dataset` (e.g. ``lazy=True``, | ||
| ``xr_opts={"decode_times": False}``). | ||
|
|
||
| Returns | ||
| ------- | ||
| :class:`xarray.Dataset` | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> from argopy.stores.float.bgcargo_plus import BGCArgoPlusStore | ||
| >>> ds = BGCArgoPlusStore(6903091).open_dataset() | ||
| """ | ||
| log.debug("BGCArgoPlusStore: fetching %s", self.url) | ||
| try: | ||
| ds = _decode_bytes_dataset(self._fs.open_dataset(self.url, **kwargs)) | ||
| except FileNotFoundError as exc: | ||
| raise FileNotFoundError( | ||
| f"Could not retrieve BGC-Argo+ file for WMO {self.WMO} " | ||
| f"(version='{self.version}') from {self.url}.\n" | ||
| f"Original error: {exc}" | ||
| ) from exc | ||
| return ds | ||
|
|
||
| def __repr__(self) -> str: | ||
| return ( | ||
| f"<BGCArgoPlusStore>\n" | ||
| f" WMO : {self.WMO}\n" | ||
| f" version : {self.version}\n" | ||
| f" URL : {self.url}\n" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmaze yes the
.argoextension recognizes the dataset. I just created another hardcoded function_decode_bytes_datasetwhile loading the'BGCArgoPlus'data to be able to go through.argo.profile2point()and back to.argo.point2profile().I was thinking you would have something already implemented in
argopyutilsthat handles the decode byte-string variables to str function of_decode_bytes_dataset.You can start a review, all pytests are working on my local machine.