diff --git a/pygmt/datatypes/grid.py b/pygmt/datatypes/grid.py index aaaded4cf04..5e3ccd9bfe4 100644 --- a/pygmt/datatypes/grid.py +++ b/pygmt/datatypes/grid.py @@ -299,7 +299,7 @@ def to_dataarray(self) -> xr.DataArray: pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1] ] - # Default dims and grid name + # Default dimension names and grid name dims, name = ("y", "x"), "z" # The x and y coordinates @@ -308,13 +308,14 @@ def to_dataarray(self) -> xr.DataArray: "y": self.y[: header.n_rows], } - # Attrs for the coordinates + # Attributes for the coordinates and the data (i.e., gloabal attributes). + # Refer to the 'gmtnc_put_units' and 'gmtnc_get_units' functions in 'gmt_nc.c'. attrs = {"x": {}, "y": {}, "z": {}} - newdims = {"x": "x", "y": "y"} - for dim, nameunits in zip( - ("x", "y", "z"), - (header.x_units, header.y_units, header.z_units), - strict=False, + newdims = {"x": "x", "y": "y"} # Map default dims to new dims + for dim, nameunits in ( + ("x", header.x_units), + ("y", header.y_units), + ("z", header.z_units), ): long_name, units = _parse_nameunits(nameunits.decode()) if long_name: @@ -340,7 +341,7 @@ def to_dataarray(self) -> xr.DataArray: if grid.x[0] < grid.x[1] or grid.y[0] < grid.y[1]: grid = grid.sortby(list(grid.dims)) - # Assign coordiante attributes + # Assign coordinate attributes for dim in dims: grid[dim].attrs.update(attrs[dim]) @@ -351,7 +352,7 @@ def to_dataarray(self) -> xr.DataArray: and attrs["y"].get("standard_name") == "latitude" ): gtype = 1 - # update the dimension name + # Update the dimension names grid = grid.rename(newdims) # Set the gmt accesssor.