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

add confidence interval plantingDate

parent 5db1b81e
No related branches found
No related tags found
No related merge requests found
...@@ -52,6 +52,7 @@ func GetAnalogYear(c *fiber.Ctx) error { ...@@ -52,6 +52,7 @@ func GetAnalogYear(c *fiber.Ctx) error {
// @Param Interval query number true "Interval values" Enums(80, 85, 90, 95, 99, 99.5, 99.9) // @Param Interval query number true "Interval values" Enums(80, 85, 90, 95, 99, 99.5, 99.9)
// @Param latitude query number true "Latitude to search for" // @Param latitude query number true "Latitude to search for"
// @Param longitude query number true "Longitude to search for" // @Param longitude query number true "Longitude to search for"
// @Param plantingDate query string true "Plant date, ISO8601 or RFC3339 format"
// @Router /gdd/confidence [get] // @Router /gdd/confidence [get]
func GetConfidenceInterval(c *fiber.Ctx) error { func GetConfidenceInterval(c *fiber.Ctx) error {
r := models.ConfidenceIntervalRequest{}.Build(c) r := models.ConfidenceIntervalRequest{}.Build(c)
......
package models package models
import ( import (
"errors" "fmt"
"strconv" "strconv"
"time"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/config" "gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/config"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/models/enums" "gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/models/enums"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/persistence/entities" "gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/persistence/entities"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/utils"
validation "github.com/go-ozzo/ozzo-validation" validation "github.com/go-ozzo/ozzo-validation"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/tgs266/dawn-go-common/errors"
) )
type ConfidenceIntervalRequest struct { type ConfidenceIntervalRequest struct {
Product enums.Product `json:"product"` Product enums.Product `json:"product"`
Latitude float64 `json:"latitude"` Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"` Longitude float64 `json:"longitude"`
Interval float64 `json:"interval"` Interval float64 `json:"interval"`
PlantingDate time.Time `json:"plantingDate"`
} }
type ConfidenceIntervalResposne struct { type ConfidenceIntervalResposne struct {
...@@ -50,7 +54,7 @@ func validateInterval(value interface{}) error { ...@@ -50,7 +54,7 @@ func validateInterval(value interface{}) error {
return nil return nil
} }
} }
return errors.New("Interval not in allowed intervals") return fmt.Errorf("Interval not in allowed intervals")
} }
func (r ConfidenceIntervalRequest) Validate() error { func (r ConfidenceIntervalRequest) Validate() error {
...@@ -67,12 +71,23 @@ func (r ConfidenceIntervalRequest) Build(c *fiber.Ctx) ConfidenceIntervalRequest ...@@ -67,12 +71,23 @@ func (r ConfidenceIntervalRequest) Build(c *fiber.Ctx) ConfidenceIntervalRequest
latitude, _ := strconv.ParseFloat(c.Query("latitude", "-100000.0"), 64) latitude, _ := strconv.ParseFloat(c.Query("latitude", "-100000.0"), 64)
longitude, _ := strconv.ParseFloat(c.Query("longitude", "-100000.0"), 64) longitude, _ := strconv.ParseFloat(c.Query("longitude", "-100000.0"), 64)
interval, _ := strconv.ParseFloat(c.Query("interval", "-100000.0"), 64) interval, _ := strconv.ParseFloat(c.Query("interval", "-100000.0"), 64)
pd := c.Query("plantingDate", utils.GetFirstOfTheYear().Format(time.RFC3339))
plantingDate, errDate := time.Parse(time.RFC3339, pd)
if errDate != nil {
panic(errors.NewBadRequest(nil).PutDetail("reason", "no planting date provided"))
}
if errDate != nil && pd != "" {
panic(errors.NewBadRequest(nil).PutDetail("reason", "date must be ISO8601 or RFC3339 format"))
}
rNew := ConfidenceIntervalRequest{ rNew := ConfidenceIntervalRequest{
Product: enums.GetProductFromString(product), Product: enums.GetProductFromString(product),
Latitude: latitude, Latitude: latitude,
Longitude: longitude, Longitude: longitude,
Interval: interval, Interval: interval,
PlantingDate: plantingDate,
} }
if rNew.Validate() != nil { if rNew.Validate() != nil {
......
...@@ -17,12 +17,20 @@ func GetConfidenceInterval(request models.ConfidenceIntervalRequest) models.Conf ...@@ -17,12 +17,20 @@ func GetConfidenceInterval(request models.ConfidenceIntervalRequest) models.Conf
var lowerBound []float64 var lowerBound []float64
var upperBound []float64 var upperBound []float64
// get the int of the planting date. If the date is less than the first date, we do nothing
// otherwise, we adjust
// need to do before because of accumulations
sliceDateInt := request.PlantingDate.YearDay() - 1
if sliceDateInt < 0 {
sliceDateInt = 0
}
rows := [][]float64{} rows := [][]float64{}
for i := 0; i < len(gs[0].MinTemps); i++ { for i := sliceDateInt; i < len(gs[0].MinTemps); i++ {
row := []float64{} row := []float64{}
for j := 0; j < len(gs); j++ { for j := 0; j < len(gs); j++ {
row = append(row, utils.CalculateSingleGdd(gs[j].MinTemps[i], gs[j].MaxTemps[i], product)) row = append(row, utils.CalculateSingleGdd(gs[j].MinTemps[i], gs[j].MaxTemps[i], product))
if i > 0 { if i > sliceDateInt {
row[j] += rows[len(rows)-1][j] row[j] += rows[len(rows)-1][j]
} }
} }
......
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