From 80e477bc260fbf4edf460060e7593333d2b027c9 Mon Sep 17 00:00:00 2001
From: brli <brita.linnestad@nibio.no>
Date: Fri, 22 Mar 2024 09:18:24 +0100
Subject: [PATCH] Differentiate MSC-calculation for VekstStart before or after
 April 7th

---
 pom.xml                                       |  2 +-
 .../RoughageNutritionModelImpl.java           | 62 ++++++++++++++++---
 2 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/pom.xml b/pom.xml
index e39697f..a26070c 100755
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
 
     <groupId>no.nibio.vips.model</groupId>
     <artifactId>RoughageNutritionModel</artifactId>
-    <version>1.1.10-SNAPSHOT</version>
+    <version>1.1.11-SNAPSHOT</version>
     <packaging>jar</packaging>
 
     <dependencies>
diff --git a/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelImpl.java b/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelImpl.java
index 5424769..2c11a7c 100755
--- a/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelImpl.java
+++ b/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelImpl.java
@@ -755,7 +755,25 @@ public class RoughageNutritionModelImpl implements CostFunction {
         return wUtil.normalizeToExactDate(cal.getTime(), timeZone);
 
     }
+    
+    /**
+     * @return
+     * @throws ModelExcecutionException 
+     */
+    private Date getSyvendeApril() throws ModelExcecutionException
+    {
+        // Calculating start and end date of weather data retrieval
+        // Start is April 1st
+        Calendar cal = Calendar.getInstance(timeZone);
+        cal.setTime(this.getVekstStart());
+        cal.set(Calendar.MONTH, Calendar.APRIL);
+        cal.set(Calendar.DATE, 7);
+
+        WeatherUtil wUtil = new WeatherUtil();
+        return wUtil.normalizeToExactDate(cal.getTime(), timeZone);
 
+    }
+    
     /**
      * Løper gjennom eksisterende klimadatasett og beregner utvikling
      * (MSC_BEREGNET) Dette trenger man bare å gjøre en gang pr. modellberegning
@@ -779,16 +797,46 @@ public class RoughageNutritionModelImpl implements CostFunction {
 
             //Setter Ts1
             // At første april (og ikke f.eks. vekststart) er grense her, er pr. 2023-09 til vurdering av Anne Kjersti Bakken
-            if (temperatur.getTimeMeasured().compareTo(this.getForsteApril()) >= 0)
+            // Endret fra mars 2024 etter innspill fra Anne Kjersti Bakken:
+            // Dersom vekststart er før 7 april akkumuleres temperatursum fra vekststart
+            // Dersom vekstart er 7 april eller senere, akkumuleres temperatursum fra 1 april
+            // Dvs at 
+            // if (temperatur.getTimeMeasured().compareTo(this.getForsteApril()) >= 0)
+            // {
+            //      if (firstTemperaturSum) {
+            //          this.AIBakgrunnsdataMatrise.setParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1, temperatur.getValue());
+            //          firstTemperaturSum = false;
+            //      } else {
+            //          this.AIBakgrunnsdataMatrise.setParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1, temperatur.getValue() + this.AIBakgrunnsdataMatrise.getParamDoubleValueForDate(forrigeDato, AIDataMatrix.TS1));
+            //      }
+            // }
+            // fra mars 2024 er byttet ut med
+            
+            if (this.getVekstStart().compareTo(this.getSyvendeApril()) >= 0 )
             {
-                if (firstTemperaturSum) {
-                    this.AIBakgrunnsdataMatrise.setParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1, temperatur.getValue());
-                    firstTemperaturSum = false;
-                } else {
-                    this.AIBakgrunnsdataMatrise.setParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1, temperatur.getValue() + this.AIBakgrunnsdataMatrise.getParamDoubleValueForDate(forrigeDato, AIDataMatrix.TS1));
+                if (temperatur.getTimeMeasured().compareTo(this.getForsteApril()) >= 0)
+                {
+                    if (firstTemperaturSum) {
+                        this.AIBakgrunnsdataMatrise.setParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1, temperatur.getValue());
+                        firstTemperaturSum = false;
+                    } else {
+                        this.AIBakgrunnsdataMatrise.setParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1, temperatur.getValue() + this.AIBakgrunnsdataMatrise.getParamDoubleValueForDate(forrigeDato, AIDataMatrix.TS1));
+                    }
                 }
             }
-
+            else
+            {
+                if (temperatur.getTimeMeasured().compareTo(this.getVekstStart()) >= 0)
+                {
+                    if (firstTemperaturSum) {
+                        this.AIBakgrunnsdataMatrise.setParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1, temperatur.getValue());
+                        firstTemperaturSum = false;
+                    } else {
+                        this.AIBakgrunnsdataMatrise.setParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1, temperatur.getValue() + this.AIBakgrunnsdataMatrise.getParamDoubleValueForDate(forrigeDato, AIDataMatrix.TS1));
+                    }
+                }
+            }
+                      
             // Beregner DVR
             double DVR;
             if (temperatur.getTimeMeasured().compareTo(this.getVekstStart()) >= 0 && this.AIBakgrunnsdataMatrise.getParamDoubleValueForDate(temperatur.getTimeMeasured(), AIDataMatrix.TS1) > 150) {
-- 
GitLab