Index: trunk/ippToPsps/src/ippToPspsBatchDetection.c
===================================================================
--- trunk/ippToPsps/src/ippToPspsBatchDetection.c	(revision 27684)
+++ trunk/ippToPsps/src/ippToPspsBatchDetection.c	(revision 27685)
@@ -35,7 +35,7 @@
   Loop loads the data from the input smf file(s), consults the DVO database, then prepares an output FITS format suitable for PSPS.
   */
-bool ippToPsps_batchDetection(IppToPsps *this) {
-
-    if (this->numOfInputFiles < 1) return false;
+int ippToPsps_batchDetection(IppToPsps *this) {
+
+    if (this->numOfInputFiles < 1) return PS_EXIT_DATA_ERROR;
 
     int status = 0;
@@ -46,5 +46,5 @@
 
         fits_report_error(stderr, status);
-        return false;
+        return PS_EXIT_SYS_ERROR;
     }
 
@@ -98,6 +98,4 @@
     int numDvoDetections = -1;
     Image *pImage = NULL;
-
-    bool ret = true;
 
     // stuff for detections table
@@ -171,8 +169,17 @@
             sprintf(ccdNumber, "%d%d", x, y);
 
+            // 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;
+            }
+
+            // 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\n", extensionName);
+                psError(PS_ERR_IO, false, "Can't move to extension: %s skipping this chip\n", extensionName);
                 continue;
             }
@@ -190,5 +197,5 @@
             skylist = dvoSkyListByExternID(this->dvoConfig, sourceId, imageId, &pImage);
             if (skylist == NULL) {
-                psError(PS_ERR_IO, false, "Can't find SkyList for sourceId='%d' imageId='%d' (CCD = XY%s): skipping\n", sourceId, imageId, ccdNumber);
+                psError(PS_ERR_IO, false, "DVO: can't find SkyList for sourceId='%d' imageId='%d' (CCD = XY%s): skipping\n", sourceId, imageId, ccdNumber);
                 continue;
             }
@@ -201,16 +208,5 @@
             numDvoDetections = dvoGetDetections(skylist, pImage->imageID, &dvoDetections, &maxDvoDetId);
 
-
-            // keep a running count of 'images' we find in order to write total to FrameMeta at the end
-            nOta++;
-
             // TODO check nDet < MAXDETECT
-            // 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\n", extensionName);
-                continue;
-            }
 
             // create ImageMeta
@@ -222,5 +218,4 @@
             fits_write_col(this->fitsOut, TSHORT, IMAGEMETA_CCDID, 1, 1, 1, &pImage->ccdnum, &status);
             // fits_write_col(this->fitsOut, TLONG, IMAGEMETA_PHOTOCALID, 1, 1, 1, &pImage->photcode, &status); TODO
-            psLogMsg("ippToPsps", PS_LOG_INFO, "photocode %d", pImage->photcode);
             fits_write_col(this->fitsOut, TBYTE, IMAGEMETA_FILTERID, 1, 1, 1, &filterID, &status);
             fits_write_col(this->fitsOut, TFLOAT, IMAGEMETA_PHOTOSCAT, 1, 1, 1, &zptObs, &status);
@@ -228,4 +223,15 @@
             fits_write_col(this->fitsOut, TFLOAT, IMAGEMETA_MOMENTFWHM, 1, 1, 1, &momentFwhm, &status);
             fits_write_col(this->fitsOut, TFLOAT, IMAGEMETA_PHOTOZERO, 1, 1, 1, &zptObs, &status);
+
+            // 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;
+            }
+
+            // keep a running count of 'images' we find in order to write total to FrameMeta at the end
+            nOta++;
 
             long nDet = 0;
@@ -346,9 +352,11 @@
 
     // write number of images we have found into FrameMeta table
+    status=0; 
     if (fits_movnam_hdu(this->fitsOut, BINARY_TBL, "FrameMeta", 0, &status))
-        fits_report_error(stderr, status);
+            psError(PS_ERR_IO, false, "Can't move back to FrameMeta extension to write number of OTAs\n");
     else 
         fits_write_col(this->fitsOut, TSHORT, FRAMEMETA_NOTA, 1, 1, 1, &nOta, &status);
 
+    status=0;
     if (fits_close_file(fitsIn, &status)) fits_report_error(stderr, status);
 
@@ -362,5 +370,7 @@
     }
 
-    return ret;
+    psLogMsg("ippToPsps", PS_LOG_INFO, "Data written for a total of %d chips/OTAs", nOta);
+
+    return PS_EXIT_SUCCESS;
 }
 
