diff --git a/uxarray/core/dataarray.py b/uxarray/core/dataarray.py index 177f888bb..570a7a8d6 100644 --- a/uxarray/core/dataarray.py +++ b/uxarray/core/dataarray.py @@ -253,7 +253,7 @@ def to_geodataframe( f"for face-centered data." ) - if self.values.size == self.uxgrid.n_face: + if self._face_centered(): gdf, non_nan_polygon_indices = self.uxgrid.to_geodataframe( periodic_elements=periodic_elements, projection=projection, @@ -290,22 +290,11 @@ def to_geodataframe( gdf[var_name] = _data - elif self.values.size == self.uxgrid.n_node: - raise ValueError( - f"Data Variable with size {self.values.size} does not match the number of faces " - f"({self.uxgrid.n_face}. Current size matches the number of nodes. Consider running " - f"``UxDataArray.topological_mean(destination='face') to aggregate the data onto the faces." - ) - elif self.values.size == self.uxgrid.n_edge: - raise ValueError( - f"Data Variable with size {self.values.size} does not match the number of faces " - f"({self.uxgrid.n_face}. Current size matches the number of edges." - ) else: - # data is not mapped to raise ValueError( - f"Data Variable with size {self.values.size} does not match the number of faces " - f"({self.uxgrid.n_face}." + f"to_geodataframe() expects face_centered data; got {self.data_location} data " + f"(with sizes={dict(**self.sizes)}). Consider running " + "``UxDataArray.topological_mean(destination='face')`` to aggregate the data onto faces." ) return gdf @@ -612,24 +601,25 @@ def integrate( >>> uxds = ux.open_dataset("grid.ug", "centroid_pressure_data_ug") >>> integral = uxds["psi"].integrate() """ - if self.values.shape[-1] == self.uxgrid.n_face: + # TODO: support integration regardless of n_face dimension position, + # and remove the self.dims[-1] == "n_face" check. + # (uxarray/xarray features should be agnostic to dimension positions.) + if self._face_centered() and self.dims[-1] == "n_face": face_areas = self.uxgrid.face_areas.values # perform dot product between face areas and last dimension of data integral = np.einsum("i,...i", face_areas, self.values) - elif self.values.shape[-1] == self.uxgrid.n_node: - raise ValueError("Integrating data mapped to each node not yet supported.") - - elif self.values.shape[-1] == self.uxgrid.n_edge: - raise ValueError("Integrating data mapped to each edge not yet supported.") + elif not self._face_centered(): + raise ValueError( + "Integration of non-face_centered data is not yet supported. " + f"(Got {self.data_location} data with sizes={dict(**self.sizes)})" + ) else: raise ValueError( - f"The final dimension of the data variable does not match the number of nodes, edges, " - f"or faces. Expected one of " - f"{self.uxgrid.n_node}, {self.uxgrid.n_edge}, or {self.uxgrid.n_face}, " - f"but received {self.values.shape[-1]}" + "Integration of data with n_face not as the final dimension is not yet supported." + f"Got face_centered data, but the final dimension was {self.dims[-1]}, not 'n_face'." ) # construct a uxda with integrated quantity