Index: trunk/ippToPsps/src/DetectionBatch.c
===================================================================
--- trunk/ippToPsps/src/DetectionBatch.c	(revision 30861)
+++ trunk/ippToPsps/src/DetectionBatch.c	(revision 30967)
@@ -15,4 +15,23 @@
     free (this->expName);
     free (this);
+}
+
+/**
+  Writes results to XML file
+  */
+static bool writeResults(DetectionBatch* this, long minObjID, long maxObjID, long totalDetectionsOut) {
+
+    if (!this->base.resultsXmlDoc) return false;
+
+    xmlNodePtr rootNode = xmlDocGetRootElement(this->base.resultsXmlDoc);
+    char tmp[100];
+    sprintf(tmp, "%ld", minObjID);
+    xmlNewChild(rootNode, NULL, BAD_CAST "minObjID", BAD_CAST tmp);
+    sprintf(tmp, "%ld", maxObjID);
+    xmlNewChild(rootNode, NULL, BAD_CAST "maxObjID", BAD_CAST tmp);
+    sprintf(tmp, "%ld", totalDetectionsOut);
+    xmlNewChild(rootNode, NULL, BAD_CAST "totalDetections", BAD_CAST tmp);
+
+    return true;
 }
 
@@ -119,5 +138,8 @@
         filterIDs[s] = filterID;
         surveyIDs[s] = this->base.surveyID;
-        strcpy(assocDate[s], this->base.todaysDate);
+
+        // if running in test mode, don't use today's date
+        if (this->base.testMode) strcpy(assocDate[s], "2010-01-01");
+        else strcpy(assocDate[s], this->base.todaysDate);
     }
 
@@ -133,8 +155,26 @@
     int ippIDetNum, instMagNum, instMagErrNum, peakMagNum;
     bool error = false;
-
-    // loop round all 60 chips
-    for (int x=0; x<8; x++) {
-        for (int y=0; y<8; y++) {
+    int startX, stopX, startY, stopY;
+
+    // in test mode, only run for chip 33
+    if (this->base.testMode) {
+
+        startX = 3;
+        stopX = 4;
+        startY = 3;
+        stopY = 4;
+    }
+    // in 'normal' mode, run for all chips
+    else {
+    
+        startX = 0;
+        stopX = 8;
+        startY = 0;
+        stopY = 8;
+    }
+
+    // loop round chips
+    for (int x=startX; x<stopX; x++) {
+        for (int y=startY; y<stopY; y++) {
 
             // dodge the corners
@@ -148,17 +188,9 @@
             // check we can move to detections table in smf
             sprintf(extensionName, "XY%s.psf", ccdNumber);
-            status=0;
-            if (fits_movnam_hdu(fitsIn, BINARY_TBL, extensionName, 0, &status)) {
-                psError(PS_ERR_IO, false, "Can't move to extension: %s skipping this chip\n", extensionName);
-                continue;
-            }
+            if (!ippToPspsConfig_moveToExtensionTable(fitsIn, extensionName)) continue;
 
             // move to header extension
             sprintf(extensionName, "XY%s.hdr", ccdNumber);
-            status=0;
-            if (fits_movnam_hdu(fitsIn, IMAGE_HDU, extensionName, 0, &status)) {
-                psError(PS_ERR_IO, false, "Can't move to extension: %s skipping this chip\n", extensionName);
-                continue;
-            }
+            if (!ippToPspsConfig_moveToExtensionHeader(fitsIn, extensionName)) continue;
 
             // stuff to save from psf.hdr
@@ -214,9 +246,5 @@
             // now move BACK to detections table in smf
             sprintf(extensionName, "XY%s.psf", ccdNumber);
-            status=0;
-            if (fits_movnam_hdu(fitsIn, BINARY_TBL, extensionName, 0, &status)) {
-                psError(PS_ERR_IO, false, "Can't move to extension: %s skipping\n", extensionName);
-                continue;
-            }
+            if (!ippToPspsConfig_moveToExtensionTable(fitsIn, extensionName)) continue;
 
             // keep a running count of 'images' we find in order to write total to FrameMeta at the end
@@ -312,5 +340,5 @@
 
                     // check for invalid flux values, and skip them
-                    if ( !peakFluxOk || !instFluxOk) {
+                    if (!peakFluxOk || !instFluxOk) {
                         removeList[numOfDuplicates+numInvalidFlux+numOfInvalidIppIDet] = s+1;
                         numInvalidFlux++;
@@ -408,25 +436,14 @@
 
     // write number of images we have found into FrameMeta table
-    status=0;
-    if (fits_movnam_hdu(this->base.fitsOut, BINARY_TBL, "FrameMeta", 0, &status))
-        psError(PS_ERR_IO, false, "Can't move back to FrameMeta extension to write number of OTAs\n");
-    else {
+    if (ippToPspsConfig_moveToExtensionTable(this->base.fitsOut, "FrameMeta")) { 
+
         fits_write_col(this->base.fitsOut, TSHORT, FRAMEMETA_NOTA, 1, 1, 1, &nOta, &status);
         fits_write_col(this->base.fitsOut, TSHORT, FRAMEMETA_NUMPHOTOREF, 1, 1, 1, &totalNumPhotoRef, &status);
     }
+
     status=0;
     if (fits_close_file(fitsIn, &status)) fits_report_error(stderr, status);
-    // write results
-    if (this->base.resultsXmlDoc) {
-
-        xmlNodePtr rootNode = xmlDocGetRootElement(this->base.resultsXmlDoc);
-        char tmp[100];
-        sprintf(tmp, "%ld", minObjID);
-        xmlNewChild(rootNode, NULL, BAD_CAST "minObjID", BAD_CAST tmp);
-        sprintf(tmp, "%ld", maxObjID);
-        xmlNewChild(rootNode, NULL, BAD_CAST "maxObjID", BAD_CAST tmp);
-        sprintf(tmp, "%ld", totalDetectionsOut);
-        xmlNewChild(rootNode, NULL, BAD_CAST "totalDetections", BAD_CAST tmp);
-    }
+
+    writeResults(this, minObjID, maxObjID, totalDetectionsOut);
 
     psLogMsg("ippToPsps", PS_LOG_INFO, "Data written for a total of %d chips/OTAs", nOta);
@@ -437,5 +454,4 @@
     return this->base.exitCode;
 }
-
 
 /**
