Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/Makefile
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/Makefile	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/Makefile	(revision 38268)
@@ -29,4 +29,5 @@
 loadICRF_client : $(BIN)/loadICRF_client.$(ARCH)
 loadwise     : $(BIN)/loadwise.$(ARCH)
+loadtycho    : $(BIN)/loadtycho.$(ARCH)
 dumpskycells : $(BIN)/dumpskycells.$(ARCH)
 findskycell  : $(BIN)/findskycell.$(ARCH)
@@ -35,7 +36,7 @@
 mkcmf        : $(BIN)/mkcmf.$(ARCH)
 
-all: addstar addstar_client sedstar load2mass loadstarpar loadstarpar_client loadICRF loadICRF_client skycells mkcmf loadwise loadsupercos dumpskycells findskycell
-
-INSTALL = addstar addstar_client sedstar load2mass loadstarpar loadstarpar_client loadICRF loadICRF_client skycells mkcmf loadwise loadsupercos dumpskycells findskycell
+all: addstar addstar_client sedstar load2mass loadstarpar loadstarpar_client loadICRF loadICRF_client skycells mkcmf loadwise loadtycho loadsupercos dumpskycells findskycell
+
+INSTALL = addstar addstar_client sedstar load2mass loadstarpar loadstarpar_client loadICRF loadICRF_client skycells mkcmf loadwise loadtycho loadsupercos dumpskycells findskycell
 
 # I need to fix the client/server version of addstar now that I have dropped Stars
@@ -49,5 +50,4 @@
 $(SRC)/SkyListForStars.$(ARCH).o \
 $(SRC)/SkyRegionUtils.$(ARCH).o \
-$(SRC)/SkyTableFromTychoIndex.$(ARCH).o \
 $(SRC)/addstar.$(ARCH).o \
 $(SRC)/addstar_create_ID.$(ARCH).o \
@@ -65,5 +65,4 @@
 $(SRC)/find_subset.$(ARCH).o \
 $(SRC)/getgsc.$(ARCH).o \
-$(SRC)/gettycho.$(ARCH).o \
 $(SRC)/getusno.$(ARCH).o \
 $(SRC)/getusnob.$(ARCH).o \
@@ -127,5 +126,4 @@
 $(SRC)/SkyListForStars.$(ARCH).o \
 $(SRC)/SkyRegionUtils.$(ARCH).o \
-$(SRC)/SkyTableFromTychoIndex.$(ARCH).o \
 $(SRC)/SocketOps.$(ARCH).o \
 $(SRC)/UpdateDatabase_Image.$(ARCH).o \
@@ -143,5 +141,4 @@
 $(SRC)/find_subset.$(ARCH).o \
 $(SRC)/getgsc.$(ARCH).o \
-$(SRC)/gettycho.$(ARCH).o \
 $(SRC)/getusno.$(ARCH).o \
 $(SRC)/getusnob.$(ARCH).o \
@@ -176,5 +173,4 @@
 $(SRC)/getusno.$(ARCH).o \
 $(SRC)/getusnob.$(ARCH).o \
-$(SRC)/gettycho.$(ARCH).o \
 $(SRC)/greference.$(ARCH).o \
 $(SRC)/in_image.$(ARCH).o \
@@ -188,5 +184,4 @@
 $(SRC)/edge_check.$(ARCH).o \
 $(SRC)/opening_angle.$(ARCH).o \
-$(SRC)/SkyTableFromTychoIndex.$(ARCH).o \
 $(SRC)/ListenClients_Thread.$(ARCH).o \
 $(SRC)/NewImage_Thread.$(ARCH).o \
@@ -334,4 +329,19 @@
 $(SRC)/psps_ids.$(ARCH).o
 
+LOAD-TYCHO = \
+$(SRC)/loadtycho.$(ARCH).o \
+$(SRC)/loadtycho_rawdata.$(ARCH).o \
+$(SRC)/loadtycho_ops.$(ARCH).o \
+$(SRC)/args_loadtycho.$(ARCH).o \
+$(SRC)/ConfigInit.$(ARCH).o \
+$(SRC)/Shutdown.$(ARCH).o \
+$(SRC)/find_matches_closest_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 \
@@ -410,4 +420,5 @@
 $(BIN)/load2mass.$(ARCH)      : $(LOAD-2MASS)
 $(BIN)/loadwise.$(ARCH)       : $(LOAD-WISE)
+$(BIN)/loadtycho.$(ARCH)      : $(LOAD-TYCHO)
 $(BIN)/loadstarpar.$(ARCH)    : $(LOAD-STARPAR)
 $(BIN)/loadstarpar_client.$(ARCH) : $(LOAD-STARPAR-CLIENT)
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/include/addstar.h
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/include/addstar.h	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/include/addstar.h	(revision 38268)
@@ -220,5 +220,4 @@
 Stars     *greference             PROTO((char *Refcat, SkyRegion *catstats, int photcode, unsigned int *nstars));
 Stars     *getgsc                 PROTO((SkyRegion *patch, unsigned int *NSTARS));
-Stars     *gettycho               PROTO((SkyRegion *catstats, int photcode, unsigned int *Nstars));
 Stars     *getusno                PROTO((SkyRegion *catstats, int photcode, unsigned int *Nstars));
 Stars     *getusnob               PROTO((SkyRegion *catstats, int photcode, unsigned int *Nstars));
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/include/tycho.h
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/include/tycho.h	(revision 38268)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/include/tycho.h	(revision 38268)
@@ -0,0 +1,17 @@
+
+# define NGROUP 6
+
+typedef struct {
+  double R, D;
+  Average average;
+  Measure measure[NGROUP];
+  int flag;
+} Tycho_Stars;
+
+AddstarClientOptions args_loadtycho (int *argc, char **argv, AddstarClientOptions options);
+
+int loadtycho_rawdata (SkyList *skytable, char *filename, AddstarClientOptions options);
+
+int gettycho_setup ();
+int gettycho_sortStars (Tycho_Stars *tstars, int Ntstars);
+int gettycho_star (Tycho_Stars *star, char *line);
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/FilterStars.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/FilterStars.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/FilterStars.c	(revision 38268)
@@ -43,4 +43,6 @@
     XY_to_RD (&stars[N].average.R, &stars[N].average.D, stars[N].measure.Xccd, stars[N].measure.Yccd, &image[0].coords);
     stars[N].average.R = ohana_normalize_angle (stars[N].average.R);
+    stars[N].measure.R = stars[N].average.R;
+    stars[N].measure.D = stars[N].average.D;
 
     stars[N].measure.photcode = image[0].photcode;
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/ReadStarsSDSS.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/ReadStarsSDSS.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/ReadStarsSDSS.c	(revision 38268)
@@ -155,4 +155,6 @@
       stars[N].average.dD        = NAN;
 
+      stars[N].measure.R         = stars[N].average.R;
+      stars[N].measure.D         = stars[N].average.D;
       stars[N].measure.Xccd      = colc[N];
       stars[N].measure.Yccd      = rowc[N];
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/ReadStarsUKIRT.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/ReadStarsUKIRT.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/ReadStarsUKIRT.c	(revision 38268)
@@ -169,4 +169,6 @@
     stars[i].average.dD        = NAN;
 
+    stars[i].measure.R         = stars[i].average.R;
+    stars[i].measure.D         = stars[i].average.D;
     stars[i].measure.Xccd      = X_coordinate[i];
     stars[i].measure.Yccd      = Y_coordinate[i];
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/args_loadtycho.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/args_loadtycho.c	(revision 38268)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/args_loadtycho.c	(revision 38268)
@@ -0,0 +1,94 @@
+# include "addstar.h"
+# include "tycho.h"
+static void help (void);
+
+AddstarClientOptions args_loadtycho (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_closest_refstars.c (value is 1 except for loadwise & loadtycho)
+  NREFSTAR_GROUP = NGROUP;
+
+  /*** check for command line options ***/
+
+  /* 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);
+  }
+  /* accept proper-motion data from reference */
+  ACCEPT_MOTION = FALSE;
+  if ((N = get_argument (*argc, argv, "-accept-motion"))) {
+    ACCEPT_MOTION = 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 loadtycho */
+  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: loadtycho [options] (tychofile) [..more files]\n");
+    exit (2);
+  }
+  return (options);
+}
+
+static void help () {
+
+  fprintf (stderr, "USAGE: loadtycho [options] (tychofile) [..more files]\n");
+  fprintf (stderr, "  add data from Tycho 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-20150419/Ohana/src/addstar/src/find_matches.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches.c	(revision 38268)
@@ -154,6 +154,4 @@
 
       // the following measure elements cannot be set until here:
-      catalog[0].measure[Nmeas].R        = stars[N].average.R;
-      catalog[0].measure[Nmeas].D        = stars[N].average.D;
       catalog[0].measure[Nmeas].dbFlags  = 0;
       catalog[0].measure[Nmeas].averef   = n; // this must be an absolute sequence number, if partial average is loaded 
@@ -274,8 +272,6 @@
 
       // the following measure elements cannot be set until here:
-      catalog[0].measure[Nmeas].R       	= stars[i].average.R;
-      catalog[0].measure[Nmeas].D       	= stars[i].average.D;
-      catalog[0].measure[Nmeas].dbFlags 	= 0;
-      catalog[0].measure[Nmeas].averef   	= Nave; // XXX EAM : must be absolute Nave if partial read
+      catalog[0].measure[Nmeas].dbFlags   = 0;
+      catalog[0].measure[Nmeas].averef    = Nave; // XXX EAM : must be absolute Nave if partial read
       catalog[0].measure[Nmeas].objID     = catalog[0].average[Nave].objID;
       catalog[0].measure[Nmeas].catID     = catalog[0].catID;
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_closest.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_closest.c	(revision 38268)
@@ -189,7 +189,4 @@
 
     // measure now carries R,D (not dR,dD) 
-    // note that ReadStarsFITS does not set measure.R,D and average.R,D
-    catalog[0].measure[Nmeas].R        = stars[N].average.R;
-    catalog[0].measure[Nmeas].D        = stars[N].average.D;
     catalog[0].measure[Nmeas].dbFlags  = 0;
     catalog[0].measure[Nmeas].averef   = n;
@@ -309,6 +306,4 @@
 
       // the following measure elements cannot be set until here:
-      catalog[0].measure[Nmeas].R        = stars[i].average.R;
-      catalog[0].measure[Nmeas].D        = stars[i].average.D;
       catalog[0].measure[Nmeas].dbFlags  = 0;
       catalog[0].measure[Nmeas].averef   = Nave;
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_closest_refstars.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_closest_refstars.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_closest_refstars.c	(revision 38268)
@@ -1,3 +1,12 @@
 # include "addstar.h"
+
+/* differences with find_matches_closest():
+ * accepts Stars **stars (vs Stars *stars)
+ * NREFSTAR_GROUP vs NSTAR_GROUP
+ * does not update lensing fields
+ * allows option to replace existing entries
+ * allows multiple photcodes
+
+ */
 
 int find_matches_closest_refstars (SkyRegion *region, Stars **stars, unsigned int NstarsIn, Catalog *catalog, AddstarClientOptions options) {
@@ -13,7 +22,13 @@
   int Nsecfilt;
 
-  if ((NREFSTAR_GROUP != 1) && (NREFSTAR_GROUP != 3)) {
-    fprintf (stderr, "ERROR: NREFSTAR_GROUP NOT SET!\n");
-    exit (1);
+  switch (NREFSTAR_GROUP) {
+    case 1:
+    case 3:
+    case 4:
+    case 6:
+      break;
+    default:
+      fprintf (stderr, "ERROR: NREFSTAR_GROUP NOT SET!\n");
+      exit (1);
   }
 
@@ -123,4 +138,10 @@
       j++;
       continue;
+    }
+
+    if (stars[N1[i]][0].found != -1) {
+        /* this star has already been assigned to an object in this or another catalog */
+        i++;
+        continue;
     }
 
@@ -173,7 +194,4 @@
     catalog[0].measure[Nmeas]          = stars[N][0].measure;
 
-    /** *** dR,dD now in arcsec *** **/
-    catalog[0].measure[Nmeas].R        = stars[N][0].average.R;
-    catalog[0].measure[Nmeas].D        = stars[N][0].average.D;
     catalog[0].measure[Nmeas].dbFlags  = 0;
     catalog[0].measure[Nmeas].averef   = n;
@@ -201,4 +219,6 @@
     // we can choose to accept the proper-motion and parallax from the reference catalog
     if (ACCEPT_MOTION) {
+      catalog[0].average[n].dR         = stars[N][0].average.dR;
+      catalog[0].average[n].dD         = stars[N][0].average.dD;
       catalog[0].average[n].uR         = stars[N][0].average.uR;
       catalog[0].average[n].uD         = stars[N][0].average.uD;
@@ -207,4 +227,5 @@
       catalog[0].average[n].P          = stars[N][0].average.P;
       catalog[0].average[n].dP         = stars[N][0].average.dP;
+      catalog[0].average[n].Tmean      = stars[N][0].average.Tmean;
     }
 
@@ -273,4 +294,5 @@
       catalog[0].average[Nave].P     	   = stars[N][0].average.P;
       catalog[0].average[Nave].dP    	   = stars[N][0].average.dP;
+      catalog[0].average[Nave].Tmean   	   = stars[N][0].average.Tmean;
     }
 
@@ -285,7 +307,4 @@
 
       catalog[0].measure[Nmeas]          = stars[N][0].measure;
-
-      catalog[0].measure[Nmeas].R       = catalog[0].average[Nave].R;
-      catalog[0].measure[Nmeas].D       = catalog[0].average[Nave].D;
       catalog[0].measure[Nmeas].dbFlags  = 0;
       catalog[0].measure[Nmeas].averef   = Nave;
@@ -311,4 +330,5 @@
   // XXX allow for unsorted output?
   catalog[0].measure = sort_measure (catalog[0].average, Nave, catalog[0].measure, Nmeas, next_meas);
+  catalog[0].sorted = TRUE;
 
   /* note stars which have been found in this catalog */
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_refstars.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_refstars.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/find_matches_refstars.c	(revision 38268)
@@ -138,6 +138,4 @@
 
       /** measure now stores R,D **/
-      catalog[0].measure[Nmeas].R        = stars[N][0].average.R;
-      catalog[0].measure[Nmeas].D        = stars[N][0].average.D;
       catalog[0].measure[Nmeas].dbFlags  = 0;
       catalog[0].measure[Nmeas].averef   = n;
@@ -260,7 +258,4 @@
       catalog[0].measure[Nmeas]          = stars[N][0].measure;
 
-      catalog[0].measure[Nmeas].R        = stars[N][0].average.R;
-      catalog[0].measure[Nmeas].D        = stars[N][0].average.D;
-
       catalog[0].measure[Nmeas].t        = (stars[N][0].measure.t == 0) ? TIMEREF : stars[N][0].measure.t;      /** careful : time_t vs e_time **/
       catalog[0].measure[Nmeas].t_msec   = (stars[N][0].measure.t == 0) ?       0 : stars[N][0].measure.t_msec; /** careful : time_t vs e_time **/
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getgsc.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getgsc.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getgsc.c	(revision 38268)
@@ -79,4 +79,7 @@
       if (stars[Nstars].average.D > UserPatch.Dmax) continue;
 
+      stars[Nstars].measure.R = stars[Nstars].average.R;
+      stars[Nstars].measure.D = stars[Nstars].average.D;
+
       fparse (&stars[Nstars].measure.M, 3, &buffer[Nbyte]);
       stars[Nstars].measure.dM 	      = NAN;
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/gettycho.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/gettycho.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/gettycho.c	(revision 38268)
@@ -101,4 +101,7 @@
 	stars[Ntycho].measure.t   = TychoEpoch;
 
+	stars[Ntycho].measure.R = stars[Ntycho].average.R;
+	stars[Ntycho].measure.D = stars[Ntycho].average.D;
+
 	/* one pass of addstar does either r or b */
 	if (photcode == TYCHO_B) {
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getusno.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getusno.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getusno.c	(revision 38268)
@@ -114,4 +114,6 @@
       stars[Nusno].average.R     = buf[0]/360000.0;
       stars[Nusno].average.D     = buf[1]/360000.0 - 90.0;
+      stars[Nusno].measure.R     = stars[Nusno].average.R;
+      stars[Nusno].measure.D     = stars[Nusno].average.D;
       stars[Nusno].measure.dM    = NAN;
 
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getusnob.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getusnob.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/getusnob.c	(revision 38268)
@@ -143,4 +143,7 @@
       stars[Nusno].average.dP  = 0;
 
+      stars[Nusno].measure.R   = stars[Nusno].average.R;
+      stars[Nusno].measure.D   = stars[Nusno].average.D;
+
       /* USNO magnitude errors are reported as a fixed 0.3 mag */
       stars[Nusno].measure.dM  = 0.3;
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/greference.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/greference.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/greference.c	(revision 38268)
@@ -24,7 +24,16 @@
   }
 
-  /* get stars from the USNO B catalog for the given region */
+  /* get stars from the Tycho catalog for the given region */
   if (!strcasecmp (Refcat, "TYCHO")) {
-    stars = gettycho (region, photcode, &Nstars);
+    // stars = gettycho2 (region, photcode, &Nstars);
+    fprintf (stderr, "Tycho load via addstar is deprecated: use loadtycho\n");
+    exit (2);
+  }
+
+  /* get stars from the Tycho catalog for the given region (old ingest) */
+  if (!strcasecmp (Refcat, "TYCHO_OLD")) {
+    // stars = gettycho (region, photcode, &Nstars);
+    fprintf (stderr, "Tycho load via addstar is deprecated: use loadtycho\n");
+    exit (2);
   }
 
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/grefstars.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/grefstars.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/grefstars.c	(revision 38268)
@@ -31,4 +31,7 @@
     fparse (&stars[N].measure.dM, 4, line);
 
+    stars[N].measure.R = stars[N].average.R;
+    stars[N].measure.D = stars[N].average.D;
+
     stars[N].measure.photcode = photcode;
 
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadsupercos_rawdata.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadsupercos_rawdata.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadsupercos_rawdata.c	(revision 38268)
@@ -73,4 +73,7 @@
 	tstars[i].average.D = sstars[i].dec;
 
+	tstars[i].measure.R = sstars[i].ra;
+	tstars[i].measure.D = sstars[i].dec;
+
 	int Ni = imlist[sstars[i].plateID];
 	if (Ni == -1) abort();
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho.c	(revision 38268)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho.c	(revision 38268)
@@ -0,0 +1,46 @@
+# include "addstar.h"
+# include "tycho.h"
+
+/* This is the DVO program to upload tycho 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 tycho 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 tycho
+   files are loaded if supplied on the command line.
+
+   USAGE: loadtycho -D CATDIR (catdir) (tychofile) [...more files]
+
+*/
+
+int main (int argc, char **argv) {
+
+  int i;
+  SkyTable *sky;
+  SkyList *skylist = NULL;
+  AddstarClientOptions options;
+
+  // need to construct these options with args_loadtycho...
+  options = ConfigInit (&argc, argv);
+  options = args_loadtycho (&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;
+  }
+
+  for (i = 1; i < argc; i++) {
+    fprintf (stderr, "loading %s\n", argv[i]);
+    loadtycho_rawdata (skylist, argv[i], options);
+  }
+  exit (0);
+}  
+
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho_ops.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho_ops.c	(revision 38268)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho_ops.c	(revision 38268)
@@ -0,0 +1,133 @@
+# include "addstar.h"
+# include "tycho.h"
+
+static short TYCHO_B = 0;
+static short TYCHO_V = 0;
+static time_t J1990 = 0;
+static time_t J2000 = 0;
+static time_t J2012 = 0;
+
+int gettycho_setup () {
+
+  J1990 = ohana_date_to_sec ("1990/01/01,00:00:00");
+  J2000 = ohana_date_to_sec ("2000/01/01,00:00:00");
+  J2012 = ohana_date_to_sec ("2012/01/01,00:00:00");
+  NAMED_PHOTCODE (TYCHO_B, "TYCHO_B");
+  NAMED_PHOTCODE (TYCHO_V, "TYCHO_V");
+
+  return TRUE;
+}
+
+int gettycho_star (Tycho_Stars *star, char *line) {
+
+  int i;
+
+  star[0].flag = FALSE; // has this yet been matched in dvo?
+  dvo_average_init (&star[0].average);
+  for (i = 0; i < NGROUP; i++) {
+    dvo_measure_init (&star[0].measure[i]);
+  }
+
+  double jyR = atof (&line[178]); // EpRAm (tycho epoch for RA - 1990.0)
+  double jyD = atof (&line[183]); // EpRAm (tycho epoch for DEC - 1990.0)
+
+  time_t TychoEpoch = 0.5*(jyR + jyD) * 365.25 * 86400.0 + J1990;
+
+  // the observed (~1991) position is that at the TychoEpoch
+  double Robs = atof (&line[152]);
+  double Dobs = atof (&line[165]);
+  double Rref = atof (&line[15]); // RAmdeg (mean RA,  epoch J2000)
+  double Dref = atof (&line[28]); // DEmdeg (mean DEC, epoch J2000)
+
+  double Rps1, Dps1;
+
+  if (line[13] == 'X') {
+    star[0].average.R = Robs; // no valid pm, mean epoch position
+    star[0].average.D = Dobs; // 
+
+    star[0].average.dR  = atof (&line[188]) / 1000.0; // e_RAmdeg
+    star[0].average.dD  = atof (&line[194]) / 1000.0; // e_DEmdeg
+
+    star[0].average.uR  = NAN;
+    star[0].average.uD  = NAN;
+
+    star[0].average.duR = NAN;
+    star[0].average.duD = NAN;
+
+    star[0].average.Tmean = TychoEpoch;
+
+    Rref = Robs;
+    Rps1 = Robs;
+    Dref = Dobs;
+    Dps1 = Dobs;
+  } else {
+    star[0].average.dR  = atof (&line[57]) / 1000.0; // e_RAmdeg
+    star[0].average.dD  = atof (&line[61]) / 1000.0; // e_DEmdeg
+
+    star[0].average.uR  = atof (&line[41]) / 1000.0; // pmRA
+    star[0].average.uD  = atof (&line[49]) / 1000.0; // pmDE
+
+    star[0].average.duR = atof (&line[65]) / 1000.0; // e_pmRA
+    star[0].average.duD = atof (&line[70]) / 1000.0; // e_pmDE
+
+    Rps1 = Rref + ((2012.0 - 2000.0)/3600.0)*star[0].average.uR*cos(RAD_DEG*Dref);
+    Dps1 = Dref + ((2012.0 - 2000.0)/3600.0)*star[0].average.uD;
+
+    // use the mean (J2000) position for average or determine J2012?
+    star[0].average.R = Rps1;
+    star[0].average.D = Dps1;
+
+    star[0].average.Tmean = J2000;
+  }
+
+  float  M_B = atof (&line[110]); // BTmag
+  float dM_B = atof (&line[117]); // e_BTmag
+  float  M_V = atof (&line[123]); // VTmag
+  float dM_V = atof (&line[130]); // e_VTmag
+
+  // we have NGROUP measurements (B,V) x (MeanEpoch, J2000, J2012)
+  star[0].measure[0].t   = TychoEpoch;
+  star[0].measure[1].t   = TychoEpoch;
+  star[0].measure[2].t   = J2000;
+  star[0].measure[3].t   = J2000;
+  star[0].measure[4].t   = J2012;
+  star[0].measure[5].t   = J2012;
+
+  star[0].measure[0].R   = Robs;
+  star[0].measure[0].D   = Dobs;
+  star[0].measure[1].R   = Robs;
+  star[0].measure[1].D   = Dobs;
+
+  star[0].measure[2].R   = Rref;
+  star[0].measure[2].D   = Dref;
+  star[0].measure[3].R   = Rref;
+  star[0].measure[3].D   = Dref;
+
+  star[0].measure[4].R   = Rps1;
+  star[0].measure[4].D   = Dps1;
+  star[0].measure[5].R   = Rps1;
+  star[0].measure[5].D   = Dps1;
+
+  for (i = 0; i < NGROUP; i+=2) {
+    star[0].measure[i+0].M        =  M_B;
+    star[0].measure[i+0].dM       = dM_B;
+    star[0].measure[i+0].photcode = TYCHO_B;
+    star[0].measure[i+1].M        =  M_V;
+    star[0].measure[i+1].dM       = dM_V;
+    star[0].measure[i+1].photcode = TYCHO_V;
+  }
+  return TRUE;
+}
+
+int gettycho_sortStars (Tycho_Stars *stars, int Nstars) {
+
+# define SWAPFUNC(A,B){ Tycho_Stars temp = stars[A]; stars[A] = stars[B]; stars[B] = temp; }
+# define COMPARE(A,B)(stars[A].average.R < stars[B].average.R)
+
+  OHANA_SORT (Nstars, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+  
+  return TRUE;
+}
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho_rawdata.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho_rawdata.c	(revision 38268)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadtycho_rawdata.c	(revision 38268)
@@ -0,0 +1,164 @@
+# include "addstar.h"
+# include "tycho.h"
+
+// the tycho data files do not have a fixed RA,DEC range but they have fixed width lines
+// files are small (~26M each) so load entire file at once.
+
+// sequence is:
+// * load all stars from the tycho file
+// * sort the stars by RA
+// * loop over stars
+//   * load catalogs 
+
+// lines are fixed length (207 bytes)
+# define DEBUG 1
+# define NBYTE 207
+# define NITEM 200000
+
+int loadtycho_rawdata (SkyList *skytable, char *filename, AddstarClientOptions options) {
+  
+  int i, j, k, Nitem;
+  char *buffer;
+
+  Tycho_Stars *tstars;
+  Stars *stars, **starrefs; // this is an array of pointers to be consistent with input to find_match_refstars
+  SkyList *skylist;
+  SkyRegion *region;
+  Catalog catalog;
+
+  gettycho_setup ();
+
+  ALLOCATE (buffer, char, NBYTE*NITEM);
+
+  // scan through the entire Tycho file
+  FILE *f = fopen (filename, "r");
+  if (f == NULL) Shutdown ("can't read Tycho data file: %s", filename);
+
+  // We are going to read the file in blocks of NBYTE*NITEM bytes.  (this may be set
+  // larger than a full file).  The entire block of stars will then be ingested
+
+  while ((Nitem = fread (buffer, NBYTE, NITEM, f)) != 0) {
+    if (Nitem == -1) Shutdown ("error reading from raw file %s", filename);
+    if (DEBUG) fprintf (stderr, " read %d items ", Nitem);
+
+    if (VERBOSE) fprintf (stderr, "read .. ");
+
+    // each row generates NGROUP measurements (B,V) x (MeanEpoch, J2000, J2012)
+    int Ntstars = Nitem;
+    ALLOCATE (tstars, Tycho_Stars, Ntstars);
+
+    double Rmin = 360.0;
+    double Rmax =   0.0;
+    double Dmin = +90.0;
+    double Dmax = -90.0;
+
+    // convert buffer into tstars, store ra,dec range
+    for (i = 0; i < Nitem; i++) {
+      gettycho_star (&tstars[i], &buffer[NBYTE*i]);
+      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) .. \n", Nitem, Rmin, Rmax, Dmin, Dmax);
+
+    // sort the array of stars
+    gettycho_sortStars (tstars, Ntstars);
+
+    // scan through the stars, find the next unused containing catalog
+    // skip through table for unsaved stars
+    for (i = 0; i < Ntstars; i++) {
+      if (tstars[i].flag) continue;
+
+      // scan forward until we reach the UserPatch
+      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]);
+
+      // collect array of (Stars *) stars in a new output catalog
+      int Nstars = 0;
+      int NSTARS = (NGROUP*1000);
+      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].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;
+	  
+	for (k = 0; k < NGROUP; k++) {
+	  stars[Nstars+k].average = tstars[j].average;
+	  stars[Nstars+k].measure = tstars[j].measure[k];
+	  stars[Nstars+k].found = -1;
+	}	  
+	tstars[j].flag = TRUE;
+
+	Nstars += NGROUP;
+	if (Nstars >= NSTARS - NGROUP) {
+	  NSTARS += (NGROUP*1000);
+	  REALLOCATE (stars, Stars, NSTARS);
+	}
+      }
+
+      ALLOCATE (starrefs, Stars *, NSTARS);
+      for (j = 0; j < Nstars; j++) {
+	starrefs[j] = &stars[j];
+      }
+
+      // 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 ();
+
+      int verbose = FALSE;
+
+      // 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
+      find_matches_closest_refstars(region, starrefs, Nstars, &catalog, options);
+
+      dvo_catalog_save (&catalog, verbose);
+      dvo_catalog_unlock (&catalog);
+      dvo_catalog_free (&catalog);
+
+      SkyListFree (skylist);
+      free (stars);
+      free (starrefs);
+    }
+    free (tstars);
+    if (VERBOSE) fprintf (stderr, "done\n");
+  }
+  if (VERBOSE) fprintf (stderr, "\n");
+  
+  fclose (f);
+  free (buffer);
+  return (TRUE);
+}
Index: /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadwise_rawdata.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadwise_rawdata.c	(revision 38267)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/addstar/src/loadwise_rawdata.c	(revision 38268)
@@ -12,5 +12,5 @@
 int loadwise_rawdata (SkyList *skytable, char *filename, AddstarClientOptions options) {
   
-  int i, j, verbose;
+  int i, j, k, verbose;
   int Nstars, NSTARS, Ntstars, NTSTARS;
   int Nbyte, Nextra, offset;
@@ -140,22 +140,12 @@
 	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;
+	for (k = 0; k < 4; k++) {
+	  ALLOCATE (stars[Nstars + k], Stars, 1);
+	  InitStar (stars[Nstars + k]);
+	  stars[Nstars+k][0].average.R = tstars[j].R;
+	  stars[Nstars+k][0].average.D = tstars[j].D;
+	  stars[Nstars+k][0].measure.R = tstars[j].R;
+	  stars[Nstars+k][0].measure.D = tstars[j].D;
+	}	  
 	
 	switch (MODE) {
