Index: trunk/psModules/src/detrend/Makefile.am
===================================================================
--- trunk/psModules/src/detrend/Makefile.am	(revision 16824)
+++ trunk/psModules/src/detrend/Makefile.am	(revision 16841)
@@ -13,5 +13,6 @@
 	pmDetrendDB.c \
 	pmShutterCorrection.c \
-	pmShifts.c
+	pmShifts.c \
+	pmDark.c
 
 #	pmSkySubtract.c
@@ -27,5 +28,6 @@
 	pmDetrendDB.h \
 	pmShutterCorrection.h \
-	pmShifts.h
+	pmShifts.h \
+	pmDark.h
 
 #	pmSkySubtract.h
Index: trunk/psModules/src/detrend/pmBias.c
===================================================================
--- trunk/psModules/src/detrend/pmBias.c	(revision 16824)
+++ trunk/psModules/src/detrend/pmBias.c	(revision 16841)
@@ -113,4 +113,5 @@
     }
     if (dark) {
+        psWarning("Dark processing is now available using pmDark --- perhaps you should use that instead?");
         PS_ASSERT_PTR_NON_NULL(view, false);
         PS_ASSERT_IMAGE_NON_NULL(dark->image, false);
@@ -121,5 +122,5 @@
 
     if (!pmOverscanSubtract (in, overscanOpts)) {
-	return false;
+        return false;
     }
 
@@ -148,16 +149,16 @@
         }
 
-	float darkNorm = 1.0;
+        float darkNorm = 1.0;
         float inNorm = pmFPADarkNorm(in->parent->parent->parent, view, inTime);
 
-	// if we have a normalized dark exposure, we simply multiply the master by inNorm.  if
-	// we do not have a normalized exposure, we have to scale the master as well.  XXX do
-	// we need to explicitly identify the master as normalized?
+        // if we have a normalized dark exposure, we simply multiply the master by inNorm.  if
+        // we do not have a normalized exposure, we have to scale the master as well.  XXX do
+        // we need to explicitly identify the master as normalized?
 
-	if (darkTime != 1.0) {
-	    darkNorm = pmFPADarkNorm(dark->parent->parent->parent, view, darkTime);
-	}
+        if (darkTime != 1.0) {
+            darkNorm = pmFPADarkNorm(dark->parent->parent->parent, view, darkTime);
+        }
 
-	if (isnan(inNorm) || isnan(darkNorm)) {
+        if (isnan(inNorm) || isnan(darkNorm)) {
             psError(PS_ERR_UNKNOWN, false, "Unable to determine dark normalisations.");
             return false;
Index: trunk/psModules/src/detrend/pmDark.c
===================================================================
--- trunk/psModules/src/detrend/pmDark.c	(revision 16824)
+++ trunk/psModules/src/detrend/pmDark.c	(revision 16841)
@@ -25,4 +25,6 @@
 static bool ordinateLookup(float *value, // Value of ordinate, to return
                            const char *name, // Name of ordinate (concept name)
+                           bool scale,  // Scale the value?
+                           float min, float max, // Minimum and maximum values for scaling
                            const pmReadout *ro // Readout of interest
                            )
@@ -60,4 +62,11 @@
         psWarning("Non-finite value (%f) of concept %s in readout", *value, name);
         return false;
+    }
+    if (scale) {
+        if (*value < min || *value > max) {
+            psWarning("Value of concept %s (%f) outside range (%f:%f)", name, *value, min, max);
+            return false;
+        }
+        *value = 2.0 * (*value - min) / (max - min) - 1.0;
     }
 
@@ -130,12 +139,9 @@
 
             float value = NAN;          // Value of ordinate
-            if (!ordinateLookup(&value, ord->name, ro)) {
+            if (!ordinateLookup(&value, ord->name, ord->scale, ord->min, ord->max, ro)) {
                 roMask->data.U8[j] = 0xff;
                 val->data.F32[i] = NAN;
                 numBadInputs++;
                 continue;
-            }
-            if (ord->scale) {
-                value = 2.0 * (value - ord->min) / (ord->max - ord->min) - 1.0;
             }
             val->data.F32[i] = value;
@@ -280,5 +286,4 @@
         return false;
     }
-    PS_ASSERT_ARRAYS_SIZE_EQUAL(dark->readouts, ordinates, false);
 
     int numOrdinates = ordinates->n;    // Number of ordinates
@@ -287,5 +292,5 @@
         pmDarkOrdinate *ord = ordinates->data[i]; // Ordinate of interest
         float value = NAN;              // Value for ordinate
-        if (!ordinateLookup(&value, ord->name, readout)) {
+        if (!ordinateLookup(&value, ord->name, ord->scale, ord->min, ord->max, readout)) {
             psError(PS_ERR_UNKNOWN, true, "Unable to find value for %s", ord->name);
             psFree(values);
@@ -317,5 +322,5 @@
             }
             float value = psPolynomialMDEval(poly, values); // Value of dark current
-            readout->image->data.F32[y][x] = value;
+            readout->image->data.F32[y][x] -= value;
             if (readout->mask && !isfinite(value)) {
                 readout->mask->data.PS_TYPE_MASK_DATA[y][x] = bad;
Index: trunk/psModules/src/detrend/pmDark.h
===================================================================
--- trunk/psModules/src/detrend/pmDark.h	(revision 16841)
+++ trunk/psModules/src/detrend/pmDark.h	(revision 16841)
@@ -0,0 +1,93 @@
+#ifndef PM_DARK_H
+#define PM_DARK_H
+
+#include <pslib.h>
+#include <pmHDU.h>
+#include <pmFPA.h>
+
+#define PM_DARK_ANALYSIS_ORDINATES "DARK.ORDINATES" // Name for dark ordinates in the cell analysis metadata
+
+// An ordinate for fitting darks
+typedef struct {
+    psString name;                      // Name of concept to fit
+    int order;                          // Polynomial order to fit
+    bool scale;                         // Rescale values?
+    float min, max;                     // Minimum and maximum values for rescaling
+} pmDarkOrdinate;
+
+// Allocator
+pmDarkOrdinate *pmDarkOrdinateAlloc(const char *name, // Name for ordinate
+                                    int order // Order for ordinate
+    );
+
+
+// Combine darks
+bool pmDarkCombine(pmCell *output,      // Output cell; readouts will be attached
+                   const psArray *inputs, // Input readouts for combination
+                   psArray *ordinates,  // Ordinates for fitting
+                   int iter,            // Number of rejection iterations
+                   float rej,           // Rejection threshold (standard deviations)
+                   psMaskType maskVal   // Value to mask
+    );
+
+// Apply dark
+bool pmDarkApply(pmReadout *readout,    // Readout to which to apply dark
+                 const pmCell *dark,    // Dark to apply
+                 psMaskType bad         // Mask value to give bad pixels
+    );
+
+// I/O functions for darks
+
+// Write all darks within an FPA
+bool pmFPAWriteDark(pmFPA *fpa,         // FPA to write
+                    psFits *fits,       // FITS file to which to write
+                    psDB *db,           // Database, for concepts
+                    bool blank,         // Write a blank only?
+                    bool recurse        // Recurse to lower levels?
+    );
+
+// Write all darks within a chip
+bool pmChipWriteDark(pmChip *chip,      // Chip to write
+                     psFits *fits,      // FITS file to which to write
+                     psDB *db,          // Database, for concepts
+                     bool blank,        // Write a blank only?
+                     bool recurse       // Recurse to lower levels?
+    );
+
+// Write a dark to a FITS file
+bool pmCellWriteDark(pmCell *cell,      // Cell containing dark information
+                     psFits *fits,      // FITS file to which to write
+                     psDB *db,          // Database, for concepts
+                     bool blank         // Write a blank only?
+    );
+
+// Read dark for all FPA from a FITS file
+bool pmFPAReadDark(pmFPA *fpa,          // FPA for which to read
+                   psFits *fits,        // FITS file to read
+                   psDB *db             // Database, for concepts
+    );
+
+// Read dark for all chip from a FITS file
+bool pmChipReadDark(pmChip *chip,       // Chip for which to read
+                    psFits *fits,       // FITS file to read
+                    psDB *db            // Database, for concepts
+    );
+
+// Read dark for a cell from a FITS file
+bool pmCellReadDark(pmCell *cell,       // Cell for which to read
+                    psFits *fits,       // FITS file to read
+                    psDB *db            // Database, for concepts
+    );
+
+// Write dark table to FITS file
+bool pmDarkWrite(psFits *fits,          // FITS file to which to write
+                 const psMetadata *header, // Header to write
+                 const psArray *ordinates // Dark ordinates to write
+    );
+
+// Read dark table from FITS file
+psArray *pmDarkRead(psFits *fits        // FITS file to read
+    );
+
+
+#endif
