Skip to content
Snippets Groups Projects

Model leaf

Merged Tor-Einar Skog requested to merge modelLeaf into develop
3 files
+ 151
1
Compare changes
  • Side-by-side
  • Inline
Files
3
/*
* Copyright (c) 2022 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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.logging.Logger;
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.logic.util.SystemTime;
import no.nibio.vips.util.WeatherElements;
import no.nibio.vips.util.WeatherUtil;
import no.nibio.vips.util.weather.WeatherDataSourceException;
import no.nibio.vips.util.weather.WeatherDataSourceUtil;
/**
*
* @author mila
*/
public class LeafBlotchModelPreprocessor extends ModelRunPreprocessor{
public final static String LEAFBLOTCH_sowingDate = "LEAFBLOTCH_SOWING_DATE";
@Override
public ModelConfiguration getModelConfiguration(ForecastConfiguration configuration) throws PreprocessorException {
PointOfInterestWeatherStation weatherStation = (PointOfInterestWeatherStation) configuration.getWeatherStationPointOfInterestId();
//Latitude for the calculation
Double latitude = weatherStation.getLatitude();
// What timezone is the calculation for
TimeZone timeZone = TimeZone.getTimeZone(weatherStation.getTimeZone());
Calendar cal = Calendar.getInstance(timeZone);
cal.setTime(SystemTime.getSystemTime());
cal.add(Calendar.DATE, 3);
WeatherUtil wUtil = new WeatherUtil();
Date endDate = wUtil.normalizeToExactDate(cal.getTime(), timeZone);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(timeZone);
Date startDate = null;
try {
startDate = format.parse(configuration.getForecastModelConfigurationValue(LEAFBLOTCH_sowingDate));
} catch (ParseException ex) {
//Logger.getLogger(LeafBlotchModelPreprocess.class.getName()).log(Level.SEVERE, null, ex);
}
ModelConfiguration retVal = new ModelConfiguration();
WeatherDataSourceUtil wdsUtil = new WeatherDataSourceUtil();
//Hourly weather observations:
List<WeatherObservation> observations;
try {
observations = wdsUtil.getWeatherObservations(
weatherStation,
WeatherObservation.LOG_INTERVAL_ID_1H,
new String[]{
WeatherElements.TEMPERATURE_MEAN,
WeatherElements.PRECIPITATION,
WeatherElements.RELATIVE_HUMIDITY_MEAN
},
startDate,
endDate);
} catch (WeatherDataSourceException ex) {
throw new PreprocessorException(ex.getMessage());
}
//Daily weather observations:
List<WeatherObservation> dailyObs;
try {
dailyObs = wdsUtil.getWeatherObservations(
weatherStation,
WeatherObservation.LOG_INTERVAL_ID_1D,
new String[]{
WeatherElements.TEMPERATURE_MEAN
},
startDate,
endDate);
} catch (WeatherDataSourceException ex) {
throw new PreprocessorException(ex.getMessage());
}
//Combining hourly and daily data
observations.addAll(dailyObs);
retVal.setModelId(this.getModelId());
retVal.setConfigParameter("timeZone", timeZone.getID());
retVal.setConfigParameter("observations", observations);
retVal.setConfigParameter("latitude", latitude);
retVal.setConfigParameter("sowingDate", format.format(startDate));
return retVal;
}
@Override
public String getModelId() {
return "LEAFBLOTCH";
}
}
Loading