Index: /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/load_zpt_table.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/load_zpt_table.c	(revision 33179)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/load_zpt_table.c	(revision 33180)
@@ -53,5 +53,5 @@
 ZptTable *load_zpt_ubercal(char *filename, int *nzpts, FlatCorrectionTable *flatcorrTable) {
 
-  int i, nfilter, nseason, ix, iy, ixc, iyc, Ncol, seq;
+  int i, nfilter, nseason, ix, iy, ixc, iyc, Ncol;
   off_t Nrow;
   char type[16], filter[80];
@@ -101,43 +101,58 @@
   // for current testing, make fake smfs that correspond to specific chips, filter, and mjd ranges?
 
-  if (!gfits_scan (&header, "NSEASON", "%d", 1, &NSEASON)) { 
-    fprintf (stderr, "cannot find NSEASON in header of %s\n", filename);
-    fclose (f);
-    return NULL;
-  }
-  if (!gfits_scan (&header, "NFILTER", "%d", 1, &NFILTER)) { 
-    fprintf (stderr, "cannot find NFILTER in header of %s\n", filename);
-    fclose (f);
-    return NULL;
-  }
-  if (!gfits_scan (&header, "NCHIP_X", "%d", 1, &NCHIP_X)) { 
-    fprintf (stderr, "cannot find NCHIP_X in header of %s\n", filename);
-    fclose (f);
-    return NULL;
-  }
-  if (!gfits_scan (&header, "NCHIP_Y", "%d", 1, &NCHIP_Y)) { 
-    fprintf (stderr, "cannot find NCHIP_Y in header of %s\n", filename);
-    fclose (f);
-    return NULL;
-  }
-  if (!gfits_scan (&header, "NCELL_X", "%d", 1, &NCELL_X)) { 
-    fprintf (stderr, "cannot find NCELL_X in header of %s\n", filename);
-    fclose (f);
-    return NULL;
-  }
-  if (!gfits_scan (&header, "NCELL_Y", "%d", 1, &NCELL_Y)) { 
-    fprintf (stderr, "cannot find NCELL_Y in header of %s\n", filename);
-    fclose (f);
-    return NULL;
-  }
-  if (!gfits_scan (&header, "CHIP_DX", "%d", 1, &CHIP_DX)) { 
-    fprintf (stderr, "cannot find CHIP_DX in header of %s\n", filename);
-    fclose (f);
-    return NULL;
-  }
-  if (!gfits_scan (&header, "CHIP_DY", "%d", 1, &CHIP_DY)) { 
-    fprintf (stderr, "cannot find CHIP_DY in header of %s\n", filename);
-    fclose (f);
-    return NULL;
+  // the simple files from Eddie have no internal metadata describing the corrections,
+  // so they must be manually encoded
+
+  // hard-wired values which describe the ubercal analysis
+  if (NO_METADATA) {
+    NFILTER = 5;
+    NSEASON = 4;
+    NCHIP_X = 8;
+    NCHIP_Y = 8;
+    NCELL_X = 2;
+    NCELL_Y = 2;
+    CHIP_DX = 4880;
+    CHIP_DY = 4864;
+  } else {
+    if (!gfits_scan (&header, "NSEASON", "%d", 1, &NSEASON)) { 
+      fprintf (stderr, "cannot find NSEASON in header of %s\n", filename);
+      fclose (f);
+      return NULL;
+    }
+    if (!gfits_scan (&header, "NFILTER", "%d", 1, &NFILTER)) { 
+      fprintf (stderr, "cannot find NFILTER in header of %s\n", filename);
+      fclose (f);
+      return NULL;
+    }
+    if (!gfits_scan (&header, "NCHIP_X", "%d", 1, &NCHIP_X)) { 
+      fprintf (stderr, "cannot find NCHIP_X in header of %s\n", filename);
+      fclose (f);
+      return NULL;
+    }
+    if (!gfits_scan (&header, "NCHIP_Y", "%d", 1, &NCHIP_Y)) { 
+      fprintf (stderr, "cannot find NCHIP_Y in header of %s\n", filename);
+      fclose (f);
+      return NULL;
+    }
+    if (!gfits_scan (&header, "NCELL_X", "%d", 1, &NCELL_X)) { 
+      fprintf (stderr, "cannot find NCELL_X in header of %s\n", filename);
+      fclose (f);
+      return NULL;
+    }
+    if (!gfits_scan (&header, "NCELL_Y", "%d", 1, &NCELL_Y)) { 
+      fprintf (stderr, "cannot find NCELL_Y in header of %s\n", filename);
+      fclose (f);
+      return NULL;
+    }
+    if (!gfits_scan (&header, "CHIP_DX", "%d", 1, &CHIP_DX)) { 
+      fprintf (stderr, "cannot find CHIP_DX in header of %s\n", filename);
+      fclose (f);
+      return NULL;
+    }
+    if (!gfits_scan (&header, "CHIP_DY", "%d", 1, &CHIP_DY)) { 
+      fprintf (stderr, "cannot find CHIP_DY in header of %s\n", filename);
+      fclose (f);
+      return NULL;
+    }
   }
 
@@ -146,22 +161,32 @@
   ALLOCATE (flatcorrTable->tstop,  e_time, NSEASON);
 
+  // ubercal hard-coded values
+  char filters_uc[5][3] = {"g", "r", "i", "z", "y"};
+  double tstart_uc[] = {55000.0, 55296.0, 55327.0, 55662.0};
+  double tstop_uc[]  = {55296.0, 55327.0, 55662.0, 60000.0};
+
+  double mjdstart, mjdstop;
   for (i = 0; i < NSEASON; i++) {
-    double mjdvalue;
-    char name[9];
-    snprintf (name, 9, "TS0_%04d", i);
-    if (!gfits_scan (&header, name, "%lf", 1, &mjdvalue)) { 
-      fprintf (stderr, "cannot find %s in header of %s\n", name, filename);
-      fclose (f);
-      return NULL;
-    }
-    flatcorrTable->tstart[i] = ohana_mjd_to_sec(mjdvalue);
-
-    snprintf (name, 9, "TS1_%04d", i);
-    if (!gfits_scan (&header, name, "%lf", 1, &mjdvalue)) { 
-      fprintf (stderr, "cannot find %s in header of %s\n", name, filename);
-      fclose (f);
-      return NULL;
-    }
-    flatcorrTable->tstop[i]  = ohana_mjd_to_sec(mjdvalue);
+    if (NO_METADATA) {
+      mjdstart = tstart_uc[i];
+      mjdstop  = tstop_uc[i];
+  } else {
+      char name[9];
+      snprintf (name, 9, "TS0_%04d", i);
+      if (!gfits_scan (&header, name, "%lf", 1, &mjdstart)) { 
+	fprintf (stderr, "cannot find %s in header of %s\n", name, filename);
+	fclose (f);
+	return NULL;
+      }
+
+      snprintf (name, 9, "TS1_%04d", i);
+      if (!gfits_scan (&header, name, "%lf", 1, &mjdstop)) { 
+	fprintf (stderr, "cannot find %s in header of %s\n", name, filename);
+	fclose (f);
+	return NULL;
+      }
+    }
+    flatcorrTable->tstart[i] = ohana_mjd_to_sec(mjdstart);
+    flatcorrTable->tstop[i]  = ohana_mjd_to_sec(mjdstop);
   }
 
@@ -216,15 +241,19 @@
     if (!gfits_load_header (f, &header)) return (NULL);
 
-    if (!gfits_scan (&header, "FILTER", "%s", 1, filter)) { 
-      fprintf (stderr, "cannot find FILTER in header of %s\n", filename);
-      fclose (f);
-      return NULL;
-    }
-
+    if (NO_METADATA) {
+      strcpy (filter, filters_uc[nfilter]);
+    } else {
+      if (!gfits_scan (&header, "FILTER", "%s", 1, filter)) { 
+	fprintf (stderr, "cannot find FILTER in header of %s\n", filename);
+	fclose (f);
+	return NULL;
+      }
+    }
+    
     // read the fits table bytes
     double *offset64 = NULL;
     float *offset32 = NULL;
     int use32 = TRUE;
-
+    
     if (!gfits_fread_matrix (f, &matrix, &header)) return (NULL);
     switch (header.bitpix) {
@@ -242,4 +271,11 @@
     }
 
+    // XXX the initial hacked-together table from Eddie is missing the last 2 elements.  they should be zero
+    if (NO_METADATA) {
+      matrix.Naxis[0] += 2;
+      REALLOCATE (offset64, double, matrix.Naxis[0]);
+      matrix.buffer = (char *) offset64;
+    }
+
     for (nseason = 0; nseason < NSEASON; nseason++) { // seasons
       for (iy = 0; iy < NCHIP_Y; iy++) { // y-chip
@@ -260,22 +296,39 @@
 	  flatcorrTable->image[Nimage].tstop  = flatcorrTable->tstop[nseason];
 	  
+	  int seq_full = -1; // sequence number within the full table (all filters concatenated together)
+	  int seq_filt = -1; // sequence number for just this filter
+
+	  // XXX we should have a NCHIP_X * NCHIP_Y array of x and y parity values
+
 	  // This enforces a 180 chip rotation for XY3n - XY7n & is only known to be valid for GPC1 (the XYnn names as well)
 	  for (iyc = 0; iyc < NCELL_Y; iyc++) {
 	    for (ixc = 0; ixc < NCELL_X; ixc++) {
-	      if (ix > 3) {
-		// XXX I think this is backwards : chips ix < 4 should be flipped
-		seq = nfilter*NSEASON*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + nseason*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + (iy*NCELL_Y + NCELL_Y - 1 - iyc)*NCHIP_X*NCELL_X + (ix*NCELL_X + NCELL_X - 1 - ixc);
+	      if (ix < 4) {
+		// chips ix < 4 should be flipped
+		seq_full = nfilter*NSEASON*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + nseason*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + (iy*NCELL_Y + NCELL_Y - 1 - iyc)*NCHIP_X*NCELL_X + (ix*NCELL_X + NCELL_X - 1 - ixc);
+		seq_filt =                                                   nseason*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + (iy*NCELL_Y + NCELL_Y - 1 - iyc)*NCHIP_X*NCELL_X + (ix*NCELL_X + NCELL_X - 1 - ixc);
+		// = .... + y_parity*iyc - (y_parity - 1)*(NCELL_Y - 1) / 2
 	      } else {
-		seq = nfilter*NSEASON*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + nseason*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + (iy*NCELL_Y + iyc)*NCHIP_X*NCELL_X + (ix*NCELL_X + ixc);
+		seq_full = nfilter*NSEASON*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + nseason*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + (iy*NCELL_Y + iyc)*NCHIP_X*NCELL_X + (ix*NCELL_X + ixc);
+		seq_filt =                                                   nseason*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + (iy*NCELL_Y + iyc)*NCHIP_X*NCELL_X + (ix*NCELL_X + ixc);
 	      }
-	      assert (!flatcorrTable->corr[seq].ID);
-	      flatcorrTable->corr[seq].x = ixc;
-	      flatcorrTable->corr[seq].y = iyc;
+	      assert (seq_full > -1);
+	      assert (seq_filt > -1);
+	      assert (seq_full < flatcorrTable->Ncorr);
+	      if (matrix.Naxes == 1) {
+		assert (seq_filt < matrix.Naxis[0]);
+	      } 
+	      if (matrix.Naxes == 2) {
+		assert (seq_filt < matrix.Naxis[0]*matrix.Naxis[1]);
+	      }
+	      assert (!flatcorrTable->corr[seq_full].ID);
+	      flatcorrTable->corr[seq_full].x = ixc;
+	      flatcorrTable->corr[seq_full].y = iyc;
 	      if (use32) {
-		flatcorrTable->corr[seq].offset = offset32[seq];
+		flatcorrTable->corr[seq_full].offset = offset32[seq_filt];
 	      } else {
-		flatcorrTable->corr[seq].offset = offset64[seq];
+		flatcorrTable->corr[seq_full].offset = offset64[seq_filt];
 	      }
-	      flatcorrTable->corr[seq].ID = corrID;
+	      flatcorrTable->corr[seq_full].ID = corrID;
 	    }
 	  }
@@ -295,162 +348,2 @@
   return zpts;
 }
-
-ZptTable *load_zpt_ubercal_nometadata (char *filename, int *nzpts, FlatCorrectionTable *flatcorrTable) {
-
-  int i, nfilter, nseason, ix, iy, ixc, iyc, Ncol, seq;
-  off_t Nrow;
-  char type[16];
-  int Nzpts, NZPTS;
-  ZptTable *zpts;
-  Header header;
-  Header theader;
-  Matrix matrix;
-  FTable ftable;
-
-  // hard-wired values which describe the ubercal analysis
-  int NFILTER = 5;
-  int NSEASON = 4;
-  int NCHIP_X = 8;
-  int NCHIP_Y = 8;
-  int NCELL_X = 2;
-  int NCELL_Y = 2;
-  int CHIP_DX = 4880;
-  int CHIP_DY = 4864;
-
-  flatcorrTable->Nseason = NSEASON;
-  ALLOCATE (flatcorrTable->tstart, e_time, NSEASON);
-  ALLOCATE (flatcorrTable->tstop,  e_time, NSEASON);
-
-  char filter[] = {'g', 'r', 'i', 'z', 'y'};
-
-  double tstart_mjd[] = {55000.0, 55296.0, 55327.0, 55662.0};
-  double tstop_mjd[]  = {55296.0, 55327.0, 55662.0, 60000.0};
-
-  for (i = 0; i < NSEASON; i++) {
-    flatcorrTable->tstart[i] = ohana_mjd_to_sec(tstart_mjd[i]);
-    flatcorrTable->tstop[i]  = ohana_mjd_to_sec(tstop_mjd[i]);
-  }
-
-  FILE *f = fopen (filename, "r");
-  if (!f) {
-    fprintf (stderr, "ERROR: cannot open zpt table file %s\n", filename);
-    exit (1);
-  }
-
-  /* load in PHU segment (ignore) */
-  if (!gfits_fread_header (f, &header)) {
-    if (VERBOSE) fprintf (stderr, "can't read Flat Correction header\n");
-    fclose (f);
-    return (NULL);
-  }
-  if (!gfits_fread_matrix (f, &matrix, &header)) {
-    if (VERBOSE) fprintf (stderr, "can't read Flat Correction matrix\n");
-    gfits_free_header (&header);
-    fclose (f);
-    return (NULL);
-  }
-
-  Nzpts = 0;
-  NZPTS = 0;
-  ALLOCATE (zpts, ZptTable, NZPTS);
-
-  // this function would be better if we read the list of filters, seasons, and the dimensions from the header
-  // for current testing, make fake smfs that correspond to specific chips, filter, and mjd ranges?
-
-  // we have 5 filters, and 4 flat-field correction sets for each
-  flatcorrTable->Ncorr = NFILTER*NSEASON*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y;
-  flatcorrTable->Nimage = NFILTER*NSEASON*NCHIP_X*NCHIP_Y;
-
-  ALLOCATE (flatcorrTable->corr, FlatCorrection, flatcorrTable->Ncorr);
-  ALLOCATE (flatcorrTable->image, FlatCorrectionImage, flatcorrTable->Nimage);
-  memset (flatcorrTable->corr, 0, flatcorrTable->Ncorr*sizeof(FlatCorrection));
-
-  int corrID = 1;
-  int Nimage = 0;
-  ftable.header = &theader;
-  for (nfilter = 0; nfilter < NFILTER; nfilter++) {
-    // load data for this header 
-    if (!gfits_load_header (f, &theader)) return (NULL);
-
-    // read the fits table bytes
-    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
-  
-    // skip over remaining bytes in data segment
-    fseeko (f, ftable.datasize - ftable.validsize, SEEK_CUR);
-
-    // need to create and assign to flat-field correction
-    double *mjd = gfits_get_bintable_column_data (&theader, &ftable, "mjd_obs", type, &Nrow, &Ncol);
-    assert (!strcmp(type, "double"));
-
-    double *zp = gfits_get_bintable_column_data (&theader, &ftable, "zp", type, &Nrow, &Ncol);
-    assert (!strcmp(type, "double"));
-      
-    // float *zperr = gfits_get_bintable_column_data (&theader, &ftable, "resid", type, &Nrow, &Ncol);
-    // assert (!strcmp(type, "float"));
-      
-    NZPTS += Nrow;
-    REALLOCATE (zpts, ZptTable, NZPTS);
-    for (i = 0; i < Nrow; i++) {
-      zpts[i+Nzpts].time = ohana_mjd_to_sec(mjd[i]);
-      zpts[i+Nzpts].zpt = zp[i];
-      // zpts[i+Nzpts].zpt_err = zperr[i];
-    }
-    Nzpts += Nrow;
-
-    // the image contains the flat-field corrections
-
-    // load data for this header 
-    if (!gfits_load_header (f, &header)) return (NULL);
-
-    // read the fits table bytes
-    if (!gfits_fread_matrix (f, &matrix, &header)) return (NULL);
-    assert (header.bitpix == -64); // hardwired as a double
-    double *offset = (double *) matrix.buffer;
-
-    for (nseason = 0; nseason < NSEASON; nseason++) { // seasons
-      for (iy = 0; iy < NCHIP_Y; iy++) { // y-chip
-	for (ix = 0; ix < NCHIP_X; ix++) { // x-chip
-
-	  // photcode name
-	  char photname[64];
-	  snprintf (photname, 64, "GPC1.%c.XY%d%d", filter[nfilter], ix, iy);
-	  // note that the XY00, XY07, etc, chips will have photcode values of 0
-
-	  flatcorrTable->image[Nimage].photcode = GetPhotcodeCodebyName(photname);
-	  flatcorrTable->image[Nimage].Nx = NCELL_X;
-	  flatcorrTable->image[Nimage].Ny = NCELL_Y;
-	  flatcorrTable->image[Nimage].ID = corrID;
-	  flatcorrTable->image[Nimage].DX = CHIP_DX;
-	  flatcorrTable->image[Nimage].DY = CHIP_DY;
-	  flatcorrTable->image[Nimage].tstart = flatcorrTable->tstart[nseason];
-	  flatcorrTable->image[Nimage].tstop  = flatcorrTable->tstop[nseason];
-	  
-	  for (iyc = 0; iyc < NCELL_Y; iyc++) {
-	    for (ixc = 0; ixc < NCELL_X; ixc++) {
-	      if (ix > 3) {
-		seq = nfilter*NSEASON*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + nseason*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + (iy*NCELL_Y + NCELL_Y - 1 - iyc)*NCHIP_X*NCELL_X + (ix*NCELL_X + NCELL_X - 1 - ixc);
-	      } else {
-		seq = nfilter*NSEASON*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + nseason*NCHIP_X*NCHIP_Y*NCELL_X*NCELL_Y + (iy*NCELL_Y + iyc)*NCHIP_X*NCELL_X + (ix*NCELL_X + ixc);
-	      }
-	      assert (!flatcorrTable->corr[seq].ID);
-	      flatcorrTable->corr[seq].x = ixc;
-	      flatcorrTable->corr[seq].y = iyc;
-	      flatcorrTable->corr[seq].offset = offset[seq];
-	      flatcorrTable->corr[seq].ID = corrID;
-	    }
-	  }
-	  corrID ++;
-	  Nimage ++;
-	}
-      }
-    }	      
-  }
-
-  /*** convert from corr,image format to offsets ***/
-  FlatCorrectionInternal (flatcorrTable);
-
-  fprintf (stderr, "loaded %d zero points\n", Nzpts);
-
-  *nzpts = Nzpts;
-  return zpts;
-}
Index: /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/setphot.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/setphot.c	(revision 33179)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/setphot.c	(revision 33180)
@@ -19,11 +19,5 @@
 
   if (UBERCAL) {
-    if (NO_METADATA) {
-      // the simple files from Eddie have no internal metadata describing the corrections,
-      // so they must be manually encoded
-      zpts = load_zpt_ubercal_nometadata (argv[1], &Nzpts, &flatcorrTable);
-    } else {
-      zpts = load_zpt_ubercal (argv[1], &Nzpts, &flatcorrTable);
-    }
+    zpts = load_zpt_ubercal (argv[1], &Nzpts, &flatcorrTable);
 
     char flatcorrfile[64];
