Index: trunk/FACT++/src/smartfact.cc
===================================================================
--- trunk/FACT++/src/smartfact.cc	(revision 17060)
+++ trunk/FACT++/src/smartfact.cc	(revision 17123)
@@ -18,4 +18,5 @@
 #include "DimWriteStatistics.h"
 #include "externals/PixelMap.h"
+#include "externals/Interpolator2D.h"
 
 #include "tools.h"
@@ -1731,69 +1732,71 @@
         // =======================================================
 
-        struct weight
-        {
-            int idx;
-            int i[3];
-            double w[3];
-        };
-
-        static vector<weight> fWeights;
-        if (fWeights.empty())
-        {
-            ifstream fin("temp-interpolator.txt");
-            int cnt = 0;
+        static vector<pair<double,double>> fPositionsSensors;
+        static vector<pair<double,double>> fPositionsBias;
+
+        if (fPositionsSensors.empty())
+        {
+            ifstream fin1("sensor-pos.txt");
             while (1)
             {
-                weight w;
-                fin >> w.idx;
-                fin >> w.i[0];
-                fin >> w.i[1];
-                fin >> w.i[2];
-                fin >> w.w[0];
-                fin >> w.w[1];
-                fin >> w.w[2];
-                if (!fin)
+                double x, y;
+                fin1 >> x;
+                fin1 >> y;
+                if (!fin1)
                     break;
 
-                if (w.idx != cnt++)
-                {
-                    Fatal("Reading interpolator failed ("+to_string(w.idx)+"|"+to_string(cnt)+")");
-                    fWeights.clear();
+                fPositionsSensors.emplace_back(x, y);
+            }
+
+            if (fPositionsSensors.size() != 31)
+                Fatal("Reading sensor positions failed ("+to_string(fPositionsSensors.size())+")");
+        }
+
+        if (fPositionsBias.empty())
+        {
+            ifstream fin1("bias-positions.txt");
+            while (1)
+            {
+                double x, y;
+                fin1 >> x;
+                fin1 >> y;
+                if (!fin1)
                     break;
-                }
-
-                fWeights.emplace_back(w);
+
+                fPositionsBias.emplace_back(x, y);
             }
 
-            if (fWeights.size() && fWeights.size() != 160)
+            if (fPositionsBias.size() != 320)
+                Fatal("Reading bias positions failed ("+to_string(fPositionsBias.size())+")");
+        }
+
+        if (!fPositionsBias.size()==320 || !fPositionsSensors.size()==31)
+            return GetCurrentState();
+
+        vector<double> temp;
+        vector<Interpolator2D::vec> xy;
+        for (int i=0; i<31; i++)
+            if (ptr[i]!=0)
             {
-                Fatal("Reading interpolator failed ("+to_string(fWeights.size())+")");
-                fWeights.clear();
+                temp.emplace_back(ptr[i]);
+                xy.emplace_back(fPositionsSensors[i].first, fPositionsSensors[i].second);
             }
-        }
-
-        if (fWeights.size()==160)
-        {
-            vector<float> temp(160);
-            vector<float> cpy(ptr, ptr+31);
-
-            cpy[8]  = (cpy[4] +cpy[6] +cpy[7] +cpy[10]+cpy[11])/5;
-            cpy[19] = (cpy[7] +cpy[11]+cpy[16]+cpy[17]+cpy[21])/5;
-            cpy[15] = (cpy[14]+cpy[16]+cpy[18])/3;
-
-            double pavg = 0;
-            for (int i=0; i<160; i++)
-            {
-                const double T =
-                    fWeights[i].w[0]*cpy[fWeights[i].i[0]]+
-                    fWeights[i].w[1]*cpy[fWeights[i].i[1]]+
-                    fWeights[i].w[2]*cpy[fWeights[i].i[2]];
-
-                temp[i] = T;
-                pavg += T;
-            }
-
-            WriteCam(d, "cam-fsccontrol-temperature", temp, 3, pavg/160-1.5);
-        }
+
+        Interpolator2D inter(xy);
+        if (!inter.SetOutputGrid(fPositionsBias))
+            return GetCurrentState();
+
+        const vector<double> T = inter.Interpolate(temp);
+
+        vector<double> tout(320);
+        double pavg = 0;
+        for (int i=0; i<320; i++)
+        {
+            const int idx = (fPixelMap.hv(i).hw()/9)*2+fPixelMap.hv(i).group();
+            tout[idx] = T[i];
+            pavg += T[i];
+        }
+
+        WriteCam(d, "cam-fsccontrol-temperature", tout, 3, pavg/320-1.5);
 
         return GetCurrentState();
