From 10bea85a4f250b214a2ab8b03e501cf12b8ece2f Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Tue, 6 Nov 2018 12:12:37 +0100
Subject: [PATCH] Fixing relation to new weather service

---
 .../modules/roughage/RoughageService.java     | 28 +++++++++++++------
 ...ptoriaSimpleRiskGridModelPreprocessor.java |  4 ++-
 .../util/weather/WeatherDataSourceUtil.java   |  4 ++-
 3 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/src/main/java/no/nibio/vips/logic/modules/roughage/RoughageService.java b/src/main/java/no/nibio/vips/logic/modules/roughage/RoughageService.java
index c8cd224a..a3a4ddfb 100755
--- a/src/main/java/no/nibio/vips/logic/modules/roughage/RoughageService.java
+++ b/src/main/java/no/nibio/vips/logic/modules/roughage/RoughageService.java
@@ -167,6 +167,7 @@ public class RoughageService {
             // We must do some special adjustmens to get weather forecasts from 3-9 days in 
             // the future
             // For EPP and Q0 we simply add the last value
+            //System.out.println("observations.size=" + observations.size());
             WeatherObservation lastEPP = wUtil.getLastObservations(observations, "EPP", 1).get(0);
             Date lastEPPDate = lastEPP.getTimeMeasured();
             while(lastEPPDate.before(dateOfLastWeatherData))
@@ -192,7 +193,7 @@ public class RoughageService {
 
             // For TM and RR we get long time forecasts from LMT (Norwegian service only)
             try {
-                //System.out.println("Last TM dated " + wUtil.getLastObservations(observations, "TM", 1).get(0).getTimeMeasured());
+                //System.out.println("dateOfLastWeatherData=" + dateOfLastWeatherData + ", last TM dated " + wUtil.getLastObservations(observations, "TM", 1).get(0).getTimeMeasured());
                 cal.setTime(wUtil.getLastObservations(observations, "TM", 1).get(0).getTimeMeasured());
                 cal.add(Calendar.DATE, 1);
                 Date firstLongTermForecastTime = cal.getTime();
@@ -222,7 +223,10 @@ public class RoughageService {
                     }*/
 
                     List<WeatherObservation> TMDLongTime = wUtil.getAggregatedDailyValues(TMLongTime, timeZone, 15, WeatherUtil.AGGREGATION_TYPE_AVERAGE);
-                    observations.addAll(TMDLongTime);
+                    if(TMDLongTime != null)
+                    {
+                        observations.addAll(TMDLongTime);
+                    }
 
 
 
@@ -240,7 +244,10 @@ public class RoughageService {
                     RRLongTime = wUtil.fillHourlyHolesBruteForce(RRLongTime, WeatherUtil.AGGREGATION_TYPE_SUM, firstLongTermForecastTime, dateOfLastWeatherData);
 
                     List<WeatherObservation> RRDLongTime = wUtil.getAggregatedDailyValues(RRLongTime, timeZone, 15, WeatherUtil.AGGREGATION_TYPE_SUM);
-                    observations.addAll(RRDLongTime);
+                    if(RRDLongTime != null)
+                    {
+                        observations.addAll(RRDLongTime);
+                    }
                 }
             }
             catch(WeatherDataSourceException | InvalidAggregationTypeException | WeatherObservationListException ex)
@@ -394,15 +401,18 @@ public class RoughageService {
                 // Last date to fetch data 
                 Date latestObsDate = wUtil.getLastObservations(observations, "TM", 1).get(0).getTimeMeasured();
                 weatherStation = em.find(PointOfInterestWeatherStation.class, normalDataWeatherStationId);
+                weatherStation.getWeatherStationDataSourceId().setDatafetchUriExpression(
+                        weatherStation.getWeatherStationDataSourceId().getDatafetchUriExpression().replaceFirst("forecastfallback", "grovfornormal")
+                );
                 observations = wsdUtil.getWeatherObservations(
                     weatherStation,
                     WeatherObservation.LOG_INTERVAL_ID_1D,
                     new String[]{
-                        WeatherElements.getNormalDataParameter(WeatherElements.TEMPERATURE_MEAN),
-                        WeatherElements.getNormalDataParameter(WeatherElements.PRECIPITATION),
-                        WeatherElements.getNormalDataParameter(WeatherElements.GLOBAL_RADIATION),
-                        WeatherElements.getNormalDataParameter(WeatherElements.SOIL_TEMPERATURE_10CM_MEAN),
-                        WeatherElements.getNormalDataParameter(WeatherElements.POTENTIAL_EVAPORATION)
+                        WeatherElements.TEMPERATURE_MEAN,
+                        WeatherElements.PRECIPITATION,
+                        WeatherElements.GLOBAL_RADIATION,
+                        WeatherElements.SOIL_TEMPERATURE_10CM_MEAN,
+                        WeatherElements.POTENTIAL_EVAPORATION
                     },
                     aprilFirst, 
                     //dateOfLastWeatherData
@@ -411,7 +421,7 @@ public class RoughageService {
 
                 // The observations are tainted with the _NORMAL prefix, must clean up 
                 // before sending to model
-                observations.stream().forEach(obs->obs.setElementMeasurementTypeId(obs.getElementMeasurementTypeId().substring(0, obs.getElementMeasurementTypeId().indexOf("_NORMAL"))));
+                //observations.stream().forEach(obs->obs.setElementMeasurementTypeId(obs.getElementMeasurementTypeId().substring(0, obs.getElementMeasurementTypeId().indexOf("_NORMAL"))));
                 
                 // Add waterings to normal data precipitation?
                 if(
diff --git a/src/main/java/no/nibio/vips/logic/scheduling/model/grid/preprocessor/ZymoseptoriaSimpleRiskGridModelPreprocessor.java b/src/main/java/no/nibio/vips/logic/scheduling/model/grid/preprocessor/ZymoseptoriaSimpleRiskGridModelPreprocessor.java
index 4dd372e9..16e302e1 100644
--- a/src/main/java/no/nibio/vips/logic/scheduling/model/grid/preprocessor/ZymoseptoriaSimpleRiskGridModelPreprocessor.java
+++ b/src/main/java/no/nibio/vips/logic/scheduling/model/grid/preprocessor/ZymoseptoriaSimpleRiskGridModelPreprocessor.java
@@ -70,7 +70,9 @@ public class ZymoseptoriaSimpleRiskGridModelPreprocessor extends ModelRunPreproc
             }
             catch(WeatherObservationListException ex)
             {
-                throw new PreprocessorException("Problem with station #" + station.getPointOfInterestId() + ": " + ex.getMessage());
+                //throw new PreprocessorException();
+                System.out.println("Problem with station " + station.getPointOfInterest().getName() + " (#" + station.getPointOfInterestId() + "): " + ex.getMessage());
+                continue;
             }
             // Checking for BT
             try
diff --git a/src/main/java/no/nibio/vips/util/weather/WeatherDataSourceUtil.java b/src/main/java/no/nibio/vips/util/weather/WeatherDataSourceUtil.java
index 80590d55..406dc962 100755
--- a/src/main/java/no/nibio/vips/util/weather/WeatherDataSourceUtil.java
+++ b/src/main/java/no/nibio/vips/util/weather/WeatherDataSourceUtil.java
@@ -47,6 +47,8 @@ import org.apache.commons.io.IOUtils;
  * @author Tor-Einar Skog <tor-einar.skog@nibio.no>
  */
 public class WeatherDataSourceUtil {
+    
+    private final boolean DEBUG = false;
 
     /**
      * Fetches measured data from the stations weather data source, and optionally
@@ -186,7 +188,7 @@ public class WeatherDataSourceUtil {
             int readTimeout = 120;
         try {
             URL weatherURL = new URL(URL.toString());
-            //System.out.println("URL=" + weatherURL);
+            if(this.DEBUG) { System.out.println("URL=" + weatherURL); }
             URLConn = weatherURL.openConnection();
             
             URLConn.setConnectTimeout(1000 * connectTimeout); // If weather data server does not reply in 30 seconds, abort
-- 
GitLab