Skip to content

DRAFT: Spatial min, max, & mean Operators for UxDataArray - #500

Closed
philipc2 wants to merge 4 commits into
mainfrom
philipc2/spatial-operators
Closed

philipc2 wants to merge 4 commits into
mainfrom
philipc2/spatial-operators

Conversation

@philipc2

@philipc2 philipc2 commented Oct 5, 2023

Copy link
Copy Markdown
Member

Closes #XXX

Overview

Expected Usage

import uxarray as ux

grid_path = "/path/to/grid.nc"
data_path = "/path/to/data.nc"

uxds = ux.open_dataset(grid_path, data_path)

# this is how you use this function
some_output = uxds.some_function()

# this is another way to use this function
other_output = uxds.some_function(some_param = True)

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 changed the title Spatial min, max, & mean operators for `UxDataArray DRAFT: Spatial min, max, & mean Operators for `UxDataArray Oct 5, 2023
@philipc2

philipc2 commented Oct 5, 2023

Copy link
Copy Markdown
Member Author

@ahijevyc @hongyuchen1030 @paullric @rljacob

I am working on implementing spatial min, max, & mean methods that compute their respective operation based on what values lie within some radius (in degrees) from a given (lon, lat) point. Below is a very (rough) sketch of how we select the spatial points for these operators.

image

This aims to implement what was requested in #46 (comment) , however internally we perform all of our distance computations using degrees, however we would ideally want to use a distance in meters.

Do you have any suggestions how we can convert between degrees and meters in the context of our internal representation (i.e. our grid resides on a unit sphere)

@philipc2 philipc2 changed the title DRAFT: Spatial min, max, & mean Operators for `UxDataArray DRAFT: Spatial min, max, & mean Operators for UxDataArray Oct 5, 2023
@hongyuchen1030

Copy link
Copy Markdown
Contributor

@ahijevyc @hongyuchen1030 @paullric @rljacob

I am working on implementing spatial min, max, & mean methods that compute their respective operation based on what values lie within some radius (in degrees) from a given (lon, lat) point. Below is a very (rough) sketch of how we select the spatial points for these operators.

image

This aims to implement what was requested in #46 (comment) , however internally we perform all of our distance computations using degrees, however we would ideally want to use a distance in meters.

Do you have any suggestions how we can convert between degrees and meters in the context of our internal representation (i.e. our grid resides on a unit sphere)

If you mean having a copy of the cartesian coordinates representation of the input point, just call my populate_xyz_ccordinates function for the input.

If you mean given any point in degree, convert it to the cartesian coordinates, we have node_lonlat_to_xyz() function

I am more curious about how you can present the distance(radius) in degrees here?

@philipc2

philipc2 commented Oct 5, 2023

Copy link
Copy Markdown
Member Author

node_lonlat_to_xyz

@ahijevyc @hongyuchen1030 @paullric @rljacob
I am working on implementing spatial min, max, & mean methods that compute their respective operation based on what values lie within some radius (in degrees) from a given (lon, lat) point. Below is a very (rough) sketch of how we select the spatial points for these operators.
image
This aims to implement what was requested in #46 (comment) , however internally we perform all of our distance computations using degrees, however we would ideally want to use a distance in meters.
Do you have any suggestions how we can convert between degrees and meters in the context of our internal representation (i.e. our grid resides on a unit sphere)

If you mean having a copy of the cartesian coordinates representation of the input point, just call my populate_xyz_ccordinates function for the input.

If you mean given any point in degree, convert it to the cartesian coordinates, we have node_lonlat_to_xyz() function

I am more curious about how you can present the distance(radius) in degrees here?

In our BallTree implementation, which we are constructing using either the corner modes or face centers, the distance is computed using the Haversine distance metric in Sklearn (https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html)

Specifically, if we indicate a radius of "90", searches for all points within a circle with radius 90 degrees

@hongyuchen1030

Copy link
Copy Markdown
Contributor

node_lonlat_to_xyz

@ahijevyc @hongyuchen1030 @paullric @rljacob
I am working on implementing spatial min, max, & mean methods that compute their respective operation based on what values lie within some radius (in degrees) from a given (lon, lat) point. Below is a very (rough) sketch of how we select the spatial points for these operators.
image
This aims to implement what was requested in #46 (comment) , however internally we perform all of our distance computations using degrees, however we would ideally want to use a distance in meters.
Do you have any suggestions how we can convert between degrees and meters in the context of our internal representation (i.e. our grid resides on a unit sphere)

If you mean having a copy of the cartesian coordinates representation of the input point, just call my populate_xyz_ccordinates function for the input.
If you mean given any point in degree, convert it to the cartesian coordinates, we have node_lonlat_to_xyz() function
I am more curious about how you can present the distance(radius) in degrees here?

In our BallTree implementation, which we are constructing using either the corner modes or face centers, the distance is computed using the Haversine distance metric in Sklearn (https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html)

Specifically, if we indicate a radius of "90", searches for all points within a circle with radius 90 degrees

I wrote a function called angle_of_two_vector on my branch, which can return the angle between two vectors on the unit spheres in cartesian coordinates if that's what you're looking for. I can also create a pull request for that.

@philipc2

philipc2 commented Oct 5, 2023

Copy link
Copy Markdown
Member Author

node_lonlat_to_xyz

@ahijevyc @hongyuchen1030 @paullric @rljacob
I am working on implementing spatial min, max, & mean methods that compute their respective operation based on what values lie within some radius (in degrees) from a given (lon, lat) point. Below is a very (rough) sketch of how we select the spatial points for these operators.
image
This aims to implement what was requested in #46 (comment) , however internally we perform all of our distance computations using degrees, however we would ideally want to use a distance in meters.
Do you have any suggestions how we can convert between degrees and meters in the context of our internal representation (i.e. our grid resides on a unit sphere)

If you mean having a copy of the cartesian coordinates representation of the input point, just call my populate_xyz_ccordinates function for the input.
If you mean given any point in degree, convert it to the cartesian coordinates, we have node_lonlat_to_xyz() function
I am more curious about how you can present the distance(radius) in degrees here?

In our BallTree implementation, which we are constructing using either the corner modes or face centers, the distance is computed using the Haversine distance metric in Sklearn (https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html)
Specifically, if we indicate a radius of "90", searches for all points within a circle with radius 90 degrees

I wrote a function called angle_of_two_vector on my branch, which can return the angle between two vectors on the unit spheres in cartesian coordinates if that's what you're looking for. I can also create a pull request for that.

That will deffinitly be useful eventually, however what I'm interest is whether there's any convention or conversion that needs to be done to be able to support a "meter" or "kilometer" radius as opposed to using "degrees"

For example, instead of doing "Find the minimum of all values within 25deg of some (lon, lat)", we would want "Find the minimum of all the values within 10km of some (lon, lat)". Since we currently represent all of our data on the unit sphere, is there any additional conversion that we need to do to go from "degrees" to "meters"

@paullric

paullric commented Oct 5, 2023

Copy link
Copy Markdown
Contributor

node_lonlat_to_xyz

@ahijevyc @hongyuchen1030 @paullric @rljacob
I am working on implementing spatial min, max, & mean methods that compute their respective operation based on what values lie within some radius (in degrees) from a given (lon, lat) point. Below is a very (rough) sketch of how we select the spatial points for these operators.
image
This aims to implement what was requested in #46 (comment) , however internally we perform all of our distance computations using degrees, however we would ideally want to use a distance in meters.
Do you have any suggestions how we can convert between degrees and meters in the context of our internal representation (i.e. our grid resides on a unit sphere)

If you mean having a copy of the cartesian coordinates representation of the input point, just call my populate_xyz_ccordinates function for the input.
If you mean given any point in degree, convert it to the cartesian coordinates, we have node_lonlat_to_xyz() function
I am more curious about how you can present the distance(radius) in degrees here?

In our BallTree implementation, which we are constructing using either the corner modes or face centers, the distance is computed using the Haversine distance metric in Sklearn (https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html)
Specifically, if we indicate a radius of "90", searches for all points within a circle with radius 90 degrees

I wrote a function called angle_of_two_vector on my branch, which can return the angle between two vectors on the unit spheres in cartesian coordinates if that's what you're looking for. I can also create a pull request for that.

That will deffinitly be useful eventually, however what I'm interest is whether there's any convention or conversion that needs to be done to be able to support a "meter" or "kilometer" radius as opposed to using "degrees"

For example, instead of doing "Find the minimum of all values within 25deg of some (lon, lat)", we would want "Find the minimum of all the values within 10km of some (lon, lat)". Since we currently represent all of our data on the unit sphere, is there any additional conversion that we need to do to go from "degrees" to "meters"

Since the data is more likely to be on the Earth, you can use 6.37122e6 * to convert from radians to meters. The conversion from degrees to radians is trivial. You may want an optional argument which allows folks to override the default sphere radius. Note that distances should never use the Euclidian distance in latlon space (e.g., sqrt((lon1-lon0)^2+(lat1-lat0)^2)).

@philipc2

philipc2 commented Oct 5, 2023 •

Copy link
Copy Markdown
Member Author

Since the data is more likely to be on the Earth, you can use 6.37122e6 * to convert from radians to meters.

Got it! This would still only be an approximation right? Since the earth isn't truly a "sphere?

You may want an optional argument which allows folks to override the default sphere radius

This is a good idea, and something MPAS already handles, would using a radius other than 1 change some of our internal computation (i.e. face areas or Cartesian conversion?)

Note that distances should never use the Euclidian distance in latlon space (e.g., sqrt((lon1-lon0)^2+(lat1-lat0)^2)).

We are using the Haversine distance for our BallTree distance metric, which should be adequate.

@paullric

paullric commented Oct 5, 2023 via email

Copy link
Copy Markdown
Contributor

@philipc2

philipc2 commented Oct 5, 2023 •

Copy link
Copy Markdown
Member Author

Yes, but nobody uses the ellipsoid. The error is at most ~0.3%, and only relevant for very long arcs.

Makes sense! Thank you!

@aaronzedwick

Copy link
Copy Markdown
Member

@philipc2 Would a standard deviation operation be useful as an analysis tool as well?

@philipc2

Copy link
Copy Markdown
Member Author

@philipc2 Would a standard deviation operation be useful as an analysis tool as well?

It would!

@ahijevyc

Copy link
Copy Markdown
Collaborator

This work is great. In addition to min sea level pressure and maximum wind speed, for hi-res hurricane simulations, we want the maximum radius of 34-knot winds in each quadrant of the hurricane. Would this utility help with that? This is less straight forward than a max function because we don't want the maximum of a variable, but the max distance at which the variable (wind speed) exceeds a threshold.

@philipc2

Copy link
Copy Markdown
Member Author

This work is great. In addition to min sea level pressure and maximum wind speed, for hi-res hurricane simulations, we want the maximum radius of 34-knot winds in each quadrant of the hurricane. Would this utility help with that? This is less straight forward than a max function because we don't want the maximum of a variable, but the max distance at which the variable (wind speed) exceeds a threshold.

Would you be able to provide a visual on the hurricane quadrants? These functions wouldn't be able to compute the max distance, but we could manipulate the way we query our tree data structure to return a max distance, I can look into it.

@rajeeja

rajeeja commented Oct 17, 2023

Copy link
Copy Markdown
Contributor

Is the typical calculation around a radius or a lat/lon box?:
Screenshot 2023-10-17 at 15 56 52

@ahijevyc

ahijevyc commented Oct 18, 2023 •

Copy link
Copy Markdown
Collaborator

Sure. Calculation is within a radius. With the hurricane in the center of the image, the quadrants are numbered 1-4 starting with the northeast quadrant and counting up clockwise. The background represents wind speed on an unstructured grid with higher values in warmer colors. We want the greatest distance of a wind speed, red or greater, within each quadrant and within the circle. In this case, there are no cells with wind speed >= red in quandrants 1, 3, or 4. Only quadrant 2 has a non-zero wind speed maximum radius, represented by the length of the yellow arrow.
TCquadrant_max_radius_of_speed_ge_34

@rajeeja

rajeeja commented Oct 19, 2023

Copy link
Copy Markdown
Contributor

TCquadrant_max_radius_of_speed_ge_34

Thanks for sharing this example image

@aaronzedwick aaronzedwick linked an issue Nov 3, 2023 that may be closed by this pull request
@philipc2 philipc2 closed this Dec 19, 2023
@philipc2 philipc2 reopened this Dec 19, 2023
@philipc2

Copy link
Copy Markdown
Member Author

Equivalent functionality in #616

@philipc2 philipc2 closed this Dec 19, 2023
@erogluorhan
erogluorhan deleted the philipc2/spatial-operators branch December 27, 2023 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement UxDataArray.spatial_std Implement UxDataArray.spatial_mean Implement UxDataArray.spatial_max Implement UxDataArray.spatial_min

6 participants