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

First commit

parents
No related branches found
No related tags found
No related merge requests found
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>BremiaLactucaeModel</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>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</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) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of BremiaLactucaeModel.
* BremiaLactucaeModel 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.
*
* BremiaLactucaeModel 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 BremiaLactucaeModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.bremialactucaemodel;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
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.i18n.I18nImpl;
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.ModelUtil;
import no.nibio.vips.util.WeatherUtil;
/**
* Weather/rule based forecasting model for Bremua lactucae
* The factors are:
* <ul>
* <li>Relative humidity > 93% consecutively for 4 hours the night before yesterday? [true/false]</li>
* <li>Sporulation the night before yesterday? (true/false)</li>
* <li>Leaf wetness at ground level for 3 consecutive hours in the period from sunset yesterday to sunrise today? (yes/no)</li>
* <li>Maximum temperature yesterday (dec C)</li>
* <li>Relative humidity > 93% consecutively for 4 hours last night? [true/false]</li>
* <li>Sporulation last night?</li>
* <li>Leaf wetness at ground level for 3 consecutive hours in the period from sunset yesterday to sunrise today? (yes/no)</li>
* <li>Maximum temperature today (deg C)</li>
* </ul>
* @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class BremiaLactucaeModel extends I18nImpl implements Model {
private final static ModelId MODEL_ID = new ModelId("BREMIALACT");
private ModelUtil modelUtil;
private WeatherUtil wUtil;
private DataMatrix dataMatrix;
private TimeZone timeZone;
private final static Double NIGHT_RADIATION_THRESHOLD = 15.0;
private final static Double LEAF_WETNESS_LOWER_THRESHOLD = 48.0;
private final static Integer LEAF_WETNESS_MINIMUM_CONSECUTIVE_HOURS = 3;
private final static Double RELATIVE_HUMIDITY_LOWER_THRESHOLD = 93.0;
private final static Integer RELATIVE_HUMIDITY_MINIMUM_CONSECUTIVE_HOURS = 4;
private final static Double MAX_TEMP_THRESHOLD = 26.5;
public BremiaLactucaeModel()
{
super("no.nibio.vips.model.bremialactucaemodel.texts");
this.modelUtil = new ModelUtil();
this.wUtil = new WeatherUtil();
}
@Override
public List<Result> getResult() throws ModelExcecutionException {
//System.out.println(this.dataMatrix.toCSV());
calculateResults();
Date currentDate = this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.ERHYN);
Date endDate = this.dataMatrix.getLastDateWithParameterValue(DataMatrix.ERHYN);
Calendar cal = Calendar.getInstance(timeZone);
List<Result> retVal = new ArrayList<>();
while(currentDate.compareTo(endDate) <= 0)
{
Result result = new ResultImpl();
result.setResultValidTime(currentDate);
// Deciding warning status
Integer warningStatus = Result.WARNING_STATUS_NO_RISK;
// Infection today?
if(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.SLN) == 1 // Either sporulation last night
|| (this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.SYN) == 1 // Or sporulation yesterday night...
&& this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.ELWASYBST) == 0 // ..where the spores laid dormant all through until today
)
) // Sporulation last night or yesterday night
{
if(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.ELWAST) == 1) // Enough leaf wetness today
{
if(this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.TXT) >= BremiaLactucaeModel.MAX_TEMP_THRESHOLD) // Too hot?
{
warningStatus = Result.WARNING_STATUS_MINOR_RISK;
}
else
{
warningStatus = Result.WARNING_STATUS_HIGH_RISK;
}
}
}
result.setWarningStatus(warningStatus);
retVal.add(result);
// Adding the different parameters to the result
result.setValue(BremiaLactucaeModel.MODEL_ID.toString(), DataMatrix.ERHYN, String.valueOf(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.ERHYN)));
result.setValue(BremiaLactucaeModel.MODEL_ID.toString(), DataMatrix.SYN, String.valueOf(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.SYN)));
result.setValue(BremiaLactucaeModel.MODEL_ID.toString(), DataMatrix.ELWASYBST, String.valueOf(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.ELWASYBST)));
result.setValue(BremiaLactucaeModel.MODEL_ID.toString(), DataMatrix.TXASYBST, String.valueOf(this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.TXASYBST)));
result.setValue(BremiaLactucaeModel.MODEL_ID.toString(), DataMatrix.ERHLN, String.valueOf(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.ERHLN)));
result.setValue(BremiaLactucaeModel.MODEL_ID.toString(), DataMatrix.SLN, String.valueOf(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.SLN)));
result.setValue(BremiaLactucaeModel.MODEL_ID.toString(), DataMatrix.ELWAST, String.valueOf(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.ELWAST)));
result.setValue(BremiaLactucaeModel.MODEL_ID.toString(), DataMatrix.TXT, String.valueOf(this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.TXT)));
cal.setTime(currentDate);
cal.add(Calendar.DATE,1);
currentDate = cal.getTime();
}
//System.out.println(this.dataMatrix.toCSV());
return retVal;
}
@Override
public ModelId getModelId() {
return BremiaLactucaeModel.MODEL_ID;
}
@Override
public String getModelName() {
return this.getModelName(Model.DEFAULT_LANGUAGE);
}
@Override
public String getModelName(String language) {
return this.getText("name", language);
}
@Override
public String getLicense() {
return "\n" +
"Copyright (c) 2016 NIBIO <http://www.nibio.no/>. \n" +
"\n" +
"This file is part of BremiaLactucaeModel.\n" +
"BremiaLactucaeModel is free software: you can redistribute it and/or modify\n" +
"it under the terms of the NIBIO Open Source License as published by \n" +
"NIBIO, either version 1 of the License, or (at your option) any\n" +
"later version.\n" +
"\n" +
"BremiaLactucaeModel 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" +
"NIBIO Open Source License for more details.\n" +
"\n" +
"You should have received a copy of the NIBIO Open Source License\n" +
"along with BremiaLactucaeModel. If not, see <http://www.nibio.no/licenses/>.";
}
@Override
public String getCopyright() {
return "(c) 2016 NIBIO (http://www.nibio.no/). Contact: post@nibio.no";
}
@Override
public String getModelDescription() {
return this.getModelDescription(Model.DEFAULT_LANGUAGE);
}
@Override
public String getModelDescription(String language) {
try
{
return this.modelUtil.getTextWithBase64EncodedImages(this.getText("description", language), this.getClass());
}
catch(IOException ex)
{
return this.getText("description", language);
}
}
@Override
public String getWarningStatusInterpretation() {
return this.getWarningStatusInterpretation(Model.DEFAULT_LANGUAGE);
}
@Override
public String getWarningStatusInterpretation(String language) {
return this.getText("statusInterpretation", language);
}
@Override
public String getModelUsage() {
return this.getModelUsage(Model.DEFAULT_LANGUAGE);
}
@Override
public String getModelUsage(String language) {
return this.getText("usage", language);
}
@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\"observations\":[\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2012-08-20T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"TX\",\n" +
"\t\t\t\t\"logIntervalId\":1,\n" +
"\t\t\t\t\"value\":13.77\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2012-08-20T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"BTg\",\n" +
"\t\t\t\t\"logIntervalId\":1,\n" +
"\t\t\t\t\"value\":48\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2012-08-20T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"Q0\",\n" +
"\t\t\t\t\"logIntervalId\":1,\n" +
"\t\t\t\t\"value\":366\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2012-08-20T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"UM\",\n" +
"\t\t\t\t\"logIntervalId\":1,\n" +
"\t\t\t\t\"value\":60\n" +
"\t\t}\n" +
"\t\t]\n" +
"\t}\n" +
"}\n";
}
@Override
public void setConfiguration(ModelConfiguration config) throws ConfigValidationException {
this.dataMatrix = new DataMatrix();
ObjectMapper objectMapper = new ObjectMapper();
this.timeZone = TimeZone.getTimeZone((String) config.getConfigParameter("timeZone"));
// Getting weather data
List<WeatherObservation> observations = objectMapper.convertValue(config.getConfigParameter("observations"), new TypeReference<List<WeatherObservation>>(){});
for(WeatherObservation o:observations)
{
this.dataMatrix.setParamDoubleValueForDate(o.getTimeMeasured(), o.getElementMeasurementTypeId(), o.getValue());
}
}
/**
* The brain of the whole thing...
*
*/
private void calculateResults() throws ModelExcecutionException {
Calendar cal = Calendar.getInstance(this.timeZone);
// Decide start date. This would be the first day with weather data from sunset on two
// days before. (Sunset time the day before yesterday)
Date firstTimeWithRH = this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.UM);
Date firstValidWeatherDataDate = wUtil.normalizeToExactDate(firstTimeWithRH, timeZone);
Date sunsetTime = this.getSunset(firstValidWeatherDataDate);
// UM not present at sunset. Must move to next day
if(sunsetTime.compareTo(firstTimeWithRH) < 0)
{
cal.setTime(firstValidWeatherDataDate);
cal.add(Calendar.DATE, 1);
firstValidWeatherDataDate = cal.getTime();
}
cal.setTime(firstValidWeatherDataDate);
cal.add(Calendar.DATE, 2);
Date currentDate = cal.getTime();
// Running the results
Date lastTimeWithWeatherData = this.dataMatrix.getLastDateWithParameterValue(DataMatrix.UM);
cal.setTime(lastTimeWithWeatherData);
Integer lastDayWithWeatherData = cal.get(Calendar.DAY_OF_YEAR);
while(currentDate.before(lastTimeWithWeatherData))
{
// Day before yesterday
cal.setTime(currentDate);
Integer thisDayInYear = cal.get(Calendar.YEAR);
cal.add(Calendar.DATE, -2);
Date dayBeforeYesterday = cal.getTime();
cal.add(Calendar.DATE, 1);
Date yesterday = cal.getTime();
cal.add(Calendar.DATE, 2);
cal.add(Calendar.SECOND, -1);
Date justBeforeMidnight = cal.getTime();
Date sunsetDayBeforeYesterday = this.getSunset(dayBeforeYesterday);
Date sunriseYesterday = this.getSunrise(yesterday);
Date sunsetYesterday = this.getSunset(yesterday);
Date sunriseToday = this.getSunrise(currentDate);
if(sunriseToday == null)
{
throw new ModelExcecutionException("Not able to determine sunrise today. Too early?");
}
Date endOfDataToday = (thisDayInYear < lastDayWithWeatherData) ? justBeforeMidnight
: getDateWithCurrentTime(currentDate);
//System.out.println(currentDate + ": sunsetYesterday=" + sunsetYesterday + ", sunriseToday=" + sunriseToday );
// Rule #1 & #2 (Sporulation the night before yesterday?)
Boolean enoughRelativeHumidityYesterdayNight = isEnoughRelativeHumidityInPeriod(sunsetDayBeforeYesterday,sunriseYesterday);
this.dataMatrix.setParamIntValueForDate(currentDate, DataMatrix.ERHYN, enoughRelativeHumidityYesterdayNight ? 1:0);
this.dataMatrix.setParamIntValueForDate(currentDate, DataMatrix.SYN, enoughRelativeHumidityYesterdayNight ? 1:0);
// Rule #3 (Enough LW between sunrise yesterday and sunrise today?
this.dataMatrix.setParamIntValueForDate(currentDate, DataMatrix.ELWASYBST, isEnoughLeafWetnessInPeriod(sunriseYesterday, sunriseToday) ? 1:0);
// Rule #4 (Max temp between sunrise yesterday and sunrise today)
this.dataMatrix.setParamDoubleValueForDate(currentDate, DataMatrix.TXASYBST, getMaxTempForPeriod(sunriseYesterday, sunriseToday));
// Rule #5 & #6 (Sporulation last night?)
Boolean enoughRelativeHumidityLastNight = isEnoughRelativeHumidityInPeriod(sunsetYesterday,sunriseToday);
this.dataMatrix.setParamIntValueForDate(currentDate, DataMatrix.ERHLN, enoughRelativeHumidityLastNight ? 1:0);
this.dataMatrix.setParamIntValueForDate(currentDate, DataMatrix.SLN, enoughRelativeHumidityLastNight ? 1:0);
// Rule #7 (Enough leaf wetness after sunrise today)
this.dataMatrix.setParamIntValueForDate(currentDate, DataMatrix.ELWAST, isEnoughLeafWetnessInPeriod(sunriseToday,endOfDataToday) ? 1:0);
// Rule #8 (Max temp today)
this.dataMatrix.setParamDoubleValueForDate(currentDate, DataMatrix.TXT, getMaxTempForPeriod(currentDate, endOfDataToday));
cal.setTime(currentDate);
cal.add(Calendar.DATE,1);
currentDate = cal.getTime();
}
}
public Date getSunset(Date theDate) {
Calendar cal = Calendar.getInstance(timeZone);
cal.setTime(theDate);
// Sunset may only appear until between 18:00:00 and midnight(23:00:00)
for(int theHour = 18; theHour <= 23; theHour++)
{
cal.set(Calendar.HOUR_OF_DAY, theHour);
if(this.dataMatrix.getParamDoubleValueForDate(cal.getTime(), DataMatrix.Q0) <= NIGHT_RADIATION_THRESHOLD)
{
return cal.getTime();
}
}
return null;
}
public Date getSunrise(Date theDate)
{
Calendar cal = Calendar.getInstance(timeZone);
cal.setTime(theDate);
// Sunrise may only appear until between 00:00:00 and noon(10:00:00)
for(int theHour = 0; theHour <= 10; theHour++)
{
cal.set(Calendar.HOUR_OF_DAY, theHour);
if(this.dataMatrix.getParamDoubleValueForDate(cal.getTime(), DataMatrix.Q0) >= NIGHT_RADIATION_THRESHOLD)
{
return cal.getTime();
}
}
return null;
}
private Boolean isEnoughLeafWetnessInPeriod(Date sunsetDayBeforeYesterday, Date sunriseYesterday) {
int consecutiveHours = 0;
Date currentHour = sunsetDayBeforeYesterday;
Calendar cal = Calendar.getInstance(this.timeZone);
while(currentHour.before(sunriseYesterday))
{
System.out.println(currentHour);
if(this.dataMatrix.getParamDoubleValueForDate(currentHour, DataMatrix.BTG) >= BremiaLactucaeModel.LEAF_WETNESS_LOWER_THRESHOLD)
{
consecutiveHours++;
if(consecutiveHours == BremiaLactucaeModel.LEAF_WETNESS_MINIMUM_CONSECUTIVE_HOURS)
{
return true;
}
}
else
{
consecutiveHours = 0;
}
cal.setTime(currentHour);
cal.add(Calendar.HOUR_OF_DAY, 1);
currentHour = cal.getTime();
}
return false;
}
private Boolean isEnoughRelativeHumidityInPeriod(Date periodStart, Date periodEnd) {
int consecutiveHours = 0;
Date currentHour = periodStart;
Calendar cal = Calendar.getInstance(this.timeZone);
while(currentHour.before(periodEnd))
{
if(this.dataMatrix.getParamDoubleValueForDate(currentHour, DataMatrix.UM) >= BremiaLactucaeModel.RELATIVE_HUMIDITY_LOWER_THRESHOLD)
{
consecutiveHours++;
if(consecutiveHours == BremiaLactucaeModel.RELATIVE_HUMIDITY_MINIMUM_CONSECUTIVE_HOURS)
{
return true;
}
}
else
{
consecutiveHours = 0;
}
cal.setTime(currentHour);
cal.add(Calendar.HOUR_OF_DAY, 1);
currentHour = cal.getTime();
}
return false;
}
private Double getMaxTempForPeriod(Date startTime, Date endTime) {
Double maxTemp = 0.0;
Calendar cal = Calendar.getInstance(this.timeZone);
while(startTime.compareTo(endTime) <= 0)
{
Double txHour = this.dataMatrix.getParamDoubleValueForDate(startTime, DataMatrix.TX);
maxTemp = Math.max(maxTemp, txHour);
cal.setTime(startTime);
cal.add(Calendar.HOUR_OF_DAY, 1);
startTime = cal.getTime();
}
return maxTemp;
}
/**
* Returns a given date with same hour and minute as system time ("now")
* @param theDate
* @return
*/
private Date getDateWithCurrentTime(Date theDate) {
Calendar cal = Calendar.getInstance(this.timeZone);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
cal.setTime(theDate);
cal.set(Calendar.HOUR_OF_DAY,hour);
cal.set(Calendar.MINUTE, minute);
return cal.getTime();
}
}
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of BremiaLactucaeModel.
* BremiaLactucaeModel 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.
*
* BremiaLactucaeModel 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 BremiaLactucaeModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.bremialactucaemodel;
import no.nibio.vips.util.DateMap;
/**
* @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class DataMatrix extends DateMap{
public final static String Q0 = "Q0";
public final static String TXD = "TXD";
public final static String UM = "UM";
public final static String BTG = "BTg";
/**
* Enough relative humidity yesterday night? (Rule #1) 1 or 0
*/
public final static String ERHYN = "ERHYN";
/**
* Sporulation yesterday night? (Rule #2) 1 or 0
*/
public final static String SYN = "SYN";
/**
* Enough leaf wetness after sunrise yesterday and before sunrise today? (Rule #3) 1 or 0
*/
public final static String ELWASYBST = "ELWASYBST";
/**
* Max temp between sunrise yesterday and sunrise today (Rule #4)
*/
public final static String TXASYBST = "TXASYBST";
/**
* Max temp hourly
*/
public final static String TX = "TX";
/**
* Enough relative humidity last night? (Rule #5) 1 or 0
*/
public final static String ERHLN = "ERHLN";
/**
* Sporulation last night? (Rule #6) 1 or 0
*/
public final static String SLN = "SLN";
/**
* Enough leaf wetness after sunrise today (Rule #7)
*/
public final static String ELWAST = "ELWAST";
/**
* Max temp today (Rule #8)
*/
public final static String TXT = "TXT";
}
# Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
#
# This file is part of MamestraBrassicaeModel.
# MamestraBrassicaeModel 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.
#
# MamestraBrassicaeModel 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 MamestraBrassicaeModel. If not, see <http://www.nibio.no/licenses/>.
#
name=Bremia lactucae model
description=TODO
statusInterpretation=TODO
usage=TODO
name=Salatbladskimmelmodell
description=TODO
statusInterpretation=TODO
usage=TODO
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of BremiaLactucaeModel.
* BremiaLactucaeModel 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.
*
* BremiaLactucaeModel 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 BremiaLactucaeModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.bremialactucaemodel;
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.ArrayList;
import java.util.Date;
import java.util.List;
import static junit.framework.Assert.fail;
import no.nibio.vips.entity.ModelConfiguration;
import no.nibio.vips.entity.Result;
import no.nibio.vips.entity.WeatherObservation;
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 BremiaLactucaeModelTest {
public BremiaLactucaeModelTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getResult method, of class BremiaLactucaeModel.
*/
@Test
public void testGetResult() throws Exception {
System.out.println("getResult");
ModelConfiguration config = this.getConfiguration("/lier_2015.json");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
instance.setConfiguration(config);
List<Result> result = instance.getResult();
assertNotNull(result);
/*for(Result r:result){
System.out.println(r.toString());
}*/
}
/**
* Test of getModelId method, of class BremiaLactucaeModel.
*/
@Test
public void testGetModelId() {
System.out.println("getModelId");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
ModelId expResult = new ModelId("BREMIALACT");
ModelId result = instance.getModelId();
assertEquals(expResult.toString(), result.toString());
}
/**
* Test of getModelName method, of class BremiaLactucaeModel.
*/
@Test
public void testGetModelName_0args() {
System.out.println("getModelName");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
String result = instance.getModelName();
assertNotNull(result);
}
/**
* Test of getLicense method, of class BremiaLactucaeModel.
*/
@Test
public void testGetLicense() {
System.out.println("getLicense");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
String result = instance.getLicense();
assertNotNull(result);
}
/**
* Test of getCopyright method, of class BremiaLactucaeModel.
*/
@Test
public void testGetCopyright() {
System.out.println("getCopyright");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
String result = instance.getCopyright();
assertNotNull(result);
}
/**
* Test of getModelDescription method, of class BremiaLactucaeModel.
*/
@Test
public void testGetModelDescription_0args() {
System.out.println("getModelDescription");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
String result = instance.getModelDescription();
assertNotNull(result);
}
/**
* Test of getWarningStatusInterpretation method, of class BremiaLactucaeModel.
*/
@Test
public void testGetWarningStatusInterpretation_0args() {
System.out.println("getWarningStatusInterpretation");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
String result = instance.getWarningStatusInterpretation();
assertNotNull(result);
}
/**
* Test of getModelUsage method, of class BremiaLactucaeModel.
*/
@Test
public void testGetModelUsage_0args() {
System.out.println("getModelUsage");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
String result = instance.getModelUsage();
assertNotNull(result);
}
/**
* Test of getSampleConfig method, of class BremiaLactucaeModel.
*/
@Test
public void testGetSampleConfig() {
System.out.println("getSampleConfig");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
String result = instance.getSampleConfig();
assertNotNull(result);
}
/**
* Test of setConfiguration method, of class BremiaLactucaeModel.
*/
@Test
public void testSetConfiguration() throws Exception {
System.out.println("setConfiguration");
ModelConfiguration config = this.getConfiguration("/lier_2015.json");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
instance.setConfiguration(config);
}
@Test
public void testGetSunset() throws Exception {
System.out.println("getSunset");
ModelConfiguration config = this.getConfiguration("/lier_2015.json");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
instance.setConfiguration(config);
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2015, Calendar.JUNE, 22, 0, 0, 0);
cal.set(Calendar.MILLISECOND,0);
Date testDate = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 22);
Date expResult = cal.getTime();
Date result = instance.getSunset(testDate);
assertEquals(expResult, result);
}
@Test
public void testGetSunrise() throws Exception {
System.out.println("getSunset");
ModelConfiguration config = this.getConfiguration("/lier_2015.json");
BremiaLactucaeModel instance = new BremiaLactucaeModel();
instance.setConfiguration(config);
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2015, Calendar.JUNE, 23, 0, 0, 0);
cal.set(Calendar.MILLISECOND,0);
Date testDate = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 5);
Date expResult = cal.getTime();
Date result = instance.getSunrise(testDate);
assertEquals(expResult, result);
}
private ModelConfiguration getConfiguration(String fileName)
{
try {
ModelConfiguration config = new ModelConfiguration();
config.setModelId("BREMIALACT");
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();
Date firstDate = null;
Date lastDate = null;
if(all.isArray())
{
for(JsonNode node : all){
Date timeMeasured = (Date)mapper.convertValue(node.get("timeMeasured").asText(), new TypeReference<Date>(){});
if(firstDate == null || firstDate.compareTo(timeMeasured) > 0)
{
firstDate = timeMeasured;
}
if(lastDate == null || lastDate.compareTo(timeMeasured) < 0)
{
lastDate = timeMeasured;
}
//System.out.println(node.toString());
WeatherObservation observation = new WeatherObservation();
observation.setTimeMeasured(timeMeasured);
observation.setLogIntervalId(node.get("logIntervalId").asInt());
observation.setElementMeasurementTypeId(node.get("elementMeasurementTypeId").asText());
observation.setValue(node.get("value").asDouble());
observations.add(observation);
}
}
else
{
fail("Data input from file is not a JSON array");
}
config.setConfigParameter("observations", observations);
return config;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment