Skip to content
Snippets Groups Projects
Commit 7021b84a authored by Brita Linnestad's avatar Brita Linnestad
Browse files

Add logic for day degree sum calculation

parent e919ba7d
Branches
Tags
1 merge request!2Main
Pipeline #3690 passed
......@@ -18,10 +18,13 @@
package no.nibio.vips.model.daydegreemodel;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
......@@ -46,94 +49,69 @@ import no.nibio.vips.util.WeatherUtil;
* @author Tor J. Johansen <tor.johansen@nibio.no> (Model research)
* @author Tor-Einar Skog <tor-einar.skog@nibio.no> (Programming)
*/
public class DayDegreeModel extends I18nImpl implements Model {
public class DayDegreeModel implements Model {
public final static ModelId MODEL_ID = new ModelId("DAYDEGREES");
private final ModelUtil modelUtil;
private TimeZone timeZone;
private DataMatrix dataMatrix;
private List<WeatherObservation> TM; // Temporary storage of hourly values
private ModelUtil modelUtil;
//private Double dayDegreeBaseTemp;
private List<WeatherObservation> TM;
private final Double dayDegreeBaseTemp = 0.0;
// Threshold values
//private final Double THRESHOLD_1 = 260.0;
//private final Double THRESHOLD_2 = 360.0;
//private final Double THRESHOLD_3 = 560.0;
private final static TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("Europe/Oslo");
private DataMatrix dataMatrix;
public final static ModelId MODEL_ID = new ModelId("DAYDEGREES");
public DayDegreeModel()
{
super("no.nibio.vips.model.psilarosaetempmodel.texts");
//super("");
this.modelUtil = new ModelUtil();
}
@Override
public List<Result> getResult() throws ModelExcecutionException {
this.calculateTemperatureSum(DataMatrix.TMD, DataMatrix.TMD0C);
List<Result> retVal = new ArrayList<>();
Date currentDate = this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.TMD0C);
Calendar cal = Calendar.getInstance(this.timeZone);
DecimalFormat dFormat = new DecimalFormat("###.##");
Collections.sort(this.TM);
Double dayDegreeSum = 0.0;
while(this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.TMD0C) != null)
List<Result> results = new ArrayList<>();
Calendar cal = Calendar.getInstance(this.DEFAULT_TIME_ZONE);
DecimalFormat dFormat = new DecimalFormat("###.##");
for(WeatherObservation obs:this.TM)
{
Result result = new ResultImpl();
result.setValidTimeStart(currentDate);
Double TMCurrentDate = ((WeatherObservation) this.dataMatrix.getParamValueForDate(currentDate, DataMatrix.TMD)).getValue();
Double TMD0C = this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.TMD0C);
/*
result.setValue(CommonNamespaces.NS_WEATHER, DataMatrix.TMD, dFormat.format(TMCurrentDate));
result.setValue(CommonNamespaces.NS_WEATHER, DataMatrix.TMDD5C, dFormat.format(TMDD5C));
result.setValue(CommonNamespaces.NS_WEATHER, DataMatrix.TMD5C, dFormat.format(TMD5C));
result.setValue(PsilaRosaeTempModel.MODEL_ID.toString(), "THRESHOLD_1", dFormat.format(this.THRESHOLD_1));
result.setValue(PsilaRosaeTempModel.MODEL_ID.toString(), "THRESHOLD_2", dFormat.format(this.THRESHOLD_2));
result.setValue(PsilaRosaeTempModel.MODEL_ID.toString(), "THRESHOLD_3", dFormat.format(this.THRESHOLD_3));
Integer warningStatus = Result.WARNING_STATUS_NO_RISK;
if(TMDD5C >= this.THRESHOLD_1)
{
warningStatus = Result.WARNING_STATUS_MINOR_RISK;
}
if(TMDD5C >= this.THRESHOLD_2)
{
warningStatus = Result.WARNING_STATUS_HIGH_RISK;
}
if(TMDD5C >= this.THRESHOLD_3)
{
warningStatus = Result.WARNING_STATUS_NO_WARNING;
}
result.setWarningStatus(warningStatus);
retVal.add(result);
// Moving on...
cal.setTime(currentDate);
cal.add(Calendar.DATE, 1);
currentDate = cal.getTime();*/
}
if(obs.getValue() > 0)
{
dayDegreeSum += obs.getValue();
}
Result result = new ResultImpl();
result.setValidTimeStart(obs.getTimeMeasured());
result.setValue(this.getModelId().toString(), "DayDegreeSum", dayDegreeSum.toString());
return null;
//return retVal;
results.add(result);
}
return results;
}
@Override
public ModelId getModelId() {
return DayDegreeModel.MODEL_ID;
}
@Override
public String getModelName() {
return this.getModelName(Model.DEFAULT_LANGUAGE);
return null;//this.getModelName(Model.DEFAULT_LANGUAGE);
}
@Override
public String getModelName(String language) {
return this.getText("name", language);
//return this.getText("name", language);
return "Day Degree model";
}
@Override
......@@ -162,6 +140,7 @@ public class DayDegreeModel extends I18nImpl implements Model {
return "(c) 2015-2021 NIBIO (http://www.nibio.no/). Contact: post@nibio.no";
}
@Override
public String getModelDescription() {
return this.getModelDescription(Model.DEFAULT_LANGUAGE);
......@@ -169,39 +148,32 @@ public class DayDegreeModel extends I18nImpl implements Model {
@Override
public String getModelDescription(String language) {
try
{
return this.modelUtil.getTextWithBase64EncodedImages(this.getText("description", language), this.getClass());
}
catch(IOException ex)
{
return this.getText("description", language);
}
return null; //this.modelUtil.getTextWithBase64EncodedImages(this.getText("description", language), this.getClass());
}
@Override
public String getWarningStatusInterpretation() {
return this.getWarningStatusInterpretation(Model.DEFAULT_LANGUAGE);
return null;//this.getWarningStatusInterpretation(Model.DEFAULT_LANGUAGE);
}
@Override
public String getWarningStatusInterpretation(String language) {
return this.getText("statusInterpretation", language);
return null;//this.getText("statusInterpretation", language);
}
@Override
public String getModelUsage() {
return this.getModelUsage(Model.DEFAULT_LANGUAGE);
return null; //this.getModelUsage(Model.DEFAULT_LANGUAGE);
}
@Override
public String getModelUsage(String language) {
return this.getText("usage", language);
return null;//this.getText("usage", language);
}
@Override
public String getSampleConfig() {
System.out.println("Kjører i getSampleConfig------");
return "{\n" +
"\t\"loginInfo\":{\n" +
"\t\t\"username\":\"example\",\n" +
......@@ -209,7 +181,6 @@ public class DayDegreeModel extends I18nImpl implements Model {
"\t},\n" +
"\t\"modelId\":\"" + MODEL_ID.toString() + "\",\n" +
"\t\"configParameters\":{\n" +
"\t\t\"timeZone\": \"Europe/Oslo\",\n" +
"\t\t\"observations\":[\n" +
"\t\t{\n" +
"\t\t\t\t\"timeMeasured\": \"2015-01-01T00:00:00+02:00\",\n" +
......@@ -224,95 +195,35 @@ public class DayDegreeModel extends I18nImpl implements Model {
public void setConfiguration(ModelConfiguration config) throws ConfigValidationException {
// Initialize the weather data collections
this.TM = new ArrayList<>();
// Init data matrix
this.dataMatrix = new DataMatrix();
ObjectMapper mapper = new ObjectMapper();
// Setting timezone
this.timeZone = TimeZone.getTimeZone((String) config.getConfigParameter("timeZone"));
//System.out.println("TimeZone=" + this.timeZone);
List<WeatherObservation> observations = this.modelUtil.extractWeatherObservationList(config.getConfigParameter("observations"));
// Importing weather data, creating collections
// Can accept both hourly and daily data
// Must be complete set of daily mean temperatures
WeatherUtil wUtil = new WeatherUtil();
List<WeatherObservation> observations = modelUtil.extractWeatherObservationList(config.getConfigParameter("observations"));
if(observations == null || observations.isEmpty())
Date potentialHole = wUtil.findFirstHoleInObservationSeries(observations, WeatherObservation.LOG_INTERVAL_ID_1D, DayDegreeModel.DEFAULT_TIME_ZONE);
if(potentialHole != null)
{
throw new ConfigValidationException("Please provide weather data.");
throw new ConfigValidationException("Hole found in temperature series at = " + potentialHole);
}
for(WeatherObservation o:observations)
{
{
switch(o.getElementMeasurementTypeId())
{
case WeatherElements.TEMPERATURE_MEAN:
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1H))
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1D))
{
this.TM.add(o);
}else {
o.setTimeMeasured(wUtil.pragmaticAdjustmentToMidnight(o.getTimeMeasured(), timeZone));
this.dataMatrix.setParamValueForDate(o.getTimeMeasured(), DataMatrix.TMD, o);
}break;
this.TM.add(o);
}
break;
default:
// Keep calm and continue importing data
// TODO: Throw validation error?
break;
}
}
// If we've received hourly weather data, create and store daily values
// Air temperature
if(dataMatrix.getFirstDateWithParameterValue(DataMatrix.TMD) == null)
{
try {
List<WeatherObservation> dailyTemperatures = new WeatherUtil().getAggregatedDailyValues(
this.TM,
this.timeZone,
15,
WeatherUtil.AGGREGATION_TYPE_AVERAGE,
0,
true);
for(WeatherObservation obs:dailyTemperatures)
{
this.dataMatrix.setParamValueForDate(obs.getTimeMeasured(), DataMatrix.TMD, obs);
}
} catch (WeatherObservationListException | InvalidAggregationTypeException ex) {
throw new ConfigValidationException(ex.getMessage());
}
}
//System.out.println("DataMatrix");
//System.out.println(this.dataMatrix.toString());
}
/**
* Operates on the datamatrix
* @param inputParameterName the parameter to sum
* @param outputParameterName the result
*/
private void calculateTemperatureSum(String inputParameterName, String outputParameterName) throws ModelExcecutionException {
Date today = this.dataMatrix.getFirstDateWithParameterValue(inputParameterName);
Date lastDate = this.dataMatrix.getLastDateWithParameterValue(inputParameterName);
Calendar cal = Calendar.getInstance(this.timeZone);
Double sum = 0.0;
while(today.compareTo(lastDate) <= 0)
{
WeatherObservation todayTemp = (WeatherObservation)this.dataMatrix.getParamValueForDate(today, inputParameterName);
if(todayTemp == null)
{
throw new ModelExcecutionException("Missing weather data at " + today + ": " + inputParameterName);
}
//System.out.println("today=" + today + ",todayTemp=" + todayTemp);
Double dailyContribution = Math.max(0.0, todayTemp.getValue() - this.dayDegreeBaseTemp);
this.dataMatrix.setParamDoubleValueForDate(today, DataMatrix.TMD0C, dailyContribution);
sum += dailyContribution;
this.dataMatrix.setParamDoubleValueForDate(today, outputParameterName, sum);
cal.setTime(today);
cal.add(Calendar.DATE, 1);
today = cal.getTime();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment