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