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

Refactoring: Making SeptoriaHumidityModel runnable from batch system

parent 50d48ced
No related branches found
No related tags found
2 merge requests!17Develop,!13Spotit septoria map models
/*
* Copyright (c) 2019 NIBIO <http://www.nibio.no/>.
*
* This file is part of VIPSLogic.
* VIPSLogic is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* VIPSLogic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.logic.scheduling.model.preprocessor;
import java.util.Collections;
import java.util.List;
import no.nibio.vips.entity.ModelConfiguration;
import no.nibio.vips.entity.WeatherObservation;
import no.nibio.vips.logic.entity.ForecastConfiguration;
import no.nibio.vips.logic.entity.PointOfInterestWeatherStation;
import no.nibio.vips.logic.scheduling.model.ModelRunPreprocessor;
import no.nibio.vips.logic.scheduling.model.PreprocessorException;
import no.nibio.vips.util.WeatherElements;
import no.nibio.vips.util.weather.WeatherDataSourceException;
import no.nibio.vips.util.weather.WeatherDataSourceUtil;
/**
* @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class SeptoriaHumidityModelPreprocessor extends ModelRunPreprocessor{
@Override
public ModelConfiguration getModelConfiguration(ForecastConfiguration configuration) throws PreprocessorException
{
ModelConfiguration retVal = new ModelConfiguration();
retVal.setModelId(this.getModelId());
retVal.setConfigParameter("timeZone", configuration.getTimeZone());
WeatherDataSourceUtil wdsUtil = new WeatherDataSourceUtil();
try
{
List<WeatherObservation> observations = wdsUtil.getWeatherObservations(
(PointOfInterestWeatherStation) configuration.getWeatherStationPointOfInterestId(),
WeatherObservation.LOG_INTERVAL_ID_1H,
new String[]{
WeatherElements.TEMPERATURE_MEAN,
WeatherElements.PRECIPITATION,
WeatherElements.RELATIVE_HUMIDITY_MEAN,
WeatherElements.LEAF_WETNESS_DURATION
},
configuration.getDateStart(),
configuration.getDateEnd()
);
Collections.sort(observations);
retVal.setConfigParameter("observations", observations);
}
catch(WeatherDataSourceException ex)
{
throw new PreprocessorException(ex.getMessage());
}
configuration.getForecastModelConfigurationSet().forEach(c->
retVal.setConfigParameter(c.getForecastModelConfigurationPK().getModelConfigParameter(), c.getParameterValue())
);
return retVal;
}
@Override
public String getModelId() {
return "SEPTORIAHU";
}
}
...@@ -22,8 +22,12 @@ package no.nibio.vips.logic.service; ...@@ -22,8 +22,12 @@ package no.nibio.vips.logic.service;
import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Coordinate;
import com.webcohesion.enunciate.metadata.Facet; import com.webcohesion.enunciate.metadata.Facet;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
...@@ -33,9 +37,15 @@ import no.nibio.vips.entity.ModelConfiguration; ...@@ -33,9 +37,15 @@ import no.nibio.vips.entity.ModelConfiguration;
import no.nibio.vips.entity.Result; import no.nibio.vips.entity.Result;
import no.nibio.vips.entity.WeatherObservation; import no.nibio.vips.entity.WeatherObservation;
import no.nibio.vips.gis.GISUtil; import no.nibio.vips.gis.GISUtil;
import no.nibio.vips.logic.entity.ForecastConfiguration;
import no.nibio.vips.logic.entity.ForecastModelConfiguration;
import no.nibio.vips.logic.entity.ForecastModelConfigurationPK;
import no.nibio.vips.logic.entity.Organization; import no.nibio.vips.logic.entity.Organization;
import no.nibio.vips.logic.entity.PointOfInterestWeatherStation; import no.nibio.vips.logic.entity.PointOfInterestWeatherStation;
import no.nibio.vips.logic.entity.WeatherStationDataSource; import no.nibio.vips.logic.entity.WeatherStationDataSource;
import no.nibio.vips.logic.scheduling.model.PreprocessorException;
import no.nibio.vips.logic.scheduling.model.preprocessor.SeptoriaApiicolaModelPreprocessor;
import no.nibio.vips.logic.scheduling.model.preprocessor.SeptoriaHumidityModelPreprocessor;
import no.nibio.vips.logic.util.RunModelException; import no.nibio.vips.logic.util.RunModelException;
import no.nibio.vips.logic.util.SessionControllerGetter; import no.nibio.vips.logic.util.SessionControllerGetter;
import no.nibio.vips.logic.util.SystemTime; import no.nibio.vips.logic.util.SystemTime;
...@@ -79,29 +89,50 @@ public class ModelFormService { ...@@ -79,29 +89,50 @@ public class ModelFormService {
){ ){
try try
{ {
ModelConfiguration mConf = new ModelConfiguration(); ForecastConfiguration fConf = new ForecastConfiguration();
mConf.setModelId("SEPTORIAHU"); fConf.setModelId("SEPTORIAHU");
mConf.setConfigParameter("dateSpraying1", dateSpraying1);
mConf.setConfigParameter("dateSpraying2", dateSpraying2); Set<ForecastModelConfiguration> fModelConf = new HashSet<>();
mConf.setConfigParameter("dateGs31", dateGs31); fModelConf.add(this.getForecastModelConfiguration("dateSpraying1", dateSpraying1));
mConf.setConfigParameter("date3rdUpperLeafEmerging", date3rdUpperLeafEmerging); fModelConf.add(this.getForecastModelConfiguration("dateSpraying2", dateSpraying2));
mConf.setConfigParameter("date2ndUpperLeafEmerging", date2ndUpperLeafEmerging); fModelConf.add(this.getForecastModelConfiguration("dateGs31", dateGs31));
mConf.setConfigParameter("dateUpperLeafEmerging", dateUpperLeafEmerging); fModelConf.add(this.getForecastModelConfiguration("date3rdUpperLeafEmerging", date3rdUpperLeafEmerging));
mConf.setConfigParameter("dateGs75", dateGs75); fModelConf.add(this.getForecastModelConfiguration("date2ndUpperLeafEmerging", date2ndUpperLeafEmerging));
mConf.setConfigParameter("thresholdRelativeHumidity", thresholdRelativeHumidity); fModelConf.add(this.getForecastModelConfiguration("dateUpperLeafEmerging", dateUpperLeafEmerging));
mConf.setConfigParameter("thresholdLeafWetness", thresholdLeafWetness); fModelConf.add(this.getForecastModelConfiguration("dateGs75", dateGs75));
mConf.setConfigParameter("thresholdPrecipitation", thresholdPrecipitation); fModelConf.add(this.getForecastModelConfiguration("thresholdRelativeHumidity", String.valueOf(thresholdRelativeHumidity)));
mConf.setConfigParameter("slidingHoursPast", slidingHoursPast); fModelConf.add(this.getForecastModelConfiguration("thresholdLeafWetness", String.valueOf(thresholdLeafWetness)));
mConf.setConfigParameter("slidingHoursAhead", slidingHoursAhead); fModelConf.add(this.getForecastModelConfiguration("thresholdPrecipitation", String.valueOf(thresholdPrecipitation)));
mConf.setConfigParameter("thresholdHumidPeriodHours", thresholdHumidPeriodHours); fModelConf.add(this.getForecastModelConfiguration("slidingHoursPast", String.valueOf(slidingHoursPast)));
mConf.setConfigParameter("sprayingProtectionDays", sprayingProtectionDays); fModelConf.add(this.getForecastModelConfiguration("slidingHoursAhead", String.valueOf(slidingHoursAhead)));
mConf.setConfigParameter("leafLifeTime", leafLifeTime); fModelConf.add(this.getForecastModelConfiguration("thresholdHumidPeriodHours", String.valueOf(thresholdHumidPeriodHours)));
fModelConf.add(this.getForecastModelConfiguration("sprayingProtectionDays", String.valueOf(sprayingProtectionDays)));
fModelConf.add(this.getForecastModelConfiguration("leafLifeTime", String.valueOf(leafLifeTime)));
fConf.setForecastModelConfigurationSet(fModelConf);
//ModelConfiguration mConf = new ModelConfiguration();
//mConf.setModelId("SEPTORIAHU");
//mConf.setConfigParameter("dateSpraying1", dateSpraying1);
//mConf.setConfigParameter("dateSpraying2", dateSpraying2);
//mConf.setConfigParameter("dateGs31", dateGs31);
//mConf.setConfigParameter("date3rdUpperLeafEmerging", date3rdUpperLeafEmerging);
//mConf.setConfigParameter("date2ndUpperLeafEmerging", date2ndUpperLeafEmerging);
//mConf.setConfigParameter("dateUpperLeafEmerging", dateUpperLeafEmerging);
//mConf.setConfigParameter("dateGs75", dateGs75);
//mConf.setConfigParameter("thresholdRelativeHumidity", thresholdRelativeHumidity);
//mConf.setConfigParameter("thresholdLeafWetness", thresholdLeafWetness);
//mConf.setConfigParameter("thresholdPrecipitation", thresholdPrecipitation);
//mConf.setConfigParameter("slidingHoursPast", slidingHoursPast);
//mConf.setConfigParameter("slidingHoursAhead", slidingHoursAhead);
//mConf.setConfigParameter("thresholdHumidPeriodHours", thresholdHumidPeriodHours);
//mConf.setConfigParameter("sprayingProtectionDays", sprayingProtectionDays);
//mConf.setConfigParameter("leafLifeTime", leafLifeTime);
// Data parsing // Data parsing
Integer organizationId = Integer.valueOf(organizationId_countryCode.split("_")[0]); Integer organizationId = Integer.valueOf(organizationId_countryCode.split("_")[0]);
Organization organization = SessionControllerGetter.getUserBean().getOrganization(organizationId); Organization organization = SessionControllerGetter.getUserBean().getOrganization(organizationId);
mConf.setConfigParameter("timeZone", organization.getDefaultTimeZone()); fConf.setTimeZone(organization.getDefaultTimeZone());
TimeZone timeZone = TimeZone.getTimeZone(organization.getDefaultTimeZone()); TimeZone timeZone = TimeZone.getTimeZone(organization.getDefaultTimeZone());
ParseRESTParamUtil pUtil = new ParseRESTParamUtil(); ParseRESTParamUtil pUtil = new ParseRESTParamUtil();
// Start time is gs31, easy // Start time is gs31, easy
...@@ -116,6 +147,9 @@ public class ModelFormService { ...@@ -116,6 +147,9 @@ public class ModelFormService {
dayAfterTomorrow = new XDate(wUtil.pragmaticAdjustmentToMidnight(dayAfterTomorrow, timeZone)); dayAfterTomorrow = new XDate(wUtil.pragmaticAdjustmentToMidnight(dayAfterTomorrow, timeZone));
// The first check here is to see if the systemtime is too early // The first check here is to see if the systemtime is too early
Date endTime = dayAfterTomorrow.after(gs75) ? gs75 : dayAfterTomorrow; Date endTime = dayAfterTomorrow.after(gs75) ? gs75 : dayAfterTomorrow;
fConf.setDateStart(startTime);
fConf.setDateEnd(endTime);
String countryCode = organizationId_countryCode.split("_")[1]; String countryCode = organizationId_countryCode.split("_")[1];
List<WeatherObservation> observations; List<WeatherObservation> observations;
...@@ -143,19 +177,10 @@ public class ModelFormService { ...@@ -143,19 +177,10 @@ public class ModelFormService {
// Weather station id maps to a regular weather station // Weather station id maps to a regular weather station
ws = (PointOfInterestWeatherStation) SessionControllerGetter.getPointOfInterestBean().getPointOfInterest(Integer.valueOf(weatherStationId)); ws = (PointOfInterestWeatherStation) SessionControllerGetter.getPointOfInterestBean().getPointOfInterest(Integer.valueOf(weatherStationId));
} }
observations = wdsUtil.getWeatherObservations( fConf.setWeatherStationPointOfInterestId(ws);
ws, fConf.setLocationPointOfInterestId(ws);
WeatherObservation.LOG_INTERVAL_ID_1H,
new String[]{ ModelConfiguration mConf = new SeptoriaHumidityModelPreprocessor().getModelConfiguration(fConf);
WeatherElements.TEMPERATURE_MEAN,
WeatherElements.PRECIPITATION,
WeatherElements.RELATIVE_HUMIDITY_MEAN,
WeatherElements.LEAF_WETNESS_DURATION
},
startTime,
endTime
);
mConf.setConfigParameter("observations",observations);
Integer VIPSCoreUserId = organization.getDefaultVipsCoreUserId(); Integer VIPSCoreUserId = organization.getDefaultVipsCoreUserId();
...@@ -163,9 +188,16 @@ public class ModelFormService { ...@@ -163,9 +188,16 @@ public class ModelFormService {
return Response.ok().entity(results).build(); return Response.ok().entity(results).build();
} }
catch(WeatherDataSourceException | RunModelException ex) catch(PreprocessorException |RunModelException ex)
{ {
return Response.serverError().entity(ex.getMessage()).build(); return Response.serverError().entity(ex.getMessage()).build();
} }
}
private ForecastModelConfiguration getForecastModelConfiguration(String key, String value)
{
ForecastModelConfiguration retVal = new ForecastModelConfiguration(new ForecastModelConfigurationPK(-1, key));
retVal.setParameterValue(value);
return retVal;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment