diff --git a/controllers/gddAccumulatedController.js b/controllers/gddAccumulatedController.js index cb2f9055e989febe28a24fd1cda01a412217c438..2d0a48afe1985fbd6623e4be3c7ae54912642d51 100644 --- a/controllers/gddAccumulatedController.js +++ b/controllers/gddAccumulatedController.js @@ -13,7 +13,7 @@ function send_response(message, gdds, data, res) { }) } -function find(collection, query, projection, temps, res) { +function find(collection, query, projection, temps, res, product) { collection.findOne(query, projection).then(function(data) { var min_temps = data["min_temps"] var max_temps = data["max_temps"] @@ -27,7 +27,7 @@ function find(collection, query, projection, temps, res) { var gdd_sum = 0; for (var i = 0; i < min_temps.length; i++) { - gdd_value = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base); + gdd_value = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base, product); gdd_sum += gdd_value gdds.push(gdd_sum) } @@ -117,9 +117,9 @@ exports.accumulated_gdd = function (req, res) { } if (year != new Date().getFullYear()) { - find(gdd_collection, query, projection, temps, res); + find(gdd_collection, query, projection, temps, res, product); } else { - find(gdd_current_collection, query, projection, temps, res); + find(gdd_current_collection, query, projection, temps, res, product); } }; @@ -194,5 +194,5 @@ exports.accumulated_normal_gdd = function (req, res) { t_min: t_min, } - find(gdd_normal_collection, query, projection, temps, res); + find(gdd_normal_collection, query, projection, temps, res, product); }; \ No newline at end of file diff --git a/controllers/gddController.js b/controllers/gddController.js index 86a8c356d117458c12cc3143aca2463a7f492902..c1b8adacc7f0ac1ed037532d9f63146058626b49 100644 --- a/controllers/gddController.js +++ b/controllers/gddController.js @@ -14,7 +14,7 @@ function send_response(message, gdds, data, res) { }) } -function find(collection, query, projection, temps, res) { +function find(collection, query, projection, temps, res, product) { collection.findOne(query, projection).then(function(data) { var min_temps = data["min_temps"] var max_temps = data["max_temps"] @@ -26,7 +26,7 @@ function find(collection, query, projection, temps, res) { var max_temp = 0 for (var i = 0; i < min_temps.length; i++) { - gdd_value = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base); + gdd_value = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base, product); gdds.push(gdd_value) } send_response("GDDs", gdds, data, res); @@ -114,9 +114,9 @@ exports.year_gdd = function (req, res) { } if (year != new Date().getFullYear()) { - find(gdd_collection, query, projection, temps, res); + find(gdd_collection, query, projection, temps, res, product); } else { - find(gdd_current_collection, query, projection, temps, res); + find(gdd_current_collection, query, projection, temps, res, product); } }; \ No newline at end of file diff --git a/controllers/gddNormalController.js b/controllers/gddNormalController.js index 52cd6b9c23e6ab40629acc3763178ef615a18667..b53a015d9d140b9586f86e93cb63d48d5f1d1e01 100644 --- a/controllers/gddNormalController.js +++ b/controllers/gddNormalController.js @@ -1,7 +1,7 @@ normals_collection = require('../models/normals.js'); utils = require('../lib/utils'); -function find(collection, query, projection, temps, res) { +function find(collection, query, projection, temps, res, product) { collection.findOne(query, projection).then(function(data) { var min_temps = data["min_temps"] var max_temps = data["max_temps"] @@ -13,7 +13,7 @@ function find(collection, query, projection, temps, res) { var max_temp = 0 for (var i = 0; i < min_temps.length; i++) { - gdd_value = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base); + gdd_value = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base, product); gdds.push(gdd_value) } @@ -98,5 +98,5 @@ exports.normal = function (req, res) { t_min: t_min, } - find(normals_collection, query, projection, temps, res); + find(normals_collection, query, projection, temps, res, product); }; \ No newline at end of file diff --git a/lib/utils.js b/lib/utils.js index b85d52052a954224810fb6598ede38cc123bd877..41023ea2946dfb39faaefa3d715ebc028025a59e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +1,4 @@ +const gdd = require("../models/gdd"); function product_base_switch(product, errors, t_base, t_min) { switch (product) { @@ -54,11 +55,17 @@ function product_base_switch(product, errors, t_base, t_min) { } -function calculate_gdd(min_temp, t_min, max_temp, t_max, t_base) { - min_temp = min_temp >= t_min ? min_temp : t_min; - max_temp = max_temp >= t_min ? max_temp : t_min; - max_temp = max_temp <= t_max ? max_temp : t_max; - gdd = ((max_temp + min_temp) / 2) - t_base; +function calculate_gdd(min_temp, t_min, max_temp, t_max, t_base, product) { + var gdd = 0; + if (product == "corn") { + min_temp = min_temp >= t_min ? min_temp : t_min; + max_temp = max_temp >= t_min ? max_temp : t_min; + max_temp = max_temp <= t_max ? max_temp : t_max; + gdd = ((max_temp + min_temp) / 2) - t_base; + } else { + gdd = ((max_temp + min_temp) / 2) - t_base; + gdd = gdd < 0 ? 0 : gdd; + } return gdd }