From c5d4e7cf2130a45feac640c720c5f55d7c72611c Mon Sep 17 00:00:00 2001
From: Tucker Siegel <tgsiegel@terpmail.umd.edu>
Date: Sun, 29 Jan 2023 14:49:20 -0500
Subject: [PATCH] fix date issues

---
 persistence/repositories/nomads.go | 29 +++++++++++++++++++++++++++++
 services/forecast_service.go       | 16 ++++++++++++----
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/persistence/repositories/nomads.go b/persistence/repositories/nomads.go
index 1f29847..4e18ce0 100644
--- a/persistence/repositories/nomads.go
+++ b/persistence/repositories/nomads.go
@@ -3,11 +3,13 @@ package repositories
 import (
 	"context"
 	errs "errors"
+	"time"
 
 	"github.com/bradfitz/slice"
 	"github.com/tgs266/dawn-go-common/common"
 	"github.com/tgs266/dawn-go-common/errors"
 	"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/persistence/entities"
+	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/mongo"
 	"go.mongodb.org/mongo-driver/mongo/options"
 )
@@ -15,6 +17,7 @@ import (
 type NomadsRepository interface {
 	FindCfsByLocation(ctx context.Context, location entities.Location) []entities.CfsGdd
 	FindCfsByLocationWithExpand(ctx context.Context, location entities.Location, areaExpand int) []entities.CfsGdd
+	FindCfsByLocationWithExpandByPlantingDate(ctx context.Context, plantingDate time.Time, location entities.Location, areaExpand int) []entities.CfsGdd
 	FindCfsAverageByLocation(ctx context.Context, location entities.Location) entities.CfsGdd
 	FindGefsByLocation(ctx context.Context, location entities.Location) []entities.GefsGdd
 }
@@ -112,3 +115,29 @@ func (g *nomadsRepositoryImpl) FindCfsByLocation(ctx context.Context, location e
 func (g *nomadsRepositoryImpl) FindCfsByLocationWithExpand(ctx context.Context, location entities.Location, areaExpand int) []entities.CfsGdd {
 	return g.findCfsByLocationInternal(ctx, location, areaExpand)
 }
+
+func (g *nomadsRepositoryImpl) FindCfsByLocationWithExpandByPlantingDate(ctx context.Context, plantingDate time.Time, location entities.Location, areaExpand int) []entities.CfsGdd {
+	count := g.FindCfsAverageByLocation(ctx, location).Count
+
+	coll := g.session.Collection("cfs")
+
+	filter := buildLocationRequestLarger(location, nil)
+	filter["date"] = bson.M{"$gte": plantingDate}
+	cursor, err := coll.Find(ctx, filter, options.Find().SetLimit(int64(count*areaExpand)))
+
+	var gs []entities.CfsGdd
+
+	if err != nil {
+		panic(errors.NewInternal(err).PutDetail("reason", "cfs"))
+	}
+
+	if err = cursor.All(ctx, &gs); err != nil {
+		panic(errors.NewInternal(err))
+	}
+
+	if len(gs) == 0 {
+		panic(errors.NewNotFound(nil).PutDetail("reason", "cfs"))
+	}
+
+	return gs
+}
diff --git a/services/forecast_service.go b/services/forecast_service.go
index 6be38cb..a5da5fc 100644
--- a/services/forecast_service.go
+++ b/services/forecast_service.go
@@ -15,12 +15,15 @@ import (
 )
 
 // this just adjusts cfs by accumulating it with the base value being the provided accumulated value
-func calculateAccumulatedCfsBasedOnAccumulatedObserved(product enums.Product, accumulated float64, cfs []entities.CfsGdd) [][]float64 {
+func calculateAccumulatedCfsBasedOnAccumulatedObserved(product enums.Product, startDate int, accumulated float64, cfs []entities.CfsGdd) [][]float64 {
 	out := [][]float64{}
 	for _, c := range cfs {
 		tempAccum := accumulated
 		temp := []float64{}
 		for i := range c.MinTemps {
+			if i < startDate {
+				continue
+			}
 			tempAccum = tempAccum + utils.CalculateSingleGdd(c.MinTemps[i], c.MaxTemps[i], product)
 			temp = append(temp, tempAccum)
 		}
@@ -39,6 +42,9 @@ func getDatesForCfsMatches(cfs [][]float64, lastDateInt int, currentMatch int, k
 		for i, v := range c {
 
 			dist := math.Abs(matches[keys[tempCMatch]] - v)
+			// if z == 0 {
+			// 	fmt.Println(keys[tempCMatch], dist, matches[keys[tempCMatch]], v, lastDateInt+i-1)
+			// }
 			// check if the last value is closer than the current. if it is, then the last value is the one to return
 			if dist > lastDist {
 				if v, exists := out[keys[tempCMatch]]; exists {
@@ -162,13 +168,15 @@ func forecast(ctx context.Context, gddReq models.GddRequest, plantdate time.Time
 			accumulatedGdds += observedValues[i]
 			lastDist = dist
 		}
+	} else {
+		date = gddReq.PlantingDate.YearDay() - 1
 	}
 
 	if currentMatch == len(keys) {
 		return out
 	}
 	// adjust cfs values to start at the accumulated value
-	adjustedCfs := calculateAccumulatedCfsBasedOnAccumulatedObserved(product, accumulatedGdds, cfs)
+	adjustedCfs := calculateAccumulatedCfsBasedOnAccumulatedObserved(product, date, accumulatedGdds, cfs)
 	cfsHist := getDatesForCfsMatches(adjustedCfs, date, currentMatch, keys, matches)
 	// this loop will actually build the 5 bins
 	for k, v := range cfsHist {
@@ -204,7 +212,7 @@ func forecast(ctx context.Context, gddReq models.GddRequest, plantdate time.Time
 		for d, v := range temp {
 			bins = append(bins, models.Bin{
 				Value: (float64(v) + alpha) / (float64(sum) + float64(binCount)*alpha),
-				Date:  plantdate.AddDate(0, 0, d),
+				Date:  plantdate.AddDate(0, 0, d-date),
 			})
 			c += 1
 		}
@@ -252,7 +260,7 @@ func comparisonNormals(ctx context.Context, request models.GddRequest, plantdate
 		start -= 1
 	}
 
-	i := start
+	i := 0
 	date := 0
 	out := map[string]time.Time{}
 
-- 
GitLab