Skip to content

Commit

Permalink
Merge pull request #303 from boutproject/xarray-2024-07
Browse files Browse the repository at this point in the history
Update to xarray 2023.1.0
  • Loading branch information
ZedThree authored Dec 10, 2024
2 parents d113e0e + 609c784 commit 0761754
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 295 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
if: always()
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.x"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false

steps:
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
run: |
sudo apt-get update && sudo apt-get install libhdf5-dev libnetcdf-dev
python -m pip install --upgrade pip
pip install xarray~=0.18.0 pandas~=1.4.0
pip install xarray~=2023.1.0 pandas~=1.4.0
- name: Install package
run: |
pip install -e .[tests]
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get update && sudo apt-get install libhdf5-dev libnetcdf-dev
python -m pip install --upgrade pip
- name: Install package
run: |
Expand Down Expand Up @@ -59,9 +58,8 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get update && sudo apt-get install libhdf5-dev libnetcdf-dev
python -m pip install --upgrade pip
pip install xarray~=0.18.0 pandas~=1.4.0
pip install xarray~=2023.1.0 pandas~=1.4.0
- name: Install package
run: |
pip install -e .[tests]
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ruff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ jobs:
dependency-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install ruff
run: pip install ruff
- name: Run ruff
run: ruff xbout
run: ruff check xbout
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ classifiers = [
]
requires-python = ">=3.8"
dependencies = [
"xarray>=0.18.0,<2022.9.0",
"xarray>=2023.01.0",
"boutdata>=0.1.4",
"dask[array]>=2.10.0",
"gelidum>=0.5.3",
Expand Down Expand Up @@ -82,5 +82,5 @@ write_to = "xbout/_version.py"
[tool.setuptools]
packages = ["xbout"]

[tool.ruff]
[tool.ruff.lint]
ignore = ["E501"]
12 changes: 6 additions & 6 deletions xbout/boutdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,12 @@ def interpolate_to_cartesian(
n_toroidal = ds.sizes[zdim]

# Create Cartesian grid to interpolate to
Xmin = ds["X_cartesian"].min()
Xmax = ds["X_cartesian"].max()
Ymin = ds["Y_cartesian"].min()
Ymax = ds["Y_cartesian"].max()
Zmin = ds["Z_cartesian"].min()
Zmax = ds["Z_cartesian"].max()
Xmin = ds["X_cartesian"].min().data[()]
Xmax = ds["X_cartesian"].max().data[()]
Ymin = ds["Y_cartesian"].min().data[()]
Ymax = ds["Y_cartesian"].max().data[()]
Zmin = ds["Z_cartesian"].min().data[()]
Zmax = ds["Z_cartesian"].max().data[()]
newX_1d = xr.DataArray(np.linspace(Xmin, Xmax, nX), dims="X")
newX = newX_1d.expand_dims({"Y": nY, "Z": nZ}, axis=[1, 2])
newY_1d = xr.DataArray(np.linspace(Ymin, Ymax, nY), dims="Y")
Expand Down
17 changes: 9 additions & 8 deletions xbout/geometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
_set_attrs_on_all_vars,
_set_as_coord,
_1d_coord_from_spacing,
_maybe_rename_dimension,
)

REGISTERED_GEOMETRIES = {}
Expand Down Expand Up @@ -144,7 +145,7 @@ def apply_geometry(ds, geometry_name, *, coordinates=None, grid=None):
# 'dx' may not be consistent between different regions (e.g. core and PFR).
# For some geometries xcoord may have already been created by
# add_geometry_coords, in which case we do not need this.
nx = updated_ds.dims[xcoord]
nx = updated_ds.sizes[xcoord]

# can't use commented out version, uncommented one works around xarray bug
# removing attrs
Expand Down Expand Up @@ -181,7 +182,7 @@ def apply_geometry(ds, geometry_name, *, coordinates=None, grid=None):
if zcoord in updated_ds.dims and zcoord not in updated_ds.coords:
# Generates a coordinate whose value is 0 on the first grid point, not dz/2, to
# match how BOUT++ generates fields from input file expressions.
nz = updated_ds.dims[zcoord]
nz = updated_ds.sizes[zcoord]

# In BOUT++ v5, dz is either a Field2D or Field3D.
# We can use it as a 1D coordinate if it's a Field3D, _or_ if nz == 1
Expand Down Expand Up @@ -213,7 +214,7 @@ def apply_geometry(ds, geometry_name, *, coordinates=None, grid=None):
dz = updated_ds["dz"]

z0 = 2 * np.pi * updated_ds.metadata["ZMIN"]
z1 = z0 + nz * dz
z1 = z0 + nz * dz.data[()]
if not np.all(
np.isclose(
z1,
Expand Down Expand Up @@ -392,12 +393,12 @@ def add_toroidal_geometry_coords(ds, *, coordinates=None, grid=None):
],
)

if "t" in ds.dims:
if coordinates["t"] != "t":
# Rename 't' if user requested it
ds = ds.rename(t=coordinates["t"])
ds = _maybe_rename_dimension(ds, "t", coordinates["t"])

# Change names of dimensions to Orthogonal Toroidal ones
ds = ds.rename(y=coordinates["y"])
ds = _maybe_rename_dimension(ds, "y", coordinates["y"])

# TODO automatically make this coordinate 1D in simplified cases?
ds = ds.rename(psixy=coordinates["x"])
Expand All @@ -413,7 +414,7 @@ def add_toroidal_geometry_coords(ds, *, coordinates=None, grid=None):

# If full data (not just grid file) then toroidal dim will be present
if "z" in ds.dims:
ds = ds.rename(z=coordinates["z"])
ds = _maybe_rename_dimension(ds, "z", coordinates["z"])

# Record which dimension 'z' was renamed to.
ds.metadata["bout_zdim"] = coordinates["z"]
Expand Down Expand Up @@ -505,7 +506,7 @@ def add_s_alpha_geometry_coords(ds, *, coordinates=None, grid=None):
ds["r"] = ds["hthe"].isel({ycoord: 0}).squeeze(drop=True)
ds["r"].attrs["units"] = "m"
ds = ds.set_coords("r")
ds = ds.rename(x="r")
ds = ds.swap_dims(x="r")
ds.metadata["bout_xdim"] = "r"

if hthe_from_grid:
Expand Down
Loading

0 comments on commit 0761754

Please sign in to comment.