Scheduled Downtime
On Friday 21 April 2023 @ 5pm MT, this website will be down for maintenance and expected to return online the morning of 24 April 2023 at the latest

how to find nearest cells in MPAS and discontinuous cells

This post was from a previous version of the WRF&MPAS-A Support Forum. New replies have been disabled and if you have follow up questions related to this post, then please start a new thread from the forum home page.

tianfeng

New member
Hi,

I am trying to do an area-averaged analysis of MPAS model data. Is there an easy way to obtain a list of cells within certain distance (degrees of latitude/longitude) from a fixed location? What I was trying was to use the variable cellsOnCell and the lat/lon(Cell). However I found that the latitudes and longitudes of cells in the neighborhood of some cell (from cellsOnCell) are discontinuous.

The following are the neighbor cells of two cells in the 60-3km resolution case. Could anyone please help? Thank you!

734701
(0) final current Cell = 734357 lat=4.07116457315735 lon=3.739056711449173
(0) final current Cell = 734073 lat=-4.933592412367122 lon=357.5038769989663
(0) final current Cell = 45805 lat=-5.549895945716914 lon=0.03451687179975495
(0) final current Cell = 734703 lat=-4.158707227972656 lon=3.61774261933559
(0) final current Cell = 736024 lat=5.400574252372644 lon=0.8741581850622008
(0) final current Cell = 183874 lat=-4.323387234497731 lon=356.6472629097582
(0) final current Cell = 0 lat=5.419508248323731 lon=358.9535832787361

318863
(0) final current Cell = 318475 lat=-30.47238510772734 lon=333.7801957403656
(0) final current Cell = 318476 lat=-3.682307041104671 lon=320.5147341002601
(0) final current Cell = 79696 lat=-30.65572769938358 lon=335.4436298722315
(0) final current Cell = 319215 lat=-29.92440110474912 lon=334.8256002795744
(0) final current Cell = 318980 lat=32.91497599367084 lon=337.6165941481133
(0) final current Cell = 79546 lat=-38.60318379728767 lon=12.18674900660047
(0) final current Cell = 0 lat=5.419508248323731 lon=358.9535832787361

regards
Feng
 
Great question. There are a few different ways to do this.

Before I mention them, I'll address your question or concern on the discontinuous of the cellsOnCell field. As I'm sure you know, MPAS works on a unstructured grid. So any neighbors to a cell wont be arrange in any particular order, so if you print out the cellsOnCell field for a particular cell, we shouldn't see any order. However, your latitude and longitude values are a little head scratching. Have you converted them from radians to degrees?

To demonstrate I have created a small python script that can read in a MPAS grid and a cell id, and produce the that cells neighbors as an example of how to correctly access the cellsOnCell field. Because each grid cell can have a different number of edges, and therefore neighbors, you'll need to first find the number of edges for the cell in question using nEdgesOnCell. Then using that cells number of edges loop through cellsOnCell to find its neighbors. If you don't you'll run into neighbors that don't exist for that cell (i.e. garbage cells).

The first method for finding the cells within a specified region, which is probably the easiest, but also the slowest and most inefficient, is to loop through all of nCells and calculate the distance between your point and the cell center and determine if a cell is within your desired distance or not. To calculate the distance, you can just calculate the sphere distance. In MPAS, we have a Fortran function in the init_atmosphere model that we use to calculate sphere distance, which you can find here .

We also have a python version of the same function, which you can find in the MPAS-Limited-Area program.

The sphere radius that of any MPAS grid can be found in the sphere_radius NetCDF attribute.

The second method, and the method that I believe you're headed to now, would be to recursively loop through cellsOnCell from a cell that contains your desired location. After determining your starting cell, you can discover that cells neighbors, calculate the distance and determine if they are within your desired distance or not and then repeat for each neighbor that is within that distance.

Again, you can use the sphere distance functions as mentioned above to calculate distances, but to find the nearest cell to your specified point you'll need to use the nearest_cell function.
 
As an illustration of the method of finding all cells within a specified radius of a starting point, you might have a look at slide 15 of this presentation from the most recent MPAS-Atmosphere tutorial. We can probably provide an example implementation of this method if it would help.
 
Top