Index: /trunk/psastro/configure.ac
===================================================================
--- /trunk/psastro/configure.ac	(revision 10863)
+++ /trunk/psastro/configure.ac	(revision 10864)
@@ -8,4 +8,10 @@
 AM_CONFIG_HEADER([src/config.h])
 AM_MAINTAINER_MODE
+
+dnl otherwise AC_PROG_CC will default CFLAGS to "-g -02"
+if test -z ${CFLAGS} ; then
+  CFLAGS=""
+fi
+AC_SUBST([CFLAGS])
 
 AC_LANG(C)
@@ -22,7 +28,10 @@
   [AS_HELP_STRING(--enable-optimize,enable compiler optimization)],
   [AC_MSG_RESULT(compile optimization enabled)
-   CFLAGS="-O2"],
+   CFLAGS="${CFLAGS=} -O2 -g"],
   [AC_MSG_RESULT([compile optimization disabled])
-   CFLAGS="-O0 -g"]
+    if test x"${CFLAGS}" == x; then
+      CFLAGS="-O0 -g"
+    fi
+  ]
 )
 
@@ -31,5 +40,13 @@
   [AS_HELP_STRING(--enable-profile,enable compiler profiler information inclusion)],
   [AC_MSG_RESULT(compile optimization enabled)
-   CFLAGS="${CFLAGS=} -pg"]
+   CFLAGS="${CFLAGS=} -pg -g"
+   LDFLAGS="${LDFLAGS=} -pg"]
+)
+
+dnl turn off trace messages
+AC_ARG_ENABLE(trace,
+  [AS_HELP_STRING(--disable-trace,disable psTrace functionality)],
+  [AC_MSG_RESULT(psTrace disabled)
+   PSASTRO_CFLAGS="${PSASTRO_CFLAGS=} -DPS_NO_TRACE"]
 )
 
@@ -41,4 +58,162 @@
 fi
 
+dnl ------------------ kapa,libkapa options -------------------------
+dnl -- libkapa implies the requirement for libpng, libjpeg as well --
+
+dnl save LIBS/CFLAGS/LDFLAGS
+TMP_LIBS=${LIBS}
+TMP_CFLAGS=${CFLAGS}
+TMP_LDFLAGS=${LDFLAGS}
+TMP_CPPFLAGS=${CPPFLAGS}
+
+dnl test for command-line options: use ohana-config if not supplied
+KAPA_CFLAGS_CONFIG="true"
+KAPA_LIBS_CONFIG="true"
+AC_ARG_WITH(kapa,
+[AS_HELP_STRING(--with-kapa=DIR,Specify location of libkapa)],
+[KAPA_CFLAGS="-I$withval/include" KAPA_LIBS="-L$withval/lib" 
+ KAPA_CFLAGS_CONFIG="false"       KAPA_LIBS_CONFIG="false"])
+AC_ARG_WITH(kapa-include,
+[AS_HELP_STRING(--with-kapa-include=DIR,Specify libkapa include directory.)],
+[KAPA_CFLAGS="-I$withval" KAPA_CFLAGS_CONFIG="false"])
+AC_ARG_WITH(kapa-lib,
+[AS_HELP_STRING(--with-kapa-lib=DIR,Specify libkapa library directory.)],
+[KAPA_LIBS="-L$withval" KAPA_LIBS_CONFIG="false"])
+
+echo "KAPA_CFLAGS_CONFIG: $KAPA_CFLAGS_CONFIG"
+echo "KAPA_LIBS_CONFIG: $KAPA_LIBS_CONFIG"
+echo "KAPA_CFLAGS: $KAPA_CFLAGS"
+echo "KAPA_LIBS: $KAPA_LIBS"
+
+dnl HAVE_KAPA is set to false if any of the tests fail
+HAVE_KAPA="true"
+AC_MSG_NOTICE([checking for libkapa])
+if test "$KAPA_CFLAGS_CONFIG" = "true" -o "$KAPA_LIBS_CONFIG" = "true"; then
+  AC_MSG_NOTICE([kapa info supplied by ohana-config])
+  KAPA_CONFIG=`which ohana-config`
+  AC_CHECK_FILE($KAPA_CONFIG,[],
+    [HAVE_KAPA="false"; AC_MSG_WARN([libkapa is not found: output plots disabled.  Obtain libkapa at http://kiawe.ifa.hawaii.edu/Elixir/Ohana or use --with-kapa to specify location])])
+  
+  echo "HAVE_KAPA: $HAVE_KAPA"
+  echo "KAPA_CFLAGS_CONFIG: $KAPA_CFLAGS_CONFIG"
+
+  if test "$HAVE_KAPA" = "true" -a "$KAPA_CFLAGS_CONFIG" = "true" ; then
+   AC_MSG_NOTICE([libkapa cflags info supplied by ohana-config])
+   AC_MSG_CHECKING([libkapa cflags])
+   KAPA_CFLAGS="`${KAPA_CONFIG} --cflags`"
+   AC_MSG_RESULT([${KAPA_CFLAGS}])
+  fi
+
+  if test "$HAVE_KAPA" = "true" -a "$KAPA_LIBS_CONFIG" = "true" ; then
+   AC_MSG_NOTICE([libkapa ldflags info supplied by ohana-config])
+   AC_MSG_CHECKING([libkapa ldflags])
+   KAPA_LIBS="`${KAPA_CONFIG} --libs` -lX11"
+   AC_MSG_RESULT([${KAPA_LIBS}])
+  fi
+fi
+
+if test "$HAVE_KAPA" = "true" ; then
+ AC_MSG_NOTICE([libkapa supplied])
+ PSASTRO_CFLAGS="${PSASTRO_CFLAGS} ${KAPA_CFLAGS}"
+ PSASTRO_LIBS="${PSASTRO_LIBS} ${KAPA_LIBS}"
+else
+ AC_MSG_NOTICE([libkapa ignored])
+fi
+
+dnl restore the CFLAGS/LDFLAGS
+LIBS=${TMP_LIBS}
+CFLAGS=${TMP_CFLAGS}
+LDFLAGS=${TMP_LDFLAGS}
+CPPFLAGS=${TMP_CPPFLAGS}
+
+dnl ------------------ libjpeg options ---------------------
+
+dnl save LIBS/CFLAGS/LDFLAGS
+TMP_LIBS=${LIBS}
+TMP_CFLAGS=${CFLAGS}
+TMP_LDFLAGS=${LDFLAGS}
+TMP_CPPFLAGS=${CPPFLAGS}
+
+AC_ARG_WITH(jpeg,
+[AS_HELP_STRING(--with-jpeg=DIR,Specify location of libjpeg.)],
+[JPEG_CFLAGS="-I$withval/include"
+ JPEG_LDFLAGS="-L$withval/lib"])
+AC_ARG_WITH(jpeg-include,
+[AS_HELP_STRING(--with-jpeg-include=DIR,Specify libjpeg include directory.)],
+[JPEG_CFLAGS="-I$withval"])
+AC_ARG_WITH(jpeg-lib,
+[AS_HELP_STRING(--with-jpeg-lib=DIR,Specify libjpeg library directory.)],
+[JPEG_LDFLAGS="-L$withval"])
+
+CFLAGS="${CFLAGS} ${JPEG_CFLAGS}"
+CPPFLAGS=${CFLAGS}
+LDFLAGS="${LDFLAGS} ${JPEG_LDFLAGS}"
+
+AC_CHECK_HEADERS([jpeglib.h],
+  [PSASTRO_CFLAGS="$PSASTRO_CFLAGS $JPEG_CFLAGS" AC_SUBST(JPEG_CFLAGS)],
+  [HAVE_KAPA=false; AC_MSG_WARN([libjpeg headers not found: output plots disabled.  Obtain libjpeg from http://www.ijg.org/ or use --with-jpeg to specify location.])]
+)
+
+AC_CHECK_LIB(jpeg,jpeg_CreateCompress,
+  [PSASTRO_LIBS="$PSASTRO_LIBS $JPEG_LDFLAGS -ljpeg"],
+  [HAVE_KAPA=false; AC_MSG_WARN([libjpeg library not found: output plots disabled.  Obtain libjpeg from http://www.ijg.org/ or use --with-jpeg to specify location.])]
+)
+
+dnl restore the CFLAGS/LDFLAGS
+LIBS=${TMP_LIBS}
+CFLAGS=${TMP_CFLAGS}
+LDFLAGS=${TMP_LDFLAGS}
+CPPFLAGS=${TMP_CPPFLAGS}
+
+dnl ------------------ libpng options ---------------------
+
+dnl save LIBS/CFLAGS/LDFLAGS
+TMP_LIBS=${LIBS}
+TMP_CFLAGS=${CFLAGS}
+TMP_LDFLAGS=${LDFLAGS}
+TMP_CPPFLAGS=${CPPFLAGS}
+
+AC_ARG_WITH(png,
+[AS_HELP_STRING(--with-png=DIR,Specify location of libpng.)],
+[PNG_CFLAGS="-I$withval/include"
+ PNG_LDFLAGS="-L$withval/lib"])
+AC_ARG_WITH(png-include,
+[AS_HELP_STRING(--with-png-include=DIR,Specify libpng include directory.)],
+[PNG_CFLAGS="-I$withval"])
+AC_ARG_WITH(png-lib,
+[AS_HELP_STRING(--with-png-lib=DIR,Specify libpng library directory.)],
+[PNG_LDFLAGS="-L$withval"])
+
+CFLAGS="${CFLAGS} ${PNG_CFLAGS}"
+CPPFLAGS=${CFLAGS}
+LDFLAGS="${LDFLAGS} ${PNG_LDFLAGS}"
+
+AC_CHECK_HEADERS([png.h],
+  [PSASTRO_CFLAGS="$PSASTRO_CFLAGS $PNG_CFLAGS" AC_SUBST(PNG_CFLAGS)],
+  [HAVE_KAPA=false; AC_MSG_WARN([libpng headers not found: output plots disabled.  Obtain libpng from http://www.ijg.org/ or use --with-png to specify location.])]
+)
+
+AC_CHECK_LIB(png,png_init_io,
+  [PSASTRO_LIBS="$PSASTRO_LIBS $PNG_LDFLAGS -lpng"],
+  [HAVE_KAPA=false; AC_MSG_WARN([libpng library not found: output plots disabled.  Obtain libpng from http://www.ijg.org/ or use --with-png to specify location.])]
+)
+
+dnl restore the CFLAGS/LDFLAGS
+LIBS=${TMP_LIBS}
+CFLAGS=${TMP_CFLAGS}
+LDFLAGS=${TMP_LDFLAGS}
+CPPFLAGS=${TMP_CPPFLAGS}
+
+dnl ------------------ use kapa or not? ---------------------
+
+if test "$HAVE_KAPA" == "true" ; then
+  AC_MSG_RESULT([including plotting functions])
+  AC_DEFINE([HAVE_KAPA],[1],[enable use of libkapa])
+else
+  AC_MSG_RESULT([skipping plotting functions])
+  AC_DEFINE([HAVE_KAPA],[0],[disable use of libkapa])
+fi
+
+dnl ------------- psLib, psModules ---------------
 PKG_CHECK_MODULES(PSLIB, pslib >= 0.12.99)
 PKG_CHECK_MODULES(PSMODULE, psmodules >= 0.12.99)
@@ -46,4 +221,6 @@
 dnl Set CFLAGS for build
 CFLAGS="${CFLAGS} -Wall -Werror -std=c99"
+echo "PSASTRO_CFLAGS: $PSASTRO_CFLAGS"
+echo "PSASTRO_LIBS: $PSASTRO_LIBS"
 
 AC_SUBST([PSASTRO_CFLAGS])
Index: /trunk/psastro/src/Makefile.am
===================================================================
--- /trunk/psastro/src/Makefile.am	(revision 10863)
+++ /trunk/psastro/src/Makefile.am	(revision 10864)
@@ -5,5 +5,5 @@
 bin_PROGRAMS = psastro
 psastro_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PSASTRO_CFLAGS)
-psastro_LDFLAGS = $(PSLIB_LIBS) $(PSMODULE_LIBS)
+psastro_LDFLAGS = $(PSLIB_LIBS) $(PSMODULE_LIBS) $(PSASTRO_LIBS)
 psastro_LDADD = libpsastro.la
 
@@ -34,5 +34,7 @@
 psastroMosaicOneChip.c      \
 psastroMosaicSetAstrom.c    \
-psastroMosaicSetMatch.c
+psastroMosaicSetMatch.c     \
+psastroDemoDump.c           \
+psastroDemoPlot.c
 
 include_HEADERS = \
Index: /trunk/psastro/src/psastro.h
===================================================================
--- /trunk/psastro/src/psastro.h	(revision 10863)
+++ /trunk/psastro/src/psastro.h	(revision 10864)
@@ -70,2 +70,11 @@
 psString psastroVersionLong(void);
 
+// demo plots
+bool psastroPlotRawstars (psArray *rawstars, pmFPA *fpa, pmChip *chip);
+bool psastroPlotRefstars (psArray *refstars);
+bool psastroPlotOneChipFit (psArray *rawstars, psArray *refstars, psArray *match, pmAstromFitResults *results);
+
+bool psastroDumpRawstars (psArray *rawstars, pmFPA *fpa, pmChip *chip);
+bool psastroDumpRefstars (psArray *refstars);
+
+bool psastroMosaicSetAstrom_tmp (pmFPA *fpa);
Index: /trunk/psastro/src/psastroAstromGuess.c
===================================================================
--- /trunk/psastro/src/psastroAstromGuess.c	(revision 10863)
+++ /trunk/psastro/src/psastroAstromGuess.c	(revision 10864)
@@ -75,12 +75,4 @@
                 if (rawstars == NULL) { continue; }
 
-		if (psTraceGetLevel("psastro.dump.rawstars") > 2) {
-		  dump_stars (rawstars, fpa, chip);
-		}
-
-		if (psTraceGetLevel("psastro.plot.rawstars") > 2) {
-		  plot_stars (rawstars, fpa, chip);
-		}
-
                 for (int i = 0; i < rawstars->n; i++) {
                     pmAstromObj *raw = rawstars->data[i];
@@ -100,4 +92,13 @@
                     DECmax = PS_MAX (raw->sky->d, DECmax);
                 }
+
+		// dump or plot the resulting projected positions
+		if (psTraceGetLevel("psastro.dump") > 0) {
+		    psastroDumpRawstars (rawstars, fpa, chip);
+		}
+
+		if (psTraceGetLevel("psastro.plot") > 0) {
+		    psastroPlotRawstars (rawstars, fpa, chip);
+		}
             }
         }
@@ -125,42 +126,2 @@
    sky (ra, dec)
 */
-
-dump_stars (psArray *rawstars, pmFPA *fpa, pmChip *chip) {
-
-  FILE *f1 = fopen ("rawstars.up.dat", "w");
-  FILE *f2 = fopen ("rawstars.dn.dat", "w");
-  for (int i = 0; i < rawstars->n; i++) {
-    pmAstromObj *raw = rawstars->data[i];
-
-    psPlaneTransformApply (raw->FP, chip->toFPA, raw->chip);
-    psPlaneTransformApply (raw->TP, fpa->toTPA, raw->FP);
-    psDeproject (raw->sky, raw->TP, fpa->toSky);
-
-    fprintf (f1, "%d  %f %f  %f %f  %f %f  %f %f\n", i,
-	     raw->sky->r, raw->sky->d, 
-	     raw->TP->x, raw->TP->y, 
-	     raw->FP->x, raw->FP->y, 
-	     raw->chip->x, raw->chip->y);
-		
-    psPlane *fp = psPlaneAlloc();
-    psPlane *tp = psPlaneAlloc();
-    psPlane *ch = psPlaneAlloc();
-			
-    psProject (tp, raw->sky, fpa->toSky);
-    psPlaneTransformApply (fp, fpa->fromTPA, tp);
-    psPlaneTransformApply (ch, chip->fromFPA, fp);
-			
-    fprintf (f2, "%d  %f %f  %f %f  %f %f  %f %f\n", i,
-	     raw->sky->r, raw->sky->d, 
-	     tp->x, tp->y, 
-	     fp->x, fp->y, 
-	     ch->x, ch->y);
-		
-    psFree (fp);
-    psFree (tp);
-    psFree (ch);
-  }
-
-  fclose (f1);
-  fclose (f2);
-}
Index: /trunk/psastro/src/psastroDemoDump.c
===================================================================
--- /trunk/psastro/src/psastroDemoDump.c	(revision 10864)
+++ /trunk/psastro/src/psastroDemoDump.c	(revision 10864)
@@ -0,0 +1,59 @@
+# include "psastro.h"
+
+// this function is used for test purposes (-trace psastro.dump.psastroAstromGuess 1)
+bool psastroDumpRawstars (psArray *rawstars, pmFPA *fpa, pmChip *chip) {
+
+    FILE *f1 = fopen ("rawstars.dat", "w");
+    FILE *f2 = fopen ("rawstars.down.dat", "w");
+
+    for (int i = 0; i < rawstars->n; i++) {
+	pmAstromObj *raw = rawstars->data[i];
+
+	psPlane *fp = psPlaneAlloc();
+	psPlane *tp = psPlaneAlloc();
+	psPlane *ch = psPlaneAlloc();
+			
+	psProject (tp, raw->sky, fpa->toSky);
+	psPlaneTransformApply (fp, fpa->fromTPA, tp);
+	psPlaneTransformApply (ch, chip->fromFPA, fp);
+			
+	// write out the upward projections
+	fprintf (f1, "%d  %f %f  %f  %f %f  %f %f  %f %f\n", i,
+		 raw->sky->r, raw->sky->d, raw->Mag, 
+		 raw->TP->x, raw->TP->y, 
+		 raw->FP->x, raw->FP->y, 
+		 raw->chip->x, raw->chip->y);
+		
+	// write out the downward projections
+	fprintf (f2, "%d  %f %f  %f  %f %f  %f %f  %f %f\n", i,
+		 raw->sky->r, raw->sky->d, raw->Mag, 
+		 tp->x, tp->y, 
+		 fp->x, fp->y, 
+		 ch->x, ch->y);
+		
+	psFree (fp);
+	psFree (tp);
+	psFree (ch);
+    }
+
+    fclose (f1);
+    fclose (f2);
+    return true;
+}
+
+// this function is used for test purposes (-trace psastro.dump.psastroLoadRefstars 1)
+bool psastroDumpRefstars (psArray *refstars) {
+
+    FILE *f = fopen ("refstars.dat", "w");
+
+    for (int i = 0; i < refstars->n; i++) {
+	pmAstromObj *ref = refstars->data[i];
+
+	// write out the refstar data
+	fprintf (f, "%d  %f %f  %f\n", i,
+		 ref->sky->r, ref->sky->d, ref->Mag);
+    }
+
+    fclose (f);
+    return true;
+}
Index: /trunk/psastro/src/psastroDemoPlot.c
===================================================================
--- /trunk/psastro/src/psastroDemoPlot.c	(revision 10864)
+++ /trunk/psastro/src/psastroDemoPlot.c	(revision 10864)
@@ -0,0 +1,283 @@
+# include "psastro.h"
+
+# if (HAVE_KAPA)
+# include <kapa.h>
+
+bool psastroPlotRawstars (psArray *rawstars, pmFPA *fpa, pmChip *chip)
+{
+    Graphdata graphdata;
+    KapaSection section;
+
+    int kapa = pmKapaOpen (true);
+    if (kapa == -1) {
+        psError(PS_ERR_UNKNOWN, true, "failure to open kapa");
+        return false;
+    }
+
+    KapaResize (kapa, 1000, 1000);
+    KapaInitGraph (&graphdata);
+    KapaClear (kapa, TRUE);
+
+    graphdata.color = KapaColorByName ("black");
+    graphdata.ptype = 0;
+    graphdata.size = 0.5;
+    graphdata.style = 2;
+
+    section.dx = 0.5;
+    section.dy = 0.5;
+
+    psVector *xVec = psVectorAlloc (rawstars->n, PS_TYPE_F32);
+    psVector *yVec = psVectorAlloc (rawstars->n, PS_TYPE_F32);
+
+    section.x = 0.0;
+    section.y = 0.5;
+    section.name = NULL;
+    psStringAppend (&section.name, "a0");
+    KapaSetSection (kapa, &section);
+    psFree (section.name);
+
+    for (int i = 0; i < rawstars->n; i++) {
+	pmAstromObj *raw = rawstars->data[i];
+	xVec->data.F32[i] = raw->chip->x;
+	yVec->data.F32[i] = raw->chip->y;
+    }
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    section.x = 0.5;
+    section.y = 0.5;
+    section.name = NULL;
+    psStringAppend (&section.name, "a1");
+    KapaSetSection (kapa, &section);
+    psFree (section.name);
+
+    for (int i = 0; i < rawstars->n; i++) {
+	pmAstromObj *raw = rawstars->data[i];
+	xVec->data.F32[i] = raw->FP->x;
+	yVec->data.F32[i] = raw->FP->y;
+    }
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    section.x = 0.0;
+    section.y = 0.0;
+    section.name = NULL;
+    psStringAppend (&section.name, "a2");
+    KapaSetSection (kapa, &section);
+    psFree (section.name);
+
+    for (int i = 0; i < rawstars->n; i++) {
+	pmAstromObj *raw = rawstars->data[i];
+	xVec->data.F32[i] = raw->TP->x;
+	yVec->data.F32[i] = raw->TP->y;
+    }
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    section.x = 0.5;
+    section.y = 0.0;
+    section.name = NULL;
+    psStringAppend (&section.name, "a3");
+    KapaSetSection (kapa, &section);
+    psFree (section.name);
+
+    for (int i = 0; i < rawstars->n; i++) {
+	pmAstromObj *raw = rawstars->data[i];
+	xVec->data.F32[i] = raw->sky->r;
+	yVec->data.F32[i] = raw->sky->d;
+    }
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    // pause and wait for user input: 
+    // continue, save (provide name), ??
+    char key, name[80];
+    fprintf (stdout, "(s)ave plot or [c]ontinue? ");
+    fscanf (stdin, "%c", &key);
+    if (key == 's') {
+	fprintf (stdout, "enter plot name [rawstars.png]: ");
+	fscanf (stdin, "%s", name);
+	if (!strcmp (name, "")) strcpy (name, "rawstars.png");
+	KapaPNG (kapa, name);
+    }
+
+    psFree (xVec);
+    psFree (yVec);
+    return true;
+}
+
+bool psastroPlotRefstars (psArray *refstars)
+{
+    Graphdata graphdata;
+
+    int kapa = pmKapaOpen (true);
+    if (kapa == -1) {
+        psError(PS_ERR_UNKNOWN, true, "failure to open kapa");
+        return false;
+    }
+
+    KapaResize (kapa, 1000, 1000);
+    KapaInitGraph (&graphdata);
+    KapaClear (kapa, TRUE);
+
+    graphdata.color = KapaColorByName ("black");
+    graphdata.ptype = 0;
+    graphdata.size = 0.5;
+    graphdata.style = 2;
+
+    psVector *xVec = psVectorAlloc (refstars->n, PS_TYPE_F32);
+    psVector *yVec = psVectorAlloc (refstars->n, PS_TYPE_F32);
+
+    for (int i = 0; i < refstars->n; i++) {
+	pmAstromObj *ref = refstars->data[i];
+	xVec->data.F32[i] = ref->sky->r;
+	yVec->data.F32[i] = ref->sky->d;
+    }
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    // pause and wait for user input: 
+    // continue, save (provide name), ??
+    char key, name[80];
+    fprintf (stdout, "(s)ave plot or [c]ontinue? ");
+    fscanf (stdin, "%c", &key);
+    if (key == 's') {
+	fprintf (stdout, "enter plot name [refstars.png]: ");
+	fscanf (stdin, "%s", name);
+	if (!strcmp (name, "")) strcpy (name, "refstars.png");
+	KapaPNG (kapa, name);
+    }
+
+    psFree (xVec);
+    psFree (yVec);
+    return true;
+}
+
+bool psastroPlotOneChipFit (psArray *rawstars, psArray *refstars, psArray *match, pmAstromFitResults *results) {
+
+    Graphdata graphdata;
+    KapaSection section;
+
+    int kapa = pmKapaOpen (true);
+    if (kapa == -1) {
+        psError(PS_ERR_UNKNOWN, true, "failure to open kapa");
+        return false;
+    }
+
+    KapaResize (kapa, 1000, 1000);
+    KapaInitGraph (&graphdata);
+    KapaClear (kapa, TRUE);
+
+    graphdata.color = KapaColorByName ("black");
+    graphdata.ptype = 0;
+    graphdata.size = 0.5;
+    graphdata.style = 2;
+
+    section.dx = 0.5;
+    section.dy = 0.5;
+
+    psVector *xVec = psVectorAlloc (match->n, PS_TYPE_F32);
+    psVector *yVec = psVectorAlloc (match->n, PS_TYPE_F32);
+
+    // X vs dX
+    section.x = 0.0;
+    section.y = 0.5;
+    section.name = NULL;
+    psStringAppend (&section.name, "a0");
+    KapaSetSection (kapa, &section);
+    psFree (section.name);
+
+    for (int i = 0; i < match->n; i++) {
+        pmAstromMatch *pair = match->data[i];
+        pmAstromObj *raw = rawstars->data[pair->raw];
+        pmAstromObj *ref = refstars->data[pair->ref];
+	
+	xVec->data.F32[i] = raw->chip->x;
+	yVec->data.F32[i] = raw->chip->x - ref->chip->x;
+    }	
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    // X vs dY
+    section.x = 0.5;
+    section.y = 0.5;
+    section.name = NULL;
+    psStringAppend (&section.name, "a1");
+    KapaSetSection (kapa, &section);
+    psFree (section.name);
+
+    for (int i = 0; i < match->n; i++) {
+        pmAstromMatch *pair = match->data[i];
+        pmAstromObj *raw = rawstars->data[pair->raw];
+        pmAstromObj *ref = refstars->data[pair->ref];
+	
+	xVec->data.F32[i] = raw->chip->x;
+	yVec->data.F32[i] = raw->chip->y - ref->chip->y;
+    }	
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    // Y vs dX
+    section.x = 0.0;
+    section.y = 0.0;
+    section.name = NULL;
+    psStringAppend (&section.name, "a2");
+    KapaSetSection (kapa, &section);
+    psFree (section.name);
+
+    for (int i = 0; i < match->n; i++) {
+        pmAstromMatch *pair = match->data[i];
+        pmAstromObj *raw = rawstars->data[pair->raw];
+        pmAstromObj *ref = refstars->data[pair->ref];
+	
+	xVec->data.F32[i] = raw->chip->y;
+	yVec->data.F32[i] = raw->chip->x - ref->chip->x;
+    }	
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    // Y vs dY
+    section.x = 0.5;
+    section.y = 0.0;
+    section.name = NULL;
+    psStringAppend (&section.name, "a3");
+    KapaSetSection (kapa, &section);
+    psFree (section.name);
+
+    for (int i = 0; i < match->n; i++) {
+        pmAstromMatch *pair = match->data[i];
+        pmAstromObj *raw = rawstars->data[pair->raw];
+        pmAstromObj *ref = refstars->data[pair->ref];
+	
+	xVec->data.F32[i] = raw->chip->y;
+	yVec->data.F32[i] = raw->chip->y - ref->chip->y;
+    }	
+    pmKapaPlotVectorPair_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec);
+
+    // pause and wait for user input: 
+    // continue, save (provide name), ??
+    char key, name[80];
+    fprintf (stdout, "(s)ave plot or [c]ontinue? ");
+    fscanf (stdin, "%c", &key);
+    if (key == 's') {
+	fprintf (stdout, "enter plot name [chipfit.png]: ");
+	fscanf (stdin, "%s", name);
+	if (!strcmp (name, "")) strcpy (name, "chipfit.png");
+	KapaPNG (kapa, name);
+    }
+
+    psFree (xVec);
+    psFree (yVec);
+    return true;
+}
+
+# else
+
+bool psastroPlotRawstars (psArray *rawstars, pmFPA *fpa, pmChip *chip)
+{
+    return false;
+}
+
+bool psastroPlotRefstars (psArray *refstars)
+{
+    return false;
+}
+
+bool psastroPlotOneChipFit (psArray *rawstars, psArray *refstars, psArray *match, pmAstromFitResults *results)
+{
+    return false;
+}
+
+# endif
Index: unk/psastro/src/psastroDemoPlots.c
===================================================================
--- /trunk/psastro/src/psastroDemoPlots.c	(revision 10863)
+++ 	(revision )
@@ -1,67 +1,0 @@
-# include "psastro.h"
-
-# if (HAVE_KAPA)
-# include <kapa.h>
-
-bool pmKapaPlotVectorPair (psVector *xVec, psVector *yVec)
-{
-
-    Graphdata graphdata;
-
-    int kapa = pmKapaOpen (true);
-    if (kapa == -1) {
-        psError(PS_ERR_UNKNOWN, true, "failure to open kapa");
-        return false;
-    }
-
-    if (xVec->n != yVec->n)
-        return false;
-
-    KapaInitGraph (&graphdata);
-    KapaClear (kapa, FALSE);
-
-    // set limits based on data values
-    graphdata.xmin = FLT_MAX;
-    graphdata.xmax = FLT_MIN;
-    graphdata.ymin = FLT_MAX;
-    graphdata.ymax = FLT_MIN;
-    for (int i = 0; i < xVec->n; i++) {
-        graphdata.xmin = PS_MIN (graphdata.xmin, xVec->data.F32[i]);
-        graphdata.xmax = PS_MAX (graphdata.xmax, xVec->data.F32[i]);
-        graphdata.ymin = PS_MIN (graphdata.ymin, yVec->data.F32[i]);
-        graphdata.ymax = PS_MAX (graphdata.ymax, yVec->data.F32[i]);
-    }
-    // add 5% to range
-    float range;
-
-    range = graphdata.xmax - graphdata.xmin;
-    graphdata.xmax += 0.05*range;
-    graphdata.xmin -= 0.05*range;
-
-    range = graphdata.ymax - graphdata.ymin;
-    graphdata.ymax += 0.05*range;
-    graphdata.ymin -= 0.05*range;
-
-    KapaSetLimits (kapa, &graphdata);
-    KapaSetFont (kapa, "helvetica", 14);
-    KapaBox (kapa, &graphdata);
-
-    graphdata.color = KapaColorByName ("black");
-    graphdata.ptype = 0;
-    graphdata.size = 0.5;
-    graphdata.style = 2;
-    KapaPrepPlot (kapa, xVec->n, &graphdata);
-    KapaPlotVector (kapa, xVec->n, xVec->data.F32);
-    KapaPlotVector (kapa, yVec->n, yVec->data.F32);
-
-    return true;
-}
-
-# else
-
-bool pmKapaPlotVectorPair (psVector *xVec, psVector *yVec)
-{
-    return false;
-}
-
-# endif
Index: /trunk/psastro/src/psastroLoadRefstars.c
===================================================================
--- /trunk/psastro/src/psastroLoadRefstars.c	(revision 10863)
+++ /trunk/psastro/src/psastroLoadRefstars.c	(revision 10864)
@@ -1,4 +1,3 @@
 # include "psastro.h"
-// int mkstemp(char *template);
 # define ELIXIR_MODE 1
 
@@ -84,5 +83,5 @@
     // convert the Average table to the pmAstromObj entries
     psTimerStart ("psastro");
-    psArray *refs = psArrayAllocEmpty (table->n);
+    psArray *refstars = psArrayAllocEmpty (table->n);
     for (int i = 0; i < table->n; i++) {
         pmAstromObj *ref = pmAstromObjAlloc ();
@@ -101,5 +100,5 @@
         }
 
-        psArrayAdd (refs, 100, ref);
+        psArrayAdd (refstars, 100, ref);
         psFree (ref);
     }
@@ -109,6 +108,15 @@
 
     psTrace ("psastro", 3, "loaded %ld reference stars from (%10.6f,%10.6f) - (%10.6f,%10.6f)\n",
-             refs->n, RAmin, DECmin, RAmax, DECmax);
+             refstars->n, RAmin, DECmin, RAmax, DECmax);
 
-    return refs;
+    // dump or plot the available refstars
+    if (psTraceGetLevel("psastro.dump") > 0) {
+	psastroDumpRefstars (refstars);
+    }
+
+    if (psTraceGetLevel("psastro.plot") > 0) {
+	psastroPlotRefstars (refstars);
+    }
+
+    return refstars;
 }
Index: /trunk/psastro/src/psastroMosaicAstrom.c
===================================================================
--- /trunk/psastro/src/psastroMosaicAstrom.c	(revision 10863)
+++ /trunk/psastro/src/psastroMosaicAstrom.c	(revision 10864)
@@ -32,6 +32,8 @@
 
     // fitted chips will follow the local plate-scale, hiding the distortion
-    // modify the chip->toFPA scaling to match knowledge about pixel scale
+    // modify the chip->toFPA scaling to match knowledge about pixel scale,
+    // then recalculate raw and ref positions
     psastroMosaicCommonScale (fpa, recipe);
+    psastroMosaicSetAstrom (fpa);
 
     gradients = psastroMosaicGradients (fpa, recipe);
@@ -62,5 +64,5 @@
     // XXX modify match radius
     // XXX set chip.order to 1
-    psastroMosaicSetAstrom (fpa);
+    psastroMosaicSetAstrom_tmp (fpa);
     psastroMosaicSetMatch (fpa, recipe, 1);
     psastroMosaicChipAstrom (fpa, recipe, 1);
Index: /trunk/psastro/src/psastroMosaicSetAstrom.c
===================================================================
--- /trunk/psastro/src/psastroMosaicSetAstrom.c	(revision 10863)
+++ /trunk/psastro/src/psastroMosaicSetAstrom.c	(revision 10864)
@@ -49,2 +49,64 @@
     return true;
 }
+
+bool psastroMosaicSetAstrom_tmp (pmFPA *fpa) {
+
+    pmChip *chip = NULL;
+    pmCell *cell = NULL;
+    pmReadout *readout = NULL;
+    pmFPAview *view = pmFPAviewAlloc (0);
+
+    FILE *f1 = fopen ("raw.tmp.dat", "w");
+    FILE *f2 = fopen ("ref.tmp.dat", "w");
+
+    // this loop selects the matched stars for all chips
+    while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
+        psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
+        if (!chip->process || !chip->file_exists) { continue; }
+	
+	while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
+            psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+            if (!cell->process || !cell->file_exists) { continue; }
+
+	    // process each of the readouts
+	    // XXX there can only be one readout per chip, right?
+	    while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
+		if (! readout->data_exists) { continue; }
+
+		// select the raw objects for this readout
+		psArray *rawstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.RAWSTARS");
+		if (rawstars == NULL) { continue; }
+
+		for (int i = 0; i < rawstars->n; i++) {
+		    pmAstromObj *raw = rawstars->data[i];
+	
+		    fprintf (f1, "%f %f  %f %f  %f %f  ", raw->sky->r, raw->sky->d, raw->TP->x, raw->TP->y, raw->FP->x, raw->FP->y);
+
+		    psPlaneTransformApply (raw->FP, chip->toFPA, raw->chip);
+		    psPlaneTransformApply (raw->TP, fpa->toTPA, raw->FP);
+		    psDeproject (raw->sky, raw->TP, fpa->toSky);
+
+		    fprintf (f1, "|  %f %f  %f %f  %f %f   %f\n", raw->sky->r, raw->sky->d, raw->TP->x, raw->TP->y, raw->FP->x, raw->FP->y, raw->Mag);
+		}
+
+		psArray *refstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.REFSTARS");
+		if (refstars == NULL) { continue; }
+
+		for (int i = 0; i < refstars->n; i++) {
+		    pmAstromObj *ref = refstars->data[i];
+	
+		    fprintf (f2, "%f %f  %f %f  %f %f  ", ref->sky->r, ref->sky->d, ref->TP->x, ref->TP->y, ref->FP->x, ref->FP->y);
+
+		    psProject (ref->TP, ref->sky, fpa->toSky);
+		    psPlaneTransformApply (ref->FP, fpa->fromTPA, ref->TP);
+		    psPlaneTransformApply (ref->chip, chip->fromFPA, ref->FP);
+
+		    fprintf (f2, "|  %f %f  %f %f  %f %f   %f\n", ref->sky->r, ref->sky->d, ref->TP->x, ref->TP->y, ref->FP->x, ref->FP->y, ref->Mag);
+		}
+	    }
+	}
+    }
+    fclose (f1);
+    fclose (f2);
+    return true;
+}
Index: /trunk/psastro/src/psastroOneChip.c
===================================================================
--- /trunk/psastro/src/psastroOneChip.c	(revision 10863)
+++ /trunk/psastro/src/psastroOneChip.c	(revision 10864)
@@ -90,8 +90,6 @@
     }
     
-    // toTPA converts from FPA units (microns) to TPA units (linear degrees)
-    float plateScale1 = hypot (fpa->toTPA->x->coeff[1][0], fpa->toTPA->x->coeff[0][1]);
-    float plateScale2 = hypot (fpa->toTPA->y->coeff[1][0], fpa->toTPA->y->coeff[0][1]);
-    float plateScale = 0.5*(plateScale1 + plateScale2)*3600.0;
+    // toSky converts from FPA & TPA units (microns) to sky units (radians)
+    float plateScale = 0.5*(fpa->toSky->Xs + fpa->toSky->Ys)*3600.0*PM_DEG_RAD;
 
     // pixError is the average 1D scatter in pixels ('results' are in FPA units = microns)
@@ -105,5 +103,5 @@
 
     // XXX should these result in errors or be handled another way?
-    psLogMsg ("psastro", PS_LOG_INFO, "astrometry solution: error: %f, Nstars: %d", astError, astNstar);
+    psLogMsg ("psastro", PS_LOG_INFO, "astrometry solution: error: %f arcsec, Nstars: %d", astError, astNstar);
     if (astError > maxError) {
         psLogMsg("psastro", PS_LOG_INFO, "residual error is too large, failed to find a solution: %f > %f", astError, maxError);
@@ -127,6 +125,10 @@
     psMetadataAddF32 (updates, PS_LIST_TAIL, "EQUINOX",  PS_META_REPLACE, "equinox of ref catalog", 2000.0); // XXX this is bogus: should be defined based on equinox of refstars
 
-// write results
+    // write results
     psastroUpdateChipToFPA (fpa, chip, rawstars, refstars);
+    
+    if (psTraceGetLevel("psastro.plot") > 0) {
+	psastroPlotOneChipFit (rawstars, refstars, match, results);
+    }
 
     psFree (match);
