Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/Makefile
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/Makefile	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/Makefile	(revision 32994)
@@ -24,8 +24,10 @@
 sedstar     : $(BIN)/sedstar.$(ARCH)
 load2mass   : $(BIN)/load2mass.$(ARCH)
+loadwise    : $(BIN)/loadwise.$(ARCH)
+loadsupercos: $(BIN)/loadsupercos.$(ARCH)
 gztest      : $(BIN)/gztest.$(ARCH)
 mkcmf       : $(BIN)/mkcmf.$(ARCH)
 
-all: addstar sedstar load2mass skycells mkcmf
+all: addstar sedstar load2mass skycells mkcmf loadwise loadsupercos
 
 # I need to fix the client/server version of addstar now that I have dropped Stars
@@ -214,4 +216,40 @@
 $(SRC)/psps_ids.$(ARCH).o
 
+LOAD-WISE = \
+$(SRC)/loadwise.$(ARCH).o \
+$(SRC)/loadwise_prelim_rawdata.$(ARCH).o \
+$(SRC)/loadwise_ops.$(ARCH).o \
+$(SRC)/loadwise_star_full.$(ARCH).o \
+$(SRC)/args_loadwise.$(ARCH).o \
+$(SRC)/ConfigInit.$(ARCH).o \
+$(SRC)/Shutdown.$(ARCH).o \
+$(SRC)/StarOps.$(ARCH).o \
+$(SRC)/find_matches_refstars.$(ARCH).o \
+$(SRC)/build_links.$(ARCH).o \
+$(SRC)/replace_match.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o \
+$(SRC)/update_coords.$(ARCH).o \
+$(SRC)/SkyRegionUtils.$(ARCH).o \
+$(SRC)/psps_ids.$(ARCH).o
+
+LOAD-SUPERCOS = \
+$(SRC)/loadsupercos.$(ARCH).o \
+$(SRC)/loadsupercos_ops.$(ARCH).o \
+$(SRC)/loadsupercos_survey.$(ARCH).o \
+$(SRC)/loadsupercos_plates.$(ARCH).o \
+$(SRC)/loadsupercos_rawdata.$(ARCH).o \
+$(SRC)/args_loadsupercos.$(ARCH).o \
+$(SRC)/ConfigInit.$(ARCH).o \
+$(SRC)/Shutdown.$(ARCH).o \
+$(SRC)/StarOps.$(ARCH).o \
+$(SRC)/find_matches_refstars.$(ARCH).o \
+$(SRC)/build_links.$(ARCH).o \
+$(SRC)/replace_match.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o \
+$(SRC)/update_coords.$(ARCH).o \
+$(SRC)/SkyRegionUtils.$(ARCH).o \
+$(SRC)/parse_time.$(ARCH).o \
+$(SRC)/psps_ids.$(ARCH).o
+
 SEDSTAR = \
 $(SRC)/sedstar.$(ARCH).o \
@@ -241,4 +279,5 @@
 $(SKYCELLS)   : $(INC)/addstar.h
 $(LOAD-2MASS) : $(INC)/addstar.h $(INC)/2mass.h
+$(LOAD-WISE)  : $(INC)/addstar.h $(INC)/WISE.h
 $(MKCMF)      : $(INC)/mkcmf.h
 
@@ -250,4 +289,6 @@
 $(BIN)/sedstar.$(ARCH)     : $(SEDSTAR)
 $(BIN)/load2mass.$(ARCH)   : $(LOAD-2MASS)
+$(BIN)/loadwise.$(ARCH)    : $(LOAD-WISE)
+$(BIN)/loadsupercos.$(ARCH): $(LOAD-SUPERCOS)
 $(BIN)/skycells.$(ARCH)    : $(SKYCELLS)
 $(BIN)/mkcmf.$(ARCH)       : $(MKCMF)
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/WISE.h
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/WISE.h	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/WISE.h	(revision 32994)
@@ -21,35 +21,25 @@
 short WISE_W1, WISE_W2, WISE_W3, WISE_W4;
 
-SkyTable *get2mass_acc (SkyRegion *patch, char *path, char *accel);
-Stars    *get2mass_2DR_data (SkyRegion *region, char *filename, SkyRegion *patch, int photcode, int *nstars);
-Stars    *get2mass_AS_data (SkyRegion *region, char *filename, SkyRegion *patch, int phocode, int *nstars);
-Stars    *get2mass_AS_rawdata (SkyRegion *region, char *filename, SkyRegion *patch, int phocode, int *nstars);
+AddstarClientOptions args_loadwise (int argc, char **argv, AddstarClientOptions options);
 
-SkyTable *scan2mass_acc (char *path, char *accel);
-int       scan2mass_as_data (char *filename);
+int loadwise_prelim_rawdata (SkyList *skytable, char *filename, AddstarClientOptions options);
 
-char     *skipNbounds (char *line, char bound, int Nbound, int Nbyte);
-e_time    get2mass_time (char *ptr, int Nbound, int Nbyte);
-e_time    get2mass_date (char *ptr, int Nbound, int Nmax);
+int getWISE_setup ();
+int getWISE_coords (char *line, double *R, double *D, int Nmax);
+e_time getWISE_date (char *ptr, int Nbound, int Nmax);
+e_time getWISE_time (char *ptr, int Nbound, int Nmax);
 
-int       load2mass_as_rawdata (SkyList *skytable, char *filename, AddstarClientOptions options);
-SkyTable *load2mass_acc (char *path, char *accel);
-int       get2mass_3star (Stars *star, char *line, int Nmax);
-int       load2mass_catalog (Catalog *catalog, Stars *stars, int Nstars);
+int getWISE_sortStars (WISE_Stars *tstars, int Ntstars);
+int loadwise_star_full (Stars **star, char *line, int Nmax);
 
-int       get2mass_setup (int photcode);
-int       get2mass_coords (char *line, double *R, double *D, int Nmax);
-int       get2mass_star (Stars *star, char *line, int Nmax);
-int       get2mass_3star (Stars *star, char *line, int Nmax);
+char *nextWISEfield (char *line);
+int setWISE_cc_flag (Stars *star, char qual);
+int setWISE_ph_qual (Stars *star, char qual);
+int setWISE_rd_flag (Stars *star, char qual);
+int setWISE_bl_flag (Stars *star, char qual);
+int setWISE_gal_flag (Stars *star, char qual);
+int setWISE_mp_flag (Stars *star, char qual);
+int setWISE_dup_flag (Stars *star, char qual);
+int setWISE_use_flag (Stars *star, char qual);
 
-int get2mass_3star_full (Stars **star, char *line, int Nmax);
-char *next2MASSfield (char *line);
-int set2MASS_ph_qual (Stars *star, char qual);
-int set2MASS_rd_flag (Stars *star, char qual);
-int set2MASS_cc_flag (Stars *star, char qual);
-int set2MASS_bl_flag (Stars *star, char qual);
-int set2MASS_gal_flag (Stars *star, char qual);
-int set2MASS_mp_flag (Stars *star, char qual);
-int set2MASS_dup_flag (Stars *star, char qual);
-int set2MASS_use_flag (Stars *star, char qual);
-int get2mass_sortStars (WISE_Stars *tstars, int Ntstars);
+char *skipNbounds (char *line, char bound, int Nbound, int Nbyte);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/addstar.h
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/addstar.h	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/addstar.h	(revision 32994)
@@ -28,4 +28,7 @@
     fprintf (stderr, "ERROR:  photcode %s not found in photcode table\n", NAME); \
     exit (0); }
+
+# define dCOS(A)   ((double) cos ((double)RAD_DEG*A))
+# define dSIN(A)   ((double) sin ((double)RAD_DEG*A))
 
 typedef struct {
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/supercos.h
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/supercos.h	(revision 32994)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/include/supercos.h	(revision 32994)
@@ -0,0 +1,84 @@
+/* header for items specific to loadsupercos.c */
+
+typedef struct {
+  double longitude;
+  double latitude;
+  double plateScale;
+  int survey_id;
+} Survey;
+
+typedef struct{
+    int64_t objID;			// 0
+    int8_t surveyID;			// 8
+    int32_t plateID;			// 9
+    double ra;				// 33
+    double dec;				// 41
+    double xCen;			// 129
+    double yCen;			// 137
+    float aU;				// 145
+    float bU;				// 149
+    int16_t thetaU;			// 154
+    int8_t class;			// 165
+    int32_t quality;			// 104
+    float prfStat;			// 208
+    float prfMag;			// 212
+    float gMag;				// 216
+    float sMag;				// 220
+} Detection;
+
+typedef struct{
+    int64_t objID;			// 0
+    int8_t surveyID;			// 8
+    int32_t plateID;			// 9
+    int64_t parentID;			// 13
+    int64_t sourceID;			// 21
+    int32_t recNum;			// 29
+    double ra;				// 33
+    double dec;				// 41
+    int64_t htmId;			// 49
+    double cx;				// 57
+    double cy;				// 65
+    double cz;				// 77
+    double xmin;			// 81
+    double xmax;			// 89
+    double ymin;			// 97
+    double ymax;			// 105
+    int32_t area;			// 113
+    float ipeak;			// 117
+    float cosmag;			// 121
+    float isky;				// 125
+    double xCen;			// 129
+    double yCen;			// 137
+    float aU;				// 145
+    float bU;				// 149
+    int16_t thetaU;			// 154
+    float aI;				// 155
+    float bI;				// 159
+    int16_t thetaI;			// 163
+    int8_t class;			// 165
+    int16_t pa;				// 166
+    int32_t ap1;			// 168
+    int32_t ap2;			// 172
+    int32_t ap3;			// 176
+    int32_t ap4;			// 180
+    int32_t ap5;			// 184
+    int32_t ap6;			// 188
+    int32_t ap7;			// 192
+    int32_t ap8;			// 196
+    int32_t blend;			// 200
+    int32_t quality;			// 204
+    float prfStat;			// 208
+    float prfMag;			// 212
+    float gMag;				// 216
+    float sMag;				// 220
+    int16_t SSAfield;			// 224
+    int16_t seam;			// 226 (228 total)
+} FullDetection;
+
+AddstarClientOptions args_loadsupercos (int argc, char **argv, AddstarClientOptions options);
+Survey *loadsupercos_survey (char *filename, int *nsurvey);
+Image *loadsupercos_plates (Survey *survey, int Nsurvey, char *filename, int *nimage);
+int loadsupercos_getFilterInfo (char *line, char *emulsion, char *filterID);
+int loadsupercos_rawdata (Image *image, int *imlist, int Nimage, SkyList *skytable, char *filename, AddstarClientOptions options);
+int loadsupercos_sortStars (Stars *tstars, int Ntstars);
+int *loadsupercos_image_index (Image *image, int Nimage);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/ConfigInit.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/ConfigInit.c	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/ConfigInit.c	(revision 32994)
@@ -213,7 +213,6 @@
 
   /* get detection filtering mask */
-  if(!ScanConfig (config, "DETECTIONFILTER", "%d", 0, &options.detectionFilter)) {
+  if (!ScanConfig (config, "DETECTIONFILTER", "%d", 0, &options.detectionFilter)) {
       options.detectionFilter = 0;
-      fprintf (stderr, "Could not find 'DETECTIONFILTER' in config, using '%x'\n", options.detectionFilter);
   }
 
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/ReadStarsSDSS.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/ReadStarsSDSS.c	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/ReadStarsSDSS.c	(revision 32994)
@@ -4,6 +4,4 @@
 
 # define NFILTER 5
-# define dCOS(A)   ((double) cos ((double)RAD_DEG*A))
-# define dSIN(A)   ((double) sin ((double)RAD_DEG*A))
 
 # define GET_COLUMN_5(NAME,TYPE) \
@@ -279,24 +277,4 @@
 }
 
-// ha/dec -> alt/az
-int altaz (double *alt, double *az, double ha, double dec, double latitude) {
-
-  double sind, sinh, cosh;
-
-  sind = dSIN (dec) * dSIN (latitude) + dCOS (dec) * dCOS (ha) * dCOS (latitude);
-  *alt  = DEG_RAD * asin (sind);
-
-  sinh = - dCOS (dec) * dSIN (ha);
-  cosh =   dSIN (dec) * dCOS (latitude) - dCOS (dec) * dCOS (ha) * dSIN (latitude);
-
-  *az = DEG_RAD * atan2 (sinh, cosh);
-
-  // I may need to use the parallactic angle to get the right plate rotation...
-  // sinh = -dCOS(az) * dSIN(alt) * dSIN(ha) * dSIN(latitude) + dSIN(az) * dSIN(alt) * dCOS(ha) - dSIN(ha) * dCOS(alt) * dCOS(latitude);
-  // cosh = -dSIN(az) * dSIN(ha) * dSIN(latitude) - dCOS(az) * dCOS(ha);
-  // rot = -DEG_RAD * atan2 (sinh, cosh);
-  return TRUE;
-}
-
 int SetSDSSFlags (Stars *star, unsigned int flags1, unsigned int flags2) {
 
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/args_loadsupercos.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/args_loadsupercos.c	(revision 32994)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/args_loadsupercos.c	(revision 32994)
@@ -0,0 +1,90 @@
+# include "addstar.h"
+static void help (void);
+
+AddstarClientOptions args_loadsupercos (int argc, char **argv, AddstarClientOptions options) {
+  
+  int N;
+
+  /* check for help request */
+  if (get_argument (argc, argv, "-help") ||
+      get_argument (argc, argv, "-h")) {
+    help ();
+  }
+
+  // a global used by find_matches_refstars.c (value is 1 except for load2mass & loadwise)
+  NREFSTAR_GROUP = 1;
+
+  /*** check for command line options ***/
+
+  /* basic mode: image, list, refcat */
+  options.mode = M_REFCAT;
+
+  /* we do not allow a subset to be extracted -- all or nothing, babe */
+  UserPatch.Rmin = 0;
+  UserPatch.Rmax= 360;
+  UserPatch.Dmin = -90;
+  UserPatch.Dmax = +90;
+
+  /* only add to existing regions */
+  options.existing_regions = FALSE;
+  if ((N = get_argument (argc, argv, "-existing-regions"))) {
+    options.existing_regions = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* only add to existing objects */
+  options.only_match = FALSE;
+  if ((N = get_argument (argc, argv, "-only-match"))) {
+    options.only_match = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* replace measurement, don't duplicate (ref/cat only) */
+  options.replace = FALSE;
+  if ((N = get_argument (argc, argv, "-replace"))) {
+    options.replace = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* extra error messages */
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* other addstar options which cannot be used in load2mass */
+  options.photcode = 0;
+  options.timeref = 0; 
+  options.mosaic = FALSE;
+  options.skip_missed = FALSE;
+  options.closest = FALSE;
+  options.nosort = FALSE;
+  options.update = FALSE;
+  options.only_images = FALSE;
+  options.calibrate = FALSE;
+  options.quality_airmass = FALSE;
+  ACCEPT_ASTROM = FALSE;
+  FORCE_READ = FALSE;
+  TEXTMODE = FALSE;
+  SUBPIX = FALSE;
+  DUMP = NULL;
+
+  if (argc < 4) {
+    fprintf (stderr, "USAGE: loadsupercos [options] (surveys.csv) (plates.csv) (detections.bin) [..more files]\n");
+    exit (2);
+  }
+  return (options);
+}
+
+static void help () {
+
+  fprintf (stderr, "USAGE: loadsupercos [options] (surveys.csv) (plates.csv) (detections.bin) [..more files]\n");
+  fprintf (stderr, "  add data from Supercosmos to DVO\n\n");
+
+  fprintf (stderr, "  optional flags:\n");
+  fprintf (stderr, "  -only-match           	  : only add measurements to existing objects\n");
+  fprintf (stderr, "  -replace              	  : replace time/photcode measurements (no duplication)\n");
+  fprintf (stderr, "  -v                    	  : verbose mode\n");
+  fprintf (stderr, "  -help                 	  : this list\n");
+  fprintf (stderr, "  -h                    	  : this list\n\n");
+  exit (2);
+}
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/args_loadwise.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/args_loadwise.c	(revision 32994)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/args_loadwise.c	(revision 32994)
@@ -0,0 +1,90 @@
+# include "addstar.h"
+static void help (void);
+
+AddstarClientOptions args_loadwise (int argc, char **argv, AddstarClientOptions options) {
+  
+  int N;
+
+  /* check for help request */
+  if (get_argument (argc, argv, "-help") ||
+      get_argument (argc, argv, "-h")) {
+    help ();
+  }
+
+  // a global used by find_matches_refstars.c (value is 1 except for load2mass & loadwise)
+  NREFSTAR_GROUP = 4;
+
+  /*** check for command line options ***/
+
+  /* basic mode: image, list, refcat */
+  options.mode = M_REFCAT;
+
+  /* we do not allow a subset to be extracted -- all or nothing, babe */
+  UserPatch.Rmin = 0;
+  UserPatch.Rmax= 360;
+  UserPatch.Dmin = -90;
+  UserPatch.Dmax = +90;
+
+  /* only add to existing regions */
+  options.existing_regions = FALSE;
+  if ((N = get_argument (argc, argv, "-existing-regions"))) {
+    options.existing_regions = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* only add to existing objects */
+  options.only_match = FALSE;
+  if ((N = get_argument (argc, argv, "-only-match"))) {
+    options.only_match = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* replace measurement, don't duplicate (ref/cat only) */
+  options.replace = FALSE;
+  if ((N = get_argument (argc, argv, "-replace"))) {
+    options.replace = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* extra error messages */
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* other addstar options which cannot be used in load2mass */
+  options.photcode = 0;
+  options.timeref = 0; 
+  options.mosaic = FALSE;
+  options.skip_missed = FALSE;
+  options.closest = FALSE;
+  options.nosort = FALSE;
+  options.update = FALSE;
+  options.only_images = FALSE;
+  options.calibrate = FALSE;
+  options.quality_airmass = FALSE;
+  ACCEPT_ASTROM = FALSE;
+  FORCE_READ = FALSE;
+  TEXTMODE = FALSE;
+  SUBPIX = FALSE;
+  DUMP = NULL;
+
+  if (argc < 2) {
+    fprintf (stderr, "USAGE: load2mass [options] (wisefile) [..more files]\n");
+    exit (2);
+  }
+  return (options);
+}
+
+static void help () {
+
+  fprintf (stderr, "USAGE: loadwise [options] (wisefile) [..more files]\n");
+  fprintf (stderr, "  add data from WISE catalog to DVO\n\n");
+
+  fprintf (stderr, "  optional flags:\n");
+  fprintf (stderr, "  -only-match           	  : only add measurements to existing objects\n");
+  fprintf (stderr, "  -replace              	  : replace time/photcode measurements (no duplication)\n");
+  fprintf (stderr, "  -v                    	  : verbose mode\n");
+  fprintf (stderr, "  -help                 	  : this list\n");
+  fprintf (stderr, "  -h                    	  : this list\n\n");
+  exit (2);
+}
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/find_matches_refstars.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/find_matches_refstars.c	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/find_matches_refstars.c	(revision 32994)
@@ -13,5 +13,10 @@
   int Nsecfilt;
 
-  if ((NREFSTAR_GROUP != 1) && (NREFSTAR_GROUP != 3)) {
+  switch (NREFSTAR_GROUP) {
+    case 1:
+    case 3:
+    case 4:
+      break;
+    default:
       fprintf (stderr, "ERROR: NREFSTAR_GROUP NOT SET!\n");
       exit (1);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos.c	(revision 32994)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos.c	(revision 32994)
@@ -0,0 +1,73 @@
+# include "addstar.h"
+# include "supercos.h"
+
+/* This is the DVO program to upload Supercosmos detections into a DVO database.  It is modeled
+   on the loadwise program.  Like that case, it is expected to be run only rarely (once?).  The
+   supercosmos data are delivered as *.bin files.  The format of the file is described by the
+   SuperCosDetection structure.  It does not allow a subset of the sky to be uploaded; entire
+   Supercosmos files are loaded if supplied on the command line.
+
+   USAGE: loadsupercos -D CATDIR (catdir) (surveys.csv) (plates.csv) (scfile) [...more files]
+ */
+
+int main (int argc, char **argv) {
+
+  int i, status;
+  SkyTable *sky;
+  SkyList *skylist = NULL;
+  AddstarClientOptions options;
+  FITS_DB db;
+
+  // need to construct these options with args_loadWISE...
+  options = ConfigInit (&argc, argv);
+  options = args_loadsupercos (argc, argv, options);
+
+  // load the full sky description table:
+  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
+  SkyTableSetFilenames (sky, CATDIR, "cpt");
+  
+  // generate the subset matching the user-selected region
+  skylist = SkyListByPatch (sky, -1, &UserPatch);
+
+  // if we only match to existing (already populated) regions, limit the select to those regions:
+  if (options.existing_regions) {
+    SkyList *tmp;
+    tmp = SkyListExistingSubset (skylist, CATDIR);
+    SkyListFree (skylist);
+    skylist = tmp;
+  }
+
+  // load the images table (needed regardless)
+  int Nsurvey = 0;
+  Survey *survey = loadsupercos_survey(argv[1], &Nsurvey);
+
+  // load the images table (needed regardless)
+  int Nimage = 0;
+  Image *image = loadsupercos_plates(survey, Nsurvey, argv[2], &Nimage);
+
+  // attempt to open the existing images table.  if it does not exist, we will save the loaded images
+  db.mode   = dvo_catalog_catmode (CATMODE);
+  db.format = dvo_catalog_catformat (CATFORMAT);
+  status    = dvo_image_lock (&db, ImageCat, 3600.0, LCK_XCLD);  // shorter timeout?
+  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+
+  /* load or create the image table */
+  if (db.dbstate == LCK_EMPTY) {
+    if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
+    dvo_image_create (&db, GetZeroPoint());
+    dvo_image_addrows (&db, image, Nimage);
+    SetProtect (TRUE);
+    dvo_image_update (&db, VERBOSE);
+    SetProtect (FALSE);
+    if (VERBOSE) fprintf (stderr, "created image table for Supercosmos\n");
+  }
+
+  int *imlist = loadsupercos_image_index (image, Nimage);
+
+  for (i = 3; i < argc; i++) {
+      fprintf (stderr, "loading %s\n", argv[i]);
+      loadsupercos_rawdata (image, imlist, Nimage, skylist, argv[i], options);
+  }
+  exit (0);
+}  
+
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_ops.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_ops.c	(revision 32994)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_ops.c	(revision 32994)
@@ -0,0 +1,94 @@
+# include "addstar.h"
+# include "supercos.h"
+
+// this is defined in libohana/src/string.c but not generally exposed
+char *_parse_nextword_csv (char *string);
+
+
+int loadsupercos_getFilterInfo (char *line, char *emulsion, char *filterID) {
+    
+  int i;
+  int Nchar = 0;
+  char *p1 = line;
+
+  // emulsion is field 11, filterID is field 12
+  for (i = 1; i < 11; i++) {
+    p1 = _parse_nextword_csv (p1);
+    if (!p1) {
+      fprintf (stderr, "error parsing filter info for line %s\n", line);
+      abort();
+    }
+  }
+  if (*p1 == ',') {
+    fprintf (stderr, "missing emulsion info for line %s\n", line);
+    abort();
+  }
+
+  char *p2 = _parse_nextword_csv (p1);
+  if (!p2) {
+    fprintf (stderr, "error parsing filter info for line %s\n", line);
+    abort();
+  }
+  if (*p2 == ',') {
+    fprintf (stderr, "missing filter ID for line %s\n", line);
+    abort();
+  }
+
+  char *p3 = _parse_nextword_csv (p2);
+  if (!p3) {
+    fprintf (stderr, "error parsing filter info for line %s\n", line);
+    abort();
+  }
+
+  Nchar = p2 - p1 - 1;
+  strncpy (emulsion, p1, Nchar);
+  emulsion[Nchar] = 0;
+  stripwhite (emulsion);
+	
+  Nchar = p3 - p2 - 1;
+  strncpy (filterID, p2, Nchar);
+  filterID[Nchar] = 0;
+  stripwhite (filterID);
+	
+  return TRUE;
+}
+
+int loadsupercos_sortStars (Stars *tstars, int Ntstars) {
+
+# define SWAPFUNC(A,B){ Stars temp = tstars[A]; tstars[A] = tstars[B]; tstars[B] = temp; }
+# define COMPARE(A,B)(tstars[A].average.R < tstars[B].average.R)
+
+  OHANA_SORT (Ntstars, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+  
+  return TRUE;
+}
+
+int *loadsupercos_image_index (Image *image, int Nimage) {
+
+  int i, *index;
+
+  // note that externID is unsigned, so we cannot set max to -1
+  int maxIndex = 0;
+  for (i = 0; i < Nimage; i++) {
+    maxIndex = MAX (maxIndex, image[i].externID);
+  }
+
+  ALLOCATE (index, int, maxIndex + 1);
+  for (i = 0; i < maxIndex + 1; i++) {
+    index[i] = -1;
+  }
+
+  for (i = 0; i < Nimage; i++) {
+    int plateID = image[i].externID;
+    if (plateID < 0) abort();
+    if (plateID > maxIndex) abort();
+    if (index[plateID] != -1) abort();
+    index[plateID] = i;
+  }
+
+  return index;
+
+}
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_plates.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_plates.c	(revision 32994)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_plates.c	(revision 32994)
@@ -0,0 +1,344 @@
+# include "addstar.h"
+# include "supercos.h"
+
+Image *loadsupercos_plates (Survey *survey, int Nsurvey, char *filename, int *nimage) {
+
+  // these are fields read directly from the file.  some are supplied unchanged to images,
+  // some require adjustments.
+
+  double mjd, exptime, sidtime, RAo, DECo, aXmin, aXmax, aYmin, aYmax, alt, az, stepsize;
+  int nstar, survey_id, plate_id; 
+  char line[3000], emulsion[128], filterID[128];
+
+  FILE *f = fopen (filename, "r");
+  if (f == NULL) {
+    fprintf (stderr, "failed to open SuperCOSMOS plate table file %s\n", filename);
+    exit (1);
+  }
+  if (scan_line (f, line) == EOF) {
+    fprintf (stderr, "error reading header line of %s\n", filename);
+    exit (1);
+  }
+  if (scan_line (f, line) == EOF) {
+    fprintf (stderr, "error reading empty plate line of %s\n", filename);
+    exit (1);
+  }
+
+  int NIMAGE = 1000;
+  int Nimage = 0;
+  Image *image = NULL;
+
+  ALLOCATE (image, Image, NIMAGE);
+
+  int status = TRUE;
+  while (scan_line (f, line) != EOF) {
+  
+    iparse_csv (&plate_id, 1, line);
+    iparse_csv (&survey_id, 2, line);
+    if (survey_id >= Nsurvey) {
+      fprintf (stderr, "unknown survey %d\n", survey_id);
+      abort();
+    }
+
+    dparse_csv (&mjd, 28, line); // NOTE dparse is 1 counting (but 0 == 1?)
+    iparse_csv (&nstar, 106, line);
+    dparse_csv (&aXmin, 39, line);
+    dparse_csv (&aXmax, 40, line);
+    dparse_csv (&aYmin, 41, line);
+    dparse_csv (&aYmax, 42, line);
+
+    dparse_csv (&stepsize, 37, line); // microns / pixel
+    dparse_csv (&exptime, 21, line);
+    dparse_csv (&sidtime, 20, line);
+    dparse_csv (&RAo, 14, line);
+    dparse_csv (&DECo, 15, line);
+      
+    loadsupercos_getFilterInfo (line, emulsion, filterID);
+
+    altaz (&alt, &az, sidtime - RAo, DECo, survey[survey_id].latitude);
+
+    float scale = stepsize * survey[survey_id].plateScale / 1000.0 / 3600.0; // scale in degrees / pixel
+
+    image[Nimage].tzero = ohana_mjd_to_sec (mjd);
+    image[Nimage].nstar = nstar;
+    image[Nimage].secz = 1.0 / cos(RAD_DEG*alt);
+
+    image[Nimage].NX = 5.00 / scale;
+    image[Nimage].NY = 4.75 / scale;
+
+    image[Nimage].apmifit = 0.0;
+    image[Nimage].dapmifit = 0.0;
+    image[Nimage].Mcal = 0.0;
+    image[Nimage].dMcal = 0.0;
+    image[Nimage].Xm = 0.0;
+
+    char photname[128];
+    snprintf (photname, 128, "SCOS.%s.%s", emulsion, filterID);
+    image[Nimage].photcode = GetPhotcodeCodebyName (photname);
+    if (!image[Nimage].photcode) {
+      fprintf (stderr, "unknown photcode %s\n", photname);
+      fprintf (stderr, "line %d: %s\n", Nimage, line);
+      abort();
+    }
+    image[Nimage].exptime = exptime * 60.0;
+    image[Nimage].sidtime = sidtime; // XXX double check units
+    image[Nimage].latitude = survey[survey_id].latitude;
+
+    image[Nimage].RAo = RAo;
+    image[Nimage].DECo = DECo;
+    image[Nimage].Radius = NAN;
+    
+    snprintf (image[Nimage].name, 121, "SCOS.%02d.%06d", survey_id, plate_id);
+    image[Nimage].imageID = Nimage + 1;
+    image[Nimage].externID = plate_id;
+
+    image[Nimage].detection_limit = 0;
+    image[Nimage].saturation_limit = 0;
+    image[Nimage].cerror = 0;
+    image[Nimage].fwhm_x = 0;
+    image[Nimage].fwhm_y = 0;
+    image[Nimage].trate = 0.0;
+    image[Nimage].ccdnum = 0;
+    image[Nimage].flags = 0;
+    image[Nimage].parentID = 0;
+    image[Nimage].sourceID = 0;
+    image[Nimage].nLinkAstrom = 0;
+    image[Nimage].nLinkPhotom = 0;
+    image[Nimage].dXpixSys = NAN;
+    image[Nimage].dYpixSys = NAN;
+    image[Nimage].dMagSys = NAN;
+    image[Nimage].nFitAstrom = 0;
+    image[Nimage].nFitPhotom = 0;
+    image[Nimage].photom_map_id = 0;
+    image[Nimage].astrom_map_id = 0;
+
+
+    // for now, we define a fake coordinate system based on the boresite center
+    strcpy (image[Nimage].coords.ctype, "RA---TAN");
+    
+    image[Nimage].coords.crval1 = RAo;
+    image[Nimage].coords.crval2 = DECo;
+    
+    image[Nimage].coords.crpix1 = 0.5*image[Nimage].NX;
+    image[Nimage].coords.crpix2 = 0.5*image[Nimage].NY;
+    image[Nimage].coords.cdelt1 = image[Nimage].coords.cdelt2 = scale;
+
+    image[Nimage].coords.pc1_1 = 0.0;
+    image[Nimage].coords.pc1_2 = 1.0;
+    image[Nimage].coords.pc2_1 = 1.0;
+    image[Nimage].coords.pc2_2 = 0.0;
+
+    image[Nimage].coords.Npolyterms = 0;
+    memset (image[Nimage].coords.polyterms, 0, 2*7*sizeof(float));
+
+    Nimage ++;
+    CHECK_REALLOCATE (image, Image, NIMAGE, Nimage, 1000);
+  }
+  fclose (f);
+
+  if (!status) abort();
+
+  *nimage = Nimage;
+  return image;
+}
+
+/*** Image fields and mappings from supercosmos ***/
+
+// tzero : mjd [27]
+// nstar : objNum [105]
+// secz : need to get telescope lattitude & longitude numbers
+// NX : from aXmin, aXmax? [38,39]
+// NY : from aXmin, aXmax? [40,41]
+// apmifit : 0.0
+// dapmifit : 0.0
+// Mcal : 0.0
+// dMcal : 0.0
+// Xm : NAN
+// photcode : from filterID [11]
+// exptime : expLength (min) [20]
+// sidtime : lstObs [19]
+// latitude : (from survey_id) [01]
+// RAo : raPnt [13]
+// DECo : decPnt [14]
+// Radius : (calculate) 
+// DUMMY : 
+// name : generate from plateID? [00]
+// detection_limit : NAN
+// saturation_limit : NAN
+// cerror : NAN
+// fwhm_x : NAN
+// fwhm_y : 
+// trate : 
+// ccdnum : 
+// flags : 
+// imageID : 
+// parentID : 
+// externID : 
+// sourceID : 
+// nLinkAstrom : 
+// nLinkPhotom : 
+// dummy3 : 
+// dXpixSys : 
+// dYpixSys : 
+// dMagSys : 
+// nFitAstrom : 
+// nFitPhotom : 
+// photom_map_id : 
+// astrom_map_id : 
+
+/*** SuperCOSMOS plate table (downloaded from the SSA).  CSV file contains the following
+ * fields:
+ ***/
+
+//  1 plateID
+//  2 surveyID
+//  3 fieldID
+//  4 plateNum
+//  5 dateMeas
+//  6 timeMeas
+//  7 instrument
+//  8 softVersion
+//  9 operator
+// 10 scanMaterial
+// 11 emulsion
+// 12 filterID
+// 13 filterComm
+// 14 raPnt
+// 15 decPnt
+// 16 radecSys
+// 17 equinox
+// 18 eqTsys
+// 19 utDateObs
+// 20 lstObs
+// 21 expLength
+// 22 epoch
+// 23 domeTemp
+// 24 domePressure
+// 25 domeHumid
+// 26 waveEffect
+// 27 tropl
+// 28 mjd
+// 29 focusNX
+// 30 focusNY
+// 31 calType
+// 32 stepWdg
+// 33 nSteps
+// 34 orientation
+// 35 emulpos
+// 36 sosp
+// 37 stepsize
+// 38 scanlen
+// 39 aXmin
+// 40 aXmax
+// 41 aYmin
+// 42 aYmax
+// 43 xPnt
+// 44 yPnt
+// 45 areaCut
+// 46 apParam
+// 47 dbParam
+// 48 dbAmin
+// 49 dbAmax
+// 50 dbAcut
+// 51 dbLevel
+// 52 skySquare
+// 53 skyDefn
+// 54 skyFilter
+// 55 fThresh
+// 56 fSclen
+// 57 pCut
+// 58 starCat
+// 59 brightLim
+// 60 faintLim
+// 61 equinRef
+// 62 tSysRef
+// 63 maxIter
+// 64 rCritIni
+// 65 rCritAbs
+// 66 rCritRel
+// 67 rCritFin
+// 68 distType
+// 69 scdGrid
+// 70 raCol
+// 71 decCol
+// 72 raPMCol
+// 73 decPMCol
+// 74 plxCol
+// 75 rvCol
+// 76 magCol
+// 77 cEquinox
+// 78 cEqTSys
+// 79 cEpoch
+// 80 cEpTSys
+// 81 starsInit
+// 82 starsSelec
+// 83 starsUsed
+// 84 coeffs1
+// 85 coeffs2
+// 86 coeffs3
+// 87 coeffs4
+// 88 coeffs5
+// 89 coeffs6
+// 90 cubicDist
+// 91 obsRaPnt
+// 92 obsDecPnt
+// 93 shiftPnt
+// 94 areaMin
+// 95 areaMax
+// 96 magMin
+// 97 magMax
+// 98 ellipMin
+// 99 ellipMax
+//100 ellipMode
+//101 orientMode
+//102 ellipMed
+//103 orientMed
+//104 ellipMean
+//105 orientMean
+//106 objNum
+//107 nParents
+//108 lane1Count
+//109 lane2Count
+//110 lane3Count
+//111 lane4Count
+//112 lane5Count
+//113 lane6Count
+//114 lane7Count
+//115 lane8Count
+//116 lane9Count
+//117 lane10Count
+//118 lane11Count
+//119 lane12Count
+//120 lane13Count
+//121 lane14Count
+//122 lane15Count
+//123 lane16Count
+//124 lane17Count
+//125 lane18Count
+//126 lane19Count
+//127 lane20Count
+//128 lane21Count
+//129 lane22Count
+//130 lane23Count
+//131 lane24Count
+//132 lane25Count
+//133 lane26Count
+//134 lane27Count
+//135 lane28Count
+//136 lane29Count
+//137 lane30Count
+//138 astResidX
+//139 astResidY
+//140 nCalCoeffs
+//141 CalXK
+//142 gradient
+//143 getCal1
+//144 getCal2
+//145 getCal3
+//146 getCal4
+//147 getCal5
+//148 getCal6
+//149 ffZp
+//150 galZp
+//151 galGrad
+//152 directory
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_rawdata.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_rawdata.c	(revision 32994)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_rawdata.c	(revision 32994)
@@ -0,0 +1,216 @@
+# include "addstar.h"
+# include "supercos.h"
+
+# define NBYTE 228
+# define NRECORDS 1000
+# define DEBUG 0
+
+int loadsupercos_rawdata (Image *image, int *imlist, int Nimage, SkyList *skytable, char *filename, AddstarClientOptions options) {
+  
+  int i, j, verbose;
+  int Nstars, NSTARS, Nrecords;
+  
+  FILE *f;
+  char *buffer;
+
+  Detection *sstars;
+  Stars **stars, *tstars;
+  SkyList *skylist;
+  SkyRegion *region;
+  Catalog catalog;
+
+  // the data files are written as an integer number of 228 byte records
+  ALLOCATE (buffer, char, NRECORDS*NBYTE);
+
+  f = fopen (filename, "r");
+  if (f == NULL) Shutdown ("can't read the Supercosmos data file: %s", filename);
+
+  ALLOCATE (tstars, Stars, NRECORDS);
+  ALLOCATE (sstars, Detection, NRECORDS); 
+
+  double Rmin = 360.0;
+  double Rmax =   0.0;
+  double Dmin = +90.0;
+  double Dmax = -90.0;
+
+  while ((Nrecords = fread (buffer, NBYTE, NRECORDS, f)) != 0) {
+    if (Nrecords == -1) Shutdown ("error reading from raw file %s", filename);
+    if (DEBUG) fprintf (stderr, "read %d bytes (%d bytes x %d rows)", NBYTE * Nrecords, NBYTE, Nrecords);
+
+    if (VERBOSE) fprintf (stderr, "read .. ");
+
+    // extract the basic values from the buffer
+    for (i = 0; i < Nrecords; i++) {
+	sstars[i].objID    = *(int64_t *) &buffer[i*NBYTE + 0];
+	sstars[i].surveyID = *(int8_t *)  &buffer[i*NBYTE + 8];
+	sstars[i].plateID  = *(int32_t *) &buffer[i*NBYTE + 9];
+	sstars[i].ra       = *(double *)  &buffer[i*NBYTE + 33];
+	sstars[i].dec      = *(double *)  &buffer[i*NBYTE + 41];
+	sstars[i].xCen     = *(double *)  &buffer[i*NBYTE + 129];
+	sstars[i].yCen     = *(double *)  &buffer[i*NBYTE + 137];
+	sstars[i].aU       = *(float *)   &buffer[i*NBYTE + 145];
+	sstars[i].bU       = *(float *)   &buffer[i*NBYTE + 149];
+	sstars[i].thetaU   = *(int16_t *) &buffer[i*NBYTE + 154];
+	sstars[i].class    = *(int8_t *)  &buffer[i*NBYTE + 165];
+	sstars[i].quality  = *(int32_t *) &buffer[i*NBYTE + 204];
+	sstars[i].prfStat  = *(float *)   &buffer[i*NBYTE + 208];
+	sstars[i].prfMag   = *(float *)   &buffer[i*NBYTE + 212];
+	sstars[i].gMag     = *(float *)   &buffer[i*NBYTE + 216];
+	sstars[i].sMag     = *(float *)   &buffer[i*NBYTE + 220];
+    }
+
+    // convert to the Stars format
+    for (i = 0; i < Nrecords; i++) {
+	InitStar (&tstars[i]);
+	tstars[i].measure.Xccd = sstars[i].xCen;
+	tstars[i].measure.Yccd = sstars[i].yCen;
+
+	tstars[i].measure.M = sstars[i].prfMag;
+	tstars[i].measure.photFlags = sstars[i].class;
+
+	tstars[i].average.R = sstars[i].ra;
+	tstars[i].average.D = sstars[i].dec;
+
+	int Ni = imlist[sstars[i].plateID];
+	if (Ni == -1) abort();
+
+	// XXX fix these
+	tstars[i].measure.Mcal = 0.0;
+	tstars[i].measure.dt = image[Ni].exptime;
+
+	double sidtime = image[Ni].sidtime;
+	double latitude = image[Ni].latitude;
+	
+	double alt, az;
+	altaz (&alt, &az, sidtime - tstars[i].average.R, tstars[i].average.D, latitude);
+
+	tstars[i].measure.airmass = 1.0 / cos(alt);
+	tstars[i].measure.az = az;
+	tstars[i].measure.t = image[Ni].tzero;
+	tstars[i].measure.imageID = image[Ni].imageID;
+
+	tstars[i].measure.FWx = ToShortPixels(sstars[i].aU);
+	tstars[i].measure.FWy = ToShortPixels(sstars[i].bU);
+	tstars[i].measure.theta = ToShortDegrees(sstars[i].thetaU);
+
+	Rmin = MIN (Rmin, tstars[i].average.R);
+	Rmax = MAX (Rmax, tstars[i].average.R);
+	Dmin = MIN (Dmin, tstars[i].average.D);
+	Dmax = MAX (Dmax, tstars[i].average.D);
+    }
+
+    if (VERBOSE) fprintf (stderr, "scan %d stars (%10.6f - %10.6f, %10.6f - %10.6f) .. ", Nrecords, Rmin, Rmax, Dmin, Dmax);
+
+    // sort the tstars by RA
+    loadsupercos_sortStars (tstars, Nrecords);
+
+    // scan through the stars, loading the containing catalogs
+    // skip through table for unsaved stars
+    for (i = 0; i < Nrecords; i++) {
+      if (tstars[i].found != -1) continue;
+
+      // scan forward until we reach the UserPatch (stars sorted by RA)
+      if (tstars[i].average.R < UserPatch.Rmin) continue;
+      if (tstars[i].average.R > UserPatch.Rmax) break;
+      if (tstars[i].average.D < UserPatch.Dmin) continue;
+      if (tstars[i].average.D > UserPatch.Dmax) continue;
+
+      // identify the relevant catalog
+      skylist = SkyRegionByPoint_List (skytable, -1, tstars[i].average.R, tstars[i].average.D);
+      if (skylist[0].Nregions == 0) {
+	  SkyListFree (skylist);
+	  continue;
+      }
+      region = skylist[0].regions[0];
+      if (DEBUG) fprintf (stderr, "writing to %s\n", skylist[0].filename[0]);
+
+      // loop over stars in this WISE region that are also in this output region
+      Nstars = 0;
+      NSTARS = 1000;
+      ALLOCATE (stars, Stars *, NSTARS);
+
+      for (j = i; j < Nrecords; j++) {
+	if (tstars[j].found != -1) continue;
+
+	// check if in skyregion
+	if (tstars[j].average.R < region[0].Rmin) continue;
+	if (tstars[j].average.R > region[0].Rmax) break;
+	if (tstars[j].average.D < region[0].Dmin) continue;
+	if (tstars[j].average.D > region[0].Dmax) continue;
+	  
+	// check if in UserPatch
+	if (tstars[j].average.R < UserPatch.Rmin) continue;
+	if (tstars[j].average.R > UserPatch.Rmax) break;
+	if (tstars[j].average.D < UserPatch.Dmin) continue;
+	if (tstars[j].average.D > UserPatch.Dmax) continue;
+
+	stars[Nstars] = &tstars[j];
+	Nstars ++;
+
+	CHECK_REALLOCATE (stars, Stars *, NSTARS, Nstars, 1000);
+      }
+	  
+      if (DEBUG) fprintf (stderr, "selected %d stars (%10.6f - %10.6f, %10.6f - %10.6f)\n", Nstars, 
+			  region[0].Rmin, region[0].Rmax, region[0].Dmin, region[0].Dmax);
+
+      if (1) {
+	verbose = VERBOSE;
+	VERBOSE = FALSE;
+
+	// now we have all of the loaded stars in this catalog
+	catalog.filename = skylist[0].filename[0];
+	catalog.catformat = dvo_catalog_catformat (CATFORMAT);  // set the default catformat from config data
+	catalog.catmode   = dvo_catalog_catmode (CATMODE);      // set the default catmode from config data
+	catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+	catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
+
+	// an error exit status here is a significant error
+	if (!dvo_catalog_open (&catalog, skylist[0].regions[0], VERBOSE, "w")) {
+	  fprintf (stderr, "ERROR: failure to open/create catalog file %s\n", catalog.filename);
+	  exit (2);
+	}
+
+	// assume no input star matches an existing star 
+	// simply add to the existing table
+	// loadWISE_catalog (&catalog, stars, Nstars);
+	find_matches_refstars (skylist[0].regions[0], stars, Nstars, &catalog, options);
+	// loadWISE_catalog (&catalog, stars, Nstars);
+
+	dvo_catalog_save (&catalog, VERBOSE);
+	dvo_catalog_unlock (&catalog);
+	dvo_catalog_free (&catalog);
+	// free (catalog.filename);
+	// XXX don't free this! it points to an element of the skytable
+      }
+
+      SkyListFree (skylist);
+
+      free (stars);
+      VERBOSE = verbose;
+    }
+    if (VERBOSE) fprintf (stderr, "done\n");
+  }
+
+  if (VERBOSE) fprintf (stderr, "\n");
+  
+  free (tstars);
+  free (sstars);
+
+  fclose (f);
+  free (buffer);
+  return (TRUE);
+}
+
+/*
+  for each WISE file:
+  for each data block
+  generate a table of: R, D, byte, flag
+  for each unsaved star
+  find containing catalog
+  load catalog
+  find all contained stars
+  add to catalog
+  save catalog 
+  mark all contained stars
+*/
+
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_survey.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_survey.c	(revision 32994)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadsupercos_survey.c	(revision 32994)
@@ -0,0 +1,69 @@
+# include "addstar.h"
+# include "supercos.h"
+
+Survey *loadsupercos_survey (char *filename, int *nsurvey) {
+
+  char line[1024];
+
+  FILE *f = fopen (filename, "r");
+  if (f == NULL) {
+    fprintf (stderr, "failed to open SuperCOSMOS survey table file %s\n", filename);
+    exit (1);
+  }
+  if (scan_line (f, line) == EOF) {
+    fprintf (stderr, "error reading header line of %s\n", filename);
+    exit (1);
+  }
+
+  int NSURVEY = 20;
+  int Nsurvey = 0;
+  Survey *survey = NULL;
+  ALLOCATE (survey, Survey, NSURVEY);
+  while (scan_line (f, line) != EOF) {
+
+    iparse_csv (&survey[Nsurvey].survey_id,   1, line);
+    dparse_csv (&survey[Nsurvey].longitude,  10, line);
+    dparse_csv (&survey[Nsurvey].latitude,   11, line);
+    dparse_csv (&survey[Nsurvey].plateScale, 13, line); // arcsec / mm
+
+    if (Nsurvey != survey[Nsurvey].survey_id) {
+      fprintf (stderr, "NEED to SORT surveys\n");
+      abort();
+    }
+
+    Nsurvey ++;
+
+    CHECK_REALLOCATE (survey, Survey, NSURVEY, Nsurvey, 10);
+  }
+  fclose (f);
+
+  *nsurvey = Nsurvey;
+  return survey;
+}
+
+// fields in the survey.csv file (this could be read from the header line, but it is too much work)
+//   1 surveyID
+//   2 surveyName
+//   3 systemID
+//   4 fieldOfView
+//   5 decMin
+//   6 decMax
+//   7 numFields
+//   8 telescope
+//   9 telAperture
+//  10 telLong
+//  11 telLat
+//  12 telHeight
+//  13 plateScale
+//  14 colour
+//  15 waveMin
+//  16 waveMax
+//  17 waveEff
+//  18 magLimit
+//  19 epochMin
+//  20 epochMax
+//  21 epTsys
+//  22 equinox
+//  23 eqTsys
+//  24 surveyRef
+
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise.c	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise.c	(revision 32994)
@@ -2,16 +2,24 @@
 # include "WISE.h"
 
+/* This is the DVO program to upload WISE detections into a DVO database.
+   It is modeled on the load2mass program, but unlike that case, it is expected to be run only
+   rarely (once?).  The WISE data are delivered as *.bz2 files.  This program assumes the files
+   have been decompressed.  It does not allow a subset of the sky to be uploaded; entire WISE
+   files are loaded if supplied on the command line.
+
+   USAGE: loadwise -D CATDIR (catdir) (wisefile) [...more files]
+
+ */
+
 int main (int argc, char **argv) {
 
-  char *path;
   int i;
-  SkyTable *sky, *skyWISE;
+  SkyTable *sky;
   SkyList *skylist = NULL;
-  SkyList *overlap = NULL;
   AddstarClientOptions options;
 
   // need to construct these options with args_loadWISE...
   options = ConfigInit (&argc, argv);
-  options = args_loadWISE (argc, argv, options);
+  options = args_loadwise (argc, argv, options);
 
   // load the full sky description table:
@@ -30,23 +38,9 @@
   }
 
-  path = WISE_DIR_PRELIM;
-
-  // the accel.dat file has the raw filenames
-  // test if the file exists, or else try the .gz version
-  // XXX can we build an accelerator?
-  skyWISE = loadWISE_acc (path, "accel.dat");
-  
-  for (i = 0; i < skyWISE[0].Nregions; i++) {
-    // check if any of the skylist entries overlap this WISE catalog:
-    overlap = SkyListByBounds_List (skylist, -1, skyWISE[0].regions[i].Rmin, skyWISE[0].regions[i].Rmax, skyWISE[0].regions[i].Dmin, skyWISE[0].regions[i].Dmax);
-    if (overlap[0].Nregions == 0) {
-      SkyListFree (overlap);
-      continue;
-    }
-    
-    fprintf (stderr, "loading %s\n", skyWISE[0].filename[i]);
-    loadWISE_as_rawdata (overlap, skyWISE[0].filename[i], options);
-    SkyListFree (overlap);
+  for (i = 1; i < argc; i++) {
+      fprintf (stderr, "loading %s\n", argv[i]);
+      loadwise_prelim_rawdata (skylist, argv[i], options);
   }
   exit (0);
 }  
+
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_ops.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_ops.c	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_ops.c	(revision 32994)
@@ -1,9 +1,4 @@
 # include "addstar.h"
 # include "WISE.h"
-
-static int FilterSkip;
-static int TimeSkip;
-static int Qentry;
-static int Photcode;
 
 int getWISE_setup () {
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_prelim_rawdata.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_prelim_rawdata.c	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_prelim_rawdata.c	(revision 32994)
@@ -10,5 +10,5 @@
 # define DEBUG 0
 
-int loadWISE_as_rawdata (SkyList *skytable, char *filename, AddstarClientOptions options) {
+int loadwise_prelim_rawdata (SkyList *skytable, char *filename, AddstarClientOptions options) {
   
   int i, j, verbose;
@@ -159,5 +159,5 @@
 	stars[Nstars+3][0].average.R = tstars[j].R;
 	stars[Nstars+3][0].average.D = tstars[j].D;
-	getWISE_star_full (&stars[Nstars], &buffer[offset], Nbyte - offset);
+	loadwise_star_full (&stars[Nstars], &buffer[offset], Nbyte - offset);
 
 	tstars[j].flag = TRUE;
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_star_full.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_star_full.c	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/loadwise_star_full.c	(revision 32994)
@@ -6,18 +6,19 @@
 // fill in the data for a WISE quad star.  takes a pointer to the start of the line the
 // RA and DEC have already been set
-int getWISE_star_full (Stars **star, char *line, int Nmax) {
+int loadwise_star_full (Stars **star, char *line, int Nmax) {
 
   char *ptr;
-  double dMfull;
   double jd;
 
   if (line == NULL) Shutdown ("format error in WISE");
 
+  ptr = line;
+
   ptr = skipNbounds (ptr, '|', 3, Nmax); // skip: desig, ra, dec,
-  star[0][0].measure.FWx = ToShortPixels(strtod (ptr, NULL));
-  ptr = nextWISEfield (ptr); // sig_dec
-  star[0][0].measure.FWy = ToShortPixels(strtod (ptr, NULL));
-  ptr = nextWISEfield (ptr); // sig_radec
-  star[0][0].measure.theta = ToShortPixels(strtod (ptr, NULL));
+  star[0][0].measure.FWx = ToShortPixels(strtod (ptr, NULL)); // sig_ra
+  ptr = nextWISEfield (ptr);
+  star[0][0].measure.FWy = ToShortPixels(strtod (ptr, NULL)); // sig_dec
+  ptr = nextWISEfield (ptr);
+  star[0][0].measure.theta = ToShortPixels(strtod (ptr, NULL)); // sig_radec
 
   star[1][0].measure.FWx   = star[0][0].measure.FWx;
@@ -31,29 +32,29 @@
   star[3][0].measure.theta = star[0][0].measure.theta;
 
-  ptr = skipNbounds (ptr, '|', 10, Nmax); // skip: glon, glat, elon, elat, wx, wy, cntr, source_id, coadd_id, src
+  ptr = skipNbounds (ptr, '|', 11, Nmax); // skip: sig_radec, glon, glat, elon, elat, wx, wy, cntr, source_id, coadd_id, src
 
   // W1
-  star[0][0].measure.M  = strtod (ptr, NULL);
-  ptr = nextWISEfield (ptr); // j_cmsig
-  star[0][0].measure.dM = strtod (ptr, NULL);
-  ptr = skipNbounds (ptr, '|', 2, Nmax); // skip: w1snr, w1rchi2
+  star[0][0].measure.M  = strtod (ptr, NULL); // w1mpro
+  ptr = nextWISEfield (ptr);
+  star[0][0].measure.dM = strtod (ptr, NULL); // w1sigmpro
+  ptr = skipNbounds (ptr, '|', 3, Nmax); // skip: w1sigmpro, w1snr, w1rchi2
 
   // W2
-  star[1][0].measure.M  = strtod (ptr, NULL);
-  ptr = nextWISEfield (ptr); // j_cmsig
-  star[1][0].measure.dM = strtod (ptr, NULL);
-  ptr = skipNbounds (ptr, '|', 2, Nmax); // skip: w2snr, w2rchi2
+  star[1][0].measure.M  = strtod (ptr, NULL); // w1mpro
+  ptr = nextWISEfield (ptr);
+  star[1][0].measure.dM = strtod (ptr, NULL); // w1sigmpro
+  ptr = skipNbounds (ptr, '|', 3, Nmax); // skip: w2sigmpro, w2snr, w2rchi2
 
   // W3
-  star[2][0].measure.M  = strtod (ptr, NULL);
-  ptr = nextWISEfield (ptr); // j_cmsig
-  star[2][0].measure.dM = strtod (ptr, NULL);
-  ptr = skipNbounds (ptr, '|', 2, Nmax); // skip: w3snr, w3rchi2
+  star[2][0].measure.M  = strtod (ptr, NULL); // w1mpro
+  ptr = nextWISEfield (ptr);
+  star[2][0].measure.dM = strtod (ptr, NULL); // w1sigmpro
+  ptr = skipNbounds (ptr, '|', 3, Nmax); // skip: w3sigmpro, w3snr, w3rchi2
 
   // W4
-  star[3][0].measure.M  = strtod (ptr, NULL);
-  ptr = nextWISEfield (ptr); // j_cmsig
-  star[3][0].measure.dM = strtod (ptr, NULL);
-  ptr = skipNbounds (ptr, '|', 2, Nmax); // skip: w4snr, w4rchi2
+  star[3][0].measure.M  = strtod (ptr, NULL); // w1mpro
+  ptr = nextWISEfield (ptr);
+  star[3][0].measure.dM = strtod (ptr, NULL); // w1sigmpro
+  ptr = skipNbounds (ptr, '|', 3, Nmax); // skip: w4sigmpro, w4snr, w4rchi2
 
   star[0][0].measure.photFlags = 0;
@@ -67,5 +68,5 @@
   setWISE_cc_flag (star[2], ptr[2]); // cc_flg
   setWISE_cc_flag (star[3], ptr[2]); // cc_flg
-  ptr = skipNbounds (ptr, '|', 2, Nmax); // skip: ext_flg, var_flg
+  ptr = skipNbounds (ptr, '|', 3, Nmax); // skip: cc_flags, ext_flg, var_flg
 
   setWISE_ph_qual (star[0], ptr[0]); // ph_qual
@@ -73,5 +74,4 @@
   setWISE_ph_qual (star[2], ptr[2]); // ph_qual
   setWISE_ph_qual (star[3], ptr[3]); // ph_qual
-  ptr = skipNbounds (ptr, '|', 2, Nmax); // skip: ext_flg, var_flg
 
   // setWISE_rd_flag (star[0], ptr[0]);
@@ -125,15 +125,19 @@
   // setWISE_use_flag (star[2], ptr[0]);
 
-  star[0][0].measure.photcode  = TM_J;
+  star[0][0].measure.photcode  = WISE_W1;
   star[0][0].measure.detID   = 0;
   star[0][0].measure.imageID = 0;
 
-  star[1][0].measure.photcode  = TM_H;
+  star[1][0].measure.photcode  = WISE_W2;
   star[1][0].measure.detID   = 0;
   star[1][0].measure.imageID = 0;
 
-  star[2][0].measure.photcode  = TM_K;
+  star[2][0].measure.photcode  = WISE_W3;
   star[2][0].measure.detID   = 0;
   star[2][0].measure.imageID = 0;
+
+  star[3][0].measure.photcode  = WISE_W4;
+  star[3][0].measure.detID   = 0;
+  star[3][0].measure.imageID = 0;
 
   return TRUE;
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/parse_time.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/parse_time.c	(revision 32993)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/parse_time.c	(revision 32994)
@@ -145,2 +145,23 @@
 
 }
+
+// ha/dec -> alt/az
+int altaz (double *alt, double *az, double ha, double dec, double latitude) {
+
+  double sind, sinh, cosh;
+
+  sind = dSIN (dec) * dSIN (latitude) + dCOS (dec) * dCOS (ha) * dCOS (latitude);
+  *alt  = DEG_RAD * asin (sind);
+
+  sinh = - dCOS (dec) * dSIN (ha);
+  cosh =   dSIN (dec) * dCOS (latitude) - dCOS (dec) * dCOS (ha) * dSIN (latitude);
+
+  *az = DEG_RAD * atan2 (sinh, cosh);
+
+  // I may need to use the parallactic angle to get the right plate rotation...
+  // sinh = -dCOS(az) * dSIN(alt) * dSIN(ha) * dSIN(latitude) + dSIN(az) * dSIN(alt) * dCOS(ha) - dSIN(ha) * dCOS(alt) * dCOS(latitude);
+  // cosh = -dSIN(az) * dSIN(ha) * dSIN(latitude) - dCOS(az) * dCOS(ha);
+  // rot = -DEG_RAD * atan2 (sinh, cosh);
+  return TRUE;
+}
+
