From 858e4295a796716a5cfae4d1bf5affef9a3512a8 Mon Sep 17 00:00:00 2001 From: tuckersiegel <siegel.tucker@gmail.com> Date: Wed, 12 May 2021 12:28:10 -0400 Subject: [PATCH] analog year update --- controllers/gddAccumulatedController.js | 17 +++++++++--- controllers/gddController.js | 27 +++++++++++++++---- models/gdd_current.js | 4 +++ pull_data.py | 36 +++++++++++++++++++++++++ swagger_definition.yaml | 11 ++++++++ 5 files changed, 87 insertions(+), 8 deletions(-) diff --git a/controllers/gddAccumulatedController.js b/controllers/gddAccumulatedController.js index 09e0c7b..222f882 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, t_base, res, product) { +function find(collection, query, projection, t_base, res, product, current) { collection.findOne(query, projection).then(function(data) { var min_temps = data["min_temps"] var max_temps = data["max_temps"] @@ -29,7 +29,17 @@ function find(collection, query, projection, t_base, res, product) { gdd_sum += gdd_value 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) { res.status(500).send({"internal error": err}) }) @@ -135,7 +145,8 @@ exports.accumulated_gdd = function (req, res) { if (year != new Date().getFullYear()) { find(gdd_collection, query, projection, t_base, res, product); } 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); } }; diff --git a/controllers/gddController.js b/controllers/gddController.js index 66a4059..b37655f 100644 --- a/controllers/gddController.js +++ b/controllers/gddController.js @@ -1,3 +1,5 @@ +const { response } = require('express'); + gdd_collection = require('../models/gdd.js'); gdd_current_collection = require('../models/gdd_current.js'); utils = require('../lib/utils'); @@ -5,16 +7,16 @@ utils = require('../lib/utils'); function send_response(message, gdds, data, res) { - res.json({ + return { message: message, date: data["last_date"], data: gdds, closest_lon: data["location"]["coordinates"][0], 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) { var min_temps = data["min_temps"] var max_temps = data["max_temps"] @@ -28,7 +30,21 @@ function find(collection, query, projection, t_base, res, product) { 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) { res.status(500).send({"internal error": err}) }) @@ -104,7 +120,8 @@ exports.year_gdd = function (req, res) { if (year != new Date().getFullYear()) { find(gdd_collection, query, projection, t_base, res, product); } 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 diff --git a/models/gdd_current.js b/models/gdd_current.js index 7658b97..62edabe 100644 --- a/models/gdd_current.js +++ b/models/gdd_current.js @@ -20,6 +20,10 @@ var gddCurrentSchema = mongoose.Schema({ type: Number, required: true }, + analog_year: { + type: Number, + required: true + }, last_date: { type: Date, default: Date.now diff --git a/pull_data.py b/pull_data.py index 08cbb5e..9d0078a 100644 --- a/pull_data.py +++ b/pull_data.py @@ -28,6 +28,9 @@ gdd.drop() gdd = db["gdd_current"] + +gdd_base = db["gdd"] + resp = gdd.create_index([ ("location", "2dsphere") ]) resp = gdd.create_index([ ("year", 1) ]) @@ -136,6 +139,38 @@ for i in tqdm.tqdm(x): lat_ = lat[i[0]] 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 t = {} @@ -147,6 +182,7 @@ for i in tqdm.tqdm(x): t["location"] = {"type": "Point", "coordinates": [float(lon_), float(lat_)]} t["prism_lat"] = int(a[0]) 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["year"] = int(year) diff --git a/swagger_definition.yaml b/swagger_definition.yaml index 8bcc877..0456d5e 100644 --- a/swagger_definition.yaml +++ b/swagger_definition.yaml @@ -78,6 +78,11 @@ paths: minimum: -125.0 maximum: -66.5 example: -76.94 + analog_year: + type: number + minimum: -1 + maximum: 2020 + example: 2005 400: description: Bad Request content: @@ -337,6 +342,11 @@ paths: minimum: -125.0 maximum: -66.5 example: -76.94 + analog_year: + type: number + minimum: -1 + maximum: 2020 + example: 2005 400: description: Bad Request content: @@ -425,6 +435,7 @@ paths: minimum: -125.0 maximum: -66.5 example: -76.94 + 400: description: Bad Request content: -- GitLab