SWXData¶
- class swxsoc.swxdata.SWXData(timeseries: TimeSeries | dict[str, TimeSeries], support: dict[Quantity | NDData] | None = None, spectra: NDCollection | None = None, meta: dict | None = None, schema: SWXSchema | None = None)[source]¶
Bases:
objectA generic object for loading, storing, and manipulating space weather time series data.
- Parameters:
timeseries (
Union[astropy.timeseries.TimeSeries, Dict[str, astropy.timeseries.TimeSeries]]) – The time-series data. This can be a singleastropy.timeseries.TimeSeriesobject or a dictionary ofstrtoastropy.timeseries.TimeSeriesobjects. If a dictionary, one key must be named ‘epoch’, the primary time axis. If non-index/time columns are included in any of the TimeSeries objects, they must beQuantityarrays.support (
Optional[dict[Union[astropy.units.Quantity, astropy.nddata.NDData]]]) – Support data arrays which do not vary with time (i.e. Non-Record-Varying data).spectra (
Optional[ndcube.NDCollection]) – One or morendcube.NDCubeobjects containing spectral or higher-dimensional timeseries data.meta (
Optional[dict]) – The metadata describing the data in an ISTP-compliant format.schema (
Optional[SWXSchema]) – An optionalSWXSchemainstance for metadata derivation. If not provided, a defaultSWXSchemawill be created.
Examples
>>> import os >>> import numpy as np >>> import astropy.units as u >>> from astropy.timeseries import TimeSeries >>> from ndcube import NDCube, NDCollection >>> from astropy.wcs import WCS >>> from astropy.nddata import NDData >>> import swxsoc >>> from swxsoc.swxdata import SWXData >>> # Set mission to HERMES for this example (eea is a HERMES instrument) >>> os.environ["SWXSOC_MISSION"] = "hermes" >>> swxsoc._reconfigure() >>> # Create a TimeSeries structure >>> data = u.Quantity([1, 2, 3, 4], "gauss", dtype=np.uint16) >>> ts = TimeSeries(time_start="2016-03-22T12:30:31", time_delta=3 * u.s, data={"Bx": data}) >>> # Create a Spectra structure >>> spectra = NDCollection( ... [ ... ( ... "test_spectra", ... NDCube( ... data=np.random.random(size=(4, 10)), ... wcs=WCS(naxis=2), ... meta={"CATDESC": "Test Spectra Variable"}, ... unit="eV", ... ), ... ) ... ] ... ) >>> # Create a Support Structure >>> support_data = { ... "data_mask": NDData(data=np.eye(100, 100, dtype=np.uint16), meta={"CATDESC": "Data Mask", "VAR_TYPE": "metadata"}) ... } >>> # Create Global Metadata Attributes >>> input_attrs = SWXData.global_attribute_template("eea", "l1", "1.0.0") >>> # Create SWXData Object >>> sw_data = SWXData(timeseries=ts, support=support_data, spectra=spectra, meta=input_attrs)
- Raises:
ValueError – If the number of columns is less than 2 or the required ‘time’ column is missing.:
TypeError – If any column, excluding ‘time’, is not an
astropy.units.Quantityobject with units.:ValueError – If the elements of a
TimeSeriescolumn are multidimensional:TypeError – If any
supportdata elements are not typeastropy.nddata.NDDataorastropy.units.Quantity.:
References
Attributes Summary
(
dict) Adictcontaining each oftimeseries,spectraandsupport.(
dict) Global metadata associated with the measurement data.(
ndcube.NDCollection]) ANDCollectionobject containing high-dimensional spectra data.(
dict[Union[astropy.units.Quantity, astropy.nddata.NDData]]) Adictcontaining one or more non-time-varying support variables.(
astropy.time.Time) The times of the measurements.(
tuple) The start and end times of the times.(
astropy.timeseries.TimeSeriesordict) ATimeSeriesrepresenting one or more measurements as a function of time.Methods Summary
add_measurement(measure_name, data[, meta])Add a new time-varying scalar measurement (column).
add_spectra(name, data[, meta])Add a new time-varying vector measurement.
add_support(name, data[, meta])Add a new non-time-varying data array.
add_timeseries(epoch_key, timeseries)Add a new TimeSeries object to the collection of epochs.
append(timeseries)Add additional measurements to an existing column.
get_timeseres_epoch_key(timeseries, var_data)Function to determine the TimeSeries Epoch for a Record-Varying Variable.
global_attribute_template([instr_name, ...])Function to generate a template of the required ISTP-compliant global attributes.
load(file_path)Load data from a file.
Function to generate a template of the required measurement attributes.
plot([axes, columns, subplots])Plot the measurement data.
remove(measure_name)Remove an existing measurement or support data array.
save([output_path, overwrite])Save the data to a CDF file.
Attributes Documentation
- spectra¶
(
ndcube.NDCollection]) ANDCollectionobject containing high-dimensional spectra data.
- support¶
(
dict[Union[astropy.units.Quantity, astropy.nddata.NDData]]) Adictcontaining one or more non-time-varying support variables.
- time¶
(
astropy.time.Time) The times of the measurements.
- timeseries¶
(
astropy.timeseries.TimeSeriesordict) ATimeSeriesrepresenting one or more measurements as a function of time. If there are multipleTimeSeries, a dictionary is returned.
Methods Documentation
- add_measurement(measure_name: str, data: Quantity, meta: dict = None)[source]¶
Add a new time-varying scalar measurement (column).
- Parameters:
measure_name (
str) – Name of the measurement to add.data (
astropy.units.Quantity) – The data to add. Must have the same time stamps as the existing data.meta (
dict, optional) – The metadata associated with the measurement.
- Raises:
TypeError – If var_data is not of type Quantity.:
ValueError – If data has more than one dimension:
- add_spectra(name: str, data: NDCube, meta: dict = None)[source]¶
Add a new time-varying vector measurement. This include higher-dimensional time-varying data.
- Parameters:
name (
str) – Name of the measurement to add.data (
ndcube.NDCube) – The data to add. Must have the same time stamps as the existing data.meta (
dict, optional) – The metadata associated with the measurement.
- Raises:
TypeError – If var_data is not of type NDCube.:
- add_support(name: str, data: Quantity | NDData, meta: dict | None = None)[source]¶
Add a new non-time-varying data array.
- add_timeseries(epoch_key: str, timeseries: TimeSeries)[source]¶
Add a new TimeSeries object to the collection of epochs.
- Parameters:
epoch_key (
str) – The key to identify the new TimeSeries.timeseries (
astropy.timeseries.TimeSeries) – The time-series data to add.
- append(timeseries: TimeSeries)[source]¶
Add additional measurements to an existing column.
- Parameters:
timeseries (
astropy.timeseries.TimeSeries) – The data to be appended (rows) as aTimeSeriesobject.
- static get_timeseres_epoch_key(timeseries, var_data, var_meta: dict = None)[source]¶
Function to determine the TimeSeries Epoch for a Record-Varying Variable.
- Parameters:
timeseries (
dict[str, astropy.timeseries.TimeSeries]) – A dictionary ofstrtoastropy.timeseries.TimeSeriesobjects. EachTimeSeriesobject represents a different epoch.var_data (
astropy.units.Quantity) – The variable data that we want to find the epoch for.var_meta (
dict, optional) – The metadata associated with the variable data.
- static global_attribute_template(instr_name: str = '', data_level: str = '', version: str = '') OrderedDict[source]¶
Function to generate a template of the required ISTP-compliant global attributes.
- Parameters:
- Returns:
template (
collections.OrderedDict) – A template for required global attributes.
- classmethod load(file_path: Path)[source]¶
Load data from a file.
- Parameters:
file_path (
pathlib.Path) – A fully specified file path of the data file to load.- Returns:
data (
SWXData) – ASWXDataobject containing the loaded data.- Raises:
ValueError – If the file type is not recognized as a file type that can be loaded.:
- static measurement_attribute_template() OrderedDict[source]¶
Function to generate a template of the required measurement attributes.
- Returns:
template (
collections.OrderedDict) – A template for required variable attributes that must be provided.
- plot(axes=None, columns=None, subplots=True, **plot_args)[source]¶
Plot the measurement data.
- Parameters:
axes (
Axes, optional) – If provided the image will be plotted on the given axes. Defaults toNoneand creates a new axis.columns (
list[str], optional) – If provided, only plot the specified measurements otherwise try to plot them all.subplots (
bool) – If set, all columns are plotted in their own plot panel.**plot_args (
dict, optional) – Additional plot keyword arguments that are handed toAxes.
- Returns:
Axes– The plot axes.
- remove(measure_name: str)[source]¶
Remove an existing measurement or support data array.
- Parameters:
measure_name (
str) – Name of the variable to remove.
- save(output_path: Path = None, overwrite: bool = False)[source]¶
Save the data to a CDF file.
- Parameters:
output_path (
pathlib.Path, optional) – Path to save location. Can be: - A directory: saves using Logical_file_id from metadata as filename - A full file path (with .cdf extension): saves using that filename If not provided, saves to the current directory with Logical_file_id.overwrite (
bool) – If set, overwrites existing file of the same name.
- Returns:
path (
str) – A path to the saved file.