GenericMappingTools / GenericMappingTools/pygmt
Better return values for grdinfo
- Dominant language
- Python
- Stars
- 874
- Forks
- 255
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 40
Description
**Description of the desired feature**
Certain [`pygmt.grdinfo`](https://www.pygmt.org/v0.1.2/api/generated/pygmt.grdinfo.html) return values can be in an array form rather than a string. This would be a good first issue to take up, and would be similar to #575 but for grids instead of tables.
_Originally posted by @liamtoney in https://github.com/GenericMappingTools/pygmt/issues/147#issuecomment-450018956_
> **tl;dr**
>
> After reading the above discussion, I would like to work on more fully wrapping `grdinfo`. It seems like a potentially good "first issue," and I can see a benefit in wrapping it even with Python containers also available. See below, for example.
>
> **Motivation**
>
> In my use case, I want to easily **see** the min/max z-values of a DEM so that I can choose my colorbar limits.
>
> **1. No specified region**
>
> With `GRIDFILE` being the full path to my grid file, I could use
> ```python
> import gmt
> print(gmt.grdinfo(GRIDFILE).strip())
> ```
> ```
> ...
> ../srtm15_plus_w180n90.nc: z_min: -7840.125 z_max: 6057.64355469 name: z
> ...
> ```
> or (with `xarray`, for example)
> ```python
> import xarray
> print(xarray.open_dataset(GRIDFILE).z.actual_range)
> ```
> ```
> [-7840.125 6057.64355469]
> ```
> These are about equal effort (and `xarray` returns things in a more usable output).
>
> **2. Specified region of interest**
>
> For viewing the min/maz z-values within a specified `REGION = (lonmin, lonmax, latmin, latmax)`, I think that `grdinfo` makes this easier:
> ```python
> import gmt
> print(gmt.grdinfo(GRIDFILE, R='{}/{}/{}/{}'.format(*REGION)).strip())
> ```
> ```
> ...
> ../srtm15_plus_w180n90.nc: z_min: -4996.11816406 z_max: 6057.64355469 name: z
> ...
> ```
> vs.
> ```python
> import xarray
> z = xarray.open_dataset(GRIDFILE).sel(lon=slice(*REGION[0:2]), lat=slice(*REGION[2:4])).z.values
> print(z.min(), z.max())
> ```
> ```
> -4996.118 6057.6436
> ```
> Plus `grdinfo` is more format agnostic. By minimally including the `@use_alias` and `@kwargs_to_strings` decorators, the `grdinfo` command above could be just
> ```python
> print(gmt.grdinfo(GRIDFILE, region=REGION).strip())
> ```
> and (with more work) could perhaps output a dictionary? Please let me know if this is a worthwhile effort and I'll make a PR for [`modules.py`](https://github.com/GenericMappingTools/gmt-python/blob/master/gmt/modules.py).
**Are you willing to help implement and maintain this feature?** Yes/No
Contributor guide
Assessment
This issue has not been assessed yet.