Index: /trunk/Ohana/src/addstar/Makefile
===================================================================
--- /trunk/Ohana/src/addstar/Makefile	(revision 5327)
+++ /trunk/Ohana/src/addstar/Makefile	(revision 5328)
@@ -9,6 +9,4 @@
 INC	= 	$(HOME)/include
 SRC	=	$(HOME)/src
-DEF	=	$(HOME)/def
-DFS	=	$(HOME)/dfs
 MAN	=	$(HOME)/doc
 DESTBIN	=	$(LBIN)
@@ -60,5 +58,4 @@
 $(SRC)/RegionOps.$(ARCH).o \
 $(SRC)/replace_match.$(ARCH).o \
-$(SRC)/save_pt_catalog.$(ARCH).o \
 $(SRC)/SetSignals.$(ARCH).o \
 $(SRC)/sort_lists.$(ARCH).o \
@@ -107,14 +104,4 @@
 default: $(PROGRAM)
 
-COMMOBJ = \
-$(DFS)/AddstarClientOptions.$(ARCH).o \
-$(DFS)/Stars.$(ARCH).o
-
-COMMINC = \
-$(DFS)/AddstarClientOptions.h \
-$(DFS)/Stars.h
-
-autocode: $(COMMOBJ)
-
 ADDSTARC = $(SRC)/addstarc.$(ARCH).o $(CLIENT)
 ADDSTARD = $(SRC)/addstard.$(ARCH).o $(SERVER)
@@ -152,18 +139,4 @@
 	$(CC) $^ -o $@ $(LFLAGS)
 
-$(INC)/commcode.h: $(COMMINC)
-	cat $(DEF)/common.h >  $(INC)/commcode.h
-	cat $(COMMINC)      >> $(INC)/commcode.h
-	@echo "# endif"     >> $(INC)/commcode.h
-	@echo done with commcode.h
-
-$(DFS)/%.c: $(DEF)/%.d $(DEF)/commcode.c $(INC)/commcode.h
-	@if [ ! -d $(DFS) ]; then mkdir -p $(DFS); fi
-	generate $< $(DEF)/commcode.c $@
-
-$(DFS)/%.h: $(DEF)/%.d $(DEF)/commcode.h
-	@if [ ! -d $(DFS) ]; then mkdir -p $(DFS); fi
-	generate $< $(DEF)/commcode.h $@
-
 $(DESTBIN)/%: $(BIN)/%.$(ARCH)
 	@if [ ! -d $(DESTBIN) ]; then mkdir -p $(DESTBIN); fi
@@ -189,5 +162,2 @@
 	rm -f `find . -name "*~"`
 	rm -f `find . -name "#*"`
-	rm -f $(DFS)/*.h
-	rm -f $(DFS)/*.c
-	rm -f $(INC)/commcode.h
Index: /trunk/Ohana/src/addstar/doc/notes.txt
===================================================================
--- /trunk/Ohana/src/addstar/doc/notes.txt	(revision 5327)
+++ /trunk/Ohana/src/addstar/doc/notes.txt	(revision 5328)
@@ -1,2 +1,39 @@
+
+todo goals:
+
+  o move addstar/gstars to AddstarClientOptions
+  o add thiscode to AddstarClientOptions
+  o address MOSAIC somehow
+  o replace skyprobe mode
+  - address the cat & ref modes in client/server
+  
+  * add airmass quality to options
+  * deal with Myyyy as sky value
+  * better way to interpret flips header info for sky?
+  * fix unlink empty file problem
+  * programs like relphot and dvo need to be provided 
+    with sorted measure tables; add this as a feature 
+    of the load_catalog API?
+
+2005.10.13
+
+  I am working to minimize usage of globals and cleanup addstar
+  functions to make them more appropriate for the client / server
+  model.  
+
+  I have dropped save_pt_catalog, and modified load_pt_catalog, as
+  well as the load_catalog functions.  Now, the choice of elements to
+  load is made by setting flags in the catalog structure itself
+  (catalog.catflags).  Thus this information is carried around by the
+  catalog variable.  the load_pt_catalog function now only returns
+  true.  it is now necessary to check the state of the disk file by
+  examining Nave_disk to decide if the file is empty or not.  This is
+  perhaps too limiting: it prevents us from using only one of the
+  catalog tables (say measure without average).  The concern I have is
+  to manage the process of cleaning up the catalogs if no data is
+  added to them:  I don't want addstar to leave behind empty files
+  (plus, I think this breaks the lock/load state logic).  Thus,
+  whenever addstar unlocks / closes a file, if the Nave_disk +
+  Naverage is 0, the file should be unlinked.
 
 2005.10.12
Index: /trunk/Ohana/src/addstar/include/2mass.h
===================================================================
--- /trunk/Ohana/src/addstar/include/2mass.h	(revision 5327)
+++ /trunk/Ohana/src/addstar/include/2mass.h	(revision 5328)
@@ -10,4 +10,4 @@
 
 GSCRegion *get2mass_acc (GSCRegion *patch, int *nfiles, char *path, char *accel);
-Stars *get2mass_2DR_data (GSCRegion *region, GSCRegion *patch, int *nstars);
-Stars *get2mass_AS_data (GSCRegion *region, GSCRegion *patch, int *nstars);
+Stars *get2mass_2DR_data (GSCRegion *region, GSCRegion *patch, int photcode, int *nstars);
+Stars *get2mass_AS_data (GSCRegion *region, GSCRegion *patch, int phocode, int *nstars);
Index: /trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- /trunk/Ohana/src/addstar/include/addstar.h	(revision 5327)
+++ /trunk/Ohana/src/addstar/include/addstar.h	(revision 5328)
@@ -52,15 +52,17 @@
 
 /* these globals modify the behavior of gstars (KEEP) */
-double SNLIMIT;
-int    ACCEPT_ASTROM;  // accept even bad astrometry solutions (NASTRO == 0)
-int    TEXTMODE;       // force input file to be loaded as RAW
-char  *DUMP;           // dump out intermediate results
-int    XOVERSCAN;      // used to modify stored image dimensions 
-int    YOVERSCAN;      // used to modify stored image dimensions 
-int    XMIN;           // used to filter loaded star list 
-int    XMAX;           // used to filter loaded star list 
-int    YMIN;           // used to filter loaded star list 
-int    YMAX;           // used to filter loaded star list 
-double Latitude;       // carried into image structure from config
+double 	SNLIMIT;
+int    	ACCEPT_ASTROM;  // accept even bad astrometry solutions (NASTRO == 0)
+int    	TEXTMODE;       // force input file to be loaded as RAW
+int     SUBPIX;         // apply a subpix correction
+char   *DUMP;           // dump out intermediate results
+int    	XOVERSCAN;      // used to modify stored image dimensions 
+int    	YOVERSCAN;      // used to modify stored image dimensions 
+int    	XMIN;           // used to filter loaded star list 
+int    	XMAX;           // used to filter loaded star list 
+int    	YMIN;           // used to filter loaded star list 
+int    	YMAX;           // used to filter loaded star list 
+double 	Latitude;       // carried into image structure from config
+Coords *MOSAIC;         // carries the mosaic into gstars
 
 /* these globals are used separately by both client and server (KEEP) */
@@ -68,31 +70,7 @@
 int    VERBOSE;
 
-/* add to options as an int / keep the global? */
-PhotCode *thiscode;
-
-/* how to carry this around? */
-Coords   *MOSAIC;
-
-/* globals set by command-line options (AddstarClientOptions) */
-/* move into AddstarClientOptions completely */
-# if (0)
-int    MODE;
-int    EXISTING_REGIONS;
-int    ONLY_MATCH;
-int    SKIP_MISSED;
-int    REPLACE;
-int    CLOSEST;
-int    NOSORT;
-int    UPDATE;
-int    ONLY_IMAGES;
-int    CALIBRATE;
-double DEFAULT_RADIUS;   // XXX add to AddstarClientOptions
-double NSIGMA;           // XXX add to AddstarClientOptions
-# endif
-
 /* modify server behavoir (make this an addstar cleanup mode?) */
 int    FORCE_READ;
 
-int   SKYPROBE;  
 // XXX this should be replaced with 
 // 1) an airmass accuracy option
@@ -126,17 +104,17 @@
 int        dump_rawstars          PROTO((Stars *stars, int Nstars));
 int        edge_check             PROTO((double *x1, double *y1, double *x2, double *y2));
-void       find_matches           PROTO((GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, AddstarClientOptions options));
-void       find_matches_closest   PROTO((GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, AddstarClientOptions options));
+void       find_matches           PROTO((GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options));
+void       find_matches_closest   PROTO((GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options));
 void       find_matches_refstars  PROTO((GSCRegion *region, Stars **stars, int Nstars, Catalog *catalog, AddstarClientOptions options));
 Stars    **find_subset            PROTO((GSCRegion *region, Stars *stars, int Nstars, int *NSTARS));
 int        gcatalog               PROTO((Catalog *catalog));
-Stars     *get2mass               PROTO((GSCRegion *patch, int *NSTARS, int mode));
+Stars     *get2mass               PROTO((GSCRegion *patch, int photcode, int mode, int *NSTARS));
 double     get_subpix             PROTO((double x, double y));
 Stars     *getgsc                 PROTO((GSCRegion *patch, int *NSTARS));
-Stars     *getusno                PROTO((GSCRegion *catstats, int *Nstars));
-Image     *gimages                PROTO((FITS_DB *db, Image *image, int *Npimage));
-Stars     *grefcat                PROTO((char *Refcat, GSCRegion *catstats, int *nstars));
-Stars     *grefstars              PROTO((char *file, int *Nstars));
-GSCRegion *gregion_image          PROTO((Image *image, int *Nregions));
+Stars     *getusno                PROTO((GSCRegion *catstats, int photcode, int *Nstars));
+Image     *gimages                PROTO((FITS_DB *db, Image *image, Coords *mosaic, int *Npimage));
+Stars     *grefcat                PROTO((char *Refcat, GSCRegion *catstats, int photcode, int *nstars));
+Stars     *grefstars              PROTO((char *file, int photcode, int *Nstars));
+GSCRegion *gregion_image          PROTO((Image *image, Coords *mosaic, int *Nregions));
 GSCRegion *gregion_match          PROTO((GSCRegion *regions, int *nregions));
 GSCRegion *gregion_patch          PROTO((GSCRegion *patch, int *nregions));
@@ -144,5 +122,5 @@
 GSCRegion *gregion_stars          PROTO((Stars *stars, int Nstars, int *Nregion));
 GSCRegion *gregions               PROTO((Image *image, int *Nregions));
-Stars     *gstars                 PROTO((char *file, int *NSTARS, Image *image));
+Stars     *gstars                 PROTO((char *file, int *NSTARS, int photcode, Image *image));
 void       help                   PROTO(());
 int        in_image               PROTO((double r, double d, Image *image));
Index: /trunk/Ohana/src/addstar/src/addstar.c
===================================================================
--- /trunk/Ohana/src/addstar/src/addstar.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/addstar.c	(revision 5328)
@@ -31,6 +31,4 @@
   }
 
-  if (SKYPROBE) load_subpix ();
-
   /* we use the image table to lock db access -- perhaps this is not necessary?*/
   lock_image_db (&db, ImageCat);
@@ -41,13 +39,13 @@
   switch (options.mode) {
   case M_IMAGE:
-    stars = gstars (argv[1], &Nstars, &image);
+    stars = gstars (argv[1], &Nstars, options.photcode, &image);
     if ((DUMP != NULL) && !strcmp (DUMP, "rawstars")) dump_rawstars (stars, Nstars);
-    regions = gregion_image (&image, &Nregions);
+    regions = gregion_image (&image, MOSAIC, &Nregions);
     /* lock_image_db (&db, ImageCat); */
-    overlap = gimages (&db, &image, &Noverlap);
+    overlap = gimages (&db, &image, MOSAIC, &Noverlap);
     /* unlock_image_db (&db); */
     break;
   case M_REFLIST:
-    stars = grefstars (argv[1], &Nstars);
+    stars = grefstars (argv[1], options.photcode, &Nstars);
     regions = gregion_stars (stars, Nstars, &Nregions);
     break;
@@ -95,11 +93,11 @@
       Nsubset = Nstars;
       if (options.closest) {
-	find_matches_closest (&regions[i], stars, Nstars, &catalog, &image, overlap, Noverlap, options);
+	find_matches_closest (&regions[i], stars, Nstars, &catalog, &image, overlap, Noverlap, MOSAIC, options);
       } else {
-	find_matches (&regions[i], stars, Nstars, &catalog, &image, overlap, Noverlap, options);
+	find_matches (&regions[i], stars, Nstars, &catalog, &image, overlap, Noverlap, MOSAIC, options);
       }
       break;
     case M_REFCAT:
-      stars = grefcat (argv[1], &regions[i], &Nstars);
+      stars = grefcat (argv[1], &regions[i], options.photcode, &Nstars);
     case M_REFLIST:
       subset = find_subset (&regions[i], stars, Nstars, &Nsubset);
Index: /trunk/Ohana/src/addstar/src/addstarc.c
===================================================================
--- /trunk/Ohana/src/addstar/src/addstarc.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/addstarc.c	(revision 5328)
@@ -14,5 +14,5 @@
 
   /* load data */
-  stars = gstars (argv[1], &Nstars, &image);
+  stars = gstars (argv[1], &Nstars, MOSAIC, &image);
 
   /* set up server connection */
Index: /trunk/Ohana/src/addstar/src/airmass.c
===================================================================
--- /trunk/Ohana/src/addstar/src/airmass.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/airmass.c	(revision 5328)
@@ -1,3 +1,9 @@
 # include "addstar.h"
+
+static AirmassQuality = FALSE;
+
+void SetAirmassQuality (int quality) {
+  AirmassQuality = quality;
+}
 
 float airmass (float secz_image, double ra, double dec, double st, double latitude) {
@@ -5,5 +11,5 @@
   double hour, cosz, secz;
 
-  if (!SKYPROBE) return (secz_image);
+  if (!AirmassQuality) return (secz_image);
 
   /*** make this optional? we may not have ST... ***/
Index: /trunk/Ohana/src/addstar/src/args.c
===================================================================
--- /trunk/Ohana/src/addstar/src/args.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/args.c	(revision 5328)
@@ -5,4 +5,5 @@
   
   int N;
+  int QUALITY_AIRMASS;
 
   /* check for help request */
@@ -43,10 +44,8 @@
   }
   /* override any header PHOTCODE values */
-  thiscode = NULL;
   options.photcode = 0;
   if ((N = get_argument (argc, argv, "-p"))) {
     remove_argument (N, &argc, argv);
     options.photcode = GetPhotcodeCodebyName (argv[N]);
-    thiscode = GetPhotcodebyName (argv[N]);
     remove_argument (N, &argc, argv);
   }
@@ -148,14 +147,25 @@
 
   /*** optional situations ***/
-  /* treat data specially for skyprobe (calibration, subpix) */ 
-  SKYPROBE = FALSE;
+  /* choose high quality airmass vs low quality airmass (per-star vs per-image) */
+  QUALITY_AIRMASS = FALSE;
+  if ((N = get_argument (argc, argv, "-quality-airmass"))) {
+    remove_argument (N, &argc, argv);
+    QUALITY_AIRMASS = TRUE;
+  }
+  /* choose high quality airmass vs low quality airmass (per-star vs per-image) */
+  SUBPIX = FALSE;
+  if ((N = get_argument (argc, argv, "-subpix"))) {
+    remove_argument (N, &argc, argv);
+    SUBPIX = TRUE;
+  }
+  /* skyprobe means: subpix correction and quality airmass */ 
   if ((N = get_argument (argc, argv, "-skyprobe"))) {
-    SKYPROBE = TRUE;
-    remove_argument (N, &argc, argv);
-    /* XXX EAM : define airmass calculation method?
-                 set calibrate to true
-		 perhaps a 'skyprobe' data format (for image.sky issue)
-    */
-  }
+    remove_argument (N, &argc, argv);
+    QUALITY_AIRMASS = TRUE;
+    SUBPIX = TRUE;
+  }
+  SetAirmassQuality (QUALITY_AIRMASS);
+  if (SUBPIX) load_subpix ();
+
   /* define 2MASS quality flags to keep */
   SELECT_2MASS_QUALITY = NULL;
@@ -196,4 +206,8 @@
   }
 
+  if ((options.mode == M_REFLIST) && (options.photcode == 0)) {
+    fprintf (stderr, "photcode must be specified for -ref\n");
+    exit (2);
+  }
 
   if (argc != 2) {
Index: /trunk/Ohana/src/addstar/src/args_client.c
===================================================================
--- /trunk/Ohana/src/addstar/src/args_client.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/args_client.c	(revision 5328)
@@ -5,4 +5,5 @@
   
   int N;
+  int QUALITY_AIRMASS;
 
   /* check for help request */
@@ -146,10 +147,24 @@
 
   /*** optional situations ***/
-  /* treat input data specially for skyprobe (calibration, subpix) */ 
-  SKYPROBE = FALSE;
+  /* choose high quality airmass vs low quality airmass (per-star vs per-image) */
+  QUALITY_AIRMASS = FALSE;
+  if ((N = get_argument (argc, argv, "-quality-airmass"))) {
+    remove_argument (N, &argc, argv);
+    QUALITY_AIRMASS = TRUE;
+  }
+  /* choose high quality airmass vs low quality airmass (per-star vs per-image) */
+  SUBPIX = FALSE;
+  if ((N = get_argument (argc, argv, "-subpix"))) {
+    remove_argument (N, &argc, argv);
+    SUBPIX = TRUE;
+  }
+  /* skyprobe means: subpix correction and quality airmass */ 
   if ((N = get_argument (argc, argv, "-skyprobe"))) {
-    SKYPROBE = TRUE;
-    remove_argument (N, &argc, argv);
-  }
+    remove_argument (N, &argc, argv);
+    QUALITY_AIRMASS = TRUE;
+    SUBPIX = TRUE;
+  }
+  if (SUBPIX) load_subpix ();
+
   /* define 2MASS quality flags to keep */
   SELECT_2MASS_QUALITY = NULL;
Index: /trunk/Ohana/src/addstar/src/calibrate.c
===================================================================
--- /trunk/Ohana/src/addstar/src/calibrate.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/calibrate.c	(revision 5328)
@@ -6,5 +6,5 @@
 void InitCalibration () {
 
-    fprintf (stderr, "calibrating the image...  %d\n", thiscode[0].equiv);
+    fprintf (stderr, "calibrating the image...\n");
     Ncal = 0;
     NCAL = 1000;
@@ -47,11 +47,13 @@
   float CalM0, CalM1, CalM2, dCalM;
   short CalC0, CalC1, CalC2;
+  PhotCode *code;
 
   found0 = found1 = found2 = FALSE;
   CalM0 = CalM1 = CalM2 = dCalM = NO_MAG;
 
-  CalC0 = thiscode[0].equiv;
-  CalC1 = thiscode[0].c1;
-  CalC2 = thiscode[0].c2;
+  code  = GetPhotcodebyCode (new[0].source);
+  CalC0 = code[0].equiv;
+  CalC1 = code[0].c1;
+  CalC2 = code[0].c2;
 
   m = average[0].offset;
@@ -85,5 +87,8 @@
   float N, M1, M2, Klam, Clam, Xlam, Mabs, *Dmag, *dDmag;
   float dMo, dMr, Mw, Dmed, W1, W2, NSigma;
-  
+  PhotCode *code;
+
+  code = GetPhotcodebyCode (image[0].source);
+
   /* reject multiple matched-stars */
   /* find maximum value of Nstar[] */
@@ -109,7 +114,7 @@
   ALLOCATE (dDmag, float, Ncal);
   Nkeep = 0;
-  Clam = thiscode[0].C*0.001;
-  Klam = thiscode[0].K;
-  Xlam = thiscode[0].X[0];
+  Clam = code[0].C*0.001; /* photcode.C still in millimags */
+  Klam = code[0].K;
+  Xlam = code[0].X[0];
   for (i = 0; i < Ncal; i++) {
     /* if this entry has too many (or two few?) matches, skip it */
@@ -121,8 +126,6 @@
     /* skip stars brighter than 8.0 */
     /* XXX EAM : perhaps make this more flexible?? */
-    if (SKYPROBE) {
-      if (Mabs > 9.0) continue;
-      if (Mabs < 5.0) continue;
-    }
+    if (Mabs > 9.0) continue;
+    if (Mabs < 5.0) continue;
     
     /* XXX EAM: note the artificial 0.005 dmag here */
Index: /trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/find_matches.c	(revision 5328)
@@ -1,5 +1,5 @@
 # include "addstar.h"
 
-void find_matches (GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, AddstarClientOptions options) {
+void find_matches (GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
 
   int i, j, n, N, J;
@@ -9,17 +9,18 @@
   int *N1, *N2,  *next_meas, *next_miss;
   int Nave, NAVE, Nmeas, NMEAS, Nmiss, NMISS, Nmatch;
-  Coords tcoords;
   int Nsecfilt, Nsec;
   float Mcat, *Mval, MTIME;
   double dtime;
   struct timeval start, stop;
+  PhotCode *code;
+  Coords tcoords;
 
   gettimeofday (&start, NULL);
+
+  code = GetPhotcodebyCode (options.photcode);
 
   /* photcode data - must by of type DEP, (PRI, SEC) - probably should restrict to DEP */
   Nsecfilt = GetPhotcodeNsecfilt ();
-  Nsec = (thiscode[0].type == PHOT_DEP) ?
-    GetPhotcodeNsec (thiscode[0].equiv) :
-    GetPhotcodeNsec (thiscode[0].code);
+  Nsec = (code[0].type == PHOT_DEP) ? GetPhotcodeNsec (code[0].equiv) : GetPhotcodeNsec (code[0].code);
   /* this function requires incoming stars to have the same photcode */
 
@@ -46,5 +47,5 @@
   /* for mosaic astrometry, the grid should be w.r.t. the tangent-plane, not chip coords */
   if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
-    tcoords = MOSAIC[0];
+    tcoords = mosaic[0];
     tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
     tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
@@ -199,5 +200,5 @@
 
   /* add reference for undetected catalog stars */
-  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) RegisterMosaic (MOSAIC);
+  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) RegisterMosaic (mosaic);
   for (j = 0; (j < Nave) && !options.skip_missed; j++) {
     n = N2[j];
Index: /trunk/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 5328)
@@ -1,5 +1,5 @@
 # include "addstar.h"
 
-void find_matches_closest (GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, AddstarClientOptions options) {
+void find_matches_closest (GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
 
   int i, j, n, N, J, Jmin;
@@ -14,12 +14,13 @@
   double dtime;
   struct timeval start, stop;
+  PhotCode *code;
 
   gettimeofday (&start, NULL);
+
+  code = GetPhotcodebyCode (options.photcode);
 
   /* photcode data - must by of type DEP, (PRI, SEC) - probably should restrict to DEP */
   Nsecfilt = GetPhotcodeNsecfilt ();
-  Nsec = (thiscode[0].type == PHOT_DEP) ?
-    GetPhotcodeNsec (thiscode[0].equiv) :
-    GetPhotcodeNsec (thiscode[0].code);
+  Nsec = (code[0].type == PHOT_DEP) ? GetPhotcodeNsec (code[0].equiv) : GetPhotcodeNsec (code[0].code);
   /* this function requires incoming stars to have the same photcode */
 
@@ -46,5 +47,5 @@
   /* for mosaic astrometry, the grid should be w.r.t. the tangent-plane, not chip coords */
   if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
-    tcoords = MOSAIC[0];
+    tcoords = mosaic[0];
     tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
     tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
@@ -197,5 +198,5 @@
 
   /** add reference for undetected catalog stars **/
-  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) RegisterMosaic (MOSAIC);
+  if (!strcmp (&image[0].coords.ctype[4], "-WRP")) RegisterMosaic (mosaic);
   for (j = 0; (j < Nave) && !options.skip_missed; j++) {
     n = N2[j];
Index: /trunk/Ohana/src/addstar/src/get2mass.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/get2mass.c	(revision 5328)
@@ -2,5 +2,5 @@
 # include "2mass.h"
 
-Stars *get2mass (GSCRegion *patch, int *NSTARS, int mode) {
+Stars *get2mass (GSCRegion *patch, int photcode, int mode, int *NSTARS) {
   
   char *path;
@@ -13,8 +13,7 @@
   NAMED_PHOTCODE (TM_H, "2MASS_H");
   NAMED_PHOTCODE (TM_K, "2MASS_K");
-  if (thiscode == NULL) Shutdown ("photcode not specified");
-  if (thiscode[0].code == TM_J) goto good_code;
-  if (thiscode[0].code == TM_H) goto good_code;
-  if (thiscode[0].code == TM_K) goto good_code;
+  if (photcode == TM_J) goto good_code;
+  if (photcode == TM_H) goto good_code;
+  if (photcode == TM_K) goto good_code;
   Shutdown ("2MASS photcode not specified");
 
@@ -31,9 +30,9 @@
     switch (mode) {
       case 0:
-	refcat = get2mass_AS_data (&regions[i], patch, &Nrefcat);
+	refcat = get2mass_AS_data (&regions[i], patch, photcode, &Nrefcat);
 	if (VERBOSE) fprintf (stderr, "loaded %d stars from 2MASS (allsky)\n", Nrefcat);
 	break;
       case 1:
-	refcat = get2mass_2DR_data (&regions[i], patch, &Nrefcat);
+	refcat = get2mass_2DR_data (&regions[i], patch, photcode, &Nrefcat);
 	if (VERBOSE) fprintf (stderr, "loaded %d stars from 2MASS (dr2)\n", Nrefcat);
 	break;
Index: /trunk/Ohana/src/addstar/src/get2mass_as.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass_as.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/get2mass_as.c	(revision 5328)
@@ -13,5 +13,5 @@
 e_time get2mass_time (char *ptr, char *buffer, int Nbound, int Nbyte);
 
-Stars *get2mass_AS_data (GSCRegion *region, GSCRegion *patch, int *nstars) {
+Stars *get2mass_AS_data (GSCRegion *region, GSCRegion *patch, int photcode, int *nstars) {
   
   int FilterSkip, TimeSkip;
@@ -33,22 +33,21 @@
   DEC1 = MIN (patch[0].DEC[1], UserPatch.DEC[1]);
 
-  code = thiscode[0].code;
   FilterSkip = TimeSkip = 0;
-  if (thiscode[0].code == TM_J) {
+  if (photcode == TM_J) {
       FilterSkip = 6;
       TimeSkip = 23;
       Qentry   = 0;
   }
-  if (thiscode[0].code == TM_H) {
+  if (photcode == TM_H) {
       FilterSkip = 10;
       TimeSkip = 19;
       Qentry   = 1;
   }
-  if (thiscode[0].code == TM_K) {
+  if (photcode == TM_K) {
       FilterSkip = 14;
       TimeSkip = 15;
       Qentry   = 2;
   }
-  if (!FilterSkip) Shutdown ("invalid photcode %d", thiscode[0].code);
+  if (!FilterSkip) Shutdown ("invalid photcode %s", GetPhotcodeNamebyCode(photcode));
 
   filename = region[0].filename;
Index: /trunk/Ohana/src/addstar/src/get2mass_dr2.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass_dr2.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/get2mass_dr2.c	(revision 5328)
@@ -5,5 +5,5 @@
 # define NLINE 30000
 
-Stars *get2mass_2DR_data (GSCRegion *region, GSCRegion *patch, int *nstars) {
+Stars *get2mass_2DR_data (GSCRegion *region, GSCRegion *patch, int photcode, int *nstars) {
   
   int i, Nstars, NSTARS, Nbyte, Nline;
@@ -64,5 +64,5 @@
       stars[Nstars].found = -1;
 
-      if (thiscode[0].code == TM_J) {
+      if (photcode == TM_J) {
 	dparse (&J,  1, &buffer[NBYTE*i + 53]);
 	dparse (&dJ, 2, &buffer[NBYTE*i + 53]);
@@ -71,5 +71,5 @@
 	stars[Nstars].code = TM_J;
       }
-      if (thiscode[0].code == TM_H) {
+      if (photcode == TM_H) {
 	dparse (&H,  1, &buffer[NBYTE*i + 72]);
 	dparse (&dH, 2, &buffer[NBYTE*i + 72]);
@@ -78,5 +78,5 @@
 	stars[Nstars].code = TM_H;
       }
-      if (thiscode[0].code == TM_K) {
+      if (photcode == TM_K) {
 	dparse (&K,  1, &buffer[NBYTE*i + 91]);
 	dparse (&dK, 2, &buffer[NBYTE*i + 91]);
Index: /trunk/Ohana/src/addstar/src/getusno.c
===================================================================
--- /trunk/Ohana/src/addstar/src/getusno.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/getusno.c	(revision 5328)
@@ -9,5 +9,5 @@
   1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10};
 
-Stars *getusno (GSCRegion *catstats, int *Nstars) {
+Stars *getusno (GSCRegion *catstats, int photcode, int *Nstars) {
 
   long int offset;
@@ -27,7 +27,6 @@
   NAMED_PHOTCODE (USNO_RED, "USNO_RED");
   NAMED_PHOTCODE (USNO_BLUE, "USNO_BLUE");
-  if (thiscode == NULL) Shutdown ("photcode not specified");
-  if (thiscode[0].code == USNO_RED) goto good_code;
-  if (thiscode[0].code == USNO_BLUE) goto good_code;
+  if (photcode == USNO_RED) goto good_code;
+  if (photcode == USNO_BLUE) goto good_code;
   Shutdown ("USNO photcode not specified");
 good_code:
@@ -130,9 +129,9 @@
 
 	/* one pass of addstar does either r or b */
-	if (thiscode[0].code == USNO_RED) {
+	if (photcode == USNO_RED) {
 	  stars[Nusno].code  = USNO_RED;
 	  stars[Nusno].M     = fabs (0.1*(buf[2] - 1000*((int)(buf[2]/1000))));
 	} 
-	if (thiscode[0].code == USNO_BLUE) {	
+	if (photcode == USNO_BLUE) {	
 	  stars[Nusno].code  = USNO_BLUE;
 	  stars[Nusno].M     = fabs (0.1*((int)(buf[2] - 1000000*((int)(buf[2]/1000000))) / 1000));
Index: /trunk/Ohana/src/addstar/src/gimages.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gimages.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/gimages.c	(revision 5328)
@@ -2,5 +2,5 @@
 
 /* given image, find catalog images which overlap it */
-Image *gimages (FITS_DB *db, Image *image, int *Npimage) {
+Image *gimages (FITS_DB *db, Image *image, Coords *mosaic, int *Npimage) {
   
   int i, j, k, addtolist;
@@ -37,5 +37,5 @@
   /* for mosaic astrometry, the grid should be w.r.t. the tangent-plane, not chip coords */
   if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
-    tcoords = MOSAIC[0];
+    tcoords = mosaic[0];
     tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
     tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
@@ -43,5 +43,5 @@
     tcoords.Npolyterms = 1;
     strcpy (tcoords.ctype, "RA---TAN");
-    RegisterMosaic (MOSAIC);
+    RegisterMosaic (mosaic);
     /* register so image->sky conversions below have correct mosaic */
   } else {
Index: /trunk/Ohana/src/addstar/src/greference.c
===================================================================
--- /trunk/Ohana/src/addstar/src/greference.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/greference.c	(revision 5328)
@@ -1,5 +1,7 @@
 # include "addstar.h"
+# define LOAD_ALLSKY 0
+# define LOAD_DR2    1
 
-Stars *grefcat (char *Refcat, GSCRegion *catstats, int *nstars) {
+Stars *grefcat (char *Refcat, GSCRegion *catstats, int photcode, int *nstars) {
 
   int Nstars;
@@ -14,5 +16,5 @@
   /* get stars from USNO for the given region */
   if (!strcasecmp (Refcat, "USNO")) {
-    stars = getusno (catstats, &Nstars);
+    stars = getusno (catstats, photcode, &Nstars);
   }
 
@@ -24,15 +26,15 @@
   /* get stars from 2MASS for the given region */
   if (!strcasecmp (Refcat, "2MASS")) {
-    stars = get2mass (catstats, &Nstars, 0);
+    stars = get2mass (catstats, photcode, LOAD_ALLSKY, &Nstars);
   }
   
   /* get stars from 2MASS for the given region */
   if (!strcasecmp (Refcat, "2MASS-ALLSKY")) {
-    stars = get2mass (catstats, &Nstars, 0);
+    stars = get2mass (catstats, photcode, LOAD_ALLSKY, &Nstars);
   }
   
   /* get stars from 2MASS for the given region */
   if (!strcasecmp (Refcat, "2MASS-DR2")) {
-    stars = get2mass (catstats, &Nstars, 1);
+    stars = get2mass (catstats, photcode, LOAD_DR2, &Nstars);
   }
   
Index: /trunk/Ohana/src/addstar/src/grefstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/grefstars.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/grefstars.c	(revision 5328)
@@ -2,5 +2,5 @@
 
 /* read ASCII file with ref star data */
-Stars *grefstars (char *file, int *Nstars) {
+Stars *grefstars (char *file, int photcode, int *Nstars) {
 
   FILE *f;
@@ -8,7 +8,4 @@
   Stars *stars;
   char line[256];
-
-  /* require photcode */
-  if (thiscode == NULL) Shutdown ("photcode not specified");
 
   /* open file */
@@ -31,5 +28,5 @@
     while (stars[N].R >= 360.0) stars[N].R -= 360.0;
     stars[N].t = 0;
-    stars[N].code = thiscode[0].code;
+    stars[N].code = photcode;
     stars[N].found = FALSE;
     CHECK_REALLOCATE (stars, Stars, NSTARS, N+1, 100);
@@ -38,4 +35,2 @@
   return (stars);
 }
-
-/* add the photcode here */
Index: /trunk/Ohana/src/addstar/src/gregion_image.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gregion_image.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/gregion_image.c	(revision 5328)
@@ -2,5 +2,5 @@
 
 /* given image with coords, find regions which overlap image */
-GSCRegion *gregion_image (Image *image, int *Nregions) {
+GSCRegion *gregion_image (Image *image, Coords *mosaic, int *Nregions) {
   
   GSCRegion *regions, impatch;
@@ -14,6 +14,6 @@
 
   if (!strcmp (&image[0].coords.ctype[4], "-WRP")) {
-    if (MOSAIC == NULL) Shutdown ("no mosaic for WRP image (use -mosaic)");
-    RegisterMosaic (MOSAIC);
+    if (mosaic == NULL) Shutdown ("no mosaic for WRP image (use -mosaic)");
+    RegisterMosaic (mosaic);
   }    
 
Index: /trunk/Ohana/src/addstar/src/gstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gstars.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/gstars.c	(revision 5328)
@@ -1,10 +1,10 @@
 # include "addstar.h"
 
-Stars *gstars (char *file, int *NSTARS, Image *image) {
+Stars *gstars (char *file, int *NSTARS, int photcode, Image *image) {
 
   FILE *f;
   int j, N, Nbytes, extend;
   int itmp, hour, min;
-  char *name, *c, photcode[64], line[80];
+  char *name, *c, photname[64], line[80];
   double tmp, sec, dMs;
   Stars *stars, *rdstars;
@@ -59,13 +59,20 @@
  
   /* get photcode from header */
-  if (thiscode == NULL) {
-    fits_scan (&header, "PHOTCODE", "%s", 1, photcode);
-    thiscode = GetPhotcodebyName (photcode);
-  }
-  if (thiscode == NULL) {
-    fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", photcode);
-    exit (1);
-  }
-  image[0].source = thiscode[0].code;
+  if (photcode == 0) {
+    if (!fits_scan (&header, "PHOTCODE", "%s", 1, photname)) {
+      fprintf (stderr, "ERROR: photcode not supplied in header\n");
+      exit (1);
+    }
+    photcode = GetPhotcodeCodebyName (photname);
+    if (photcode == 0) {
+      fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", photname);
+      exit (1);
+    }
+  }
+  if (photcode == 0) {
+    fprintf (stderr, "ERROR: no valid photcode is supplied\n");
+    exit (1);
+  }
+  image[0].source = photcode;
 
   image[0].NX -= XOVERSCAN;
@@ -141,5 +148,6 @@
   ***/
 
-  if (SKYPROBE) {
+  /** I'm essentially using subpix to be synonymous with SKYPROBE */
+  if (SUBPIX) {
     char *p;
     int sky;
@@ -199,7 +207,7 @@
     while (stars[N].R >= 360.0) stars[N].R -= 360.0;
     stars[N].found = -1;
-    stars[N].code = thiscode[0].code;
-
-    if (SKYPROBE) {
+    stars[N].code = photcode;
+
+    if (SUBPIX) {
       dMs = get_subpix (stars[N].X, stars[N].Y);
       stars[N].M    -= dMs;
Index: /trunk/Ohana/src/addstar/src/replace_match.c
===================================================================
--- /trunk/Ohana/src/addstar/src/replace_match.c	(revision 5327)
+++ /trunk/Ohana/src/addstar/src/replace_match.c	(revision 5328)
@@ -7,5 +7,5 @@
   /* search for entry and replace values M, dM, R, D */
   for (i = 0; i < average[0].Nm; i++) {
-    if (measure[i].source != thiscode[0].code) continue;
+    if (measure[i].source != star[0].code) continue;
     measure[i].dR_PS       = 3600.0*(average[0].R_PS - star[0].R);
     measure[i].dD_PS       = 3600.0*(average[0].D_PS - star[0].D);
