Skip to content

Commit e23d622

Browse files
authored
Merge branch 'master' into runid-sort
2 parents 9b50486 + c5a8eda commit e23d622

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

xbout/boutdataarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def interpolate_parallel(
352352
da[ycoord].attrs = {}
353353

354354
da = da.interp(
355-
{ycoord: y_fine.data},
355+
{ycoord: y_fine},
356356
assume_sorted=True,
357357
method=method,
358358
kwargs={"fill_value": "extrapolate"},

xbout/boutdataset.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,10 +1239,15 @@ def is_list(variable):
12391239
aspect=this_aspect,
12401240
vmin=this_vmin,
12411241
vmax=this_vmax,
1242+
logscale=this_logscale,
12421243
**this_kwargs,
12431244
)
12441245
)
12451246
else:
1247+
if this_vmin is None:
1248+
this_vmin = min(np.min(w).values for w in v)
1249+
if this_vmax is None:
1250+
this_vmax = max(np.max(w).values for w in v)
12461251
for w in v:
12471252
blocks.append(
12481253
animate_line(
@@ -1254,6 +1259,7 @@ def is_list(variable):
12541259
aspect=this_aspect,
12551260
vmin=this_vmin,
12561261
vmax=this_vmax,
1262+
logscale=this_logscale,
12571263
label=w.name,
12581264
**this_kwargs,
12591265
)

xbout/load.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
from natsort import natsorted
1212

1313
from . import geometries
14-
from .utils import _set_attrs_on_all_vars, _separate_metadata, _check_filetype, _is_path
14+
from .utils import (
15+
_set_attrs_on_all_vars,
16+
_separate_metadata,
17+
_check_filetype,
18+
_is_path,
19+
_is_dir,
20+
)
1521

1622

1723
_BOUT_PER_PROC_VARIABLES = [
@@ -60,6 +66,7 @@ def open_boutdataset(
6066
inputfilepath=None,
6167
geometry=None,
6268
gridfilepath=None,
69+
grid_mismatch="raise", #: Union[Literal["raise"], Literal["warn"], Literal["ignore"]]
6370
chunks=None,
6471
keep_xboundaries=True,
6572
keep_yboundaries=False,
@@ -121,6 +128,12 @@ def open_boutdataset(
121128
gridfilepath : str, optional
122129
The path to a grid file, containing any variables needed to apply the geometry
123130
specified by the 'geometry' option, which are not contained in the dump files.
131+
This may either be the path of the grid file itself, or the directory
132+
relative to which the grid from the settings file can be found.
133+
grid_mismatch : str, optional
134+
How to handle if the grid is not the grid that has been used for the
135+
simulation. Can be "raise" to raise a RuntimeError, "warn" to raise a
136+
warning, or ignore to ignore the mismatch silently.
124137
keep_xboundaries : bool, optional
125138
If true, keep x-direction boundary cells (the cells past the physical
126139
edges of the grid, where boundary conditions are set); increases the
@@ -302,6 +315,13 @@ def attrs_remove_section(obj, section):
302315
if info:
303316
print("Applying {} geometry conventions".format(geometry))
304317

318+
if _is_dir(gridfilepath):
319+
if "grid" in ds.options:
320+
gridfilepath += "/" + ds.options["grid"]
321+
else:
322+
warn(
323+
"gridfilepath set to a directory, but no grid used in simulation. Continuing without grid."
324+
)
305325
if gridfilepath is not None:
306326
grid = _open_grid(
307327
gridfilepath,
@@ -317,6 +337,19 @@ def attrs_remove_section(obj, section):
317337
if info:
318338
warn("No geometry type found, no physical coordinates will be added")
319339

340+
if grid:
341+
grididoptions = ds.metadata.get("grid_id", None)
342+
grididfile = grid.get("grid_id", None)
343+
if grididoptions and grididfile and grididfile != grididoptions:
344+
msg = f"""The grid used for the simulation is not the one used.
345+
For the simulation {grididoptions} was used,
346+
but we did load {grididfile}."""
347+
if grid_mismatch == "warn":
348+
warn(msg)
349+
elif grid_mismatch == "ignore":
350+
pass
351+
else:
352+
raise ValueError(msg)
320353
# Update coordinates to match particular geometry of grid
321354
ds = geometries.apply_geometry(ds, geometry, grid=grid)
322355

xbout/plotting/animate.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ def animate_line(
520520
axis_coords=None,
521521
vmin=None,
522522
vmax=None,
523+
logscale=False,
523524
fps=10,
524525
save_as=None,
525526
sep_pos=None,
@@ -555,6 +556,12 @@ def animate_line(
555556
vmax : float, optional
556557
Maximum value to use for colorbar. Default is to use maximum value of
557558
data across whole timeseries.
559+
logscale : bool or float, optional
560+
If True, default to a logarithmic color scale instead of a linear one.
561+
If a non-bool type is passed it is treated as a float used to set the linear
562+
threshold of a symmetric logarithmic scale as
563+
linthresh=min(abs(vmin),abs(vmax))*logscale, defaults to 1e-5 if True is
564+
passed.
558565
fps : int, optional
559566
Frames per second of resulting gif
560567
save_as : True or str, optional
@@ -640,6 +647,17 @@ def animate_line(
640647
y_label = y_label + f" [{data.units}]"
641648
ax.set_ylabel(y_label)
642649

650+
if logscale:
651+
if vmin * vmax > 0.0:
652+
ax.set_yscale("log")
653+
else:
654+
if not isinstance(logscale, bool):
655+
linear_scale = logscale
656+
else:
657+
linear_scale = 1.0e-5
658+
linear_threshold = min(abs(vmin), abs(vmax)) * linear_scale
659+
ax.set_yscale("symlog", linthresh=linear_threshold)
660+
643661
# Plot separatrix
644662
if sep_pos:
645663
ax.plot_vline(sep_pos, "--")

xbout/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from copy import deepcopy
22
from itertools import chain
33
from pathlib import Path
4+
import os
45

56
import numpy as np
67
import xarray as xr
@@ -46,6 +47,10 @@ def _is_path(p):
4647
return False
4748

4849

50+
def _is_dir(p):
51+
return _is_path(p) and os.path.isdir(p)
52+
53+
4954
def _separate_metadata(ds):
5055
"""
5156
Extract the metadata (nxpe, myg etc.) from the Dataset.

0 commit comments

Comments
 (0)