Skip to content
Snippets Groups Projects
Commit 1c3d750d 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 570 additions and 0 deletions
target/
classes/
pom.xml 0 → 100644
<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>AppleScabModel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AppleScabModel</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>no.bioforsk.vips.common</groupId>
<artifactId>VIPSCommon</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
package no.bioforsk.vips.model.applescabmodel;
import no.bioforsk.vips.util.DateMap;
/**
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class AppleScabCalculations extends DateMap{
public final static String AGGREGATED_TEMPERATURE = "TMD";
public final static String AGGREGATED_LEAF_WETNESS = "BTD";
public final static String AGGREGATED_PRECIPITATION = "RRD";
public final static String ACCUMULATED_TEMPERATURE = "ACCTEMP";
public final static String ASCOSPORE_MATURITY = "ASCMAT";
public final static String ACCUMULATED_MILLS = "ACCMILLS";
public final static String TM = "TM";
public final static String BT = "BT";
public final static String RR = "RR";
}
This diff is collapsed.
package no.bioforsk.vips.model.applescabmodel;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class AscosporeMaturityTable {
private static final Map<Double, Double> maturityTable;
/**
*
* @param accumulatedTemperature Accumulation of
* daily temperature &gt; 0 degrees Celcius over a given period
* @return
*/
public Double getAscosporeMaturity(Double accumulatedTemperature)
{
Double ascosporeMaturity;
if ( accumulatedTemperature >= 0 )
{
if ( accumulatedTemperature < 600 )
{
Double roundedTemperature = Math.floor(accumulatedTemperature/10) * 10;
ascosporeMaturity = maturityTable.get(roundedTemperature);
}
else
{
ascosporeMaturity = 100d;
}
}
else
{
ascosporeMaturity = 0d;
}
return ascosporeMaturity;
}
static {
Map<Double, Double> aMap = new HashMap();
/**
* These were generated from Bioforsk's VIPS database from this query:
* SELECT 'aMap.put(' || temperatur || 'd,' || akkumulert_sporemodning || 'd);' FROM askosporemodning;
*/
aMap.put(0d,1d);
aMap.put(10d,1d);
aMap.put(20d,1d);
aMap.put(30d,1d);
aMap.put(40d,2d);
aMap.put(50d,2d);
aMap.put(60d,3d);
aMap.put(70d,4d);
aMap.put(80d,5d);
aMap.put(90d,6d);
aMap.put(100d,7d);
aMap.put(110d,8d);
aMap.put(120d,9d);
aMap.put(130d,12d);
aMap.put(140d,14d);
aMap.put(150d,16d);
aMap.put(160d,19d);
aMap.put(170d,21d);
aMap.put(180d,25d);
aMap.put(190d,28d);
aMap.put(200d,31d);
aMap.put(210d,35d);
aMap.put(220d,39d);
aMap.put(230d,42d);
aMap.put(240d,46d);
aMap.put(250d,50d);
aMap.put(260d,54d);
aMap.put(270d,58d);
aMap.put(280d,62d);
aMap.put(290d,66d);
aMap.put(300d,70d);
aMap.put(310d,73d);
aMap.put(320d,76d);
aMap.put(330d,79d);
aMap.put(340d,82d);
aMap.put(350d,84d);
aMap.put(360d,87d);
aMap.put(370d,89d);
aMap.put(380d,90d);
aMap.put(390d,92d);
aMap.put(400d,93d);
aMap.put(410d,95d);
aMap.put(420d,96d);
aMap.put(430d,96d);
aMap.put(440d,97d);
aMap.put(450d,98d);
aMap.put(460d,98d);
aMap.put(470d,99d);
aMap.put(480d,99d);
aMap.put(490d,99d);
aMap.put(500d,99d);
aMap.put(510d,100d);
aMap.put(520d,100d);
aMap.put(530d,100d);
aMap.put(540d,100d);
aMap.put(550d,100d);
aMap.put(560d,100d);
aMap.put(570d,100d);
aMap.put(580d,100d);
aMap.put(590d,100d);
aMap.put(600d,100d);
maturityTable = Collections.unmodifiableMap(aMap);
}
}
package no.bioforsk.vips.model.applescabmodel;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class MillsTable {
private static final Map<MillsTableKey, Double> MillsTable;
public Double getMillsValue(Double temperature, Integer appleScabStadium)
{
Double millsValue = MillsTable.get(new MillsTableKey(Math.floor(temperature), appleScabStadium));
return millsValue != null ? millsValue : 0d;
}
static {
Map<MillsTableKey, Double> aMap = new HashMap();
/**
* These were generated from Bioforsk's VIPS database from this query:
* SELECT 'aMap.put(new MillsTableKey(' || temperatur || 'd,' || type || '),' || mills_verdi || 'd);' FROM mills_tabell;
*/
aMap.put(new MillsTableKey(0d,1),2d);
aMap.put(new MillsTableKey(1d,1),2d);
aMap.put(new MillsTableKey(2d,1),3d);
aMap.put(new MillsTableKey(3d,1),3d);
aMap.put(new MillsTableKey(4d,1),4d);
aMap.put(new MillsTableKey(5d,1),5d);
aMap.put(new MillsTableKey(6d,1),6d);
aMap.put(new MillsTableKey(7d,1),6d);
aMap.put(new MillsTableKey(8d,1),7d);
aMap.put(new MillsTableKey(9d,1),8d);
aMap.put(new MillsTableKey(10d,1),9d);
aMap.put(new MillsTableKey(11d,1),11d);
aMap.put(new MillsTableKey(12d,1),12d);
aMap.put(new MillsTableKey(13d,1),13d);
aMap.put(new MillsTableKey(14d,1),14d);
aMap.put(new MillsTableKey(15d,1),14d);
aMap.put(new MillsTableKey(16d,1),16d);
aMap.put(new MillsTableKey(17d,1),17d);
aMap.put(new MillsTableKey(18d,1),17d);
aMap.put(new MillsTableKey(19d,1),17d);
aMap.put(new MillsTableKey(20d,1),17d);
aMap.put(new MillsTableKey(21d,1),17d);
aMap.put(new MillsTableKey(22d,1),17d);
aMap.put(new MillsTableKey(23d,1),17d);
aMap.put(new MillsTableKey(24d,1),16d);
aMap.put(new MillsTableKey(25d,1),13d);
aMap.put(new MillsTableKey(23d,2),12d);
aMap.put(new MillsTableKey(24d,2),11d);
aMap.put(new MillsTableKey(25d,2),9d);
aMap.put(new MillsTableKey(26d,2),7d);
aMap.put(new MillsTableKey(27d,1),0d);
aMap.put(new MillsTableKey(28d,1),0d);
aMap.put(new MillsTableKey(29d,1),0d);
aMap.put(new MillsTableKey(30d,1),0d);
aMap.put(new MillsTableKey(31d,1),0d);
aMap.put(new MillsTableKey(32d,1),0d);
aMap.put(new MillsTableKey(33d,1),0d);
aMap.put(new MillsTableKey(27d,2),0d);
aMap.put(new MillsTableKey(28d,2),0d);
aMap.put(new MillsTableKey(29d,2),0d);
aMap.put(new MillsTableKey(30d,2),0d);
aMap.put(new MillsTableKey(31d,2),0d);
aMap.put(new MillsTableKey(32d,2),0d);
aMap.put(new MillsTableKey(33d,2),0d);
aMap.put(new MillsTableKey(26d,1),9d);
aMap.put(new MillsTableKey(0d,2),3d);
aMap.put(new MillsTableKey(1d,2),3d);
aMap.put(new MillsTableKey(2d,2),3d);
aMap.put(new MillsTableKey(3d,2),3d);
aMap.put(new MillsTableKey(4d,2),4d);
aMap.put(new MillsTableKey(5d,2),4d);
aMap.put(new MillsTableKey(6d,2),5d);
aMap.put(new MillsTableKey(7d,2),6d);
aMap.put(new MillsTableKey(8d,2),7d);
aMap.put(new MillsTableKey(9d,2),8d);
aMap.put(new MillsTableKey(10d,2),10d);
aMap.put(new MillsTableKey(11d,2),11d);
aMap.put(new MillsTableKey(12d,2),11d);
aMap.put(new MillsTableKey(13d,2),11d);
aMap.put(new MillsTableKey(14d,2),11d);
aMap.put(new MillsTableKey(15d,2),11d);
aMap.put(new MillsTableKey(16d,2),11d);
aMap.put(new MillsTableKey(17d,2),11d);
aMap.put(new MillsTableKey(18d,2),12d);
aMap.put(new MillsTableKey(19d,2),12d);
aMap.put(new MillsTableKey(20d,2),13d);
aMap.put(new MillsTableKey(21d,2),13d);
aMap.put(new MillsTableKey(22d,2),13d);
MillsTable = Collections.unmodifiableMap(aMap);
}
}
package no.bioforsk.vips.model.applescabmodel;
import java.util.Objects;
/**
* Composite key for the map in {@see MillsTable}
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class MillsTableKey {
private Double temperature;
private Integer appleScabStadium;
public MillsTableKey(Double temperature,Integer appleScabStadium)
{
this.temperature = temperature;
this.appleScabStadium = appleScabStadium;
}
@Override
public int hashCode()
{
return this.temperature.intValue() * 100 + this.appleScabStadium;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final MillsTableKey other = (MillsTableKey) obj;
if (!Objects.equals(this.temperature, other.temperature)) {
return false;
}
if (!Objects.equals(this.appleScabStadium, other.appleScabStadium)) {
return false;
}
return true;
}
}
# To change this template, choose Tools | Templates
# and open the template in the editor.
name=Apple Scab Model
usage=TODO: Add usage
description=The N\u00e6rstad model\n\nTODO: Add description\n\nThe N\u00e6rstad model has been developed by Ragnhild N\u00e6rstad
# To change this template, choose Tools | Templates
# and open the template in the editor.
name=Epleskurvmodell
usage=TODO: Legg til bruksinformasjon
description=TODO: Legg til beskrivelse
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package no.bioforsk.vips.model.applescabmodel;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import junit.framework.Test;
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;
import no.bioforsk.vips.util.WeatherObservationListException;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.MappingJsonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
/**
*
* @author treinar
*/
public class AppleScabModelTest extends TestCase {
public AppleScabModelTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
// TODO add test methods here. The name must begin with 'test'. For example:
// public void testHello() {}
public void testAcceptance()
{
System.out.println("testAcceptance");
try
{
Date start = new Date();
AppleScabModel instance = new AppleScabModel();
instance.setConfiguration(this.getConfiguration());
List<Result> results = null;
try {
results = instance.getResult();
} catch (ModelExcecutionException ex) {
Logger.getLogger(AppleScabModelTest.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Model execution took " + (new Date().getTime() - start.getTime()) + " milliseconds");
if(results != null)
{
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Collections.sort(results);
System.out.println(
"Timestamp" +
"\tAccumulatedTemperature" +
"\tAggregatedLeafWetness" +
"\tAggregatedTemperature" +
"\tAggregatedPrecipitation"
);
for(Result result:results)
{
if(!result.getValue(AppleScabCalculations.ACCUMULATED_TEMPERATURE).equals(""))
{
System.out.println(
format.format(result.getResultValidTime()) +
"\t" + result.getValue(AppleScabCalculations.ACCUMULATED_TEMPERATURE) +
"\t" + result.getValue(AppleScabCalculations.AGGREGATED_LEAF_WETNESS) +
"\t" + result.getValue(AppleScabCalculations.AGGREGATED_TEMPERATURE) +
"\t" + result.getValue(AppleScabCalculations.AGGREGATED_PRECIPITATION)
);
}
}
Collections.reverse(results);
System.out.println(
"Timestamp" +
"\tAccumulatedMills" +
"\tAscosporeMaturity" +
"\tTM" +
"\tBT" +
"\tRR"
);
for(Result result:results)
{
System.out.println(
format.format(result.getResultValidTime()) +
"\t" + result.getValue("AccumulatedMills") +
"\t" + result.getValue("AscosporeMaturity") +
"\t" + result.getValue("TM") +
"\t" + result.getValue("BT") +
"\t" + result.getValue("RR")
);
}
}
}
catch(ConfigValidationException ex)
{
fail("Exception: " + ex.getMessage());
}
}
public void testGetAccumulatedTemperature()
{
System.out.println("testGetAccumulatedTemperature");
try
{
AppleScabModel instance = new AppleScabModel();
instance.setConfiguration(this.getConfiguration());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2012, Calendar.JULY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND,0);
assertEquals(842.8150416666666, instance.getAccumulatedTemperature(cal.getTime()));
}
catch(ConfigValidationException | WeatherObservationListException ex)
{
fail("Exception: " + ex.getMessage());
}
}
public void testcalculateAscosporeMaturity()
{
System.out.println("testcalculateAscosporeMaturity");
try
{
AppleScabModel instance = new AppleScabModel();
instance.setConfiguration(this.getConfiguration());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2012, Calendar.MAY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND,0);
assertEquals(9d, instance.calculateAscosporeMaturity(cal.getTime()));
cal.set(Calendar.DATE, 15);
assertEquals(42d, instance.calculateAscosporeMaturity(cal.getTime()));
cal.set(Calendar.MONTH, Calendar.JULY);
assertEquals(100d, instance.calculateAscosporeMaturity(cal.getTime()));
}
catch(ConfigValidationException | WeatherObservationListException ex)
{
fail("Exception: " + ex.getMessage());
}
}
private ModelConfiguration getConfiguration()
{
try {
ModelConfiguration config = new ModelConfiguration();
config.setModelId(AppleScabModel.MODEL_ID.toString());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2012, Calendar.MARCH, 25, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
config.setConfigParameter("startDateAscosporeMaturity", cal.getTime());
config.setConfigParameter("timeZone", cal.getTimeZone().getID());
BufferedInputStream inputStream = new BufferedInputStream(this.getClass().getResourceAsStream("/weatherData.json"));
JsonFactory f = new MappingJsonFactory();
JsonParser jp = f.createJsonParser(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").getTextValue(), 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").getIntValue());
observation.setElementMeasurementTypeId(node.get("elementMeasurementTypeId").getTextValue());
observation.setValue(node.get("value").getDoubleValue());
observations.add(observation);
}
}
else
{
fail("Data input from file is not a JSON array");
}
config.setConfigParameter("observations", observations);
return config;
} catch (IOException ex) {
return null;
}
}
public void testMillsTable()
{
System.out.println("testMillsTable");
MillsTable table = new MillsTable();
Double expResult = 2d;
assertEquals(2d, table.getMillsValue(0d, 1));
assertEquals(3d, table.getMillsValue(3d, 1));
assertEquals(9d, table.getMillsValue(10d, 1));
assertEquals(13d, table.getMillsValue(13.5, 1));
assertEquals(11d, table.getMillsValue(11.6, 2));
assertEquals(0d, table.getMillsValue(27d, 1));
assertEquals(0d, table.getMillsValue(27d, 2));
assertEquals(0d, table.getMillsValue(50d, 2));
}
}
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