.. _postprocess:

POSTPROCESS
#############

An optional :term:`verbatim` section that post processes the |cx| variables after running the :ref:`predictions` section. This section can be used to alter the final |cx| variables, or possibly remove some data, |eg| negative concentrations.

A kind of flexible post generation filter implemented in |python|, similar in functionality to |preprocess|.

The |postprocess| is available in the following scripts:-

* :ref:`tut_script`
* :ref:`mtut_script`
* :ref:`gen_script`
* :ref:`mgen_script`

|ie| any script that generates a population data set from a model.

.. _example_postprocess:

Example POSTPROCESS section
=======================================

Some simple examples of using a |postprocess| section. Use the 'return' syntax to remove observation rows with negative concentrations:-

.. code-block:: pyml

    POSTPROCESS: |
        if c[CONC] < 0.0 and c[TYPE] == 'obs': 
            return
        
Setting negative concentrations to zero for observation rows:-

.. code-block:: pyml

    POSTPROCESS: |
        if c[CONC] < 0.0 and c[TYPE] == 'obs':
            c[CONC] = 0.0

Enforcing a below quantification limit for observation rows:-

.. code-block:: pyml

    POSTPROCESS: |
        bql_limit = 5.0
        if c[CONC] < bql_limit and c[TYPE] == 'obs':
            c[CONC] = bql_limit

Creating some derived data for observation rows:-

.. code-block:: pyml

    POSTPROCESS: |
        if c[CONC] > 100 and c[TYPE] == 'obs':
            c[HIGH_CONC_FLAG] = 1.0
        else:
            c[HIGH_CONC_FLAG] = 0.0
            

Like the |preprocess| section each row of the data is processed separately, but otherwise any valid |python| function can be used to create new |cx| data or remove rows using the "return" syntax.

.. _rules_postprocess:

Rules for POSTPROCESS section
=======================================

Like all :term:`verbatim` sections the |postprocess| section of the config file accepts free form pseudo |python| code, but there are some rules regarding which variables are allowed in a |postprocess| section as follows:- 

* **Only** |cx| variables and local |python| variables are allowed 
* |cx| on the |rhs| and within :python:`if` statements must be previously defined on the |lhs| or in the |data_file|
* |cx| declared on the |lhs| must |not| already exist in the |data_file|
* return must always be **null**

So you can |not| use |mx|, |fx|, |rx|, |dx| etc variables in this section. 

Like all :term:`verbatim` sections it is possible to introduce syntax errors by writing malformed |python|. This will hopefully be picked up when |popy| attempts to compile or run the |postprocess| function as a temporary .py file.
