Skip to content

MPAS Reader Updates & Additional Coordinate Attributes - #455

Merged
philipc2 merged 31 commits into
mainfrom
philipc2/mpas-updates
Oct 19, 2023
Merged

philipc2 merged 31 commits into
mainfrom
philipc2/mpas-updates

Conversation

@philipc2

@philipc2 philipc2 commented Sep 18, 2023 •

Copy link
Copy Markdown
Member

Closes #457, #456, #454

Overview

Grid Improvments

  • Added access properties for the following:
    • Mesh2_face_cart_x/y/z
    • Mesh2_edge_cart_x/y/z
    • Mesh2_edge_x/y/z
    • Mesh2_edge_node_distances and Mesh2_edge_face_distances
  • MPAS Parsing
    • General parsing improvements
    • Added masking for invalid faces (initial solution until we have more sophisticated correction methods in place)

PR Checklist

General

  • An issue is linked created and linked
  • Add appropriate labels
  • Filled out Overview and Expected Usage (if applicable) sections

Testing

  • Adequate tests are created if there is new functionality
  • Tests cover all possible logical paths in your function
  • Tests are not too basic (such as simply calling a function and nothing else)

Documentation

  • Docstrings have been added to all new functions
  • Docstrings have updated with any function changes
  • Internal functions have a preceding underscore (_) and have been added to docs/internal_api/index.rst
  • User functions have been added to docs/user_api/index.rst

Examples

  • Any new notebook examples added to docs/examples/ folder
  • Clear the output of all cells before committing
  • New notebook files added to docs/examples.rst toctree
  • New notebook files added to new entry in docs/gallery.yml with appropriate thumbnail photo in docs/_static/thumbnails/

@philipc2 philipc2 self-assigned this Sep 18, 2023
@philipc2 philipc2 changed the title DRAFT: MPAS Reader Updates DRAFT: MPAS Reader Updates & Additional Coordinate Attributes Sep 18, 2023
@philipc2 philipc2 linked an issue Sep 18, 2023 that may be closed by this pull request
@philipc2 philipc2 changed the title DRAFT: MPAS Reader Updates & Additional Coordinate Attributes MPAS Reader Updates & Additional Coordinate Attributes Oct 3, 2023
@philipc2 philipc2 added this to the MPAS I/O milestone Oct 3, 2023
@xylar

xylar commented Oct 11, 2023

Copy link
Copy Markdown
Collaborator

@philipc2, it's possible that I'm just misusing uxarray but the following doesn't work, and I would have expected it to:

#!/usr/bin/env python3
import uxarray as ux

mpas_dataset_filepath = "test/meshfiles/mpas/QU/oQU480.231010.nc"

uxds = ux.open_dataset(mpas_dataset_filepath, mpas_dataset_filepath,
                       use_dual=True)
gdf_grid = uxds.uxgrid.to_geodataframe()
gdf_lat = uxds['latVertex'].to_geodataframe()

I see:

Traceback (most recent call last):
  File "/home/xylar/code/uxarray/philipc2/mpas-updates/./test3.py", line 26, in <module>
    gdf_lat = uxds['latVertex'].to_geodataframe()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xylar/code/uxarray/philipc2/mpas-updates/uxarray/core/dataarray.py", line 156, in to_geodataframe
    gdf[self.name] = self.data
    ~~~^^^^^^^^^^^
  File "/home/xylar/mambaforge/envs/uxarray_build/lib/python3.11/site-packages/pandas/core/frame.py", line 3950, in __setitem__
    self._set_item(key, value)
  File "/home/xylar/mambaforge/envs/uxarray_build/lib/python3.11/site-packages/pandas/core/frame.py", line 4143, in _set_item
    value = self._sanitize_column(value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xylar/mambaforge/envs/uxarray_build/lib/python3.11/site-packages/pandas/core/frame.py", line 4870, in _sanitize_column
    com.require_length_match(value, self.index)
  File "/home/xylar/mambaforge/envs/uxarray_build/lib/python3.11/site-packages/pandas/core/common.py", line 576, in require_length_match
    raise ValueError(
ValueError: Length of values (3947) does not match length of index (3179)

I didn't follow the details of the code well enough to debug this but my assumption is that the first value is without the MPAS vertices that were removed by masking but the second value is after masking.

@xylar

xylar commented Oct 11, 2023 •

Copy link
Copy Markdown
Collaborator

There is another, far thornier issue that we should discuss. For both visualization and analysis purposes, removing the incomplete triangles is not really what we want when we work with quantities on the MPAS dual mesh. Instead, we want to break each triangle into 3 "kites" (diamond shapes) that are the intersections between the triangle and each neighboring cell. If a neighboring cell doesn't exist (because it is land), we want to drop that kite but keep the remaining ones. For analysis purposes, the areas of the 1 to 3 kites are the correct area for a dual cell. For visualization purposes, the we want to plot the 1 or 2 kites even when the triangle is incomplete.

I don't know what the right way to handle this in uxarray might be but I wanted to put out there that the current implementation of the dual mesh is not quite what we would need for MPAS meshes that have had cells (e.g. land) culled from them.

@xylar

xylar commented Oct 11, 2023 •

Copy link
Copy Markdown
Collaborator

Continuing with my last comment, an approach to generating dual-mesh cells for MPAS meshes that would combine the 1 to 3 kites I mentioned above is:

  • start with 6 polygon vertices at the lat and lon of the MPAS vertex
  • for even-numbered polygon vertices, use the lat and lon of the edge for each valid edge on the MPAS vertex
  • for odd-numbered polygon vertices, use the lat and lon of the cell for each valid cell on the MPAS vertex

Here is a block of code I use to do this:
https://github.com/MPAS-Dev/pyremap/blob/76797c982b72f7ce3c33ddfa01d12f03b6975abe/pyremap/descriptor/mpas_vertex_mesh_descriptor.py#L129-L148

This approach relies on allowing degenerate polygon vertices. If that is not allowed, the degenerate vertices would have to be masked or removed.

If this discussion is way beyond the scope of this PR, I'm happy to move it to a discussion page. But since my approach would be an alternative that doesn't require masking, I thought I would bring it up here first.

@philipc2

Copy link
Copy Markdown
Member Author

scope

@philipc2, it's possible that I'm just misusing uxarray but the following doesn't work, and I would have expected it to:

#!/usr/bin/env python3
import uxarray as ux

mpas_dataset_filepath = "test/meshfiles/mpas/QU/oQU480.231010.nc"

uxds = ux.open_dataset(mpas_dataset_filepath, mpas_dataset_filepath,
                       use_dual=True)
gdf_grid = uxds.uxgrid.to_geodataframe()
gdf_lat = uxds['latVertex'].to_geodataframe()

I see:

Traceback (most recent call last):
  File "/home/xylar/code/uxarray/philipc2/mpas-updates/./test3.py", line 26, in <module>
    gdf_lat = uxds['latVertex'].to_geodataframe()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xylar/code/uxarray/philipc2/mpas-updates/uxarray/core/dataarray.py", line 156, in to_geodataframe
    gdf[self.name] = self.data
    ~~~^^^^^^^^^^^
  File "/home/xylar/mambaforge/envs/uxarray_build/lib/python3.11/site-packages/pandas/core/frame.py", line 3950, in __setitem__
    self._set_item(key, value)
  File "/home/xylar/mambaforge/envs/uxarray_build/lib/python3.11/site-packages/pandas/core/frame.py", line 4143, in _set_item
    value = self._sanitize_column(value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xylar/mambaforge/envs/uxarray_build/lib/python3.11/site-packages/pandas/core/frame.py", line 4870, in _sanitize_column
    com.require_length_match(value, self.index)
  File "/home/xylar/mambaforge/envs/uxarray_build/lib/python3.11/site-packages/pandas/core/common.py", line 576, in require_length_match
    raise ValueError(
ValueError: Length of values (3947) does not match length of index (3179)

I didn't follow the details of the code well enough to debug this but my assumption is that the first value is without the MPAS vertices that were removed by masking but the second value is after masking.

This is indeed due to the masking going on. I think I will move the fix for #359 and your points above into a separate PR. The masking solution that I implemented here seems does not seem to be the best approach, even if it is meant to just be a quick "patch"

@xylar

xylar commented Oct 11, 2023

Copy link
Copy Markdown
Collaborator

@philipc2 and all, by the way, I appreciated being asked to review this. It gave me an excuse to use uxarray more than I have so far, and it already seems clear that we should be adopting it in our new tool Polaris. I'm hoping to move a bunch of our plotting over to using uxarray and holoviews in the next couple of weeks.

@philipc2

Copy link
Copy Markdown
Member Author

@philipc2 and all, by the way, I appreciated being asked to review this. It gave me an excuse to use uxarray more than I have so far, and it already seems clear that we should be adopting it in our new tool Polaris. I'm hoping to move a bunch of our plotting over to using uxarray and holoviews in the next couple of weeks.

Of course! I appreciate the review, especially from someone that has a much better understand of MPAS than me :)

Speaking of visualization, we've been making a big push this iteration for visualuzation. If you're interested, we have a WIP notebook that showcases a few methods we are currently implemented on a few high-resolution MPAS datasets. https://github.com/NCAR/geocat-scratch/blob/ecmwf_conf/uxarray/ecmwf_conf/uxarray_ecmwf-conf.ipynb

@rajeeja

rajeeja commented Oct 11, 2023

Copy link
Copy Markdown
Contributor

@philipc2 and all, by the way, I appreciated being asked to review this. It gave me an excuse to use uxarray more than I have so far, and it already seems clear that we should be adopting it in our new tool Polaris. I'm hoping to move a bunch of our plotting over to using uxarray and holoviews in the next couple of weeks.

awesome!
Let us know if you need any help

@philipc2
philipc2 requested review from rajeeja and xylar October 12, 2023 17:27
@rajeeja

rajeeja commented Oct 16, 2023

Copy link
Copy Markdown
Contributor

@philipc2 what are the other methods to fix the mesh apart from masking?
It'd be good to put those down as a new issue.

Also, would the validate mesh PR+x be useful for handling such things?

@philipc2

Copy link
Copy Markdown
Member Author

such

@philipc2 what are the other methods to fix the mesh apart from masking? It'd be good to put those down as a new issue.

Also, would the validate mesh PR+x be useful for handling such things?

I'll make an issue for the other methods, especially the one method mentioned by @xylar

I can see validate being used, but not 100% sure exactly how right now.

@xylar

xylar commented Oct 17, 2023

Copy link
Copy Markdown
Collaborator

@philipc2, could you let me know when this branch is in a state that you'd like me to re-review?

@philipc2

Copy link
Copy Markdown
Member Author

@philipc2, could you let me know when this branch is in a state that you'd like me to re-review?

Sure thing! I'll give you a ping this morning after I give it a quick look through.

@philipc2

Copy link
Copy Markdown
Member Author

@xylar

It should be good for a re-review now!

@xylar

xylar commented Oct 17, 2023

Copy link
Copy Markdown
Collaborator

@philipc2, great! I don't think I'll have time to re-review today but I'll put on my list for tomorrow.

@philipc2

Copy link
Copy Markdown
Member Author

@philipc2, great! I don't think I'll have time to re-review today but I'll put on my list for tomorrow.

Thank you!!

@xylar

xylar commented Oct 18, 2023

Copy link
Copy Markdown
Collaborator

I didn't manage to get to this today. Hopefully tomorrow.

@philipc2

Copy link
Copy Markdown
Member Author

I didn't manage to get to this today. Hopefully tomorrow.

No worries!

@xylar xylar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! I was able to plot temperature on the primal grid as before. I was also able to plot both the primal and dual grids. But I was not able to plot a quantity on the dual grid using to_geodataframe(), as we have already noted (because of indices masked with invalid values).

I have a few mostly cosmetic suggestions but I think this is ready to go after that and I don't need to review it again.

Comment thread uxarray/grid/grid.py Resolved
Comment thread uxarray/grid/grid.py Resolved
Comment thread uxarray/grid/grid.py Resolved
Comment thread uxarray/io/_mpas.py Resolved
Comment thread uxarray/io/_mpas.py Resolved
@philipc2

Copy link
Copy Markdown
Member Author

This looks great! I was able to plot temperature on the primal grid as before. I was also able to plot both the primal and dual grids. But I was not able to plot a quantity on the dual grid using to_geodataframe(), as we have already noted (because of indices masked with invalid values).

I have a few mostly cosmetic suggestions but I think this is ready to go after that and I don't need to review it again.

Thank you so much for the review! I'm really glad to hear you were able to get those plots working :)

We'll work on getting better support for the dual mesh with those "invalid" values.

@philipc2
philipc2 merged commit e76a1f3 into main Oct 19, 2023
@erogluorhan
erogluorhan deleted the philipc2/mpas-updates branch December 7, 2023 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants