Index: /trunk/psastro/src/psastroMosaicHeaders.c
===================================================================
--- /trunk/psastro/src/psastroMosaicHeaders.c	(revision 7333)
+++ /trunk/psastro/src/psastroMosaicHeaders.c	(revision 7333)
@@ -0,0 +1,57 @@
+# include "psastro.h"
+
+bool psastroMosaicHeaders (pmConfig *config) {
+
+    bool status = false;
+    pmChip *chip = NULL;
+
+    // select the current recipe
+    psMetadata *recipe  = psMetadataLookupPtr (NULL, config->recipes, "PSASTRO");
+    if (!recipe) {
+	psErrorStackPrint(stderr, "Can't find PSASTRO recipe!\n");
+	exit(EXIT_FAILURE);
+    }
+
+    // select the input data sources
+    pmFPAfile *input = psMetadataLookupPtr (NULL, config->files, "PSASTRO.INPUT");
+    if (!input) {
+	psErrorStackPrint(stderr, "Can't find input data!\n");
+	exit(EXIT_FAILURE);
+    }
+
+    char *mosastro = psMetadataLookupStr (NULL, config->arguments, "MOSASTRO");
+
+    double plateScale = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLATE.SCALE");
+    if (!status) plateScale = 1.0;
+
+    pmFPAview *view = pmFPAviewAlloc (0);
+    pmFPA *fpa = input->fpa;
+
+    while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
+        psTrace (__func__, 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
+        if (!chip->process || !chip->file_exists) { continue; }
+	
+        // read WCS data from the corresponding header
+        pmHDU *hdu = pmFPAviewThisHDU (view, fpa);
+
+	pmAstromWriteBilevelChip (chip->toFPA, hdu->header, plateScale);
+    }
+
+    psMetadata *mosaic = pmAstromWriteBilevelMosaic (fpa->projection, fpa->toTangentPlane, plateScale);
+    
+    psFits *fits = psFitsOpen (mosastro, "w");
+    psFitsWriteHeaderNotImage (fits, mosaic);
+    psFitsClose (fits);
+
+    psFree (view);
+    return true;
+}
+
+/* coordinate frame hierachy
+   pixels (on a given readout)
+   cell
+   chip
+   FP (focal plane)
+   TP (tangent plane)
+   sky (ra, dec)
+*/
Index: /trunk/psastro/src/psastroMosaicRescaleChips.c
===================================================================
--- /trunk/psastro/src/psastroMosaicRescaleChips.c	(revision 7333)
+++ /trunk/psastro/src/psastroMosaicRescaleChips.c	(revision 7333)
@@ -0,0 +1,18 @@
+# include "psastro.h"
+
+bool psastroMosaicRescaleChips (pmFPA *fpa) {
+
+    pmChip *chip = NULL;
+    pmFPAview *view = pmFPAviewAlloc (0);
+
+    // this loop selects the matched stars for all chips
+    while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
+        psTrace (__func__, 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
+        if (!chip->process || !chip->file_exists) { continue; }
+	
+	psFree (chip->toFPA);
+	chip->toFPA = p_psPlaneTransformLinearInvert(chip->fromFPA);
+    }
+    psFree (view);
+    return true;
+}
