-
Notifications
You must be signed in to change notification settings - Fork 9
[FEAT] New NAC reader #134
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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,188 @@ | ||
| """North Atlantic Current (NAC) data reader for AMOCatlas. | ||
|
|
||
| This module provides functions to read and process the North Atlantic current time series from satellite and float observations. | ||
| The NAC is a key component of the Atlantic Meridional Overturning Circulation, transporting warm, saline water from the tropics to the high northern latitudes. | ||
|
|
||
| The dataset includes NAC transport estimates from satellite and float observations and an NAC estimation from satellite altimetry alone. | ||
|
|
||
| Key functions: | ||
| - read_nac(): Main data loading interface for North Atlantic Current data | ||
|
|
||
| Data source: Satellite and float observations | ||
| Location: Between tip of Greenland and northern Spain | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
| from typing import Union | ||
|
|
||
| import xarray as xr | ||
|
|
||
| # Import the modules used | ||
| from amocatlas import logger, utilities | ||
| from amocatlas.logger import log_error, log_info, log_warning | ||
| from amocatlas.utilities import apply_defaults | ||
| from amocatlas.reader_utils import ReaderUtils | ||
|
|
||
| log = logger.log # Use the global logger | ||
|
|
||
| # Datasource identifier for automatic standardization | ||
| DATASOURCE_ID = "nac" | ||
|
|
||
| # Default list of NAC data files | ||
| NAC_DEFAULT_FILES = ["_2_1.nc"] | ||
| NAC_TRANSPORT_FILES = ["_2_1.nc"] | ||
| NAC_DEFAULT_SOURCE = "https://library.ucsd.edu/dc/object/bb6635909m" | ||
|
|
||
| NAC_METADATA = { | ||
| "project": "North Atlantic Current Time Series from Satellite and Float Observations (1993-2025)", | ||
| "weblink": "https://library.ucsd.edu/dc/object/bb6635909m", | ||
| "comment": "Dataset accessed and processed via http://github.com/AMOCcommunity/amocatlas", | ||
| } | ||
|
|
||
| NAC_FILE_METADATA = { | ||
| "_2_1.nc": { | ||
| "data_product": "6-monthly estimates of NAC transport from satellite altimetry and float observations", | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @apply_defaults(NAC_DEFAULT_SOURCE, NAC_DEFAULT_FILES) | ||
| def read_nac( | ||
| source: Union[str, Path, None], | ||
| file_list: Union[str, list[str]], | ||
| transport_only: bool = True, | ||
| data_dir: Union[str, Path, None] = None, | ||
| redownload: bool = False, | ||
| track_added_attrs: bool = False, | ||
| ) -> list[xr.Dataset]: | ||
| """Load the NAC (North Atlantic Current) transport datasets from a URL or local file path into xarray Datasets. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| source : str, optional | ||
| Local path to the data directory (remote source is handled per-file). | ||
|
|
||
| file_list : str or list of str, optional | ||
| Filename or list of filenames to process. | ||
| Defaults to NAC_DEFAULT_FILES. | ||
|
|
||
| transport_only : bool, optional | ||
| If True, restrict to transport files only. | ||
|
|
||
| data_dir : str, Path or None, optional | ||
| Optional local data directory. | ||
|
|
||
| redownload : bool, optional | ||
| If True, force redownload of the data. | ||
| track_added_attrs : bool, optional | ||
| If True, track which attributes were added during metadata enrichment. | ||
|
|
||
| Returns | ||
| ------- | ||
| list of xr.Dataset | ||
| List of loaded xarray datasets with basic inline and file-specific metadata. | ||
| And if track_added_attrs is True, also returns a list of dictionaries with the attributes that were added to each dataset during metadata enrichment. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If no source is provided for a file and no default URL mapping is found. | ||
|
|
||
| FileNotFoundError | ||
| If the file cannot be downloaded or does not exist locally. | ||
|
|
||
| """ | ||
| log.info("Starting to read NAC dataset") | ||
|
|
||
| # Load YAML metadata with fallback | ||
| global_metadata, yaml_file_metadata = ReaderUtils.load_array_metadata_with_fallback( | ||
| DATASOURCE_ID, NAC_METADATA | ||
| ) | ||
|
|
||
| # Ensure file_list has a default | ||
| if file_list is None: | ||
| file_list = NAC_DEFAULT_FILES | ||
| if transport_only: | ||
| file_list = NAC_TRANSPORT_FILES | ||
| if isinstance(file_list, str): | ||
| file_list = [file_list] | ||
| # Determine the local storage path | ||
| local_data_dir = Path(data_dir) if data_dir else utilities.get_default_data_dir() | ||
| local_data_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Print information about files being loaded | ||
| ReaderUtils.print_loading_info(file_list, DATASOURCE_ID, NAC_FILE_METADATA) | ||
|
|
||
| datasets = [] | ||
|
|
||
| added_attrs_per_dataset = [] if track_added_attrs else None | ||
| for file in file_list: | ||
| if not (file.lower().endswith(".nc")): | ||
| log_warning("Skipping unsupported file type : %s", file) | ||
| continue | ||
|
|
||
| download_url = ( | ||
| f"{source.rstrip('/')}/{file}" if utilities.is_valid_url(source) else None | ||
| ) | ||
|
|
||
| file_path = utilities.resolve_file_path( | ||
| file_name=file, | ||
| source=source, | ||
| download_url=download_url, | ||
| local_data_dir=local_data_dir, | ||
| redownload=redownload, | ||
| ) | ||
|
|
||
| # Open dataset | ||
|
|
||
| if file.lower().endswith(".nc"): | ||
| # Use ReaderUtils for consistent dataset loading | ||
|
|
||
| ds = ReaderUtils.safe_load_dataset(file_path) | ||
| # Attach metadata | ||
| # Attach metadata with optional tracking | ||
|
|
||
| if track_added_attrs: | ||
|
|
||
| ds, attr_changes = ReaderUtils.attach_metadata_with_tracking( | ||
| ds, | ||
| file, | ||
| file_path, | ||
| global_metadata, | ||
| yaml_file_metadata, | ||
| NAC_FILE_METADATA, | ||
| DATASOURCE_ID, | ||
| track_added_attrs=True, | ||
| ) | ||
|
|
||
| added_attrs_per_dataset.append(attr_changes) | ||
|
|
||
| else: | ||
|
|
||
| ds = ReaderUtils.attach_metadata_with_tracking( | ||
| ds, | ||
| file, | ||
| file_path, | ||
| global_metadata, | ||
| yaml_file_metadata, | ||
| NAC_FILE_METADATA, | ||
| DATASOURCE_ID, | ||
| track_added_attrs=False, | ||
| ) | ||
| else: | ||
| raise ValueError( | ||
| f"Unsupported file type for {file}. Only .nc files are supported." | ||
| ) | ||
|
|
||
| datasets.append(ds) | ||
|
|
||
| if not datasets: | ||
| log_error("No valid NAC files in %s", file_list) | ||
| raise FileNotFoundError(f"No valid data files found in {file_list}") | ||
|
|
||
| log_info("Successfully loaded %d NAC dataset(s)", len(datasets)) | ||
|
|
||
| if track_added_attrs: | ||
| return datasets, added_attrs_per_dataset | ||
| else: | ||
| return datasets | ||
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,39 @@ | ||
| metadata: | ||
| program: "NAC" | ||
| description: "North Atlantic Current Time Series from Satellite and Float Observations (1993-2025)" | ||
| project: "Lankhorst, Matthias (2025). North Atlantic Current Time Series from Satellite and Float Observations (1993-2025). " | ||
| weblink: https://library.ucsd.edu/dc/object/bb6635909m | ||
| comment: Dataset accessed and processed via http://github.com/AMOCcommunity/amocatlas | ||
| acknowledgment: > | ||
| Earlier versions of this dataset were created with support from the European Commission through awards EVK2-CT-2000-00087 and EVR1-CT-2001-40014 (projects 'GYROSCOPE' and 'ANIMATE'). Updated versions were partially supported through award NA15OAR4320071 from U.S. NOAA OOMD. | ||
| citation: > | ||
| Lankhorst, Matthias (2025). North Atlantic Current Time Series from Satellite and Float Observations (1993-2025). In North Atlantic Current Time Series from Satellite and Float Observations. UC San Diego Library Digital Collections. https://doi.org/10.6075/J0D79CCG | ||
| license: | ||
| featureType: timeSeries | ||
| time_coverage_start: '1993-01-01' | ||
| time_coverage_end: '2025-07-02' | ||
|
|
||
| files: | ||
| _2_1.nc: | ||
| source_url: https://library.ucsd.edu/dc/object/bb6635909m/ | ||
| data_product: "6-monthly mean NAC transport time series (1993-2025) estimated from satellite and float observations" | ||
| variable_mapping: | ||
| "NAC": TRANS_NAC | ||
| "NAC_UNCERTAINTY": TRANS_NAC_UNCERTAINTY | ||
| "NAC_PROXY": TRANS_NAC_PROXY | ||
| original_variable_metadata: | ||
| NAC: | ||
| long_name: "NAC Transport" | ||
| description: "North Atlantic Current transport time series from satellite and float observations" | ||
| units: Sverdrup | ||
| standard_name: ocean_volume_transport_across_line | ||
| NAC_UNCERTAINTY: | ||
| long_name: "Uncertainty of values in NAC variable" | ||
| description: "Uncertainty of North Atlantic Current transport time series" | ||
| units: Sverdrup | ||
| standard_name: ocean_volume_transport_across_line_uncertainty | ||
| NAC_PROXY: | ||
| long_name: "NAC Transport Proxy" | ||
| description: "Proxy for North Atlantic Current transport time series from satellite altimetry" | ||
| units: Sverdrup | ||
| standard_name: ocean_volume_transport_across_line |
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
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
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
Binary file not shown.
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.
Uh oh!
There was an error while loading. Please reload this page.