You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am frustrated with the interpolation in our transects, which I feel obscures both the complexity and the coarseness of the native grid and also gives a false sense of resolution.
I would at least like to support the option of transects on the native grid, meaning quadrilaterals that have corners at layer interfaces and edges, and a constant value corresponding to the field within the given cell. This would mean a truer representation of the ice draft and bathymetry to what is actually in the model as well.
To accomplish this, there would need to be an efficient way to find the nearest MPAS edge to a given point in a transect (possibly subdivided to make sure not to skip cells). A promising way to do this (used in COMPASS) is scipy's KDTree. A KDTree could be constructed with xEdge, yEdge and zEdge, a corresponding set of (x, y, z) vectors could constucted for points along the transect, and KDTree.query() could be used to find the edge index closes to each of these points. Then, redundant points could be removed and the cells connecting adjacent edges (if any) could be discovered.
It is fairly clear how this would be used to display model data. It is much less clear how to use this effectively to compare with WOCE or SOSE data, given that the data are sampled at different locations and at different spatial frequency. It may be appropriate to display each data set on its native grid and then use nearest neighbor, rather than linear, interpolation to a comparison grid for differencing. This will likely require some trial and error.
Well, we could do model-only transects on the native grid, and still interpolate when we do a direct comparison with obs. Really it's just the bias plot that would have to go, we could even consider plotting the WOCE transect on its own z,x data alongside the model transect.
@milenaveneziani , @xylar , after talking about this with Milena, I propose a workflow something like this:
So I see a workflow like this:
Define line segments between pairs of points in lat/lon space that define the transect
Convert each pair of points to x,y,z on the sphere.
For each line segment, create an array of points at high resolution between the two end points along a great circle between the two points. (Define the equation of the great circle that intersects those two points in x,y,z space, and then create an array with spacing of D meters along that arc, where D is smaller than the smallest cell spacing in the entire mesh.)
Create a kd tree for the entire MPAS mesh using x,y,zCell (or edge or vertex as appropriate) (If creating the kd tree for the entire MPAS mesh takes too long, a smaller one could be created using the subset of the points in the mesh that cover a box around the given line segment)
Query the kd tree to get the nearest neighbor on the MPAS grid to each of the points in the array created in step 3.
Remove duplicates from the resulting list of points
The cleaned list can now be used to build the transect.
@xylar , maybe you've already thought about this in more detail and have other ideas.
@xylar, to give you a bit of contest: I was thinking of trying out a 'simple' script, outside of MPAS-Analysis, just to get a feel of how this would work. My vision is to use this mostly for model visualization purposes, not for model-data comparisons (we have already a task for that). We could plot the obs (or climatology or other reanalysis data) alongside it, but just for visual comparison, not quantitative.
@xylar I don't have a clear sense of what advantage kd-tree offers in this context over the field "cellsOnCell"
@milenaveneziani, I'd propose a modified version of @matthewhoffman's workflow:
(1)-(3) as above
4. Choose starting cell (index k) by minimizing distance to one end of the transect line.
5. Loop over cellsOnCell[k], choose the cell index that minimizes the distance to the "ideal" transect line (excluding previously chosen cells)
6. Stop when the other end of the transect line is reached (a cell is found within a certain distance of the end point? Not sure yet how to make this step robust)
I've been working on this for awhile (off and on) and my approach is mostly similar to @matthewhoffman's suggestion. It is important to use edges rather than cell centers because there needs to be a way to not only find the nearest cell but also to determine if you are outside of the nearest cell. I believe that's where I ran into some difficulty but I'll have to refresh my memory.
@cbegeman's approach runs into trouble when the transect intersects land, which is frequently the case for us unless transects are chosen very carefully (especially because land is different at different resolutions). It turns out to be exceedingly difficult in my experience to use cellsOnCell to try to traverse around land, which is one of the major issues with the transect code Doug Jacobsen wrote.
A potential issue I see is selecting too many cells near the transect (i.e., selected edges snake too much). It seems like we would need an additional cleaning step besides @matthewhoffman's (6) to make sure each cell shares at most 2 edges with other cells or some similar criterion.
Yes, exactly! I vaguely recall that ended up really having to determine if a given edge intersects the transect, not just if its center is closest to the transect. That's obviously a bit trickier.
I'll try to get back to the work I was doing later this week and see where I left things. Assuming the path forward isn't obvious, I'll ask for advice.
Thanks @cbegeman and @xylar for the discussion. Xylar: I should have known you are thousand of miles ahead of me :) Let me know when you get to a point when it would make sense for me to try/test something or help with anything else.
compute Cartesian bounding boxes for each edge in the mesh
break transects into 10 km segments (so curvature is more or less negligible)
find all edge bounding boxes that intersect the bounding box of each transect segment (expanded with a buffer by the maximum cell size, just to be safe)
Find which edges actually intersect the transect and where
Find cells between edges, or if there is no shared cell this must be land
Interpolate the vertical coordinate for layer interfaces from cells to edges
Here is an image showing the trial transects I'm playing with on the EC60to30wLI grid (the old 100 vertical layer version):
In orange, blue, green and red are the "candidate" edges for intersections. If you zoom in a lot, magenta points are the intersections between edges and transects, black lines are the edges, small green points are the cell centers of cells between these edges where properties will be taken from. Note the zig-zag but that's likely realistic...
Here are some example temperature transects.
Let me know what you think, keeping in mind this is still very much in prototype mode.
This is because the normal velocity points in different directions at different edges. That is, it's not normal to the smooth transect, it's normal to the zig-zag of edges. That's fine for computing total transport but not great for plotting velocity transects. You would probably want use the reconstructed zonal and meridional velocities at cell centers and compute the normal to the "real" transect.
An alternative to consider: If you want to have a velocity transect that more closely resembles the computed flux normal to the transect, I've been using normalVelocity and angleEdge as well as the angle the transect makes with angleEdge, which removes these stripes.
So effectively you're taking the velocity component normal to an edge, multiplying it by the normal vector, and then dotting it into a vector normal to the transect, is that the correct idea? I don't think that is equivalent to the velocity normal to the transect so I would be careful about using that.
@xylar You're right. I do use the approach you originally suggested for plotting and that seems like the right approach for Milena. Sorry for the diversion!
I think perhaps, one way could be if we had, for each edge that makes up the MPAS-grid transect, an associated 'real' transect piece, which is part of a binned spherical distance between the two end points that define the transect, basically this bullet in the methodology outlined by @xylar a few months ago:
break transects into 10 km segments (so curvature is more or less negligible)
Then, we would simply rotate the normalVelocity with respect to that.
Of course the script I have now doesn't do the initial transect binning, so I have no way of knowing about the piecewise 'real' transect.
I think I will leave the plot as is now, at least it shows the actual model velocity across each edge. Maybe I'll consider some simple filtering, just for pleasing the eye, but I'll see.
One last thing that would be nice to add is a few potential density contours overlapping these fields.
I'll make these few changes later today, and call it done for the moment.
Then, we would simply rotate the normalVelocity with respect to that.
The trouble is that we simply don't have both components of the velocity at edges. MPAS has a way of constructing the tangential component of velocity at edges, but it is only for the very specific purpose of computing the Coriolis force and I have it on good authority from @toddringler that this velocity should not be treated as appropriate for reconstructing a velocity vector at an edge. Instead, we use radial basis functions to reconstruct the velocity components (either 3D Cartesian or zonal/meridional) at cell centers and those are the only full vector velocities available from MPAS. It would be nice to have something else based more directly on normalVelocity but I do not believe that any such thing is available.
at least it shows the actual model velocity across each edge.
So I think that's where my approach and yours have slightly different purposes. I was never intending to follow model edges. Instead, I wanted to show model properties as if you were slicing through cells where scalar properties are constant within the polygonal prism of each cell. This could be done with velocity (dotted into the local normal to the transect) as well.
I want to think about how to do smoother plotting if that's what we desire. I am not a fan of contourf, so I don't necessarily intend to take that route but I will try to make sure whatever approach I take could mimic that result even if it's produced with pcolormesh. I'm thinking about trying an approach similar to what I did in the horizontal here: MPAS-Dev/MPAS-Tools#281 (comment). The principle is essentially the same.
I started writing this comment below, but then I saw yours and am not sure it's relevant any longer, but since I'm done writing it, here it goes :D:
We haven't talked about the transect mask part, but I guess it will depend on whether we'll want to adopt this methodology to identify the transect or not.
If that's what we want to do, maybe it would be nice to add some transects to the StandardTransect group, especially for the SO: maybe a few corresponding with the ones we use for SOSE and WOCE transects, plus a few more that would be more helpful for cryo papers, such as at the entrance of Ronne-Filchner, Ross, etc.
Other options are:
compute the transect mask on the fly
do not rely on the transect mask at all, which is what I assume Xylar's script does.
I am personally fine either way, I chose to go this route only because it was simpler for me :)
Do we have general script(s) to accept start and end coordinates (or similar) and return both:
(1) cell indices - I think this is what @xylar's transect script does, and seems most convenient for plotting transect properties for the reasons he mentioned
(2) edge indices suitable for computing transect-normal fluxes - I think this is what @milenaveneziani's script does
@cbegeman, the MpasMaskCreator.x (mpas_tools.conversion.mask in python) does this but it produces a less-than-straight transect so I was not happy with it. This is what @milenaveneziani is using as a starting point.
After working on this off and on for several weeks, I'm almost there, see: MPAS-Dev/MPAS-Tools#324. I believe I should have it all wrapped up in a few more days.
Activity
milenaveneziani commentedon May 21, 2019
Well, we could do model-only transects on the native grid, and still interpolate when we do a direct comparison with obs. Really it's just the bias plot that would have to go, we could even consider plotting the WOCE transect on its own z,x data alongside the model transect.
matthewhoffman commentedon Nov 26, 2019
@milenaveneziani , @xylar , after talking about this with Milena, I propose a workflow something like this:
So I see a workflow like this:
@xylar , maybe you've already thought about this in more detail and have other ideas.
milenaveneziani commentedon Nov 26, 2019
Thanks @matthewhoffman, that all seems reasonable to me.
@xylar, to give you a bit of contest: I was thinking of trying out a 'simple' script, outside of MPAS-Analysis, just to get a feel of how this would work. My vision is to use this mostly for model visualization purposes, not for model-data comparisons (we have already a task for that). We could plot the obs (or climatology or other reanalysis data) alongside it, but just for visual comparison, not quantitative.
milenaveneziani commentedon Nov 26, 2019
The most important sticking point is how efficient is kd-tree (I must admit I don't really understand how this k-dimensional tree is constructed).
cbegeman commentedon Nov 26, 2019
@xylar I don't have a clear sense of what advantage kd-tree offers in this context over the field "cellsOnCell"
@milenaveneziani, I'd propose a modified version of @matthewhoffman's workflow:
(1)-(3) as above
4. Choose starting cell (index
k) by minimizing distance to one end of the transect line.5. Loop over
cellsOnCell[k], choose the cell index that minimizes the distance to the "ideal" transect line (excluding previously chosen cells)6. Stop when the other end of the transect line is reached (a cell is found within a certain distance of the end point? Not sure yet how to make this step robust)
xylar commentedon Nov 27, 2019
Thanks for these thoughts everyone!
I've been working on this for awhile (off and on) and my approach is mostly similar to @matthewhoffman's suggestion. It is important to use edges rather than cell centers because there needs to be a way to not only find the nearest cell but also to determine if you are outside of the nearest cell. I believe that's where I ran into some difficulty but I'll have to refresh my memory.
@cbegeman's approach runs into trouble when the transect intersects land, which is frequently the case for us unless transects are chosen very carefully (especially because land is different at different resolutions). It turns out to be exceedingly difficult in my experience to use
cellsOnCellto try to traverse around land, which is one of the major issues with the transect code Doug Jacobsen wrote.cbegeman commentedon Nov 27, 2019
@xylar, I see w.r.t. land difficulties.
A potential issue I see is selecting too many cells near the transect (i.e., selected edges snake too much). It seems like we would need an additional cleaning step besides @matthewhoffman's (6) to make sure each cell shares at most 2 edges with other cells or some similar criterion.
xylar commentedon Nov 27, 2019
Yes, exactly! I vaguely recall that ended up really having to determine if a given edge intersects the transect, not just if its center is closest to the transect. That's obviously a bit trickier.
I'll try to get back to the work I was doing later this week and see where I left things. Assuming the path forward isn't obvious, I'll ask for advice.
milenaveneziani commentedon Dec 2, 2019
Thanks @cbegeman and @xylar for the discussion. Xylar: I should have known you are thousand of miles ahead of me :) Let me know when you get to a point when it would make sense for me to try/test something or help with anything else.
xylar commentedon Dec 8, 2019
@cbegeman, @matthewhoffman and @milenaveneziani, here is my progress so far.
Steps:
Here is an image showing the trial transects I'm playing with on the EC60to30wLI grid (the old 100 vertical layer version):

In orange, blue, green and red are the "candidate" edges for intersections. If you zoom in a lot, magenta points are the intersections between edges and transects, black lines are the edges, small green points are the cell centers of cells between these edges where properties will be taken from. Note the zig-zag but that's likely realistic...
Here are some example temperature transects.
Let me know what you think, keeping in mind this is still very much in prototype mode.
20 remaining items
milenaveneziani commentedon Jun 19, 2020
indeed, I have already done the 'borrowing' in the other scripts that I have to do regional time series and T,S,density profiles :)
cbegeman commentedon Jun 19, 2020
An alternative to consider: If you want to have a velocity transect that more closely resembles the computed flux normal to the transect, I've been using
normalVelocityandangleEdgeas well as the angle the transect makes withangleEdge, which removes these stripes.xylar commentedon Jun 19, 2020
@cbegeman, how does that work when you don't have the tangential component of the velocity at the edge?
cbegeman commentedon Jun 19, 2020
It doesn't, it only works for the normal component.
xylar commentedon Jun 19, 2020
So effectively you're taking the velocity component normal to an edge, multiplying it by the normal vector, and then dotting it into a vector normal to the transect, is that the correct idea? I don't think that is equivalent to the velocity normal to the transect so I would be careful about using that.
cbegeman commentedon Jun 19, 2020
@xylar You're right. I do use the approach you originally suggested for plotting and that seems like the right approach for Milena. Sorry for the diversion!
milenaveneziani commentedon Jun 19, 2020
I think perhaps, one way could be if we had, for each edge that makes up the MPAS-grid transect, an associated 'real' transect piece, which is part of a binned spherical distance between the two end points that define the transect, basically this bullet in the methodology outlined by @xylar a few months ago:
Then, we would simply rotate the normalVelocity with respect to that.
Of course the script I have now doesn't do the initial transect binning, so I have no way of knowing about the piecewise 'real' transect.
I think I will leave the plot as is now, at least it shows the actual model velocity across each edge. Maybe I'll consider some simple filtering, just for pleasing the eye, but I'll see.
One last thing that would be nice to add is a few potential density contours overlapping these fields.
I'll make these few changes later today, and call it done for the moment.
xylar commentedon Jun 19, 2020
The trouble is that we simply don't have both components of the velocity at edges. MPAS has a way of constructing the tangential component of velocity at edges, but it is only for the very specific purpose of computing the Coriolis force and I have it on good authority from @toddringler that this velocity should not be treated as appropriate for reconstructing a velocity vector at an edge. Instead, we use radial basis functions to reconstruct the velocity components (either 3D Cartesian or zonal/meridional) at cell centers and those are the only full vector velocities available from MPAS. It would be nice to have something else based more directly on normalVelocity but I do not believe that any such thing is available.
So I think that's where my approach and yours have slightly different purposes. I was never intending to follow model edges. Instead, I wanted to show model properties as if you were slicing through cells where scalar properties are constant within the polygonal prism of each cell. This could be done with velocity (dotted into the local normal to the transect) as well.
I want to think about how to do smoother plotting if that's what we desire. I am not a fan of contourf, so I don't necessarily intend to take that route but I will try to make sure whatever approach I take could mimic that result even if it's produced with pcolormesh. I'm thinking about trying an approach similar to what I did in the horizontal here: MPAS-Dev/MPAS-Tools#281 (comment). The principle is essentially the same.
milenaveneziani commentedon Jun 19, 2020
I started writing this comment below, but then I saw yours and am not sure it's relevant any longer, but since I'm done writing it, here it goes :D:
We haven't talked about the transect mask part, but I guess it will depend on whether we'll want to adopt this methodology to identify the transect or not.
If that's what we want to do, maybe it would be nice to add some transects to the
StandardTransectgroup, especially for the SO: maybe a few corresponding with the ones we use for SOSE and WOCE transects, plus a few more that would be more helpful for cryo papers, such as at the entrance of Ronne-Filchner, Ross, etc.Other options are:
I am personally fine either way, I chose to go this route only because it was simpler for me :)
cbegeman commentedon Jun 19, 2020
Do we have general script(s) to accept start and end coordinates (or similar) and return both:
(1) cell indices - I think this is what @xylar's transect script does, and seems most convenient for plotting transect properties for the reasons he mentioned
(2) edge indices suitable for computing transect-normal fluxes - I think this is what @milenaveneziani's script does
xylar commentedon Jun 19, 2020
@cbegeman, the
MpasMaskCreator.x(mpas_tools.conversion.maskin python) does this but it produces a less-than-straight transect so I was not happy with it. This is what @milenaveneziani is using as a starting point.xylar commentedon Jul 20, 2020
After working on this off and on for several weeks, I'm almost there, see: MPAS-Dev/MPAS-Tools#324. I believe I should have it all wrapped up in a few more days.
xylar commentedon Aug 3, 2020
2 weeks later, I have this almost wrapped up...