Skip to content
Snippets Groups Projects
Commit 858e4295 authored by tuckersiegel's avatar tuckersiegel
Browse files

analog year update

parent 471d0c25
No related branches found
No related tags found
1 merge request!8analog year update
...@@ -13,7 +13,7 @@ function send_response(message, gdds, data, res) { ...@@ -13,7 +13,7 @@ function send_response(message, gdds, data, res) {
}) })
} }
function find(collection, query, projection, t_base, res, product) { function find(collection, query, projection, t_base, res, product, current) {
collection.findOne(query, projection).then(function(data) { collection.findOne(query, projection).then(function(data) {
var min_temps = data["min_temps"] var min_temps = data["min_temps"]
var max_temps = data["max_temps"] var max_temps = data["max_temps"]
...@@ -29,7 +29,17 @@ function find(collection, query, projection, t_base, res, product) { ...@@ -29,7 +29,17 @@ function find(collection, query, projection, t_base, res, product) {
gdd_sum += gdd_value gdd_sum += gdd_value
gdds.push(gdd_sum) gdds.push(gdd_sum)
} }
send_response("Accumulated GDDs", gdds, data, res); if (current) {
analog_year = data["analog_year"]
} else {
analog_year = -1
}
res.json({message: "Accumulated GDDs",
date: data["last_date"],
data: gdds,
analog_year: analog_year,
closest_lon: data["location"]["coordinates"][0],
closest_lat: data["location"]["coordinates"][1]});
}, function(err) { }, function(err) {
res.status(500).send({"internal error": err}) res.status(500).send({"internal error": err})
}) })
...@@ -135,7 +145,8 @@ exports.accumulated_gdd = function (req, res) { ...@@ -135,7 +145,8 @@ exports.accumulated_gdd = function (req, res) {
if (year != new Date().getFullYear()) { if (year != new Date().getFullYear()) {
find(gdd_collection, query, projection, t_base, res, product); find(gdd_collection, query, projection, t_base, res, product);
} else { } else {
find(gdd_current_collection, query, projection, t_base, res, product); projection["analog_year"] = 1;
find(gdd_current_collection, query, projection, t_base, res, product, true);
} }
}; };
......
const { response } = require('express');
gdd_collection = require('../models/gdd.js'); gdd_collection = require('../models/gdd.js');
gdd_current_collection = require('../models/gdd_current.js'); gdd_current_collection = require('../models/gdd_current.js');
utils = require('../lib/utils'); utils = require('../lib/utils');
...@@ -5,16 +7,16 @@ utils = require('../lib/utils'); ...@@ -5,16 +7,16 @@ utils = require('../lib/utils');
function send_response(message, gdds, data, res) { function send_response(message, gdds, data, res) {
res.json({ return {
message: message, message: message,
date: data["last_date"], date: data["last_date"],
data: gdds, data: gdds,
closest_lon: data["location"]["coordinates"][0], closest_lon: data["location"]["coordinates"][0],
closest_lat: data["location"]["coordinates"][1] closest_lat: data["location"]["coordinates"][1]
}) };
} }
function find(collection, query, projection, t_base, res, product) { function find(collection, query, projection, t_base, res, product, current) {
collection.findOne(query, projection).then(function(data) { collection.findOne(query, projection).then(function(data) {
var min_temps = data["min_temps"] var min_temps = data["min_temps"]
var max_temps = data["max_temps"] var max_temps = data["max_temps"]
...@@ -28,7 +30,21 @@ function find(collection, query, projection, t_base, res, product) { ...@@ -28,7 +30,21 @@ function find(collection, query, projection, t_base, res, product) {
gdds.push(gdd_value) gdds.push(gdd_value)
} }
send_response("GDDs", gdds, data, res); // response = send_response("GDDs", gdds, data, res);
// if (current) {
// response["analog_year"] = data["analog_year"];
// }
if (current) {
analog_year = data["analog_year"]
} else {
analog_year = -1
}
res.json({message: "GDDs",
date: data["last_date"],
data: gdds,
analog_year: analog_year,
closest_lon: data["location"]["coordinates"][0],
closest_lat: data["location"]["coordinates"][1]});
}, function(err) { }, function(err) {
res.status(500).send({"internal error": err}) res.status(500).send({"internal error": err})
}) })
...@@ -104,7 +120,8 @@ exports.year_gdd = function (req, res) { ...@@ -104,7 +120,8 @@ exports.year_gdd = function (req, res) {
if (year != new Date().getFullYear()) { if (year != new Date().getFullYear()) {
find(gdd_collection, query, projection, t_base, res, product); find(gdd_collection, query, projection, t_base, res, product);
} else { } else {
find(gdd_current_collection, query, projection, t_base, res, product); projection["analog_year"] = 1;
find(gdd_current_collection, query, projection, t_base, res, product, true);
} }
}; };
\ No newline at end of file
...@@ -20,6 +20,10 @@ var gddCurrentSchema = mongoose.Schema({ ...@@ -20,6 +20,10 @@ var gddCurrentSchema = mongoose.Schema({
type: Number, type: Number,
required: true required: true
}, },
analog_year: {
type: Number,
required: true
},
last_date: { last_date: {
type: Date, type: Date,
default: Date.now default: Date.now
......
...@@ -28,6 +28,9 @@ gdd.drop() ...@@ -28,6 +28,9 @@ gdd.drop()
gdd = db["gdd_current"] gdd = db["gdd_current"]
gdd_base = db["gdd"]
resp = gdd.create_index([ ("location", "2dsphere") ]) resp = gdd.create_index([ ("location", "2dsphere") ])
resp = gdd.create_index([ ("year", 1) ]) resp = gdd.create_index([ ("year", 1) ])
...@@ -136,6 +139,38 @@ for i in tqdm.tqdm(x): ...@@ -136,6 +139,38 @@ for i in tqdm.tqdm(x):
lat_ = lat[i[0]] lat_ = lat[i[0]]
lon_ = lon[i[1]] lon_ = lon[i[1]]
query = {
"location": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [float(lon_), float(lat_)]
},
},
},
}
proj = {
"min_temps": 1,
"max_temps": 1,
"year": 1
}
min_err = 10000000
analog_year = 1981
yearly_min_data = np.zeros((year - 1981, len(tmin_)))
yearly_max_data = np.zeros((year - 1981, len(tmin_)))
for loc_data in gdd_base.find(query, proj).limit(year - 1981):
yearly_min_data[loc_data["year"] - 1981] = np.array(loc_data["min_temps"][:len(tmin_)])
yearly_max_data[loc_data["year"] - 1981] = np.array(loc_data["max_temps"][:len(tmin_)])
l2_dist_min = np.mean((yearly_min_data - tmin_) ** 2, axis=1)
l2_dist_max = np.mean((yearly_max_data - tmax_) ** 2, axis=1)
l2_dist = (l2_dist_max + l2_dist_min) / 2
analog_year = np.argmin(l2_dist) + 1981
a = i a = i
t = {} t = {}
...@@ -147,6 +182,7 @@ for i in tqdm.tqdm(x): ...@@ -147,6 +182,7 @@ for i in tqdm.tqdm(x):
t["location"] = {"type": "Point", "coordinates": [float(lon_), float(lat_)]} t["location"] = {"type": "Point", "coordinates": [float(lon_), float(lat_)]}
t["prism_lat"] = int(a[0]) t["prism_lat"] = int(a[0])
t["prism_lon"] = int(a[1]) t["prism_lon"] = int(a[1])
t["analog_year"] = int(analog_year)
t["last_date"] = datetime.datetime.strptime(str(soy + np.timedelta64(len(tmin_) - 1, "D")) , "%Y-%m-%d") t["last_date"] = datetime.datetime.strptime(str(soy + np.timedelta64(len(tmin_) - 1, "D")) , "%Y-%m-%d")
t["year"] = int(year) t["year"] = int(year)
......
...@@ -78,6 +78,11 @@ paths: ...@@ -78,6 +78,11 @@ paths:
minimum: -125.0 minimum: -125.0
maximum: -66.5 maximum: -66.5
example: -76.94 example: -76.94
analog_year:
type: number
minimum: -1
maximum: 2020
example: 2005
400: 400:
description: Bad Request description: Bad Request
content: content:
...@@ -337,6 +342,11 @@ paths: ...@@ -337,6 +342,11 @@ paths:
minimum: -125.0 minimum: -125.0
maximum: -66.5 maximum: -66.5
example: -76.94 example: -76.94
analog_year:
type: number
minimum: -1
maximum: 2020
example: 2005
400: 400:
description: Bad Request description: Bad Request
content: content:
...@@ -425,6 +435,7 @@ paths: ...@@ -425,6 +435,7 @@ paths:
minimum: -125.0 minimum: -125.0
maximum: -66.5 maximum: -66.5
example: -76.94 example: -76.94
400: 400:
description: Bad Request description: Bad Request
content: content:
......
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