Index: /branches/eam_branches/ipp-20140904/Ohana/src/addstar/src/args_loadstarpar.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/addstar/src/args_loadstarpar.c	(revision 37394)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/addstar/src/args_loadstarpar.c	(revision 37394)
@@ -0,0 +1,99 @@
+# include "addstar.h"
+# include "WISE.h"
+
+static void help (void);
+
+AddstarClientOptions args_loadstarpar (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
+  NREFSTAR_GROUP = X;
+
+  /*** check for command line options ***/
+
+  /* basic mode: image, list, refcat */
+  options.mode = ADDSTAR_MODE_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);
+  }
+
+  /* load the prelim data dump */
+  ALLSKY = TRUE;
+  if ((N = get_argument (argc, argv, "-prelim"))) {
+    ALLSKY = FALSE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* other addstar options which cannot be used in loadstarpar */
+  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: loadstarpar [options] (wisefile) [..more files]\n");
+    exit (2);
+  }
+  return (options);
+}
+
+static void help () {
+
+  fprintf (stderr, "USAGE: loadstarpar [options] (file) [..more files]\n");
+  fprintf (stderr, "  add data from stellar parameter file 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-20140904/Ohana/src/addstar/src/loadstarpar.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/addstar/src/loadstarpar.c	(revision 37393)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/addstar/src/loadstarpar.c	(revision 37394)
@@ -3,11 +3,10 @@
 
 /* This is the DVO program to upload Greg Green's stellar parametersq 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.
+   It is modeled on the loadwise program and is expected to be run only rarely (once?).
+   The stellar parameter data are delivered as *.fits files.  It does not allow a subset
+   of the sky to be uploaded; entire stellar parameter files are loaded if supplied on the
+   command line.
 
-   USAGE: loadwise -D CATDIR (catdir) (wisefile) [...more files]
-
+   USAGE: loadstarpar -D CATDIR (catdir) (file.fits) [...more files]
  */
 
@@ -19,10 +18,10 @@
   AddstarClientOptions options;
 
-  // need to construct these options with args_loadWISE...
+  // need to construct these options with args_loadstarpar...
   options = ConfigInit (&argc, argv);
-  options = args_loadwise (argc, argv, options);
+  options = args_loadstarpar (argc, argv, options);
 
-  // load the full sky description table:
-  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
+  // load the full sky description table (dvodb must exist)
+  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, FALSE, SKY_DEPTH, VERBOSE);
   SkyTableSetFilenames (sky, CATDIR, "cpt");
   
@@ -40,5 +39,5 @@
   for (i = 1; i < argc; i++) {
       fprintf (stderr, "loading %s\n", argv[i]);
-      loadwise_rawdata (skylist, argv[i], options, ALLSKY);
+      loadstarpar_rawdata (skylist, argv[i], options, ALLSKY);
   }
   exit (0);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/addstar/src/loadstarpar_rawdata.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/addstar/src/loadstarpar_rawdata.c	(revision 37394)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/addstar/src/loadstarpar_rawdata.c	(revision 37394)
@@ -0,0 +1,244 @@
+# include "addstar.h"
+# include "WISE.h"
+
+/* WISE raw data tables are not fixed bytes per row.  split lines at the RETURN char.
+ * handle fractional lines at the end of each read block
+ */
+
+# define GET_COLUMN(OUT,NAME,TYPE) \
+  TYPE *OUT = gfits_get_bintable_column_data (&theader, &ftable, NAME, type, &Nrow, &Ncol); \
+  myAssert (!strcmp(type, #TYPE), "wrong column type");
+
+int loadstarpar_rawdata (SkyList *skytable, char *filename, AddstarClientOptions options, int ALLSKY) {
+  
+  int i, j, verbose;
+  int Nstars, NSTARS, Ntstars, NTSTARS;
+  int Nbyte, Nextra, offset;
+
+  double Rmin, Rmax, Dmin, Dmax;
+
+  FILE *f;
+  char *buffer, *p, *q;
+
+  Stars **stars; // this is an array of pointers to be consistent with input to find_match_refstars
+  WISE_Stars *tstars;
+  SkyList *skylist;
+  SkyRegion *region;
+  Catalog catalog;
+
+  // read in the full FITS files ('cause I don't have a partial read option)
+  f = fopen (filename, "r");
+  if (f == NULL) Shutdown ("can't read stellar parameter file: %s", filename);
+
+  Header header;
+  Matrix matrix;
+  Header theader;
+  FTable ftable;
+  
+  // load in PHU segment (ignore)
+  if (!gfits_fread_header (f, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read image subset header\n");
+    fclose (f);
+    return NULL;
+  }
+  if (!gfits_fread_matrix (f, &matrix, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read image subset matrix\n");
+    gfits_free_header (&header);
+    fclose (f);
+    return NULL;
+  }
+
+  ftable.header = &theader;
+
+  // load data for this header 
+  if (!gfits_load_header (f, &theader)) {
+    fclose (f);
+    return NULL;
+  }
+  if (!gfits_fread_ftable_data (f, &ftable, FALSE)) {
+    fclose (f);
+    return (NULL);
+  }
+  fclose (f);
+
+  // we read the entire block of data, then extract the columns, then set the image structure values.
+  // I free the FITS table data after extracting the colums to avoid having 3 copies in memory.
+
+  char type[16];
+
+  GET_COLUMN (glat, "l", float);
+  GET_COLUMN (glon, "b", float);
+
+  // free the memory associated with the FITS files
+  gfits_free_header (&header);
+  gfits_free_matrix (&matrix);
+  gfits_free_header (&theader);
+  gfits_free_table (&ftable);
+
+  Rmin = 360.0;
+  Rmax = 360.0;
+  Dmin = 360.0;
+  Dmax = 360.0;
+
+  for (i = 0; i < Nrow; i++) {
+    galactic_to_celestial (&R, &D, glat[i], glon[i]);
+
+    Rmin = MIN (Rmin, R);
+    Rmax = MAX (Rmax, R);
+    Dmin = MIN (Dmin, D);
+    Dmax = MAX (Dmax, D);
+
+      Ntstars ++;
+      CHECK_REALLOCATE (tstars, WISE_Stars, NTSTARS, Ntstars, 10000);
+  }
+
+    // sort the tstars by RA
+    getWISE_sortStars (tstars, Ntstars);
+
+    // scan through the stars, loading the containing catalogs
+    // skip through table for unsaved stars
+    for (i = 0; i < Ntstars; i++) {
+      if (tstars[i].flag) continue;
+
+      // scan forward until we read the UserPatch
+      if (tstars[i].R < UserPatch.Rmin) continue;
+      if (tstars[i].R > UserPatch.Rmax) break;
+      if (tstars[i].D < UserPatch.Dmin) continue;
+      if (tstars[i].D > UserPatch.Dmax) continue;
+
+      // identify the relevant catalog
+      skylist = SkyRegionByPoint_List (skytable, -1, tstars[i].R, tstars[i].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]);
+
+      // collect array of (Stars *) stars in a new output catalog
+      Nstars = 0;
+      NSTARS = 3000;
+      ALLOCATE (stars, Stars *, NSTARS);
+
+      // loop over stars in this WISE region that are also in this output region
+      for (j = i; j < Ntstars; j++) {
+	if (tstars[j].flag) continue;
+
+	// check if in skyregion
+	if (tstars[j].R < region[0].Rmin) continue;
+	if (tstars[j].R > region[0].Rmax) break;
+	if (tstars[j].D < region[0].Dmin) continue;
+	if (tstars[j].D > region[0].Dmax) continue;
+	  
+	// check if in UserPatch
+	if (tstars[j].R < UserPatch.Rmin) continue;
+	if (tstars[j].R > UserPatch.Rmax) break;
+	if (tstars[j].D < UserPatch.Dmin) continue;
+	if (tstars[j].D > UserPatch.Dmax) continue;
+	  
+	offset = tstars[j].offset;
+
+	ALLOCATE (stars[Nstars+0], Stars, 1);
+	ALLOCATE (stars[Nstars+1], Stars, 1);
+	ALLOCATE (stars[Nstars+2], Stars, 1);
+	ALLOCATE (stars[Nstars+3], Stars, 1);
+
+	InitStar (stars[Nstars+0]);
+	InitStar (stars[Nstars+1]);
+	InitStar (stars[Nstars+2]);
+	InitStar (stars[Nstars+3]);
+
+	stars[Nstars+0][0].average.R = tstars[j].R;
+	stars[Nstars+0][0].average.D = tstars[j].D;
+	stars[Nstars+1][0].average.R = tstars[j].R;
+	stars[Nstars+1][0].average.D = tstars[j].D;
+	stars[Nstars+2][0].average.R = tstars[j].R;
+	stars[Nstars+2][0].average.D = tstars[j].D;
+	stars[Nstars+3][0].average.R = tstars[j].R;
+	stars[Nstars+3][0].average.D = tstars[j].D;
+	
+	if (ALLSKY) {
+	  loadwise_star_allsky (&stars[Nstars], &buffer[offset], Nbyte - offset);
+	} else {
+	  loadwise_star_prelim (&stars[Nstars], &buffer[offset], Nbyte - offset);
+	}
+
+	tstars[j].flag = TRUE;
+
+	Nstars += 4;
+	if (Nstars >= NSTARS - 4) {
+	  NSTARS += 4000;
+	  REALLOCATE (stars, Stars *, NSTARS);
+	}
+      }
+
+      if (!Nstars) {
+	free (stars);
+	continue;
+      }
+
+      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);
+      for (j = 0; j < Nstars; j++) free (stars[j]);
+      free (stars);
+      VERBOSE = verbose;
+    }
+    free (tstars);
+    if (VERBOSE) fprintf (stderr, "done\n");
+
+    // at end, p points at the start of last, partial line
+    if (Nextra) memmove (buffer, p, Nextra);
+  }
+
+  if (VERBOSE) fprintf (stderr, "\n");
+  
+  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
+*/
+
