PsiData#
- PsiData(ifile, /, *args, **kwargs)[source]#
Open a PSI MAS or POT3D HDF file and return the appropriate data reader.
Inspects the file extension and model argument, selects the correct concrete reader (HDF5 or HDF4 backend), and returns it. No data are read from disk at construction time — metadata is resolved from the filename and HDF file attributes, and data transfer happens only inside
read()orvslice(). Full-array reads are cached automatically; partial reads are not.The returned object is an
_HdfDatainstance; see_HdfDataand the inherited_HdfArrayinterface for the full set of metadata properties (name,desc,unit,mesh,scales,shape,dtype…) and reader methods. In brief, useread()to load a slice by index andvslice()to slice by physical coordinate value with linear interpolation; both returnQuantitydata in physical(r, θ, φ)order, and the object supports the context-manager protocol.By default the reader is lazy (
cache='lazy'): array data is transferred from disk only on access, and a full-array read is then cached on the reader. Passcache='eager'to load the data immediately at construction, orcache=Noneto disable caching entirely.Warning
POT3D unit convention
POT3D applies no normalization to its output. The stored values are in whatever physical unit the input photospheric magnetogram used — most commonly Gauss, but this is not encoded in the file. The default
unitfor POT3D isdimensionless_unscaled(scale factor 1), soread(unit='physical')will not perform a meaningful conversion unless the correct unit is supplied at construction:reader = PsiData('br001.h5', model='pot3d', unit='Gauss') data, r, t, p = reader.read()
Metadata resolution
Every metadata field is drawn from up to three sources, in decreasing priority:
Keyword arguments passed to this function, named after the
ModelPropsfields (name,desc,unit,scalar,mesh,order,sequence,scales).The HDF dataset attribute dictionary (
reader.attrs), for any of those same field names stored on the file.The model-mapping defaults in
psi_io.models, consulted only when model names a recognized PSI model.
A value supplied at a higher level overrides the levels beneath it: an explicit keyword argument always wins over a file attribute, which in turn overrides the model default. This is how the keyword arguments below can correct or complete metadata that is missing, wrong, or absent from the file.
When model is
'mas'or'pot3d', the quantitynameandsequenceadditionally fall back to the values parsed from the filename stem (e.g.br001001→name='br',sequence=1001), and the resolvednameselects theModelPropsentry that provides the defaultunit,mesh,scalar,order,scales, anddesc.When model is the default
'custom', no defaults are inferred — every field must be given as a keyword argument or be present in the file attribute dictionary.name,mesh,scalar,order, andscalesare required;unit(defaults to dimensionless),sequence(defaults to0), anddesc(defaults to'') are optional. If a required field cannot be resolved,ValueError(“Missing or incompatible metadata”) is raised.- Parameters:
- ifile
PathLike Path to the HDF4 (
.hdf) or HDF5 (.h5) file.- model
ModelType, optional PSI model type. Defaults to
'custom'. When'mas'or'pot3d'is given, the reader resolves the quantity name, unit, mesh stagger, and other metadata from the corresponding mapping inpsi_io.models. With the default'custom', no metadata is inferred and the required fields (name,mesh,scalar,order,scales) must be supplied as keyword arguments.- dataset_id
str, optional Dataset name within the HDF file. Defaults to the PSI standard identifier for the given format.
- name
str, optional Override the quantity name inferred from the filename or file attributes.
- sequence
int, optional Override the time-step sequence number.
- unit
UnitLike, optional Override the code-to-physical unit from the quantity’s
ModelPropsentry. Accepts any string parseable byUnitor aUnitinstance.- mesh
MeshCodeType, optional Override the mesh stagger from the quantity’s
ModelPropsentry. Required when model is'custom'and nomeshattribute is stored on the file.- scalarbool, optional
Whether the quantity is a scalar (rather than a component of a vector) field. Required when model is
'custom'.- order
ArrayOrdering, optional Memory layout of the stored array —
'F'(Fortran / column-major, the PSI default) or'C'(row-major). Determines whethershapeand the slicing axes are reversed relative to the on-disk layout. Required when model is'custom'.- scales
Sequence, optional Names of the coordinate axes, in physical order (e.g.
('r', 't', 'p')). The argument does three things:Names the axes — the strings become the fields of the
reader.scalesnamed tuple and thenameof each coordinate scale reader.Orders them — the names are zipped positionally with the file’s stored dimension arrays, so the i-th name labels the i-th dimension; the ordering must therefore match the physical axis order.
Fixes the dimensionality —
len(scales)sets the expected dataset rank;validate_metadata()emits aMetaDataWarningif it disagrees withndim(or with the length of mesh /shape), or if any named axis does not match the size of its corresponding data dimension.
Required when model is
'custom'.- desc
str, optional Override the human-readable description. Optional; defaults to
''.- cache
CacheType, optional Cache mode.
'lazy'(default) caches the array on the first full read,'eager'loads it immediately, andNonedisables caching.
- ifile
- Returns:
- out
H5Data|H4Data Open reader implementing the full
_HdfDataAPI. Concrete type depends on the file extension.
- out
- Raises:
ValueErrorIf the file extension / model combination is unsupported or required metadata cannot be resolved.
FileNotFoundErrorIf ifile does not exist.
See also
astropy.units.UnitUnit constructor — accepts strings, compound expressions, and
Unitinstances.astropy.units.Quantity.toUnit conversion used internally when a
unitstring is supplied toread()._HdfDataBase data reader interface.
_HdfArrayBase data array interface.
Examples
Read a MAS radial field — full array with coordinate scales, then convert:
>>> from psi_io.mhd_io import PsiData >>> reader = PsiData('br001001.h5') >>> data, r, t, p = reader.read() # code units (MAS_b) >>> data, r, t, p = reader.read(unit='Gauss') # convert to Gauss
Use as a context manager:
>>> with PsiData('vr001001.h5') as reader: ... data, r, t, p = reader.read(unit='km/s')
Inspect metadata without loading data:
>>> reader = PsiData('rho001001.h5') >>> reader.name # 'rho' >>> reader.unit # MAS_n >>> reader.mesh # Mesh(HALF, HALF, HALF) >>> reader.data_cached # False