diff --git a/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModel.java b/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModel.java index 25f96d14edb3c56713ba3df8868137c3da9a68a7..91da5f9bca6768e8d0a2d08d4bd2c8b94f4e68e2 100755 --- a/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModel.java +++ b/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModel.java @@ -73,6 +73,7 @@ public class RoughageNutritionModel implements Model { private Date firstHarvest; private Date secondHarvest; + private Date firstPossibleGrowthStartDate; private Integer soilType; private Integer cloverShare; private List<OptimizationObservation> optimizationObservations; @@ -90,6 +91,7 @@ public class RoughageNutritionModel implements Model { Q0D, firstHarvest, secondHarvest, + firstPossibleGrowthStartDate, soilType, cloverShare, optimizationObservations, @@ -246,7 +248,7 @@ public class RoughageNutritionModel implements Model { List<WeatherObservation> globalStraaling, Q0 Date foersteslaatt, Date andreslaatt, - Date tidligsteVekststart, + Date tidligsteVekststart, // Default: 1. mars int jordtype, int kloeverandel, List<OptimizationObservation> observasjoner @@ -289,6 +291,20 @@ public class RoughageNutritionModel implements Model { { this.secondHarvest = mapper.convertValue(config.getConfigParameter("secondHarvest"), Date.class); } + + if(config.getConfigParameter("firstPossibleGrowthStartDate") != null) + { + this.firstPossibleGrowthStartDate = mapper.convertValue(config.getConfigParameter("firstPossibleGrowthStartDate"), Date.class); + } + else + { + Calendar cal = Calendar.getInstance(); + cal.setTime(firstHarvest); + cal.set(Calendar.MONTH, Calendar.MARCH); + cal.set(Calendar.DATE, 1); + this.firstPossibleGrowthStartDate = cal.getTime(); + } + if(config.getConfigParameter("soilType") != null) { this.soilType = mapper.convertValue(config.getConfigParameter("soilType"), Integer.class); @@ -309,6 +325,8 @@ public class RoughageNutritionModel implements Model { + + List<WeatherObservation> observations = mapper.convertValue(config.getConfigParameter("observations"), new TypeReference<List<WeatherObservation>>(){}); for(WeatherObservation o:observations) 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 31fea1e9c8e23169908306dd0d9b058b3c0ef3eb..c2730bcf3be34e983396daed7412f86fb0fe4190 100755 --- a/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelImpl.java +++ b/src/main/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelImpl.java @@ -33,6 +33,7 @@ import java.util.TimeZone; import java.util.concurrent.LinkedBlockingQueue; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; import no.nibio.vips.entity.WeatherObservation; import no.nibio.vips.model.ModelExcecutionException; @@ -43,12 +44,15 @@ import org.apache.commons.math.optimization.CostException; import org.apache.commons.math.optimization.CostFunction; import org.apache.commons.math.optimization.NelderMead; import org.apache.commons.math.optimization.PointCostPair; +import org.slf4j.LoggerFactory; /** * @copyright 2018 <a href="http://www.nibio.no/">NIBIO</a> * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ public class RoughageNutritionModelImpl implements CostFunction { + + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(RoughageNutritionModelImpl.class); // Globale variable som må nullstilles for hver beregning av modellen private List<WeatherObservation> nedboerVerdier; @@ -67,6 +71,7 @@ public class RoughageNutritionModelImpl implements CostFunction { private Date datoFoersteSlaatt; private Date datoAndreSlaatt; + private Date foersteMuligeVekststartdato; private Integer jordtype; private Integer kloeverandel; @@ -135,6 +140,7 @@ public class RoughageNutritionModelImpl implements CostFunction { List<WeatherObservation> dailyGlobalRadiation, Date firstHarvest, Date secondHarvest, + Date firstPossibleGrowthStartDate, int soilType, int cloverShare, List<OptimizationObservation> optimizationObservations, @@ -150,6 +156,7 @@ public class RoughageNutritionModelImpl implements CostFunction { this.datoAndreSlaatt = secondHarvest; this.jordtype = soilType; this.kloeverandel = cloverShare; + this.foersteMuligeVekststartdato = firstPossibleGrowthStartDate; this.calculatedDateOfGrowthStart = null; this.timeZone = timeZone; @@ -456,6 +463,8 @@ public class RoughageNutritionModelImpl implements CostFunction { Integer dagNummer = 1; for (Iterator<WeatherObservation> tempI = this.luftTemperaturVerdier.iterator(); tempI.hasNext();) { temperatur = tempI.next(); + + // Beregner TS1 double TS1; @@ -1221,17 +1230,30 @@ public class RoughageNutritionModelImpl implements CostFunction { /** * - * @return @throws com.ac.march.ModelExcecutionException + * @return + * @throws no.nibio.vips.model.ModelExcecutionException */ protected Date getVekstStart() throws ModelExcecutionException { if (this.calculatedDateOfGrowthStart == null) { - this.calculatedDateOfGrowthStart = this.getVekstStart(this.luftTemperaturVerdier, this.jordTemperaturVerdier); + // Filtrer bort datoer før første mulige vekststartdato + if(this.foersteMuligeVekststartdato != null) + { + List<WeatherObservation> TMFiltrert = this.luftTemperaturVerdier.stream() + .filter(wo->wo.getTimeMeasured().compareTo(this.foersteMuligeVekststartdato) >=0 ) + .collect(Collectors.toList()); + List<WeatherObservation> TJMFiltrert = this.jordTemperaturVerdier.stream() + .filter(wo->wo.getTimeMeasured().compareTo(this.foersteMuligeVekststartdato) >=0 ) + .collect(Collectors.toList()); + this.calculatedDateOfGrowthStart = this.getVekstStart(TMFiltrert, TJMFiltrert); + LOGGER.debug("calculatedDateOfGrowthStart=" + this.calculatedDateOfGrowthStart); + } + } return this.calculatedDateOfGrowthStart; } /** - * Beregner datoen hvor vekststart antas å inntreffe. Vekststart antas å + * Beregner datoen hvor vekststart antas å inntreffe.Vekststart antas å * inntreffe etter tre påfølgende femdøgnsmiddel av temperatur > 5 grader * Celcius. Dvs. datoen blir den tredje av disse påfølgende femdøgnsmiddel. * Endring 2013-10-06: Hvis vi har jordtemperatur tilgjengelig, så må det @@ -1242,6 +1264,7 @@ public class RoughageNutritionModelImpl implements CostFunction { * @param jordTemperatur * @return Dato for vekststart, eller null hvis ikke betingelsene for * vekstart er oppfylt i de gitte klimadata + * @throws no.nibio.vips.model.ModelExcecutionException */ protected Date getVekstStart(Collection dognMiddelLuftTemperatur, Collection jordTemperatur) throws ModelExcecutionException { double lufttempTerskel = 5.0; diff --git a/src/test/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelTest.java b/src/test/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelTest.java index efc61196f058d65e47f147286845058d69e7068d..94073fd0ef76cf528b7c9170601484ca875833c6 100755 --- a/src/test/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelTest.java +++ b/src/test/java/no/bioforsk/vips/model/roughagenutritionmodel/RoughageNutritionModelTest.java @@ -49,7 +49,7 @@ import static org.junit.Assert.*; /** * - * @author treinar + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ public class RoughageNutritionModelTest { @@ -86,6 +86,9 @@ public class RoughageNutritionModelTest { cal.set(Calendar.MILLISECOND,0); config.setConfigParameter("timeZone","Europe/Oslo"); config.setConfigParameter("firstHarvest", cal.getTime()); + cal.set(Calendar.MONTH, Calendar.APRIL); + cal.set(Calendar.DATE, 15); + config.setConfigParameter("firstPossibleGrowthStartDate", cal.getTime()); config.setConfigParameter("soilType", RoughageNutritionModel.JORDTYPE_SAND); config.setConfigParameter("cloverShare", RoughageNutritionModel.ENGSAMMENSETNING_KLOEVERANDEL_LITEN); String[] optimizationInfoLines = {"2016-06-02,2,3,,,,"};