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

Added a new weather station proxy for fruitweb/Davis

Added Latvian to available locales and Riga to available time zones
parent 6e7f19a8
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,8 @@ package no.nibio.vips.logic.service;
import com.ibm.icu.util.ULocale;
import java.util.TimeZone;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
......@@ -66,6 +68,7 @@ import no.nibio.vips.util.CSVPrintUtil;
import no.nibio.vips.util.ServletUtil;
import no.nibio.vips.util.SolarRadiationUtil;
import no.nibio.vips.util.weather.ALabDataParser;
import no.nibio.vips.util.weather.FruitWebDavisDataParser;
import no.nibio.vips.util.weather.MetosDataParser;
import no.nibio.vips.util.weather.ParseWeatherDataException;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
......@@ -537,6 +540,35 @@ public class LogicService {
return Response.ok().entity(observations).build();
}
@GET
@POST
@Path("weather/proxy/fruitwebdavis/{stationId}")
@Produces("application/json;charset=UTF-8")
public Response getFruitWebDavisWeatherData(
@PathParam("stationId") String stationId,
@FormParam("timeZone") String timeZonePOST,
@QueryParam("timeZone") String timeZoneGET,
@FormParam("startDate") String startDatePOST,
@QueryParam("startDate") String startDateGET
)
{
List<WeatherObservation> observations;
try
{
String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC";
TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(timeZone);
String startDateParam = startDatePOST != null ? startDatePOST : startDateGET;
Date startDate1 = format.parse(startDateParam);
observations = new FruitWebDavisDataParser().getWeatherObservations(URLDecoder.decode(stationId, "UTF-8"), timeZone, startDate1);
} catch (ParseException | ParseWeatherDataException | NullPointerException | UnsupportedEncodingException ex) {
return Response.serverError().entity(ex).build();
}
return Response.ok().entity(observations).build();
}
@GET
@POST
@Path("weather/proxy/alab/{stationId}")
......
......@@ -76,7 +76,9 @@ public class Globals {
"Europe/Sarajevo",
"Europe/Stockholm",
"Europe/Helsinki",
"Europe/Riga",
"Europe/Sofia"
};
public static int DEFAULT_UUID_VALIDITY_DURATION_DAYS = 30;
......
......@@ -22,6 +22,7 @@ package no.nibio.vips.util.weather;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.MessageFormat;
import java.text.ParseException;
......@@ -71,9 +72,9 @@ public class FruitWebDavisDataParser {
String[] headers;
Map<Integer, Integer> elementOrdering = new HashMap<>();
try {
URL metosURL = new URL(MessageFormat.format(FruitWebDavisDataParser.FRUITWEB_URL_TEMPLATE, stationID,urlDFormat.format(startDate)));
URL fruitwebDavisURL = new URL(MessageFormat.format(FruitWebDavisDataParser.FRUITWEB_URL_TEMPLATE, stationID,urlDFormat.format(startDate)));
BufferedReader in = new BufferedReader(
new InputStreamReader(metosURL.openStream()));
new InputStreamReader(fruitwebDavisURL.openStream()));
String inputLine;
Date testTimestamp;
......@@ -82,6 +83,7 @@ public class FruitWebDavisDataParser {
// If the resolution is 30 minutes or 1 hour
while ((inputLine = in.readLine()) != null)
{
//System.out.println(inputLine);
String[] lineData = inputLine.split(";");
// Skip empty lines
if(lineData.length <= 1)
......@@ -124,11 +126,12 @@ public class FruitWebDavisDataParser {
// Data comes in half-hour chunks (resolution = 30 minutes)
// Unlike Metos, the hour starts at :30
if(logIntervalId.equals(WeatherObservation.LOG_INTERVAL_ID_30M))
{
Boolean shouldBe00Now = true;
String[] data00 = null;
Boolean shouldBe30Now = true;
String[] data30 = null;
String[] data00 = null;
Date timestamp = null;
for(String[] lineData: data)
{
......@@ -140,7 +143,14 @@ public class FruitWebDavisDataParser {
{
continue;
}
else if(lineData[1].split(":")[1].equals("00") && shouldBe00Now)
else if(lineData[1].split(":")[1].equals("30") && shouldBe30Now)
{
data30 = lineData;
shouldBe30Now = false;
continue; // So that we summarize only after :00 data has been set too
}
else if(lineData[1].split(":")[1].equals("00") && !shouldBe30Now)
{
data00 = lineData;
try
......@@ -151,20 +161,14 @@ public class FruitWebDavisDataParser {
{
throw new ParseWeatherDataException("Error with time stamp in weather data from Davis/FruitWeb station: " + ex.getMessage());
}
shouldBe00Now = false;
continue; // So that we summarize only after :30 data has been set too
}
else if(lineData[1].split(":")[1].equals("30") && !shouldBe00Now)
{
data30 = lineData;
shouldBe00Now = true;
shouldBe30Now = true;
}
else
{
throw new ParseWeatherDataException("Doesn't make sense!");
throw new ParseWeatherDataException("Doesn't make sense at " + lineData[0] + " " + lineData[1] + "!");
}
for(Integer i=2;i<data00.length;i++)
for(Integer i=2;i<data30.length;i++)
{
Double aggregateValue = null;
Double value00 = Double.valueOf(data00[i].replaceAll(",","."));
......
......@@ -69,10 +69,10 @@ public class FruitWebDavisDataParserTest {
FruitWebDavisDataParser instance = new FruitWebDavisDataParser();
List<WeatherObservation> result = instance.getWeatherObservations(stationID, timeZone, startDate);
for(WeatherObservation obs:result)
/*for(WeatherObservation obs:result)
{
System.out.println(obs);
}
}*/
assertNotNull( result);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment