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

add anchoring

parent 73b7a8e4
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,8 @@ import (
// @Param plant_date query string true "Plant date, ISO8601 or RFC3339 format"
// @Param mode query string true "Mode, rm or gdds_to_maturity"
// @Param value query int true "Value of mode"
// @Param anchor_stage query string false "anchoring stage"
// @Param anchor_date query string false "anchoring date"
// @Router /gdd/forecast/stages [get]
func ForecastStages(c *fiber.Ctx) error {
ctx := common.DawnCtx{FiberCtx: c}
......
......@@ -12,11 +12,13 @@ import (
)
type StageRequest struct {
PlantDate time.Time `json:"plant_date"`
Mode string `json:"mode"`
Value int `json:"value"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
PlantDate time.Time `json:"plant_date"`
Mode string `json:"mode"`
Value int `json:"value"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
AnchorStage string `json:"anchor_stage"`
AnchorDate time.Time `json:"anchor_date"`
}
func BuildStageRequest(ctx common.DawnCtx) StageRequest {
......@@ -25,12 +27,22 @@ func BuildStageRequest(ctx common.DawnCtx) StageRequest {
value := ctx.FiberCtx.Query("value")
latitude := ctx.FiberCtx.Query("latitude")
longitude := ctx.FiberCtx.Query("longitude")
anchorStage := ctx.FiberCtx.Query("anchor_stage")
anchorDateString := ctx.FiberCtx.Query("anchor_date")
pd, err := time.Parse(time.RFC3339, plantDate)
if err != nil {
panic(config.BAD_REQUEST.PutDetail("reason", err.Error()))
}
anchorDate := time.Time{}
if anchorStage != "" {
anchorDate, err = time.Parse(time.RFC3339, anchorDateString)
if err != nil {
panic(config.BAD_REQUEST.PutDetail("reason", err.Error()))
}
}
modeLower := strings.ToLower(mode)
if modeLower != "rm" && modeLower != "gdds_to_maturity" {
panic(config.BAD_REQUEST.PutDetail("reason", "\""+mode+"\" is not valid"))
......@@ -52,11 +64,13 @@ func BuildStageRequest(ctx common.DawnCtx) StageRequest {
}
stageRequest := StageRequest{
PlantDate: pd,
Mode: modeLower,
Value: valueInt,
Latitude: latitudeFloat,
Longitude: longitudeFloat,
PlantDate: pd,
Mode: modeLower,
Value: valueInt,
Latitude: latitudeFloat,
Longitude: longitudeFloat,
AnchorDate: anchorDate,
AnchorStage: anchorStage,
}
e := stageRequest.Validate()
......@@ -113,7 +127,7 @@ func (s StageStateInner) ExtractDates(plantDate time.Time, start int) []time.Tim
return []time.Time{maxDate, meanDate, minDate}
}
func BuildStageMatches(mode string, value int) map[string]float64 {
func BuildStageMatches(mode string, value int, start int, fyData StageData, req StageRequest) map[string]float64 {
var harvestVal float64
if mode == "rm" {
harvestVal = float64(((float64(value) - 95.0) * 22.0) + 2375.0)
......@@ -121,6 +135,31 @@ func BuildStageMatches(mode string, value int) map[string]float64 {
harvestVal = float64(value)
}
if !req.AnchorDate.IsZero() {
mod := 1.0
switch req.AnchorStage {
case "emergence":
mod = 1.0 / 0.07
case "3LeafCollar":
mod = 1.0 / 0.13
case "6LeafCollar":
mod = 1.0 / 0.2
case "silk":
mod = 1.0 / 0.545
case "milk":
mod = 1.0 / 0.725
}
accMean := 0.0
for i := start; i < len(fyData.MaxGdds); i++ {
accMean += fyData.MeanGdds[i]
meanDate := req.PlantDate.AddDate(0, 0, i-start)
if req.AnchorDate == meanDate {
harvestVal = accMean * mod
break
}
}
}
return map[string]float64{
"emergence": harvestVal * 0.07,
"3LeafCollar": harvestVal * 0.13,
......
......@@ -60,7 +60,7 @@ func CalculateStages(ctx common.DawnCtx, request models.StageRequest) map[string
accMean := 0.0
state := map[string]models.StageStateInner{}
stageMatches := models.BuildStageMatches(request.Mode, request.Value)
stageMatches := models.BuildStageMatches(request.Mode, request.Value, start, fyData, request)
for i := start; i < len(fyData.MaxGdds); i++ {
accMin += fyData.MinGdds[i]
......
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