Monitors¶

Monitor objects store states in some way, whether it is by displaying the new state on a plot that is shown to the user, updating information on a web server, or saving the state to a file. They are called like so:

monitor = MyMonitor()
monitor.store(state)


The Monitor will take advantage of the ‘time’ key in the state dictionary in order to determine the model time of the state. This is particularly important for a Monitor which outputs a series of states to disk.

class sympl.Monitor[source]
__repr__()[source]

Return repr(self).

__str__()[source]

Return str(self).

store(state)[source]

Stores the given state in the Monitor and performs class-specific actions.

Parameters: state (dict) – A model state dictionary. InvalidStateError – If state is not a valid input for the DiagnosticComponent instance.
class sympl.NetCDFMonitor(filename, time_units='seconds', store_names=None, write_on_store=False, aliases=None)[source]

A Monitor which caches stored states and then writes them to a NetCDF file when requested.

__init__(filename, time_units='seconds', store_names=None, write_on_store=False, aliases=None)[source]
Parameters: filename (str) – The file to which the NetCDF file will be written. time_units (str, optional) – The units in which time will be stored in the NetCDF file. Time is stored as an integer number of these units. Default is seconds. store_names (iterable of str, optional) – Names of quantities to store. If not given, all quantities are stored. write_on_store (bool, optional) – If True, stored changes are immediately written to file. This can result in many file open/close operations. Default is to write only when the write() method is called directly. aliases (dict) – A dictionary of string replacements to apply to state variable names before saving them in netCDF files.
store(state)[source]

Caches the given state. If write_on_store=True was passed on initialization, also writes to file. Normally a call to the write() method is required to write to file.

Parameters: state (dict) – A model state dictionary. InvalidStateError – If state is not a valid input for the DiagnosticComponent instance.
write()[source]

Write all cached states to the NetCDF file, and clear the cache. This will append to any existing NetCDF file.

Raises: InvalidStateError – If cached states do not all have the same quantities as every other cached and written state.
class sympl.PlotFunctionMonitor(plot_function, interactive=True)[source]

A Monitor which uses a user-defined function to draw figures using model state.

__init__(plot_function, interactive=True)[source]

Initialize a PlotFunctionMonitor.

Parameters: plot_function (func) – A function plot_function(fig, state) that draws the given state onto the given (initially clear) figure. interactive (bool, optional) – If true, matplotlib’s interactive mode will be enabled, allowing plot animation while other computation is running.
store(state)[source]

Updates the plot using the given state.

Parameters: state (dict) – A model state dictionary.