diff --git a/controllers/gddAccumulatedController.js b/controllers/gddAccumulatedController.js index d9fb70738eea6a878f6533975b28c75840d28597..1a733bd93c03e044391c6e3f48417994a507e302 100644 --- a/controllers/gddAccumulatedController.js +++ b/controllers/gddAccumulatedController.js @@ -1,5 +1,6 @@ gdd = require('../models/gdd.js'); gdd_current = require('../models/gdd_current.js'); +gdd_normal = require('../models/normals.js'); utils = require('../lib/utils'); function send_response(message, gdds, data, res) { @@ -122,4 +123,77 @@ exports.accumulated_gdd = function (req, res) { find(gdd_current, query, projection, temps, res); } +}; + + +exports.accumulated_normal_gdd = function (req, res) { + + var product = req.params.product; + + var latitude = parseFloat(req.body.latitude) + var longitude = parseFloat(req.body.longitude) + + var query = { + location: { + "$near": { + "$geometry": { + "type": "Point", + "coordinates": [longitude, latitude] + }, + }, + }, + } + + var t_base = 50 + var t_max = 86 + var t_min = 50 + + errors = [] + + if (latitude < 24.083334 || latitude > 49.916668) { + errors.push({ + parameter_error: "latitude", + message: latitude.toString() + " is out of bounds for GDD calculations. Must be between 24.083334 - 49.916668" + }); + } + + if (req.body.hasOwnProperty("t_base")) { + t_base = parseFloat(req.body.t_base); + if (t_base < t_min) { + t_min = t_base; + } + } else { + out = utils.product_base_switch(product, errors); + t_base = out.t_base; + t_min = out.t_min; + errors = out.errors; + } + + + if (longitude < -125 || longitude > -66.5) { + errors.push({ + parameter_error: "longitude", + message: longitude.toString() + " is out of bounds for GDD calculations. Must be between -125.0 - -66.5" + }); + } + + if (errors.length > 0) { + res.status(400).send({ + errors: errors + }) + } + + var projection = { + min_temps: 1, + max_temps: 1, + location: 1, + } + + temps = { + t_base: t_base, + t_max: t_max, + t_min: t_min, + } + + find(gdd_normal, query, projection, temps, res); }; \ No newline at end of file diff --git a/routes/api/product/accumulated/accumulated_year.js b/routes/api/product/accumulated/accumulated_year.js deleted file mode 100644 index 42830e8b7566c26c53d6ac45663756e221a970b6..0000000000000000000000000000000000000000 --- a/routes/api/product/accumulated/accumulated_year.js +++ /dev/null @@ -1,6 +0,0 @@ -const router = require('express').Router(); -const controller = require("../../../../controllers/gddAccumulatedController") - -router.route("/:year").post(controller.accumulated_gdd) - -module.exports = router; diff --git a/routes/api/product/daily/daily_year.js b/routes/api/product/daily/daily_routes.js similarity index 55% rename from routes/api/product/daily/daily_year.js rename to routes/api/product/daily/daily_routes.js index 4b74c662b44bff177e232ff21072e3f0c325a50a..2c2e85fbc59f602653049eb7aeea4c89ee35e8cf 100644 --- a/routes/api/product/daily/daily_year.js +++ b/routes/api/product/daily/daily_routes.js @@ -1,6 +1,8 @@ const router = require('express').Router(); +const accController = require("../../../../controllers/gddAccumulatedController") const controller = require("../../../../controllers/gddController") +router.route("/:year/accumulated").post(accController.accumulated_gdd) router.route("/:year").post(controller.year_gdd) module.exports = router; diff --git a/routes/api/product/normal/normal.js b/routes/api/product/normal/normal_routes.js similarity index 51% rename from routes/api/product/normal/normal.js rename to routes/api/product/normal/normal_routes.js index 302bccc3054a762183e3dba57351fbef915a6741..9f4736198bd18513cf33cf43482ae49df3d3e77f 100644 --- a/routes/api/product/normal/normal.js +++ b/routes/api/product/normal/normal_routes.js @@ -1,6 +1,8 @@ const router = require('express').Router(); -const controller = require("../../../../controllers/gddNormalController") +const controller = require("../../../../controllers/gddNormalController"); +const accController = require("../../../../controllers/gddAccumulatedController"); router.route("/").post(controller.normal) +router.route("/accumulated/").post(accController.accumulated_normal_gdd) module.exports = router; diff --git a/routes/api/product/product_index.js b/routes/api/product/product_index.js index d669452d4cc259eb1f67a6d82bdaa843c3ca35ad..05b1cc6d03f58250970efcb97ff97e25d98c94f9 100644 --- a/routes/api/product/product_index.js +++ b/routes/api/product/product_index.js @@ -1,7 +1,6 @@ const router = require('express').Router(); -router.use('/normal', require('./normal/normal')); -router.use('/daily', require('./daily/daily_year')); -router.use('/accumulated', require('./accumulated/accumulated_year')); +router.use('/normal', require('./normal/normal_routes')); +router.use('/daily', require('./daily/daily_routes')); module.exports = router; diff --git a/swagger_definition.yaml b/swagger_definition.yaml index b9113e5504f3b35c4a0c2efb25b3afe48b9bfe56..71b8d7d6f15037338639ad14ed7d4060d49f8ed9 100644 --- a/swagger_definition.yaml +++ b/swagger_definition.yaml @@ -177,7 +177,88 @@ paths: message: type: string example: 22.5 is out of bounds for GDD calculations. Must be between 24.083334 - 49.916668 - /api/{product}/accumulated/{year}: + /api/{product}/normal/accumulated: + post: + summary: Returns accumulated GDD data on a 30 year normal + description: Returns accumulated GDD normals for a specific lat, and lon + parameters: + - in: path + name: product + required: true + description: Agricultural product to calculate gdd for + schema: + type: string + enum: [corn, soybean, sugar_beet, sunflower, tomato, potato, wheat, peas, parsley, brussels_sprouts, cabbage] + requestBody: + content: + application/json: + schema: + type: object + required: + - longitude + - latitude + properties: + latitude: + description: latitude to calculate gdd on + type: number + minimum: 24.083334 + maximum: 49.916668 + example: 38.99 + longitude: + description: longitude to calculate gdd on + type: number + minimum: -125.0 + maximum: -66.5 + example: -76.94 + t_base: + description: Base temperature to calculate gdd on, in fahrenheit. NOT REQUIRED + type: number + example: 50 + + responses: + 200: + description: Success + content: + application/json: + schema: + type: object + properties: + message: + type: string + example: 30-year normal GDDs + data: + type: array + items: + type: number + closest_lat: + type: number + minimum: 24.083334 + maximum: 49.916668 + example: 38.99 + closest_lon: + type: number + minimum: -125.0 + maximum: -66.5 + example: -78.5 + 400: + description: Bad Request + content: + application/json: + schema: + type: object + properties: + errors: + type: array + items: + type: object + properties: + parameter_error: + type: string + example: latitude + message: + type: string + example: 22.5 is out of bounds for GDD calculations. Must be between 24.083334 - 49.916668 + /api/{product}/daily/{year}/accumulated: post: summary: Returns accumulated GDD data description: Returns accumulated GDD data for a specific product, year, lat, and lon