Index: trunk/ippToPsps/src/ippToPspsBatchStack.c
===================================================================
--- trunk/ippToPsps/src/ippToPspsBatchStack.c	(revision 27686)
+++ trunk/ippToPsps/src/ippToPspsBatchStack.c	(revision 29298)
@@ -9,4 +9,6 @@
 #include "ippToPsps.h"
 #include "ippToPspsConfig.h"
+#include "ippToPspsStackEnums.h"
+
 
 /**
@@ -15,13 +17,124 @@
 int ippToPsps_batchStack(IppToPsps *this) {
 
-//    char extensionName[20];
-    uint32_t skycell = 0; // TODO
 
-    for (uint16_t i = 0; i<this->numOfInputFiles; i++) {
+    int status = 0;
+    fitsfile *fitsIn;
 
-        skycell = i; // TODO
+    if (fits_open_file(&fitsIn, this->inputFiles[0], READONLY, &status)) {
+
+        fits_report_error(stderr, status);
+        return PS_EXIT_SYS_ERROR;
+    }
+
+    long removeList[MAXDETECT];
+    float instMag[MAXDETECT];
+    float floatnull = -999.0;
+    int anynull = 0;
+
+    // get primary header and pull stuff out for later
+    int nKeys = 0;
+    fits_get_hdrspace(fitsIn, &nKeys, NULL, &status);
+    float exposureTime; status=0; fits_read_key(fitsIn, TFLOAT, "EXPTIME", &exposureTime, NULL, &status);
+    char filterType[20]; status=0; fits_read_key(fitsIn, TSTRING, "FPA.FILTERID", filterType, NULL, &status);
 
 
+    // write StackMeta
+    ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, 1, "StackMeta", true);
+
+    int8_t filterID = -1;
+    if (!ippToPspsConfig_getFilterId(this->config, filterType, &filterID)) {return PS_EXIT_DATA_ERROR;}
+    fits_write_col(this->fitsOut, TBYTE, STACKMETA_FILTERID, 1, 1, 1, &filterID, &status);
+
+    // psf detections
+    char extensionName[15];
+    sprintf(extensionName, "SkyChip.psf");
+    if (fits_movnam_hdu(fitsIn, BINARY_TBL, extensionName, 0, &status)) {
+        psError(PS_ERR_IO, false, "Can't move to extension: %s\n", extensionName);
+        
     }
+    else {
+    
+        long nDet = 0;
+        if (fits_get_num_rows(fitsIn, &nDet, &status)) {
+            fits_report_error(stderr, status);
+        }
+
+        int instMagNum;
+        status=0;fits_get_colnum(fitsIn, CASESEN, "PSF_INST_MAG", &instMagNum, &status);
+        if (status) psError(PS_ERR_IO, false, "Unable to read col num for PSF_INST_MAG");
+        fits_read_col(fitsIn, TFLOAT, instMagNum, 1, 1, nDet, &floatnull, instMag, &anynull, &status);
+
+
+        printf("Looping through %ld psf detections\n", nDet);
+        float mag;
+        long unmatched = 0, totalDetections = 0, numOfDuplicates = 0, numInvalidFlux = 0, numDetectionsOut = 0;
+
+        for (long s = 0; s<nDet; s++) {
+
+            // TODO implement this match in DVO
+            if (1) {
+
+                mag = instMag[s];
+                if (!isfinite(mag) || mag < -998.0) {
+
+                    removeList[numOfDuplicates+numInvalidFlux] = s+1;
+                    numInvalidFlux++;
+                }
+
+                totalDetections++;
+            }
+            else {
+
+                unmatched++;
+                continue;
+            }
+        }
+
+        numDetectionsOut = totalDetections - numInvalidFlux;
+
+        if (numDetectionsOut > 0) {
+
+            ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, nDet, "StackDetection", false);
+            if (numInvalidFlux) fits_delete_rowlist(this->fitsOut, removeList, numInvalidFlux, &status);
+
+        }
+        psLogMsg("ippToPsps", PS_LOG_INFO,
+                "+---------------+---------+----------+------------------+---------------+--------------+\n"
+                "|   Extension   | Rows in | Rows out | Missing from DVO | Duplicate IDs | Invalid Flux |\n"
+                "|  %12s |  %5ld  |   %5ld  |      %5ld       |    %5ld      |    %5ld     |\n",
+                extensionName, nDet, numDetectionsOut, unmatched, numOfDuplicates, numInvalidFlux);
+
+    }
+
+
+
+    // extended source
+    sprintf(extensionName, "SkyChip.xsrc");
+    if (fits_movnam_hdu(fitsIn, BINARY_TBL, extensionName, 0, &status)) {
+        psError(PS_ERR_IO, false, "Can't move to extension: %s\n", extensionName);
+
+    }
+    else {
+
+        long nDet = 0;
+        if (fits_get_num_rows(fitsIn, &nDet, &status)) {
+            fits_report_error(stderr, status);
+        }
+
+        printf("Looping through %ld extended source detections\n", nDet);
+        for (long s = 0; s<nDet; s++) {
+
+
+
+        }
+
+
+
+        ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, nDet, "StackApFlx", false);
+    }
+
+    status=0;
+    if (fits_close_file(fitsIn, &status)) fits_report_error(stderr, status);
+
 
     return PS_EXIT_SUCCESS;
