diff --git a/controllers/gefsController.js b/controllers/gefsController.js new file mode 100644 index 0000000000000000000000000000000000000000..03803615223a49129c18d511309256e14733c3d4 --- /dev/null +++ b/controllers/gefsController.js @@ -0,0 +1,101 @@ +gefsCoords = require('../models/coordinates.js'); +current_year = require('../models/gdd_current.js'); +utils = require('../lib/utils'); + +function find(collection, query, projection, t_base, res, product) { + current_year.find(query).limit(1).then(function(cy) { + var min_temps = cy[0]["min_temps"] + var max_temps = cy[0]["max_temps"] + + var gdd_sum_cy = 0; + + for (var i = 0; i < min_temps.length; i++) { + gdd_value = utils.calculate_gdd(min_temps[i], max_temps[i], t_base, product); + gdd_sum_cy += gdd_value + } + + collection.find(query).limit(10).then(function(data) { + // console.log(data); + var base_date = data[0]["time"] + var gdds = [] + for (var i = 0; i < 10; i++) { + gdd_value = utils.calculate_gdd(data[i].min_temp, data[i].max_temp, t_base, product); + gdd_sum_cy += gdd_value + gdds.push(gdd_sum_cy) + } + + res.json({ + message: "GEFS GDD", + data: gdds, + base_date: base_date, + closest_lon: data[0]["location"]["coordinates"][0], + closest_lat: data[0]["location"]["coordinates"][1] + }) + }, function(err) { + res.status(500).send({"internal error": err}) + }) + + }, function(err) { + res.status(500).send({"internal error": err}) + }) + +} + + +exports.gefs = 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 projection = { + min_temp: 1, + max_temp: 1, + location: 1, + } + + var t_base = 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); + } else { + out = utils.product_base_switch(product, errors, t_base); + t_base = out.t_base; + 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 + }) + } + + find(gefsCoords, query, projection, t_base, res, product); +}; \ No newline at end of file diff --git a/models/coordinates.js b/models/coordinates.js new file mode 100644 index 0000000000000000000000000000000000000000..6aaa2ed292f578816b1fb606d3710b71a27e4a18 --- /dev/null +++ b/models/coordinates.js @@ -0,0 +1,30 @@ +var mongoose = require('mongoose'); +//schema +var coordinatesSchema = mongoose.Schema({ + id_: { + type: String, + required: true + }, + location: { + type: { + type: String, + enum: ['Point', 'Polygon'] + }, + coordinates: [Number], + }, + max_temp: { + type: Number, + required: true + }, + min_temp: { + type: Number, + required: true + }, + time: { + type: Date, + default: Date.now + }, +}); + +var Model = module.exports = mongoose.model('gefs', coordinatesSchema, "gefs"); + diff --git a/routes.js b/routes.js index 892577d37b0ce350cf8345272127be0afe1f7c40..1d5d4495146ab68de3d4d982ae7c6efed4d3b964 100644 --- a/routes.js +++ b/routes.js @@ -4,9 +4,11 @@ const controller = require("./controllers/gddController") const normController = require("./controllers/gddNormalController") const freezingDatesController = require("./controllers/freezingDatesController") const confidenceIntervalController = require("./controllers/gddConfidenceInterval") +const gefsController = require("./controllers/gefsController") router.route("/:product/daily/:year/accumulated").post(accController.accumulated_gdd) router.route("/:product/daily/:year").post(controller.year_gdd) +router.route("/:product/gefs/accumulated").post(gefsController.gefs) router.route("/:product/normal").post(normController.normal) router.route("/:product/confidence").post(confidenceIntervalController.confidence_interval) router.route("/:product/normal/accumulated").post(accController.accumulated_normal_gdd) diff --git a/server.js b/server.js index ddd88ef9119ff6a835552e3c3f126c13a0193591..f3b651f7a64a02dcd2a72d6ddb098f4b30a7a2a1 100644 --- a/server.js +++ b/server.js @@ -21,9 +21,9 @@ app.use(cors()); app.use('/api', apiRoutes); -const dbPath = "mongodb+srv://gdd-server:u8i3icLAJXjZEhTs@cluster0.wdxf4.mongodb.net/gdd_database"; +const Path = "mongodb+srv://gdd-server:u8i3icLAJXjZEhTs@cluster0.wdxf4.mongodb.net/gdd_database"; const options = {useNewUrlParser: true, useUnifiedTopology: true} -const mongo = mongoose.connect(dbPath, options); +const mongo = mongoose.connect(Path, options); const swaggerDefinition = { openapi: '3.0.0', diff --git a/swagger_definition.yaml b/swagger_definition.yaml index 3b28fc2d3b4889dc02c2752270857c016ee7f6b3..8bcc877a98f113baf60e870d24aeac2f8fa07a66 100644 --- a/swagger_definition.yaml +++ b/swagger_definition.yaml @@ -532,4 +532,92 @@ paths: example: latitude message: type: string - example: 22.5 is out of bounds for freezing date calculations. Must be between 24.083334 - 49.916668 \ No newline at end of file + example: 22.5 is out of bounds for freezing date calculations. Must be between 24.083334 - 49.916668 + /api/{product}/gefs/accumulated: + post: + summary: Returns GEFS forecast GDD data + description: Returns accumulated GEFS forecast GDD data for a specific product, lat, and lon + produces: + - application/json + consumes: + - application/json + parameters: + - in: path + name: product + required: true + description: Agricultural product to calculate gdd for + schema: + type: string + enum: [corn, cotton, oat, peanut, pea, potato, rice, soybean, sorghum, spring_wheat, sugar_beet, sunflower, tomato, wheat] + 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: GEFS GDD + base_date: + type: string + format: date + 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: -76.94 + 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 \ No newline at end of file diff --git a/test_server.py b/test_server.py index 9283d12b11151a2a4db83b906da8e2cc415fc6a6..e2919ad89d497cd8c1240d6f373753cf02ba9912 100644 --- a/test_server.py +++ b/test_server.py @@ -20,37 +20,37 @@ t = time.time() # print (r.json()) # print (time.time() - t) # print () -# r = requests.post("http://localhost:4000/api/soybean/normal", data=data) -# print (r.status_code) -# print (r.json()) -# print (time.time() - t) -# print () -r = requests.post("http://localhost:4000/api/soybean/daily/2021", data=data) +r = requests.post("http://localhost:4000/api/soybean/normal", data=data) print (r.status_code) print (r.json()) print (time.time() - t) -r = requests.post("http://localhost:4000/api/freezing/28.2", data=data) -print (r.status_code) -print (r.json()) -r = requests.post("http://localhost:4000/api/corn/confidence", data=data) +print () +r = requests.post("http://localhost:4000/api/soybean/gefs", data=data) print (r.status_code) print (r.json()) +print (time.time() - t) +# r = requests.post("http://localhost:4000/api/freezing/28.2", data=data) +# print (r.status_code) +# print (r.json()) +# r = requests.post("http://localhost:4000/api/corn/confidence", data=data) +# print (r.status_code) +# print (r.json()) -min_gdds = r.json()["minimum"] -max_gdds = r.json()["maximum"] +# min_gdds = r.json()["minimum"] +# max_gdds = r.json()["maximum"] -r = requests.post("http://localhost:4000/api/corn/normal/accumulated", data=data) -print (r.status_code) -print (r.json()) +# r = requests.post("http://localhost:4000/api/corn/normal/accumulated", data=data) +# print (r.status_code) +# print (r.json()) -normals = r.json()["data"] +# normals = r.json()["data"] -# max_gdds = np.array(max_gdds) + np.array(normals) -# max_gdds /= 2 -# min_gdds = np.array(min_gdds) + np.array(normals) -# min_gdds /= 2 +# # max_gdds = np.array(max_gdds) + np.array(normals) +# # max_gdds /= 2 +# # min_gdds = np.array(min_gdds) + np.array(normals) +# # min_gdds /= 2 -plt.plot(list(range(len(normals))), normals, color="black") -plt.plot(list(range(len(normals))), max_gdds, color="orange") -plt.plot(list(range(len(normals))), min_gdds, color="blue") -plt.savefig("test.png", dpi=600) \ No newline at end of file +# plt.plot(list(range(len(normals))), normals, color="black") +# plt.plot(list(range(len(normals))), max_gdds, color="orange") +# plt.plot(list(range(len(normals))), min_gdds, color="blue") +# plt.savefig("test.png", dpi=600) \ No newline at end of file