Skip to content

Commit 4cf3fbe

Browse files
committed
Generalized convex_hull calculation to use dynamical tlon and tlat instead of hardcoded lon, lat
1 parent aa5f136 commit 4cf3fbe

3 files changed

Lines changed: 26 additions & 18 deletions

File tree

examples/example_nc_particles.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import xarray as xr
88
import trajan as ta
99

10+
1011
#%%
1112
# Demonstrating analysis of a trajecory_dataset from GNOME in the nc_particles format
1213
# https://noaa-orr-erd.github.io/nc_particles/nc_particle_standard.html
@@ -45,4 +46,4 @@
4546

4647
#%%
4748
# Basic animation
48-
ds_ortho.traj.animate().set_title('nc_particles sample dataset').show()
49+
ds_ortho.traj.animate(land=None).set_title('nc_particles sample dataset').show()

tests/test_convex_hull.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
import numpy as np
3+
import scipy
4+
import trajan as ta
5+
6+
7+
def test_convex_hull(openoil):
8+
ch = openoil.isel(time=-1).traj.convex_hull()
9+
assert isinstance(ch, scipy.spatial._qhull.ConvexHull)
10+
11+
cha = openoil.isel(time=-1).traj.get_area_convex_hull()
12+
np.testing.assert_allclose(cha.values, 7.963e+09, rtol=1e-5)

trajan/traj/__init__.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,9 @@ def crs(self) -> pyproj.crs.CRS:
433433
transform, set_crs
434434
"""
435435
if len(self.ds.cf.grid_mapping_names) == 0:
436-
logger.debug(
437-
f'No grid-mapping specified, checking if coordinates are lon/lat..'
438-
)
436+
#logger.debug(
437+
# f'No grid-mapping specified, checking if coordinates are lon/lat..'
438+
#)
439439
if self.tx.name == 'lon' or self.tx.name == 'longitude':
440440
# assume this is in latlon projection
441441
return self.__gcrs__
@@ -932,11 +932,8 @@ def convex_hull(self):
932932

933933
from scipy.spatial import ConvexHull
934934

935-
lon = self.ds.lon
936-
lat = self.ds.lat
937-
if 'status' in self.ds.variables:
938-
lon = lon.where(self.ds.status == 0)
939-
lat = lat.where(self.ds.status == 0)
935+
lon = self.ds.traj.tlon.values.ravel()
936+
lat = self.ds.traj.tlat.values.ravel()
940937
fin = np.isfinite(lat + lon)
941938
if np.sum(fin) <= 3:
942939
return None
@@ -980,12 +977,10 @@ def get_area_convex_hull(self):
980977
"""
981978
from scipy.spatial import ConvexHull
982979

983-
if 'status' in self.ds.variables:
984-
lon = self.ds.lon.where(self.ds.status == 0) # OpenDrift specific
985-
lat = self.ds.lat.where(self.ds.status == 0)
986-
else:
987-
lon = self.ds.lon.where(np.isfinite(self.ds.lon) is True)
988-
lat = self.ds.lat.where(np.isfinite(self.ds.lat) is True)
980+
lon = self.ds.traj.tlon.values.ravel()
981+
lat = self.ds.traj.tlat.values.ravel()
982+
lon = lon[np.isfinite(lon)]
983+
lat = lat[np.isfinite(lat)]
989984

990985
fin = np.isfinite(lat + lon)
991986
if np.sum(fin) <= 3:
@@ -996,10 +991,10 @@ def get_area_convex_hull(self):
996991
return xr.DataArray(0,
997992
name="convex_hull_area",
998993
attrs={"units": "m2"})
999-
lat = lat.where(fin)
1000-
lon = lon.where(fin)
994+
lat = lat[fin]
995+
lon = lon[fin]
1001996
aea = pyproj.Proj(
1002-
f'+proj=aea +lat_0={lat.mean().values} +lat_1={lat.min().values} +lat_2={lat.max().values} +lon_0={lon.mean().values} +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs'
997+
f'+proj=aea +lat_0={lat.mean()} +lat_1={lat.min()} +lat_2={lat.max()} +lon_0={lon.mean()} +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs'
1003998
)
1004999
x, y = aea(lat, lon, inverse=False)
10051000
fin = np.isfinite(x + y)

0 commit comments

Comments
 (0)