diff --git a/controllers/misc_controller.go b/controllers/misc_controller.go index 9b770e6aa56dfd67f987a4e6385c6b0d1cac6cbb..e9c4d707e5e625214eb4b11a95c8df7e43a8a29c 100644 --- a/controllers/misc_controller.go +++ b/controllers/misc_controller.go @@ -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 latitude query number true "Latitude 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] func GetConfidenceInterval(c *fiber.Ctx) error { r := models.ConfidenceIntervalRequest{}.Build(c) diff --git a/models/confidence_interval.go b/models/confidence_interval.go index 3d00b6820442e198cbb337dc44027e5f91289559..4ec68422d44db342588f3822f919020f7ef3d318 100644 --- a/models/confidence_interval.go +++ b/models/confidence_interval.go @@ -1,22 +1,26 @@ package models import ( - "errors" + "fmt" "strconv" + "time" "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/persistence/entities" + "gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/utils" validation "github.com/go-ozzo/ozzo-validation" "github.com/gofiber/fiber/v2" + "github.com/tgs266/dawn-go-common/errors" ) type ConfidenceIntervalRequest struct { - Product enums.Product `json:"product"` - Latitude float64 `json:"latitude"` - Longitude float64 `json:"longitude"` - Interval float64 `json:"interval"` + Product enums.Product `json:"product"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` + Interval float64 `json:"interval"` + PlantingDate time.Time `json:"plantingDate"` } type ConfidenceIntervalResposne struct { @@ -50,7 +54,7 @@ func validateInterval(value interface{}) error { return nil } } - return errors.New("Interval not in allowed intervals") + return fmt.Errorf("Interval not in allowed intervals") } func (r ConfidenceIntervalRequest) Validate() error { @@ -67,12 +71,23 @@ func (r ConfidenceIntervalRequest) Build(c *fiber.Ctx) ConfidenceIntervalRequest latitude, _ := strconv.ParseFloat(c.Query("latitude", "-100000.0"), 64) longitude, _ := strconv.ParseFloat(c.Query("longitude", "-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{ - Product: enums.GetProductFromString(product), - Latitude: latitude, - Longitude: longitude, - Interval: interval, + Product: enums.GetProductFromString(product), + Latitude: latitude, + Longitude: longitude, + Interval: interval, + PlantingDate: plantingDate, } if rNew.Validate() != nil { diff --git a/services/confidence_interval_service.go b/services/confidence_interval_service.go index cc9d89afb6214ffc75d3d890c48d13045e74ffe4..afc64ca33a2da09318c2111e6f010bd86e83bb68 100644 --- a/services/confidence_interval_service.go +++ b/services/confidence_interval_service.go @@ -17,12 +17,20 @@ func GetConfidenceInterval(request models.ConfidenceIntervalRequest) models.Conf var lowerBound []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{} - for i := 0; i < len(gs[0].MinTemps); i++ { + for i := sliceDateInt; i < len(gs[0].MinTemps); i++ { row := []float64{} for j := 0; j < len(gs); j++ { 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] } }