From 3637f06b59ab908aaa6231d082896c9f540cb69a Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@bioforsk.no>
Date: Mon, 17 Mar 2014 15:30:01 +0100
Subject: [PATCH] Added fallback calculation of leaf wetness

---
 .../model/applescabmodel/AppleScabModel.java  | 44 ++++++++++++++++---
 1 file changed, 39 insertions(+), 5 deletions(-)

diff --git a/src/main/java/no/bioforsk/vips/model/applescabmodel/AppleScabModel.java b/src/main/java/no/bioforsk/vips/model/applescabmodel/AppleScabModel.java
index da4a12a..5d237cf 100644
--- a/src/main/java/no/bioforsk/vips/model/applescabmodel/AppleScabModel.java
+++ b/src/main/java/no/bioforsk/vips/model/applescabmodel/AppleScabModel.java
@@ -85,9 +85,12 @@ public class AppleScabModel extends I18nImpl implements Model{
     private Date startDateAscosporeMaturity;
     
     // Weather data collections
-    private List<WeatherObservation> TM;
-    private List<WeatherObservation> RR;
-    private List<WeatherObservation> BT;
+    private List<WeatherObservation> TM; // Mean temperature
+    private List<WeatherObservation> RR; // Rainfall
+    private List<WeatherObservation> BT; // Leaf wetness
+    
+    // Helper for calculating if missing BT
+    private List<WeatherObservation> UM; // Relative humidity
     
     // Helper class
     private final WeatherUtil weatherUtil;
@@ -315,20 +318,50 @@ public class AppleScabModel extends I18nImpl implements Model{
                     this.BT.add(o); 
                     this.calculations.setParamDoubleValueForDate(o.getTimeMeasured(), AppleScabCalculations.BT, o.getValue());
                     break;
+                case WeatherElements.RELATIVE_HUMIDITY:
+                    this.UM.add(o); 
+                    break;
                 default:
-                    // TODO: Throw validation error? 
+                    // Let it pass in silence 
                     break;
             }
         }
         // TODO: Validate all input!!!!
 
+        // Problems with weather observations
+        // Unequal length of lists
         if  (        
                 this.RR.size() != this.TM.size()
                 || this.BT.size() != this.TM.size()
                 || this.RR.size() != this.TM.size()
             )
         {
-            throw new ConfigValidationException("Incorrect number of weather data. TM.size() = " + this.TM.size() + ", BT.size()=" + this.BT.size() +  ", RR.size()=" + this.RR.size() );
+            // Fallback if missing leaf wetness: If we have relative humidity. leaf wetness may be calculated
+            if(this.BT.size() != this.TM.size() && this.UM.size() == this.TM.size())
+            {
+                WeatherUtil wUtil = new WeatherUtil();
+                Collections.sort(this.TM);
+                Collections.sort(this.RR);
+                Collections.sort(this.UM);
+                this.BT = wUtil.calculateLeafWetnessHourSeriesSimple(this.TM, this.RR, this.UM);
+                
+                if(this.BT.size() == this.TM.size())
+                {
+                    // Need to set the values in result set
+                    for(WeatherObservation o: this.BT)
+                    {
+                        this.calculations.setParamDoubleValueForDate(o.getTimeMeasured(), AppleScabCalculations.BT, o.getValue());
+                    }
+                }
+                else
+                {
+                    throw new ConfigValidationException("Missing leaf wetness data. Also, attempting to calculate leaf wetness from other weather parameters failed.");
+                }
+            }
+            else
+            {
+                throw new ConfigValidationException("Incorrect number of weather data. TM.size() = " + this.TM.size() + ", BT.size()=" + this.BT.size() +  ", RR.size()=" + this.RR.size() );
+            }
         }
         
         
@@ -530,6 +563,7 @@ public class AppleScabModel extends I18nImpl implements Model{
         this.BT = new ArrayList();
         this.RR = new ArrayList();
         this.TM = new ArrayList();
+        this.UM = new ArrayList();
     }
     
     /**
-- 
GitLab