Index: trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- trunk/Ohana/src/relphot/include/relphot.h	(revision 41557)
+++ trunk/Ohana/src/relphot/include/relphot.h	(revision 41647)
@@ -5,11 +5,14 @@
 # include <pthread.h>
 
-/* # define GRID_V1 */
-# define GRID_V2
-# define NO_IMAGE -100
-
 # define ID_SECF_STACK_PRIMARY 0x00004000
 
 # define NBOOTSTRAP 100
+
+// MEAS_BAD is used in ImageOps.c, MosaicOps.c, TGroupOps.c to skip specific measurements.
+// ID_MEAS_AREA is raised for measurements outside the user-specified region of the chips
+// ID_MEAS_NOCAL is raised for measurements outside the valid time range or for inactive photcodes
+// Note: these bits are only raised on the temporary MeasureTiny elements and are not saved
+// to the database files.
+# define MEAS_BAD (ID_MEAS_AREA | ID_MEAS_NOCAL)
 
 # ifndef MAX_INT
@@ -53,9 +56,9 @@
 
 typedef enum {
-    MODE_NONE   = 0,
-    MODE_LOAD   = 1,
-    MODE_UPDATE = 2,
+    MODE_NONE           = 0,
+    MODE_LOAD           = 1,
+    MODE_UPDATE         = 2,
     MODE_UPDATE_OBJECTS = 3,
-    MODE_SYNTH_PHOT = 4,
+    MODE_SYNTH_PHOT     = 4,
 } ModeType;
 
@@ -70,4 +73,10 @@
   PS1_w = 6,
 } PS1filters;
+
+typedef enum {
+  GRID_MEAN = 0,
+  GRID_STDEV = 1,
+  GRID_NPTS = 2,
+} GridOutputMode;
 
 typedef struct {
@@ -99,5 +108,5 @@
   char skipCal;		      // if TRUE, this mosaic is incomplete and should not be calibrated
   char inTGroup;
-  Coords coords;
+  Coords coords;	      // this is only used to set the mosaic center for assign_images used by region hosts
 } Mosaic; 
 
@@ -140,4 +149,16 @@
   int      nCode;
 } TGTimes;
+
+typedef struct {
+  unsigned short photcode;
+  float **Mgrid; // grid of average corrections
+  float **dMgrid; // grid of stdev of corrections
+    int **nMgrid; // grid of number of stars for corrections
+  int Nx; // bin = int(Xchip * (Nx / NxChip))
+  int Ny; // bin = int(Ychip * (Ny / NyChip))
+  float dX; // bin = int(Xchip * dX), dX = Nx / NxChip
+  float dY; // bin = int(Ychip * dY), dY = Ny / NyChip
+  // NxChip, NyChip = 4900,4900 for now
+} GridCorrectionType;
 
 typedef enum {
@@ -366,4 +387,5 @@
 int    SKY_DEPTH;  /** XXX EAM : depth of catalog tables, fix usage */
 char  *SYNTH_ZERO_POINTS;
+char  *GRID_MEANFILE;
 
 // globals for parallel region operations
@@ -389,5 +411,7 @@
 double IMAGE_OFFSET;
 double NIGHT_SCATTER;
-double NIGHT_OFFSET;
+double NIGHT_CHISQ;
+double MOSAIC_SCATTER;
+double MOSAIC_CHISQ;
 double STAR_SCATTER;
 double STAR_CHISQ;
@@ -413,4 +437,10 @@
 int    MOSAIC_ZEROPT;
 int    TGROUP_ZEROPT;
+int    GRID_ZEROPT;
+
+int    GRID_BIN_HSC;
+int    GRID_BIN_CFH;
+int    GRID_BIN_GPC1;
+int    GRID_BIN_GPC2;
 
 int    FREEZE_IMAGES;
@@ -424,9 +454,10 @@
 int    MANUAL_ITERATION;
 int    NLOOP;
-int    NGRID;
 int    RESET;
 int    RESET_ZEROPTS;
+int    RESET_FLATCORR;
 int    REPAIR_WARPS;
 int    PRESERVE_PS1;
+int    REQUIRE_PSFFIT;
 int    UPDATE;
 int    SAVE_IMAGE_UPDATES;
@@ -436,13 +467,10 @@
 char   MOSAICNAME[256];
 char   STATMODE[32];
-int    STAR_BAD;
-int    MEAS_BAD;
 int    STAR_TOOFEW;
 int    GRID_TOOFEW;
 int    IMAGE_TOOFEW;
 double IMAGE_GOOD_FRACTION;
-int    IMAGE_BAD;
 int    CALIBRATE_STACKS_AND_WARPS;
-int    USE_GRID;
+
 int    KEEP_UBERCAL;
 char  *OUTROOT;
@@ -457,8 +485,4 @@
 char  *PhotcodeList;
 
-int    RELPHOT_GRID_X;
-int    RELPHOT_GRID_Y;
-int    RELPHOT_GRID_BINNING;
-
 int      *photseclist;
 int      Nphotcodes;
@@ -490,4 +514,7 @@
 char *UserCatalog;
 
+enum {GRID_ZPT_MODE_NONE, GRID_ZPT_MODE_ALL, };
+int GRID_ZPT_MODE;
+
 enum {TGROUP_ZPT_MODE_NONE, TGROUP_ZPT_MODE_GOOD_NIGHT, TGROUP_ZPT_MODE_ALL, };
 int TGROUP_ZPT_MODE;
@@ -504,12 +531,4 @@
 
 RelphotStages STAGES;
-
-# ifdef GRID_V1
-int setGridMeasure (off_t meas, int cat, double X, double Y);
-# endif
-
-# ifdef GRID_V2
-int setGridMeasure (off_t meas, int cat, double X, double Y, int ccdnum);
-# endif
 
 /*** relphot prototypes ***/
@@ -523,5 +542,5 @@
 int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog, int Ncat));
 void          clean_images        PROTO((void));
-void          clean_measures      PROTO((Catalog *catalog, int Ncatalog, int final, FlatCorrectionTable *flatcorr));
+void          clean_measures      PROTO((Catalog *catalog, int Ncatalog, int final));
 void          clean_mosaics       PROTO((void));
 void          clean_tgroups       PROTO((void));
@@ -549,5 +568,5 @@
 int SetSignals (void);
 
-void          freeGridBins        PROTO((int Ncatalog));
+void          freeGridBins        PROTO((void));
 void          freeImageBins       PROTO((int Ncatalog, int doImageList));
 void          freeMosaicBins      PROTO((int Ncatalog, int doMosaicList));
@@ -560,6 +579,7 @@
 
 float         getMcal             PROTO((off_t meas, int cat, dvoMagClassType class));
-float         getMflat            PROTO((off_t meas, int cat, FlatCorrectionTable *flatcorr, Catalog *catalog));
-float         getMgrid            PROTO((off_t meas, int cat));
+float         getMflat            PROTO((off_t meas, int cat, Catalog *catalog));
+float         getMgridTiny        PROTO((MeasureTiny *measure));
+float         getMgrid            PROTO((Measure *measure));
 float         getMmos             PROTO((off_t meas, int cat));
 float         getMgrp             PROTO((off_t meas, int cat, float airmass, float *dZpt));
@@ -570,12 +590,18 @@
 Image        *getimages           PROTO((off_t *N, off_t **LineNumber));
 ImageSubset  *getimages_subset    PROTO((off_t *N));
-void          global_stats        PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr, int nloop));
-void          initGrid            PROTO((int dX, int dY));
-void          initGridBins        PROTO((Catalog *catalog, int Ncatalog));
+void          global_stats        PROTO((Catalog *catalog, int Ncatalog, int nloop));
+void          initGrid            PROTO((void));
+void          setMflatFromGrid    PROTO((Catalog *catalog));
+void          initGridBins        PROTO((void));
+GridCorrectionType *getGridCorrByCode PROTO((int code));
+GridCorrectionType *getGridCorrNext   PROTO((int *Nlast));
+int           GridCorrectionSave  PROTO((void));
+int           GridCorrectionSaveFile  PROTO((char *filename, GridOutputMode mode));
+void          GridCorrectionLoad  PROTO((char *filename));
 void          initImageBins       PROTO((Catalog *catalog, int Ncatalog, int doImageList));
 void          initImagesSubset    PROTO((ImageSubset *input, off_t *line_number, off_t N));
 void          initImages          PROTO((Image *input, off_t *LineNumber, off_t N));
 void          initMosaicBins      PROTO((Catalog *catalog, int Ncatalog, int doMosaicList));
-void          initMosaicGrid      PROTO((Image *image, off_t Nimage));
+void          initMosaicMcal      PROTO((Image *image, off_t Nimage));
 void          initMosaics         PROTO((Image *subset, off_t Nsubset, Image *image, char *inSubset, off_t Nimage));
 void          initTGroups         PROTO((Image *subset, off_t Nsubset));
@@ -621,10 +647,9 @@
 void          plot_chisq          PROTO((Catalog *catalog, int Ncatalog));
 void          plot_defaults       PROTO((Graphdata *graphdata));
-void          plot_grid           PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
 void          plot_images         PROTO((void));
 void          plot_list           PROTO((Graphdata *graphdata, double *xlist, double *ylist, int N, char *label, char *format, ...) OHANA_FORMAT(printf, 6, 7) );
 void          plot_mosaic_fields  PROTO((Catalog *catalog));
 void          plot_mosaics        PROTO((void));
-void          plot_scatter        PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
+void          plot_scatter        PROTO((Catalog *catalog, int Ncatalog));
 void          plot_star_coords    PROTO((Catalog *catalog, int Ncatalog));
 void          plot_stars          PROTO((Catalog *catalog, int Ncatalog));
@@ -635,27 +660,29 @@
 int           open_graph          PROTO((int N));
 
-void          reload_catalogs     PROTO((SkyList *skylist, FlatCorrectionTable *flatcorr, int hostID, char *hostpath));
+void          reload_catalogs     PROTO((SkyList *skylist, int hostID, char *hostpath));
 int           reload_catalogs_parallel PROTO((SkyList *sky));
 int           reload_images       PROTO((FITS_DB *db));
 int           setExclusions       PROTO((Catalog *catalog, int Ncatalog, int verbose));
-void          setMgrid            PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
+void          setMgrid            PROTO((Catalog *catalog, int Ncatalog));
+void          resetMgrid          PROTO((void));
+
 void          setMcalFromMosaics  PROTO((void));
 void          setMcalFromTGroups  PROTO((void));
-int           setMcalOutput       PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
-
-void          setMcal             PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
-int           setMmos             PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
-int           setMgrp             PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
-
-int           setMrel             PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
-void          setMrelFinal        PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr, int simpleAverage));
-int           setMrelOutput       PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
-
-void          setMcalTest         PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
+int           setMcalOutput       PROTO((Catalog *catalog, int Ncatalog));
+
+void          setMcal             PROTO((Catalog *catalog));
+int           setMmos             PROTO((Catalog *catalog));
+int           setMgrp             PROTO((Catalog *catalog));
+
+int           setMrel             PROTO((Catalog *catalog, int Ncatalog));
+void          setMrelFinal        PROTO((Catalog *catalog, int simpleAverage));
+int           setMrelOutput       PROTO((Catalog *catalog, int Ncatalog));
+
+void          setMcalTest         PROTO((Catalog *catalog));
 
 int           setMave             PROTO((Catalog *catalog, int Ncatalog));
 void          set_ZP              PROTO((double ZERO));
 int           setrefcode          PROTO((Image *image, off_t Nimage)); 
-void          skip_measurements   PROTO((Catalog *catalog, int pass, FlatCorrectionTable *flatcorr));
+void          skip_measurements   PROTO((Catalog *catalog, int pass));
 void          sortA               PROTO((double *X, int N));
 void          sortB               PROTO((double *X, double *Y, int N));
@@ -663,9 +690,9 @@
 void          sortD               PROTO((double *X, double *Y, double *Z, int N));
 StatType      statsImageM         PROTO((Catalog *catalog));
-StatType      statsImageN         PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
+StatType      statsImageN         PROTO((Catalog *catalog));
 StatType      statsImageX         PROTO((Catalog *catalog));
 StatType      statsImagedM        PROTO((Catalog *catalog));
 StatType      statsMosaicM        PROTO((Catalog *catalog));
-StatType      statsMosaicN        PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
+StatType      statsMosaicN        PROTO((Catalog *catalog));
 StatType      statsMosaicX        PROTO((Catalog *catalog));
 StatType      statsMosaicdM       PROTO((Catalog *catalog));
@@ -674,5 +701,5 @@
 StatType      statsTGroupdM       PROTO((Catalog *catalog));
 
-StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog, int Nsec, int seccode, FlatCorrectionTable *flatcorr));
+StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog, int Nsec, int seccode));
 StatType      statsStarS          PROTO((Catalog *catalog, int Ncatalog, int Nsec));
 StatType      statsStarX          PROTO((Catalog *catalog, int Ncatalog, int Nsec));
@@ -720,8 +747,10 @@
 
 int print_measure_set_alt (Average *average, SecFilt *secfilt, Measure *measure);
-int setMrel_catalog_alt (Catalog *catalog, int Nc, int isSetMrelFinal, FlatCorrectionTable *flatcorr, SetMrelInfo *results, int Nsecfilt);
-int setMrelAverageExposure (Catalog *catalog, int cat, off_t ave, int Nsecfilt, int isSetMrelFinal, FlatCorrectionTable *flatcorr, SetMrelInfo *results);
-int setMrelAverageStack (Catalog *catalog, int cat, off_t ave, int Nsecfilt, FlatCorrectionTable *flatcorr);
-int setMrelAverageForcedWarp (Catalog *catalog, int cat, off_t ave, int Nsecfilt, FlatCorrectionTable *flatcorr, SetMrelInfo *results);
+
+// in setMrelCatalog.c:
+int setMrelCatalog (Catalog *catalog, int Nc, int isSetMrelFinal, SetMrelInfo *results, int Nsecfilt);
+int setMrelAverageExposure (Catalog *catalog, int cat, off_t ave, int Nsecfilt, int isSetMrelFinal, SetMrelInfo *results);
+int setMrelAverageStack (Catalog *catalog, int cat, off_t ave, int Nsecfilt);
+int setMrelAverageForcedWarp (Catalog *catalog, int cat, off_t ave, int Nsecfilt, SetMrelInfo *results);
 
 int setGlobalObjStats (Average *average, Measure *measure);
@@ -735,5 +764,5 @@
 StatDataSet *StatDataSetAlloc (int Nsecfilt, int Nmax);
 
-int init_synthetic_mags ();
+int init_synthetic_mags (void);
 int add_synthetic_mags (AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, off_t *Nmeasure, off_t *Nm);
 
@@ -796,5 +825,5 @@
 
 int SynthZeroPointsLoad (char *filename);
-SynthZeroPoints *SynthZeroPointsGet ();
+SynthZeroPoints *SynthZeroPointsGet (void);
 
 int relphot_synthphot (SkyList *sky, int hostID, char *hostpath);
@@ -815,18 +844,5 @@
 
 void put_astrom_table (AstromOffsetTable *myTable);
-void free_astrom_table ();
-
-int RepairWarpMeasures (Catalog *catalog);
-
-int FindWarpGroups (void);
-int GetWarpSeq (Image *image, int obstime, unsigned short photcode, double Rave, double Dave, float X, float Y);
-void FreeWarpGroups (void);
-
-myIndexType *myIndexInit ();
-int myIndexFree (myIndexType *myIndex);
-int myIndexUpdateLimits (myIndexType *myIndex, int value);
-int myIndexSetRange (myIndexType *myIndex);
-int myIndexSetEntry (myIndexType *myIndex, int value, int entry);
-int myIndexGetEntry (myIndexType *myIndex, int value);
+void free_astrom_table (void);
 
 uint64_t CreatePSPSObjectID(double ra, double dec);
@@ -834,11 +850,5 @@
 uint64_t CreatePSPSDetectionID(double tobs, int ccdid, int detID);
 
-int MakeStackIndex (void);
-void FreeStackGroups (void);
-int GetStackSeq (Image *image, double Rstk, double Dstk, unsigned short photcode, float X, float Y);
 void sort_by_ra (double *R, double *D, int *I, int *S, int N);
-
-void RepairWarpMeasuresOpenLogfile ();
-void RepairWarpMeasuresCloseLogfile ();
 
 void dump_tgroups (Catalog *catalog, int Npass);
@@ -849,5 +859,5 @@
 int GetZptIteration (void);
 
-void SetZeroPointModes (void);
+void SetZeroPointModes (Catalog *catalog, int Ncatalog);
 int UseStandardOLS (ZptFitModeType mode);
 int GetActivePhotcodeIndex (int photcode);
@@ -857,2 +867,10 @@
 
 int MagResidSave(char *filename, Catalog *catalog);
+
+void ResetAverageAndMeasure (Catalog *catalog);
+void ResetAverageObjects (Catalog *catalog);
+void ResetImages (Image *subset, off_t Nsubset);
+void ResetMeasureZeroPoints (MeasureTiny *measure, off_t Nmeasure, off_t Ncat);
+void ResetAverageActivePhotcodes (SecFilt *secfilt);
+
+
