diff --git a/src/main/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModel.java b/src/main/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModel.java index 2a79914fd348eda167c9f708c226926fba176bac..7fbe9277891d1078e59ff1c78c3db36b22d81ac6 100644 --- a/src/main/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModel.java +++ b/src/main/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModel.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2013-2014 Bioforsk <http://www.bioforsk.no/>. + * + * This file is part of VIPS/NaerstadModel. + * VIPS/NaerstadModel is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VIPS/NaerstadModel 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with VIPS/NaerstadModel. If not, see <http://www.gnu.org/licenses/>. + * + */ package no.bioforsk.vips.model.naerstadmodel; import java.text.DecimalFormat; @@ -65,7 +83,6 @@ public class NaerstadModel extends I18nImpl implements Model{ { this.calculateNaerstadModellBakgroundData(); - this.backgroundData.toString(); List<Result> results = new ArrayList<>(); Calendar cal = Calendar.getInstance(); cal.setTime(this.calculationStart); @@ -75,33 +92,46 @@ public class NaerstadModel extends I18nImpl implements Model{ */ cal.add(Calendar.DATE, 1); DecimalFormat dFormat = new DecimalFormat("###.##"); - for(int j=0;j<4;j++) + //System.out.println("RESULTS: " + this.backgroundData.toString()); + Date thePresent = cal.getTime(); + while(thePresent.compareTo(calculationEnd) <= 0) { - Date dateStart = cal.getTime(); - cal.add(Calendar.DATE, 1); - Date dateEnd = cal.getTime(); - Date thePresent = dateStart; - while(thePresent.before(dateEnd)) + + Result result = new ResultImpl(); + result.setResultValidTime(thePresent); + Double RISK = this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.RISK); + // TODO: Find better way of terminating? + if(RISK == null) { - Result result = new ResultImpl(); - result.setResultValidTime(thePresent); - Double RISK = this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.RISK); - // TODO: Find better way of terminating? - if(RISK == null) - { - break; - } - if(this.DEBUG) - System.out.println("RISK(" + thePresent + ")=" + RISK); - result.setValue("RISK", dFormat.format(RISK)); - result.setWarningStatus(this.getWarningStatus(RISK)); - results.add(result); - - // Moving on - cal.setTime(thePresent); - cal.add(Calendar.HOUR_OF_DAY, 1); - thePresent = cal.getTime(); + break; } + if(this.DEBUG) + System.out.println("RISK(" + thePresent + ")=" + RISK); + result.setValue("RISK", dFormat.format(RISK)); + result.setValue("TSHH", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.TSHH))); + result.setValue("SPH", dFormat.format(this.backgroundData.getParamIntValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.SPH))); + result.setValue("VAS", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.VAS))); + result.setValue("VRS", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.VRS))); + result.setValue("WHS", dFormat.format(this.backgroundData.getParamIntValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.WHS))); + result.setValue("WD", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.WD))); + result.setValue("WH", dFormat.format(this.backgroundData.getParamIntValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.WH))); + result.setValue("IR", dFormat.format(this.backgroundData.getParamIntValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.IR))); + result.setWarningStatus(this.getWarningStatus(RISK)); + + // Adding weather data + result.setValue("TM", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.TM))); + result.setValue("UM", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.UM))); + result.setValue("RR", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.RR))); + result.setValue("Q0", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.Q0))); + result.setValue("BT", dFormat.format(this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.BT))); + + results.add(result); + + // Moving on + cal.setTime(thePresent); + cal.add(Calendar.HOUR_OF_DAY, 1); + thePresent = cal.getTime(); + } return results; @@ -318,6 +348,13 @@ public class NaerstadModel extends I18nImpl implements Model{ for(Integer i=0;i<TMliste.length;i++) { + // Storing the weather data + this.backgroundData.setParamDoubleValueForDate(TMliste[i].getTimeMeasured(), NaerstadModelBackgroundDataMatrix.TM, TMliste[i].getValue()); + this.backgroundData.setParamDoubleValueForDate(TMliste[i].getTimeMeasured(), NaerstadModelBackgroundDataMatrix.UM, UMliste[i].getValue()); + this.backgroundData.setParamDoubleValueForDate(TMliste[i].getTimeMeasured(), NaerstadModelBackgroundDataMatrix.RR, RRliste[i].getValue()); + this.backgroundData.setParamDoubleValueForDate(TMliste[i].getTimeMeasured(), NaerstadModelBackgroundDataMatrix.Q0, Q0liste[i].getValue()); + this.backgroundData.setParamDoubleValueForDate(TMliste[i].getTimeMeasured(), NaerstadModelBackgroundDataMatrix.BT, BTliste[i].getValue()); + // Calculating TSHH (Temperature Sum Humid Hours) Double TSHHLastHour = first ? 0 : this.backgroundData.getParamDoubleValueForDate(TMliste[i-1].getTimeMeasured(), NaerstadModelBackgroundDataMatrix.TSHH); Double TSHH; @@ -464,7 +501,7 @@ public class NaerstadModel extends I18nImpl implements Model{ this.backgroundData.setParamIntValueForDate(TMliste[i].getTimeMeasured(), NaerstadModelBackgroundDataMatrix.IR, IR); // Calculating RISK. - Double RISK = IR ==1 ? (TSWH/40) * VRS : 0d; + Double RISK = IR==1 ? (TSWH/40) * VRS : 0d; this.backgroundData.setParamDoubleValueForDate(TMliste[i].getTimeMeasured(), NaerstadModelBackgroundDataMatrix.RISK, RISK); } @@ -562,12 +599,26 @@ public class NaerstadModel extends I18nImpl implements Model{ @Override public String getLicense() { - return "Open Source. Details will follow."; + return "Copyright (c) 2013 Bioforsk <http://www.bioforsk.no/>. \n" + + " \n" + + " This file is part of VIPS/NaerstadModel \n" + + " VIPS/NaerstadModel is free software: you can redistribute it and/or modify\n" + + " it under the terms of the GNU Affero General Public License as published by\n" + + " the Free Software Foundation, either version 3 of the License, or\n" + + " (at your option) any later version.\n" + + " \n" + + " VIPS/NaerstadModel is distributed in the hope that it will be useful,\n" + + " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + + " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + + " GNU Affero General Public License for more details.\n" + + " \n" + + " You should have received a copy of the GNU Affero General Public License\n" + + " along with VIPS/NaerstadModel. If not, see <http://www.gnu.org/licenses/>."; } @Override public String getCopyright() { - return "(c) 2013 Bioforsk (http://www.bioforsk.no/). Contact: post@bioforsk.no"; + return "(c) 2013-2014 Bioforsk (http://www.bioforsk.no/). Contact: post@bioforsk.no"; } diff --git a/src/main/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModelBackgroundDataMatrix.java b/src/main/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModelBackgroundDataMatrix.java index 50b2340c9082d2e111f077e32c2269c2f6b2d048..f986c0790303d448006b2c4c0a223c1a83586927 100644 --- a/src/main/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModelBackgroundDataMatrix.java +++ b/src/main/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModelBackgroundDataMatrix.java @@ -1,8 +1,26 @@ +/* + * Copyright (c) 2013-2014 Bioforsk <http://www.bioforsk.no/>. + * + * This file is part of VIPS/NaerstadModel. + * VIPS/NaerstadModel is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VIPS/NaerstadModel 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with VIPS/NaerstadModel. If not, see <http://www.gnu.org/licenses/>. + * + */ package no.bioforsk.vips.model.naerstadmodel; /** * Matrix with calculated background data for Naerstad Model - * © 2009 Bioforsk.no + * @copyright 2009-2014 Bioforsk.no * @author Tor-Einar Skog <tor-einar.skog@bioforsk.no> */ public class NaerstadModelBackgroundDataMatrix extends no.bioforsk.vips.util.DateMap{ @@ -18,6 +36,15 @@ public class NaerstadModelBackgroundDataMatrix extends no.bioforsk.vips.util.Dat public final static String WH = "WH"; public final static String IR = "IR"; public final static String RISK = "RISK"; + + // Weather data stored for convenience (putting into result object) + public final static String TM = "TM"; + public final static String UM = "UM"; + public final static String RR = "RR"; + public final static String Q0 = "Q0"; + public final static String BT = "BT"; + + public NaerstadModelBackgroundDataMatrix() { diff --git a/src/test/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModelTest.java b/src/test/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModelTest.java index bc544dab845da6c2d3c40325009c786b81cc07bc..c2c976ed6f2b631254ee0461937aae9cb1baac3c 100644 --- a/src/test/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModelTest.java +++ b/src/test/java/no/bioforsk/vips/model/naerstadmodel/NaerstadModelTest.java @@ -7,11 +7,14 @@ package no.bioforsk.vips.model.naerstadmodel; import java.io.BufferedInputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.TimeZone; import static junit.framework.Assert.fail; import junit.framework.TestCase; import no.bioforsk.vips.entity.ModelConfiguration; +import no.bioforsk.vips.entity.Result; import no.bioforsk.vips.entity.WeatherObservation; import no.bioforsk.vips.model.ConfigValidationException; import no.bioforsk.vips.model.ModelExcecutionException; @@ -51,11 +54,23 @@ public class NaerstadModelTest extends TestCase { System.out.println("getResult"); NaerstadModel instance = new NaerstadModel(); instance.setConfiguration(this.getConfiguration()); - List result; - result = instance.getResult(); + List<Result> results; + results = instance.getResult(); - //System.out.println("Result size=" + result.size()); - assertTrue(result.size() == 96); + System.out.println("Result size=" + results.size()); + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); + cal.set(2012, Calendar.AUGUST, 26, 0, 0, 0); + cal.set(Calendar.MILLISECOND, 0); + for(Result res:results) + { + if(res.getResultValidTime().compareTo(cal.getTime()) == 0) + { + assertTrue(res.getAllValues().get("RISK").equals("5.36")); + } + + System.out.println("Res[" + res.getResultValidTime() + "]:" + res.getAllValues()); + } + assertTrue(results.size() == 209); } catch (ConfigValidationException | ModelExcecutionException ex) { fail("Exception: " + ex.getMessage()); }