question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unstructured grids

See original GitHub issue

I tried regionmask on unstructured grids and it works with a tweak: https://gist.github.com/aaronspring/1b45cf22f80360ddc4ef13af1cadec63

Unstructured grids like ICON just have one spatial dimension like ncells and 2 spatial coordinates clon and clat.

To avoid:

/work/mh0727/m300524/conda-envs/pymistral/lib/python3.8/site-packages/regionmask/core/mask.py in _get_LON_LAT_out_shape(lon, lat, fill)
    467 
    468     if ndim == 1:
--> 469         LON, LAT = np.meshgrid(lon, lat)
    470     elif ndim == 2:
    471         LON, LAT = lon, lat

and use L470 instead, I add a fake dimension dummy:

ds = ds.assign_coords(clon=ds.coords['clon'].expand_dims('dummy'))
ds = ds.assign_coords(clat=ds.coords['clat'].expand_dims('dummy'))

To identify unstructured grids, we could maybe use the metadata CDI_grid_type from the xr.DataArrays variables:

ds[v].attrs
{'standard_name': 'sea_water_potential_temperature',
 'long_name': 'sea water potential temperature',
 'units': 'deg C',
 'code': 2,
 'CDI_grid_type': 'unstructured',
 'number_of_grid_in_reference': 1}

or we just check for the number of dimensions used in lon and lat and add such a fake dimension.

Do you think regionmask could handle such unstructured grids without such tweaks? A solution should also work for other unstructured output from MPAS @mgrover1, FESOM @koldunovn, etc

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mgrover1commented, Sep 14, 2021

Here are a couple use cases. Both of these can be accessed using the geocat-datafiles repository Here is an MPAS file:

<xarray.Dataset>
Dimensions:           (nCells: 163842, Time: 1)
Dimensions without coordinates: nCells, Time
Data variables:
    lonCell           (nCells) float64 ...
    latCell           (nCells) float64 ...
    surface_pressure  (Time, nCells) float64 ...

And a CAM-SE (unstructured atmospheric climate model output) case

<xarray.Dataset>
Dimensions:  (ncol: 48602)
Dimensions without coordinates: ncol
Data variables:
    T850     (ncol) float32 283.6 283.1 282.5 282.2 ... 265.8 266.3 267.0 267.3
    lon      (ncol) float64 315.0 315.8 317.2 318.0 ... 135.0 133.7 136.3 135.0
    lat      (ncol) float64 -35.26 -35.65 -36.26 -36.62 ... 36.68 36.68 36.05
Attributes: (12/16)
    np:                        4
    ne:                        30
    Conventions:               CF-1.0
    source:                    CAM
    case:                      b1850cam5cn_camse
    title:                     UNSET
    ...                        ...
    initial_file:              /glade/scratch/juliob/cami_minimal_ne30np4_L30.nc
    topography_file:           /fis/cgd/cseg/csm/inputdata/atm/cam/topo/USGS-...
    history:                   Thu Jul 21 11:14:15 2011: ncatted -O -a yrs_av...
    nco_openmp_thread_number:  1
    yrs_averaged:              3-10
    description:               This file is a subset of a CAM-SE file. It was...

You can access these files for testing using the following:

import geocat.datafiles
import xarray as xr

# MPAS data
mpas_file = 'netcdf_files/MPAS.nc'
mpas_ds = xr.open_dataset(geocat.datafiles.get(mpas_file))

# CAM-SE data
camse_file = 'netcdf_files/camse_unstructured_grid.nc'
camse_ds = xr.open_dataset(geocat.datafiles.get(camse_file))

@aaronspring let me know if you have any questions

1reaction
mathausecommented, Sep 10, 2021

only do meshgrid when data are 2d and the coordinates are 1d?

I don’t look at the data only the coordinates. @aaronspring can you share a repr of the dataset? I could only call meshgrid if the dimension name is not equal to the coords name. I.e. call meshgrid:

<xarray.Dataset>
Dimensions:  (lat: 25, time: 2920, lon: 53)
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  * lon      (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
...

don’t call meshgrid:

<xarray.Dataset>
Dimensions:  (idx: 25, time: 2920)
Coordinates:
  * lat      (idx) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  * lon      (idx) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
...

That might work.

At the end of the day unstructured meshes are not that common, so maybe just an additional argument to the function would be enough?

Yes that is probably necessary (although I prefer to not add yet another argument). Unless we have enough trust in the above solution.


b.t.w. - the __version__ was wrong when installing with conda (see the gist) - that should be fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unstructured Grid - an overview | ScienceDirect Topics
The use of unstructured grid offers several advantages over structured grids. These lie in the ability to deal with arbitrary complex geometries and...
Read more >
UNSTRUCTURED GRID TECHNIQUES
An overview of the current state of the art in unstructured mesh techniques for computational fluid dynamics is given. The topics of mesh...
Read more >
Grid Systems | CFD-101 | What Are the Advantages and ...
A popular type of unstructured grid consists of tetrahedral elements. These grids tend to be easier to generate than those composed of hexahedral...
Read more >
Chapter 11: Unstructured Grids Introduction
Structured curvilinear grids are based on mapping of the flow domain onto a computational domain. It is very difficult to find viable mappings...
Read more >
Smoothing of Unstructured Grids
Abstract. Two mesh smoothing techniques applied to unstructured grids are compared. These are based on the solution of elliptic equations,.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found