Skip to content

Commit

Permalink
Merge branch 'main' into rajeeja/from_file_notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
rajeeja authored Dec 17, 2024
2 parents 348191b + 79275c5 commit dfc44e4
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 452 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.2
rev: v0.8.3
hooks:
# Run the linter.
- id: ruff
Expand Down
1 change: 1 addition & 0 deletions ci/asv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
- pandas
- pathlib
- pre_commit
- polars
- pyarrow
- pytest
- pytest-cov
Expand Down
1 change: 1 addition & 0 deletions ci/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies:
- pandas
- geocat-datafiles
- spatialpandas
- polars
- geopandas
- pip:
- antimeridian
Expand Down
1 change: 1 addition & 0 deletions ci/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
- pandas
- pathlib
- pre_commit
- polars
- pyarrow
- pytest
- pip
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies = [
"geopandas",
"xarray",
"hvplot",
"polars",
]
# minimal dependencies end

Expand Down
87 changes: 22 additions & 65 deletions test/test_arcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import uxarray as ux

from uxarray.grid.coordinates import _lonlat_rad_to_xyz
from uxarray.grid.arcs import point_within_gca, _point_within_gca_cartesian
from uxarray.grid.arcs import point_within_gca

try:
import constants
Expand All @@ -31,65 +31,46 @@ class TestIntersectionPoint(TestCase):
def test_pt_within_gcr(self):
# The GCR that's eexactly 180 degrees will have Value Error raised

gcr_180degree_cart = [
gcr_180degree_cart = np.asarray([
_lonlat_rad_to_xyz(0.0, np.pi / 2.0),
_lonlat_rad_to_xyz(0.0, -np.pi / 2.0)
]
])
pt_same_lon_in = np.asarray(_lonlat_rad_to_xyz(0.0, 0.0))

pt_same_lon_in = _lonlat_rad_to_xyz(0.0, 0.0)
with self.assertRaises(ValueError):
_point_within_gca_cartesian(pt_same_lon_in, gcr_180degree_cart)

point_within_gca(pt_same_lon_in, gcr_180degree_cart[0],gcr_180degree_cart[1] )
#
# Test when the point and the GCA all have the same longitude
gcr_same_lon_cart = [
gcr_same_lon_cart = np.asarray([
_lonlat_rad_to_xyz(0.0, 1.5),
_lonlat_rad_to_xyz(0.0, -1.5)
]
pt_same_lon_in = _lonlat_rad_to_xyz(0.0, 0.0)
self.assertTrue(_point_within_gca_cartesian(pt_same_lon_in, gcr_same_lon_cart))
])
pt_same_lon_in = np.asarray(_lonlat_rad_to_xyz(0.0, 0.0))
self.assertTrue(point_within_gca(pt_same_lon_in, gcr_same_lon_cart[0], gcr_same_lon_cart[1]))

pt_same_lon_out = _lonlat_rad_to_xyz(0.0, 1.500000000000001)
res = _point_within_gca_cartesian(pt_same_lon_out, gcr_same_lon_cart)
pt_same_lon_out = np.asarray(_lonlat_rad_to_xyz(0.0, 1.5000001))
res = point_within_gca(pt_same_lon_out, gcr_same_lon_cart[0], gcr_same_lon_cart[1])
self.assertFalse(res)

pt_same_lon_out_2 = _lonlat_rad_to_xyz(0.1, 1.0)
res = _point_within_gca_cartesian(pt_same_lon_out_2, gcr_same_lon_cart)
pt_same_lon_out_2 = np.asarray(_lonlat_rad_to_xyz(0.1, 1.0))
res = point_within_gca(pt_same_lon_out_2, gcr_same_lon_cart[0], gcr_same_lon_cart[1])
self.assertFalse(res)

# And if we increase the digital place by one, it should be true again
pt_same_lon_out_add_one_place = _lonlat_rad_to_xyz(0.0, 1.5000000000000001)
res = _point_within_gca_cartesian(pt_same_lon_out_add_one_place, gcr_same_lon_cart)
self.assertTrue(res)

# Normal case
# GCR vertex0 in radian : [1.3003315590159483, -0.007004587172323237],
# GCR vertex1 in radian : [3.5997458123873827, -1.4893379576608758]
# Point in radian : [1.3005410084914981, -0.010444274637648326]
gcr_cart_2 = np.array([[0.267, 0.963, -0.007], [-0.073, -0.036,
-0.997]])
pt_cart_within = np.array(
[0.25616109352676675, 0.9246590335292105, -0.010021496695000144])
self.assertTrue(_point_within_gca_cartesian(pt_cart_within, gcr_cart_2, True))

# Test other more complicate cases : The anti-meridian case

def test_pt_within_gcr_antimeridian(self):
# GCR vertex0 in radian : [5.163808182822441, 0.6351384888657234],
# GCR vertex1 in radian : [0.8280410325693055, 0.42237025187091526]
# Point in radian : [0.12574759138415173, 0.770098701904903]
gcr_cart = np.array([[0.351, -0.724, 0.593], [0.617, 0.672, 0.410]])
pt_cart = np.array(
[0.9438777657502077, 0.1193199333436068, 0.922714737029319])
self.assertTrue(_point_within_gca_cartesian(pt_cart, gcr_cart, is_directed=True))
# If we swap the gcr, it should throw a value error since it's larger than 180 degree
self.assertTrue(
point_within_gca(pt_cart, gcr_cart[0], gcr_cart[1]))

gcr_cart_flip = np.array([[0.617, 0.672, 0.410], [0.351, -0.724,
0.593]])
with self.assertRaises(ValueError):
_point_within_gca_cartesian(pt_cart, gcr_cart_flip, is_directed=True)

# If we flip the gcr in the undirected mode, it should still work
self.assertTrue(
_point_within_gca_cartesian(pt_cart, gcr_cart_flip, is_directed=False))
point_within_gca(pt_cart, gcr_cart_flip[0], gcr_cart_flip[1]))

# 2nd anti-meridian case
# GCR vertex0 in radian : [4.104711496596806, 0.5352983676533828],
Expand All @@ -100,22 +81,9 @@ def test_pt_within_gcr_antimeridian(self):
pt_cart_within = np.array(
[0.6136726305712109, 0.28442243941920053, -0.365605190899831])
self.assertFalse(
_point_within_gca_cartesian(pt_cart_within, gcr_cart_1, is_directed=True))
self.assertFalse(
_point_within_gca_cartesian(pt_cart_within, gcr_cart_1, is_directed=False))

# The first case should not work and the second should work
v1_rad = [0.1, 0.0]
v2_rad = [2 * np.pi - 0.1, 0.0]
v1_cart = _lonlat_rad_to_xyz(v1_rad[0], v1_rad[1])
v2_cart = _lonlat_rad_to_xyz(v2_rad[0], v1_rad[1])
gcr_cart = np.array([v1_cart, v2_cart])
pt_cart = _lonlat_rad_to_xyz(0.01, 0.0)
with self.assertRaises(ValueError):
_point_within_gca_cartesian(pt_cart, gcr_cart, is_directed=True)
gcr_car_flipped = np.array([v2_cart, v1_cart])
self.assertTrue(
_point_within_gca_cartesian(pt_cart, gcr_car_flipped, is_directed=True))
point_within_gca(pt_cart_within, gcr_cart_1[0], gcr_cart_1[1]))



def test_pt_within_gcr_cross_pole(self):
gcr_cart = np.array([[0.351, 0.0, 0.3], [-0.351, 0.0, 0.3]])
Expand All @@ -125,15 +93,4 @@ def test_pt_within_gcr_cross_pole(self):
# Normalize the point abd the GCA
pt_cart = pt_cart / np.linalg.norm(pt_cart)
gcr_cart = np.array([x / np.linalg.norm(x) for x in gcr_cart])
self.assertTrue(_point_within_gca_cartesian(pt_cart, gcr_cart, is_directed=False))

gcr_cart = np.array([[0.351, 0.0, 0.3], [-0.351, 0.0, -0.6]])
pt_cart = np.array(
[0.10, 0.0, 0.8])

# When the point is not within the GCA
pt_cart = pt_cart / np.linalg.norm(pt_cart)
gcr_cart = np.array([x / np.linalg.norm(x) for x in gcr_cart])
self.assertFalse(_point_within_gca_cartesian(pt_cart, gcr_cart, is_directed=False))
with self.assertRaises(ValueError):
_point_within_gca_cartesian(pt_cart, gcr_cart, is_directed=True)
self.assertTrue(point_within_gca(pt_cart, gcr_cart[0], gcr_cart[1]))
25 changes: 25 additions & 0 deletions test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,31 @@ def test_angle_of_2_vectors(self):
v2 = np.array([1.0, 0.0, 0.0])
self.assertAlmostEqual(_angle_of_2_vectors(v1, v2), 0.0)

def test_angle_of_2_vectors_180_degree(self):
GCR1_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(0.0),
np.deg2rad(0.0)),
_lonlat_rad_to_xyz(np.deg2rad(181.0),
np.deg2rad(0.0))
])

res = _angle_of_2_vectors( GCR1_cart[0], GCR1_cart[1])

# The angle between the two vectors should be 181 degree
self.assertAlmostEqual(res, np.deg2rad(181.0), places=8)

GCR1_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(170.0),
np.deg2rad(89.0)),
_lonlat_rad_to_xyz(np.deg2rad(170.0),
np.deg2rad(-10.0))
])

res = _angle_of_2_vectors( GCR1_cart[0], GCR1_cart[1])

# The angle between the two vectors should be 181 degree
self.assertAlmostEqual(res, np.deg2rad(89.0+10.0), places=8)


class TestFaceEdgeConnectivityHelper(TestCase):

Expand Down
Loading

0 comments on commit dfc44e4

Please sign in to comment.