diff --git a/services/forecast_service.go b/services/forecast_service.go index 569a7bf0befbc38c0cbbc6dd07760c4a9bcdc513..f7a4bb6790c8bd4fffe5d16e03c2bf6f8da3cdaf 100644 --- a/services/forecast_service.go +++ b/services/forecast_service.go @@ -37,7 +37,8 @@ func getDatesForCfsMatches(cfs [][]float64, lastDateInt int, currentMatch int, k lastDist := matches[keys[currentMatch]] tempCMatch := currentMatch for i, v := range c { - dist := math.Abs(matches[keys[currentMatch]] - v) + + dist := math.Abs(matches[keys[tempCMatch]] - v) // 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 { @@ -49,6 +50,7 @@ func getDatesForCfsMatches(cfs [][]float64, lastDateInt int, currentMatch int, k if tempCMatch >= len(keys) { break } + dist = matches[keys[tempCMatch]] } lastDist = dist } @@ -120,41 +122,42 @@ func forecast(ctx common.DawnCtx, gddReq models.GddRequest, plantdate time.Time, // start at plantdate, begin accumulating OBSERVED until we run out // since this observed, we should only have 1 bin with a value of 100% // at the end, we should have all observed stages along with all accumulated gdds of the planting year - observedValues := observed.GddValues[start:] accumulatedGdds := 0.0 - lastDist := matches[keys[currentMatch]] date := 0 - for i := 0; i < len(observedValues); i++ { - date = i - dist := math.Abs(matches[keys[currentMatch]] - accumulatedGdds) - // 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 { - out[keys[currentMatch]] = &models.Bins{ - Bins: []models.Bin{ - { - Value: 1, - Date: plantdate.AddDate(0, 0, i-1), + + if start < len(observed.GddValues) { + observedValues := observed.GddValues[start:] + lastDist := matches[keys[currentMatch]] + for i := 0; i < len(observedValues); i++ { + date = i + dist := math.Abs(matches[keys[currentMatch]] - accumulatedGdds) + // 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 { + out[keys[currentMatch]] = &models.Bins{ + Bins: []models.Bin{ + { + Value: 1, + Date: plantdate.AddDate(0, 0, i-1), + }, }, - }, - } - currentMatch += 1 - if currentMatch >= len(keys) { - break + } + currentMatch += 1 + if currentMatch >= len(keys) { + break + } + dist = matches[keys[currentMatch]] } - dist = matches[keys[currentMatch]] + accumulatedGdds += observedValues[i] + lastDist = dist } - accumulatedGdds += observedValues[i] - lastDist = dist } if currentMatch == len(keys) { return out } - // adjust cfs values to start at the accumulated value adjustedCfs := calculateAccumulatedCfsBasedOnAccumulatedObserved(product, accumulatedGdds, cfs) cfsHist := getDatesForCfsMatches(adjustedCfs, date, currentMatch, keys, matches) - // this loop will actually build the 5 bins for k, v := range cfsHist { diff --git a/utils/dispatch/dispatch.go b/utils/dispatch/dispatch.go index aa4d2132787e437eec83765d9e56130d4a38ebb1..e80ae64fe6a58d7cbfa725b34b537e62cde4a372 100644 --- a/utils/dispatch/dispatch.go +++ b/utils/dispatch/dispatch.go @@ -18,8 +18,6 @@ Example: gddThread.Result(gdds) }() - gddThread.Wait() - gdds, err := gddThread.Get() if err != nil { panic(err)