Skip to content

Commit f123650

Browse files
cleaned up version and mpl_plottin gof sgrids (not finished yet)
1 parent 00a8395 commit f123650

File tree

5 files changed

+70
-57
lines changed

5 files changed

+70
-57
lines changed

pixi.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_grids/test_sgrid.py

+56-49
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import numpy as np
44
import xarray as xr
5+
import pytest
56

67
import xarray_subset_grid.accessor # noqa: F401
78
from tests.test_utils import get_test_file_dir
89
from xarray_subset_grid.grids.sgrid import _get_location_info_from_topology
910

1011
# open dataset as zarr object using fsspec reference file system and xarray
12+
try:
13+
import fsspec
14+
except ImportError:
15+
fsspec = None
1116

1217

1318
test_dir = get_test_file_dir()
@@ -33,56 +38,58 @@ def test_grid_topology_location_parse():
3338
'coords': ['lon_rho', 'lat_rho'],
3439
'padding': {'xi_rho': 'both', 'eta_rho': 'both'}}
3540

41+
@pytest.mark.online
42+
def test_polygon_subset():
43+
'''
44+
This is a basic integration test for the subsetting of a ROMS sgrid dataset using a polygon.
45+
'''
46+
if fsspec is None:
47+
raise ImportError("Must have fsspec installed to run --online tests")
48+
fs = fsspec.filesystem(
49+
"reference",
50+
fo="s3://nextgen-dmac-cloud-ingest/nos/wcofs/nos.wcofs.2ds.best.nc.zarr",
51+
remote_protocol="s3",
52+
remote_options={"anon": True},
53+
target_protocol="s3",
54+
target_options={"anon": True},
55+
)
56+
m = fs.get_mapper("")
57+
58+
ds = xr.open_dataset(
59+
m, engine="zarr", backend_kwargs=dict(consolidated=False), chunks={}
60+
)
61+
62+
polygon = np.array(
63+
[
64+
[-122.38488806417945, 34.98888604471138],
65+
[-122.02425311530737, 33.300351211467074],
66+
[-120.60402628930146, 32.723214427630836],
67+
[-116.63789131284673, 32.54346959375448],
68+
[-116.39346090873218, 33.8541384965596],
69+
[-118.83845767505964, 35.257586401855164],
70+
[-121.34541503969862, 35.50073821008141],
71+
[-122.38488806417945, 34.98888604471138],
72+
]
73+
)
74+
ds_temp = ds.xsg.subset_vars(['temp_sur'])
75+
ds_subset = ds_temp.xsg.subset_polygon(polygon)
3676

37-
# def test_polygon_subset():
38-
# '''
39-
# This is a basic integration test for the subsetting of a ROMS sgrid dataset using a polygon.
40-
# '''
41-
# fs = fsspec.filesystem(
42-
# "reference",
43-
# fo="s3://nextgen-dmac-cloud-ingest/nos/wcofs/nos.wcofs.2ds.best.nc.zarr",
44-
# remote_protocol="s3",
45-
# remote_options={"anon": True},
46-
# target_protocol="s3",
47-
# target_options={"anon": True},
48-
# )
49-
# m = fs.get_mapper("")
50-
51-
# ds = xr.open_dataset(
52-
# m, engine="zarr", backend_kwargs=dict(consolidated=False), chunks={}
53-
# )
54-
55-
# polygon = np.array(
56-
# [
57-
# [-122.38488806417945, 34.98888604471138],
58-
# [-122.02425311530737, 33.300351211467074],
59-
# [-120.60402628930146, 32.723214427630836],
60-
# [-116.63789131284673, 32.54346959375448],
61-
# [-116.39346090873218, 33.8541384965596],
62-
# [-118.83845767505964, 35.257586401855164],
63-
# [-121.34541503969862, 35.50073821008141],
64-
# [-122.38488806417945, 34.98888604471138],
65-
# ]
66-
# )
67-
# ds_temp = ds.xsg.subset_vars(['temp_sur'])
68-
# ds_subset = ds_temp.xsg.subset_polygon(polygon)
69-
70-
# #Check that the subset dataset has the correct dimensions given the original padding
71-
# assert ds_subset.sizes['eta_rho'] == ds_subset.sizes['eta_psi'] + 1
72-
# assert ds_subset.sizes['eta_u'] == ds_subset.sizes['eta_psi'] + 1
73-
# assert ds_subset.sizes['eta_v'] == ds_subset.sizes['eta_psi']
74-
# assert ds_subset.sizes['xi_rho'] == ds_subset.sizes['xi_psi'] + 1
75-
# assert ds_subset.sizes['xi_u'] == ds_subset.sizes['xi_psi']
76-
# assert ds_subset.sizes['xi_v'] == ds_subset.sizes['xi_psi'] + 1
77-
78-
# #Check that the subset rho/psi/u/v positional relationsip makes sense aka psi point is
79-
# #'between' it's neighbor rho points
80-
# #Note that this needs to be better generalized; it's not trivial to write a test that
81-
# #works in all potential cases.
82-
# assert (ds_subset['lon_rho'][0,0] < ds_subset['lon_psi'][0,0]
83-
# and ds_subset['lon_rho'][0,1] > ds_subset['lon_psi'][0,0])
84-
85-
# #ds_subset.temp_sur.isel(ocean_time=0).plot(x="lon_rho", y="lat_rho")
77+
#Check that the subset dataset has the correct dimensions given the original padding
78+
assert ds_subset.sizes['eta_rho'] == ds_subset.sizes['eta_psi'] + 1
79+
assert ds_subset.sizes['eta_u'] == ds_subset.sizes['eta_psi'] + 1
80+
assert ds_subset.sizes['eta_v'] == ds_subset.sizes['eta_psi']
81+
assert ds_subset.sizes['xi_rho'] == ds_subset.sizes['xi_psi'] + 1
82+
assert ds_subset.sizes['xi_u'] == ds_subset.sizes['xi_psi']
83+
assert ds_subset.sizes['xi_v'] == ds_subset.sizes['xi_psi'] + 1
84+
85+
#Check that the subset rho/psi/u/v positional relationsip makes sense aka psi point is
86+
#'between' it's neighbor rho points
87+
#Note that this needs to be better generalized; it's not trivial to write a test that
88+
#works in all potential cases.
89+
assert (ds_subset['lon_rho'][0,0] < ds_subset['lon_psi'][0,0]
90+
and ds_subset['lon_rho'][0,1] > ds_subset['lon_psi'][0,0])
91+
92+
#ds_subset.temp_sur.isel(ocean_time=0).plot(x="lon_rho", y="lat_rho")
8693

8794
def test_polygon_subset_2():
8895
ds = xr.open_dataset(sample_sgrid_file, decode_times=False)

tests/test_visualization/test_mpl_plotting.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ def test_plot_ugrid_start_index_1():
6868
# SGRID tests
6969
#############
7070

71-
def test_plot_sgrid_only_grid():
72-
import cftime
73-
ds = xr.open_dataset(EXAMPLE_DATA / "wcofs_small_subset.nc")
71+
# def test_plot_sgrid_only_grid():
72+
# import cftime
73+
# ds = xr.open_dataset(EXAMPLE_DATA / "wcofs_small_subset.nc", decode_times=False)
7474

75-
fig, axis = plt.subplots()
75+
# fig, axis = plt.subplots()
7676

77-
plot_sgrid(axis, ds)
77+
# plot_sgrid(axis, ds)
7878

79-
fig.savefig(OUTPUT_DIR / "sgrid_just_plot")
79+
# fig.savefig(OUTPUT_DIR / "sgrid_just_plot")
8080

8181

8282

xarray_subset_grid/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
from . import accessor # noqa
66
from .selector import Selector
77

8+
from._version import __version__
9+
810
__all__ = ['Selector']

xarray_subset_grid/visualization/mpl_plotting.py

+4
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,13 @@ def plot_sgrid(axes, ds, nodes=False, rho_points=False, edge_points=False):
111111
(where U and V are in ROMS)
112112
"""
113113

114+
raise NotImplementedError("have to port ugrid code to Sgrid")
115+
114116
mesh_defs = ds[ds.cf.cf_roles["grid_topology"][0]].attrs
115117
lon_var, lat_var = mesh_defs["node_coordinates"].split()
116118
nodes_lon, nodes_lat = (ds[n] for n in mesh_defs["node_coordinates"].split())
119+
120+
117121
faces = ds[mesh_defs["face_node_connectivity"]]
118122

119123
if faces.shape[0] == 3:

0 commit comments

Comments
 (0)