UXARRAY / uxarray

Xarray-styled package for reading and directly operating on unstructured grid datasets following UGRID conventions
https://uxarray.readthedocs.io/
Apache License 2.0
142 stars 31 forks source link

UxDataArray.inverse_distance_weighted_remap returns UxDataset #776

Open ahijevyc opened 2 months ago

ahijevyc commented 2 months ago

Version

2024.04.0

How did you install UXarray?

Conda

What happened?

When I used plot.rasterize or plot.polygon on a UxDataArray, I got an error AttributeError: Unsupported Plotting Method: 'polygon'

What did you expect to happen?

Does UxDataArray.inverse_distance_weighted_remap have to return a UxDataset? I was wondering if it could return a UxDataArray, given a UxDataArray to begin with.

I noticed the change to UxDataset when trying to plot a smoothed UxDataArray. See code below.

Can you provide a MCVE to repoduce the bug?

import uxarray

grid_path = "/glade/campaign/mmm/parc/schwartz/MPAS/480km_mesh/x1.2562.grid.nc"
data_path = "/glade/campaign/mmm/parc/schwartz/MPAS/480km_mesh/x1.2562.static.nc"
uxds = uxarray.open_dataset(
    grid_path,
    data_path,
)
# UxDataArray from UxDataset
var = uxds["landmask"]
var.inverse_distance_weighted_remap(destination_obj=var).plot.polygon()
# .inverse_distance_weighted_remap returns UxDataset, not UxDataArray
philipc2 commented 2 months ago

Hi @ahijevyc

Can you try running the following?

var.inverse_distance_weighted_remap(destination_obj=var.uxgrid).plot.polygon()

This should return a UxDataArray

When you pass through the UxDataArray as the destination_obj, we are attempting to store both the remapped and original variable together in a single dataset. Doing it the above way always will return a new UxDataArray

Let me know if this works!

ahijevyc commented 2 months ago

I do now get a UxDataArray from var.inverse_distance_weighted_remap(destination_obj=var.uxgrid) However, the plot.polygon() trips up with an error:

/glade/work/ahijevyc/conda-envs/uxarray/lib/python3.11/site-packages/uxarray/remap/inverse_distance_weighted.py:190: UserWarning: Your data is stored on n_face, but you are remapping to nodes warnings.warn(

---------------------------------------------------------------------------AttributeError Traceback (most recent call last) Cell In[10], line 12 10 # UxDataArray from UxDataset 11 var = uxds["landmask"]---> 12 var.inverse_distance_weighted_remap(destination_obj=var.uxgrid).plot.polygon() 13 # returns UxDataset, not UxDataArray

File /glade/work/ahijevyc/conda-envs/uxarray/lib/python3.11/site-packages/uxarray/plot/accessor.py:286, in UxDataArrayPlotAccessor.getattr(self, name) 284 return getattr(xarray_plot_accessor, name) 285 else:--> 286 raise AttributeError(f"Unsupported Plotting Method: '{name}'") AttributeError: Unsupported Plotting Method: 'polygon'

On Mon, May 6, 2024 at 2:31 PM Philip Chmielowiec @.***> wrote:

Hi @ahijevyc https://github.com/ahijevyc

Can you try running the following?

var.inverse_distance_weighted_remap(destination_obj=var.uxgrid).plot.polygon()

This should return a UxDataArray

When you pass through the UxDataArray as the destination_obj, we are attempting to store both the remapped and original variable together in a single dataset. Doing it the above way always will return a new UxDataArray

Let me know if this works!

— Reply to this email directly, view it on GitHub https://github.com/UXARRAY/uxarray/issues/776#issuecomment-2096853900, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEIPI2APBJNR2WWMC3CE3W3ZA7SBLAVCNFSM6AAAAABHJI4FEOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJWHA2TGOJQGA . You are receiving this because you were mentioned.Message ID: @.***>

philipc2 commented 2 months ago

You want to set remap_to='face centers' to have the result stored on each face.

var.inverse_distance_weighted_remap(destination_obj=var.uxgrid, remap_to='face centers').plot.polygon()
ahijevyc commented 2 months ago

I added that, but still got the same error:

---> 12 var.inverse_distance_weighted_remap(destination_obj=var.uxgrid, remap_to="face centers").plot.polygon() 13 # returns UxDataset, not UxDataArray

File /glade/work/ahijevyc/conda-envs/uxarray/lib/python3.11/site-packages/uxarray/plot/accessor.py:286, in UxDataArrayPlotAccessor.getattr(self, name) 284 return getattr(xarray_plot_accessor, name) 285 else:--> 286 raise AttributeError(f"Unsupported Plotting Method: '{name}'") AttributeError: Unsupported Plotting Method: 'polygon'

On Mon, May 6, 2024 at 3:40 PM Philip Chmielowiec @.***> wrote:

You want to set remap_to='face centers' to have the result stored on each face.

https://github.com/UXARRAY/uxarray/blob/e240f7a3d9cb89e1ef201bd11df61279ca893a9d/uxarray/remap/dataarray_accessor.py#L52-L80

— Reply to this email directly, view it on GitHub https://github.com/UXARRAY/uxarray/issues/776#issuecomment-2096971074, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEIPI2HOLCBFZBT6E5GIH63ZA72FJAVCNFSM6AAAAABHJI4FEOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJWHE3TCMBXGQ . You are receiving this because you were mentioned.Message ID: @.***>

philipc2 commented 2 months ago

I was able to get this generated on Casper.

uxds["landmask"].remap.inverse_distance_weighted(destination_obj=uxds.uxgrid, remap_to='face centers').plot.polygons()
image

Can you give that a try?

philipc2 commented 2 months ago

It looks like the remapping isn't actually doing anything though. Looking into it further.

ahijevyc commented 1 month ago

Do you have a workaround for this?

On Mon, May 6, 2024 at 5:36 PM Philip Chmielowiec @.***> wrote:

It appears that the remapping is functioning normally (new values are returned in the data array), but the plotting functions are using the existing value of the data variable (i.e. landmask) and is not plotting the new one.

I'll get a fix for this out, it's an issue with some of our internal caching.

— Reply to this email directly, view it on GitHub https://github.com/UXARRAY/uxarray/issues/776#issuecomment-2097097553, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEIPI2DRUV7J7S6DQCZ4CM3ZBAHY3AVCNFSM6AAAAABHJI4FEOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJXGA4TONJVGM . You are receiving this because you were mentioned.Message ID: @.***>

philipc2 commented 1 month ago

Do you have a workaround for this? On Mon, May 6, 2024 at 5:36 PM Philip Chmielowiec @.> wrote: It appears that the remapping is functioning normally (new values are returned in the data array), but the plotting functions are using the existing value of the data variable (i.e. landmask) and is not plotting the new one. I'll get a fix for this out, it's an issue with some of our internal caching. — Reply to this email directly, view it on GitHub <#776 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEIPI2DRUV7J7S6DQCZ4CM3ZBAHY3AVCNFSM6AAAAABHJI4FEOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJXGA4TONJVGM . You are receiving this because you were mentioned.Message ID: @.>

Thanks for the follow up. Pinging @aaronzedwick and we can take a look into it this week.

philipc2 commented 1 month ago

@aaronzedwick

I noticed the change to UxDataset when trying to plot a smoothed UxDataArray. See code below.

This is related to what we talked about on Monday when we met, there appear to be issues when running IDW on the same source and destination grid.

aaronzedwick commented 1 month ago

@aaronzedwick

I noticed the change to UxDataset when trying to plot a smoothed UxDataArray. See code below.

This is related to what we talked about on Monday when we met, there appear to be issues when running IDW on the same source and destination grid.

Could it be that when we try and remap the variable to the same dataset, it is using the same exact variable name, so there is an error because it already exists and thus tries to overwrite it, at the same time as it is being read from?