Index: branches/eam_branch_20081230/Branches.txt
===================================================================
--- branches/ipp-magic-v0/Branches.txt	(revision 21062)
+++ branches/eam_branch_20081230/Branches.txt	(revision 21072)
@@ -1,2 +1,19 @@
+
+eam_branch_20081230
+
+ * entire tree: This branch contains the image and vector mask
+   upgrades.
+
+ipp-magic-v0
+
+ * entire tree: This branch marks the point at which magic was
+   initially accepted for automatic release.  For magic processing
+   leading to automated release, it is necessary to use this branch.
+
+eam_branch_20081214
+
+ * psphot: This branch contains the work to improve the morphological
+   masking of cosmic rays.
+
 sj_ippTests_branch_20080929
  Branch rooted at tag sj_root_20080929
Index: branches/eam_branch_20081230/Ohana/src/addstar/include/skycells.h
===================================================================
--- branches/ipp-magic-v0/Ohana/src/addstar/include/skycells.h	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/addstar/include/skycells.h	(revision 21072)
@@ -12,5 +12,6 @@
 # include <glob.h>
 
-enum {SQUARES, TRIANGLES};
+enum {SQUARES, TRIANGLES, LOCAL};
+enum {TETRAHEDRON, CUBE, OCTOHEDRON, DODECAHEDRON, ICOSAHEDRON};
 
 typedef struct {
@@ -60,4 +61,5 @@
 int    VERBOSE;
 int    MODE;
+int    SOLID;
 int    FIX_NS;
 int    NMAX;
@@ -65,27 +67,34 @@
 double SCALE;
 double PADDING;
+int    LEVEL;
+
+double CENTER_RA, CENTER_DEC;
+double RANGE_RA,  RANGE_DEC;
 
 double EULER_A;
 double EULER_B;
 
-void         SetProtect                PROTO((int mode));
-int          SetSignals                PROTO(());
-int          Shutdown                  PROTO((char *message, ...); ) 
-void         TrapSignal                PROTO((int sig));
+void         SetProtect                	    PROTO((int mode));
+int          SetSignals                	    PROTO(());
+int          Shutdown                  	    PROTO((char *message, ...); ) 
+void         TrapSignal                	    PROTO((int sig));
 
-int 	     args_skycells 	       PROTO((int argc, char **argv));
-int 	     ConfigInit_skycells       PROTO((int *argc, char **argv));
-int 	     sky_tessalation 	       PROTO((FITS_DB *db, int level, int Nmax, int mode, double scale));
-int          sky_tessalation_init      PROTO((double scale));
-int 	     sky_tessalation_triangles PROTO((FITS_DB *db, int level, int Nmax));
-int 	     sky_tessalation_squares   PROTO((FITS_DB *db, int level, int Nmax));
-int 	     sky_triangle_to_image     PROTO((Image *image, SkyTriangle *triangle));
-int 	     sky_triangle_to_rectangle PROTO((SkyRectangle *image, SkyTriangle *triangle));
-int 	     sky_subdivide_image       PROTO((Image *output, SkyRectangle *input, int Nx, int Ny));
-int 	     sky_triangle_coords       PROTO((SkyTriangle *triangle));
-SkyTriangle *sky_divide_triangles      PROTO((SkyTriangle *in, int *ntriangles));
-SkyTriangle *sky_base_triangles        PROTO((int *ntriangles));
-int          sky_base_rotation         PROTO((SkyTriangle *base, int Nbase));
-Point        sky_divide_edge           PROTO((Point v1, Point v2));
+int 	     args_skycells 	       	    PROTO((int argc, char **argv));
+int 	     ConfigInit_skycells       	    PROTO((int *argc, char **argv));
+int 	     sky_tessellation 	       	    PROTO((FITS_DB *db, int level, int Nmax, int mode, double scale));
+int          sky_tessellation_init          PROTO((double scale));
+int 	     sky_tessellation_local         PROTO((FITS_DB *db, int level, int Nmax));
+int 	     sky_tessellation_triangles     PROTO((FITS_DB *db, int level, int Nmax));
+int 	     sky_tessellation_squares       PROTO((FITS_DB *db, int level, int Nmax));
+int 	     sky_triangle_to_image     	    PROTO((Image *image, SkyTriangle *triangle));
+int 	     sky_triangle_to_rectangle 	    PROTO((SkyRectangle *image, SkyTriangle *triangle));
+int          sky_rectangle_local            PROTO((SkyRectangle *rectangle));
+int 	     sky_subdivide_image       	    PROTO((Image *output, SkyRectangle *input, int Nx, int Ny));
+int 	     sky_triangle_coords       	    PROTO((SkyTriangle *triangle));
+SkyTriangle *sky_divide_triangles      	    PROTO((SkyTriangle *in, int *ntriangles));
+SkyTriangle *sky_base_triangles        	    PROTO((int *ntriangles));
+SkyTriangle *sky_base_triangles_icosahedron PROTO((int *ntriangles));
+int          sky_base_rotation         	    PROTO((SkyTriangle *base, int Nbase));
+Point        sky_divide_edge           	    PROTO((Point v1, Point v2));
 
 // XXX migrate to libdvo eventually
Index: branches/eam_branch_20081230/Ohana/src/addstar/src/args_skycells.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/addstar/src/args_skycells.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/addstar/src/args_skycells.c	(revision 21072)
@@ -6,4 +6,6 @@
   int N;
   char *ptr;
+
+  if (argc == 1) goto escape;
 
   /* check for help request */
@@ -22,7 +24,63 @@
   /* what type of output files? */
   MODE = SQUARES;
-  if ((N = get_argument (argc, argv, "-triangles"))) {
-    MODE = TRIANGLES;
+  if ((N = get_argument (argc, argv, "-mode"))) {
     remove_argument (N, &argc, argv);
+    if (!strcasecmp (argv[N], "squares")) {
+      MODE = SQUARES;
+    }
+    if (!strcasecmp (argv[N], "triangles")) {
+      MODE = SQUARES;
+    }
+    if (!strcasecmp (argv[N], "local")) {
+      MODE = LOCAL;
+    }
+    remove_argument (N, &argc, argv);
+  }
+
+  /* what base solid? */
+  SOLID = ICOSAHEDRON;
+  if ((N = get_argument (argc, argv, "-solid"))) {
+    remove_argument (N, &argc, argv);
+    if (!strncasecmp (argv[N], "TETRAHEDRON", MIN(4, strlen(argv[N])))) {
+      SOLID = TETRAHEDRON;
+    }
+    if (!strncasecmp (argv[N], "CUBE", MIN(4, strlen(argv[N])))) {
+      SOLID = CUBE;
+    }
+    if (!strncasecmp (argv[N], "OCTOHEDRON", MIN(4, strlen(argv[N])))) {
+      SOLID = OCTOHEDRON;
+    }
+    if (!strncasecmp (argv[N], "DODECAHEDRON", MIN(4, strlen(argv[N])))) {
+      SOLID = DODECAHEDRON;
+    }
+    if (!strncasecmp (argv[N], "ICOSAHEDRON", MIN(4, strlen(argv[N])))) {
+      SOLID = ICOSAHEDRON;
+    }
+    remove_argument (N, &argc, argv);
+  }
+
+  if (MODE == LOCAL) {
+    // for local mode, need to define the center and range
+    if ((N = get_argument (argc, argv, "-center"))) {
+      remove_argument (N, &argc, argv);
+      CENTER_RA  = atof (argv[N]);
+      remove_argument (N, &argc, argv);
+      CENTER_DEC = atof (argv[N]);
+      remove_argument (N, &argc, argv);
+    } else {
+      fprintf (stderr, "missing -center RA DEC for LOCAL mode\n");
+      help ();
+    }
+    // for local mode, need to define the center and range
+    if ((N = get_argument (argc, argv, "-size"))) {
+      remove_argument (N, &argc, argv);
+      RANGE_RA  = atof (argv[N]);
+      remove_argument (N, &argc, argv);
+      RANGE_DEC = atof (argv[N]);
+      remove_argument (N, &argc, argv);
+    } else {
+      fprintf (stderr, "missing -size dRA dDEC for LOCAL mode\n");
+      help ();
+    }
   }
 
@@ -81,10 +139,21 @@
   }
 
-  if (argc == 2) {
-    strtol (argv[1], &ptr, 10);
-    if (*ptr == 0) return (TRUE);
+  LEVEL = 8;
+  if ((MODE != LOCAL) && (N = get_argument (argc, argv, "-level"))) {
+    remove_argument (N, &argc, argv);
+    LEVEL = strtol (argv[N], &ptr, 10);
+    remove_argument (N, &argc, argv);
+    if ((*ptr != 0) || (LEVEL < 0)) {
+      fprintf (stderr, "-level requires an integer (>= 0) argument\n");
+      help ();
+    }  
   }
 
-  fprintf (stderr, "USAGE: skycells (level) [-scale arcsec/pix] [-nx (Nx cells)] [-ny (Ny cells)]\n");
+  if (argc != 1) goto escape;
+
+  return (TRUE);
+
+escape:
+  fprintf (stderr, "USAGE: skycells [-mode mode] [-level level] [-scale arcsec/pix] [-nx (Nx cells)] [-ny (Ny cells)]\n");
   fprintf (stderr, "  [-h for details and other options]\n");
   exit (2);
@@ -96,14 +165,23 @@
   fprintf (stderr, "  skycells\n\n");
 
-  fprintf (stderr, "  -v                    	  : verbose mode\n");
+  fprintf (stderr, "  -mode (name)                : define the type of tessellation.  available options are:\n");
+  fprintf (stderr, "     SQUARE                   : generate rectangular skycells using icosahedron base solid (default)\n");
+  fprintf (stderr, "     TRIANGLE                 : generate triangular skycells using icosahedron base solid\n");
+  fprintf (stderr, "     LOCAL                    : generate a local tessellation around a spot on the sky\n");
+  fprintf (stderr, "                                 (Note that this tessellation does not cover the full sky)\n");
+  fprintf (stderr, "  -solid (name)               : specify the base solid\n");
+  fprintf (stderr, "                                value may be one of: TETRAHEDRON, CUBE, OCTOHEDRON, DODECAHEDRON, ICOSAHEDRON\n");
+  fprintf (stderr, "                                for convenience, only the first 4 characters are required\n");
+  fprintf (stderr, "  -v                          : verbose mode\n");
   fprintf (stderr, "  -triangles                  : save base triangles instead of skycells\n");
   fprintf (stderr, "  -fix-ns                     : orient skycells with y-axis aligned with Dec\n");
-  fprintf (stderr, "  -scale                      : set pixel scale (default 1.0 arcsec / pixel)\n");
-  fprintf (stderr, "  -padding                    : pad skycells by this fraction in each dimension\n");
-  fprintf (stderr, "  -nmax                    	  : only keep nmax skycells in memory\n");
-  fprintf (stderr, "  -nx                    	  : subdivide skycell projection in x\n");
-  fprintf (stderr, "  -ny                    	  : subdivide skycell projection in y\n");
-  fprintf (stderr, "  -help                 	  : this list\n");
-  fprintf (stderr, "  -h                    	  : this list\n\n");
+  fprintf (stderr, "  -scale (scale)              : set pixel scale in arcsec (default 1.0 arcsec / pixel)\n");
+  fprintf (stderr, "  -euler (A) (B)              : define Euler A and B rotation angles (degrees)\n");
+  fprintf (stderr, "  -padding (fraction)         : pad skycells by this fraction in each dimension\n");
+  fprintf (stderr, "  -nmax Nmax                  : only keep Nmax skycells in memory\n");
+  fprintf (stderr, "  -nx Nx                      : subdivide skycell projection in x by Nx\n");
+  fprintf (stderr, "  -ny Ny                      : subdivide skycell projection in y by Ny\n");
+  fprintf (stderr, "  -help                       : this list\n");
+  fprintf (stderr, "  -h                          : this list\n\n");
   exit (2);
 }
Index: branches/eam_branch_20081230/Ohana/src/addstar/src/sky_tessalation.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/addstar/src/sky_tessalation.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/addstar/src/sky_tessalation.c	(revision 21072)
@@ -7,22 +7,27 @@
 static Coords *refcoords = NULL;
 
-int sky_tessalation (FITS_DB *db, int level, int Nmax, int mode, double scale) {
-
-  sky_tessalation_init (scale);
+int sky_tessellation (FITS_DB *db, int level, int Nmax, int mode, double scale) {
+
+  sky_tessellation_init (scale);
 
   if (mode == SQUARES) {
-    sky_tessalation_squares (db, level, Nmax);
+    sky_tessellation_squares (db, level, Nmax);
     return TRUE;
   }
 
   if (mode == TRIANGLES) {
-    sky_tessalation_triangles (db, level, Nmax);
+    sky_tessellation_triangles (db, level, Nmax);
     return TRUE;
   }
 
+  if (mode == LOCAL) {
+    sky_tessellation_local (db, level, Nmax);
+    return TRUE;
+  }
+
   return FALSE;
 }
 
-int sky_tessalation_triangles (FITS_DB *db, int level, int Nmax) {
+int sky_tessellation_triangles (FITS_DB *db, int level, int Nmax) {
 
   int i, j, Ndigit, Ntriangles, Nbase, Ntotal, Ltop, Nout, Nimages;
@@ -96,5 +101,5 @@
 }
 
-int sky_tessalation_squares (FITS_DB *db, int level, int Nmax) {
+int sky_tessellation_squares (FITS_DB *db, int level, int Nmax) {
 
   int i, j, Nname, Ndigit, Ntriangles, Nbase, Nimage, Ntotal, Ntop, Ltop, Nsubset, Nx, Ny;
@@ -186,4 +191,35 @@
     free (tri);
   }
+  return (TRUE);
+}
+
+// use CENTER_RA and CENTER_DEC as the starting point.  generate a single projection 
+// of size RANGE_RA and RANGE_DEC, and subdivide as specified.  This pseudo-tessellation may be used
+// for local projects such as the PS1 Medium Deep fields
+int sky_tessellation_local (FITS_DB *db, int level, int Nmax) {
+
+  int Nimage;
+  SkyRectangle rectangle;
+  Image *image;
+
+  // this tessellation consists of a single projection center with Nx * Ny cells
+  sky_rectangle_local (&rectangle);
+  strcpy (rectangle.name, "skycell");
+
+  // subdivide each image (Nx x Ny subcells)
+  Nimage = NX_SUB*NY_SUB;
+  ALLOCATE (image, Image, Nimage);
+
+  // convert the SkyRectangle to Images for output
+  sky_subdivide_image (image, &rectangle, NX_SUB, NY_SUB);
+
+  /* add the new images and save */
+  dvo_image_addrows (db, image, Nimage);
+  SetProtect (TRUE);
+  dvo_image_update (db, VERBOSE);
+  SetProtect (FALSE);
+  dvo_image_clear_vtable (db);
+
+  free (image);
   return (TRUE);
 }
@@ -402,4 +438,48 @@
 }
 
+// define the parameters of a single sky projection center
+int sky_rectangle_local (SkyRectangle *rectangle) {
+
+  int NX, NY;
+  float angle;
+
+  memset (rectangle, 0, sizeof(SkyRectangle));
+  rectangle[0].coords.crval1 = CENTER_RA;
+  rectangle[0].coords.crval2 = CENTER_DEC;
+
+  // we will add this as an option later
+  angle = 0.0;
+  if (FIX_NS) {
+    rectangle[0].coords.pc1_1 = +1.0;
+    rectangle[0].coords.pc1_2 = +0.0;
+    rectangle[0].coords.pc2_1 = -0.0;
+    rectangle[0].coords.pc2_2 = +1.0;
+  } else {
+    rectangle[0].coords.pc1_1 = +cos(angle);
+    rectangle[0].coords.pc1_2 = +sin(angle);
+    rectangle[0].coords.pc2_1 = -sin(angle);
+    rectangle[0].coords.pc2_2 = +cos(angle);
+  }
+  
+  // range values are in projected degrees
+  NX = RANGE_RA  * 3600.0 / SCALE;
+  NY = RANGE_DEC * 3600.0 / SCALE;
+
+  // crpix1,crpix2 is the projection center
+  rectangle[0].coords.crpix1 = 0.5*NX;
+  rectangle[0].coords.crpix2 = 0.5*NY;
+
+  rectangle[0].coords.cdelt1 = SCALE / 3600.0;
+  rectangle[0].coords.cdelt2 = SCALE / 3600.0;
+
+  strcpy (rectangle[0].coords.ctype, "DEC--TAN");
+
+  rectangle[0].NX = NX;
+  rectangle[0].NY = NY;
+  rectangle[0].code = 1; // this needs to be set more sensibly
+
+  return (TRUE);
+}
+
 // an allocated image set is supplied, we fill in the values
 int sky_subdivide_image (Image *output, SkyRectangle *input, int Nx, int Ny) {
@@ -517,8 +597,39 @@
 }
 
+SkyTriangle *sky_base_triangles (int *ntriangles) {
+
+  SkyTriangle *tri;
+
+  tri = NULL;
+
+  switch (SOLID) {
+    case TETRAHEDRON:
+      fprintf (stderr, "TETRAHEDRON is not yet defined\n");
+      exit (2);
+      break;
+    case CUBE:
+      fprintf (stderr, "CUBE is not yet defined\n");
+      exit (2);
+      break;
+    case OCTOHEDRON:
+      fprintf (stderr, "OCTOHEDRON is not yet defined\n");
+      exit (2);
+      break;
+    case DODECAHEDRON:
+      fprintf (stderr, "DODECAHEDRON is not yet defined\n");
+      exit (2);
+      break;
+    case ICOSAHEDRON:
+      tri = sky_base_triangles_icosahedron (ntriangles);
+  }
+
+  return tri;
+
+}
+
 # define THETA RAD_DEG*26.565
 # define D_PSI RAD_DEG*360.0/5.0
 
-SkyTriangle *sky_base_triangles (int *ntriangles) {
+SkyTriangle *sky_base_triangles_icosahedron (int *ntriangles) {
 
   int i;
@@ -642,5 +753,5 @@
 }
 
-int sky_tessalation_init (double scale) {
+int sky_tessellation_init (double scale) {
 
   ALLOCATE (refcoords, Coords, 1);
Index: branches/eam_branch_20081230/Ohana/src/addstar/src/skycells.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/addstar/src/skycells.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/addstar/src/skycells.c	(revision 21072)
@@ -3,5 +3,5 @@
 int main (int argc, char **argv) {
 
-  int status, level;
+  int status;
   FITS_DB db;
 
@@ -9,6 +9,5 @@
   ConfigInit_skycells (&argc, argv);
   args_skycells (argc, argv);
-  level = atoi (argv[1]);
-  
+
   /*** update the image table ***/
   /* setup image table format and lock */
@@ -28,7 +27,7 @@
   }
 
-  // we have to put the database update calls deep down in the sky_tessalation code so we
+  // we have to put the database update calls deep down in the sky_tessellation code so we
   // can write out the skycells in limited-sized chunks.
-  sky_tessalation (&db, level, NMAX, MODE, SCALE);
+  sky_tessellation (&db, LEVEL, NMAX, MODE, SCALE);
 
   dvo_image_unlock (&db);
Index: branches/eam_branch_20081230/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kapa2/include/prototypes.h	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/kapa2/include/prototypes.h	(revision 21072)
@@ -144,4 +144,5 @@
 
 int           Center              PROTO(());
+int           Parity              PROTO(());
 void          SetColorScale       PROTO((Graphic *graphic, KapaImageWidget *image));
 void          SetColorScale1D     PROTO((Graphic *graphic, KapaImageWidget *image));
Index: branches/eam_branch_20081230/Ohana/src/kapa2/src/Center.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kapa2/src/Center.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/kapa2/src/Center.c	(revision 21072)
@@ -31,2 +31,34 @@
 }
 
+int Parity (int sock) {
+
+  int X, Y;
+  Graphic *graphic;
+  Section *section;
+  KapaImageWidget *image;
+
+  KiiScanMessage (sock, "%d %d", &X,  &Y);
+
+  graphic = GetGraphic();
+  section = GetActiveSection();
+  image = section->image;
+  if (image == NULL) return (TRUE);
+
+  image[0].picture.flipx = X;
+  image[0].picture.flipy = Y;
+
+  image[0].zoom.flipx 	 = X;
+  image[0].zoom.flipy 	 = Y;
+
+  image[0].wide.flipx 	 = X;
+  image[0].wide.flipy 	 = Y;
+
+  if (USE_XWINDOW) {
+    Remap (graphic, image);
+    Refresh ();
+    XFlush (graphic[0].display);
+  }
+
+  return (TRUE);
+}
+
Index: branches/eam_branch_20081230/Ohana/src/kapa2/src/CheckPipe.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kapa2/src/CheckPipe.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/kapa2/src/CheckPipe.c	(revision 21072)
@@ -304,4 +304,10 @@
   }
 
+  if (!strcmp (word, "PARI")) {
+    status = Parity (sock);
+    KiiSendCommand (sock, 4, "DONE");
+    FINISHED (status);
+  }
+
   if (!strcmp (word, "NPIX")) {
     GetPixelCount (sock);
Index: branches/eam_branch_20081230/Ohana/src/kapa2/src/LoadPicture.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kapa2/src/LoadPicture.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/kapa2/src/LoadPicture.c	(revision 21072)
@@ -89,8 +89,10 @@
     return (FALSE);
   }
+  SetColorScale (graphic, image);
+
+  SetColorScale (graphic, image);
 
   if (!USE_XWINDOW) return (TRUE);
 
-  SetColorScale (graphic, image);
   Remap (graphic, image);
   CreateWide (graphic, image);
Index: branches/eam_branch_20081230/Ohana/src/libdvo/src/mosaic_astrom.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/libdvo/src/mosaic_astrom.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/libdvo/src/mosaic_astrom.c	(revision 21072)
@@ -117,5 +117,6 @@
     if (strcmp(&images[i].coords.ctype[4], "-WRP")) continue;
     for (j = 0; j < Ndis; j++) {
-      if (DIStzero[j] != images[i].tzero) continue;
+      if (DIStzero[j] < images[i].tzero) continue;
+      if (DIStzero[j] > images[i].tzero + (int) images[i].exptime) continue;
       ChipMatch[i] = DISentry[j];
       goto got_match;
Index: branches/eam_branch_20081230/Ohana/src/libfits/header/F_H_field.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/libfits/header/F_H_field.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/libfits/header/F_H_field.c	(revision 21072)
@@ -60,4 +60,65 @@
 }
 
+// find the given field among the HIERARCH keywords
+// these have the form: HIERARCH KEYWORD = value
+// returns a pointer to the start of the field
+char *gfits_header_hierarch_field (Header *header, char *field, int N) {
+
+  char *buf, *ptr;
+  int i, Nwant, Nfound, Nfield;
+
+  Nfield = strlen (field);
+  if (Nfield >= 71) return NULL;
+
+  buf = header[0].buffer;
+
+  /* find the Nth entry */
+  if (N > 0) {
+    Nfound = 0;
+    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
+      // have we found a HIERARCH entry?
+      if (strncmp ("HIERARCH", buf, 8)) continue;
+      ptr = buf + 9; // start of following keyword
+      if (strncmp (field, ptr, Nfield)) continue;
+      Nfound ++;
+      if (Nfound == N) return (ptr);
+    }
+  }
+
+  /* find the Ntotal - Nth entry */
+  if (N < 0) {
+    /* count the entries */
+    Nfound = 0;
+    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
+      if (strncmp ("HIERARCH", buf, 8)) continue;
+      ptr = buf + 9; // start of following keyword
+      if (strncmp (field, ptr, Nfield)) continue;
+      Nfound ++;
+    }
+
+    Nwant = Nfound + N + 1;
+    buf = header[0].buffer;
+
+    /* find the Nwant entry */
+    Nfound = 0;
+    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
+      if (strncmp ("HIERARCH", buf, 8)) continue;
+      ptr = buf + 9; // start of following keyword
+      if (strncmp (field, ptr, Nfield)) continue;
+      Nfound ++;
+      if (Nwant == Nfound) return (ptr);
+    }
+  }
+
+  return ((char *) NULL);
+
+/* 
+
+   find the Nth entry of this keyword.
+   if N < 0, find the last - N - 1 word (ie, -1 = last)
+
+*/
+}
+
 /*********************** fits header field ****************************/
 char *gfits_header_lineno (Header *header, int N) {
Index: branches/eam_branch_20081230/Ohana/src/libfits/header/F_modify.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/libfits/header/F_modify.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/libfits/header/F_modify.c	(revision 21072)
@@ -1,8 +1,4 @@
 # include <ohana.h>
 # include <gfitsio.h>
-
-char *gfits_keyword_start (char *line);
-char *gfits_keyword_end (char *line);
-void pad_ending (char *line, char value, int Nbyte);
 
 int gfits_modify (Header *header, char *field, char *mode, int N,...) {
@@ -51,5 +47,5 @@
     strncpy (comment, qe, p + 80 - qe);
   }
-  pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
+  gfits_pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
 
   /* write the numeric modes */
@@ -96,5 +92,5 @@
     strncpy (data, qs, MAX (MIN (qe - qs, 71), 0));
     strncpy (comment, va_arg (argp, char *), 80);
-    pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
+    gfits_pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
     snprintf (string, 81, "%-8s= %s / %-s", field, data, comment);
     /* this will keep the original line, but truncate the comment */
@@ -156,4 +152,57 @@
   } else {
     while ((*c1 != ' ') && (c1 < line + 80)) c1++;
+  }
+  return (c1);
+}
+
+/* given a pointer to the FITS HIERARCH card field name, return pointer to the start of the data area */
+char *gfits_hierarch_keyword_start (char *line, char *field) {
+
+  char *c;
+
+  /* find the end of the existing data region */
+  c = line + strlen(field) + 1;
+  if (*c != '=') return (c);  /* non-data fields are not allowed */
+  
+  c += 2;
+  /* advance pointer over WHITESPACE */
+  while ((*c == ' ') && (c < line + 71)) { c++; }
+  
+  /* skip one quote mark */
+  if ((*c == 0x27) && (c < line + 71)) { c++; }
+
+  return (c);
+}
+
+/* given a pointer to the FITS HIERARCH card field name, return pointer to the end of the data area */
+char *gfits_hierarch_keyword_end (char *line, char *field) {
+
+  int done;
+  char *c1, *c2;
+
+  /* find the end of the existing data region */
+  c1 = line + strlen(field) + 1;
+  if (*c1 != '=') return (NULL);  /* non-data fields are not allowed */
+  c1 += 2;
+
+  /* advance pointer over WHITESPACE */
+  while ((*c1 == ' ') && (c1 < line + 71)) { c1++; }
+  
+  if (c1[0] == 0x27) { /* entry a string, skip over 'fred' */
+    for (done = FALSE, c2 = c1 + 1; !done && (c2 < line + 71); c2++) {
+      if ((c2[0] == 0x27) && (c2[1] == 0x27)) {
+	c2 += 2;
+	continue;
+      }
+      if (c2[0] == 0x27) {
+	c1 = c2;
+	done = TRUE;
+      }
+    }
+    if (!done) { /* error in line: mismatched ' chars, return fixed position */
+      c1 = line + 30;
+    }
+  } else {
+    while ((*c1 != ' ') && (c1 < line + 71)) c1++;
   }
   return (c1);
@@ -171,5 +220,5 @@
 
 /* fill 'line' with Nbyte space from first NULL to last byte with value */
-void pad_ending (char *line, char value, int Nbyte) {
+void gfits_pad_ending (char *line, char value, int Nbyte) {
   
   char *p;
Index: branches/eam_branch_20081230/Ohana/src/libfits/header/F_scan.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/libfits/header/F_scan.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/libfits/header/F_scan.c	(revision 21072)
@@ -1,7 +1,4 @@
 # include <ohana.h>
 # include <gfitsio.h>
-
-char *gfits_keyword_start (char *line);
-char *gfits_keyword_end (char *line);
 
 int gfits_scan (Header *header, char *field, char *mode, int N,...) {
@@ -19,10 +16,13 @@
 
   char *p, *q, *s, tmp[81];
-  int Nchar;
+  int Nchar, status;
   double value;
   
   /* find the correct line with field */
   p = gfits_header_field (header, field, N);
-  if (p == NULL) return (FALSE);
+  if (p == NULL) {
+    status = gfits_vscan_hierarch (header, field, mode, N, argp);
+    return (status);
+  }
 
   /* non-data entry (COMMENT, HISTORY) */
@@ -92,4 +92,83 @@
 }
 
+# define HIERARCH_LENGTH 71
+int gfits_vscan_hierarch (Header *header, char *field, char *mode, int N, va_list argp) {
+
+  char *p, *q, *s, tmp[81];
+  int Nchar, Nfield;
+  double value;
+  
+  /* find the correct line with field */
+  p = gfits_header_hierarch_field (header, field, N);
+  if (p == NULL) return (FALSE);
+
+  /* non-data entries (COMMENT, HISTORY) are not allowed */
+  if (!strcmp (mode, "%S")) {
+    return (FALSE);
+  }
+
+  /* all others require '=' in column p + 1 + strlen(field) */
+  Nfield = strlen (field);
+  if (p[Nfield + 1] != '=') return (FALSE);
+
+  /* comment from data line */
+  if (!strcmp (mode, "%C")) {
+    q = gfits_hierarch_keyword_end (p, field);
+    if (!q) return (FALSE);
+    q += 3;
+    q = MIN (p + HIERARCH_LENGTH, q);
+    bzero (tmp, 81);
+    Nchar = MIN (HIERARCH_LENGTH, p + HIERARCH_LENGTH - q);
+    memcpy (tmp, q, Nchar);
+    stripwhite (tmp);
+    strcpy (va_arg (argp, char *), tmp);
+    return (TRUE);
+  }
+
+  /* extract data into char array (exclude containing ' chars) */
+  if (!strcmp (mode, "%s")) {
+    s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
+    q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
+
+    Nchar = MIN (HIERARCH_LENGTH, MAX (0, (q - s)));
+    bzero (tmp, 81);
+    memcpy (tmp, s, Nchar);
+    stripwhite (tmp);
+    strcpy (va_arg (argp, char *), tmp);
+    return (TRUE);
+  }
+  
+  /* boolean data, requires int target */
+  if (!strcmp (mode, "%t")) {
+    s = gfits_hierarch_keyword_start (p, field);
+    if (*s == 'T') {
+      *va_arg (argp, int *) = TRUE;
+      return (TRUE);
+    }
+    if (*s == 'F') {
+      *va_arg (argp, int *) = FALSE;   
+      return (TRUE);
+    }
+  }
+
+  /* remaining options are numerical data */
+  /* need to interpret 1.0d5 as 1.0e5 */
+  s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
+  q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
+  value = strtod (s, &q);
+  if ((*q == 'd') || (*q == 'D')) 
+    value *= pow (10.0, atof (q + 1));
+
+  if (!strcmp (mode, "%d"))  { *va_arg (argp, int *)       = value; return (TRUE); }
+  if (!strcmp (mode, "%u"))  { *va_arg (argp, unsigned *)  = value; return (TRUE); }
+  if (!strcmp (mode, "%ld")) { *va_arg (argp, long *)      = value; return (TRUE); }
+  if (!strcmp (mode, "%hd")) { *va_arg (argp, short *)     = value; return (TRUE); }
+  if (!strcmp (mode, "%f"))  { *va_arg (argp, float *)     = value; return (TRUE); }
+  if (!strcmp (mode, "%lf")) { *va_arg (argp, double *)    = value; return (TRUE); }
+
+  /* no valid mode found */
+  return (FALSE);
+}
+
 /* if the variable argument stuff breaks on another system, look 
 at F_modify.c as it has the old code */
Index: branches/eam_branch_20081230/Ohana/src/libfits/include/gfitsio.h
===================================================================
--- branches/ipp-magic-v0/Ohana/src/libfits/include/gfitsio.h	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/libfits/include/gfitsio.h	(revision 21072)
@@ -133,5 +133,11 @@
 int 	gfits_primary_to_extended      PROTO((Header *header, char *exttype, char *comment));
 int 	gfits_modify_extended          PROTO((Header *header, char *exttype, char *comment));
-
+char   *gfits_keyword_start 	       PROTO((char *line));
+char   *gfits_keyword_end   	       PROTO((char *line));
+char   *gfits_hierarch_keyword_start   PROTO((char *line, char *field));
+char   *gfits_hierarch_keyword_end     PROTO((char *line, char *field));
+void    gfits_pad_ending               PROTO((char *line, char value, int Nbyte));
+int     gfits_vscan_hierarch           PROTO((Header *header, char *field, char *mode, int N, va_list argp));
+char   *gfits_header_hierarch_field    PROTO((Header *header, char *field, int N));
 
 /******************************* Matrix functions *************/
Index: branches/eam_branch_20081230/Ohana/src/libkapa/include/kapa.h
===================================================================
--- branches/ipp-magic-v0/Ohana/src/libkapa/include/kapa.h	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/libkapa/include/kapa.h	(revision 21072)
@@ -155,4 +155,5 @@
 int KiiResize (int fd, int Nx, int Ny);
 int KiiCenter (int fd, double x, double y, int zoom);
+int KiiParity (int fd, int xflip, int yflip);
 int KapaBox (int fd, Graphdata *graphdata);
 int KapaClearCurrentPlot (int fd);
Index: branches/eam_branch_20081230/Ohana/src/libkapa/src/KapaWindow.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/libkapa/src/KapaWindow.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/libkapa/src/KapaWindow.c	(revision 21072)
@@ -5,4 +5,13 @@
   KiiSendCommand (fd, 4, "CENT");
   KiiSendMessage (fd, "%8.3f %8.3f %8d ", x, y, zoom);
+  KiiWaitAnswer (fd, "DONE");
+  
+  return (TRUE);
+}
+
+int KiiParity (int fd, int xflip, int yflip) {
+
+  KiiSendCommand (fd, 4, "PARI");
+  KiiSendMessage (fd, "%2d %2d ", xflip, yflip);
   KiiWaitAnswer (fd, "DONE");
   
Index: branches/eam_branch_20081230/Ohana/src/opihi/cmd.astro/Makefile
===================================================================
--- branches/ipp-magic-v0/Ohana/src/opihi/cmd.astro/Makefile	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/opihi/cmd.astro/Makefile	(revision 21072)
@@ -30,4 +30,5 @@
 $(SRC)/flux.$(ARCH).o		   \
 $(SRC)/fixwrap.$(ARCH).o	   \
+$(SRC)/fixcols.$(ARCH).o	   \
 $(SRC)/gauss.$(ARCH).o		   \
 $(SRC)/getvel.$(ARCH).o	   \
Index: branches/eam_branch_20081230/Ohana/src/opihi/cmd.astro/init.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/opihi/cmd.astro/init.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/opihi/cmd.astro/init.c	(revision 21072)
@@ -13,4 +13,6 @@
 int flux                    PROTO((int, char **));
 int fixwrap                 PROTO((int, char **));
+int fixcols                 PROTO((int, char **));
+int fixrows                 PROTO((int, char **));
 int gauss                   PROTO((int, char **));
 int gaussfit                PROTO((int, char **));
@@ -50,4 +52,6 @@
   {1, "flux",        flux,         "flux in a convex contour"},
   {1, "fixwrap",     fixwrap,      "fix megacam over-wrapped pixels"},
+  {1, "fixcols",     fixcols,      "fix bad columns by comparing with others"},
+  {1, "fixrows",     fixrows,      "fix bad rows by comparing with others"},
   {1, "gauss",       gauss,        "get statistics on a star, assuming gaussian profile"},
   {1, "getvel",      getvel,       "rotcurve to velocities"},
Index: branches/eam_branch_20081230/Ohana/src/opihi/cmd.data/center.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/opihi/cmd.data/center.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/opihi/cmd.data/center.c	(revision 21072)
@@ -30,2 +30,29 @@
   return (TRUE);
 }
+
+int parity (int argc, char **argv) {
+  
+  int x, y;
+  int kapa, N;
+  char *name;
+  
+  name = NULL;
+  if ((N = get_argument (argc, argv, "-n"))) {
+    remove_argument (N, &argc, argv);
+    name = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
+
+  if (argc != 3) {
+    gprint (GP_ERR, "USAGE: parity x y\n");
+    return (FALSE);
+  }
+
+  x = atof (argv[1]);
+  y = atof (argv[2]);
+
+  KiiParity (kapa, x, y);
+  return (TRUE);
+}
Index: branches/eam_branch_20081230/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/opihi/cmd.data/init.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/opihi/cmd.data/init.c	(revision 21072)
@@ -7,4 +7,5 @@
 int book_command     PROTO((int, char **));
 int center           PROTO((int, char **));
+int parity           PROTO((int, char **));
 int circstats        PROTO((int, char **));
 int clear            PROTO((int, char **));
@@ -128,4 +129,5 @@
   {1, "book",         book_command,     "commands to manipulate book/page/word data"},
   {1, "center",       center,           "center image on coords"},
+  {1, "parity",       parity,           "set image parity"},
   {1, "circstats",    circstats,        "circular statistics"},
   {1, "clear",        clear,            "erase plot"},
Index: branches/eam_branch_20081230/Ohana/src/opihi/cmd.data/rebin.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/opihi/cmd.data/rebin.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/opihi/cmd.data/rebin.c	(revision 21072)
@@ -128,4 +128,6 @@
 	  for (i = 0; i < nx; i++, Vout++) {
 	    for (x = 0; x < n; x++, Vin++) {
+	      if (isnan(*Vin)) continue;
+	      if (isinf(*Vin)) continue;
 	      if (Ignore && (*Vin == IgnoreValue)) continue;
 	      *Vout += *Vin;
@@ -144,5 +146,5 @@
 	  if (Normalize) { Vn = Npix + j*nx; }
 	  for (i = 0; i < in[0].header.Naxis[0]; i++, Vin++) {
-	    if (Ignore && (*Vin == IgnoreValue)) { 
+	    if (isnan(*Vin) || isinf(*Vin) || (Ignore && (*Vin == IgnoreValue))) { 
 	      Vout += n; 
 	      if (Normalize) Vn += n; 
Index: branches/eam_branch_20081230/Ohana/src/opihi/dvo/imbox.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/opihi/dvo/imbox.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/opihi/dvo/imbox.c	(revision 21072)
@@ -49,15 +49,15 @@
     // XXX currently, image uses an unsigned short for NX,XY. this is rather restrictive
     // and needs to be at least checked.
-    haveNx = gfits_scan (&header, "NAXIS1",   "%d", 1, &Nx);
-    haveNy = gfits_scan (&header, "NAXIS2",   "%d", 1, &Ny);
-
-    if (!haveNx && !haveNy) {
-	haveNx = gfits_scan (&header, "IMNAXIS1",   "%d", 1, &Nx);
-	haveNy = gfits_scan (&header, "IMNAXIS2",   "%d", 1, &Ny);
-    }
+    haveNx = gfits_scan (&header, "IMNAXIS1",   "%d", 1, &Nx);
+    haveNy = gfits_scan (&header, "IMNAXIS2",   "%d", 1, &Ny);
 
     if (!haveNx && !haveNy) {
 	haveNx = gfits_scan (&header, "ZNAXIS1",   "%d", 1, &Nx);
 	haveNy = gfits_scan (&header, "ZNAXIS2",   "%d", 1, &Ny);
+    }
+
+    if (!haveNx && !haveNy) {
+	haveNx = gfits_scan (&header, "NAXIS1",   "%d", 1, &Nx);
+	haveNy = gfits_scan (&header, "NAXIS2",   "%d", 1, &Ny);
     }
 
@@ -69,5 +69,5 @@
     x[0] = 0;  y[0] = 0;
     x[1] = Nx; y[1] = 0;
-    x[2] = Ny; y[2] = Ny;
+    x[2] = Nx; y[2] = Ny;
     x[3] = 0;  y[3] = Ny;
     status = FALSE;
Index: branches/eam_branch_20081230/Ohana/src/tools/src/fits_insert.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/tools/src/fits_insert.c	(revision 21062)
+++ branches/eam_branch_20081230/Ohana/src/tools/src/fits_insert.c	(revision 21072)
@@ -46,5 +46,5 @@
 
   if (argc != 3) {
-    fprintf (stderr, "USAGE: gfits_insert (image.fits) (header.hdx) [-X N] [-comment N line] [-delete N]\n");
+    fprintf (stderr, "USAGE: fits_insert (image.fits) (header.hdx) [-X N] [-comment N line] [-delete Nstart Nstop]\n");
     exit (2);
   }
Index: branches/eam_branch_20081230/doc/psphot/sdss.summary.txt
===================================================================
--- branches/eam_branch_20081230/doc/psphot/sdss.summary.txt	(revision 21072)
+++ branches/eam_branch_20081230/doc/psphot/sdss.summary.txt	(revision 21072)
@@ -0,0 +1,43 @@
+
+The Legacy survey is restricted
+to high latitudes, and \photo\  is adequate everywhere
+there.  But at lower latitudes, when the density of stars brighter
+than $r=21$ grows above 5000 deg$^{-2}$, the pipeline
+is known to fail, due to a combination of being unable to find
+sufficiently isolated stars to measure an accurate PSF and limitations
+of the deblender.   Many of the SEGUE scans probe these low
+latitudes (Figure~\ref{fig:skydist}), and we therefore adapted an
+alternative stellar photometry code developed by the Pan-STARRS team
+(Kaiser 2002) to be used for these runs.
+
+Magnier (2006) has developed this code, PSPhot, to be used for the
+Pan-STARRS project; like, e.g., DOPHOT {\bf ref}, it begins with the
+assumption that every object is unresolved, and therefore does a
+better job in crowded stellar regions.  It uses an analytical model as
+to describe the basic PSF shape, with parameters which may vary across
+the field of the image to follow the PSF variations.  The software may
+use any of several functions to describe the radial profile (Gaussian;
+polynomial Gaussian approximation; and other power-law models), all of
+which are implemented as a 2D model using an elliptical contour.  The
+{\bf ??} model was used for the SEGUE scans.
+
+In addition to the analytical model, PSPhot uses a pixel-based
+representation of the residuals between the PSF objects and the
+analytical model, with the residual pixel contribution also varying
+across the field of the image.
+
+Candidate PSF stars are selected from the collection of bright objects
+in the frame by examining the distribution of the second moments of
+the objects and searching for a tight clump representing the PSF.  The
+candidate stars are fitted independently to the PSF model function,
+with all of the parameters fitted.  Poorly fitting objects are then
+excluded, and the fit parameters for the remaining objects are used to
+constrain the 2D variations in the PSF model.
+
+Note that unlike \photo, the code processes each frame separately
+(without any requirement of continuity of PSF estimation across frame
+boundaries), and each filter separately (without any requirement that
+the list of objects between the separate filters agree).  The pipeline
+outputs positions and PSF magnitudes (and errors) for each detected
+object.  The resulting photometry is then matched between filters
+using a $1^{\prime\prime}$ {\bf ??} radius.
Index: branches/eam_branch_20081230/ippTasks/simtest.pro
===================================================================
--- branches/ipp-magic-v0/ippTasks/simtest.pro	(revision 21062)
+++ branches/eam_branch_20081230/ippTasks/simtest.pro	(revision 21072)
@@ -82,5 +82,5 @@
       file TESS/Images.dat found
       if ($found == 0)
-        exec skycells 8 -scale 0.2 -D CATDIR TESS
+        exec skycells -mode square -level 8 -scale 0.2 -D CATDIR TESS
       end
     end
Index: branches/eam_branch_20081230/ippconfig/cfh12k/camera.config
===================================================================
--- branches/ipp-magic-v0/ippconfig/cfh12k/camera.config	(revision 21062)
+++ branches/eam_branch_20081230/ippconfig/cfh12k/camera.config	(revision 21072)
@@ -3,5 +3,5 @@
 # File formats that we know about
 FORMATS		METADATA
-#	EARLY	STR	cfh12k/format_mef_early.config
+	EARLY	STR	cfh12k/format_mef_early.config
 #	ESPLIT	STR	cfh12k/format_split_early.config
 	MEF	STR	cfh12k/format_mef.config
@@ -34,4 +34,6 @@
    	              		
    Z       MULTI
+   Z       STR    Z''           	
+   Z       STR    Z''''           	
    Z       STR    Z           	
    Z       STR    Zp           
Index: branches/eam_branch_20081230/ippconfig/cfh12k/format_mef.config
===================================================================
--- branches/ipp-magic-v0/ippconfig/cfh12k/format_mef.config	(revision 21062)
+++ branches/eam_branch_20081230/ippconfig/cfh12k/format_mef.config	(revision 21072)
@@ -114,10 +114,10 @@
 	CHIP.Y0.DEPEND		STR	CHIP.NAME
 	CHIP.Y0		METADATA
-		ccd00	S32	0
-		ccd01	S32	0
-		ccd02	S32	0
-		ccd03	S32	0
-		ccd04	S32	0
-		ccd05	S32	0
+		ccd00	S32	8192
+		ccd01	S32	8192
+		ccd02	S32	8192
+		ccd03	S32	8192
+		ccd04	S32	8192
+		ccd05	S32	8192
 		ccd06	S32	0
 		ccd07	S32	0
Index: branches/eam_branch_20081230/ippconfig/cfh12k/psastro.config
===================================================================
--- branches/ipp-magic-v0/ippconfig/cfh12k/psastro.config	(revision 21062)
+++ branches/eam_branch_20081230/ippconfig/cfh12k/psastro.config	(revision 21072)
@@ -8,38 +8,50 @@
 # max grid offset in FP units (microns)
 # use plate-scale to make this in pixels?
-PSASTRO.GRID.OFFSET    F32   2500.0
+PSASTRO.GRID.OFFSET    F32    1500.0
+# PSASTRO.GRID.OFFSET    F32    12500.0
 PSASTRO.GRID.SCALE     F32     50.0
 
+PSASTRO.GRID.NSTAR.MAX S32     800 # max stars accepted for fitting
+
+PSASTRO.GRID.MIN.ANGLE F32 -1.0 # start angle (degrees)
+PSASTRO.GRID.MAX.ANGLE F32 +1.0
+PSASTRO.GRID.DEL.ANGLE F32  0.5
+
+PSASTRO.GRID.MIN.SCALE F32  0.98
+PSASTRO.GRID.MAX.SCALE F32  1.021
+PSASTRO.GRID.DEL.SCALE F32  0.02
+
+PSASTRO.GRID.MIN.SIGMA F32  5.0
+
 # extra field for ref stars:
-PSASTRO.FIELD.PADDING  F32 1.0
+# PSASTRO.FIELD.PADDING  F32 6.0
+PSASTRO.FIELD.PADDING  F32 0.25
 
-# these tweak are in FP units (pixels, currently)
-PSASTRO.TWEAK.SCALE     F32      1
-PSASTRO.TWEAK.RANGE     F32     75
-PSASTRO.TWEAK.SMOOTH    F32      2
-PSASTRO.TWEAK.NSIGMA    F32      3
+# allow saturated stars if needed
+PSASTRO.MIN.INST.MAG.RAW       F32      -17.0  # min instrumental magnitude for stars accepted for fitting
+PSASTRO.MAX.INST.MAG.RAW       F32      -10.0  # max instrumental magnitude for stars accepted for fitting
+PSASTRO.IGNORE                 STR    CRLIMIT,SATURATED,DEFECT,BLEND,FAIL
 
 # single-chip radius match in pixels
-PSASTRO.MATCH.FIT.NITER S32   3
-PSASTRO.MATCH.RADIUS    F32   12.0
-PSASTRO.MATCH.RADIUS.N0 F32   15.0
-PSASTRO.MATCH.RADIUS.N1 F32   10.0
-PSASTRO.MATCH.RADIUS.N2 F32    5.0
+PSASTRO.MATCH.RADIUS.N0 F32   20.0
+PSASTRO.MATCH.RADIUS.N1 F32   15.0
+PSASTRO.MATCH.RADIUS.N2 F32   10.0
+PSASTRO.MATCH.RADIUS.N3 F32   10.0
+PSASTRO.MATCH.RADIUS.N4 F32   10.0
+PSASTRO.MATCH.FIT.NITER S32   5
 
 PSASTRO.MAX.NRAW       S32      1500   # max stars accepted for fitting (0 for all)
 PSASTRO.MAX.NREF       S32      1500   # max stars accepted for fitting (0 for all)
 
-PSASTRO.MIN.INST.MAG.RAW       F32      -17.0  # min instrumental magnitude for stars accepted for fitting
-PSASTRO.MAX.INST.MAG.RAW       F32       -8.0  # max instrumental magnitude for stars accepted for fitting
-
 PSASTRO.MAX.ERROR      F32      10.0 # max error in pixels
 PSASTRO.MIN.NSTAR      S32      10   # min fitted stars in solution
 
-PSASTRO.MOSAIC.MODE           BOOL     FALSE
+PSASTRO.MOSAIC.MODE           BOOL     TRUE
 
-PSASTRO.MOSAIC.MAX.ERROR.N0 F32    1.50 # max allow error for valid solution (arcsec)
-PSASTRO.MOSAIC.MAX.ERROR.N1 F32    1.50 # max allow error for valid solution (arcsec)
-PSASTRO.MOSAIC.MAX.ERROR.N2 F32    0.90 # max allow error for valid solution (arcsec)
-PSASTRO.MOSAIC.MAX.ERROR.N3 F32    0.90 # max allow error for valid solution (arcsec)
+PSASTRO.MOSAIC.MAX.ERROR.N0 F32    0.00 # max allow error for valid solution (arcsec)
+PSASTRO.MOSAIC.MAX.ERROR.N1 F32    0.00 # max allow error for valid solution (arcsec)
+PSASTRO.MOSAIC.MAX.ERROR.N2 F32    0.00 # max allow error for valid solution (arcsec)
+PSASTRO.MOSAIC.MAX.ERROR.N3 F32    0.00 # max allow error for valid solution (arcsec)
+PSASTRO.MOSAIC.MAX.ERROR.N4 F32    1.00 # max allow error for valid solution (arcsec)
 
 PSASTRO.MOSAIC.RADIUS.N0    F32   10.0 # initial match (after chip astrom)
@@ -47,9 +59,11 @@
 PSASTRO.MOSAIC.RADIUS.N2    F32    8.0 # do not refine the match
 PSASTRO.MOSAIC.RADIUS.N3    F32    4.0 # 
+PSASTRO.MOSAIC.RADIUS.N4    F32    4.0 # 
 
 PSASTRO.MOSAIC.CHIP.ORDER.N0  S32      0 # fit order (-1 means use default)
-PSASTRO.MOSAIC.CHIP.ORDER.N1  S32      1 # fit order (-1 means use default)
-PSASTRO.MOSAIC.CHIP.ORDER.N2  S32      1 # fit order (-1 means use default)
-PSASTRO.MOSAIC.CHIP.ORDER.N3  S32      3 # fit order (-1 means use default)
+PSASTRO.MOSAIC.CHIP.ORDER.N1  S32      0 # fit order (-1 means use default)
+PSASTRO.MOSAIC.CHIP.ORDER.N2  S32      0 # fit order (-1 means use default)
+PSASTRO.MOSAIC.CHIP.ORDER.N3  S32      1 # fit order (-1 means use default)
+PSASTRO.MOSAIC.CHIP.ORDER.N4  S32      3 # fit order (-1 means use default)
 
 PSASTRO.MOSAIC.GRADIENT.NX    S32     2   # number of x-dir cells per chip
@@ -74,6 +88,33 @@
 END
 
-PSASTRO.GRID.MIN.SCALE F32  0.98
-PSASTRO.GRID.MAX.SCALE F32  1.04
-PSASTRO.GRID.DEL.SCALE F32  0.02
-PSASTRO.GRID.MIN.SIGMA F32  5.0
+PSASTRO.CATDIR              STR      SYNTH.GRIZY
+DVO.GETSTAR.MAX.RHO         F32      3000.0
+DVO.GETSTAR.MIN.MAG         F32      12.0
+DVO.GETSTAR.MIN.MAG.INST    F32     -15.0
+
+PHOTCODE.DATA MULTI
+PHOTCODE.DATA METADATA
+  FILTER   STR B
+  ZEROPT   F32 26.00
+  PHOTCODE STR g
+END
+PHOTCODE.DATA METADATA
+  FILTER   STR V
+  ZEROPT   F32 26.15
+  PHOTCODE STR g
+END
+PHOTCODE.DATA METADATA
+  FILTER   STR R
+  ZEROPT   F32 26.19
+  PHOTCODE STR r
+END
+PHOTCODE.DATA METADATA
+  FILTER   STR I
+  ZEROPT   F32 26.14
+  PHOTCODE STR i
+END
+PHOTCODE.DATA METADATA
+  FILTER   STR Z
+  ZEROPT   F32 25.83
+  PHOTCODE STR z
+END
Index: branches/eam_branch_20081230/ippconfig/gpc1/ppImage.config
===================================================================
--- branches/ipp-magic-v0/ippconfig/gpc1/ppImage.config	(revision 21062)
+++ branches/eam_branch_20081230/ippconfig/gpc1/ppImage.config	(revision 21072)
@@ -23,9 +23,9 @@
     FILTER  STR FPA.FILTERID
   END
-  FLAT_CORRECTION METADATA
-    FILTER   STR FPA.FILTER
+  FLATCORR METADATA
+    FILTER   STR FPA.FILTERID
   END
   FRINGE METADATA
-    FILTER   STR FPA.FILTER
+    FILTER   STR FPA.FILTERID
   END
   SHUTTER METADATA
@@ -300,6 +300,9 @@
       FILTER  STR FPA.FILTERID
     END
+    FLATCORR METADATA
+      FILTER  STR FPA.FILTERID
+    END
     FRINGE METADATA
-      FILTER   STR FPA.FILTER
+      FILTER   STR FPA.FILTERID
     END
     SHUTTER METADATA
@@ -344,5 +347,5 @@
     END
     FRINGE METADATA
-      FILTER   STR FPA.FILTER
+      FILTER   STR FPA.FILTERID
     END
     SHUTTER METADATA
Index: branches/eam_branch_20081230/ippconfig/gpc1/psphot.config
===================================================================
--- branches/ipp-magic-v0/ippconfig/gpc1/psphot.config	(revision 21062)
+++ branches/eam_branch_20081230/ippconfig/gpc1/psphot.config	(revision 21072)
@@ -39,6 +39,6 @@
 # PSF.TREND.MODE                      STR   POLY_CHEB
 # PSF.TREND.MODE                      STR   POLY_ORD
-PSF.TREND.NX                        S32   3
-PSF.TREND.NY                        S32   3
+PSF.TREND.NX                        S32   1
+PSF.TREND.NY                        S32   1
 
 MOMENTS_SN_MIN      F32   30.0
@@ -52,5 +52,5 @@
 PSF_MAX_CHI          F32  50.0		 # reject objects worse that this
 
-APTREND.ORDER.MAX    S32  3
+APTREND.ORDER.MAX    S32  1
 
 MEASURE.APTREND      BOOL TRUE           ### XXX for now, skip this (too many errors)
Index: branches/eam_branch_20081230/ippconfig/megacam/camera.config
===================================================================
--- branches/ipp-magic-v0/ippconfig/megacam/camera.config	(revision 21062)
+++ branches/eam_branch_20081230/ippconfig/megacam/camera.config	(revision 21072)
@@ -56,4 +56,5 @@
    r        STR   r.MP9601     
    i        STR   i.MP9701     
+   i        STR   i.MP9702     
    z        MULTI
    z        STR   z.MP9801     
Index: branches/eam_branch_20081230/ippconfig/megacam/psastro.config
===================================================================
--- branches/ipp-magic-v0/ippconfig/megacam/psastro.config	(revision 21062)
+++ branches/eam_branch_20081230/ippconfig/megacam/psastro.config	(revision 21072)
@@ -12,5 +12,14 @@
 
 # extra field for ref stars:
-PSASTRO.FIELD.PADDING  F32 1.0
+PSASTRO.FIELD.PADDING  F32 0.25
+
+# pmAstromGridMatch parameters
+PSASTRO.GRID.MIN.ANGLE F32 -1.0 # start angle (degrees)
+PSASTRO.GRID.MAX.ANGLE F32 +1.0
+PSASTRO.GRID.DEL.ANGLE F32  1.0
+
+PSASTRO.GRID.MIN.SCALE F32  0.96
+PSASTRO.GRID.MAX.SCALE F32  1.00
+PSASTRO.GRID.DEL.SCALE F32  0.02
 
 # these tweak are in FP units (pixels, currently)
@@ -30,5 +39,5 @@
 PSASTRO.MAX.NREF       S32      1500   # max stars accepted for fitting (0 for all)
 
-PSASTRO.MIN.INST.MAG.RAW       F32      -15.5  # min instrumental magnitude for stars accepted for fitting
+PSASTRO.MIN.INST.MAG.RAW       F32      -17.0  # min instrumental magnitude for stars accepted for fitting
 PSASTRO.MAX.INST.MAG.RAW       F32       -8.0  # max instrumental magnitude for stars accepted for fitting
 
@@ -36,5 +45,5 @@
 PSASTRO.MIN.NSTAR      S32      10   # min fitted stars in solution
 
-PSASTRO.MOSAIC.MODE           BOOL     FALSE
+PSASTRO.MOSAIC.MODE           BOOL     TRUE
 
 PSASTRO.MOSAIC.MAX.ERROR.N0 F32    1.50 # max allow error for valid solution (arcsec)
@@ -42,4 +51,5 @@
 PSASTRO.MOSAIC.MAX.ERROR.N2 F32    0.90 # max allow error for valid solution (arcsec)
 PSASTRO.MOSAIC.MAX.ERROR.N3 F32    0.90 # max allow error for valid solution (arcsec)
+PSASTRO.MOSAIC.MAX.ERROR.N4 F32    0.90 # max allow error for valid solution (arcsec)
 
 PSASTRO.MOSAIC.RADIUS.N0    F32    10.0
@@ -47,12 +57,14 @@
 PSASTRO.MOSAIC.RADIUS.N2    F32     8.0 # do not refine the match
 PSASTRO.MOSAIC.RADIUS.N3    F32     4.0 
+PSASTRO.MOSAIC.RADIUS.N4    F32     4.0 
 
 PSASTRO.MOSAIC.CHIP.ORDER.N0  S32      0 # fit order (-1 means use default)
-PSASTRO.MOSAIC.CHIP.ORDER.N1  S32      1 # fit order (-1 means use default)
-PSASTRO.MOSAIC.CHIP.ORDER.N2  S32      1 # fit order (-1 means use default)
-PSASTRO.MOSAIC.CHIP.ORDER.N3  S32      3 # fit order (-1 means use default)
+PSASTRO.MOSAIC.CHIP.ORDER.N1  S32      0 # fit order (-1 means use default)
+PSASTRO.MOSAIC.CHIP.ORDER.N2  S32      0 # fit order (-1 means use default)
+PSASTRO.MOSAIC.CHIP.ORDER.N3  S32      1 # fit order (-1 means use default)
+PSASTRO.MOSAIC.CHIP.ORDER.N4  S32      3 # fit order (-1 means use default)
 
 PSASTRO.MOSAIC.GRADIENT.NX    S32      2   # number of x-dir cells per chip
-PSASTRO.MOSAIC.GRADIENT.NY    S32      4   # number of y-dir cells per chip
+PSASTRO.MOSAIC.GRADIENT.NY    S32      5   # number of y-dir cells per chip
 
 # use this recipe to set a tight constraint
@@ -73,2 +85,35 @@
   PSASTRO.MOSAIC.CHIP.ORDER.N3  S32      1 # fit order (-1 means use default)
 END
+
+PSASTRO.CATDIR              STR      SYNTH.GRIZY
+DVO.GETSTAR.MAX.RHO         F32      3000.0
+DVO.GETSTAR.MIN.MAG         F32      12.0
+DVO.GETSTAR.MIN.MAG.INST    F32     -15.0
+
+# we match the FILTER.ID (corresponding to the FILTER, see camera.config) to the dvo db PHOTCODE
+PHOTCODE.DATA MULTI
+PHOTCODE.DATA METADATA
+  FILTER   STR u
+  ZEROPT   F32 25.28
+  PHOTCODE STR g
+END
+PHOTCODE.DATA METADATA
+  FILTER   STR g
+  ZEROPT   F32 26.46
+  PHOTCODE STR g
+END
+PHOTCODE.DATA METADATA
+  FILTER   STR r
+  ZEROPT   F32 25.98
+  PHOTCODE STR r
+END
+PHOTCODE.DATA METADATA
+  FILTER   STR i
+  ZEROPT   F32 25.74
+  PHOTCODE STR i
+END
+PHOTCODE.DATA METADATA
+  FILTER   STR z
+  ZEROPT   F32 24.80
+  PHOTCODE STR z
+END
Index: branches/eam_branch_20081230/ippconfig/megacam/psphot.config
===================================================================
--- branches/ipp-magic-v0/ippconfig/megacam/psphot.config	(revision 21062)
+++ branches/eam_branch_20081230/ippconfig/megacam/psphot.config	(revision 21072)
@@ -31,7 +31,7 @@
 PSF_MAX_CHI          F32  50.0		 # reject objects worse that this
 
-#PSF.TREND.MODE STR MAP
-#PSF.TREND.NX   S32 1
-#PSF.TREND.NY   S32 1
+PSF.TREND.MODE STR MAP
+PSF.TREND.NX   S32 1
+PSF.TREND.NY   S32 1
 
 DIAGNOSTIC.PLOTS		METADATA
Index: branches/eam_branch_20081230/ppArith/.cvsignore
===================================================================
--- branches/ipp-magic-v0/ppArith/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/ppArith/.cvsignore	(revision 21072)
@@ -16,2 +16,3 @@
 config.sub
 test
+Doxyfile
Index: branches/eam_branch_20081230/ppConfigDump/.cvsignore
===================================================================
--- branches/ipp-magic-v0/ppConfigDump/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/ppConfigDump/.cvsignore	(revision 21072)
@@ -14,2 +14,3 @@
 missing
 libtool
+Doxyfile
Index: branches/eam_branch_20081230/ppImage/.cvsignore
===================================================================
--- branches/ipp-magic-v0/ppImage/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/ppImage/.cvsignore	(revision 21072)
@@ -16,2 +16,3 @@
 ltmain.sh
 libtool
+Doxyfile
Index: branches/eam_branch_20081230/ppMerge/.cvsignore
===================================================================
--- branches/ipp-magic-v0/ppMerge/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/ppMerge/.cvsignore	(revision 21072)
@@ -15,2 +15,3 @@
 libtool
 ltmain.sh
+Doxyfile
Index: branches/eam_branch_20081230/ppStack/.cvsignore
===================================================================
--- branches/ipp-magic-v0/ppStack/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/ppStack/.cvsignore	(revision 21072)
@@ -16,2 +16,3 @@
 test
 autom4te.cache
+Doxyfile
Index: branches/eam_branch_20081230/ppSub/.cvsignore
===================================================================
--- branches/ipp-magic-v0/ppSub/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/ppSub/.cvsignore	(revision 21072)
@@ -16,2 +16,3 @@
 config.sub
 test
+Doxyfile
Index: branches/eam_branch_20081230/psastro/.cvsignore
===================================================================
--- branches/ipp-magic-v0/psastro/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/psastro/.cvsignore	(revision 21072)
@@ -17,2 +17,3 @@
 config.sub
 psastro.pc
+Doxyfile
Index: branches/eam_branch_20081230/psphot/.cvsignore
===================================================================
--- branches/ipp-magic-v0/psphot/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/psphot/.cvsignore	(revision 21072)
@@ -18,2 +18,3 @@
 psphot.pc
 psphot-config
+Doxyfile
Index: branches/eam_branch_20081230/pswarp/.cvsignore
===================================================================
--- branches/ipp-magic-v0/pswarp/.cvsignore	(revision 21062)
+++ branches/eam_branch_20081230/pswarp/.cvsignore	(revision 21072)
@@ -17,2 +17,3 @@
 config.sub
 pswarp.pc
+Doxyfile
