Skip to content

Commit

Permalink
Improve the to_dataarray method
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Mar 15, 2024
1 parent c64aeed commit f2445d1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pygmt/datatypes/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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])

Expand All @@ -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.
Expand Down

0 comments on commit f2445d1

Please sign in to comment.