bsed / ala

Automatically exported from code.google.com/p/ala
0 stars 0 forks source link

DO%sat calculation in Water Quality Measurements output #580

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Add algorithm to auto-calculate and populate the "dissolved oxygen (% sat)" 
field in the "Water Quality Measurements" output according to the following 
(from BDRS:NSW Waterwatch:water quality survey). Note that the "dissolved 
oxygen (% sat)" field should not be editable.

/*  Synopsis

    OxygenSaturation.setSaturationHandlers('#saturation', '#tempC', '#dox')

    - or (before object creation, e.g. inside a BDRS form) -

    (function(OxygenSaturation, undefined) {
        OxygenSaturation.autorun = {
            'sat': '#attribute_7719',
            'tmp': '#attribute_7711',
            'dox': '#attribute_7718'
        };
    })(window.OxygenSaturation = window.OxygenSaturation || {});

    Description

    Calculate the disolved oxygen percent-saturation using the water
    temperature and disolved oxygen measure (mg/l).  Based on a
    spreadsheet from Ingrid Berthold (via Peter B).

    jQuery handlers are created to update the %sat when the temp and
    oxygen fields are changed.  The %sat field is also made read-only.

    If the dictionary `OxygenSaturation.autorun` is defined, then the handlers
    will automatically be allocated to the form fields named by the
    dictionary. This is to cope when this file is included AFTER we need it.
    The "calling" code will create the `autorun` dictionary and it will be
    used to initialise jQuery when this code is executed.

    Also see http://water.epa.gov/type/rsl/monitoring/vms52.cfm

    Copyright

        Copyright (c) 2014 by CSIRO, Australia.  All rights reserved.
*/

// Wrap code in a namespace: OxygenSaturation.*

(function ( OxygenSaturation, undefined )
{
    // hold input field IDs for %sat calculation change handlers
    var formIds = {};  // sat, tmp, dox

    /*
     Convert the temperature and disolved oxygen into the saturation.

     Input is string (or float) values, output is an int.
     (private)
     */
    function calcSaturation(temperature, disolved_oxygen)
    {
        /*
        Array index [0..45] is Temperature, value is %saturation multiplyer
         */
        var saturation_factor = [
        14.62,  // 0C
        14.22,
        13.83,
        13.46,
        13.11,
        12.77,
        12.45,
        12.14,
        11.84,
        11.56,
        11.29,  // 10C
        11.03,
        10.78,
        10.54,
        10.31,
        10.08,
        9.87,
        9.67,
        9.47,
        9.28,
        9.09,   // 20C
        8.92,
        8.72,
        8.58,
        8.42,
        8.26,
        8.11,
        7.97,
        7.83,
        7.69,
        7.56,   // 30C
        7.43,
        7.31,
        7.18,
        7.07,
        6.95,
        6.84,
        6.73,
        6.62,
        6.52,
        6.41,   // 40C
        6.31,
        6.21,
        6.12,
        6.02,
        5.93    // 45C
        ] ;

        // convert string args...
        var tmp = Math.round(parseFloat(temperature));
        var dox = parseFloat(disolved_oxygen);
        var saturation = 0;

        if ( isNaN(tmp) || isNaN(dox) )
        {
            saturation = ''; // TODO: value error values (or blank?)
        }
        else if ( tmp < 0 || tmp >= saturation_factor.length )
        {
            saturation = '';
        }
        else
        {
            saturation = Math.round(dox / saturation_factor[tmp] * 100);
        }

        return saturation;
    }

    /*
     Perform the calculation on two form fields, storing the result in a third.
     (private)
     */
    function setSaturation(keys)
    {
        var t = $( keys.tmp ).val();
        var d = $( keys.dox ).val();
        $( keys.sat ).val( calcSaturation(t,d) );
    }

    /*
    Init the jQuery handlers.
    (private)
     */
    function setHandlers(keys)
    {
        formIds = keys;

        // make the %saturation field read-only
        $(formIds.sat).attr('readonly', 'readonly');

        // set change handler for the temperature and disolved oxygen fields
        $(formIds.tmp).change( function()
                {
                    setSaturation(formIds);
                }
        );

        $(formIds.dox).change( function()
                {
                    setSaturation(formIds);
                }
        );
    }

    /*
    Set the jQuery handlers to calc & store the saturation when the input fields change.
    (public)
    */
    OxygenSaturation.setSaturationHandlers = function(saturation_id, temperature_id, disolved_oxygen_id)
    {
        setHandlers( {
            sat : saturation_id,
            tmp : temperature_id,
            dox : disolved_oxygen_id
        } );
    }

    console.log('Init OxygenSaturation');

    /* Automatically set the handlers? */
    if (typeof OxygenSaturation.autorun === 'object')
    {
        console.log('Oxygen sat: '+OxygenSaturation.autorun.sat);
        setHandlers(OxygenSaturation.autorun);
    }

    // check to evaluate whether 'OxygenSaturation' exists in the global OxygenSaturation -
    // if not, assign window.OxygenSaturation an object literal
})(window.OxygenSaturation = window.OxygenSaturation || {});

Original issue reported on code.google.com by CoolDa...@gmail.com on 13 Feb 2014 at 12:57