PsiData#
- PsiData(ifile, /, model='mas', **kwargs)[source]#
Open a PSI MAS or POT3D HDF file and return the appropriate data reader.
This is the recommended entry point for reading PSI model output. The function inspects the file extension and the model argument to select the correct concrete reader class, then instantiates and returns it.
Returned object
The returned object is a lazy reader that holds an open file handle. The HDF dataset is not copied into memory at construction time — data transfer happens only when
read()is called (or when the underlyingdatahandle is explicitly indexed). The object exposes:read()— primary method for loading a slice of the dataset.scales—Scales(r, t, p)named tuple of coordinate scale readers; each element supports the sameread()interface.quantity— canonical lower-case quantity string (e.g.'br').sequence— integer time-step sequence number.unit—Unitfor converting from code units to physical units.mesh— tuple ofMeshflags describing the Yee-grid stagger position of each axis.description— human-readable description of the physical quantity.native_properties— fullPropsdescriptor.
The object also supports the context-manager protocol; the file handle is closed on exit from the
withblock.The read method
odata[, r, t, p] = reader.read(*args, units=None, mesh=None, scales=True)
Positional arguments — each positional argument selects elements along one spatial axis in physical
(r, θ, φ)order. Supported forms:Omitted /
None— full axis (slice(None)).int— single index; the axis is retained as a length-1 dimension.slice— standard Python slice.(start, stop)or(start, stop, step)— converted to a slice.Ellipsis— expands toNonefor all remaining axes.
Keyword arguments:
units— output unit. String aliases:'native'/'code'/'model'return values in the custom MAS code unit;'real'/'phys'/'physical'decompose to CGS base units. Any other string is forwarded toUnitand must be parseable by it — this includes SI and CGS unit names ('Gauss','nT','T'), compound expressions ('km/s','erg/cm**2/s'), and scale prefixes ('mG','μT'); see astropy:unit-format for the full grammar. AnUnitinstance may also be passed directly.mesh— target mesh stagger (MeshCodeType). Half-mesh axes that are on the main mesh in mesh are averaged to the main mesh viaremesh_arr()before return.scales— ifTrue(default), return the coordinate slice for each axis as additionalQuantityvalues after the data.
Note
PSI HDF files are written in Fortran column-major order so that numpy reads them with shape
(Nφ, Nθ, Nr)— the last axis is radial. Thereadpositional arguments and coordinate scales are always returned in physical(r, θ, φ)order regardless of storage order.Warning
POT3D unit convention
POT3D applies no normalization to its output. The stored values are in whatever physical units the input photospheric boundary magnetogram was provided in — most commonly Gauss, but this is not encoded in the file. The default
unitfor POT3D quantities is thereforedimensionless_unscaled(scale factor 1), meaning that callingread(units='physical')will not perform a meaningful unit conversion unless the correct unit is supplied explicitly via the unit keyword argument at construction time:reader = PsiData('br001.h5', model='pot3d', unit='Gauss') data, r, t, p = reader.read() # data.unit == u.Gauss
- Parameters:
- ifile
PathLike Path to the HDF4 (
.hdf) or HDF5 (.h5) file.- model{‘mas’, ‘pot3d’}, optional
PSI model type. Defaults to
'mas'.- dataset_id
str, optional Name of the dataset within the HDF file. Defaults to the PSI standard identifier for the given format. Only needed when the file uses a non-standard dataset name.
- quantity
str, optional Override the quantity name inferred from the filename or file attributes (e.g.
'br'). Must be a key in the appropriate properties mapping.- sequence
int, optional Override the time-step sequence number inferred from the filename or file attributes.
- unit
strorastropy.units.Unit, optional Override the code-to-physical unit derived from the quantity’s
Propsentry. Accepts any string parseable byUnit, including named units ('Gauss','nT','km/s'), compound expressions ('erg/cm**2/s'), and scale-prefixed forms ('mG','μT'); see astropy:unit-format for the complete unit grammar. AnUnitinstance may also be passed directly.- mesh
MeshCodeType, optional Override the mesh stagger inferred from the quantity’s
Propsentry. Useful for files whose stagger differs from the PSI convention.
- ifile
- Returns:
- out
H5MasDataorH4MasDataorH5Pot3dDataorH4Pot3dData An open reader object implementing the full
_HdfInterfaceAPI. The concrete type depends on model and the file extension.
- out
- Raises:
ValueErrorIf the combination of file extension and model is not supported, or if required metadata cannot be resolved from the file, its attributes, and the supplied keyword arguments.
FileNotFoundErrorIf ifile does not exist on disk.
See also
astropy.units.UnitUnit constructor — accepts strings, compound expressions, and
Unitinstances.astropy.units.Quantity.toUnit conversion used internally when a
unitsstring is supplied toread().
Examples
Read a MAS radial magnetic field file — full array with coordinate scales:
>>> from psi_io.mhd_io import PsiData >>> reader = PsiData('br001001.h5') >>> data, r, t, p = reader.read() >>> data.unit # code unit (MAS_b)
Convert to Gauss on the fly:
>>> data, r, t, p = reader.read(units='Gauss')
Read only the inner radial shell (indices 0–9 in r) in CGS base units, without returning coordinate arrays:
>>> data = reader.read(slice(0, 10), units='physical', scales=False)
Read a POT3D HDF4 file. The boundary magnetogram was in Gauss, so the unit must be declared explicitly — it cannot be inferred from the file:
>>> reader = PsiData('br001.hdf', model='pot3d', unit='Gauss') >>> data, r, t, p = reader.read()
Use as a context manager to guarantee the file handle is released:
>>> with PsiData('vr001001.h5') as reader: ... data, r, t, p = reader.read(units='km/s')
Inspect metadata without loading data:
>>> reader = PsiData('rho001001.h5') >>> reader.quantity # 'rho' >>> reader.description # 'Mass Density' >>> reader.unit # MAS_n (code unit) >>> reader.mesh # (Mesh.HALF, Mesh.HALF, Mesh.HALF) >>> reader.shape # (Nφ, Nθ, Nr) — numpy storage order