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

Changing dc calc to (TX+TN)/2

parent 92daac44
No related branches found
No related tags found
No related merge requests found
Pipeline #317 canceled
target/
classes/
nbproject/
.project
.classpath
.settings/
This diff is collapsed.
......@@ -26,7 +26,8 @@ import no.nibio.vips.util.DateMap;
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class DataMatrix extends DateMap{
public final static String TMD = "TM";
public final static String TND = "TN"; // min daily temp
public final static String TXD = "TX"; // max daily temp
public final static String DAILY_HEAT_SUM_CONTRIBUTION = "DC";
public final static String HEAT_SUM = "HEAT_SUM";
public final static String PHASE = "PHASE";
......
......@@ -25,10 +25,13 @@ import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -82,7 +85,7 @@ public class MaizePhenologyModel extends I18nImpl implements Model{
Date currentDate = this.sowingDate;
Calendar cal = Calendar.getInstance(this.timeZone);
Date lastDate = this.dataMatrix.getLastDateWithParameterValue(DataMatrix.TMD);
Date lastDate = this.dataMatrix.getLastDateWithParameterValue(DataMatrix.TND);
Double heatSum = 0.0;
List<Result> retVal = new ArrayList<>();
DecimalFormat dFormat = new DecimalFormat("###.##");
......@@ -90,13 +93,15 @@ public class MaizePhenologyModel extends I18nImpl implements Model{
{
Result result = new ResultImpl();
result.setValidTimeStart(currentDate);
Double TMD = this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.TMD);
Double dailyContribution = TMD != null ? this.getDailyContribution(TMD) // Default: All is well
Double TND = this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.TND);
Double TXD = this.dataMatrix.getParamDoubleValueForDate(currentDate, DataMatrix.TXD);
Double dailyContribution = (TND != null && TXD != null) ? this.getDailyContribution(TND,TXD) // Default: All is well
: retVal.size() > 0 ? Double.valueOf(retVal.get(retVal.size()-1).getValue(MaizePhenologyModel.MODEL_ID.toString(), DataMatrix.DAILY_HEAT_SUM_CONTRIBUTION)) // Simple copy from yesterday
: 0.0; // Panic
heatSum += dailyContribution != null ? dailyContribution : 0.0;
result.setValue(CommonNamespaces.NS_WEATHER, DataMatrix.TMD, TMD != null ? dFormat.format(TMD) : "null");
result.setValue(CommonNamespaces.NS_WEATHER, DataMatrix.TND, TND != null ? dFormat.format(TND) : "null");
result.setValue(CommonNamespaces.NS_WEATHER, DataMatrix.TXD, TXD != null ? dFormat.format(TXD) : "null");
result.setValue(MaizePhenologyModel.MODEL_ID.toString(), DataMatrix.DAILY_HEAT_SUM_CONTRIBUTION, dailyContribution != null ? dFormat.format(dailyContribution) : "null");
result.setValue(MaizePhenologyModel.MODEL_ID.toString(), DataMatrix.HEAT_SUM, dFormat.format(heatSum));
result.setValue(MaizePhenologyModel.MODEL_ID.toString(), DataMatrix.PHASE, this.phaseInfo.getPhase(heatSum).getPhaseName());
......@@ -111,11 +116,15 @@ public class MaizePhenologyModel extends I18nImpl implements Model{
return retVal;
}
private Double getDailyContribution(Double TMD)
private Double getDailyContribution(Double TND, Double TXD)
{
return TMD == null ? null
: TMD < this.baseTemp ? 0.0 :
TMD - this.baseTemp;
if(TND == null || TXD == null)
{
return null;
}
Double sortOfMean = (TND + TXD) / 2;
return sortOfMean < this.baseTemp ? 0.0 : sortOfMean - this.baseTemp;
}
......@@ -223,13 +232,36 @@ public class MaizePhenologyModel extends I18nImpl implements Model{
{
try
{
inputWeatherData = this.weatherUtil.getAggregatedDailyValues(inputWeatherData, timeZone, 15, WeatherUtil.AGGREGATION_TYPE_AVERAGE,1, true);
List<WeatherObservation> onlyMeanTemps = this.weatherUtil.filterWeatherObservationsByParameter(inputWeatherData, WeatherElements.TEMPERATURE_MEAN);
List<WeatherObservation> minTemps = this.weatherUtil.getAggregatedDailyValues(onlyMeanTemps, timeZone, 15, WeatherUtil.AGGREGATION_TYPE_MINIMUM,1, true);
this.weatherUtil.renameParameter(minTemps, WeatherElements.TEMPERATURE_MEAN, WeatherElements.TEMPERATURE_MINIMUM);
List<WeatherObservation> maxTemps = this.weatherUtil.getAggregatedDailyValues(onlyMeanTemps, timeZone, 15, WeatherUtil.AGGREGATION_TYPE_MAXIMUM,1, true);
this.weatherUtil.renameParameter(maxTemps, WeatherElements.TEMPERATURE_MEAN, WeatherElements.TEMPERATURE_MAXIMUM);
inputWeatherData = minTemps;
inputWeatherData.addAll(maxTemps);
}
catch(WeatherObservationListException | InvalidAggregationTypeException ex)
{
throw new ConfigValidationException("ERROR with weather data aggregation: " + ex.getMessage());
}
}
else
{
List<String> requiredParameters = List.of(WeatherElements.TEMPERATURE_MINIMUM, WeatherElements.TEMPERATURE_MAXIMUM);
// Check that we have daily min and max temps
Set<String> parametersFound = new HashSet<>();
for(WeatherObservation o:inputWeatherData)
{
if(o.getLogIntervalId().equals(WeatherObservation.LOG_INTERVAL_ID_1D))
{
parametersFound.add(o.getElementMeasurementTypeId());
}
}
if(! parametersFound.containsAll(requiredParameters))
{
throw new ConfigValidationException("ERROR with weather data: Missing daily values for minimum and/or maximum temperatures");
}
}
this.dataMatrix = new DataMatrix();
Collections.sort(inputWeatherData);
......@@ -240,22 +272,27 @@ public class MaizePhenologyModel extends I18nImpl implements Model{
System.out.println(om.writeValueAsString(inputWeatherData));
} catch (JsonProcessingException ex) {
Logger.getLogger(MaizePhenologyModel.class.getName()).log(Level.SEVERE, null, ex);
}
*/
}*/
for(WeatherObservation o: inputWeatherData)
{
if(o.getElementMeasurementTypeId().equals(WeatherElements.TEMPERATURE_MEAN))
if(o.getElementMeasurementTypeId().equals(WeatherElements.TEMPERATURE_MINIMUM))
{
//System.out.println(o);
this.dataMatrix.setParamDoubleValueForDate(o.getTimeMeasured(), DataMatrix.TND, o.getValue());
}
if(o.getElementMeasurementTypeId().equals(WeatherElements.TEMPERATURE_MAXIMUM))
{
//System.out.println(o);
this.dataMatrix.setParamDoubleValueForDate(o.getTimeMeasured(), DataMatrix.TMD, o.getValue());
this.dataMatrix.setParamDoubleValueForDate(o.getTimeMeasured(), DataMatrix.TXD, o.getValue());
}
}
// Check that sowing date is not before first daily temp
if(this.sowingDate.before(this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.TMD)))
if(this.sowingDate.before(this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.TND)))
{
throw new ConfigValidationException("ERROR: sowing date (" + this.sowingDate + ") is before weather data starts (" + this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.TMD) + ")");
throw new ConfigValidationException("ERROR: sowing date (" + this.sowingDate + ") is before weather data starts (" + this.dataMatrix.getFirstDateWithParameterValue(DataMatrix.TND) + ")");
}
if(config.getConfigParameter("phaseInfo") == null)
......
......@@ -67,29 +67,29 @@ public class MaizePhenologyModelTest {
WeatherDataFileReader wfr = new WeatherDataFileReader();
// Weather data files can be placed in ("src/test/resources")
//ModelConfiguration config = wfr.getModelConfigurationWithWeatherData("/weather_data/hourly_avg_temps_01_station_id_TA00644.json", MaizePhenologyModel.MODEL_ID.toString());
ModelConfiguration config = wfr.getModelConfigurationWithWeatherData("/weather_data/daily_avg_temps_01_station_id_TA00644.json", MaizePhenologyModel.MODEL_ID.toString());
ModelConfiguration config = wfr.getModelConfigurationWithWeatherData("/weather_data/hourly_avg_temps_01_station_id_TA00644.json", MaizePhenologyModel.MODEL_ID.toString());
// The timezone is used to set daily temperatures and biofix date correctly
config.setConfigParameter("timeZone", "GMT+01:00");
// The date for when to start calculating heat sums
config.setConfigParameter("sowingDate", "2020-11-18");
System.out.println(config.toJSON());
//System.out.println(config.toJSON());
instance.setConfiguration(config);
List<Result> result = instance.getResult();
/*
ObjectMapper om = new ObjectMapper();
try {
System.out.println(om.writeValueAsString(result));
} catch (JsonProcessingException ex) {
fail(ex);
}
*/
assertNotNull(result);
result.forEach(r->System.out.println(r));
//result.forEach(r->System.out.println(r));
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
......
[{
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-15T23:00:00Z",
"value": 28.808570075757572
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-16T23:00:00Z",
"value": 24.803124999999998
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-17T23:00:00Z",
"value": 23.585416666666664
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-18T23:00:00Z",
"value": 22.24021464646464
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-19T23:00:00Z",
"value": 21.294791666666672
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-20T23:00:00Z",
"value": 22.479513888888892
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-21T23:00:00Z",
"value": 23.483680555555555
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-22T23:00:00Z",
"value": 23.693749999999994
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-23T23:00:00Z",
"value": 24.222569444444446
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-24T23:00:00Z",
"value": 24.49965277777778
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-25T23:00:00Z",
"value": 24.984722222222228
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-26T23:00:00Z",
"value": 25.753472222222225
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-27T23:00:00Z",
"value": 26.517013888888894
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-28T23:00:00Z",
"value": 25.756597222222215
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-11-29T23:00:00Z",
"value": 24.773611111111112
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-12-01T23:00:00Z",
"value": 24.747045454545454
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-12-02T23:00:00Z",
"value": 23.485416666666666
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-12-03T23:00:00Z",
"value": 24.048611111111114
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-12-04T23:00:00Z",
"value": 25.025000000000002
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-12-05T23:00:00Z",
"value": 25.715277777777786
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-12-06T23:00:00Z",
"value": 25.610416666666666
}, {
"elementMeasurementTypeId": "TM",
"logIntervalId": 2,
"timeMeasured": "2020-12-07T23:00:00Z",
"value": 25.48472222222222
}]
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment