Skip to content
Snippets Groups Projects
Commit b5789191 authored by Lene Wasskog's avatar Lene Wasskog
Browse files

feat: Use organizations default grid weather station data source to get weather data for point

parent 49e27836
Branches
No related tags found
1 merge request!191Add map module and Open-Meteo support
...@@ -170,18 +170,24 @@ public class BarleyNetBlotchModelService { ...@@ -170,18 +170,24 @@ public class BarleyNetBlotchModelService {
Double preparationDose = parseUtil.parseDouble(preparationDoseStr); Double preparationDose = parseUtil.parseDouble(preparationDoseStr);
Double latitudeDouble = parseUtil.parseDouble(latitude); Double latitudeDouble = parseUtil.parseDouble(latitude);
Double longitudeDouble = parseUtil.parseDouble(longitude); Double longitudeDouble = parseUtil.parseDouble(longitude);
if(weatherDataSourceType == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("WeatherDataSourceType (grid or weather station) is required").build();
}
// Must get the VIPSCore user id for this organization
Organization org = em.find(Organization.class, organizationId);
Integer VIPSCoreUserId = org.getDefaultVipsCoreUserId();
// Build model configuration // Build model configuration
ModelConfiguration config = new ModelConfiguration(); ModelConfiguration config = new ModelConfiguration();
config.setModelId("BARLEYNETB"); config.setModelId("BARLEYNETB");
// Get weather data from weather station
WeatherDataSourceUtil wsdUtil = new WeatherDataSourceUtil(); WeatherDataSourceUtil wsdUtil = new WeatherDataSourceUtil();
Date endDateForWeatherData = calculateEndDateForWeatherData(timeZone, sowingDate); Date endDateForWeatherData = calculateEndDateForWeatherData(timeZone, sowingDate);
List<WeatherObservation> observations;
if (weatherDataSourceType != null && "weatherstation".equals(weatherDataSourceType.trim())) { List<WeatherObservation> observations = new ArrayList<>();
if ("weatherstation".equals(weatherDataSourceType.trim())) {
PointOfInterestWeatherStation weatherStation = PointOfInterestWeatherStation weatherStation =
em.find(PointOfInterestWeatherStation.class, weatherStationId); em.find(PointOfInterestWeatherStation.class, weatherStationId);
LOGGER.info("Run model with weatherdata from weatherstation {}", weatherStation.getName()); LOGGER.info("Run model with weatherdata from weatherstation {}", weatherStation.getName());
...@@ -203,14 +209,12 @@ public class BarleyNetBlotchModelService { ...@@ -203,14 +209,12 @@ public class BarleyNetBlotchModelService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Could not find weather data for weather station with id=" + weatherStationId).build(); .entity("Could not find weather data for weather station with id=" + weatherStationId).build();
} }
} else { } else if("grid".equals(weatherDataSourceType.trim())) {
PointOfInterest coordinates = new PointOfInterest();
coordinates.setLatitude(latitudeDouble);
coordinates.setLongitude(longitudeDouble);
LOGGER.info("Run model with weatherdata for latitude={} and longitude={}", latitude, longitude); LOGGER.info("Run model with weatherdata for latitude={} and longitude={}", latitude, longitude);
String datafetchUriExpression =
org.getDefaultGridWeatherStationDataSource().getDatafetchUriExpression();
try { try {
observations = wsdUtil.getWeatherObservations( observations = wsdUtil.getWeatherObservations(String.format(datafetchUriExpression, longitude + "_" + latitude),
"https://weather.vips.nibio.no/rest/grid/openmeteo/" + coordinates.getLongitude() + "_" + coordinates.getLatitude(),
WeatherObservation.LOG_INTERVAL_ID_1H, WeatherObservation.LOG_INTERVAL_ID_1H,
new String[] { new String[] {
WeatherElements.TEMPERATURE_MEAN, WeatherElements.TEMPERATURE_MEAN,
...@@ -223,15 +227,15 @@ public class BarleyNetBlotchModelService { ...@@ -223,15 +227,15 @@ public class BarleyNetBlotchModelService {
new HashSet<>(Collections.singletonList(WeatherObservation.LOG_INTERVAL_ID_1H)) new HashSet<>(Collections.singletonList(WeatherObservation.LOG_INTERVAL_ID_1H))
); );
} catch (WeatherDataSourceException ex) { } catch (WeatherDataSourceException ex) {
LOGGER.error("Exception while getting observations for latitude={} longitude={}", latitude, longitude, ex); LOGGER.error("Exception while getting observations for lat={} lon={}", latitude, longitude, ex);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
} }
if (observations == null || observations.isEmpty()) { if (observations == null || observations.isEmpty()) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Could not find weather data for weather station with id=" + weatherStationId).build(); .entity(String.format("Could not find weather data for lat=%s lon=%s between %s and %s", latitude, longitude, sowingDate, endDateForWeatherData)).build();
} }
} }
// Mandatory parameters // Mandatory parameters
config.setConfigParameter("observations", observations); config.setConfigParameter("observations", observations);
config.setConfigParameter("timeZone", timeZone.getID()); config.setConfigParameter("timeZone", timeZone.getID());
...@@ -257,11 +261,6 @@ public class BarleyNetBlotchModelService { ...@@ -257,11 +261,6 @@ public class BarleyNetBlotchModelService {
config.setConfigParameter("spraying", spraying); config.setConfigParameter("spraying", spraying);
} }
// Must get the VIPSCore user id for this organization
Organization org = em.find(Organization.class, organizationId);
Integer VIPSCoreUserId = org.getDefaultVipsCoreUserId();
List<Result> results; List<Result> results;
try try
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment