diff --git a/services/forecast_service.go b/services/forecast_service.go index 6bb6e643a221db996d811cb307a5b5bb006dbdb5..0604dc06590b600b08e8a1e9310a848db5e63090 100644 --- a/services/forecast_service.go +++ b/services/forecast_service.go @@ -58,10 +58,14 @@ func getDatesForCfsMatches(cfs [][]float64, lastDateInt int, currentMatch int, k return out } -// bin array of dates into a frequency map and return that map and keys -func bin(data []int) (map[int]int, []int) { +// bin array of dates into a frequency map +// +// returns: frequency map, keys, and the key with the highest frequency +func bin(data []int) (map[int]int, []int, int) { out := map[int]int{} keys := []int{} + maxKey := 0 + max := 0 for _, d := range data { if count, exists := out[d]; exists { out[d] = count + 1 @@ -69,21 +73,12 @@ func bin(data []int) (map[int]int, []int) { out[d] = 1 keys = append(keys, d) } - } - return out, keys -} - -// get the highest frequency bin -func getLargestBinCount(bins map[int]int) int { - largest := 0 - largestKey := 0 - for key, count := range bins { - if count > largest { - largest = count - largestKey = key + if out[d] > max { + max = out[d] + maxKey = d } } - return largestKey + return out, keys, maxKey } // get keys of the stage matches in sorted order @@ -161,9 +156,8 @@ func forecast(ctx common.DawnCtx, gddReq models.GddRequest, plantdate time.Time, // this loop will actually build the 5 bins for k, v := range cfsHist { - binnedDates, keys := bin(v) + binnedDates, keys, largestKey := bin(v) stepSize := int(math.Ceil(AvgDiff(keys)) + 1) // add 1 to increase range and for uncertainty - largestKey := getLargestBinCount(binnedDates) sort.Ints(keys) @@ -333,14 +327,13 @@ func CalculateStages(ctx common.DawnCtx, request models.StageRequest) map[string Accumulate: false, Product: "CORN", } - - gdds, cfs := asyncCollectGddsAndCfs(ctx, gddReq) - stageMatches := models.BuildStageMatches(request.Mode, request.Value) comparisonThread := dispatch.New[map[string]time.Time]() - go comparisonGoRoutine(gddReq, request.PlantDate, stageMatches, request.Comparison, comparisonThread) + + gdds, cfs := asyncCollectGddsAndCfs(ctx, gddReq) + out := forecast(ctx, gddReq, request.PlantDate, stageMatches, gdds, cfs) // block, wait for comparison results