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

Refactoring and other changes

parent 62113477
Branches
Tags
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<!--
&copy; 2014 Bioforsk
Author: Tor-Einar Skog <tor-einar.skog@bioforsk.no>
&copy; 2015 NIBIO
Author: Tor-Einar Skog <tor-einar.skog@nibio.no>
-->
<module xmlns="urn:jboss:module:1.1" name="no.bioforsk.vips.VIPSCommon">
<module xmlns="urn:jboss:module:1.1" name="no.nibio.vips.VIPSCommon">
<resources>
<!--resource-root path="."/-->
<resource-root path="VIPSCommon-1.0-SNAPSHOT.jar"/>
......@@ -13,10 +13,12 @@
<!-- Insert resources here -->
</resources>
<dependencies>
<module name="org.codehaus.jackson.jackson-core-asl" export="false"/>
<module name="org.codehaus.jackson.jackson-mapper-asl" export="false"/>
<module name="com.fasterxml.jackson.core.jackson-core" export="false"/>
<module name="com.fasterxml.jackson.core.jackson-databind" export="false"/>
<module name="javax.ws.rs.api" export="false"/>
<module name="javax.servlet.api" export="false"/>
<module name="org.apache.commons.io" export="false"/>
<module name="org.apache.commons.codec" export="false"/>
</dependencies>
</module>
......@@ -29,8 +29,7 @@ public class ObservationImpl implements Observation{
private Date timeOfObservation;
private String geoInfo;
private Double observedValue;
private Integer denominator;
private String observationData;
private String name;
@Override
......@@ -39,13 +38,8 @@ public class ObservationImpl implements Observation{
}
@Override
public Double getObservedValue() {
return this.observedValue;
}
@Override
public Integer getDenominator() {
return this.denominator;
public String getObservationData(){
return this.observationData;
}
@Override
......@@ -60,22 +54,11 @@ public class ObservationImpl implements Observation{
this.timeOfObservation = timeOfObservation;
}
/**
* @param observedValue the observedValue to set
*/
public void setObservedValue(Double observedValue) {
this.observedValue = observedValue;
}
/**
* @param denominator the denominator to set
*/
public void setDenominator(Integer denominator) {
this.denominator = denominator;
public void setObservationData(String observationData){
this.observationData = observationData;
}
/**
* @param name the name to set
*/
......
......@@ -202,7 +202,7 @@ public class DateMap {
* @param date
* @return
*/
public Set getValueLabelsForDate(Date date)
public Set<String> getValueLabelsForDate(Date date)
{
try
{
......
......@@ -98,7 +98,7 @@ public class ModelUtil {
tagText += " style=\"float:" + attrVal + "\"";
}
}
tagText += "/>";
tagText += " class=\"img-responsive\"/>";
if(filenameAttributeFound)
{
tm.appendReplacement(transformedText,tagText);
......
......@@ -79,10 +79,16 @@ public class WeatherElements {
*/
public static final String WIND_SPEED_2M = "FM2";
public static final String POTENTIAL_EVAPORATION = "EPP";
public static final String FIXING_STRATEGY_INTERPOLATE = "INTERPOLATE";
public static final String FIXING_STRATEGY_SET_ZERO = "SET_ZERO";
public static String getNormalDataParameter(String parameterName)
{
return parameterName + "_NORMAL";
}
}
......@@ -29,6 +29,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.TimeZone;
import no.nibio.vips.entity.WeatherObservation;
......@@ -44,6 +45,7 @@ public class WeatherUtil {
public final static int AGGREGATION_TYPE_SUM = 2;
public final static int AGGREGATION_TYPE_MINIMUM = 3;
public final static int AGGREGATION_TYPE_MAXIMUM = 4;
public final static int AGGREGATION_TYPE_SUM_GLOBAL_RADIATION = 5;
/**
* Ensures that hourly values at the end of series that exceed
......@@ -318,6 +320,8 @@ public class WeatherUtil {
aggregateValue = getMinimum(hourValuesForADay.values()); break;
case WeatherUtil.AGGREGATION_TYPE_MAXIMUM:
aggregateValue = getMaximum(hourValuesForADay.values()); break;
case WeatherUtil.AGGREGATION_TYPE_SUM_GLOBAL_RADIATION:
aggregateValue = getSum(hourValuesForADay.values()) * 0.0036; break;
default:
throw new InvalidAggregationTypeException(
"No aggregation method with id= " + typeOfAggregation + " exists."
......@@ -333,12 +337,23 @@ public class WeatherUtil {
return aggregatedObservations;
}
private Double getAverage(Collection<Double> values)
public List<Double> getObservationValues(List<WeatherObservation> observations)
{
List<Double> values = new ArrayList<>();
for(WeatherObservation obs:observations)
{
values.add(obs.getValue());
}
return values;
}
public Double getAverage(Collection<Double> values)
{
return this.getSum(values)/values.size();
}
private Double getSum(Collection<Double> values)
public Double getSum(Collection<Double> values)
{
Double sum = 0d;
for(Double value:values)
......@@ -348,7 +363,7 @@ public class WeatherUtil {
return sum;
}
private Double getMinimum(Collection<Double> values)
public Double getMinimum(Collection<Double> values)
{
Double minimum = null;
for(Double value:values)
......@@ -358,7 +373,7 @@ public class WeatherUtil {
return minimum;
}
private Double getMaximum(Collection<Double> values)
public Double getMaximum(Collection<Double> values)
{
Double maximum = null;
for(Double value:values)
......@@ -1357,5 +1372,46 @@ public class WeatherUtil {
return retVal;
}
/**
* Mining in list, picking the numberOfObservations last observations of the requested weatherElement
* @param allObservations
* @param weatherElement
* @param numberOfObservations
* @return
*/
public List<WeatherObservation> getLastObservations(List<WeatherObservation> allObservations, String weatherElement, int numberOfObservations)
{
PriorityQueue<WeatherObservation> lastObs = new PriorityQueue<>();
for(WeatherObservation obs:allObservations)
{
if(obs.getElementMeasurementTypeId().equals(weatherElement))
{
lastObs.add(obs);
if(lastObs.size() > numberOfObservations)
{
lastObs.poll();
}
}
}
return new ArrayList(lastObs);
}
public List<WeatherObservation> getFirstObservations(List<WeatherObservation> allObservations, String weatherElement, int numberOfObservations)
{
PriorityQueue<WeatherObservation> firstObs = new PriorityQueue<>(numberOfObservations, Collections.reverseOrder());
for(WeatherObservation obs:allObservations)
{
if(obs.getElementMeasurementTypeId().equals(weatherElement))
{
firstObs.add(obs);
if(firstObs.size() > numberOfObservations)
{
firstObs.poll();
}
}
}
List<WeatherObservation> retVal = new ArrayList(firstObs);
Collections.sort(retVal);
return retVal;
}
}
......@@ -614,6 +614,38 @@ public class WeatherUtilTest extends TestCase {
fail(ex.getMessage());
}
}
public void testGetLastObservations()
{
System.out.println("testGetLastObservations");
List<WeatherObservation> obs = this.getObservations("/weatherDataNotFaulty.json");
WeatherUtil instance = new WeatherUtil();
List<WeatherObservation> result = instance.getLastObservations(obs, "Q0", 5);
assertEquals(5, result.size());
Collections.sort(result);
WeatherObservation last = result.get(result.size()-1);
//2014-08-03T12:00:00+02:00
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2014, Calendar.AUGUST, 3, 12, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
assertEquals(cal.getTime(), last.getTimeMeasured());
}
public void testGetFirstObservations()
{
System.out.println("testGetLastObservations");
List<WeatherObservation> obs = this.getObservations("/weatherDataNotFaulty.json");
WeatherUtil instance = new WeatherUtil();
List<WeatherObservation> result = instance.getFirstObservations(obs, "Q0", 5);
assertEquals(5, result.size());
Collections.sort(result);
WeatherObservation last = result.get(result.size()-1);
//2014-08-01T15:00:00+02:00
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo"));
cal.set(2014, Calendar.AUGUST, 1, 15, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
assertEquals(cal.getTime(), last.getTimeMeasured());
}
/**
* Test of normalizeToExactDate method, of class WeatherUtil.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment