diff --git a/src/main/java/no/nibio/vips/logic/scheduling/model/preprocessor/FinnCerealModelsPreprocessor.java b/src/main/java/no/nibio/vips/logic/scheduling/model/preprocessor/FinnCerealModelsPreprocessor.java new file mode 100644 index 0000000000000000000000000000000000000000..790d467bb0fe60f94a173561336098e7bdf480bd --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/scheduling/model/preprocessor/FinnCerealModelsPreprocessor.java @@ -0,0 +1,95 @@ +package no.nibio.vips.logic.scheduling.model.preprocessor; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; + +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.model.ConfigValidationException; +import no.nibio.vips.util.WeatherElements; +import no.nibio.vips.util.WeatherObservationListException; +import no.nibio.vips.util.WeatherUtil; +import no.nibio.vips.util.weather.WeatherDataSourceException; +import no.nibio.vips.util.weather.WeatherDataSourceUtil; + +public class FinnCerealModelsPreprocessor extends ModelRunPreprocessor { + + public static final String SOWING_DATE ="FINNCEREAL_FIELD_SOWING_DATE"; + public static final String PREVIOUS_CROP ="FINNCEREAL_PREVIOUS_CROP"; + public static final String TILLAGE ="FINNCEREAL_TILLAGE_METHOD"; + public static final String SUSCEPTIBILITY ="FINNCEREAL_CROP_SUSCEPTIBILITY"; + public static final String MODEL ="FINNCEREAL_MODEL_NAME"; + + /** + * This method implementation is based on the DOWNCASTModelPreprocessor implemention of getModelCOnfiguration. + */ + @Override + public ModelConfiguration getModelConfiguration(ForecastConfiguration configuration) throws PreprocessorException { + ModelConfiguration config = new ModelConfiguration(); + PointOfInterestWeatherStation weatherStation = (PointOfInterestWeatherStation) configuration.getWeatherStationPointOfInterestId(); + WeatherDataSourceUtil wdsUtil = new WeatherDataSourceUtil(); + WeatherUtil wUtil = new WeatherUtil(); + + List<WeatherObservation> observations; + Date sowingDate = null; + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + + try { + sowingDate = format.parse(configuration.getForecastModelConfigurationValue(SOWING_DATE)); + observations = wdsUtil.getWeatherObservations( + weatherStation, + WeatherObservation.LOG_INTERVAL_ID_1H, + new String[]{ + WeatherElements.WIND_SPEED_2M, + WeatherElements.WIND_SPEED_10MIN_2M, + WeatherElements.RELATIVE_HUMIDITY_MEAN, + WeatherElements.PRECIPITATION, + WeatherElements.TEMPERATURE_MEAN + }, + sowingDate, + configuration.getDateEndInTimeZone()); + observations = wUtil.checkForAndFixHourlyTimeSeriesHoles(observations); + List<WeatherObservation> temperature = observations.stream().filter(t -> t.getElementMeasurementTypeId().equals(WeatherElements.TEMPERATURE_MEAN)) + .collect(Collectors.toList()); + List<WeatherObservation> rainfall = observations.stream().filter(t -> t.getElementMeasurementTypeId().equals(WeatherElements.PRECIPITATION)) + .collect(Collectors.toList()); + List<WeatherObservation> rh = observations.stream().filter(t -> t.getElementMeasurementTypeId().equals(WeatherElements.RELATIVE_HUMIDITY_MEAN)) + .collect(Collectors.toList()); + List<WeatherObservation> windSpeedAll = observations.stream().filter(t -> t.getElementMeasurementTypeId().equals(WeatherElements.WIND_SPEED_2M)) + .collect(Collectors.toList()); + if(windSpeedAll.isEmpty()) { + windSpeedAll = observations.stream().filter(t -> t.getElementMeasurementTypeId().equals(WeatherElements.WIND_SPEED_10MIN_2M)) + .collect(Collectors.toList()); + } + config.setModelId(this.getModelId()); + config.setConfigParameter("timeZone", weatherStation.getTimeZone()); + config.setConfigParameter("temperature", temperature); + config.setConfigParameter("rainfall", rainfall); + config.setConfigParameter("rh", rh); + config.setConfigParameter("windspeed", windSpeedAll); + config.setConfigParameter("precedingCrop", configuration.getForecastModelConfigurationValue(PREVIOUS_CROP)); + config.setConfigParameter("tillageMethod", configuration.getForecastModelConfigurationValue(TILLAGE)); + config.setConfigParameter("diseaseSusceptibility", configuration.getForecastModelConfigurationValue(SUSCEPTIBILITY)); + config.setConfigParameter("model", configuration.getForecastModelConfigurationValue(MODEL)); + } catch (WeatherDataSourceException | WeatherObservationListException | ParseException ex ) { + throw new PreprocessorException(ex.getMessage()); + } + return config; + } + + @Override + public String getModelId() { + return "FINNCEREAL"; + } + +} diff --git a/src/main/webapp/formdefinitions/models/FINNCEREAL.json b/src/main/webapp/formdefinitions/models/FINNCEREAL.json new file mode 100644 index 0000000000000000000000000000000000000000..9a2abf234c5d737dfb3bd6059fdb6dee3eaddc64 --- /dev/null +++ b/src/main/webapp/formdefinitions/models/FINNCEREAL.json @@ -0,0 +1,77 @@ +{ + "_licensenbte": [ + "Copyright (c) 2014 NIBIO <http://www.nibio.nb/>. ", + "", + "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 nbt, see <http://www.nibio.nb/licenses/>. " + ], + "_comment" : "Sowing date, previous crop, tillage method, and current crop disease susceptibility for the specific field", + "fields": [ + { + "name" : "modelName", + "dataType" : "STRING", + "fieldType" : "SELECT_SINGLE", + "required" : true, + "options": [ + {"value":"PyrenophoraTeres", "label":{"en":"Net Blotch","nb":"?","fi":"Verkkolaikku"}, "selected":"true"}, + {"value":"DrechsleraTriticiRepentis", "label":{"en":"Tan Spot","nb":"","fi":"Ruskolaikku"}}, + {"value":"StagonosporaNodorum", "label":{"en" : "Stagonspora spot", "nb":"?", "fi":"Pistelaikku"}} + ] + }, + { + "name" : "fieldSowingDate", + "dataType" : "DATE", + "dateFormat" : "yyyy-MM-dd", + "required" : true + }, + { + "name" : "previousCrop", + "dataType" : "STRING", + "fieldType" : "SELECT_SINGLE", + "required" : true, + "options": [ + {"value":"Wheat", "label":{"en":"Wheat","nb":"Hvete","fi":"Vehn�"}, "selected":"true"}, + {"value":"Barley", "label":{"en":"Barley","nb":"Barley","fi":"Kaura"}}, + {"value":"Oats", "label":{"en" : "Oats", "nb":"?", "fi":"Ohra"}}, + {"value":"Grass", "label":{"en" : "Grass", "nb":"?","fi":"Hein�"}}, + {"value":"Rapeseed", "label":{"en" : "Turnip rape", "nb":"?","fi":"Rypsi"}}, + {"value":"SugarBeet", "label":{"en" : "Sugar beet", "nb":"?","fi":"Sokerijuurikas"}}, + {"value":"Legumes", "label":{"en" : "Legumes", "nb":"?","fi":"Herneet"}} + ] + }, + { + "name" : "tillageMethod", + "dataType" : "STRING", + "fieldType" : "SELECT_SINGLE", + "required" : true, + "options": [ + {"value":"Intensive", "label":{"en":"Ploughing","nb":"?","fi":"Kynt�"}, "selected":"true"}, + {"value":"Reduced", "label":{"en":"Reduced tillage","nb":"?","fi":"Kevytmuokkaus"}}, + {"value":"nbne", "label":{"en" : "Direct seeding", "nb":"?","fi":"Suorakylv�"}} + ] + }, + { + "name" : "cropSusceptibility", + "dataType" : "STRING", + "fieldType" : "SELECT_SINGLE", + "required" : true, + "options": [ + {"value":"nbrmal", "label":{"en":"nbrmal","nb":"?", "fi":"normaali"}, "selected":"true"}, + {"value":"Resistant", "label":{"en":"Resistant","nb":"?","fi":"Resistentti"}}, + {"value":"Sensitive", "label":{"en" : "Susceptible", "nb":"?", "fi":"Haavoittuva"}} + ] + } + + ] +} \ No newline at end of file