-
Notifications
You must be signed in to change notification settings - Fork 235
Add an internal function to load GMT remote datasets #2200
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 46 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
188dd58
create load_earth_dataset.py and move functions over from earth_age.py
willschlitzer 79560e3
remove unused imports
willschlitzer f171dcc
shorten docstring
willschlitzer 39d8b25
add error handling for pixel/gridline only registrations
willschlitzer 31a728a
update earth_relief.py to use load_earth_dataset
willschlitzer 4ffbe82
change pylint disable argument
willschlitzer ffcf9f4
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer 16059d5
add test for incompatible registration/resolution to test_datasets_ea…
willschlitzer 2780568
fix test name
willschlitzer 6a08ca2
add test_earth_relief_incorrect_resolution_registration to test_datas…
willschlitzer e2378c3
modify test to check that grid name and attributes are properly set
willschlitzer d81a62f
modify test to check that grid name and attributes are properly set
willschlitzer 1bf3060
run make format
willschlitzer 46dedf0
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer 9dba27c
remove error handling from earth_relief.py that has been moved to loa…
willschlitzer cde524f
add synbath to parameters for test
willschlitzer f2e6bdf
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer 40ce1ed
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer 83f1f74
create classes for Resolutions and Dataset info in load_earth_dataset.py
willschlitzer 6062122
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer ac366d0
remove pylint comment
willschlitzer aeb2bf6
update import statements
willschlitzer 10da3bf
change import order
willschlitzer 59781d4
add class docstrings in load_earth_dataset.py
willschlitzer 0e6508a
fix error raising when registration not set
willschlitzer 8dd5fbc
Apply suggestions from code review
willschlitzer b9f511c
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer cd03c33
Apply suggestions from code review
willschlitzer 0c286c6
[format-command] fixes
actions-bot 886870a
Apply suggestions from code review
willschlitzer 4a2f3c1
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer f9b09ab
add class docstrings
willschlitzer 8af523f
add gebcosi to list of parameters for registration failure test
willschlitzer ba4a4fe
Update pygmt/datasets/load_earth_dataset.py
willschlitzer 40b9d03
Update pygmt/datasets/load_earth_dataset.py
willschlitzer 1af51e8
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer bb760d8
Apply suggestions from code review
willschlitzer 4d39bae
fix test for long_name
willschlitzer 2bef861
add attribute_datum to allow for varying numbers of datums
willschlitzer 65a212e
rewrite docstring
willschlitzer fe819cd
remove type hint for Python 3.8 compatibility
willschlitzer 2af61d3
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer da271a3
Apply suggestions from code review
willschlitzer bbaf95c
Apply suggestions from code review
willschlitzer 4d3f7d3
Apply suggestions from code review
willschlitzer 7b10624
run make format
willschlitzer 7be9eea
rename load_remote_dataset.py
willschlitzer 8302535
change name to extra attributes
willschlitzer 0051c5a
change extra attributes docstring
willschlitzer cc8249f
Apply suggestions from code review
willschlitzer 193b4f2
Merge branch 'main' into load-remote-dataset/load-earth-dataset
willschlitzer 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
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,204 @@ | ||
| """ | ||
| Internal function to load GMT remote datasets. | ||
| """ | ||
| from typing import Dict, NamedTuple | ||
|
|
||
| from pygmt.exceptions import GMTInvalidInput | ||
| from pygmt.helpers import kwargs_to_strings | ||
| from pygmt.io import load_dataarray | ||
| from pygmt.src import grdcut, which | ||
|
|
||
|
|
||
| class Resolution(NamedTuple): | ||
| """ | ||
| The available grid registrations for a given resolution and whether it is a | ||
| tiled grid. | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Attributes | ||
| ---------- | ||
| registrations : list | ||
| A list of the accepted registrations for a given resolution. | ||
| Can be either "pixel" or "gridline". | ||
|
|
||
| tiled : bool | ||
| States if the given resolution is tiled, which requires an | ||
| argument for ``region``." | ||
| """ | ||
|
|
||
| registrations: list | ||
| tiled: bool | ||
|
|
||
|
|
||
| class GMTRemoteDataset(NamedTuple): | ||
| """ | ||
| Standard information about a dataset and grid metadata. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| title : str | ||
| The title of the dataset, used in error messages. | ||
|
|
||
| name : str | ||
| The name assigned as an attribute to the DataArray. | ||
|
|
||
| long_name : str | ||
| The long name assigned as an attribute to the DataArray. | ||
|
|
||
| units : str | ||
| The units of the values in the DataArray. | ||
|
|
||
| resolutions : dict | ||
| Dictionary of available resolution as keys and the values are | ||
| Resolution objects. | ||
|
|
||
| attribute_datum : dict | ||
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| A dictionary of the datum or data for the axis on the dataset. | ||
| """ | ||
|
|
||
| title: str | ||
| name: str | ||
| long_name: str | ||
| units: str | ||
| resolutions: Dict[str, Resolution] | ||
| attribute_datum: dict | ||
|
|
||
|
|
||
| datasets = { | ||
| "earth_relief": GMTRemoteDataset( | ||
| title="Earth relief", | ||
| name="elevation", | ||
| long_name="Earth elevation relative to the geoid", | ||
| units="meters", | ||
| attribute_datum={"vertical_datum": "EMG96", "horizontal_datum": "WGS84"}, | ||
| resolutions={ | ||
| "01d": Resolution(["pixel", "gridline"], False), | ||
| "30m": Resolution(["pixel", "gridline"], False), | ||
| "20m": Resolution(["pixel", "gridline"], False), | ||
| "15m": Resolution(["pixel", "gridline"], False), | ||
| "10m": Resolution(["pixel", "gridline"], False), | ||
| "06m": Resolution(["pixel", "gridline"], False), | ||
| "05m": Resolution(["pixel", "gridline"], True), | ||
| "04m": Resolution(["pixel", "gridline"], True), | ||
| "03m": Resolution(["pixel", "gridline"], True), | ||
| "02m": Resolution(["pixel", "gridline"], True), | ||
| "01m": Resolution(["pixel", "gridline"], True), | ||
| "30s": Resolution(["pixel", "gridline"], True), | ||
| "15s": Resolution(["pixel"], True), | ||
| "03s": Resolution(["gridline"], True), | ||
| "01s": Resolution(["gridline"], True), | ||
| }, | ||
| ), | ||
| "earth_age": GMTRemoteDataset( | ||
| title="seafloor age", | ||
| name="seafloor_age", | ||
| long_name="age of seafloor crust", | ||
| units="Myr", | ||
| attribute_datum={"horizontal_datum": "WGS84"}, | ||
| resolutions={ | ||
| "01d": Resolution(["pixel", "gridline"], False), | ||
| "30m": Resolution(["pixel", "gridline"], False), | ||
| "20m": Resolution(["pixel", "gridline"], False), | ||
| "15m": Resolution(["pixel", "gridline"], False), | ||
| "10m": Resolution(["pixel", "gridline"], False), | ||
| "06m": Resolution(["pixel", "gridline"], False), | ||
| "05m": Resolution(["pixel", "gridline"], True), | ||
| "04m": Resolution(["pixel", "gridline"], True), | ||
| "03m": Resolution(["pixel", "gridline"], True), | ||
| "02m": Resolution(["pixel", "gridline"], True), | ||
| "01m": Resolution(["gridline"], True), | ||
| }, | ||
| ), | ||
| } | ||
|
|
||
|
|
||
| @kwargs_to_strings(region="sequence") | ||
| def _load_remote_dataset( | ||
| dataset_name, dataset_prefix, resolution, region, registration | ||
| ): | ||
| r""" | ||
| Load GMT remote datasets. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| dataset_name : str | ||
| The name for the dataset in the 'datasets' dictionary. | ||
|
|
||
| dataset_prefix : str | ||
| The prefix for the dataset that will be passed to the GMT C API. | ||
|
|
||
| resolution : str | ||
| The grid resolution. The suffix ``d``, ``m``, and ``s`` stand for | ||
| arc-degree, arc-minute, and arc-second respectively. | ||
|
|
||
| region : str or list | ||
| The subregion of the grid to load, in the forms of a list | ||
| [*xmin*, *xmax*, *ymin*, *ymax*] or a string *xmin/xmax/ymin/ymax*. | ||
| Required for tiled grids. | ||
|
|
||
| registration : str | ||
| Grid registration type. Either ``"pixel"`` for pixel registration or | ||
| ``"gridline"`` for gridline registration. Default is ``None``, where | ||
| a pixel-registered grid is returned unless only the | ||
| gridline-registered grid is available. | ||
|
|
||
| Returns | ||
| ------- | ||
| grid : :class:`xarray.DataArray` | ||
| The GMT remote dataset grid. | ||
|
|
||
| Note | ||
| ---- | ||
| The returned :class:`xarray.DataArray` doesn't support slice operation for | ||
| tiled grids. | ||
| """ | ||
|
|
||
| if registration in ("pixel", "gridline", None): | ||
| # If None, let GMT decide on Pixel/Gridline type | ||
| reg = f"_{registration[0]}" if registration else "" | ||
| else: | ||
| raise GMTInvalidInput( | ||
| f"Invalid grid registration: '{registration}', should be either " | ||
| "'pixel', 'gridline' or None. Default is None, where a " | ||
| "pixel-registered grid is returned unless only the " | ||
| "gridline-registered grid is available." | ||
| ) | ||
| dataset = datasets[dataset_name] | ||
| if resolution not in dataset.resolutions.keys(): | ||
| raise GMTInvalidInput(f"Invalid resolution '{resolution}'.") | ||
| if registration and ( | ||
| registration not in dataset.resolutions[resolution].registrations | ||
| ): | ||
| raise GMTInvalidInput( | ||
| f"{registration} registration is not available for the " | ||
| f"{resolution} {dataset.title} dataset. Only " | ||
| f"{dataset.resolutions[resolution].registrations[0]}" | ||
| " registration is available." | ||
| ) | ||
|
|
||
| # different ways to load tiled and non-tiled grids. | ||
| # Known issue: tiled grids don't support slice operation | ||
| # See https://github.com/GenericMappingTools/pygmt/issues/524 | ||
| if region is None: | ||
| if dataset.resolutions[resolution].tiled: | ||
| raise GMTInvalidInput( | ||
| f"'region' is required for {dataset.title}" | ||
| f"resolution '{resolution}'." | ||
| ) | ||
| fname = which(f"@{dataset_prefix}{resolution}{reg}", download="a") | ||
| grid = load_dataarray(fname, engine="netcdf4") | ||
| else: | ||
| grid = grdcut(f"@{dataset_prefix}{resolution}{reg}", region=region) | ||
|
|
||
| # Add some metadata to the grid | ||
| grid.name = dataset.name | ||
| grid.attrs["long_name"] = dataset.long_name | ||
| grid.attrs["units"] = dataset.units | ||
| for key, value in dataset.attribute_datum.items(): | ||
| grid.attrs[key] = value | ||
| # Remove the actual range because it gets outdated when indexing the grid, | ||
| # which causes problems when exporting it to netCDF for usage on the | ||
| # command-line. | ||
| grid.attrs.pop("actual_range") | ||
| for coord in grid.coords: | ||
| grid[coord].attrs.pop("actual_range") | ||
| return grid | ||
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.