Skip to content
Snippets Groups Projects
Commit 97280c9b authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

First commit

parents
Branches
Tags
No related merge requests found
Showing
with 1363 additions and 0 deletions
target/
classes/
*~
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.license>nibio_open_source_license.ftl</netbeans.hint.license>
</properties>
</project-shared-configuration>
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>no.bioforsk.vips.model</groupId>
<artifactId>RoughageNutritionModel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>no.nibio.vips.common</groupId>
<artifactId>VIPSCommon</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>commons-math</groupId>
<artifactId>commons-math</artifactId>
<version>1.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.bioforsk.vips.model.roughagenutritionmodel;
import no.nibio.vips.util.DateMap;
/**
* Keeping track of data used for calculating AI index
* <br/>
* &copy; 2009-2015 NIBIO
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class AIDataMatrix extends DateMap{
public final static String MSC_BEREGNET = "MSC_BEREGNET";
public final static String MSC_REGISTRERT = "MSC_REGISTRERT";
public final static String TS1 = "TS1";
public final static String DVR = "DVR";
/** Creates a new instance of GrovforModellAIBakgrunnsdataMatriseBO */
public AIDataMatrix()
{
super();
}
}
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.bioforsk.vips.model.roughagenutritionmodel;
import no.nibio.vips.util.DateMap;
/**
* Matrisedataobjekt for � holde styr p� alle data som brukes under beregning av FEM-verdi
* i grovformodellen
* <br/>
* &copy; 2009-2015 NIBIO
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class FEmDataMatrix extends DateMap{
public final static String TS2 = "TS2";
public final static String FEM = "FEM";
/** Creates a new instance of GrovforModellNDFBakgrunnsdataMatriseBO */
public FEmDataMatrix()
{
super();
}
}
package no.bioforsk.vips.model.roughagenutritionmodel;
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
import no.nibio.vips.util.DateMap;
/**
* Matrisedataobjekt for � holde styr p� alle data som brukes under beregning av INDF-indeks
* i grovformodellen
* <br/>
* &copy; 2009-2015 NIBIO
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class INDFDataMatrix extends DateMap{
public final static String TS1 = "TS1";
public final static String INDF = "INDF";
/** Creates a new instance of INDFDataMatrix */
public INDFDataMatrix()
{
super();
}
}
package no.bioforsk.vips.model.roughagenutritionmodel;
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
import no.nibio.vips.util.DateMap;
/**
* Matrisedataobjekt for � holde styr p� alle data som brukes under beregning av NDF-indeks
* i grovformodellen
* <br/>
* &copy; 2009-2015 NIBIO
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class NDFDataMatrix extends DateMap{
public final static String TS1 = "TS1";
public final static String NDF = "NDF";
/** Creates a new instance of GrovforModellNDFBakgrunnsdataMatriseBO */
public NDFDataMatrix()
{
super();
}
}
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.bioforsk.vips.model.roughagenutritionmodel;
import java.util.Date;
/**
* Dataobjekt for lagring av inndata for optimeringsrutinene
* &copy; 2009-2015 NIBIO
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class OptimizationObservation implements Comparable{
private Date date;
private float hoyde;
private float utviklingstrinn;
private float NDF;
private float INDF;
private float raaprotein;
private float FEm;
public OptimizationObservation()
{
}
/**
* Bruker formelen Avling (kg TS/daa) = 34.86 * 0.017h<sup>2</sup> + 10.21h
* @return
*/
public float getAvling()
{
return 34.86f + 0.017f * (float) Math.pow(this.getHoyde(), 2) + 10.21f * this.getHoyde();
}
@Override
public int compareTo(Object o) {
if(! (o instanceof OptimizationObservation))
return 0;
else
return this.compareTo((OptimizationObservation) o);
}
public int compareTo(OptimizationObservation other)
{
return this.getDate().compareTo(other.getDate());
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
/**
* @return the hoyde
*/
public float getHoyde() {
return hoyde;
}
/**
* @param hoyde the hoyde to set
*/
public void setHoyde(float hoyde) {
this.hoyde = hoyde;
}
/**
* @return the utviklingstrinn
*/
public float getUtviklingstrinn() {
return utviklingstrinn;
}
/**
* @param utviklingstrinn the utviklingstrinn to set
*/
public void setUtviklingstrinn(float utviklingstrinn) {
this.utviklingstrinn = utviklingstrinn;
}
/**
* @return the NDF
*/
public float getNDF() {
return NDF;
}
/**
* @param NDF the NDF to set
*/
public void setNDF(float NDF) {
this.NDF = NDF;
}
/**
* @return the INDF
*/
public float getINDF() {
return INDF;
}
/**
* @param INDF the INDF to set
*/
public void setINDF(float INDF) {
this.INDF = INDF;
}
/**
* @return the raaprotein
*/
public float getRaaprotein() {
return raaprotein;
}
/**
* @param raaprotein the raaprotein to set
*/
public void setRaaprotein(float raaprotein) {
this.raaprotein = raaprotein;
}
/**
* @return the FEm
*/
public float getFEm() {
return FEm;
}
/**
* @param FEm the FEm to set
*/
public void setFEm(float FEm) {
this.FEm = FEm;
}
}
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.bioforsk.vips.model.roughagenutritionmodel;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import no.nibio.vips.util.DateMap;
/**
* Matrisedataobjekt som lagrer alle resultat fra beregning av grovformodellen
* <br/>
* &copy; 2009-2015 NIBIO
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class ResultMatrix extends DateMap{
//public final static String V = "V";
public final static String AVLING = "Avling";
private final String AVLING_DECIMALFORMAT_PATTERN = "###";
//public final static String DVR = "DVR";
public final static String MSC_BEREGNET = "Beregnet MSC";
private final String MSC_DECIMALFORMAT_PATTERN = "###.##";
public final static String NDF = "NDF";
private final String NDF_DECIMALFORMAT_PATTERN = "###.#";
public final static String INDF = "INDF";
private final String INDF_DECIMALFORMAT_PATTERN = "###.#";
public final static String RAAPROTEIN = "R&aring;protein";
private final String RAAPROTEIN_DECIMALFORMAT_PATTERN = "###.#";
public final static String FEM = "FEm";
private final String FEM_DECIMALFORMAT_PATTERN = "###.##";
private final String DEFAULT_DECIMALFORMAT_PATTERN ="###.##";
private HashMap<String, DecimalFormat> decimalFormats;
// Optimerte parametre
private boolean optimeringForetatt = false;
private Double alfa;
private Double RmFoersteslaatt;
private Double RmAndreslaatt;
private Double NRmFoersteslaatt;
private Double NRmAndreslaatt;
private Double cFoersteslaatt;
private Double IRmFoersteslaatt;
//private Float betaINDF;
private Double pINDF;
private Double FEmj;
private Double FEmm;
/** Creates a new instance of GrovforModellResultatMatriseBO */
public ResultMatrix()
{
super();
initDecimalFormats();
}
private void initDecimalFormats()
{
this.decimalFormats = new HashMap();
this.decimalFormats.put(ResultMatrix.AVLING, new DecimalFormat(this.AVLING_DECIMALFORMAT_PATTERN));
this.decimalFormats.put(ResultMatrix.FEM, new DecimalFormat(this.FEM_DECIMALFORMAT_PATTERN));
this.decimalFormats.put(ResultMatrix.INDF, new DecimalFormat(this.INDF_DECIMALFORMAT_PATTERN));
this.decimalFormats.put(ResultMatrix.MSC_BEREGNET, new DecimalFormat(this.MSC_DECIMALFORMAT_PATTERN));
this.decimalFormats.put(ResultMatrix.NDF, new DecimalFormat(this.NDF_DECIMALFORMAT_PATTERN));
this.decimalFormats.put(ResultMatrix.RAAPROTEIN, new DecimalFormat(this.RAAPROTEIN_DECIMALFORMAT_PATTERN));
this.decimalFormats.put("DEFAULT", new DecimalFormat(this.DEFAULT_DECIMALFORMAT_PATTERN));
}
private DecimalFormat getDecimalFormat(String paramName)
{
return this.decimalFormats.get(paramName) != null ? this.decimalFormats.get(paramName) : this.decimalFormats.get("DEFAULT");
}
/**
* Returns a formatted string representing the number
* The format is decided internally in this object
* @param date
* @param paramName
* @return
*/
public String getDefaultFormattedDoubleValue(Date date, String paramName)
{
Double value = this.getParamDoubleValueForDate(date, paramName);
return this.getDecimalFormat(paramName).format(value);
}
/**
* @return the alfa
*/
public Double getAlfa() {
return alfa;
}
/**
* @param alfa the alfa to set
*/
public void setAlfa(Double alfa) {
this.alfa = alfa;
}
/**
* @return the RmFoersteslaatt
*/
public Double getRmFoersteslaatt() {
return RmFoersteslaatt;
}
/**
* @param RmFoersteslaatt the RmFoersteslaatt to set
*/
public void setRmFoersteslaatt(Double RmFoersteslaatt) {
this.RmFoersteslaatt = RmFoersteslaatt;
}
/**
* @return the RmAndreslaatt
*/
public Double getRmAndreslaatt() {
return RmAndreslaatt;
}
/**
* @param RmAndreslaatt the RmAndreslaatt to set
*/
public void setRmAndreslaatt(Double RmAndreslaatt) {
this.RmAndreslaatt = RmAndreslaatt;
}
/**
* @return the NRmFoersteslaatt
*/
public Double getNRmFoersteslaatt() {
return NRmFoersteslaatt;
}
/**
* @param NRmFoersteslaatt the NRmFoersteslaatt to set
*/
public void setNRmFoersteslaatt(Double NRmFoersteslaatt) {
this.NRmFoersteslaatt = NRmFoersteslaatt;
}
/**
* @return the NRmAndreslaatt
*/
public Double getNRmAndreslaatt() {
return NRmAndreslaatt;
}
/**
* @param NRmAndreslaatt the NRmAndreslaatt to set
*/
public void setNRmAndreslaatt(Double NRmAndreslaatt) {
this.NRmAndreslaatt = NRmAndreslaatt;
}
/**
* @return the cFoersteslaatt
*/
public Double getCFoersteslaatt() {
return cFoersteslaatt;
}
/**
* @param cFoersteslaatt the cFoersteslaatt to set
*/
public void setCFoersteslaatt(Double cFoersteslaatt) {
this.cFoersteslaatt = cFoersteslaatt;
}
/**
* @return the IRmFoersteslaatt
*/
public Double getIRmFoersteslaatt() {
return IRmFoersteslaatt;
}
/**
* @param IRmFoersteslaatt the IRmFoersteslaatt to set
*/
public void setIRmFoersteslaatt(Double IRmFoersteslaatt) {
this.IRmFoersteslaatt = IRmFoersteslaatt;
}
/**
* @return the betaINDF
*
public Float getBetaINDF() {
return betaINDF;
}
/**
* @param betaINDF the betaINDF to set
*
public void setBetaINDF(Float betaINDF) {
this.betaINDF = betaINDF;
}*/
/**
* @return the optimeringForetatt
*/
public boolean isOptimeringForetatt() {
return optimeringForetatt;
}
/**
* @param optimeringForetatt the optimeringForetatt to set
*/
public void setOptimeringForetatt(boolean optimeringForetatt) {
this.optimeringForetatt = optimeringForetatt;
}
/**
* @return the FEmj
*/
public Double getFEmj() {
return FEmj;
}
/**
* @param FEmj the FEmj to set
*/
public void setFEmj(Double FEmj) {
this.FEmj = FEmj;
}
/**
* @return the pINDF
*/
public Double getpINDF() {
return pINDF;
}
/**
* @param pINDF the pINDF to set
*/
public void setpINDF(Double pINDF) {
this.pINDF = pINDF;
}
/**
* @return the FEmm
*/
public Double getFEmm() {
return FEmm;
}
/**
* @param FEmm the FEmm to set
*/
public void setFEmm(Double FEmm) {
this.FEmm = FEmm;
}
}
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.bioforsk.vips.model.roughagenutritionmodel;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import no.nibio.vips.entity.ModelConfiguration;
import no.nibio.vips.entity.Result;
import no.nibio.vips.entity.ResultImpl;
import no.nibio.vips.entity.WeatherObservation;
import no.nibio.vips.model.ConfigValidationException;
import no.nibio.vips.model.Model;
import no.nibio.vips.model.ModelExcecutionException;
import no.nibio.vips.model.ModelId;
import no.nibio.vips.util.InvalidAggregationTypeException;
import no.nibio.vips.util.WeatherElements;
import no.nibio.vips.util.WeatherObservationListException;
import no.nibio.vips.util.WeatherUtil;
/**
* @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class RoughageNutritionModel implements Model {
public final static ModelId MODEL_ID = new ModelId("ROUGHAGENU");
private TimeZone timeZone;
public final static int JORDTYPE_LEIR = 1;
public final static int JORDTYPE_SAND = 2;
public final static int JORDTYPE_SILT = 3;
public final static int ENGSAMMENSETNING_KLOEVERANDEL_LITEN = 1;
public final static int ENGSAMMENSETNING_KLOEVERANDEL_MEDIUM = 2;
public final static int ENGSAMMENSETNING_KLOEVERANDEL_STOR = 3;
private List<WeatherObservation> TM;
private List<WeatherObservation> TJM10;
private List<WeatherObservation> RR;
private List<WeatherObservation> EPP;
private List<WeatherObservation> Q0;
private List<WeatherObservation> TMD;
private List<WeatherObservation> TJM10D;
private List<WeatherObservation> RRD;
private List<WeatherObservation> Q0D;
private List<WeatherObservation> EPPD;
private Date firstHarvest;
private Date secondHarvest;
private Integer soilType;
private Integer cloverShare;
private List<OptimizationObservation> optimizationObservations;
@Override
public List<Result> getResult() throws ModelExcecutionException {
RoughageNutritionModelImpl impl = new RoughageNutritionModelImpl();
ResultMatrix resultMatrix = impl.beregnModell(TMD, TJM10D, RRD, EPPD, Q0D, firstHarvest, secondHarvest, soilType, cloverShare, optimizationObservations, timeZone);
List<Result> results = new ArrayList<>();
// Get april first of the current year
Calendar cal = Calendar.getInstance(this.timeZone);
cal.setTime(firstHarvest);
cal.set(Calendar.MONTH, Calendar.APRIL);
cal.set(Calendar.DATE, 1);
Date aprilFirst = new WeatherUtil().normalizeToExactDate(cal.getTime(), timeZone);
// Get all labels
//Set labels = resultMatrix.getValueLabelsForDate(aprilFirst);
Date resultDate = aprilFirst;
// Iterate all dates in matrix, starting April 1st
while(resultMatrix.getValueLabelsForDate(resultDate) != null)
{
Result result = new ResultImpl();
result.setResultValidTime(resultDate);
for(String label:resultMatrix.getValueLabelsForDate(resultDate))
{
result.setValue(this.getModelId().toString(), label, resultMatrix.getDefaultFormattedDoubleValue(resultDate, label));
}
cal.setTime(resultDate);
cal.add(Calendar.DATE, 1);
resultDate = cal.getTime();
results.add(result);
}
return results;
}
@Override
public ModelId getModelId() {
return RoughageNutritionModel.MODEL_ID;
}
@Override
public String getModelName() {
return this.getModelName(Model.DEFAULT_LANGUAGE);
}
@Override
public String getModelName(String language) {
return "Grovfôrmodellen";
}
@Override
public String getLicense() {
return "NIBIO Open Source License";
}
@Override
public String getCopyright() {
return "NIBIO";
}
@Override
public String getModelDescription() {
return this.getModelDescription(Model.DEFAULT_LANGUAGE);
}
@Override
public String getModelDescription(String language) {
return "TODO";
}
@Override
public String getWarningStatusInterpretation() {
return this.getWarningStatusInterpretation(Model.DEFAULT_LANGUAGE);
}
@Override
public String getWarningStatusInterpretation(String language) {
return "TODO";
}
@Override
public String getModelUsage() {
return this.getModelName(Model.DEFAULT_LANGUAGE);
}
@Override
public String getModelUsage(String language) {
return "TODO";
}
@Override
public String getSampleConfig() {
return "{\n" +
"\t\"loginInfo\":{\n" +
"\t\t\"username\":\"example\",\n" +
"\t\t\"password\":\"example\"\n" +
"\t},\n" +
"\t\"modelId\":\"" + MODEL_ID.toString() + "\",\n" +
"\t\"configParameters\":{\n" +
"\t\t\"timeZone\":\"Europe/Oslo\",\n" +
"\t\t\"firstHarvest\":\"2015-06-01\",\n" +
"\t\t\"secondHarvest\":\"2015-08-01\",\n" +
"\t\t\"soilType\":1,\n" +
"\t\t\"cloverShare\":2,\n" +
"\t\t\"observations\":[\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2015-04-01T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"TM\",\n" +
"\t\t\t\t\"logIntervalId\":2,\n" +
"\t\t\t\t\"value\":10.9\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2012-04-01T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"TJM10\",\n" +
"\t\t\t\t\"logIntervalId\":2,\n" +
"\t\t\t\t\"value\":2.9\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2012-04-01T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"RR\",\n" +
"\t\t\t\t\"logIntervalId\":2,\n" +
"\t\t\t\t\"value\":1.7\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2012-04-25T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"EPP\",\n" +
"\t\t\t\t\"logIntervalId\":2,\n" +
"\t\t\t\t\"value\":0.5\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2012-04-01T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"Q0\",\n" +
"\t\t\t\t\"logIntervalId\":2,\n" +
"\t\t\t\t\"value\":10.7\n" +
"\t\t},\n" +
"\t\t]\n" +
"\t}\n" +
"}\n";
}
@Override
public void setConfiguration(ModelConfiguration config) throws ConfigValidationException {
/*
// Daily values
List<WeatherObservation> luftTemperatur, TM
List<WeatherObservation> jordTemperatur, TJM10
List<WeatherObservation> nedboer, RR
List<WeatherObservation> potensiellFordamping, EPP
List<WeatherObservation> globalStraaling, Q0
Date foersteslaatt,
Date andreslaatt,
int jordtype,
int kloeverandel,
List<OptimizationObservation> observasjoner
*/
// Hourly data
this.TM = new ArrayList<>();
this.TJM10 = new ArrayList<>();
this.RR = new ArrayList<>();
this.EPP = new ArrayList<>();
this.Q0 = new ArrayList<>();
this.TMD = new ArrayList<>();
this.TJM10D = new ArrayList<>();
this.RRD = new ArrayList<>();
this.EPPD = new ArrayList<>();
this.Q0D = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
// Set time zone
if(config.getConfigParameter("timeZone") != null)
{
this.timeZone = TimeZone.getTimeZone((String) config.getConfigParameter("timeZone"));
}
else
{
throw new ConfigValidationException("Missing timezone information");
}
if(config.getConfigParameter("firstHarvest") != null)
{
this.firstHarvest = mapper.convertValue(config.getConfigParameter("firstHarvest"), Date.class);
}
else
{
throw new ConfigValidationException("Missing date of first harvest");
}
if(config.getConfigParameter("secondHarvest") != null)
{
this.secondHarvest = mapper.convertValue(config.getConfigParameter("secondHarvest"), Date.class);
}
if(config.getConfigParameter("soilType") != null)
{
this.soilType = mapper.convertValue(config.getConfigParameter("soilType"), Integer.class);
}
if(config.getConfigParameter("cloverShare") != null)
{
this.cloverShare = mapper.convertValue(config.getConfigParameter("cloverShare"), Integer.class);
}
if(config.getConfigParameter("optimizationObsevations") != null)
{
this.optimizationObservations = mapper.convertValue(config.getConfigParameter("optimizatioObservations"), new TypeReference<List<OptimizationObservation>>(){});
}
List<WeatherObservation> observations = mapper.convertValue(config.getConfigParameter("observations"), new TypeReference<List<WeatherObservation>>(){});
for(WeatherObservation o:observations)
{
//System.out.println(o);
switch(o.getElementMeasurementTypeId())
{
case WeatherElements.TEMPERATURE_MEAN:
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H))
{
TM.add(o);
}
else if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1D))
{
TMD.add(o);
}
break;
case WeatherElements.SOIL_TEMPERATURE_10CM_MEAN:
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H))
{
TJM10.add(o);
}
else if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1D))
{
TJM10D.add(o);
}
break;
case WeatherElements.PRECIPITATION:
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H))
{
RR.add(o);
}
else if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1D))
{
RRD.add(o);
}
break;
case WeatherElements.POTENTIAL_EVAPORATION:
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H))
{
EPP.add(o);
}
else if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1D))
{
EPPD.add(o);
}
break;
case WeatherElements.GLOBAL_RADIATION:
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H))
{
Q0.add(o);
}
else if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1D))
{
Q0D.add(o);
}
break;
default:
// Keep calm and continue importing data
break;
}
}
// Fallback: Using hourly values to calculate dailies
if(TMD == null || TMD.isEmpty())
{
try {
List<WeatherObservation> dailyMeanTemperatures = new WeatherUtil().getAggregatedDailyValues(TM,
this.timeZone,
15,
WeatherUtil.AGGREGATION_TYPE_AVERAGE, 1);
if(dailyMeanTemperatures != null)
{
this.TMD = dailyMeanTemperatures;
}
} catch (WeatherObservationListException | InvalidAggregationTypeException ex) {
throw new ConfigValidationException(ex.getMessage());
}
}
if(TJM10D == null || TJM10D.isEmpty())
{
try {
List<WeatherObservation> dailyMeanSoilTemperatures = new WeatherUtil().getAggregatedDailyValues(TJM10,
this.timeZone,
15,
WeatherUtil.AGGREGATION_TYPE_AVERAGE, 1);
if(dailyMeanSoilTemperatures != null)
{
this.TJM10D = dailyMeanSoilTemperatures;
}
} catch (WeatherObservationListException | InvalidAggregationTypeException ex) {
throw new ConfigValidationException(ex.getMessage());
}
}
if(RRD == null || RRD.isEmpty())
{
try {
List<WeatherObservation> dailyPrecipitation = new WeatherUtil().getAggregatedDailyValues(RR,
this.timeZone,
15,
WeatherUtil.AGGREGATION_TYPE_SUM, 1);
if(dailyPrecipitation != null)
{
this.RRD = dailyPrecipitation;
}
} catch (WeatherObservationListException | InvalidAggregationTypeException ex) {
throw new ConfigValidationException(ex.getMessage());
}
}
if(EPPD == null || EPPD.isEmpty())
{
try {
List<WeatherObservation> dailyEvap = new WeatherUtil().getAggregatedDailyValues(EPP,
this.timeZone,
15,
WeatherUtil.AGGREGATION_TYPE_SUM, 1);
if(dailyEvap != null)
{
this.EPPD = dailyEvap;
}
} catch (WeatherObservationListException | InvalidAggregationTypeException ex) {
throw new ConfigValidationException(ex.getMessage());
}
}
if(Q0D == null || Q0D.isEmpty())
{
try {
List<WeatherObservation> dailyRad = new WeatherUtil().getAggregatedDailyValues(Q0,
this.timeZone,
15,
WeatherUtil.AGGREGATION_TYPE_SUM_GLOBAL_RADIATION, 1);
if(dailyRad != null)
{
this.Q0D = dailyRad;
}
} catch (WeatherObservationListException | InvalidAggregationTypeException ex) {
throw new ConfigValidationException(ex.getMessage());
}
}
}
}
package no.bioforsk.vips.model.roughagenutritionmodel;
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
import no.nibio.vips.util.DateMap;
/**
* Matrisedataobjekt for � holde styr p� alle data som brukes under beregning av VI-indeks
* i grovformodellen
* <br/>
* &copy; 2009-2015 NIBIO
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class VIDataMatrix extends DateMap{
public final static String TTV = "TTV";
public final static String LTV = "LTV";
public final static String AKTUELL_FORDAMPING = "EA";
public final static String POTENSIELL_FORDAMPING = "EP";
/** Creates a new instance of GrovforModellVIBakgrunnsdataMatriseBO */
public VIDataMatrix()
{
super();
}
}
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.bioforsk.vips.model.roughagenutritionmodel;
import no.nibio.vips.util.DateMap;
/**
* Matrisedataobjekt for � holde styr p� alle data som brukes under beregning av Avling
* i grovformodellen
* <br/>
* &copy; 2009-2015 NIBIO.no
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class YieldDataMatrix extends DateMap{
public final static String LAI = "LAI";
public final static String AVLING = "AVLING";
public final static String V = "V";
public final static String TS1 = "TS1";
public final static String CPOT = "CPOT";
/** Creates a new instance of GrovforModellAIBakgrunnsdataMatriseBO */
public YieldDataMatrix()
{
super();
}
}
/*
* Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
*
* This file is part of RoughageNutritionModel.
* RoughageNutritionModel is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* RoughageNutritionModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with RoughageNutritionModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.bioforsk.vips.model.roughagenutritionmodel;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.TimeZone;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import no.nibio.vips.entity.ModelConfiguration;
import no.nibio.vips.entity.Result;
import no.nibio.vips.entity.WeatherObservation;
import no.nibio.vips.model.ConfigValidationException;
import no.nibio.vips.model.ModelExcecutionException;
import no.nibio.vips.model.ModelId;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author treinar
*/
public class RoughageNutritionModelTest {
public RoughageNutritionModelTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getResult method, of class RoughageNutritionModel.
*/
@Test
public void testGetResult() throws Exception {
System.out.println("getResult");
try
{
ModelConfiguration config = this.getConfiguration("/weatherdata_boe_2015.json");
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2015, Calendar.JUNE, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND,0);
config.setConfigParameter("timeZone","Europe/Oslo");
config.setConfigParameter("firstHarvest", cal.getTime());
config.setConfigParameter("soilType", RoughageNutritionModel.JORDTYPE_SAND);
config.setConfigParameter("cloverShare", RoughageNutritionModel.ENGSAMMENSETNING_KLOEVERANDEL_LITEN);
RoughageNutritionModel instance = new RoughageNutritionModel();
instance.setConfiguration(config);
//List<Result> expResult = null;
List<Result> result = instance.getResult();
//assertEquals(expResult, result);
assertNotNull(result);
/*Collections.sort(result);
//System.out.println("Number of result objects: " + result.size());
for(Result r:result)
{
System.out.println(r);
}*/
}
catch(ConfigValidationException | ModelExcecutionException ex)
{
ex.printStackTrace();
fail(ex.getMessage());
}
}
/**
/**
* Test of getModelId method, of class RoughageNutritionModel.
*/
@Test
public void testGetModelId() {
System.out.println("getModelId");
RoughageNutritionModel instance = new RoughageNutritionModel();
ModelId expResult = new ModelId( "ROUGHAGENU");
ModelId result = instance.getModelId();
assertEquals(expResult.toString(), result.toString());
}
/**
* Test of setConfiguration method, of class RoughageNutritionModel.
*/
@Test
public void testSetConfiguration() throws Exception {
System.out.println("setConfiguration");
try
{
ModelConfiguration config = this.getConfiguration("/weatherdata_boe_2015.json");
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2015, Calendar.JUNE, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND,0);
config.setConfigParameter("firstHarvest", cal.getTime());
config.setConfigParameter("soilType", RoughageNutritionModel.JORDTYPE_SAND);
config.setConfigParameter("cloverShare", RoughageNutritionModel.ENGSAMMENSETNING_KLOEVERANDEL_LITEN);
RoughageNutritionModel instance = new RoughageNutritionModel();
instance.setConfiguration(config);
}
catch(ConfigValidationException ex)
{
fail(ex.getMessage());
}
}
private ModelConfiguration getConfiguration(String fileName)
{
try {
ModelConfiguration config = new ModelConfiguration();
config.setModelId(RoughageNutritionModel.MODEL_ID.toString());
config.setConfigParameter("timeZone", "Europe/Oslo");
BufferedInputStream inputStream = new BufferedInputStream(this.getClass().getResourceAsStream(fileName));
JsonFactory f = new MappingJsonFactory();
JsonParser jp = f.createParser(inputStream);
JsonNode all = jp.readValueAsTree();
//List<WeatherObservation> observations = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
List<WeatherObservation> observations = mapper.convertValue(all, new TypeReference<List<WeatherObservation>>(){});
config.setConfigParameter("observations", observations);
return config;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment