|
| 1 | +""" |
| 2 | +Function to download the Earth seafloor age datasets from the GMT data server, |
| 3 | +and load as :class:`xarray.DataArray`. |
| 4 | +
|
| 5 | +The grids are available in various resolutions. |
| 6 | +""" |
| 7 | +from pygmt.exceptions import GMTInvalidInput |
| 8 | +from pygmt.helpers import kwargs_to_strings |
| 9 | +from pygmt.io import load_dataarray |
| 10 | +from pygmt.src import grdcut, which |
| 11 | + |
| 12 | + |
| 13 | +@kwargs_to_strings(region="sequence") |
| 14 | +def load_earth_age(resolution="01d", region=None, registration=None): |
| 15 | + r""" |
| 16 | + Load Earth seafloor crustal ages in various resolutions. |
| 17 | +
|
| 18 | + The grids are downloaded to a user data directory |
| 19 | + (usually ``~/.gmt/server/earth/earth_age/``) the first time you invoke |
| 20 | + this function. Afterwards, it will load the grid from the data directory. |
| 21 | + So you'll need an internet connection the first time around. |
| 22 | +
|
| 23 | + These grids can also be accessed by passing in the file name |
| 24 | + **@earth_age**\_\ *res*\[_\ *reg*] to any grid plotting/processing |
| 25 | + function. *res* is the grid resolution (see below), and *reg* is grid |
| 26 | + registration type (**p** for pixel registration or **g** for gridline |
| 27 | + registration). |
| 28 | +
|
| 29 | + Refer to |
| 30 | + :gmt-docs:`datasets/remote-data.html#global-earth-seafloor-crustal-age-grids` |
| 31 | + for more details. |
| 32 | +
|
| 33 | + Parameters |
| 34 | + ---------- |
| 35 | + resolution : str |
| 36 | + The grid resolution. The suffix ``d`` and ``m`` stand for |
| 37 | + arc-degree, arc-minute and arc-second. It can be ``'01d'``, ``'30m'``, |
| 38 | + ``'20m'``, ``'15m'``, ``'10m'``, ``'06m'``, ``'05m'``, ``'04m'``, |
| 39 | + ``'03m'``, ``'02m'``, or ``'01m'``. |
| 40 | +
|
| 41 | + region : str or list |
| 42 | + The subregion of the grid to load, in the forms of a list |
| 43 | + [*xmin*, *xmax*, *ymin*, *ymax*] or a string *xmin/xmax/ymin/ymax*. |
| 44 | + Required for grids with resolutions higher than 5 |
| 45 | + arc-minute (i.e., ``05m``). |
| 46 | +
|
| 47 | + registration : str |
| 48 | + Grid registration type. Either ``pixel`` for pixel registration or |
| 49 | + ``gridline`` for gridline registration. Default is ``None``, where |
| 50 | + a pixel-registered grid is returned unless only the |
| 51 | + gridline-registered grid is available. |
| 52 | +
|
| 53 | + Returns |
| 54 | + ------- |
| 55 | + grid : :class:`xarray.DataArray` |
| 56 | + The Earth seafloor crustal age grid. Coordinates are latitude and |
| 57 | + longitude in degrees. Age is in millions of years (Myr). |
| 58 | +
|
| 59 | + Notes |
| 60 | + ----- |
| 61 | + The :class:`xarray.DataArray` grid doesn't support slice operation, for |
| 62 | + Earth seafloor crustal age with resolutions of 5 arc-minutes or higher, |
| 63 | + which are stored as smaller tiles. |
| 64 | + """ |
| 65 | + |
| 66 | + # earth seafloor crust age data stored as single grids for low resolutions |
| 67 | + non_tiled_resolutions = ["01d", "30m", "20m", "15m", "10m", "06m"] |
| 68 | + # earth seafloor crust age data stored as tiles for high resolutions |
| 69 | + tiled_resolutions = ["05m", "04m", "03m", "02m", "01m"] |
| 70 | + |
| 71 | + if registration in ("pixel", "gridline", None): |
| 72 | + # If None, let GMT decide on Pixel/Gridline type |
| 73 | + reg = f"_{registration[0]}" if registration else "" |
| 74 | + else: |
| 75 | + raise GMTInvalidInput( |
| 76 | + f"Invalid grid registration: '{registration}', should be either " |
| 77 | + "'pixel', 'gridline' or None. Default is None, where a " |
| 78 | + "pixel-registered grid is returned unless only the " |
| 79 | + "gridline-registered grid is available." |
| 80 | + ) |
| 81 | + |
| 82 | + if resolution not in non_tiled_resolutions + tiled_resolutions: |
| 83 | + raise GMTInvalidInput(f"Invalid Earth relief resolution '{resolution}'.") |
| 84 | + |
| 85 | + # Choose earth relief data prefix |
| 86 | + earth_age_prefix = "earth_age_" |
| 87 | + |
| 88 | + # different ways to load tiled and non-tiled earth relief data |
| 89 | + # Known issue: tiled grids don't support slice operation |
| 90 | + # See https://github.com/GenericMappingTools/pygmt/issues/524 |
| 91 | + if region is None: |
| 92 | + if resolution not in non_tiled_resolutions: |
| 93 | + raise GMTInvalidInput( |
| 94 | + f"'region' is required for Earth age resolution '{resolution}'." |
| 95 | + ) |
| 96 | + fname = which(f"@earth_age_{resolution}{reg}", download="a") |
| 97 | + grid = load_dataarray(fname, engine="netcdf4") |
| 98 | + else: |
| 99 | + grid = grdcut(f"@{earth_age_prefix}{resolution}{reg}", region=region) |
| 100 | + |
| 101 | + # Add some metadata to the grid |
| 102 | + grid.name = "seafloor_age" |
| 103 | + grid.attrs["long_name"] = "age of seafloor crust" |
| 104 | + grid.attrs["units"] = "Myr" |
| 105 | + grid.attrs["vertical_datum"] = "EMG96" |
| 106 | + grid.attrs["horizontal_datum"] = "WGS84" |
| 107 | + # Remove the actual range because it gets outdated when indexing the grid, |
| 108 | + # which causes problems when exporting it to netCDF for usage on the |
| 109 | + # command-line. |
| 110 | + grid.attrs.pop("actual_range") |
| 111 | + for coord in grid.coords: |
| 112 | + grid[coord].attrs.pop("actual_range") |
| 113 | + return grid |
0 commit comments