Skip to content

Commit 7808092

Browse files
authored
Merge pull request #46 from chrishavlin/fix_axis_flipping
apply the reversal with xr commands when reading
2 parents 26a2ace + e979b57 commit 7808092

File tree

6 files changed

+28
-19
lines changed

6 files changed

+28
-19
lines changed

HISTORY.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# History
22

3+
## 0.1.4 (2023-03-21)
4+
5+
Bug fix release.
6+
7+
### Fixes:
8+
* fix coordinate flipping bug (PR [46](https://github.com/data-exp-lab/yt_xarray/pull/46))
39
## 0.1.3 (2023-03-10)
410

5-
Bug fix release.
11+
Bug fix release.
612

713
### Fixes:
814
* handle the case where data coordinate lengths are 1 (PR [41](https://github.com/data-exp-lab/yt_xarray/pull/41))

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.1.3
2+
current_version = 0.1.4
33
commit = True
44
tag = True
55

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@
4141
test_suite="tests",
4242
tests_require=test_requirements,
4343
url="https://github.com/data-exp-lab/yt_xarray",
44-
version="0.1.3",
44+
version="0.1.4",
4545
zip_safe=False,
4646
)

yt_xarray/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
__author__ = """Chris Havlin"""
44
__email__ = "chris.havlin@gmail.com"
5-
__version__ = "0.1.3"
5+
__version__ = "0.1.4"
66

77

8-
# import the yt frontend and the xarray accessor so they are registered with
9-
# their respective codes
8+
# import the the xarray accessor so it is registered with xarray
109

1110
from .accessor import YtAccessor
1211
from .accessor._xr_to_yt import known_coord_aliases

yt_xarray/accessor/_readers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ def _reader(grid, field_name):
8181
# load into memory (if its not) as xr DataArray
8282
datavals = datavals.load()
8383

84+
# reverse axis ordering if needed
85+
for axname in sel_info.reverse_axis_names:
86+
dimvals = getattr(datavals, axname)
87+
datavals = datavals.sel({axname: dimvals[::-1]})
88+
8489
if interp_required:
8590
# interpolate from nodes to cell centers across all remaining dims
8691
datavals = _xr_to_yt._interpolate_to_cell_centers(datavals)
8792

88-
# final flips to account for all the index reversing
89-
for idim in range(sel_info.ndims):
90-
if sel_info.reverse_axis[idim]:
91-
datavals = np.flip(datavals, axis=idim)
92-
9393
# return the plain values
9494
vals = datavals.values.astype(np.float64)
9595
if sel_info.ndims == 2:

yt_xarray/accessor/_xr_to_yt.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def _process_selection(self, xr_ds):
132132
cell_widths = [] # cell widths after selection
133133
grid_type = _GridType.UNIFORM # start with uniform assumption
134134
reverse_axis = [] # axes must be positive-monitonic for yt
135+
reverse_axis_names = []
135136
global_dims = [] # the global shape
136137
for c in full_coords:
137138
coord_da = getattr(xr_ds, c) # the full coordinate data array
@@ -140,6 +141,8 @@ def _process_selection(self, xr_ds):
140141
if coord_da.size > 1:
141142
rev_ax = coord_da[1] <= coord_da[0]
142143
reverse_axis.append(bool(rev_ax.values))
144+
if rev_ax:
145+
reverse_axis_names.append(c)
143146

144147
# store the global ranges
145148
global_dims.append(coord_da.size)
@@ -204,6 +207,7 @@ def _process_selection(self, xr_ds):
204207
self.grid_type = grid_type
205208
self.cell_widths = cell_widths
206209
self.reverse_axis = reverse_axis
210+
self.reverse_axis_names = reverse_axis_names
207211
self.global_dims = np.array(global_dims)
208212
# self.coord_selected_arrays = coord_selected_arrays
209213

@@ -250,9 +254,15 @@ def _validate_fields(self, xr_ds, fields: List[str]) -> List[str]:
250254

251255
def select_from_xr(self, xr_ds, field):
252256
if self.sel_dict_type == "isel":
253-
return xr_ds[field].isel(self.sel_dict)
257+
vars = xr_ds[field].isel(self.sel_dict)
254258
else:
255-
return xr_ds[field].sel(self.sel_dict)
259+
vars = xr_ds[field].sel(self.sel_dict)
260+
261+
for axname in self.reverse_axis_names:
262+
dimvals = getattr(vars, axname)
263+
vars = vars.sel({axname: dimvals[::-1]})
264+
265+
return vars
256266

257267
def interp_validation(self, geometry):
258268
# checks if yt will need to interpolate to cell center
@@ -439,12 +449,6 @@ def _load_full_field_from_xr(
439449

440450
if interp_required:
441451
vals = _interpolate_to_cell_centers(vals)
442-
if any(sel_info.reverse_axis):
443-
# if any dims are in decreaseing order, flip that axis
444-
# after reading in the data
445-
for idim, flip_it in enumerate(sel_info.reverse_axis):
446-
if flip_it:
447-
vals = np.flip(vals, axis=idim)
448452

449453
vals = vals.values.astype(np.float64)
450454
if sel_info.ndims == 2:

0 commit comments

Comments
 (0)