Skip to content

Commit

Permalink
moved subset parse back into sgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-hennen committed Sep 13, 2024
1 parent 02fc924 commit e8692f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
1 change: 0 additions & 1 deletion tests/test_grids/test_sgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def test_polygon_subset():
#Check that the subset rho/psi/u/v positional relationsip makes sense aka psi point is 'between' it's neighbor rho points
#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]
breakpoint()

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

Expand Down
27 changes: 26 additions & 1 deletion xarray_subset_grid/grids/sgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from xarray_subset_grid.grid import Grid
from xarray_subset_grid.selector import Selector
from xarray_subset_grid.utils import compute_2d_subset_mask, parse_padding_string
from xarray_subset_grid.utils import compute_2d_subset_mask


class SGridSelector(Selector):
Expand Down Expand Up @@ -187,3 +187,28 @@ def _get_sgrid_dim_coord_names(
coords.append(v.split(" "))

return list(zip(dims, coords))

def parse_padding_string(dim_string):
'''
Given a grid_topology dimension string, parse the padding for each dimension.
Returns a dict of {dim0name: padding,
dim1name: padding
}
valid values of padding are: 'none', 'low', 'high', 'both'
'''
parsed_string = dim_string.replace('(padding: ', '').replace(')', '').replace(':', '')
split_parsed_string = parsed_string.split(' ')
if len(split_parsed_string) == 6:
return {split_parsed_string[0]:split_parsed_string[2], split_parsed_string[3]:split_parsed_string[5]}
elif len(split_parsed_string) == 5:
if split_parsed_string[4] in {'none', 'low', 'high', 'both'}:
#2nd dim has padding, and with len 5 that means first does not
split_parsed_string.insert(2, 'none')
else:
split_parsed_string.insert(5, 'none')
return {split_parsed_string[0]:split_parsed_string[2], split_parsed_string[3]:split_parsed_string[5]}
elif len(split_parsed_string) == 2:
#node dimensions string could look like this: 'node_dimensions: xi_psi eta_psi'
return {split_parsed_string[0]: 'none', split_parsed_string[1]: 'none'}
else:
raise ValueError(f"Padding parsing failure: {dim_string}")
25 changes: 0 additions & 25 deletions xarray_subset_grid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,4 @@ def compute_2d_subset_mask(
polygon_mask = np.where(polygon_mask > 1, True, False)

return xr.DataArray(polygon_mask, dims=mask_dims)

def parse_padding_string(dim_string):
'''
Given a grid_topology dimension string, parse the padding for each dimension.
Returns a dict of {dim0name: padding,
dim1name: padding
}
valid values of padding are: 'none', 'low', 'high', 'both'
'''
parsed_string = dim_string.replace('(padding: ', '').replace(')', '').replace(':', '')
split_parsed_string = parsed_string.split(' ')
if len(split_parsed_string) == 6:
return {split_parsed_string[0]:split_parsed_string[2], split_parsed_string[3]:split_parsed_string[5]}
elif len(split_parsed_string) == 5:
if split_parsed_string[4] in {'none', 'low', 'high', 'both'}:
#2nd dim has padding, and with len 5 that means first does not
split_parsed_string.insert(2, 'none')
else:
split_parsed_string.insert(5, 'none')
return {split_parsed_string[0]:split_parsed_string[2], split_parsed_string[3]:split_parsed_string[5]}
elif len(split_parsed_string) == 2:
#node dimensions string could look like this: 'node_dimensions: xi_psi eta_psi'
return {split_parsed_string[0]: 'none', split_parsed_string[1]: 'none'}
else:
raise ValueError(f"Padding parsing failure: {dim_string}")

0 comments on commit e8692f8

Please sign in to comment.