[docs]@xr.register_dataarray_accessor("gmt")classGMTDataArrayAccessor:""" This is the GMT extension for :class:`xarray.DataArray`. You can access various GMT specific metadata about your grid as follows: >>> from pygmt.datasets import load_earth_relief >>> # Use the global Earth relief grid with 1 degree spacing >>> grid = load_earth_relief(resolution="01d") >>> # See if grid uses Gridline (0) or Pixel (1) registration >>> grid.gmt.registration 1 >>> # See if grid uses Cartesian (0) or Geographic (1) coordinate system >>> grid.gmt.gtype 1 """def__init__(self,xarray_obj):self._obj=xarray_objtry:self._source=self._obj.encoding["source"]# filepath to NetCDF sourceifnotPath(self._source).exists():raiseValueError(f"Grid source file {self._source} doesn't exist.")# Get grid registration and grid type from the last two columns of# the shortened summary information of `grdinfo`.self._registration,self._gtype=map(int,grdinfo(self._source,per_column="n").split()[-2:])except(KeyError,ValueError):self._registration=0# Default to Gridline registrationself._gtype=0# Default to Cartesian grid type@propertydefregistration(self):""" Registration type of the grid, either Gridline (0) or Pixel (1). """returnself._registration@registration.setterdefregistration(self,value):ifvaluein(0,1):self._registration=valueelse:raiseGMTInvalidInput(f"Invalid grid registration value: {value}, should be a boolean of ""either 0 for Gridline registration or 1 for Pixel registration")@propertydefgtype(self):""" Coordinate system type of the grid, either Cartesian (0) or Geographic (1). """returnself._gtype@gtype.setterdefgtype(self,value):ifvaluein(0,1):self._gtype=valueelse:raiseGMTInvalidInput(f"Invalid coordinate system type: {value}, should be a boolean of ""either 0 for Cartesian or 1 for Geographic")