Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading of grid files #307

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Fix loading of grid files #307

wants to merge 2 commits into from

Conversation

johnomotani
Copy link
Collaborator

There is (now) a 'theta' variable in grid files, which prevents naming a dimension 'theta', so need to choose a different dimension name when opening a grid file.

Also keep the 'psixy' variable in the Dataset - create a new variable referecing it rather than renaming it to make the 'psi_poloidal' coordinate.

Along with boutproject/hypnotoad#187, fixes #306.

The examples/plot_grid.py script should work again.

There is a 'theta' variable in grid files, which prevents naming a
dimension 'theta'.

Also keep the 'psixy' variable in the Dataset - create a new variable
referecing it rather than renaming it to make the 'psi_poloidal'
coordinate.
@johnomotani johnomotani added the bugfix Fix for a bug label Nov 13, 2024
@johnomotani
Copy link
Collaborator Author

Test failures seem to be numpy/xarray incompatibility issues. Maybe would be fixed by #303?

@mrhardman
Copy link
Collaborator

I have tried checking out bout and #307, rebuilding, and running the scripts featured in #306 . I still see the error

Traceback (most recent call last):
  File "/*/lib/python3.13/site-packages/xarray/core/dataset.py", line 1317, in _construct_dataarray
    variable = self._variables[name]
               ~~~~~~~~~~~~~~~^^^^^^
KeyError: 't_array'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/*/plot_test.py", line 11, in <module>
    grid = open_boutdataset(gridfilepath, geometry="toroidal")
  File "/*/xBOUT/xbout/load.py", line 279, in open_boutdataset
    ds, remove_yboundaries = _auto_open_mfboutdataset(
                             ~~~~~~~~~~~~~~~~~~~~~~~~^
        datapath=datapath,
        ^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        **kwargs,
        ^^^^^^^^^
    )
    ^
  File "/*/xBOUT/xbout/load.py", line 741, in _auto_open_mfboutdataset
    _, unique_indices = unique(ds["t_array"], return_index=True)
                               ~~^^^^^^^^^^^
  File "/*/lib/python3.13/site-packages/xarray/core/dataset.py", line 1410, in __getitem__
    return self._construct_dataarray(key)
           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/*/lib/python3.13/site-packages/xarray/core/dataset.py", line 1319, in _construct_dataarray
    _, name, variable = _get_virtual_variable(self._variables, name, self.dims)
                        ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/*/lib/python3.13/site-packages/xarray/core/dataset.py", line 175, in _get_virtual_variable
    raise KeyError(key)
KeyError: 't_array'

What did I miss?

@johnomotani
Copy link
Collaborator Author

If you ncdump -h your grid file, does it have a 't' dimension? With the updated boutproject/hypnotoad#187 it shouldn't.

@mrhardman
Copy link
Collaborator

mrhardman commented Nov 14, 2024

If you ncdump -h your grid file, does it have a 't' dimension? With the updated boutproject/hypnotoad#187 it shouldn't.

Here's my check:

netcdf grid {
dimensions:
        t = UNLIMITED ; // (5 currently)

Which is odd, given that I have an editable hypnotoad install on your branch.

hypnotoad$ git log
commit b23d387b93d3a15107205a572866322ecf7c13d7 (HEAD -> leg-only-grid, origin/leg-only-grid)
Author: John Omotani <john.omotani@ukaea.uk>
Date:   Thu Nov 7 16:44:51 2024 +0000

    Make "single_region" option work for non-orthogonal grids

@johnomotani
Copy link
Collaborator Author

You need to pull the latest version, with the changes I added yesterday.

@mrhardman
Copy link
Collaborator

You need to pull the latest version, with the changes I added yesterday.

My mistake. Now I have the following:

/*/lib/python3.13/site-packages/xarray/core/concat.py:527: FutureWarning: unique with argument that is not not a Series, Index, ExtensionArray, or np.ndarray is deprecated and will raise in a future version.
  common_dims = tuple(pd.unique([d for v in vars for d in v.dims]))
Traceback (most recent call last):
  File "/home/mrhardman/hermes-3-work/hypnotoad_experiment/plot_test.py", line 18, in <module>
    grid["psi_poloidal"].bout.regions()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'BoutDataArrayAccessor' object has no attribute 'regions'

from snippet

grid = open_boutdataset(gridfilepath, geometry="toroidal")
grid["psi_poloidal"].bout.contourf()
grid["psi_poloidal"].bout.contour()
grid["psi_poloidal"].bout.pcolormesh()
grid["psi_poloidal"].bout.pcolormesh(shading="gouraud")
grid["psi_poloidal"].bout.regions()

@johnomotani
Copy link
Collaborator Author

Ah, that was just a mistake in the example plot script that I've forgotten to update. The line should have been

grid["psi_poloidal"].bout.plot_regions()

@mrhardman
Copy link
Collaborator

Plots are now generating from the example script on my local machine. Thanks for tracking down the issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Fix for a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plotting grids using open_boutdataset seems broken
2 participants