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

First version with working existing model (Nærstad model)!!!

parent 7b455061
No related branches found
No related tags found
No related merge requests found
package no.bioforsk.vips.entity;
import java.util.Date;
/**
* Data object that represents a weather observation. Could be measured (historical
* and forecasted (future).
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class Observation implements Comparable{
private String elementMeasurementTypeId;
private Integer logIntervalId;
private Date timeMeasured;
private double value;
/** Creates a new instance of Observation */
public Observation() {
}
public void setTimeMeasured(Date timeMeasured) { this.timeMeasured = timeMeasured; }
public Date getTimeMeasured() { return this.timeMeasured; }
public void setElementMeasurementTypeId(String elementMeasurementTypeId) {
this.elementMeasurementTypeId = elementMeasurementTypeId;
}
public String getElementMeasurementTypeId() {
return this.elementMeasurementTypeId;
}
public void setLogIntervalId(Integer id) { this.logIntervalId = id.intValue(); }
public Integer getLogIntervalId() { return this.logIntervalId; }
/**
* Compares by time measured in ascending order
*/
public int compareTo(Object o) {
Observation other = (Observation) o;
return this.compareTo(other);
}
/**
* Compares by time measured in ascending order
* @param other the observation to compare to
* @return
*/
public int compareTo(Observation other)
{
if(this.getTimeMeasured() == null && other.getTimeMeasured() == null)
return 0;
else if(this.getTimeMeasured() == null)
return -1;
else if(other.getTimeMeasured() == null)
return 1;
else
return this.getTimeMeasured().compareTo(other.getTimeMeasured());
}
@Override
public String toString()
{
return "Observation \n" +
"elementMeasurementTypeId=" + this.getElementMeasurementTypeId() + "\n" +
"timeMeasured=" + this.getTimeMeasured().toString() + "\n" +
"value=" + this.getValue();
}
/**
* @return the value
*/
public double getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(double value) {
this.value = value;
}
}
......@@ -2,6 +2,7 @@ package no.bioforsk.vips.model;
import no.bioforsk.vips.entity.Result;
import java.util.List;
import no.bioforsk.vips.entity.ModelConfiguration;
/**
* All models must implement this interface
......@@ -44,4 +45,10 @@ public interface Model {
* @return a thorough manual for this model, in the specified language (English as fallback)
*/
public String getModelUsage(String language);
/**
* Set the configuration object (with all its possible parameters(
* @param config
*/
public void setConfiguration(ModelConfiguration config);
}
package no.bioforsk.vips.util;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
/**
* Multiple parameters mapped by Date
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class DateMap {
private HashMap matrix;
private DecimalFormat decimalFormat;
/** Creates a new instance of DateMap */
public DateMap()
{
this.matrix = new HashMap();
this.decimalFormat = new DecimalFormat("0.0");
}
private Date getJavaUtilDate(Date date)
{
if(date instanceof java.sql.Date)
return new Date(date.getTime());
else
return date;
}
public void setParamDoubleValueForDate(Date date, String paramName, Double value)
{
this.setParamValueForDate(getJavaUtilDate(date), paramName, value);
}
public Double getParamDoubleValueForDate(Date date, String paramName)
{
try
{
return (Double) this.getParamValueForDate(getJavaUtilDate(date), paramName);
}
catch(NullPointerException npe) {return null;}
catch(ClassCastException cce) {return null;}
}
/**
* Set a parameter value of type Float for a given date
* @param date
* @param paramName
* @param value
*/
public void setParamFloatValueForDate(Date date, String paramName, Float value)
{
this.setParamValueForDate(getJavaUtilDate(date), paramName, value);
}
/**
*
* @param date
* @param paramName
* @return a parameter value of type Float for a given date
*/
public Float getParamFloatValueForDate(Date date, String paramName)
{
try
{
return (Float) this.getParamValueForDate(getJavaUtilDate(date), paramName);
}
catch(NullPointerException npe) {return null;}
catch(ClassCastException cce) {return null;}
}
/**
* Set a parameter value of type Integer for a given date
* @param date
* @param paramName
* @param value
*/
public void setParamIntValueForDate(Date date, String paramName, Integer value)
{
this.setParamValueForDate(getJavaUtilDate(date), paramName, value);
}
/**
*
* @param date
* @param paramName
* @return a parameter value of type Integer for a given date
*/
public Integer getParamIntValueForDate(Date date, String paramName)
{
try
{
return (Integer) this.getParamValueForDate(getJavaUtilDate(date), paramName);
}
catch(NullPointerException npe) {return null;}
catch(ClassCastException cce) {return null;}
}
/**
* Set a parameter value of type String for a given date
* @param date
* @param paramName
* @param value
*/
public void setParamStringValueForDate(Date date, String paramName, String value)
{
this.setParamValueForDate(getJavaUtilDate(date), paramName, value);
}
public String getParamStringValueForDate(Date date, String paramName)
{
try
{
return (String) this.getParamValueForDate(getJavaUtilDate(date), paramName);
}
catch(NullPointerException npe) {return null;}
catch(ClassCastException cce) {return null;}
}
/**
* Set a parameter value for a given date
* @param date
* @param paramName
* @param value
*/
public void setParamValueForDate(Date date, String paramName, Object value)
{
HashMap valueSet = (HashMap) this.matrix.get(getJavaUtilDate(date));
if(valueSet == null)
{
valueSet = new HashMap();
this.matrix.put(getJavaUtilDate(date), valueSet);
}
valueSet.put(paramName, value);
}
/**
* Get a parameter value for a given date
* @param date
* @param paramName
* @return the value, or NULL if value not found
*/
public Object getParamValueForDate(Date date, String paramName)
{
try
{
return ((HashMap)this.matrix.get(getJavaUtilDate(date))).get(paramName);
}
catch(NullPointerException npe) {return null;}
catch(ClassCastException cce) {return null;}
}
/**
* Returnerer en samling av de labels som finnes for den gitte datoen
* @param date
* @return
*/
public Set getValueLabelsForDate(Date date)
{
try
{
return ((HashMap)this.matrix.get(getJavaUtilDate(date))).keySet();
}
catch(NullPointerException npe) {return null;}
catch(ClassCastException cce) {return null;}
}
/**
* Returnerer standard formattering for en verdi
* <ul>
* <li>Strenger og heltall gjengis "in natura"</li>
* <li>Flyttall (inkl. double) gjengis med to desimaler</li>
* </ul>
* @param date
* @param paramName
* @return
*/
public String getDefaultFormattedValueForDate(Date date, String paramName)
{
Object retVal = this.getParamValueForDate(getJavaUtilDate(date), paramName);
if(retVal != null)
{
if(retVal instanceof Float || retVal instanceof Double)
{
return this.decimalFormat.format(retVal);
}
else
{
return retVal.toString();
}
}
else
return "";
}
/**
* Print values for a given date to standard out
* @param date
*/
public void printValuesForDate(Date date)
{
System.out.println(this.getValuesForDate(getJavaUtilDate(date)));
}
/**
*
* @param date
* @return
*/
private String getValuesForDate(Date date)
{
try
{
HashMap valueSet = (HashMap)this.matrix.get(getJavaUtilDate(date));
if(valueSet != null)
{
StringBuilder result = new StringBuilder(getJavaUtilDate(date).toString() + ": ");
for(Iterator i = valueSet.keySet().iterator();i.hasNext();)
{
String key = (String) i.next();
result.append(key + "=" + valueSet.get(key).toString() + ",");
}
return result.toString();
}
else
{
return getJavaUtilDate(date).toString() + ": No values found for this date";
}
}
catch(NullPointerException npe) {return "No values";}
catch(ClassCastException cce) {return "A ClassCastException occured. Message is (" + cce.getMessage() +")";}
}
/**
*
* @return The first date with values
*/
public Date getFirstDateWithValues()
{
ArrayList<Date> dates = new ArrayList(this.matrix.keySet());
Collections.sort(dates);
return dates.get(0);
}
@Override
public String toString()
{
// Get all date keys, sort them
ArrayList keys = new ArrayList(this.matrix.keySet());
Collections.sort(keys);
StringBuilder buffer = new StringBuilder();
for(Iterator<Date> i = keys.iterator();i.hasNext();)
{
Date date = i.next();
buffer.append(this.getValuesForDate(getJavaUtilDate(date))).append("\n");
}
return buffer.toString();
}
}
package no.bioforsk.vips.util;
/**
* (Probably) temporary definitions of weather elements
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class WeatherElements {
/**
* Mean temperature (Celcius)
*/
public static final String TEMPERATURE_MEAN = "TM";
/**
* Minimum temperature (Celcius)
*/
public static final String TEMPERATURE_MINIMUM = "TN";
/**
* Maximum temperature (Celcius)
*/
public static final String TEMPERATURE_MAXIMUM = "TX";
/**
* Aggregated rainfall (millimeters)
*/
public static final String PRECIPITATION = "RR";
/**
* Relative humidity (%)
*/
public static final String RELATIVE_HUMIDITY = "UM";
/**
* Global radiation (unit??)
*/
public static final String GLOBAL_RADIATION = "Q0";
/**
* Leaf wetness (Minutes per hour)
*/
public static final String LEAF_WETNESS = "BT";
}
package no.bioforsk.vips.util;
/**
* Weather related utility methods
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class WeatherUtil {
/**
* Calculates Water Vapor Deficiency in Pa
* @param temperature
* @param relative humidity
* @return Water Vapor Deficiency in Pa
*/
public double getWVD(double temperature, double relativeHumidity) {
double saturationPressure = this.getSaturationPressure(temperature);
double partialPressure = this.getPartialPressure(saturationPressure, relativeHumidity);
/*
if(this.DEBUG)
System.out.println("[BlightModelActivity] DEBUG: saturationPressure=" + saturationPressure + ", partialPressure=" + partialPressure + ", WVD=" + ((saturationPressure - partialPressure) * 1000));
* */
return (saturationPressure - partialPressure) * 1000;
}
/**
* Calculates the partial pressure
* @param saturationPressure in kPa
* @param RH
* @return partial pressure in kPa
*/
public double getPartialPressure(double saturationPressure, double relativeHumidity) {
return relativeHumidity * saturationPressure / 100;
}
/**
* Calculates the saturation pressure, based on temperature
* @param temperature
* @return saturation pressure in kPa
*/
public double getSaturationPressure(double temperature) {
return 0.61078 * Math.exp(17.269 * temperature / (temperature + 237.3));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment