Skip to content
Snippets Groups Projects
Commit 50dd5b39 authored by Tucker Gary Siegel's avatar Tucker Gary Siegel
Browse files

Merge branch 'gefs' into 'master'

GEFS

See merge request !7
parents c9272f72 471d0c25
No related branches found
No related tags found
1 merge request!7GEFS
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
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");
......@@ -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)
......
......@@ -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',
......
......@@ -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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment