-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pygmt.read to read a dataset/grid/image into pandas.DataFrame/xar…
…ray.DataArray
- Loading branch information
Showing
5 changed files
with
84 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -172,6 +172,7 @@ Input/output | |
:toctree: generated | ||
|
||
load_dataarray | ||
read | ||
|
||
GMT Defaults | ||
------------ | ||
|
This file contains 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 |
---|---|---|
|
@@ -54,6 +54,7 @@ | |
makecpt, | ||
nearneighbor, | ||
project, | ||
read, | ||
select, | ||
sph2grd, | ||
sphdistance, | ||
|
This file contains 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 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 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,76 @@ | ||
""" | ||
Read data from files | ||
""" | ||
|
||
from typing import Literal | ||
|
||
import pandas as pd | ||
import xarray as xr | ||
from pygmt.clib import Session | ||
|
||
|
||
def read( | ||
file, kind: Literal["dataset", "grid", "image"], **kwargs | ||
) -> pd.DataFrame | xr.DataArray: | ||
""" | ||
Read a dataset, grid, or image from a file and return the appropriate object. | ||
For datasets, it returns a :class:`pandas.DataFrame`. For grids and images, | ||
it returns a :class:`xarray.DataArray`. | ||
Parameters | ||
---------- | ||
file | ||
The file name to read. | ||
kind | ||
The kind of data to read. Valid values are ``"dataset"``, ``"grid"``, | ||
and ``"image"``. | ||
For datasets, the following keyword arguments are supported: | ||
column_names | ||
A list of column names. | ||
header | ||
Row number containing column names. ``header=None`` means not to parse the | ||
column names from table header. Ignored if the row number is larger than the | ||
number of headers in the table. | ||
dtype | ||
Data type. Can be a single type for all columns or a dictionary mapping | ||
column names to types. | ||
index_col | ||
Column to set as index. | ||
Returns | ||
------- | ||
Return type depends on the ``kind`` argument: | ||
- ``"dataset"``: :class:`pandas.DataFrame` | ||
- ``"grid"`` or ``"image"``: :class:`xarray.DataArray` | ||
Examples | ||
-------- | ||
Read a dataset into a :class:`pandas.DataFrame` object: | ||
>>> from pygmt import read | ||
>>> df = read("@hotspots.txt", kind="dataset") | ||
>>> type(df) | ||
<class 'pandas.core.frame.DataFrame'> | ||
Read a grid into an :class:`xarray.DataArray` object: | ||
>>> dataarray = read("@earth_relief_01d", kind="grid") | ||
>>> type(dataarray) | ||
<class 'xarray.core.dataarray.DataArray'> | ||
""" | ||
code = {"dataset": "d", "grid": "g", "image": "i"}[kind] | ||
|
||
with Session() as lib: | ||
with lib.virtualfile_out(kind=kind) as voutfile: | ||
lib.call_module("read", args=[file, voutfile, f"-T{code}"]) | ||
|
||
match kind: | ||
case "dataset": | ||
return lib.virtualfile_to_dataset(vfname=voutfile, **kwargs) | ||
case "grid" | "image": | ||
return lib.virtualfile_to_raster(vfname=voutfile, kind=kind) |