The task structure#
Instead of plain functions, ExoSim uses a task system. A
exosim.tasks.task.Task is a class that carries out one operation and,
through Logger, comes with logging built in. Tasks
have a fixed shape, so they can be swapped for custom versions (see
Custom tasks).
Write a task#
First, create a class that inherits from Task:
from exosim.tasks.task import Task
class ExampleTask(Task):
"""
This is an example Task
"""
Then declare the inputs in __init__, with
add_task_param:
def __init__(self):
"""
Parameters
__________
parameters: dict
dictionary containing the parameters. This is usually parsed from :class:`~exosim.tasks.load.load_options.LoadOptions`
wavelength: :class:`~astropy.units.Quantity`
wavelength grid.
output: :class:`~exosim.output.output.Output` (optional)
output file
"""
self.add_task_param('parameters', 'channel parameters dict')
self.add_task_param('wavelength', 'wavelength grid')
self.add_task_param('output', 'output file', None)
Here the task takes three inputs: a dictionary, a wavelength grid and an output
file. The last one is optional, since it is given the default value None.
Then describe what the task does, in execute:
def execute(self):
parameters = self.get_task_param('parameters')
wavelength = self.get_task_param('wavelength')
output = self.get_task_param('output')
...
variable = None
self.set_output(variable)
get_task_param returns the value associated with
the input name. After the work is done, the result is handed back with
set_output. To return several values, pass a
list:
variable1 = None
variable2 = None
self.set_output([variable1, variable2])
Logging#
The Task class provides the same logging methods as
Logger:
self.info("info message")
self.debug("debug message")
self.warning("warning message")
self.error("error message")
self.critical("critical message")
They can be used in any method of the task.
Use a task#
Initialise the task, then call it with its parameters:
exampleTask = ExampleTask()
variable = exampleTask(parameters=par_dic, wavelength=wl_grid)