Skip to content
Snippets Groups Projects
Commit cad222a0 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
Showing
with 977 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.nibio.vips.model</groupId>
<artifactId>NegativePrognosisModel</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>
<name>NegativePrognosisModel</name>
</project>
\ No newline at end of file
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of NegativePrognosisModel.
* NegativePrognosisModel 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.
*
* NegativePrognosisModel 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 NegativePrognosisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.negativeprognosismodel;
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 TM = "TM";
public final static String UM = "UM";
public final static String RR = "RR";
public final static String HOURLY_CONTRIB_A = "HOURLY_CONTRIB_A"; // Infection
public final static String HOURLY_CONTRIB_B = "HOURLY_CONTRIB_B"; // Sporulation
public final static String HOURLY_CONTRIB_C = "HOURLY_CONTRIB_C"; // Mycel tissue
public final static String HOURLY_CONTRIB_D = "HOURLY_CONTRIB_D"; // Drying inhibition (?)
public final static String DAILY_CONTRIB_A = "DAILY_CONTRIB_A"; // Infection
public final static String DAILY_CONTRIB_B = "DAILY_CONTRIB_B"; // Sporulation
public final static String DAILY_CONTRIB_C = "DAILY_CONTRIB_C"; // Mycel tissue
public final static String DAILY_CONTRIB_D = "DAILY_CONTRIB_D"; // Drying inhibition (?)
public final static String DAILY_CONTRIB = "DAILY_CONTRIB"; // SUM A-D per day
public final static String AGGREGATED_CONTRIB = "AGGREGATED_CONTRIB"; // Aggregated SUM A-D from start of calculation
}
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of NegativePrognosisModel.
* NegativePrognosisModel 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.
*
* NegativePrognosisModel 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 NegativePrognosisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.negativeprognosismodel;
/**
* @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class NegativePrognosisContribution {
private String type;
private Double tmMin, tmMax, contribution;
public NegativePrognosisContribution(String type, Double tmMin, Double tmMax, Double contribution){
this.type = type;
this.tmMin = tmMin;
this.tmMax = tmMax;
this.contribution = contribution;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the tmMin
*/
public Double getTmMin() {
return tmMin;
}
/**
* @param tmMin the tmMin to set
*/
public void setTmMin(Double tmMin) {
this.tmMin = tmMin;
}
/**
* @return the tmMax
*/
public Double getTmMax() {
return tmMax;
}
/**
* @param tmMax the tmMax to set
*/
public void setTmMax(Double tmMax) {
this.tmMax = tmMax;
}
/**
* @return the contribution
*/
public Double getContribution() {
return contribution;
}
/**
* @param contribution the contribution to set
*/
public void setContribution(Double contribution) {
this.contribution = contribution;
}
}
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of NegativePrognosisModel.
* NegativePrognosisModel 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.
*
* NegativePrognosisModel 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 NegativePrognosisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.negativeprognosismodel;
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.TimeZone;
import java.util.List;
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;
/**
* @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class NegativePrognosisModel extends I18nImpl implements Model{
private final static ModelId MODEL_ID = new ModelId("NEGPROGMOD");
private DataMatrix dataMatrix;
private TimeZone timeZone;
// Minimum consecutive hours of humid conditions for
// adding A and B type contributions (sporulation and infection)
private final Integer CONTRIBUTION_A_MINIMUM_DURATION = 4;
private final Integer CONTRIBUTION_B_MINIMUM_DURATION = 10;
// Humid/dry conditions
private final Double CONTRIBUTION_AB_MINIMUM_PRECIPITATION = 0.1;
private final Double CONTRIBUTION_AB_MINIMUM_HUMIDITY = 90.0;
private final Double CONTRIBUTION_D_MAXIMUM_HUMIDITY = 70.0;
// Other model thresholds
// When we go from green to yellow or red status
private final Double INFECTION_THRESHOLD = 150.0;
// When daily contribution exeeds this (after total INFECTION_THRESHOLD has
// been exceeded), daily status is red, yellow otherwise
private final Double INFECTION_DAILY_CONTRIBUTION_THRESHOLD = 7.0;
// Other model constants
private final Double DAILY_CONSTANT_C = 1.0926;
private final Double DAILY_CONSTANT_D = -1.1232;
public NegativePrognosisModel()
{
super("no.nibio.vips.model.negativeprognosismodel.texts");
this.dataMatrix = new DataMatrix();
}
@Override
public List<Result> getResult() throws ModelExcecutionException {
this.calculateDailyContributions();
Date currentDate = this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.AGGREGATED_CONTRIB);
Date lastDate = this.dataMatrix.getLastDateWithParameterValue(DataMatrix.AGGREGATED_CONTRIB);
Calendar cal = Calendar.getInstance(this.timeZone);
List<Result> retVal = new ArrayList<>();
while(currentDate.compareTo(lastDate) <= 0)
{
Result result = new ResultImpl();
result.setResultValidTime(currentDate);
// Set model params
result.setValue(this.getModelId().toString(), DataMatrix.DAILY_CONTRIB_A, this.dataMatrix.getDefaultFormattedValueForDate(currentDate, DataMatrix.DAILY_CONTRIB_A));
result.setValue(this.getModelId().toString(), DataMatrix.DAILY_CONTRIB_B, this.dataMatrix.getDefaultFormattedValueForDate(currentDate, DataMatrix.DAILY_CONTRIB_B));
result.setValue(this.getModelId().toString(), DataMatrix.DAILY_CONTRIB_C, this.dataMatrix.getDefaultFormattedValueForDate(currentDate, DataMatrix.DAILY_CONTRIB_C));
result.setValue(this.getModelId().toString(), DataMatrix.DAILY_CONTRIB_D, this.dataMatrix.getDefaultFormattedValueForDate(currentDate, DataMatrix.DAILY_CONTRIB_D));
result.setValue(this.getModelId().toString(), DataMatrix.DAILY_CONTRIB, this.dataMatrix.getDefaultFormattedValueForDate(currentDate, DataMatrix.DAILY_CONTRIB));
result.setValue(this.getModelId().toString(), DataMatrix.AGGREGATED_CONTRIB, this.dataMatrix.getDefaultFormattedValueForDate(currentDate, DataMatrix.AGGREGATED_CONTRIB));
// Deciding warning status
Double aggregatedContribution = this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.AGGREGATED_CONTRIB);
Double dailyContribution = this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.DAILY_CONTRIB);
Integer warningStatus = 2;
// If aggregated contribution < INFECTION_THRESHOLD: NO RISK
if(aggregatedContribution <= this.INFECTION_THRESHOLD)
{
// If aggregated contribution >= INFECTION_THRESHOLD and daily contribution <= INFECTION_DAILY_CONTRIBUTION_THRESHOLD: MEDIUM RISK
warningStatus = 3;
// If aggregated contributin >= INFECTION_THRESHOLD and daily contribution > INFECTION_DAILY_CONTRIBUTION_THRESHOLD: HIGH RISK
if(dailyContribution >this.INFECTION_DAILY_CONTRIBUTION_THRESHOLD)
{
warningStatus = 4;
}
}
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 NegativePrognosisModel.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 NegFryModel.\n" +
"NegFryModel 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" +
"NegFryModel 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 NegFryModel. 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) {
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\":\"TM\",\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\":\"RR\",\n" +
"\t\t\t\t\"logIntervalId\":1,\n" +
"\t\t\t\t\"value\":0\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 {
ObjectMapper mapper = new ObjectMapper();
// Setting timezone
this.timeZone = TimeZone.getTimeZone((String) config.getConfigParameter("timeZone"));
// Getting weather data
List<WeatherObservation> observations = mapper.convertValue(config.getConfigParameter("observations"), new TypeReference<List<WeatherObservation>>(){});
for(WeatherObservation o:observations)
{
this.dataMatrix.setParamDoubleValueForDate(o.getTimeMeasured(), o.getElementMeasurementTypeId(), o.getValue());
}
}
/**
* Calculating the contribution of infection, sporulation, mycel and drying inhibition per hour
*/
private void calculateHourlyContributions()
{
// Iterate the set of weather data
Date currentTime = this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.TM);
Date endTime = this.dataMatrix.getLastDateWithParameterValue(DataMatrix.TM);
Calendar cal = Calendar.getInstance(this.timeZone);
Integer contributionACounter = 0;
Integer contributionBCounter = 0;
Double aggregatedContributionA = 0.0;
Double aggregatedContributionB = 0.0;
while(currentTime.compareTo(endTime) <= 0)
{
Double temp = this.dataMatrix.getParamDoubleValueForDate(currentTime, DataMatrix.TM);
Double hum = this.dataMatrix.getParamDoubleValueForDate(currentTime, DataMatrix.UM);
Double rain = this.dataMatrix.getParamDoubleValueForDate(currentTime, DataMatrix.RR);
// A (infection) and B (sporulation) contributions
// Humid conditions?
// If so, contributions may be added, given that we have enough consecutive hours of humid conditions
if(hum >= this.CONTRIBUTION_AB_MINIMUM_HUMIDITY || rain >= this.CONTRIBUTION_AB_MINIMUM_PRECIPITATION)
{
contributionACounter++;
aggregatedContributionA += NegativePrognosisTable.getNegativePrognosisContribution("A", temp);
if(contributionACounter >= this.CONTRIBUTION_A_MINIMUM_DURATION)
{
this.dataMatrix.setParamDoubleValueForDate(currentTime, DataMatrix.HOURLY_CONTRIB_A, aggregatedContributionA);
aggregatedContributionA = 0.0;
}
contributionBCounter++;
aggregatedContributionB += NegativePrognosisTable.getNegativePrognosisContribution("B", temp);
if(contributionBCounter >= this.CONTRIBUTION_B_MINIMUM_DURATION)
{
this.dataMatrix.setParamDoubleValueForDate(currentTime, DataMatrix.HOURLY_CONTRIB_B, aggregatedContributionB);
aggregatedContributionB = 0.0;
}
}
// Otherwise, reset counter and contribution
else
{
contributionACounter = 0;
contributionBCounter = 0;
aggregatedContributionA = 0.0;
aggregatedContributionB = 0.0;
}
// C (mycel) contributions
this.dataMatrix.setParamDoubleValueForDate(currentTime, DataMatrix.HOURLY_CONTRIB_C, NegativePrognosisTable.getNegativePrognosisContribution("C", temp));
// D (drying inhibition) contributions
this.dataMatrix.setParamDoubleValueForDate(currentTime, DataMatrix.HOURLY_CONTRIB_D,
hum < this.CONTRIBUTION_D_MAXIMUM_HUMIDITY ?
NegativePrognosisTable.getNegativePrognosisContribution("D", temp)
: 0.0
);
cal.setTime(currentTime);
cal.add(Calendar.HOUR_OF_DAY, 1);
currentTime = cal.getTime();
}
//System.out.println(this.dataMatrix.toCSV());
}
public void calculateDailyContributions()
{
this.calculateHourlyContributions();
// Finding the first midnight time with data, starting from there
Date firstPotentialTime = this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.HOURLY_CONTRIB_C);
Calendar cal = Calendar.getInstance(this.timeZone);
cal.setTime(firstPotentialTime);
while(cal.get(Calendar.HOUR_OF_DAY) != 0)
{
cal.add(Calendar.HOUR_OF_DAY, 1);
}
Date currentDayStart = cal.getTime();
cal.add(Calendar.HOUR_OF_DAY, 23);
Date currentDayEnd = cal.getTime();
Date lastTimeWithData = this.dataMatrix.getLastDateWithParameterValue(DataMatrix.HOURLY_CONTRIB_C);
Double aggregatedContribution = 0.0;
while(currentDayEnd.compareTo(lastTimeWithData) <= 0)
{
Double dailyContributionA = 0.0;
Double dailyContributionB = 0.0;
Double dailyContributionC = this.DAILY_CONSTANT_C;
Double dailyContributionD = this.DAILY_CONSTANT_D;
Date currentHour = currentDayStart;
while(currentHour.compareTo(currentDayEnd) <= 0)
{
dailyContributionA += this.dataMatrix.getParamValueForDate(currentHour, DataMatrix.HOURLY_CONTRIB_A) != null ?
this.dataMatrix.getParamDoubleValueForDate(currentHour, DataMatrix.HOURLY_CONTRIB_A)
: 0.0;
dailyContributionB += this.dataMatrix.getParamValueForDate(currentHour, DataMatrix.HOURLY_CONTRIB_B) != null ?
this.dataMatrix.getParamDoubleValueForDate(currentHour, DataMatrix.HOURLY_CONTRIB_B)
: 0.0;
dailyContributionC += this.dataMatrix.getParamDoubleValueForDate(currentHour, DataMatrix.HOURLY_CONTRIB_C);
dailyContributionD += this.dataMatrix.getParamDoubleValueForDate(currentHour, DataMatrix.HOURLY_CONTRIB_D);
// Moving to next hour of day
cal.setTime(currentHour);
cal.add(Calendar.HOUR_OF_DAY, 1);
currentHour = cal.getTime();
}
this.dataMatrix.setParamDoubleValueForDate(currentDayStart, DataMatrix.DAILY_CONTRIB_A, dailyContributionA);
this.dataMatrix.setParamDoubleValueForDate(currentDayStart, DataMatrix.DAILY_CONTRIB_B, dailyContributionB);
this.dataMatrix.setParamDoubleValueForDate(currentDayStart, DataMatrix.DAILY_CONTRIB_C, dailyContributionC);
this.dataMatrix.setParamDoubleValueForDate(currentDayStart, DataMatrix.DAILY_CONTRIB_D, dailyContributionD);
Double dailyTotalContribution =
dailyContributionA
+ dailyContributionB
+ dailyContributionC
+ dailyContributionD;
this.dataMatrix.setParamDoubleValueForDate(currentDayStart, DataMatrix.DAILY_CONTRIB, dailyTotalContribution);
aggregatedContribution += dailyTotalContribution;
this.dataMatrix.setParamDoubleValueForDate(currentDayStart, DataMatrix.AGGREGATED_CONTRIB, aggregatedContribution);
// Moving to next day
cal.setTime(currentDayStart);
cal.add(Calendar.DATE, 1);
currentDayStart = cal.getTime();
cal.add(Calendar.HOUR_OF_DAY, 23);
currentDayEnd = cal.getTime();
}
//System.out.println(this.dataMatrix.toCSV());
}
}
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of NegativePrognosisModel.
* NegativePrognosisModel 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.
*
* NegativePrognosisModel 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 NegativePrognosisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.negativeprognosismodel;
import java.util.HashMap;
import java.util.Map;
/**
* @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class NegativePrognosisTable {
private static final Map<String, NegativePrognosisContribution[]> NEGATIVE_PROGNOSIS_CONTRIBUTION;
static{
NEGATIVE_PROGNOSIS_CONTRIBUTION = new HashMap<>();
NEGATIVE_PROGNOSIS_CONTRIBUTION.put(
"A", new NegativePrognosisContribution[] {
new NegativePrognosisContribution("A",10.0,12.0,0.898999989032745),
new NegativePrognosisContribution("A",14.0,16.0,0.411799997091293),
new NegativePrognosisContribution("A",16.0,18.0,0.533599972724915),
new NegativePrognosisContribution("A",18.0,20.0,0.881600022315979),
new NegativePrognosisContribution("A",20.0,22.0,1.04980003833771),
new NegativePrognosisContribution("A",22.0,24.0,0.585799992084503),
}
);
NEGATIVE_PROGNOSIS_CONTRIBUTION.put(
"B", new NegativePrognosisContribution[] {
new NegativePrognosisContribution("B",10.0,12.0,0.392399996519089),
new NegativePrognosisContribution("B",14.0,16.0,0.070200003683567),
new NegativePrognosisContribution("B",16.0,18.0,0.12780000269413),
new NegativePrognosisContribution("B",18.0,20.0,0.910799980163574),
new NegativePrognosisContribution("B",20.0,22.0,1.47060000896454),
new NegativePrognosisContribution("B",22.0,24.0,0.855000019073486),
}
);
NEGATIVE_PROGNOSIS_CONTRIBUTION.put("C", new NegativePrognosisContribution[] {new NegativePrognosisContribution("C", 15.0, 20.0, 0.163900002837181)});
NEGATIVE_PROGNOSIS_CONTRIBUTION.put("D", new NegativePrognosisContribution[] {new NegativePrognosisContribution("D", -100.0, 100.0, 0.0467999987304211)});
}
public static Double getNegativePrognosisContribution(String type, Double temperature){
for(NegativePrognosisContribution contribution: NEGATIVE_PROGNOSIS_CONTRIBUTION.get(type))
{
if(temperature >= contribution.getTmMin() && temperature < contribution.getTmMax())
{
return contribution.getContribution();
}
}
// No temperature interval match, return zero.
return 0.0;
}
}
# 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=Negative prognosis model
description=TODO
statusInterpretation=TODO
usage=TODO
name=Negativprognosemodell
description=TODO
statusInterpretation=TODO
usage=TODO
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of NegativePrognosisModel.
* NegativePrognosisModel 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.
*
* NegativePrognosisModel 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 NegativePrognosisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.negativeprognosismodel;
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.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 NegativePrognosisModelTest {
public NegativePrognosisModelTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getResult method, of class NegativePrognosisModel.
*/
@Test
public void testGetResult() throws Exception {
System.out.println("getResult");
NegativePrognosisModel instance = new NegativePrognosisModel();
ModelConfiguration config = this.getConfiguration("/aas_2015.json");
instance.setConfiguration(config);
List<Result> result = instance.getResult();
assertNotNull(result);
}
/**
* Test of getModelId method, of class NegativePrognosisModel.
*/
@Test
public void testGetModelId() {
System.out.println("getModelId");
NegativePrognosisModel instance = new NegativePrognosisModel();
ModelId result = instance.getModelId();
assertEquals("NEGPROGMOD", result.toString());
}
/**
* Test of getModelName method, of class NegativePrognosisModel.
*/
@Test
public void testGetModelName_0args() {
System.out.println("getModelName");
NegativePrognosisModel instance = new NegativePrognosisModel();
String result = instance.getModelName();
assertNotNull(result);
}
/**
* Test of getLicense method, of class NegativePrognosisModel.
*/
@Test
public void testGetLicense() {
System.out.println("getLicense");
NegativePrognosisModel instance = new NegativePrognosisModel();
assertNotNull(instance.getLicense());
}
/**
* Test of getCopyright method, of class NegativePrognosisModel.
*/
@Test
public void testGetCopyright() {
System.out.println("getCopyright");
NegativePrognosisModel instance = new NegativePrognosisModel();
assertNotNull(instance.getCopyright());
}
/**
* Test of getModelDescription method, of class NegativePrognosisModel.
*/
@Test
public void testGetModelDescription_0args() {
System.out.println("getModelDescription");
NegativePrognosisModel instance = new NegativePrognosisModel();
String result = instance.getModelDescription();
assertNotNull(result);
}
/**
* Test of getWarningStatusInterpretation method, of class NegativePrognosisModel.
*/
@Test
public void testGetWarningStatusInterpretation_0args() {
System.out.println("getWarningStatusInterpretation");
NegativePrognosisModel instance = new NegativePrognosisModel();
String result = instance.getWarningStatusInterpretation();
assertNotNull(result);
}
/**
* Test of getModelUsage method, of class NegativePrognosisModel.
*/
@Test
public void testGetModelUsage_0args() {
System.out.println("getModelUsage");
NegativePrognosisModel instance = new NegativePrognosisModel();
String result = instance.getModelUsage();
assertNotNull(result);
}
/**
* Test of getSampleConfig method, of class NegativePrognosisModel.
*/
@Test
public void testGetSampleConfig() {
System.out.println("getSampleConfig");
NegativePrognosisModel instance = new NegativePrognosisModel();
String result = instance.getSampleConfig();
assertNotNull(result);
}
/**
* Test of setConfiguration method, of class NegativePrognosisModel.
*/
@Test
public void testSetConfiguration() throws Exception {
System.out.println("setConfiguration");
ModelConfiguration config = this.getConfiguration("/aas_2015.json");
NegativePrognosisModel instance = new NegativePrognosisModel();
instance.setConfiguration(config);
}
@Test
public void testCalculateDailyContributions() throws Exception
{
System.out.println("calculateDailyContributions");
ModelConfiguration config = this.getConfiguration("/aas_2015.json");
NegativePrognosisModel instance = new NegativePrognosisModel();
instance.setConfiguration(config);
instance.calculateDailyContributions();
}
private ModelConfiguration getConfiguration(String fileName)
{
try {
ModelConfiguration config = new ModelConfiguration();
config.setModelId("NEGPROGMOD");
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;
}
}
}
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of NegativePrognosisModel.
* NegativePrognosisModel 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.
*
* NegativePrognosisModel 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 NegativePrognosisModel. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.model.negativeprognosismodel;
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 NegativePrognosisTableTest {
public NegativePrognosisTableTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getNegativePrognosisContribution method, of class NegativePrognosisTable.
*/
@Test
public void testGetNegativePrognosisContribution() {
System.out.println("getNegativePrognosisContribution");
String[] types = {"A","B","C","D"};
Double[] temps = {0.0,10.2,13.1,16.9,18.001,30.0};
Double[] expResults = {
0.0,0.898999989032745,0.0,0.533599972724915,0.881600022315979,0.0, // A
0.0,0.392399996519089,0.0,0.12780000269413,0.910799980163574,0.0, // B
0.0,0.0,0.0,0.163900002837181,0.163900002837181,0.0, // C
0.0467999987304211,0.0467999987304211,0.0467999987304211,0.0467999987304211,0.0467999987304211,0.0467999987304211// D
};
for(int i=0;i<types.length;i++)
{
for(int j=0;j<temps.length;j++)
{
System.out.println("Type = " + types[i] + ", temp = " + temps[j]);
Double result = NegativePrognosisTable.getNegativePrognosisContribution(types[i], temps[j]);
assertEquals(expResults[i*6+j],result);
}
}
}
}
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