Previous: madrigal.ui.web   Up: Developer's toc   Next: CEDAR Madrigal Hdf5 File Format

Madrigal Derivation Engine

The Madrigal derivation is defined in the python module madrigal.derivation. In particular, that module begins with a collections.OrderedDict dictionary called MadrigalDerivedMethods. To add a new derivation method, simply define the new method in MadrigalDerivedMethods, and then implement it in C or python. If you add a new C method, you will also need to update the file source/madpy/madrigal/_derive.c, which is done simply by running $MADROOT/bin/python $MADROOT/source/madpy/scripts/bin/ This page goes into the derivation engine in detail.


MadrigalDerivedMethods is a collections.OrderDict in madrigal.derivation. The name of each derivation method is the key, and so must be unique. The value is a list with between two and four items.

  1. The first item is the input mnemonic(s).
  2. The second item is the output mnemonic(s).
  3. The optional third parameter is either 'python' or 'C'. If only two parameters, 'C' is assumed. This determines whether the method is implementated on C (or Fortran wrapped in C), or python.
  4. The optional fourth parameter is a list of kinst values for which the derivation is valid (used in statistical models, etc). This feature requires 'python' to be the implementation method.

The order of these derivation methods matter - they will be called in a single pass from first to last. The MadrigalDerivationPlan determines which need to be called, and what parameters are derivable given the initial measured parameters. Any parameter will be derived by the first available method that has that parameter as an output and for which all input parameters are measured or themselves derivable.

For the C derivation methods, the methods are implemented in source/madc/madrec/madDeriveMethods.c. All methods must have a signature of the form:

int methodName(int inCount,
   double * inputArr,
   int outCount,
   double * outputArr,
   FILE * errFile)

where inCount is the number of input arguments, and inputArr is a double array of length inCount, with values for each input variable. Likewise, outCount is the number of output arguments, and outputArr is already allocated double array of length outCount, where the C method sets the values for each output variable when it returns. If values cannot be calulated, that output value is set to nan.

When a new C derivation method is added, this method needs to be exposed to python. This is done through the file madpy/madrigal/_derive.c. However, you do not need to manually edit this file. Instead, cd $MADROOT/source/madpy/scripts/bin and then run:

$MADROOT/bin/python $MADROOT/source/madpy/scripts/bin/

This will create the file madpy/madrigal/_derive_new.c. Compare this new file with the existing file madpy/madrigal/_derive.c, and if the change looks reasonable, add it to source control.

For the python derivation methods, all methods are defined in madrigal.derivation.MadrigalDerivationMethods in this module. Each method has two arguments - a double array of input values of length the number of input variables, and the output array the length of the number of output variable. As in the C, the output array is passed in to the derivation method preallocated.

Previous: madrigal.ui.web   Up: Developer's toc   Next: CEDAR Madrigal Hdf5 File Format