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

gefs

parent 834b295c
No related branches found
No related tags found
No related merge requests found
*.exe *.exe
\ No newline at end of file .idea
\ No newline at end of file
...@@ -7,5 +7,5 @@ server: ...@@ -7,5 +7,5 @@ server:
context-path: "/api/weather" context-path: "/api/weather"
db: db:
uri: "mongodb+srv://gdd-server:u8i3icLAJXjZEhTs@cluster0.wdxf4.mongodb.net/" uri: "mongodb://127.0.0.1:27017/"
database: "gdd_database" database: "weather-service"
\ No newline at end of file \ No newline at end of file
...@@ -2,10 +2,12 @@ package config ...@@ -2,10 +2,12 @@ package config
import ( import (
"dawn-weather/controllers" "dawn-weather/controllers"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func GddRoutes(route fiber.Router) { func GddRoutes(route fiber.Router) {
route.Get("gdd/daily", controllers.GetDailyGdd) route.Get("gdd/daily", controllers.GetDailyGdd)
route.Get("gdd/normals", controllers.GetNormalGdd) route.Get("gdd/normals", controllers.GetNormalGdd)
route.Get("gdd/gefs", controllers.GetGefsGDD)
} }
package controllers
import (
"dawn-weather/models"
"dawn-weather/services"
"github.com/gofiber/fiber/v2"
)
// GetGefs godoc
// @Summary Get GDD values calculated from GEFS
// @Tags Gdd
// @Description Get GDD values calculated from GEFS
// @Accept json
// @Produce json
// @Success 200 {object} models.GefsGddResponse
// @Failure 400 {object} errors.StandardError
// @Param year query int true "Year to get gdd for"
// @Param product query string true "Crop to calculate gdd for" Enums(corn, soybean, sunflower, tomato, sugar_beet, peanut, cotton, potato, wheat, pea, oat, spring_wheat, rice, sorghum)
// @Param latitude query number true "Latitude to search for"
// @Param longitude query number true "Longitude to search for"
// @Param accumulate query boolean true "Accumulate gdd values"
// @Router /api/weather/gdd/gefs [get]
func GetGefsGDD(c *fiber.Ctx) error {
request := models.GddRequest{}.Build(c)
return c.Status(fiber.StatusOK).JSON(services.GetGefsGddValues(request))
}
...@@ -109,6 +109,88 @@ var doc = `{ ...@@ -109,6 +109,88 @@ var doc = `{
} }
} }
}, },
"/api/weather/gdd/gefs": {
"get": {
"description": "Get GDD values calculated from GEFS",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Gdd"
],
"summary": "Get GDD values calculated from GEFS",
"parameters": [
{
"type": "integer",
"description": "Year to get gdd for",
"name": "year",
"in": "query",
"required": true
},
{
"enum": [
"corn",
"soybean",
"sunflower",
"tomato",
"sugar_beet",
"peanut",
"cotton",
"potato",
"wheat",
"pea",
"oat",
"spring_wheat",
"rice",
"sorghum"
],
"type": "string",
"description": "Crop to calculate gdd for",
"name": "product",
"in": "query",
"required": true
},
{
"type": "number",
"description": "Latitude to search for",
"name": "latitude",
"in": "query",
"required": true
},
{
"type": "number",
"description": "Longitude to search for",
"name": "longitude",
"in": "query",
"required": true
},
{
"type": "boolean",
"description": "Accumulate gdd values",
"name": "accumulate",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.GefsGddResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/errors.StandardError"
}
}
}
}
},
"/api/weather/gdd/normals": { "/api/weather/gdd/normals": {
"get": { "get": {
"description": "get gdd normals", "description": "get gdd normals",
...@@ -221,7 +303,33 @@ var doc = `{ ...@@ -221,7 +303,33 @@ var doc = `{
} }
}, },
"last_date": { "last_date": {
"type": "integer" "type": "string"
},
"nearest_latitude": {
"type": "number"
},
"nearest_longitude": {
"type": "number"
},
"product": {
"type": "string"
}
}
},
"models.GefsGddResponse": {
"type": "object",
"properties": {
"first_date": {
"type": "string"
},
"gdd_values": {
"type": "array",
"items": {
"type": "number"
}
},
"last_date": {
"type": "string"
}, },
"nearest_latitude": { "nearest_latitude": {
"type": "number" "type": "number"
......
...@@ -94,6 +94,88 @@ ...@@ -94,6 +94,88 @@
} }
} }
}, },
"/api/weather/gdd/gefs": {
"get": {
"description": "Get GDD values calculated from GEFS",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Gdd"
],
"summary": "Get GDD values calculated from GEFS",
"parameters": [
{
"type": "integer",
"description": "Year to get gdd for",
"name": "year",
"in": "query",
"required": true
},
{
"enum": [
"corn",
"soybean",
"sunflower",
"tomato",
"sugar_beet",
"peanut",
"cotton",
"potato",
"wheat",
"pea",
"oat",
"spring_wheat",
"rice",
"sorghum"
],
"type": "string",
"description": "Crop to calculate gdd for",
"name": "product",
"in": "query",
"required": true
},
{
"type": "number",
"description": "Latitude to search for",
"name": "latitude",
"in": "query",
"required": true
},
{
"type": "number",
"description": "Longitude to search for",
"name": "longitude",
"in": "query",
"required": true
},
{
"type": "boolean",
"description": "Accumulate gdd values",
"name": "accumulate",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.GefsGddResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/errors.StandardError"
}
}
}
}
},
"/api/weather/gdd/normals": { "/api/weather/gdd/normals": {
"get": { "get": {
"description": "get gdd normals", "description": "get gdd normals",
...@@ -206,7 +288,33 @@ ...@@ -206,7 +288,33 @@
} }
}, },
"last_date": { "last_date": {
"type": "integer" "type": "string"
},
"nearest_latitude": {
"type": "number"
},
"nearest_longitude": {
"type": "number"
},
"product": {
"type": "string"
}
}
},
"models.GefsGddResponse": {
"type": "object",
"properties": {
"first_date": {
"type": "string"
},
"gdd_values": {
"type": "array",
"items": {
"type": "number"
}
},
"last_date": {
"type": "string"
}, },
"nearest_latitude": { "nearest_latitude": {
"type": "number" "type": "number"
......
...@@ -23,7 +23,24 @@ definitions: ...@@ -23,7 +23,24 @@ definitions:
type: number type: number
type: array type: array
last_date: last_date:
type: integer type: string
nearest_latitude:
type: number
nearest_longitude:
type: number
product:
type: string
type: object
models.GefsGddResponse:
properties:
first_date:
type: string
gdd_values:
items:
type: number
type: array
last_date:
type: string
nearest_latitude: nearest_latitude:
type: number type: number
nearest_longitude: nearest_longitude:
...@@ -100,6 +117,66 @@ paths: ...@@ -100,6 +117,66 @@ paths:
summary: Get gdd values summary: Get gdd values
tags: tags:
- Gdd - Gdd
/api/weather/gdd/gefs:
get:
consumes:
- application/json
description: Get GDD values calculated from GEFS
parameters:
- description: Year to get gdd for
in: query
name: year
required: true
type: integer
- description: Crop to calculate gdd for
enum:
- corn
- soybean
- sunflower
- tomato
- sugar_beet
- peanut
- cotton
- potato
- wheat
- pea
- oat
- spring_wheat
- rice
- sorghum
in: query
name: product
required: true
type: string
- description: Latitude to search for
in: query
name: latitude
required: true
type: number
- description: Longitude to search for
in: query
name: longitude
required: true
type: number
- description: Accumulate gdd values
in: query
name: accumulate
required: true
type: boolean
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.GefsGddResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/errors.StandardError'
summary: Get GDD values calculated from GEFS
tags:
- Gdd
/api/weather/gdd/normals: /api/weather/gdd/normals:
get: get:
consumes: consumes:
......
...@@ -8,6 +8,7 @@ require ( ...@@ -8,6 +8,7 @@ require (
github.com/ansrivas/fiberprometheus/v2 v2.1.2 // indirect github.com/ansrivas/fiberprometheus/v2 v2.1.2 // indirect
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
github.com/arsmn/fiber-swagger/v2 v2.15.0 // indirect github.com/arsmn/fiber-swagger/v2 v2.15.0 // indirect
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013 // indirect
github.com/coreos/etcd v3.3.10+incompatible // indirect github.com/coreos/etcd v3.3.10+incompatible // indirect
github.com/coreos/go-etcd v2.0.0+incompatible // indirect github.com/coreos/go-etcd v2.0.0+incompatible // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
...@@ -28,6 +29,7 @@ require ( ...@@ -28,6 +29,7 @@ require (
github.com/valyala/fasthttp v1.28.0 // indirect github.com/valyala/fasthttp v1.28.0 // indirect
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect
go.mongodb.org/mongo-driver v1.7.1 // indirect go.mongodb.org/mongo-driver v1.7.1 // indirect
go4.org v0.0.0-20201209231011-d4a079459e60 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
golang.org/x/tools v0.1.5 // indirect golang.org/x/tools v0.1.5 // indirect
......
...@@ -82,6 +82,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= ...@@ -82,6 +82,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013 h1:/P9/RL0xgWE+ehnCUUN5h3RpG3dmoMCOONO1CCvq23Y=
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013/go.mod h1:pccXHIvs3TV/TUqSNyEvF99sxjX2r4FFRIyw6TZY9+w=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
...@@ -462,6 +464,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR ...@@ -462,6 +464,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
...@@ -565,6 +568,8 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9E ...@@ -565,6 +568,8 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9E
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
go4.org v0.0.0-20201209231011-d4a079459e60 h1:iqAGo78tVOJXELHQFRjR6TMwItrvXH4hrGJ32I/NFF8=
go4.org v0.0.0-20201209231011-d4a079459e60/go.mod h1:CIiUVy99QCPfoE13bO4EZaz5GZMZXMSBGhxRdsvzbkg=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"dawn-weather/config" "dawn-weather/config"
"dawn-weather/errors" "dawn-weather/errors"
"dawn-weather/persistence" "dawn-weather/persistence"
"github.com/ansrivas/fiberprometheus/v2" "github.com/ansrivas/fiberprometheus/v2"
swagger "github.com/arsmn/fiber-swagger/v2" swagger "github.com/arsmn/fiber-swagger/v2"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
...@@ -30,7 +31,7 @@ func registerSwagger(app *fiber.App) { ...@@ -30,7 +31,7 @@ func registerSwagger(app *fiber.App) {
func registerCors(app *fiber.App) { func registerCors(app *fiber.App) {
app.Use(cors.New(cors.Config{ app.Use(cors.New(cors.Config{
AllowOrigins: "http://localhost:3000, http://localhost:4200", AllowOrigins: "http://localhost:3000, http://localhost:4200",
AllowHeaders: "Origin, Content-Type, Accept", AllowHeaders: "Origin, Content-Type, Accept",
})) }))
} }
...@@ -51,7 +52,7 @@ func registerLogging(app *fiber.App) { ...@@ -51,7 +52,7 @@ func registerLogging(app *fiber.App) {
func registerPrometheus(app *fiber.App) { func registerPrometheus(app *fiber.App) {
prometheus := fiberprometheus.New(viper.GetString("app.name")) prometheus := fiberprometheus.New(viper.GetString("app.name"))
prometheus.RegisterAt(app, viper.GetString("server.context-path") + "/metrics") prometheus.RegisterAt(app, viper.GetString("server.context-path")+"/metrics")
app.Use(prometheus.Middleware) app.Use(prometheus.Middleware)
} }
......
...@@ -15,7 +15,7 @@ type GddResponse struct { ...@@ -15,7 +15,7 @@ type GddResponse struct {
NearestLatitude float64 `json:"nearest_latitude"` NearestLatitude float64 `json:"nearest_latitude"`
NearestLongitude float64 `json:"nearest_longitude"` NearestLongitude float64 `json:"nearest_longitude"`
GddValues []float64 `json:"gdd_values"` GddValues []float64 `json:"gdd_values"`
LastDate string `json:"last_date"` LastDate time.Time `json:"last_date"`
} }
type GddRequest struct { type GddRequest struct {
......
package models
import "time"
type GefsGddResponse struct {
Product string `json:"product"`
NearestLatitude float64 `json:"nearest_latitude"`
NearestLongitude float64 `json:"nearest_longitude"`
GddValues []float64 `json:"gdd_values"`
LastDate time.Time `json:"last_date"`
FirstDate time.Time `json:"first_date"`
}
...@@ -16,7 +16,15 @@ type Gdd struct { ...@@ -16,7 +16,15 @@ type Gdd struct {
LastDate primitive.DateTime `bson:"last_date,omitempty"` LastDate primitive.DateTime `bson:"last_date,omitempty"`
AnalogYear int `bson:"analog_year,omitempty"` AnalogYear int `bson:"analog_year,omitempty"`
Year int `bson:"year,omitempty"` Year int `bson:"year,omitempty"`
MinTemps []float64 `bson:"min_temps,omitempty""` MinTemps []float64 `bson:"min_temps,omitempty"`
MaxTemps []float64 `bson:"max_temps,omitempty""` MaxTemps []float64 `bson:"max_temps,omitempty"`
Location Location `bson:"location,omitempty""` Location Location `bson:"location,omitempty"`
}
type GefsGdd struct {
ID string `bson:"_id,omitempty"`
Location Location `bson:"location,omitempty"`
MinTemp float64 `bson:"min_temp,omitempty"`
MaxTemp float64 `bson:"max_temp,omitempty"`
Date primitive.DateTime `bson:"date,omitempty"`
} }
...@@ -5,10 +5,10 @@ type Normal struct { ...@@ -5,10 +5,10 @@ type Normal struct {
Location Location `bson:"location,omitempty"` Location Location `bson:"location,omitempty"`
PrismLat int `bson:"prism_lat,omitempty"` PrismLat int `bson:"prism_lat,omitempty"`
PrismLon int `bson:"prism_lon,omitempty"` PrismLon int `bson:"prism_lon,omitempty"`
GddBase []float64 `bson:"normals_gdd_base,omitempty"` GddBase []float64 `bson:"global_normal_base,omitempty"`
CornBase []float64 `bson:"corn_normals_gdd_base,omitempty"` CornBase []float64 `bson:"corn_normal_base,omitempty"`
GddMean []float64 `bson:"global_mean,omitempty"` GddMean []float64 `bson:"global_mean,omitempty"`
CornMean []float64 `bson:"corn_global_mean,omitempty"` CornMean []float64 `bson:"corn_mean,omitempty"`
GddStd []float64 `bson:"main_std,omitempty"` GddStd []float64 `bson:"global_std,omitempty"`
CornStd []float64 `bson:"corn_std,omitempty"` CornStd []float64 `bson:"corn_std,omitempty"`
} }
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"context" "context"
"dawn-weather/errors" "dawn-weather/errors"
"dawn-weather/persistence/entities" "dawn-weather/persistence/entities"
"github.com/bradfitz/slice"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
...@@ -31,27 +33,25 @@ func CreateDBSession() error { ...@@ -31,27 +33,25 @@ func CreateDBSession() error {
return nil return nil
} }
func buildGddRequest(location entities.Location, year *int) bson.D { func buildLocationRequest(location entities.Location, year *int) bson.D {
var filter bson.D var filter bson.D
if year != nil { if year != nil {
filter = bson.D{ filter = bson.D{
{"location", {Key: "location", Value: bson.D{
bson.D{ {Key: "$near", Value: bson.D{
{"$near", bson.D{ {Key: "$geometry", Value: location},
{"$geometry", location}, }},
}}, },
},
}, },
{"year", *year}, {Key: "year", Value: *year},
} }
} else { } else {
filter = bson.D{ filter = bson.D{
{"location", {Key: "location", Value: bson.D{
bson.D{ {Key: "$near", Value: bson.D{
{"$near", bson.D{ {Key: "$geometry", Value: location},
{"$geometry", location}, }},
}}, },
},
}, },
} }
} }
...@@ -61,7 +61,7 @@ func buildGddRequest(location entities.Location, year *int) bson.D { ...@@ -61,7 +61,7 @@ func buildGddRequest(location entities.Location, year *int) bson.D {
func CurrentGddFindFirstByYearAndLocation(location entities.Location) entities.Gdd { func CurrentGddFindFirstByYearAndLocation(location entities.Location) entities.Gdd {
coll := Conn.Database(viper.GetString("db.database")).Collection("gdd_current") coll := Conn.Database(viper.GetString("db.database")).Collection("gdd_current")
filter := buildGddRequest(location, nil) filter := buildLocationRequest(location, nil)
var g entities.Gdd var g entities.Gdd
err := coll.FindOne(Ctx, filter).Decode(&g) err := coll.FindOne(Ctx, filter).Decode(&g)
...@@ -75,7 +75,7 @@ func CurrentGddFindFirstByYearAndLocation(location entities.Location) entities.G ...@@ -75,7 +75,7 @@ func CurrentGddFindFirstByYearAndLocation(location entities.Location) entities.G
func GddFindFirstByYearAndLocation(year int, location entities.Location) entities.Gdd { func GddFindFirstByYearAndLocation(year int, location entities.Location) entities.Gdd {
coll := Conn.Database(viper.GetString("db.database")).Collection("gdd") coll := Conn.Database(viper.GetString("db.database")).Collection("gdd")
filter := buildGddRequest(location, &year) filter := buildLocationRequest(location, &year)
var g entities.Gdd var g entities.Gdd
err := coll.FindOne(Ctx, filter).Decode(&g) err := coll.FindOne(Ctx, filter).Decode(&g)
...@@ -87,9 +87,9 @@ func GddFindFirstByYearAndLocation(year int, location entities.Location) entitie ...@@ -87,9 +87,9 @@ func GddFindFirstByYearAndLocation(year int, location entities.Location) entitie
} }
func NormalsFindFirstByYearAndLocation(location entities.Location) entities.Normal { func NormalsFindFirstByYearAndLocation(location entities.Location) entities.Normal {
coll := Conn.Database(viper.GetString("db.database")).Collection("normals") coll := Conn.Database(viper.GetString("db.database")).Collection("normal_gdd")
filter := buildGddRequest(location, nil) filter := buildLocationRequest(location, nil)
var n entities.Normal var n entities.Normal
err := coll.FindOne(Ctx, filter).Decode(&n) err := coll.FindOne(Ctx, filter).Decode(&n)
...@@ -99,3 +99,38 @@ func NormalsFindFirstByYearAndLocation(location entities.Location) entities.Norm ...@@ -99,3 +99,38 @@ func NormalsFindFirstByYearAndLocation(location entities.Location) entities.Norm
return n return n
} }
func GefsFindAllByLocation(location entities.Location) []entities.GefsGdd {
coll := Conn.Database(viper.GetString("db.database")).Collection("gefs")
filter := buildLocationRequest(location, nil)
var results []entities.GefsGdd
options := options.Find()
count := 10
options.SetLimit(int64(count))
cursor, err := coll.Find(Ctx, filter, options)
if err != nil {
panic(errors.NO_DATA_FOUND)
}
for cursor.Next(context.TODO()) {
var elem entities.GefsGdd
err := cursor.Decode(&elem)
if err != nil {
panic(errors.NO_DATA_FOUND)
}
results = append(results, elem)
}
slice.Sort(results[:], func(i, j int) bool {
return results[i].Date.Time().Unix() < results[j].Date.Time().Unix()
})
return results
}
...@@ -5,62 +5,10 @@ import ( ...@@ -5,62 +5,10 @@ import (
"dawn-weather/models/enums" "dawn-weather/models/enums"
"dawn-weather/persistence" "dawn-weather/persistence"
"dawn-weather/persistence/entities" "dawn-weather/persistence/entities"
"dawn-weather/utils"
"time" "time"
) )
func calculateGdd(minTemp float64, maxTemp float64, product enums.Product) float64 {
if product.Name == "CORN" {
if minTemp < 50 {
minTemp = 50
}
if minTemp > 86 {
minTemp = 86
}
if maxTemp < 50 {
maxTemp = 50
}
if maxTemp > 86 {
maxTemp = 86
}
}
mean := (maxTemp + minTemp) / 2.0
value := mean - product.BaseTemp
if product.Name == "CORN" {
return value
}
if value < 0 {
value = 0
}
return value
}
func CalculateGddValues(minTemps []float64, maxTemps []float64, product enums.Product, accumulate bool) []float64 {
var returnList []float64
for i := 0; i < len(minTemps); i++ {
value := calculateGdd(minTemps[i], maxTemps[i], product)
if accumulate && i > 0 {
value += returnList[len(returnList)-1]
}
returnList = append(returnList, value)
}
return returnList
}
func CalculateNormalGddValues(base []float64, product enums.Product, accumulate bool) []float64 {
var returnList []float64
for i := 0; i < len(base); i++ {
value := base[i] - product.BaseTemp
if value < 0 {
value = 0
}
if accumulate && i > 0 {
value += returnList[len(returnList)-1]
}
returnList = append(returnList, value)
}
return returnList
}
func GetGddValues(request models.GddRequest) models.GddResponse { func GetGddValues(request models.GddRequest) models.GddResponse {
product := enums.GetProductFromString(request.Product) product := enums.GetProductFromString(request.Product)
var gdds entities.Gdd var gdds entities.Gdd
...@@ -73,8 +21,8 @@ func GetGddValues(request models.GddRequest) models.GddResponse { ...@@ -73,8 +21,8 @@ func GetGddValues(request models.GddRequest) models.GddResponse {
Product: product.Name, Product: product.Name,
NearestLatitude: gdds.Location.Coordinates[1], NearestLatitude: gdds.Location.Coordinates[1],
NearestLongitude: gdds.Location.Coordinates[0], NearestLongitude: gdds.Location.Coordinates[0],
GddValues: CalculateGddValues(gdds.MinTemps, gdds.MaxTemps, product, request.Accumulate), GddValues: utils.CalculateGddValues(gdds.MinTemps, gdds.MaxTemps, product, request.Accumulate),
LastDate: gdds.LastDate.Time().String(), LastDate: gdds.LastDate.Time(),
} }
return returnGdds return returnGdds
} }
...@@ -88,14 +36,14 @@ func GetNormalValues(request models.GddRequest) models.GddResponse { ...@@ -88,14 +36,14 @@ func GetNormalValues(request models.GddRequest) models.GddResponse {
Product: product.Name, Product: product.Name,
NearestLatitude: g.Location.Coordinates[1], NearestLatitude: g.Location.Coordinates[1],
NearestLongitude: g.Location.Coordinates[0], NearestLongitude: g.Location.Coordinates[0],
GddValues: CalculateNormalGddValues(g.CornBase, product, request.Accumulate), GddValues: utils.CalculateNormalGddValues(g.CornBase, product, request.Accumulate),
} }
} else { } else {
returnGdds = models.GddResponse{ returnGdds = models.GddResponse{
Product: product.Name, Product: product.Name,
NearestLatitude: g.Location.Coordinates[1], NearestLatitude: g.Location.Coordinates[1],
NearestLongitude: g.Location.Coordinates[0], NearestLongitude: g.Location.Coordinates[0],
GddValues: CalculateNormalGddValues(g.GddBase, product, request.Accumulate), GddValues: utils.CalculateNormalGddValues(g.GddBase, product, request.Accumulate),
} }
} }
return returnGdds return returnGdds
......
package services
import (
"dawn-weather/models"
"dawn-weather/models/enums"
"dawn-weather/persistence"
"dawn-weather/utils"
)
func GetGefsGddValues(request models.GddRequest) models.GefsGddResponse {
product := enums.GetProductFromString(request.Product)
location := request.BuildLocation()
g := persistence.GefsFindAllByLocation(location)
var returnGdds models.GefsGddResponse
var gdds []float64
for i := 0; i < 10; i++ {
temp := g[i]
gdds = append(gdds, utils.CalculateSingleGdd(temp.MinTemp, temp.MaxTemp, product))
}
returnGdds = models.GefsGddResponse{
Product: product.Name,
NearestLatitude: location.Coordinates[1],
NearestLongitude: location.Coordinates[0],
GddValues: gdds,
FirstDate: g[0].Date.Time(),
LastDate: g[9].Date.Time(),
}
return returnGdds
}
package utils
import "dawn-weather/models/enums"
func CalculateSingleGdd(minTemp float64, maxTemp float64, product enums.Product) float64 {
if product.Name == "CORN" {
if minTemp < 50 {
minTemp = 50
}
if minTemp > 86 {
minTemp = 86
}
if maxTemp < 50 {
maxTemp = 50
}
if maxTemp > 86 {
maxTemp = 86
}
}
mean := (maxTemp + minTemp) / 2.0
value := mean - product.BaseTemp
if product.Name == "CORN" {
return value
}
if value < 0 {
value = 0
}
return value
}
func CalculateNormalGddValues(base []float64, product enums.Product, accumulate bool) []float64 {
var returnList []float64
for i := 0; i < len(base); i++ {
value := base[i] - product.BaseTemp
if value < 0 {
value = 0
}
if accumulate && i > 0 {
value += returnList[len(returnList)-1]
}
returnList = append(returnList, value)
}
return returnList
}
func CalculateGddValues(minTemps []float64, maxTemps []float64, product enums.Product, accumulate bool) []float64 {
var returnList []float64
for i := 0; i < len(minTemps); i++ {
value := CalculateSingleGdd(minTemps[i], maxTemps[i], product)
if accumulate && i > 0 {
value += returnList[len(returnList)-1]
}
returnList = append(returnList, value)
}
return returnList
}
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