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

Bugfix in file parsing and log interval setting

parent d8e23a3f
No related branches found
No related tags found
No related merge requests found
...@@ -49,12 +49,19 @@ import org.xml.sax.SAXException; ...@@ -49,12 +49,19 @@ import org.xml.sax.SAXException;
*/ */
public class YrWeatherForecastProvider implements WeatherForecastProvider{ public class YrWeatherForecastProvider implements WeatherForecastProvider{
private final static String YR_API_URL = "http://api.yr.no/weatherapi/locationforecastlts/1.2/?lat={0};lon={1};msl={2}"; private final static String YR_API_URL = "http://api.yr.no/weatherapi/locationforecastlts/1.3/?lat={0};lon={1};msl={2}";
@Override @Override
public List<WeatherObservation> getWeatherForecasts(PointOfInterest location) throws ParseWeatherDataException public List<WeatherObservation> getWeatherForecasts(PointOfInterest location) throws ParseWeatherDataException
{ {
return this.getWeatherForecasts(location.getGisGeom().getCoordinate().x, location.getGisGeom().getCoordinate().y, location.getGisGeom().getCoordinate().z); if(location.getGisGeom() != null)
{
return this.getWeatherForecasts(location.getGisGeom().getCoordinate().x, location.getGisGeom().getCoordinate().y, location.getGisGeom().getCoordinate().z);
}
else
{
return this.getWeatherForecasts(location.getLongitude(), location.getLatitude(), location.getAltitude());
}
} }
...@@ -69,6 +76,8 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{ ...@@ -69,6 +76,8 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{
altitude) altitude)
); );
//System.out.println("yrURL=" + yrURL.toString());
// TODO: Parse with DOM parser // TODO: Parse with DOM parser
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); DocumentBuilder db = dbf.newDocumentBuilder();
...@@ -113,9 +122,16 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{ ...@@ -113,9 +122,16 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{
RR.setTimeMeasured(fromTime); RR.setTimeMeasured(fromTime);
RR.setElementMeasurementTypeId(WeatherElements.PRECIPITATION); RR.setElementMeasurementTypeId(WeatherElements.PRECIPITATION);
RR.setValue(Double.parseDouble(DOMUtils.getNodeAttr("precipitation","value",node2.getChildNodes()))); RR.setValue(Double.parseDouble(DOMUtils.getNodeAttr("precipitation","value",node2.getChildNodes())));
//System.out.println("Timediff=" + (toTime.getTime() - fromTime.getTime()));
Date earliestHourlyObservation = null;
if(toTime.getTime() - fromTime.getTime() == hourDelta) if(toTime.getTime() - fromTime.getTime() == hourDelta)
{ {
//System.out.println("Found 1 hour record at " + fromTime);
RR.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); RR.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H);
if(earliestHourlyObservation == null)
{
earliestHourlyObservation = RR.getTimeMeasured();
}
} }
else if(toTime.getTime() - fromTime.getTime() == threeHoursDelta) else if(toTime.getTime() - fromTime.getTime() == threeHoursDelta)
{ {
...@@ -129,13 +145,25 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{ ...@@ -129,13 +145,25 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{
{ {
continue; continue;
} }
// The earliest observations may be with 6 or 3 hour resolution
// In order to avoid overestimation of rain in the beginning,
// We skip ahead until we find observations with 1 hour resolution
if(!RR.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H)
&& (earliestHourlyObservation == null || earliestHourlyObservation.after(RR.getTimeMeasured()))
)
{
continue;
}
// We keep the rain observation with the highest resolution // We keep the rain observation with the highest resolution
WeatherObservation obsWithSameTimeStamp = RRMap.get(RR.getTimeMeasured()); WeatherObservation obsWithSameTimeStamp = RRMap.get(RR.getTimeMeasured());
/*System.out.println("Timediff=" + (toTime.getTime() - fromTime.getTime())); //System.out.println("Timediff=" + (toTime.getTime() - fromTime.getTime()));
System.out.println(RR.getTimeMeasured() + ": " + RR.getValue()); //System.out.println(RR.getTimeMeasured() + ": " + RR.getValue());
System.out.println(RR.getLogIntervalId());*/ //System.out.println(RR.getLogIntervalId());
if(obsWithSameTimeStamp != null) if(obsWithSameTimeStamp != null)
{ {
// Replace if better resolution
if( if(
(RR.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_3H) && obsWithSameTimeStamp.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_6H)) (RR.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_3H) && obsWithSameTimeStamp.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_6H))
|| (RR.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H) && obsWithSameTimeStamp.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_3H)) || (RR.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H) && obsWithSameTimeStamp.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_3H))
...@@ -143,9 +171,14 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{ ...@@ -143,9 +171,14 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{
) )
{ {
RRMap.remove(RR.getTimeMeasured()); RRMap.remove(RR.getTimeMeasured());
RRMap.put(RR.getTimeMeasured(),RR);
} }
} }
RRMap.put(RR.getTimeMeasured(),RR); else
{
// No duplicate, so safely store
RRMap.put(RR.getTimeMeasured(),RR);
}
} }
} }
yrValues.addAll(RRMap.values()); yrValues.addAll(RRMap.values());
...@@ -169,6 +202,8 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{ ...@@ -169,6 +202,8 @@ public class YrWeatherForecastProvider implements WeatherForecastProvider{
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
for(WeatherObservation yrValue : yrValues) for(WeatherObservation yrValue : yrValues)
{ {
// Need to do this in order to make it work properly!
yrValue.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H);
switch(yrValue.getElementMeasurementTypeId()) switch(yrValue.getElementMeasurementTypeId())
{ {
case WeatherElements.TEMPERATURE_INSTANTANEOUS: case WeatherElements.TEMPERATURE_INSTANTANEOUS:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment