Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrote the subset selector for sgrid, added test based on example notebook #61

Merged
merged 9 commits into from
Sep 13, 2024
24 changes: 23 additions & 1 deletion tests/test_grids/test_sgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
import xarray_subset_grid.accessor # noqa: F401
from xarray_subset_grid.utils import ray_tracing_numpy
# open dataset as zarr object using fsspec reference file system and xarray

from xarray_subset_grid.utils import get_test_file_dir

from gridded import Variable, VectorVariable

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to put this inline:

TEST_DIR = Path(__file__).parent.parent / 'test_data'

sample_sgrid_file = TEST_DIR / 'arakawa_c_test_grid.nc'

test_dir = get_test_file_dir()
sample_sgrid_file = os.path.join(test_dir, 'arakawa_c_test_grid.nc')

def test_polygon_subset():
'''
This is a basic integration test for the subsetting of a ROMS sgrid dataset using a polygon.
Expand Down Expand Up @@ -51,4 +59,18 @@ def test_polygon_subset():
#Note that this needs to be better generalized; it's not trivial to write a test that works in all potential cases.
assert ds_subset['lon_rho'][0,0] < ds_subset['lon_psi'][0,0] and ds_subset['lon_rho'][0,1] > ds_subset['lon_psi'][0,0]

#ds_subset.temp_sur.isel(ocean_time=0).plot(x="lon_rho", y="lat_rho")
#ds_subset.temp_sur.isel(ocean_time=0).plot(x="lon_rho", y="lat_rho")

def test_polygon_subset_2():
ds = xr.open_dataset(sample_sgrid_file)
polygon = np.array(
[6.5, 37.5],
[6.5, 39.5],
[9.5, 40.5],
[8.5, 37.5],
[6.5, 37.5]
)
ds_subset = ds.xsg.subset_polygon(polygon)

breakpoint()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want this in there :-)


24 changes: 24 additions & 0 deletions tests/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Assorted utilities useful for the tests.
"""

import os
import contextlib

try:
import urllib.request as urllib_request # for python 3
except ImportError:
import urllib2 as urllib_request # for python 2

import pytest
import glob

from .get_remote_data import get_datafile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want to put this in xarray_subset_grid.

I imagine we will put something like this in libgoods, or maybe gridded, but not here.



def get_test_file_dir():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can just put this in the test file directly -- or maybe make it a fixture.

And pathlib is great:

from pathlib import Path

test_file_dir = Path(__file__).parent / 'test_data')`

"""
returns the test file dir path
"""
test_file_dir = os.path.join(os.path.dirname(__file__), 'test_data')
return test_file_dir