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

More weather data massaging stuff

parent c154f5a0
No related branches found
No related tags found
No related merge requests found
...@@ -95,7 +95,17 @@ public abstract class ModelRunPreprocessor { ...@@ -95,7 +95,17 @@ public abstract class ModelRunPreprocessor {
URLConn = weatherURL.openConnection(); URLConn = weatherURL.openConnection();
URLStream = URLConn.getInputStream(); URLStream = URLConn.getInputStream();
URLOutput = IOUtils.toString(URLStream); URLOutput = IOUtils.toString(URLStream);
return this.getWeatherObservations(URLOutput); List<WeatherObservation> preliminaryResult = this.getWeatherObservations(URLOutput);
// Need to filter only the values with correct resolution (logIntervalId)
List<WeatherObservation> filteredObservations = new ArrayList<>();
for(WeatherObservation candidateObs:preliminaryResult)
{
if(candidateObs.getLogIntervalId().equals(logIntervalId))
{
filteredObservations.add(candidateObs);
}
}
return filteredObservations;
}catch(IOException ex) }catch(IOException ex)
{ {
// Create error message to logging system // Create error message to logging system
...@@ -144,6 +154,23 @@ public abstract class ModelRunPreprocessor { ...@@ -144,6 +154,23 @@ public abstract class ModelRunPreprocessor {
return new ObjectMapper().readValue(JSONtext, new TypeReference<List<WeatherObservation>>(){}); return new ObjectMapper().readValue(JSONtext, new TypeReference<List<WeatherObservation>>(){});
} }
/**
* Fetches measured data from the stations weather data source, and optionally
* a weather forecast provider (if so specified in the weather station configuration).
* Regarding weather forecast parameters: All requested parameters need be present in the
* forecast in order for any parameters to be fetched. So if you request e.g. TJM5 and TM,
* you won't get forecast values for any of them, because TJM5 is not present. Solve this
* by calling this method twice: Once for the parameters with forecasts, and one for the
* remaining.
*
* @param station The WeatherStation to fetch data from
* @param logIntervalId hourly/daily etc.
* @param elementMeasurementTypes Which parameters should be fetched
* @param startTime When to start
* @param endTime When to stop
* @return
* @throws PreprocessorException
*/
protected List<WeatherObservation> getWeatherObservations(PointOfInterestWeatherStation station, Integer logIntervalId, String[] elementMeasurementTypes, Date startTime, Date endTime) throws PreprocessorException protected List<WeatherObservation> getWeatherObservations(PointOfInterestWeatherStation station, Integer logIntervalId, String[] elementMeasurementTypes, Date startTime, Date endTime) throws PreprocessorException
{ {
List<WeatherObservation> observations = this.getWeatherObservations(station.getDataFetchUri(), logIntervalId, elementMeasurementTypes, startTime, endTime, TimeZone.getTimeZone(station.getTimeZone())); List<WeatherObservation> observations = this.getWeatherObservations(station.getDataFetchUri(), logIntervalId, elementMeasurementTypes, startTime, endTime, TimeZone.getTimeZone(station.getTimeZone()));
......
...@@ -30,6 +30,7 @@ import java.util.Date; ...@@ -30,6 +30,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
...@@ -310,17 +311,25 @@ public class LogicService { ...@@ -310,17 +311,25 @@ public class LogicService {
@POST @POST
@Path("weather/proxy/metos/{stationId}") @Path("weather/proxy/metos/{stationId}")
@Produces("application/json;charset=UTF-8") @Produces("application/json;charset=UTF-8")
public Response getMetosWeatherData(@PathParam("stationId") String stationId, @QueryParam("timeZone") String timeZone, @QueryParam("startDate") String startDate) public Response getMetosWeatherData(
@PathParam("stationId") String stationId,
@FormParam("timeZone") String timeZonePOST,
@QueryParam("timeZone") String timeZoneGET,
@FormParam("startDate") String startDatePOST,
@QueryParam("startDate") String startDateGET
)
{ {
List<WeatherObservation> observations; List<WeatherObservation> observations;
try try
{ {
TimeZone timeZone1 = TimeZone.getTimeZone(timeZone); String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC";
TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(timeZone1); format.setTimeZone(timeZone);
Date startDate1 = format.parse(startDate); String startDateParam = startDatePOST != null ? startDatePOST : startDateGET;
observations = new MetosDataParser().getWeatherObservations(stationId, startDate1); Date startDate1 = format.parse(startDateParam);
} catch (ParseWeatherDataException | ParseException ex) { observations = new MetosDataParser().getWeatherObservations(stationId, timeZone, startDate1);
} catch (ParseException | ParseWeatherDataException | NullPointerException ex) {
return Response.serverError().entity(ex).build(); return Response.serverError().entity(ex).build();
} }
return Response.ok().entity(observations).build(); return Response.ok().entity(observations).build();
......
...@@ -31,6 +31,7 @@ import java.util.Date; ...@@ -31,6 +31,7 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TimeZone;
import no.bioforsk.vips.entity.WeatherObservation; import no.bioforsk.vips.entity.WeatherObservation;
/** /**
...@@ -61,10 +62,10 @@ public class MetosDataParser { ...@@ -61,10 +62,10 @@ public class MetosDataParser {
* @return * @return
* @throws ParseWeatherDataException * @throws ParseWeatherDataException
*/ */
public List<WeatherObservation> getWeatherObservations(String stationID, Date startDate) throws ParseWeatherDataException public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone, Date startDate) throws ParseWeatherDataException
{ {
List<WeatherObservation> retVal = new ArrayList<>(); List<WeatherObservation> retVal = new ArrayList<>();
List<WeatherObservation> allObservations = this.getWeatherObservations(stationID); List<WeatherObservation> allObservations = this.getWeatherObservations(stationID, timeZone);
for(WeatherObservation obs: allObservations) for(WeatherObservation obs: allObservations)
{ {
if(obs.getTimeMeasured().compareTo(startDate) >= 0) if(obs.getTimeMeasured().compareTo(startDate) >= 0)
...@@ -80,10 +81,11 @@ public class MetosDataParser { ...@@ -80,10 +81,11 @@ public class MetosDataParser {
* @param stationID the METOS station ID * @param stationID the METOS station ID
* @return * @return
*/ */
public List<WeatherObservation> getWeatherObservations(String stationID) throws ParseWeatherDataException public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone) throws ParseWeatherDataException
{ {
List<WeatherObservation> retVal = new ArrayList<>(); List<WeatherObservation> retVal = new ArrayList<>();
SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dFormat.setTimeZone(timeZone);
// Assuming 1 hour resolution until we find a timestamp that says :30 // Assuming 1 hour resolution until we find a timestamp that says :30
Integer logIntervalId = WeatherObservation.LOG_INTERVAL_ID_1H; Integer logIntervalId = WeatherObservation.LOG_INTERVAL_ID_1H;
List<String[]> data = new ArrayList<>(); List<String[]> data = new ArrayList<>();
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package no.bioforsk.vips.util.weather; package no.bioforsk.vips.util.weather;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import no.bioforsk.vips.entity.WeatherObservation; import no.bioforsk.vips.entity.WeatherObservation;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
...@@ -63,17 +64,17 @@ public class MetosDataParserTest { ...@@ -63,17 +64,17 @@ public class MetosDataParserTest {
MetosDataParser instance = new MetosDataParser(); MetosDataParser instance = new MetosDataParser();
//List<WeatherObservation> expResult = null; //List<WeatherObservation> expResult = null;
Integer expResult = 0; Integer expResult = 0;
List<WeatherObservation> result = instance.getWeatherObservations(stationID); List<WeatherObservation> result = instance.getWeatherObservations(stationID, TimeZone.getTimeZone("Europe/Sofia"));
assertNotNull(result); assertNotNull(result);
assertNotSame(expResult, result.size()); assertNotSame(expResult, result.size());
System.out.println("Result sample: " + result.get(10).toString()); //System.out.println("Result sample: " + result.get(10).toString());
// Testing different station with different parameters // Testing different station with different parameters
stationID = "00000428"; stationID = "00000428";
result = instance.getWeatherObservations(stationID); result = instance.getWeatherObservations(stationID, TimeZone.getTimeZone("Europe/Sarajevo"));
assertNotNull(result); assertNotNull(result);
assertNotSame(expResult, result.size()); assertNotSame(expResult, result.size());
System.out.println("Result sample: " + result.get(10).toString()); //System.out.println("Result sample: " + result.get(10).toString());
/*for(WeatherObservation obs:result) /*for(WeatherObservation obs:result)
{ {
System.out.println(obs.toString()); System.out.println(obs.toString());
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
*/ */
package no.bioforsk.vips.util.weather; package no.bioforsk.vips.util.weather;
import java.util.Collections;
import java.util.List; import java.util.List;
import no.bioforsk.vips.entity.WeatherObservation; import no.bioforsk.vips.entity.WeatherObservation;
import no.bioforsk.vips.logic.entity.PointOfInterestWeatherStation; import no.bioforsk.vips.logic.entity.PointOfInterestWeatherStation;
...@@ -81,6 +82,19 @@ public class YrWeatherForecastProviderTest { ...@@ -81,6 +82,19 @@ public class YrWeatherForecastProviderTest {
weatherStation.setLatitude(59.63851390); weatherStation.setLatitude(59.63851390);
result = instance.getWeatherForecasts(weatherStation); result = instance.getWeatherForecasts(weatherStation);
assertNotNull(result); assertNotNull(result);
// Testing Jablanica (Republika Srpska, Bosnia)
weatherStation.setAltitude(219.0);
weatherStation.setLongitude(16.988056);
weatherStation.setLatitude(45.115);
result = instance.getWeatherForecasts(weatherStation);
assertNotNull(result);
/**Collections.sort(result);
for(WeatherObservation obs:result)
{
System.out.println(obs.toString());
}*/
} }
catch(ParseWeatherDataException ex) catch(ParseWeatherDataException ex)
{ {
...@@ -90,11 +104,7 @@ public class YrWeatherForecastProviderTest { ...@@ -90,11 +104,7 @@ public class YrWeatherForecastProviderTest {
} }
} }
/*Collections.sort(result);
for(WeatherObservation obs:result)
{
System.out.println(obs.toString());
}*/
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment