Skip to content

Fix loading of grid files#307

Merged
bendudson merged 8 commits intomasterfrom
fix-gridfile-loading
Apr 18, 2025
Merged

Fix loading of grid files#307
bendudson merged 8 commits intomasterfrom
fix-gridfile-loading

Conversation

@johnomotani
Copy link
Collaborator

@johnomotani johnomotani commented Nov 13, 2024

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 referencing 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!

mrhardman and others added 6 commits December 6, 2024 15:19
…t file as a dump file rather than a grid file.
…. Mike Kryjak indicates that this coordinate use was originally an error.
…s labels. Mike Kryjak indicates that this coordinate use was originally an error."

This reverts commit 168244b.
Seems it was removed in `master`, then the merge partly put
it back. Hopefully it's supposed to be removed...
@bendudson bendudson merged commit f416a66 into master Apr 18, 2025
@bendudson bendudson deleted the fix-gridfile-loading branch April 18, 2025 17:55
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

5 participants