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

confidence interval repeat years

parent 1bf3a553
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,7 @@ func GetAnalogYear(c *fiber.Ctx) error {
// @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"
// @Param repeatYears query number false "number of times to repeat the request. When included with planting date, will extrapolate plantingdate -> end of repeatYears range"
// @Router /gdd/confidence [get]
func GetConfidenceInterval(c *fiber.Ctx) error {
r := models.ConfidenceIntervalRequest{}.Build(c)
......
......@@ -21,6 +21,7 @@ type ConfidenceIntervalRequest struct {
Longitude float64 `json:"longitude"`
Interval float64 `json:"interval"`
PlantingDate time.Time `json:"plantingDate"`
RepeatYears int `json:"repeatYears"`
}
type ConfidenceIntervalResposne struct {
......@@ -67,6 +68,15 @@ func (r ConfidenceIntervalRequest) Validate() error {
}
func (r ConfidenceIntervalRequest) Build(c *fiber.Ctx) ConfidenceIntervalRequest {
repeatYears, errYears := strconv.Atoi(c.Query("repeatYears", "1"))
if errYears != nil {
panic(errors.NewBadRequest(nil).PutDetail("reason", "repeat years must be an integer"))
}
if repeatYears < 1 || repeatYears > 3 {
panic(errors.NewBadRequest(nil).PutDetail("reason", "repeat years must be between 1 and 3 inclusive"))
}
product := c.Query("product", "NONE")
latitude, _ := strconv.ParseFloat(c.Query("latitude", "-100000.0"), 64)
longitude, _ := strconv.ParseFloat(c.Query("longitude", "-100000.0"), 64)
......@@ -88,6 +98,7 @@ func (r ConfidenceIntervalRequest) Build(c *fiber.Ctx) ConfidenceIntervalRequest
Longitude: longitude,
Interval: interval,
PlantingDate: plantingDate,
RepeatYears: repeatYears,
}
if rNew.Validate() != nil {
......
......@@ -14,6 +14,11 @@ func GetConfidenceInterval(request models.ConfidenceIntervalRequest) models.Conf
// zScore := models.IntervalConvert[request.Interval]
// zScore := models.IntervalConvertPercentiles[request.Interval]
r := 1
if request.RepeatYears > 0 {
r = request.RepeatYears
}
var lowerBound []float64
var upperBound []float64
......@@ -26,10 +31,14 @@ func GetConfidenceInterval(request models.ConfidenceIntervalRequest) models.Conf
}
rows := [][]float64{}
for i := sliceDateInt; i < len(gs[0].MinTemps); i++ {
for i := sliceDateInt; i < len(gs[0].MinTemps)*r; i++ {
row := []float64{}
idx := i
if i >= len(gs[0].MinTemps) {
idx -= len(gs[0].MinTemps)
}
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[idx], gs[j].MaxTemps[idx], product))
if i > sliceDateInt {
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