CO2 chamber protocol macro


Works with in situ Soil CarbonspeQ
/**
 * Macro for data evaluation on PhotosynQ.org
 * by: David M. Kramer
 * created: 2017-05-12 @ 11:07:27

The macro converts a stream of environmental data sets obtained using the _protocol_set_ feature
into arrays and displays them.

For example, the following protocol will take temperature_humidity_pressure2, and light_intensity
once per second for 20 times.
[{ "_protocol_set_":
[
        {
                "environmental": [
                ["temperature_humidity_pressure2"],
         ["light_intensity"]
                ],
      "protocols":20,
      "protocols_delay":1000
        }
]
 }]

The macro will generate one array for each sensor, i.,e. temperature2, humidity2 and PAR,
as well as time. The time array is not displayed unless you un-comment the following line:

  //output.timescale = output.time;

Note: It is important to exclude data sets that are arrays, especially data_raw. This is done
by adding the "data_raw"] to the list exclude_these:

var exclude_these=["data_raw"];

The macro can be modified to exclude the output and display of other the paramters by adding
the names to the list called exclude_these. For example, I do not want to see "data_raw", "r",
"g", "b", "light_intensity_raw"

var exclude_these=["data_raw", "r", "g", "b", "light_intensity_raw"];

*/


// Define the output object here
var output = {};

// Check if the key time exists in json
if (json.time !== undefined){

    //add values to exclude from the analyses
    var exclude_these=["data_raw", "r", "g", "b", "light_intensity_raw"];

        keys = Object.keys(json.set[2]); //find the keys to the sensors
    Object.keys(json.set[2]).join(","); //join them in a string for display
    number_sensors=keys.length;  // find the number of sensors
    output.number_sensors=number_sensors; //display the number of sensors

    // Add key time and value to output
    number_measurements=json.set.length;
    output.number_measurements=number_measurements;

   // var number_sensors = json.set[1]
  for (var j=0; j< number_sensors; j++){

    use_this_key=keys[j];
    if (exclude_these.indexOf(use_this_key)<0){
    temp=[];
   var i;
    for (i=1; i < number_measurements; i++){
      var t=json.set[i][use_this_key];
      temp.push(t);
    }
    output[use_this_key]=temp;
    }
  }
  //output.timescale = output.time;
}


var analog_read = MathMEDIAN(output.analog_read);
var temperature = MathMEDIAN(output.temperature);
var humidity = MathMEDIAN(output.humidity);
var pressure = MathMEDIAN(output.pressure);
//var VWC = ((analog_read * analog_read * 0.0000000000742) + (0.00000313 * analog_read) + 0.1157) * 100;
var Volume = .000614;
var Surface_area = .012272;
var Pressure_constant = 100;
var Rconstant = 8.31441;
var Cmoleweight = 12.01;
var Kelvin = 273;
var Conversion_factor1 = .000001;
var Conversion_factor2 = 1000;
var Conversion_factor3 = 864;
var Conversion_factor4 = 3600;
var mgCm3Array = [];
var carbonOutputArray = [];

output ["temp"] = output.temperature;


var co2 = [];
for(i in output.co2){
	if(output.co2[i] > 10000)
      co2.push(null);
   else
     co2.push(output.co2[i]);
}

output.co2 = co2;

for (var i = 0; i < co2.length; i++) {
  if(co2[i] === null)
    mgCm3Array.push(null);
 else
  mgCm3Array.push((((output.pressure[i] * Pressure_constant) * Cmoleweight * co2[i] * Conversion_factor1) / (Rconstant * (output.temperature[i] + Kelvin))) * Conversion_factor2);
}
/*
for (var i = 0; i < co2.length; i++) {
  if(co2[i] === null)
    carbonOutputArray.push(null);
 else
  carbonOutputArray.push(((mgCm3Array[i] * Volume) / Surface_area) * Conversion_factor3);
}
*/
for (var i = 0; i < co2.length; i++) {
  if(co2[i] === null)
    carbonOutputArray.push(null);
 else
  carbonOutputArray.push(((mgCm3Array[i] * Volume) / Surface_area) * Conversion_factor4);
}


carbonOutputArray = carbonOutputArray.slice(6);

var xArray = [];
var yArray = [];
for (var i = 0; i < carbonOutputArray.length; i++){
  if(carbonOutputArray[i] !== null){
  	xArray.push(i * 5);
    yArray.push(carbonOutputArray[i]);
  }
}
                         
var Carbon_output = MathLINREG(xArray, yArray);
output["mg C m-2 hour-1"] = Carbon_output.m;
output["carbonOutputB"] = Carbon_output.b;
output["carbonOutputR2"] = Carbon_output.r2;


output["mgCm3Array"] = mgCm3Array;
output["Carbon Output"] = carbonOutputArray;
//output["analog_read"] = (analog_read);
//output["VWC"] = VWC;
output["temperature"] = temperature;
output["humidity"] = humidity;
output["pressure"] = pressure;

// Return data
return output;
{
  "time": "1495040914960",
  "device_name": "MultispeQ",
  "device_version": "1",
  "device_id": "01:17:13:10",
  "device_battery": -277,
  "device_firmware": 1.18,
  "sample": [
    {
      "time": "1495040914968",
      "light_intensity_raw": 0,
      "data_raw": [

      ],
      "protocol_id": 1,
      "set": [
        {
          "time": "1495040914968",
          "blank:": "blank"
        },
        {
          "time": "1495040914978",
          "temperature": 26.18,
          "humidity": 47.555664,
          "pressure": 977.992371,
          "co2": 1551,
          "analog_read": 252,
          "data_raw": [

          ]
        },
        {
          "time": "1495040920135",
          "temperature": 26.190001,
          "humidity": 47.478516,
          "pressure": 978.030396,
          "co2": 1521,
          "analog_read": 245,
          "data_raw": [

          ]
        },
        {
          "time": "1495040925351",
          "temperature": 26.200001,
          "humidity": 47.368164,
          "pressure": 978.001709,
          "co2": 1476,
          "analog_read": 244,
          "data_raw": [

          ]
        },
        {
          "time": "1495040930521",
          "temperature": 26.200001,
          "humidity": 47.246094,
          "pressure": 977.983582,
          "co2": 1422,
          "analog_read": 246,
          "data_raw": [

          ]
        },
        {
          "time": "1495040935681",
          "temperature": 26.219999,
          "humidity": 47.180664,
          "pressure": 978.017334,
          "co2": 1397,
          "analog_read": 244,
          "data_raw": [

          ]
        },
        {
          "time": "1495040940856",
          "temperature": 26.23,
          "humidity": 47.103516,
          "pressure": 978.028137,
          "co2": 1351,
          "analog_read": 244,
          "data_raw": [

          ]
        },
        {
          "time": "1495040946013",
          "temperature": 26.23,
          "humidity": 47.003906,
          "pressure": 978.025879,
          "co2": 1329,
          "analog_read": 254,
          "data_raw": [

          ]
        },
        {
          "time": "1495040951177",
          "temperature": 26.24,
          "humidity": 46.893555,
          "pressure": 978.039978,
          "co2": 1281,
          "analog_read": 241,
          "data_raw": [

          ]
        },
        {
          "time": "1495040956343",
          "temperature": 26.25,
          "humidity": 46.727539,
          "pressure": 978.045776,
          "co2": 1254,
          "analog_read": 241,
          "data_raw": [

          ]
        },
        {
          "time": "1495040961507",
          "temperature": 26.27,
          "humidity": 49.448242,
          "pressure": 978.050598,
          "co2": 1326,
          "analog_read": 267,
          "data_raw": [

          ]
        },
        {
          "time": "1495040966672",
          "temperature": 26.290001,
          "humidity": 52.724609,
          "pressure": 978.027161,
          "co2": 1789,
          "analog_read": 237,
          "data_raw": [

          ]
        },
        {
          "time": "1495040971839",
          "temperature": 26.309999,
          "humidity": 53.282227,
          "pressure": 978.040771,
          "co2": 2231,
          "analog_read": 236,
          "data_raw": [

          ]
        },
        {
          "time": "1495040977003",
          "temperature": 26.32,
          "humidity": 53.339844,
          "pressure": 978.053223,
          "co2": 2838,
          "analog_read": 236,
          "data_raw": [

          ]
        },
        {
          "time": "1495040982167",
          "temperature": 26.33,
          "humidity": 53.328125,
          "pressure": 978.037476,
          "co2": 3201,
          "analog_read": 238,
          "data_raw": [

          ]
        },
        {
          "time": "1495040987333",
          "temperature": 26.34,
          "humidity": 53.297852,
          "pressure": 978.029053,
          "co2": 3593,
          "analog_read": 235,
          "data_raw": [

          ]
        },
        {
          "time": "1495040992498",
          "temperature": 26.35,
          "humidity": 53.219727,
          "pressure": 978.017639,
          "co2": 3892,
          "analog_read": 239,
          "data_raw": [

          ]
        },
        {
          "time": "1495040997663",
          "temperature": 26.360001,
          "humidity": 53.009766,
          "pressure": 978.036377,
          "co2": 4020,
          "analog_read": 238,
          "data_raw": [

          ]
        },
        {
          "time": "1495041002829",
          "temperature": 26.360001,
          "humidity": 52.753906,
          "pressure": 978.014893,
          "co2": 4129,
          "analog_read": 239,
          "data_raw": [

          ]
        },
        {
          "time": "1495041007994",
          "temperature": 26.370001,
          "humidity": 52.532227,
          "pressure": 977.991882,
          "co2": 4140,
          "analog_read": 237,
          "data_raw": [

          ]
        },
        {
          "time": "1495041013159",
          "temperature": 26.379999,
          "humidity": 52.311523,
          "pressure": 977.998596,
          "co2": 4155,
          "analog_read": 242,
          "data_raw": [

          ]
        },
        {
          "time": "1495041018324",
          "temperature": 26.379999,
          "humidity": 52.078125,
          "pressure": 977.998352,
          "co2": 4147,
          "analog_read": 233,
          "data_raw": [

          ]
        },
        {
          "time": "1495041023490",
          "temperature": 26.389999,
          "humidity": 51.87793,
          "pressure": 978.052185,
          "co2": 4099,
          "analog_read": 239,
          "data_raw": [

          ]
        },
        {
          "time": "1495041028656",
          "temperature": 26.4,
          "humidity": 51.734375,
          "pressure": 978.016846,
          "co2": 4049,
          "analog_read": 231,
          "data_raw": [

          ]
        },
        {
          "time": "1495041033819",
          "temperature": 26.4,
          "humidity": 51.59082,
          "pressure": 978.036865,
          "co2": 4007,
          "analog_read": 239,
          "data_raw": [

          ]
        }
      ]
    }
  ],
  "app_os": "win32-ia32",
  "app_name": "PhotosynQ",
  "app_version": "0.0.9",
  "app_device": "ia32",
  "location": [
    "42.7225742",
    "-84.4746871"
  ],
  "time_offset": "America/New_York",
  "ConsoleMacro": "335"
}
Sam 0645   copy   copy  2
Created by

Dan TerAvest


Protocol connections:
1
Latest Update:
Jul 2017