***********
Data access
***********

Loading run data
----------------

When running any Experiment, Station Control stores the acquired data in its database
with a given unique identifier (UUID) called run ID.

Each experiment run has a corresponding run ID, and is associated with a request to the station, which
has its own sweep ID. The IDs generated by a run can be seen on the plot titles and accessed in the metadata of
the dataset, ``experiment.run_results[-1].run_definition.run_id``.

The results of new runs are loaded into the memory, so using the UUID is not necessary to view latest results.

To view older results, the experiment class and the data can be reconstructed using the run ID, as

.. code-block:: python

    from exa.core import Experiment

    experiment = Experiment.get_by_run_id("<run_id>")
    experiment.analyze().plot()

:meth:`.Experiment.get_by_run_id` constructs the dataset from persisted pieces of raw data,
applies experiment-specific post-processing to the data, and returns a new instance of the appropriate Experiment,
with the dataset inside it.
The loading works also if the run is still ongoing and raw results are being saved.
In this case, the dataset is constructed from the available data.

Querying
--------

The ExaRuntime storage has an interface that supports querying all available runs and observations,
combined with filtering by date, user, experiment, etc.
See :class:`.StorageInterface` for available methods.

The storage interface is accessible through

.. code-block:: python

    from exa.core.provider import ExaRuntime
    storage = ExaRuntime.storage()

    # Query runs
    runs = storage.query_runs(username="John", limit=10)

    # Get run metadata
    storage.get_run("<run_id>")

