pygmt.clib.Session.virtualfile_to_raster

Session.virtualfile_to_raster(vfname, kind='grid', outgrid=None)[source]

Output raster data stored in a virtual file to an xarray.DataArray object.

The raster data can be a grid, an image or a cube.

Parameters:
  • vfname (str) – The virtual file name that stores the result grid/image/cube.

  • kind (Literal['grid', 'image', 'cube', None], default: 'grid') – Type of the raster data. Valid values are "grid", "image", "cube" or None. If None, will inquire the data type from the virtual file name.

  • outgrid (str | None, default: None) – Name of the output grid/image/cube. If specified, it means the raster data was already saved into an actual file and will return None.

Return type:

DataArray | None

Returns:

result – The result grid/image/cube. If outgrid is specified, return None.

Examples

>>> from pathlib import Path
>>> from pygmt.clib import Session
>>> from pygmt.helpers import GMTTempFile
>>> with Session() as lib:
...     # file output
...     with GMTTempFile(suffix=".nc") as tmpfile:
...         outgrid = tmpfile.name
...         with lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd:
...             lib.call_module("read", f"@earth_relief_01d_g {voutgrd} -Tg")
...             result = lib.virtualfile_to_raster(
...                 vfname=voutgrd, outgrid=outgrid
...             )
...             assert result == None
...             assert Path(outgrid).stat().st_size > 0
...
...     # xarray.DataArray output
...     outgrid = None
...     with lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd:
...         lib.call_module("read", f"@earth_relief_01d_g {voutgrd} -Tg")
...         result = lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
...         assert isinstance(result, xr.DataArray)