Skip to content

Commit

Permalink
Merge pull request #3 from UXARRAY/pr/357
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyuchen1030 authored Aug 9, 2023
2 parents 9d4e600 + 5b8aa6e commit 29809cb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 49 deletions.
33 changes: 21 additions & 12 deletions docs/internal_api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ UxDataset
The ``uxarray.UxDataset`` class inherits from ``xarray.Dataset``. Below is a list of
features explicitly added to work on Unstructured Grids.


Class
-----
.. autosummary::
Expand All @@ -31,7 +30,6 @@ Attributes
UxDataset._source_datasets
UxDataset._uxgrid


Methods
-------
.. autosummary::
Expand All @@ -58,15 +56,13 @@ Class

UxDataArray


Attributes
----------
.. autosummary::
:toctree: _autosummary

UxDataArray._uxgrid


Methods
-------
.. autosummary::
Expand All @@ -76,6 +72,7 @@ Methods
UxDataArray._copy
UxDataArray._replace


Grid
===========

Expand Down Expand Up @@ -111,19 +108,15 @@ Operators
Grid.__eq__
Grid.__ne__


Helpers
===========
=======

Connectivity
------------
.. autosummary::
:toctree: _autosummary

<<<<<<< HEAD
utils.helpers._is_ugrid
utils.helpers._replace_fill_values
utils.helpers._angle_of_2_vectors
=======
grid.connectivity._replace_fill_values
grid.connectivity._build_nNodes_per_face
grid.connectivity._build_edge_node_connectivity
Expand All @@ -137,10 +130,26 @@ Coordinates
grid.coordinates._populate_cartesian_xyz_coord
grid.coordinates._populate_lonlat_coord

Lines
-----
.. autosummary::
:toctree: _autosummary

grid.lines._angle_of_2_vectors

IO
---
==

Ugrid
-----
.. autosummary::
:toctree: _autosummary

io._ugrid._is_ugrid

Utils
-----
.. autosummary::
:toctree: _autosummary

io.utils._parse_grid_type
>>>>>>> main
25 changes: 8 additions & 17 deletions docs/user_api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,6 @@ Face Area
.. autosummary::
:toctree: _autosummary

<<<<<<< HEAD
calculate_face_area
calculate_spherical_triangle_jacobian
calculate_spherical_triangle_jacobian_barycentric
close_face_nodes
get_all_face_area_from_coords
get_gauss_quadratureDG
get_tri_quadratureDG
grid_center_lat_lon
node_xyz_to_lonlat_rad
node_lonlat_rad_to_xyz
normalize_in_place
parse_grid_type
is_between
point_within_GCA
=======
grid.area.calculate_face_area
grid.area.get_all_face_area_from_coords
grid.area.calculate_spherical_triangle_jacobian
Expand All @@ -180,6 +164,14 @@ Coordinates
grid.coordinates.normalize_in_place
grid.coordinates.grid_center_lat_lon

Lines
-----
.. autosummary::
:toctree: _autosummary

grid.lines.is_between
grid.lines.point_within_GCA

Numba
-----
.. autosummary::
Expand All @@ -189,4 +181,3 @@ Numba
utils.disable_jit_cache
utils.enable_jit
utils.disable_jit
>>>>>>> main
3 changes: 1 addition & 2 deletions test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,4 @@ def test_pt_within_gcr(self):
with self.assertRaises(ValueError):
point_within_GCA(pt_cart, gcr_cart)
gcr_car_flipped = np.array([v2_cart, v1_cart])
self.assertTrue(
point_within_GCA(pt_cart, gcr_car_flipped))
self.assertTrue(point_within_GCA(pt_cart, gcr_car_flipped))
24 changes: 9 additions & 15 deletions uxarray/grid/coordinates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numpy
import xarray as xr
import numpy as np
import math
Expand All @@ -22,24 +21,20 @@ def node_lonlat_rad_to_xyz(node_coord):
Returns
----------
float numpy.array
float list
the result array of the unit 3D coordinates [x, y, z] vector where :math:`x^2 + y^2 + z^2 = 1`
Raises
----------
RuntimeError
The input array doesn't have the size of 3.
"""
node_coord = np.asarray(node_coord, dtype=np.float64)
if node_coord.size != 2:
if len(node_coord) != 2:
raise RuntimeError(
"Input array should have a length of 2: [longitude, latitude]")
lon = node_coord[0]
lat = node_coord[1]
return np.array(
[np.cos(lon) * np.cos(lat),
np.sin(lon) * np.cos(lat),
np.sin(lat)])
return [np.cos(lon) * np.cos(lat), np.sin(lon) * np.cos(lat), np.sin(lat)]


@njit(cache=ENABLE_JIT_CACHE)
Expand All @@ -54,17 +49,17 @@ def node_xyz_to_lonlat_rad(node_coord):
Returns
----------
float np.array
float list
the result array of longitude and latitude in radian [longitude_rad, latitude_rad]
Raises
----------
RuntimeError
The input array doesn't have the size of 3.
"""
node_coord = np.asarray(node_coord, dtype=np.float64)
if node_coord.size != 3:
if len(node_coord) != 3:
raise RuntimeError("Input array should have a length of 3: [x, y, z]")

[dx, dy, dz] = normalize_in_place(node_coord)
dx /= np.absolute(dx * dx + dy * dy + dz * dz)
dy /= np.absolute(dx * dx + dy * dy + dz * dz)
Expand All @@ -83,7 +78,7 @@ def node_xyz_to_lonlat_rad(node_coord):
d_lon_rad = 0.0
d_lat_rad = -0.5 * np.pi

return np.array([d_lon_rad, d_lat_rad])
return [d_lon_rad, d_lat_rad]


@njit(cache=ENABLE_JIT_CACHE)
Expand All @@ -107,11 +102,10 @@ def normalize_in_place(node):
RuntimeError
The input array doesn't have the size of 3.
"""
node = np.asarray(node, dtype=np.float64)
if node.size != 3:
if len(node) != 3:
raise RuntimeError("Input array should have a length of 3: [x, y, z]")

return node / np.linalg.norm(np.array(node), ord=2)
return np.array(node) / np.linalg.norm(np.array(node), ord=2)


def grid_center_lat_lon(ds):
Expand Down
23 changes: 20 additions & 3 deletions uxarray/grid/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
from uxarray.constants import ERROR_TOLERANCE


def convert_to_list_if_needed(obj):
if not isinstance(obj, list):
if isinstance(obj, np.ndarray):
# Convert the NumPy array to a list using .tolist()
obj = obj.tolist()
else:
# If not a list or NumPy array, return the object as-is
obj = [obj]
return obj


def point_within_GCA(pt, gca_cart):
"""Check if a point lies on a given Great Circle Arc (GCA). The anti-
meridian case is also considered.
Expand Down Expand Up @@ -45,9 +56,15 @@ def point_within_GCA(pt, gca_cart):
"""
# Convert the cartesian coordinates to lonlat coordinates, the node_xyz_to_lonlat_rad is already overloaded
# with gmpy2 data type
pt_lonlat = node_xyz_to_lonlat_rad(pt)
GCRv0_lonlat = node_xyz_to_lonlat_rad(gca_cart[0])
GCRv1_lonlat = node_xyz_to_lonlat_rad(gca_cart[1])
pt_lonlat = node_xyz_to_lonlat_rad(convert_to_list_if_needed(pt))
GCRv0_lonlat = node_xyz_to_lonlat_rad(convert_to_list_if_needed(
gca_cart[0]))
GCRv1_lonlat = node_xyz_to_lonlat_rad(convert_to_list_if_needed(
gca_cart[1]))

# Convert the list to np.float64
gca_cart[0] = np.array(gca_cart[0], dtype=np.float64)
gca_cart[1] = np.array(gca_cart[1], dtype=np.float64)

# First if the input GCR is exactly 180 degree, we throw an exception, since this GCR can have multiple planes
angle = _angle_of_2_vectors(gca_cart[0], gca_cart[1])
Expand Down

0 comments on commit 29809cb

Please sign in to comment.