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

Added isHomogenousTimeSeries and tests

parent a7996f06
Branches
Tags
No related merge requests found
......@@ -698,4 +698,66 @@ public class WeatherUtil {
}
return null;
}
/**
* Basic weather data time series health check. Same length? Starts and
* ends at same time?
* @param series
* @return true if passed all tests
* @throws WeatherObservationListException if at least one test fails
*/
public boolean isHomogenousTimeSeries(List<WeatherObservation>... series) throws WeatherObservationListException
{
// Must be of same length
for(List<WeatherObservation> list:series)
{
// If a list has no elements, it's not a time series
if(list == null || list.isEmpty())
{
throw new WeatherObservationListException("At least one list is empty or NULL");
}
Collections.sort(list);
// Must be of same length
if(series[0].size() != list.size())
{
StringBuilder msg = new StringBuilder("Unequal length of lists.");
for (List<WeatherObservation> serie : series) {
msg .append("List with ")
.append(serie.get(0).getElementMeasurementTypeId())
.append(" length=")
.append(serie.size())
.append(".\n");
}
throw new WeatherObservationListException(msg.toString());
}
// Must be starting at same time
if(series[0].get(0).getTimeMeasured().compareTo(list.get(0).getTimeMeasured()) != 0)
{
StringBuilder msg = new StringBuilder("Weather parameter lists do not start at same time");
for (List<WeatherObservation> serie : series) {
msg .append("List with ")
.append(serie.get(0).getElementMeasurementTypeId())
.append(" starts at ")
.append(serie.get(0).getTimeMeasured())
.append(".\n");
}
throw new WeatherObservationListException(msg.toString());
}
// Must be ending at same time
int lastIndex = series[0].size() - 1;
if(series[0].get(lastIndex).getTimeMeasured().compareTo(list.get(lastIndex).getTimeMeasured()) != 0)
{
StringBuilder msg = new StringBuilder("Weather parameter lists do not end at same time");
for (List<WeatherObservation> serie : series) {
msg .append("List with ")
.append(serie.get(lastIndex).getElementMeasurementTypeId())
.append(" ends at ")
.append(serie.get(lastIndex).getTimeMeasured())
.append(".\n");
}
throw new WeatherObservationListException(msg.toString());
}
}
return true;
}
}
......@@ -330,6 +330,172 @@ public class WeatherUtilTest extends TestCase {
System.out.println("Found hole here: " + result);
assertNotNull(result);
}
public void testIsHomogenousTimeSeries()
{
System.out.println("testIsHomogenousTimeSeries");
WeatherUtil instance = new WeatherUtil();
List<WeatherObservation> TM = new ArrayList<>();
List<WeatherObservation> RR = new ArrayList<>();
List<WeatherObservation> UM = new ArrayList<>();
List<WeatherObservation> faultySeries = this.getObservations("/weatherDataFaultyUnequalSize.json");
for(WeatherObservation o:faultySeries)
{
switch(o.getElementMeasurementTypeId())
{
case WeatherElements.TEMPERATURE_MEAN:
TM.add(o);
break;
case WeatherElements.PRECIPITATION:
RR.add(o);
break;
case WeatherElements.RELATIVE_HUMIDITY:
UM.add(o);
break;
default:
// Let it pass in silence
break;
}
}
try
{
// Should throw exception if at least one list is empty/NULL
instance.isHomogenousTimeSeries(TM, new ArrayList<WeatherObservation>());
fail("Method should have thrown exception");
}
catch(WeatherObservationListException ex) {
// This means that the method works as expected
}
try
{
// Should throw exception with faulty data
instance.isHomogenousTimeSeries(TM,RR,UM);
fail("Method should have thrown exception");
}
catch(WeatherObservationListException ex) {
// This means that the method works as expected
}
// Test for data where at least one series starts at a different time
TM.removeAll(TM);
UM.removeAll(UM);
RR.removeAll(RR);
faultySeries = this.getObservations("/weatherDataFaultyUnequalStart.json");
for(WeatherObservation o:faultySeries)
{
switch(o.getElementMeasurementTypeId())
{
case WeatherElements.TEMPERATURE_MEAN:
TM.add(o);
break;
case WeatherElements.PRECIPITATION:
RR.add(o);
break;
case WeatherElements.RELATIVE_HUMIDITY:
UM.add(o);
break;
default:
// Let it pass in silence
break;
}
}
try
{
// Should throw exception with faulty data
instance.isHomogenousTimeSeries(TM,RR,UM);
fail("Method should have thrown exception");
}
catch(WeatherObservationListException ex) {
System.out.println(ex.getMessage());
// This means that the method works as expected
}
// Test for data where at least one series ends at a different time
TM.removeAll(TM);
UM.removeAll(UM);
RR.removeAll(RR);
faultySeries = this.getObservations("/weatherDataFaultyUnequalEnd.json");
for(WeatherObservation o:faultySeries)
{
switch(o.getElementMeasurementTypeId())
{
case WeatherElements.TEMPERATURE_MEAN:
TM.add(o);
break;
case WeatherElements.PRECIPITATION:
RR.add(o);
break;
case WeatherElements.RELATIVE_HUMIDITY:
UM.add(o);
break;
default:
// Let it pass in silence
break;
}
}
try
{
// Should throw exception with faulty data
instance.isHomogenousTimeSeries(TM,RR,UM);
fail("Method should have thrown exception");
}
catch(WeatherObservationListException ex) {
System.out.println(ex.getMessage());
// This means that the method works as expected
}
// Finally, should let valid series pass
TM.removeAll(TM);
UM.removeAll(UM);
RR.removeAll(RR);
faultySeries = this.getObservations("/weatherDataNotFaulty.json");
for(WeatherObservation o:faultySeries)
{
switch(o.getElementMeasurementTypeId())
{
case WeatherElements.TEMPERATURE_MEAN:
TM.add(o);
break;
case WeatherElements.PRECIPITATION:
RR.add(o);
break;
case WeatherElements.RELATIVE_HUMIDITY:
UM.add(o);
break;
default:
// Let it pass in silence
break;
}
}
try
{
// Should not throw exception
instance.isHomogenousTimeSeries(TM,RR,UM);
}
catch(WeatherObservationListException ex) {
fail(ex.getMessage());
}
}
/**
* Test of normalizeToExactDate method, of class WeatherUtil.
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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