1
1
import os
2
2
3
- import fsspec
4
3
import numpy as np
5
- import pytest
6
4
import xarray as xr
7
5
8
6
import xarray_subset_grid .accessor # noqa: F401
9
7
from tests .test_utils import get_test_file_dir
8
+ from xarray_subset_grid .grids .sgrid import _get_location_info_from_topology
10
9
11
10
# open dataset as zarr object using fsspec reference file system and xarray
12
11
13
12
14
13
test_dir = get_test_file_dir ()
15
14
sample_sgrid_file = os .path .join (test_dir , 'arakawa_c_test_grid.nc' )
16
15
17
- @pytest .mark .online
18
- def test_polygon_subset ():
19
- '''
20
- This is a basic integration test for the subsetting of a ROMS sgrid dataset using a polygon.
21
- '''
22
- if fsspec is None :
23
- raise ImportError ("Must have fsspec installed to run --online tests" )
24
-
25
- fs = fsspec .filesystem (
26
- "reference" ,
27
- fo = "s3://nextgen-dmac-cloud-ingest/nos/wcofs/nos.wcofs.2ds.best.nc.zarr" ,
28
- remote_protocol = "s3" ,
29
- remote_options = {"anon" : True },
30
- target_protocol = "s3" ,
31
- target_options = {"anon" : True },
32
- )
33
- m = fs .get_mapper ("" )
34
-
35
- ds = xr .open_dataset (
36
- m , engine = "zarr" , backend_kwargs = dict (consolidated = False ), chunks = {}
37
- )
38
-
39
- polygon = np .array (
40
- [
41
- [- 122.38488806417945 , 34.98888604471138 ],
42
- [- 122.02425311530737 , 33.300351211467074 ],
43
- [- 120.60402628930146 , 32.723214427630836 ],
44
- [- 116.63789131284673 , 32.54346959375448 ],
45
- [- 116.39346090873218 , 33.8541384965596 ],
46
- [- 118.83845767505964 , 35.257586401855164 ],
47
- [- 121.34541503969862 , 35.50073821008141 ],
48
- [- 122.38488806417945 , 34.98888604471138 ],
49
- ]
50
- )
51
- ds_temp = ds .xsg .subset_vars (['temp_sur' ])
52
- ds_subset = ds_temp .xsg .subset_polygon (polygon )
16
+ def test_grid_topology_location_parse ():
17
+ ds = xr .open_dataset (sample_sgrid_file , decode_times = False )
18
+ node_info = _get_location_info_from_topology (ds ['grid' ], 'node' )
19
+ edge1_info = _get_location_info_from_topology (ds ['grid' ], 'edge1' )
20
+ edge2_info = _get_location_info_from_topology (ds ['grid' ], 'edge2' )
21
+ face_info = _get_location_info_from_topology (ds ['grid' ], 'face' )
53
22
54
- #Check that the subset dataset has the correct dimensions given the original padding
55
- assert ds_subset .sizes ['eta_rho' ] == ds_subset .sizes ['eta_psi' ] + 1
56
- assert ds_subset .sizes ['eta_u' ] == ds_subset .sizes ['eta_psi' ] + 1
57
- assert ds_subset .sizes ['eta_v' ] == ds_subset .sizes ['eta_psi' ]
58
- assert ds_subset .sizes ['xi_rho' ] == ds_subset .sizes ['xi_psi' ] + 1
59
- assert ds_subset .sizes ['xi_u' ] == ds_subset .sizes ['xi_psi' ]
60
- assert ds_subset .sizes ['xi_v' ] == ds_subset .sizes ['xi_psi' ] + 1
23
+ assert node_info == {'dims' : ['xi_psi' , 'eta_psi' ],
24
+ 'coords' : ['lon_psi' , 'lat_psi' ],
25
+ 'padding' : {'xi_psi' : 'none' , 'eta_psi' : 'none' }}
26
+ assert edge1_info == {'dims' : ['xi_u' , 'eta_u' ],
27
+ 'coords' : ['lon_u' , 'lat_u' ],
28
+ 'padding' : {'eta_u' : 'both' , 'xi_u' : 'none' }}
29
+ assert edge2_info == {'dims' : ['xi_v' , 'eta_v' ],
30
+ 'coords' : ['lon_v' , 'lat_v' ],
31
+ 'padding' : {'xi_v' : 'both' , 'eta_v' : 'none' }}
32
+ assert face_info == {'dims' : ['xi_rho' , 'eta_rho' ],
33
+ 'coords' : ['lon_rho' , 'lat_rho' ],
34
+ 'padding' : {'xi_rho' : 'both' , 'eta_rho' : 'both' }}
35
+
36
+
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
+ # )
61
54
62
- #Check that the subset rho/psi/u/v positional relationsip makes sense aka psi point is
63
- #'between' it's neighbor rho points
64
- #Note that this needs to be better generalized; it's not trivial to write a test that
65
- #works in all potential cases.
66
- assert (ds_subset ['lon_rho' ][0 ,0 ] < ds_subset ['lon_psi' ][0 ,0 ]
67
- and ds_subset ['lon_rho' ][0 ,1 ] > ds_subset ['lon_psi' ][0 ,0 ])
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)
68
69
69
- #ds_subset.temp_sur.isel(ocean_time=0).plot(x="lon_rho", y="lat_rho")
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")
70
86
71
87
def test_polygon_subset_2 ():
72
88
ds = xr .open_dataset (sample_sgrid_file , decode_times = False )
@@ -89,3 +105,5 @@ def test_polygon_subset_2():
89
105
90
106
assert ds_subset .lon_psi .min () <= 6.5 and ds_subset .lon_psi .max () >= 9.5
91
107
assert ds_subset .lat_psi .min () <= 37.5 and ds_subset .lat_psi .max () >= 40.5
108
+
109
+ assert 'u' in ds_subset .variables .keys ()
0 commit comments