From ae936979798b2b7db06648f69b3364ba36e29fd1 Mon Sep 17 00:00:00 2001
From: Peter Alexander <>
Date: Tue, 15 Sep 2020 09:14:34 +0100
Subject: [PATCH] Allow NaN yields to be clustered.  Needed for GGCMI

---
 src/ac/ed/lurg/utils/cluster/Cluster.java   |  4 +++-
 src/ac/ed/lurg/yield/YieldClusterPoint.java | 10 +++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/ac/ed/lurg/utils/cluster/Cluster.java b/src/ac/ed/lurg/utils/cluster/Cluster.java
index d96f3640..1ad395fb 100644
--- a/src/ac/ed/lurg/utils/cluster/Cluster.java
+++ b/src/ac/ed/lurg/utils/cluster/Cluster.java
@@ -55,7 +55,9 @@ public class Cluster<K, P extends ClusteringPoint<K>> {
     protected double distanceFromCentroid(ClusteringPoint<K> p) {
     	double squaredTotal=0;
     	for (K key : centroid.getAllClusteringKeys()) {
-       		squaredTotal += Math.pow(centroid.getClusteringValue(key)-p.getClusteringValue(key), 2);
+    		double a = centroid.getClusteringValue(key);
+    		double b = p.getClusteringValue(key);
+    		squaredTotal += Math.pow(a-b, 2);
     	}
     	
         return Math.sqrt(squaredTotal);
diff --git a/src/ac/ed/lurg/yield/YieldClusterPoint.java b/src/ac/ed/lurg/yield/YieldClusterPoint.java
index 91f9eb80..7b3882eb 100644
--- a/src/ac/ed/lurg/yield/YieldClusterPoint.java
+++ b/src/ac/ed/lurg/yield/YieldClusterPoint.java
@@ -51,7 +51,11 @@ public class YieldClusterPoint implements ClusteringPoint<String> {
 
 	@Override
 	public double getClusteringValue(String key) {
-
+		double v = getClusteringValueNaN(key);		
+		return Double.isNaN(v) ? 0.0 : v;  // LPJ with emulator provides NaN yields.  How to cluster these is tricky, but assuming they are 0 
+	}
+	
+	private double getClusteringValueNaN(String key) {
 		switch (key) {
 			case PASTURE:  return pasture;
 			case WHEAT_MIN:  return wheatMin;
@@ -61,9 +65,9 @@ public class YieldClusterPoint implements ClusteringPoint<String> {
 			case MAIZE_MAX:  return maizeMax;
 			case IRRIG_WATER_AVAL:  return irrigWaterAval;
 			case PROTECTED_AREA:  return protectedArea;
+			default:
+				throw new RuntimeException("YieldClusterPoint.getClusteringValue: got unknown value " + key);
 		}
-		LogWriter.printlnError("YieldClusterPoint.getClusteringValue: got unknown value " + key);
-		return Double.NaN;
 	}
 
 	@Override
-- 
GitLab