Index: /trunk/ppImage/src/Makefile.am
===================================================================
--- /trunk/ppImage/src/Makefile.am	(revision 6848)
+++ /trunk/ppImage/src/Makefile.am	(revision 6849)
@@ -3,5 +3,4 @@
 noinst_HEADERS = \
 	ppImage.h \
-	ppImageDetrend.h \
 	ppImageOptions.h \
 	ppMem.h
@@ -15,8 +14,9 @@
 	ppImageParseDetrend.c \
 	ppImageLoop.c \
-	ppImageDetrendCell.c \
+	ppImageDetrendReadout.c \
 	ppImageDetrendMask.c \
 	ppImageDetrendBias.c \
 	ppImageDetrendNonLinear.c \
+	ppImageRebinReadout.c \
 	ppMem.c
 #	ppImageParseCamera.c \
@@ -35,6 +35,4 @@
 	ppMem.c
 
-
-
 clean-local:
 	-rm -f TAGS
Index: /trunk/ppImage/src/ppImage.c
===================================================================
--- /trunk/ppImage/src/ppImage.c	(revision 6848)
+++ /trunk/ppImage/src/ppImage.c	(revision 6849)
@@ -9,5 +9,5 @@
     // Determine camera, format from header if not already defined
     // Construct camera in preparation for reading
-    pmConfig *config = ppImageConfig(argc, argv);
+    pmConfig *config = ppImageConfig(&argc, argv);
 
     // Set various tasks (define optional operations)
Index: /trunk/ppImage/src/ppImage.h
===================================================================
--- /trunk/ppImage/src/ppImage.h	(revision 6848)
+++ /trunk/ppImage/src/ppImage.h	(revision 6849)
@@ -12,5 +12,5 @@
 
 // Get the configuration
-pmConfig *ppImageConfig(int argc, char **argv);
+pmConfig *ppImageConfig(int *argc, char **argv);
 
 // Determine what type of camera, and initialise
@@ -30,3 +30,5 @@
 bool ppImageDetrendNonLinearPolynomial(pmReadout *input, psMetadataItem *dataItem);
 
+bool ppImageRebinReadout (pmConfig *config, pmFPAview *view, char *filename);
+
 #endif // Pau.
Index: /trunk/ppImage/src/ppImageConfig.c
===================================================================
--- /trunk/ppImage/src/ppImageConfig.c	(revision 6848)
+++ /trunk/ppImage/src/ppImageConfig.c	(revision 6849)
@@ -6,13 +6,13 @@
 }
 
-pmConfig *ppImageConfig(int argc, char **argv)
+pmConfig *ppImageConfig(int *argc, char **argv)
 {
 
     bool status;
 
-    if (argc == 1) usage ();
+    if (*argc == 1) usage ();
 
     // load the site-wide configuration information
-    pmConfig *config = pmConfigRead(&argc, argv);
+    pmConfig *config = pmConfigRead(argc, argv);
     if (! config) {
         psErrorStackPrint(stderr, "Can't find site configuration!\n");
@@ -25,15 +25,15 @@
     // if these command-line options are supplied, load the file name lists into config->arguments
     // override any configuration-specified source for these files
-    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "BIAS", "-bias", "-biaslist");
-    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "DARK", "-dark", "-darklist");
-    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "FLAT", "-flat", "-flatlist");
-    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "MASK", "-mask", "-masklist");
-    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "FRINGE", "-fringe", "-fringelist");
+    status = pmConfigFileSetsMD (config->arguments, argc, argv, "BIAS", "-bias", "-biaslist");
+    status = pmConfigFileSetsMD (config->arguments, argc, argv, "DARK", "-dark", "-darklist");
+    status = pmConfigFileSetsMD (config->arguments, argc, argv, "FLAT", "-flat", "-flatlist");
+    status = pmConfigFileSetsMD (config->arguments, argc, argv, "MASK", "-mask", "-masklist");
+    status = pmConfigFileSetsMD (config->arguments, argc, argv, "FRINGE", "-fringe", "-fringelist");
 
     // the input file is a required argument; if not found, we will exit
-    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "INPUT", "-file", "-list");
+    status = pmConfigFileSetsMD (config->arguments, argc, argv, "INPUT", "-file", "-list");
     if (!status) { usage ();}
 
-    if (! psArgumentParse(config->arguments, &argc, argv) || argc != 2) {
+    if (! psArgumentParse(config->arguments, argc, argv) || *argc != 2) {
 	usage ();
     }
Index: unk/ppImage/src/ppImageDetrendCell.c
===================================================================
--- /trunk/ppImage/src/ppImageDetrendCell.c	(revision 6848)
+++ 	(revision )
@@ -1,62 +1,0 @@
-#include "ppImage.h"
-
-bool ppImageDetrendReadout (ppImageOptions *options, pmConfig *config, pmFPAview *view) {
-
-    bool status;
-    pmFPAfile *file;
-
-    // construct a view for the detrend images (which have only one readout)
-    pmFPAview *detview = pmFPAviewAlloc (0);
-    *detview = *view;
-    detview->readout = 0;
-
-    // find the currently selected readout
-    file = psMetadataLookupPtr (&status, config->files, "PPIMAGE.INPUT");
-    pmReadout *input = pmFPAviewThisReadout (view, file->fpa);
-
-    pmReadoutSetWeights (input);
-
-    // Mask bad pixels
-    if (options->doMask) {
-	file = psMetadataLookupPtr (&status, config->files, "PPIMAGE.MASK");
-	pmReadout *mask = pmFPAviewThisReadout (detview, file->fpa);
-	ppImageDetrendMask(input, mask);
-    }
-
-# if 0
-    // Non-linearity correction
-    if (options->doNonLin) {
-	ppImageDetrendNonLinear(detrend->input, input, options);
-    }
-# endif
-
-    // set up the readouts for dark and bias
-    pmReadout  *dark = NULL;
-    pmReadout  *bias = NULL;
-    if (options->doBias) {
-	file   = psMetadataLookupPtr (&status, config->files, "PPIMAGE.BIAS");
-	bias = pmFPAviewThisReadout (detview, file->fpa);
-    }
-    if (options->doDark) {
-	file   = psMetadataLookupPtr (&status, config->files, "PPIMAGE.DARK");
-	dark = pmFPAviewThisReadout (detview, file->fpa);
-    }
-
-    // Bias, dark and overscan subtraction are all merged.
-    ppImageDetrendBias(input, bias, dark, options);
-
-    // Flat-field correction (no options used?)
-    if (options->doFlat) {
-	file = psMetadataLookupPtr (&status, config->files, "PPIMAGE.FLAT");
-	pmReadout *flat = pmFPAviewThisReadout (detview, file->fpa);
-	pmFlatField(input, flat);
-    }
-
-    psFree (detview);
-    return true;
-}
-
-    // XXX in psphot, the recipe options are parsed at this depth
-    // XXX consider if we move ppImageOptions in here
-    // psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, "PSPHOT");
-
Index: /trunk/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- /trunk/ppImage/src/ppImageDetrendReadout.c	(revision 6849)
+++ /trunk/ppImage/src/ppImageDetrendReadout.c	(revision 6849)
@@ -0,0 +1,53 @@
+#include "ppImage.h"
+
+bool ppImageDetrendReadout (ppImageOptions *options, pmConfig *config, pmFPAview *view)
+{
+    // construct a view for the detrend images (which have only one readout)
+    pmFPAview *detview = pmFPAviewAlloc (0);
+    *detview = *view;
+    detview->readout = 0;
+
+    // find the currently selected readout
+    pmReadout *input = pmFPAfileThisReadout (config->files, view, "PPIMAGE.INPUT");
+    pmReadoutSetWeights (input);
+
+    // Mask bad pixels
+    if (options->doMask) {
+	pmReadout *mask = pmFPAfileThisReadout (config->files, detview, "PPIMAGE.MASK");
+	ppImageDetrendMask(input, mask);
+    }
+
+# if 0
+    // Non-linearity correction
+    if (options->doNonLin) {
+	ppImageDetrendNonLinear(detrend->input, input, options);
+    }
+# endif
+
+    // set up the readouts for dark and bias
+    pmReadout  *dark = NULL;
+    pmReadout  *bias = NULL;
+    if (options->doBias) {
+	bias = pmFPAfileThisReadout (config->files, detview, "PPIMAGE.BIAS");
+    }
+    if (options->doDark) {
+	dark = pmFPAfileThisReadout (config->files, detview, "PPIMAGE.DARK");
+    }
+
+    // Bias, dark and overscan subtraction are all merged.
+    ppImageDetrendBias(input, bias, dark, options);
+
+    // Flat-field correction (no options used?)
+    if (options->doFlat) {
+	pmReadout *flat = pmFPAfileThisReadout (config->files, detview, "PPIMAGE.FLAT");
+	pmFlatField(input, flat);
+    }
+
+    psFree (detview);
+    return true;
+}
+
+    // XXX in psphot, the recipe options are parsed at this depth
+    // XXX consider if we move ppImageOptions in here
+    // psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, "PSPHOT");
+
Index: /trunk/ppImage/src/ppImageLoop.c
===================================================================
--- /trunk/ppImage/src/ppImageLoop.c	(revision 6848)
+++ /trunk/ppImage/src/ppImageLoop.c	(revision 6849)
@@ -37,4 +37,12 @@
 		ppImageDetrendReadout (options, config, view);
 
+		if (options->doBin1) {
+		    ppImageRebinReadout (config, view, "PPIMAGE.BIN1");
+		}
+
+		if (options->doBin2) {
+		    ppImageRebinReadout (config, view, "PPIMAGE.BIN2");
+		}
+
 		pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
 	    }
Index: /trunk/ppImage/src/ppImageOptions.c
===================================================================
--- /trunk/ppImage/src/ppImageOptions.c	(revision 6848)
+++ /trunk/ppImage/src/ppImageOptions.c	(revision 6849)
@@ -23,5 +23,5 @@
 ppImageOptions *ppImageOptionsParse(pmConfig *config)
 {
-
+    bool status;
     ppImageOptions *options = ppImageOptionsAlloc ();
 
@@ -137,23 +137,26 @@
 
     // Mask recipe options
-    if (psMetadataLookupBool(NULL, recipe, "MASK")) {
-	options->doMask = true;
-    }
+    options->doMask = psMetadataLookupBool(NULL, recipe, "MASK");
+    options->doBias = psMetadataLookupBool(NULL, recipe, "BIAS");
+    options->doDark = psMetadataLookupBool(NULL, recipe, "DARK");
+    options->doFlat = psMetadataLookupBool(NULL, recipe, "FLAT");
 
-    // Bias recipe options
-    if (psMetadataLookupBool(NULL, recipe, "BIAS")) {
-	options->doBias = true;
-    }
+    // binned image options
+    options->xBin1 = psMetadataLookupS32(&status, recipe, "BIN1.XBIN"); 
+    if (!status) options->xBin1 = 16;
+    options->yBin1 = psMetadataLookupS32(&status, recipe, "BIN1.YBIN"); 
+    if (!status) options->yBin1 = 16;
 
-    // Dark recipe options
-    if (psMetadataLookupBool(NULL, recipe, "DARK")) {
-	options->doDark = true;
-    }
+    options->xBin2 = psMetadataLookupS32(&status, recipe, "BIN2.XBIN"); 
+    if (!status) options->xBin1 = 16;
+    options->yBin2 = psMetadataLookupS32(&status, recipe, "BIN2.YBIN"); 
+    if (!status) options->yBin1 = 16;
 
-    // Flat recipe options
-    if (psMetadataLookupBool(NULL, recipe, "FLAT")) {
-	options->doFlat = true;
-    }
+    options->doBin1 = psMetadataLookupBool(NULL, recipe, "BIN1.FITS");
+    options->doBin2 = psMetadataLookupBool(NULL, recipe, "BIN2.FITS");
 
+    // options->doBin1JPEG = psMetadataLookupBool(NULL, recipe, "BIN1.JPEG");
+    // options->doBin2JPEG = psMetadataLookupBool(NULL, recipe, "BIN2.JPEG");
+    
     return options;
 }
Index: /trunk/ppImage/src/ppImageOptions.h
===================================================================
--- /trunk/ppImage/src/ppImageOptions.h	(revision 6848)
+++ /trunk/ppImage/src/ppImageOptions.h	(revision 6849)
@@ -11,10 +11,19 @@
     bool doSource;                      // Source identification and photometry
     bool doAstrom;                      // Astrometry
+
     bool doOverscan;                    // Overscan subtraction
     pmOverscanOptions *overscan;        // Overscan options
+
     bool doNonLin;                      // Non-linearity correction
     psDataType nonLinearType;
     psMetadataItem *nonLinearData;
     void *nonLinearSource;
+
+    bool doBin1;
+    int xBin1, yBin1;
+
+    bool doBin2;
+    int xBin2, yBin2;
+
 } ppImageOptions;
 
Index: /trunk/ppImage/src/ppImageParseDetrend.c
===================================================================
--- /trunk/ppImage/src/ppImageParseDetrend.c	(revision 6848)
+++ /trunk/ppImage/src/ppImageParseDetrend.c	(revision 6849)
@@ -48,8 +48,12 @@
 
     // XXX do these need to construct a new fpa?
-    // pmFPAfileDefine (config->files, config->camera, input->input, "PPIMAGE.BIN1");
-    // pmFPAfileDefine (config->files, config->camera, input->input, "PPIMAGE.BIN2");
-    // pmFPAfileDefine (config->files, config->camera, input->input, "PPIMAGE.JPG1");
-    // pmFPAfileDefine (config->files, config->camera, input->input, "PPIMAGE.JPG2");
+    if (options->doBin1) {
+	pmFPAfileFromFPA (config, input->fpa, options->xBin1, options->yBin1, "PPIMAGE.BIN1");
+    }
+    if (options->doBin2) {
+	pmFPAfileFromFPA (config, input->fpa, options->xBin2, options->yBin2, "PPIMAGE.BIN2");
+    }
+    // pmFPAfileDefine (config->files, config->camera, input->fpa, "PPIMAGE.JPG1");
+    // pmFPAfileDefine (config->files, config->camera, input->fpa, "PPIMAGE.JPG2");
 
     // ppImage is supplied with an output name, which may be used by multiple
Index: /trunk/ppImage/src/ppImageRebinReadout.c
===================================================================
--- /trunk/ppImage/src/ppImageRebinReadout.c	(revision 6849)
+++ /trunk/ppImage/src/ppImageRebinReadout.c	(revision 6849)
@@ -0,0 +1,46 @@
+#include "ppImage.h"
+
+bool ppImageRebinReadout (pmConfig *config, pmFPAview *view, char *filename)
+{
+    bool status;
+    psF32 value;
+    psS32 nPix;
+
+    // find the currently selected readout
+    pmReadout *input = pmFPAfileThisReadout (config->files, view, "PPIMAGE.INPUT");
+
+    // set up the readouts for dark and bias
+    pmReadout  *output = pmFPAfileThisReadout (config->files, view, filename);
+
+    // we need the file for the binning factors as well
+    pmFPAfile *file = psMetadataLookupPtr (&status, config->files, filename);
+    if (file == NULL) return NULL;
+
+    // the binning process must not change the size of the output image...
+    // psStats *stats = psStatsAlloc (PS_STATS_SAMPLE_MEAN);
+    // psImageRebin (output->image, input->image, NULL, 0, scale, stats);
+    // psFree (stats);
+
+    int dX = file->xBin;
+    int dY = file->yBin;
+
+    int nX = input->image->numCols;
+    int nY = input->image->numRows;
+
+    // do the rebinning by hand, mean only for test
+    for (int yOut = 0; yOut < output->image->numRows; yOut++) {
+	for (int xOut = 0; xOut < output->image->numCols; xOut++) {
+	    value = 0;
+	    nPix = 0;
+	    for (int yIn = yOut * dY; (yIn < yOut * dY + dY) && (yIn < nY); yIn ++) {
+		for (int xIn = xOut * dX; (xIn < xOut * dX + dX) && (xIn < nX); xIn ++) {
+		    value += input->image->data.F32[yIn][xIn];
+		    nPix ++;
+		}
+	    }
+	    output->image->data.F32[yOut][xOut] = value / nPix;
+	}
+    }
+
+    return true;
+}
Index: /trunk/ppImage/src/ppTest.c
===================================================================
--- /trunk/ppImage/src/ppTest.c	(revision 6848)
+++ /trunk/ppImage/src/ppTest.c	(revision 6849)
@@ -91,5 +91,5 @@
 
     // Add in a source file
-    pmFPAview *view = pmFPAAddSource(fpa, phu, cameraFormat);
+    pmFPAview *view = pmFPAAddSourceFromHeader(fpa, phu, cameraFormat);
     printf("View chip: %d\n", view->chip);
     printf("View cell: %d\n", view->cell);
@@ -101,5 +101,5 @@
     psMetadata *newFormat = psMetadataConfigParse(NULL, NULL, "mcshort_splice.config", true);
     pmConfigConformHeader(phu, newFormat);
-    pmFPAview *newView = pmFPAAddSource(newFPA, phu, newFormat);
+    pmFPAview *newView = pmFPAAddSourceFromHeader(newFPA, phu, newFormat);
     printf("View chip: %d\n", newView->chip);
     printf("View cell: %d\n", newView->cell);
