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

First commit

parents
Branches
Tags
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.nibio.vips.model</groupId>
<artifactId>LygusRugulipennisModel</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 LygusRugulipennisModel.
* LygusRugulipennisModel 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.
*
* LygusRugulipennisModel 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 LygusRugulipennisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.lygusrugulipennismodel;
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 TXD = "TXD";
public final static String DAYS_ABOVE_THRESHOLD = "DAYS_ABOVE_THRESHOLD";
}
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of LygusRugulipennisModel.
* LygusRugulipennisModel 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.
*
* LygusRugulipennisModel 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 LygusRugulipennisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.lygusrugulipennismodel;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.ArrayList;
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.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.CommonNamespaces;
import no.nibio.vips.util.InvalidAggregationTypeException;
import no.nibio.vips.util.WeatherElements;
import no.nibio.vips.util.WeatherObservationListException;
import no.nibio.vips.util.WeatherUtil;
/**
* Temperature based model for infection risk of Lygus rugulipennis (NO: Håra engtege)
* Counting number of days from start of warning period up until and including today
* where the TX > treshold value (pt. 15 degrees Celcius). Uses historical
* measurements as far as possible/available
*
* @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor J. Johansen <tor.johansen@nibio.no> (Model research)
* @author Tor-Einar Skog <tor-einar.skog@nibio.no> (Programming)
*
*/
public class LygusRugulipennisModel extends I18nImpl implements Model{
public final static ModelId MODEL_ID = new ModelId("LYGUSRUGUL");
private TimeZone timeZone;
private DataMatrix dataMatrix;
public final static Double LOWER_TEMPERATURE_THRESHOLD = 10.0; // Degrees Celcius
public final static Double UPPER_TEMPERATURE_THRESHOLD = 15.0; // Degrees Celcius
public final static Integer MINIMUM_DAYS_ABOVE_UPPER_THRESHOLD = 3;
public LygusRugulipennisModel()
{
super("no.nibio.vips.model.lygusrugulipennismodel.texts");
}
@Override
public List<Result> getResult() throws ModelExcecutionException {
this.aggregateDaysAboveThreshold();
System.out.println(this.dataMatrix.toString());
List<Result> retVal = new ArrayList<>();
Date currentDate = this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.DAYS_ABOVE_THRESHOLD);
Calendar cal = Calendar.getInstance(this.timeZone);
DecimalFormat dFormat = new DecimalFormat("###.##");
while(this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.DAYS_ABOVE_THRESHOLD) != null)
{
Result result = new ResultImpl();
result.setResultValidTime(currentDate);
// Adding model parameters
Integer daysAboveUpperThreshold = this.dataMatrix.getParamIntValueForDate(currentDate, DataMatrix.DAYS_ABOVE_THRESHOLD);
Double TXDCurrentDate = ((WeatherObservation)this.dataMatrix.getParamValueForDate(currentDate, DataMatrix.TXD)).getValue();
result.setValue(CommonNamespaces.NS_WEATHER, DataMatrix.TXD, dFormat.format(TXDCurrentDate));
result.setValue(this.getModelId().toString(), DataMatrix.DAYS_ABOVE_THRESHOLD, String.valueOf(daysAboveUpperThreshold));
// Deciding warning status
Integer warningStatus = Result.WARNING_STATUS_NO_RISK;
// When number of days with max temp >= upper threshold reaches 3
// The warning status is at least yellow as long as max temp > 10
if(daysAboveUpperThreshold >= LygusRugulipennisModel.MINIMUM_DAYS_ABOVE_UPPER_THRESHOLD && TXDCurrentDate >= LygusRugulipennisModel.LOWER_TEMPERATURE_THRESHOLD)
{
warningStatus = Result.WARNING_STATUS_MINOR_RISK;
// When 4 days or more with max temp >= upper threshold,
// all days with max temp >= upper threshold causes high risk of infection
if(daysAboveUpperThreshold > LygusRugulipennisModel.MINIMUM_DAYS_ABOVE_UPPER_THRESHOLD && TXDCurrentDate >= LygusRugulipennisModel.UPPER_TEMPERATURE_THRESHOLD)
{
warningStatus = Result.WARNING_STATUS_HIGH_RISK;
}
}
result.setWarningStatus(warningStatus);
retVal.add(result);
// Moving on...
cal.setTime(currentDate);
cal.add(Calendar.DATE, 1);
currentDate = cal.getTime();
}
return retVal;
}
@Override
public ModelId getModelId() {
return LygusRugulipennisModel.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 "Copyright (c) 2015 NIBIO <http://www.nibio.no/>. \n" +
"\n" +
"This file is part of LygusRugulipennisModel. \n" +
"LygusRugulipennisModel 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" +
"LygusRugulipennisModel 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" +
"" +
"You should have received a copy of the NIBIO Open Source License \n" +
"along with LygusRugulipennisModel. If not, see <http://www.nibio.no/licenses/>. \n";
}
@Override
public String getCopyright() {
return "(c) 2015 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) {
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\": \"2015-01-01T00:00:00+02:00\",\n" +
"\t\t\t\t\"elementMeasurementTypeId\":\"TX\",\n" +
"\t\t\t\t\"logIntervalId\":2,\n" +
"\t\t\t\t\"value\":1.1\n" +
"\t\t}\n" +
"}\n";
}
@Override
public void setConfiguration(ModelConfiguration config) throws ConfigValidationException {
this.dataMatrix = new DataMatrix();
ObjectMapper mapper = new ObjectMapper();
// Setting timezone
this.timeZone = TimeZone.getTimeZone((String) config.getConfigParameter("timeZone"));
// Importing weather data, creating collections
// Either daily values of TX
// OR hourly values of TX
List<WeatherObservation> TX = new ArrayList<>();
// OR lastly hourly values of TM (not optimal, but better than nothing)
List<WeatherObservation> TM = new ArrayList<>();
WeatherUtil wUtil = new WeatherUtil();
List<WeatherObservation> observations = mapper.convertValue(config.getConfigParameter("observations"), new TypeReference<List<WeatherObservation>>(){});
for(WeatherObservation o:observations)
{
switch(o.getElementMeasurementTypeId())
{
case WeatherElements.TEMPERATURE_MAXIMUM:
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1D))
{
o.setTimeMeasured(wUtil.pragmaticAdjustmentToMidnight(o.getTimeMeasured(), timeZone));
this.dataMatrix.setParamValueForDate(o.getTimeMeasured(), DataMatrix.TXD, o);
}else{
TX.add(o);
}break;
case WeatherElements.TEMPERATURE_MEAN:
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H))
{
TM.add(o);
}
default:
// Keep calm and continue importing data
break;
}
}
// So, what have we got here?
if(dataMatrix.getFirstDateWithParameterValue(DataMatrix.TXD) == null)
{
List<WeatherObservation> TXDCalculated = null;
// No daily TX values, check for hourly TX values
if(!TX.isEmpty())
{
try
{
TXDCalculated = new WeatherUtil().getAggregatedDailyValues(TX, timeZone, 15, 15, WeatherUtil.AGGREGATION_TYPE_MAXIMUM);
}catch(WeatherObservationListException | InvalidAggregationTypeException ex)
{
throw new ConfigValidationException(ex.getMessage());
}
}
else if(!TM.isEmpty())
{
try
{
TXDCalculated = new WeatherUtil().getAggregatedDailyValues(TM, timeZone, 15, 15, WeatherUtil.AGGREGATION_TYPE_MAXIMUM);
}catch(WeatherObservationListException | InvalidAggregationTypeException ex)
{
throw new ConfigValidationException(ex.getMessage());
}
}
if(TXDCalculated != null && !TXDCalculated.isEmpty())
{
for(WeatherObservation o:TXDCalculated)
{
this.dataMatrix.setParamValueForDate(o.getTimeMeasured(), DataMatrix.TXD, o);
}
}
}
if(dataMatrix.getFirstDateWithParameterValue(DataMatrix.TXD) == null)
{
throw new ConfigValidationException("Could not find any valid weather data.");
}
}
private void aggregateDaysAboveThreshold() {
Date currentDate = dataMatrix.getFirstDateWithParameterValue(DataMatrix.TXD);
System.out.println("currentDate=" + currentDate +", value=" + ((WeatherObservation) dataMatrix.getParamValueForDate(currentDate, DataMatrix.TXD)).getValue());
Calendar cal = Calendar.getInstance(this.timeZone);
Integer daysAboveThreshold = 0;
while(dataMatrix.getParamValueForDate(currentDate, DataMatrix.TXD) != null)
{
Double TXD = ((WeatherObservation) dataMatrix.getParamValueForDate(currentDate, DataMatrix.TXD)).getValue();
if(TXD >= LygusRugulipennisModel.UPPER_TEMPERATURE_THRESHOLD)
{
daysAboveThreshold++;
}
dataMatrix.setParamIntValueForDate(currentDate, DataMatrix.DAYS_ABOVE_THRESHOLD, daysAboveThreshold);
cal.setTime(currentDate);
cal.add(Calendar.DATE, 1);
currentDate = cal.getTime();
}
}
}
# Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
#
# This file is part of LygusRugulipennisModel.
# LygusRugulipennisModel 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.
#
# LygusRugulipennisModel 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 LygusRugulipennisModel. If not, see <http://www.nibio.no/licenses/>.
#
name=Lygus rugulipennis model
usage=TODO
statusInterpretation=TODO
description=TODO
# Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
#
# This file is part of LygusRugulipennisModel.
# LygusRugulipennisModel 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.
#
# LygusRugulipennisModel 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 LygusRugulipennisModel. If not, see <http://www.nibio.no/licenses/>.
#
name=H\u00e5ret engtege-modell
usage=TODO
statusInterpretation=TODO
description=TODO
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of LygusRugulipennisModel.
* LygusRugulipennisModel 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.
*
* LygusRugulipennisModel 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 LygusRugulipennisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.lygusrugulipennismodel;
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 java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
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 LygusRugulipennisModelTest {
public LygusRugulipennisModelTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getResult method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetResult() throws Exception {
System.out.println("getResult");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
ModelConfiguration config = this.getConfiguration("/aas_2015_txd.json");
instance.setConfiguration(config);
List<Result> result = instance.getResult();
assertNotNull(result);
/*
Collections.sort(result);
for(Result oneResult:result)
{
System.out.println(oneResult);
}*/
}
/**
* Test of getModelId method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetModelId() {
System.out.println("getModelId");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
ModelId expResult = LygusRugulipennisModel.MODEL_ID;
ModelId result = instance.getModelId();
assertEquals(expResult, result);
}
/**
* Test of getModelName method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetModelName_0args() {
System.out.println("getModelName");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
String result = instance.getModelName();
assertNotNull(result);
}
/**
* Test of getLicense method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetLicense() {
System.out.println("getLicense");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
String result = instance.getLicense();
assertNotNull(result);
}
/**
* Test of getCopyright method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetCopyright() {
System.out.println("getCopyright");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
String result = instance.getCopyright();
assertNotNull(result);
}
/**
* Test of getModelDescription method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetModelDescription_0args() {
System.out.println("getModelDescription");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
String result = instance.getModelDescription();
assertNotNull(result);
}
/**
* Test of getWarningStatusInterpretation method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetWarningStatusInterpretation_0args() {
System.out.println("getWarningStatusInterpretation");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
String result = instance.getWarningStatusInterpretation();
assertNotNull(result);
}
/**
* Test of getModelUsage method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetModelUsage_0args() {
System.out.println("getModelUsage");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
String result = instance.getModelUsage();
assertNotNull(result);
}
/**
* Test of getSampleConfig method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testGetSampleConfig() {
System.out.println("getSampleConfig");
LygusRugulipennisModel instance = new LygusRugulipennisModel();
String result = instance.getSampleConfig();
assertNotNull(result);
}
/**
* Test of setConfiguration method, of class LygusRugulipennisModel.
*/
@org.junit.Test
public void testSetConfiguration() throws Exception {
System.out.println("setConfiguration");
ModelConfiguration config = this.getConfiguration("/aas_2015_txd.json");
assertNotNull(config);
LygusRugulipennisModel instance = new LygusRugulipennisModel();
instance.setConfiguration(config);
}
private ModelConfiguration getConfiguration(String fileName)
{
try {
ModelConfiguration config = new ModelConfiguration();
config.setModelId(LygusRugulipennisModel.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();
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;
}
}
}
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