RemixDataReader

class PlasmaCalcs.hookups.mage.remix.remix_io_tools.RemixDataReader(ftag, *, dir='.')

Bases: MageicDataReader

handles reading data from remix file(s).

The most common case is that remix data is all stored in file like ‘runname.mix.h5’
ftag: str
file tag, e.g. ‘runname’ from file like ‘runname.mix.h5’
dir: str
directory where files are located. Stored as self.dirname = os.path.abspath(dir).
While it is technically possible to have more complicated grids,
for now PlasmaCalcs assumes (and checks!) the remix grid is in polar coordinates,
and uses dimensions named (radius, theta).
This helps to avoid ambiguity with the i, j, k dims of gamera and other mage calculators.
This is okay because, e.g., i=7 would represent very different regions in remix vs gamera.

Methods

directly_loadable_vars()

return tuple of all directly loadable vars from 'Step#0' of file0.

file0_attr(key, *[, decode])

return value of attr, from self.open_file0().attrs[key].

file0_attrs(*[, decode])

return dict of file0.attrs.

get_var(v, step, *[, slices])

return numpy array of variable v at this step.

get_var0(v, step)

return numpy array of variable v at this step, from file0,

grid([x, center, cache])

return xarray.Dataset of grid coordinates.

grid_coords(*[, center, cache, xy, ij, ...])

return standard grid coords as Dataset with data vars from 'x', 'y', 'theta', 'radius'.

ijk_coords(*[, center])

return dict of {'i': i_coords, 'j': j_coords, (and 'k': k_coords, if 3D)}

open_file0()

context manager for opening file0, for checking data info like shape, dtype, steps, etc.

radius(*[, center, cache, squeeze, closeness_as])

return sqrt(x^2 + y^2), in same units as grid data in file, based on grid coordinates.

steps_info()

return dict of arrays telling 's', 't', 'MJD', and possibly 'timestep' too.

theta(*[, center, cache, squeeze, closeness_as])

return arctan(y,x) % (2*pi), in radians, based on grid coordinates.

_assert_shape_and_dtype(arr, shape, dtype, ...)

helper for gamera loading routines, ensure var has expected shape and dtype,

_grid_to_center(grid)

returns grid values at cell centers, given grid values (numpy array) at corners.

_resqueeze_slices(array, unsqueezed)

squeeze array along any axes which were unsqueezed.

_unsqueeze_slices(slices)

"un-squeezes slices, returning (unsqueezed_slices, dict telling which slices were changed).

Attributes

Nshape

(Ni, Nj) or (Ni, Nj, Nk) shape of a single full data var.

file0

the file used for checking data info like shape, dtype, steps, timeAttributeCache, etc.

steps

return list of all steps available (as strs).

property Nshape

(Ni, Nj) or (Ni, Nj, Nk) shape of a single full data var.

This is the shape after transposing from fortran order.
static _assert_shape_and_dtype(arr, shape, dtype, *, var)

helper for gamera loading routines, ensure var has expected shape and dtype,

crash if shape or dtype is wrong.
arr can be array or any object with shape and dtype
(e.g. h5py dataset even if didn’t load data into memory yet).
static _grid_to_center(grid)

returns grid values at cell centers, given grid values (numpy array) at corners.

grid: 1D, 2D, or 3D numpy array

grid values at corners, with shape (Ni+1,), (Ni+1, Nj+1), or (Ni+1, Nj+1, Nk+1).
result has shape (Ni,), (Ni, Nj), or (Ni, Nj, Nk).
static _resqueeze_slices(array, unsqueezed)

squeeze array along any axes which were unsqueezed.

assumes array axes will be ‘i’, ‘j’, (and ‘k’ if 3D).
static _unsqueeze_slices(slices)

“un-squeezes slices, returning (unsqueezed_slices, dict telling which slices were changed).

unsqueeze converts integer to iterable of length 1.
The relevant dim should be squeezed away at some point to respect original inputs,
but for internal processing it is nice to not worry about number of dims changing.
directly_loadable_vars()

return tuple of all directly loadable vars from ‘Step#0’ of file0.

property file0

the file used for checking data info like shape, dtype, steps, timeAttributeCache, etc.

Here, just returns abspath to the {self.ftag}.mix.h5 file.
file0_attr(key, *, decode=True)

return value of attr, from self.open_file0().attrs[key].

decode: bool, whether to result.decode(‘utf-8’) if result is a bytes string.
file0_attrs(*, decode=True)

return dict of file0.attrs.

E.g. result[‘UnitsID’] tells the UnitsID for the data.
decode: bool, whether to s.decode(‘utf-8’) for bytes strings s.
get_var(v, step, *, slices=None)

return numpy array of variable v at this step.

(Transposed from fortran order to python order.)
slices: None or dict
if provided, only load the pieces needed by slices; keys can be i, j, k.
Not implemented here; subclass should implement. (See also: self.get_var0)
get_var0(v, step)

return numpy array of variable v at this step, from file0,

assuming file0 data shape matches full Nshape (else will crash).
(Transposed from fortran order to python order.)
grid(x=None, *, center=True, cache=True)

return xarray.Dataset of grid coordinates.

Will return DataArray instead if x is a single str.
x: None, ‘x’, ‘y’, ‘z’, or list of strs.
tells which coordinates to return. (‘X’, ‘Y’, ‘Z’ in the files themselves.)
‘x’, ‘y’, or ‘z’ –> return DataArray of that coordinate only.
list of strs –> return Dataset but with those coordinates only.
None –> (‘x’, ‘y’) if ‘Z’ missing from file0, else (‘x’, ‘y’, ‘z’)
center: bool
whether to return coordinates at cell centers.
When False, the grid has shape (Ni+1, Nj+1) or (Ni+1, Nj+1, Nk+1).
telling the coordinates at grid corners.
When True, the grid has shape (Ni, Nj) or (Ni, Nj, Nk),
telling the coordinates at grid centers.
cache: bool
whether to cache result (for x and center).
if True, cache result in case it is reloaded.
if False, don’t check or alter cache.
grid_coords(*, center=True, cache=True, xy=True, ij=False, as_coords=False, u_l=None)

return standard grid coords as Dataset with data vars from ‘x’, ‘y’, ‘theta’, ‘radius’.

Some kwargs control which data vars are included, and what will be the underlying dims.
‘theta’ and ‘radius’ are always provided in the result, as 1D coords,
and will crash if that is not possible.
center, cache:
passed directly to self.grid(); see help(self.grid) for details.
xy: bool
whether to include ‘x’ and ‘y’ coords in the result.
ij: bool
whether to include ‘i’ and ‘j’ coords and dims in the result.
if False, squeezing to get ‘theta’ and ‘radius’ must be successful (both must be 1D),
and will use ‘theta’ and ‘radius’ as dims instead of ‘i’ and ‘j’.
if True, will use ‘i’ and ‘j’ as dims.
as_coords: bool
whether to set_coords for all data vars in result.
if False, data_vars will not be included in result.coords.
if True, will do result.set_coords(result.data_vars).
u_l: None or number
if provided, scale length coords (i.e. x, y, radius, but not theta) by u_l.
if u_l=1, length coords’ units match units of raw data from file.
ijk_coords(*, center=False)

return dict of {‘i’: i_coords, ‘j’: j_coords, (and ‘k’: k_coords, if 3D)}

where coords depend on center:
if center: i_coords = np.arange(Ni)
else: i_coords = -0.5+np.arange(1+Ni)
and similar for j_coords and k_coords.
open_file0()

context manager for opening file0, for checking data info like shape, dtype, steps, etc.

radius(*, center=True, cache=True, squeeze='force', closeness_as='coords')

return sqrt(x^2 + y^2), in same units as grid data in file, based on grid coordinates.

There is a good chance that result will be a 1D array (if squeeze=True),
because radius should only vary with one of the coordinates (j).
center: bool
whether to center grid coordinates on cell centers (True) or use the full grid.
if False, result will have size Nj+1 instead of Nj.
if True, centering will occur after squeezing when squeeze=’force’.
cache: bool
whether to use caching, instead of recalculating each time.
squeeze: ‘force’ or bool
whether to squeeze result to 1D array, if possible.
if ‘force’, crash if squeezing isn’t possible.
closeness_as: ‘coords’, ‘attrs’, or None
where to put info about closeness; see xarray_squeeze_close for details.
property steps

return list of all steps available (as strs).

Sorts steps in increasing order of step number, i.e. step 7 comes before step 10.
steps_info()

return dict of arrays telling ‘s’, ‘t’, ‘MJD’, and possibly ‘timestep’ too.

‘s’ is step number (as str); ‘t’ is time; ‘MJD’ is Modified Julian Date.
theta(*, center=True, cache=True, squeeze='force', closeness_as='coords')

return arctan(y,x) % (2*pi), in radians, based on grid coordinates.

There is a good chance that result will be a 1D array (if squeeze=True),
because theta should only vary with one of the coordinates (i).
center: bool
whether to center grid coordinates on cell centers (True) or use the full grid.
if False, result will have size Ni+1 instead of Ni.
if True, centering will occur after squeezing when squeeze=’force’.
cache: bool
whether to use caching, instead of recalculating each time.
squeeze: ‘force’ or bool
whether to squeeze result to 1D array, if possible.
if ‘force’, crash if squeezing isn’t possible.
closeness_as: ‘coords’, ‘attrs’, or None
where to put info about closeness; see xarray_squeeze_close for details.