holoviz-topics / EarthSim

Tools for working with and visualizing environmental simulations.
https://earthsim.holoviz.org
BSD 3-Clause "New" or "Revised" License
65 stars 21 forks source link

Reading UGRID-compliant NetCDF files #326

Open rsignell-usgs opened 3 years ago

rsignell-usgs commented 3 years ago

It would be great if the earthsim.io module could read UGRID-compliant NetCDF files. UGRID adds onto the CF-Conventions to provide an unambiguous way of reading topology and variable locations on the mesh from an unstructured mesh NetCDF file.

An example triangular mesh dataset is here:

import xarray as xr
url = 'http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_GOM3_FORECAST.nc'
ds = xr.open_dataset(url,drop_variables='siglay')
jbednar commented 3 years ago

To make sure it's clear, EarthSim shouldn't include any code for reading files or pretty much anything else; it's almost entirely a collection of examples. It originally did have lots of code, but nearly all of that code has migrated into generic OSS libraries as planned, with the remainder expected to migrate out whenever we get a chance to do so. We try our hardest not to write anything deeply domain specific except for examples!

Here, we'd be happy for EarthSim to include examples of reading UGRID NetCDF files via xarray, but are you asking for something beyond an example? E.g. extracting more of the metadata and making use of it better? If so I'd expect such changes to go into xarray or maybe GeoViews, not EarthSim.

rsignell-usgs commented 3 years ago

@jbednar, I was looking at the visualizing meshes notebook and saw that it contains:

from earthsim.io import read_3dm_mesh, read_mesh2d
jbednar commented 3 years ago

D'oh! Any suggestions on where that code and any UGRID-supporting reading code should go in the long term?

rsignell-usgs commented 3 years ago

Maybe in https://github.com/xarray-contrib, where we already have https://github.com/xarray-contrib/cf-xarray?
What thinkest thou, @dcherian?

dcherian commented 3 years ago

tris, verts = read_3dm_mesh(fpath)

IIUC, you want to replace this line with a function that would parse the UGRID metadata and generate tris, verts? If so, we could add that to cf-xarray. It would mirror the helper functions for converting edges to vertices that were merged recently (https://github.com/xarray-contrib/cf-xarray/pull/108)

rsignell-usgs commented 3 years ago

@ocefpaf, we already have something simple that will return tris, verts from a CF-compliant UGRID, don't we? The goal would be to simplify (and standardize) notebooks like: https://gallery.pangeo.io/repos/rsignell-usgs/esip-gallery/01_hurricane_ike_water_levels.html

ocefpaf commented 3 years ago

@dcherian I agree that this would be an awesome addition to cf-xarray. This is what I wrote some time ago, it is virtually the UGRID spec as code. I think you can adapt that to cf-xarray without many problems, either using the same variable search "get_var_by" or a more modern approach. (I do not recommend depending on that code though! It was not meant to be a library.)

jbednar commented 3 years ago

Sounds great! Just to be clear, by "not depending on that code", do you just mean not importing from its current location in pyoceans/gridgeo/blob/master/gridgeo/ugrid.py? It sounds like you don't think it's in bad shape otherwise, and that once it's been moved to cf-xarray and cleaned up slightly it would be fine to depend on?

ocefpaf commented 3 years ago

Sounds great! Just to be clear, by "not depending on that code", do you just mean not importing from its current location in pyoceans/gridgeo/blob/master/gridgeo/ugrid.py? It sounds like you don't think it's in bad shape otherwise, and that once it's been moved to cf-xarray and cleaned up slightly it would be fine to depend on?

Yep. Don't import that code from that library. The UGRID implementation/logic in there is solid.

jbednar commented 3 years ago

Sounds great! I'd love for there to be an example at EarthSim using such an implementation, imported from somewhere appropriate. Rich, if the code does get published properly in that way, then I'd be happy to help adapt one of our current examples to use it or to take a new example and add it to EarthSim.

Do you think this UGRID support should replace the 3dm stuff, or should I keep looking for a place to put the earthsim.io readers in the long term?

ocefpaf commented 3 years ago

Do you think this UGRID support should replace the 3dm stuff, or should I keep looking for a place to put the earthsim.io readers in the long term?

Not really replace but the 3DM output should ideally be UGRID compliant so standard tools can easily read it. That is the AdH model output, right? I recall helping some folks reading that as UGRID some years ago. Not sure if that effort evolved into code somewhere.


Edit: found it! See cell [5] in https://nbviewer.jupyter.org/gist/ocefpaf/933da65c3e3faa0a2ff9c0838f60a3b0

dcherian commented 3 years ago

Also see https://github.com/pydata/xarray/issues/4222