Index: /branches/eam_branches/ohana.20190329/src/addstar/Makefile
===================================================================
--- /branches/eam_branches/ohana.20190329/src/addstar/Makefile	(revision 40832)
+++ /branches/eam_branches/ohana.20190329/src/addstar/Makefile	(revision 40833)
@@ -27,4 +27,5 @@
 loadgaia     : $(BIN)/loadgaia.$(ARCH)
 loadgaia_dr2 : $(BIN)/loadgaia_dr2.$(ARCH)
+loadukirt_uhs : $(BIN)/loadukirt_uhs.$(ARCH)
 loadstarpar  : $(BIN)/loadstarpar.$(ARCH)
 loadstarpar_client : $(BIN)/loadstarpar_client.$(ARCH)
@@ -44,5 +45,5 @@
 # programs in 'SERVER' use the client-server concept and are out of date
 
-INSTALL = addstar addstar_client sedstar loadgalphot loadgaia loadgaia_dr2 loadstarpar loadstarpar_client setobjflags setobjflags_client loadICRF loadICRF_client skycells mkcmf dumpskycells findskycell load2mass loadwise loadtycho loadbsc loadsupercos 
+INSTALL = addstar addstar_client sedstar loadgalphot loadgaia loadgaia_dr2 loadukirt_uhs loadstarpar loadstarpar_client setobjflags setobjflags_client loadICRF loadICRF_client skycells mkcmf dumpskycells findskycell load2mass loadwise loadtycho loadbsc loadsupercos 
 SERVER  = addstarc addstard addstart
 
@@ -305,4 +306,22 @@
 $(SRC)/psps_ids.$(ARCH).o
 
+LOAD-UKIRT_UHS = \
+$(SRC)/loadukirt_uhs.$(ARCH).o \
+$(SRC)/ConfigInit.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o \
+$(SRC)/Shutdown.$(ARCH).o \
+$(SRC)/args_loadukirt_uhs.$(ARCH).o \
+$(SRC)/find_matches_ukirt_uhs.$(ARCH).o \
+$(SRC)/loadukirt_uhs_catalog.$(ARCH).o \
+$(SRC)/loadukirt_uhs_make_subset.$(ARCH).o \
+$(SRC)/loadukirt_uhs_readstars.$(ARCH).o \
+$(SRC)/loadukirt_uhs_table.$(ARCH).o \
+$(SRC)/parse_csv.$(ARCH).o \
+$(SRC)/resort_catalog.$(ARCH).o \
+$(SRC)/build_links.$(ARCH).o \
+$(SRC)/strhash.$(ARCH).o \
+$(SRC)/sortIDs.$(ARCH).o \
+$(SRC)/psps_ids.$(ARCH).o
+
 LOAD-STARPAR = \
 $(SRC)/loadstarpar.$(ARCH).o \
@@ -528,4 +547,5 @@
 $(LOAD-GAIA)	       : $(INC)/addstar.h $(INC)/gaia.h
 $(LOAD-GAIA_DR2)       : $(INC)/addstar.h $(INC)/gaia_dr2.h
+$(LOAD-UKIRT_UHS)      : $(INC)/addstar.h $(INC)/ukirt_uhs.h
 $(LOAD-STARPAR)	       : $(INC)/addstar.h $(INC)/loadstarpar.h
 $(LOAD-STARPAR-CLIENT) : $(INC)/addstar.h $(INC)/loadstarpar.h
@@ -551,4 +571,5 @@
 $(BIN)/loadgaia.$(ARCH)       : $(LOAD-GAIA)
 $(BIN)/loadgaia_dr2.$(ARCH)   : $(LOAD-GAIA_DR2)
+$(BIN)/loadukirt_uhs.$(ARCH)  : $(LOAD-UKIRT_UHS)
 $(BIN)/loadstarpar.$(ARCH)    : $(LOAD-STARPAR)
 $(BIN)/loadstarpar_client.$(ARCH) : $(LOAD-STARPAR-CLIENT)
Index: /branches/eam_branches/ohana.20190329/src/addstar/include/ukirt_uhs.h
===================================================================
--- /branches/eam_branches/ohana.20190329/src/addstar/include/ukirt_uhs.h	(revision 40832)
+++ /branches/eam_branches/ohana.20190329/src/addstar/include/ukirt_uhs.h	(revision 40833)
@@ -1,8 +1,8 @@
 
-// measure[0] = g, measure[1] = b, measure[2] = r, 
+// measure[1] = J
 
 typedef struct {
   Average average;
-  Measure measure[3];
+  Measure measure[1];
   int flag; // in a subset?
   int found; // assigned to an object?
Index: /branches/eam_branches/ohana.20190329/src/addstar/src/loadukirt_uhs_readstars.c
===================================================================
--- /branches/eam_branches/ohana.20190329/src/addstar/src/loadukirt_uhs_readstars.c	(revision 40832)
+++ /branches/eam_branches/ohana.20190329/src/addstar/src/loadukirt_uhs_readstars.c	(revision 40833)
@@ -78,6 +78,6 @@
 
   // XXX I need a UKIRT UHS Mean Epoch
-  time_t UKIRT_UHS_EPOCH = ohana_date_to_sec ("2016/01/01,00:00:00");
-  fprintf (stderr, "WARNING: using an invalid UKIRT_UHS_EPOCH (see loadukirt_uhs_readstars.c:60)\n");
+  // time_t UKIRT_UHS_EPOCH = ohana_date_to_sec ("2016/01/01,00:00:00");
+  // fprintf (stderr, "WARNING: using an invalid UKIRT_UHS_EPOCH (see loadukirt_uhs_readstars.c:60)\n");
 
   // read in the full FITS files
@@ -90,28 +90,23 @@
   int NELEM = 10000;  // currently-allocated number of output rows
 
-  // for initial testing, a minimal subset:
-  ALLOCATE_PTR (sourceID, uint64_t, NELEM);
-  ALLOCATE_PTR (Rg,       double, NELEM);
-  ALLOCATE_PTR (Dg,       double, NELEM);
-  ALLOCATE_PTR (chiSq,    double, NELEM);
-  ALLOCATE_PTR (drg,      double, NELEM);
-
-  ALLOCATE_PTR (sourceID, , NELEM);
-  ALLOCATE_PTR (frameSetID,  , NELEM);
-  ALLOCATE_PTR (ra,  , NELEM);
-  ALLOCATE_PTR (dec,  , NELEM);
-  ALLOCATE_PTR (pStar,  , NELEM);
-  ALLOCATE_PTR (pGalaxy,  , NELEM);
-  ALLOCATE_PTR (jHallMag,  , NELEM);
-  ALLOCATE_PTR (jHallMagErr,  , NELEM);
-  ALLOCATE_PTR (jAperMag3,  , NELEM);
-  ALLOCATE_PTR (jAperMag3Err,  , NELEM);
-  ALLOCATE_PTR (jAperMag4,  , NELEM);
-  ALLOCATE_PTR (jAperMag4Err,  , NELEM);
-  ALLOCATE_PTR (jPA,  , NELEM);
-  ALLOCATE_PTR (jClass,  , NELEM);
-  ALLOCATE_PTR (jppErrBits,  , NELEM);
-  ALLOCATE_PTR (photFlags2,  , NELEM);
-  ALLOCATE_PTR (jSeqNum,  , NELEM);
+  // vectors to hold the data loaded from the csv file (names are UKIRT native names)
+  ALLOCATE_PTR (sourceID,     uint64_t, NELEM);
+  ALLOCATE_PTR (frameSetID,   uint64_t, NELEM); // XXX doc says 8-bytes for this value!
+  ALLOCATE_PTR (ra,           double,   NELEM);
+  ALLOCATE_PTR (dec,          double,   NELEM);
+  ALLOCATE_PTR (priOrSec,     uint64_t, NELEM);
+  ALLOCATE_PTR (mergedClass,  int,      NELEM); // XXX doc says 2-bytes (could use a short)
+  ALLOCATE_PTR (pStar,        double,   NELEM);
+  ALLOCATE_PTR (pGalaxy,      double,   NELEM);
+  ALLOCATE_PTR (jHallMag,     double,   NELEM);
+  ALLOCATE_PTR (jHallMagErr,  double,   NELEM);
+  ALLOCATE_PTR (jAperMag3,    double,   NELEM);
+  ALLOCATE_PTR (jAperMag3Err, double,   NELEM);
+  ALLOCATE_PTR (jAperMag4,    double,   NELEM);
+  ALLOCATE_PTR (jAperMag4Err, double,   NELEM);
+  ALLOCATE_PTR (jPA,          double,   NELEM);
+  ALLOCATE_PTR (jClass,       int,      NELEM);
+  ALLOCATE_PTR (jppErrBits,   int,      NELEM);
+  ALLOCATE_PTR (jSeqNum,      int,      NELEM);
 
   // we allocate one extra byte into which we never read so there will always be a NULL terminating the string
@@ -178,5 +173,5 @@
       if (*c0 == '!')          { if (!EndOfFile) { c0 = c1 + 1; } continue; }
 
-      // XXX for Gaia, we know which columns we want in advance
+      // for UKIRT UHS, we know which columns we want in advance
 
       int lineStatus = TRUE;
@@ -189,5 +184,5 @@
       cA = c0;
 
-      // Tref == 2015.5 for DR2
+      // Tref : TBD for UHS
 
       // the start of the line is the 1st element (fields are 1-counting)
@@ -207,5 +202,5 @@
       dPARSE (30, 29, jAperMag4Err, "jAperMag4Err");
       dPARSE (35, 30, jPA,          "jPA");
-      dPARSE (38, 35, jClass,       "jClass");
+      iPARSE (38, 35, jClass,       "jClass");
       iPARSE (40, 38, jppErrBits,   "jppErrBits");
       iPARSE (41, 40, jSeqNum,      "jSeqNum");
@@ -223,21 +218,22 @@
 	NELEM += 1000;
 
-	REALLOCATE (sourceID, uint64_t, NELEM);
-	REALLOCATE (ra, double, NELEM);
-	REALLOCATE (dec, double, NELEM);
-	REALLOCATE (priOrSec, uint64_t, NELEM);
-	REALLOCATE (mergedClass, int, NELEM);
-	REALLOCATE (pStar, double, NELEM);
-	REALLOCATE (pGalaxy, double, NELEM);
-	REALLOCATE (jHallMag, double, NELEM);
-	REALLOCATE (jHallMagErr, double, NELEM);
-	REALLOCATE (jAperMag3, double, NELEM);
-	REALLOCATE (jAperMag3Err, double, NELEM);
-	REALLOCATE (jAperMag4, double, NELEM);
-	REALLOCATE (jAperMag4Err, double, NELEM);
-	REALLOCATE (jPA, double, NELEM);
-	REALLOCATE (jClass,  double, NELEM);
-	REALLOCATE (jppErrBits, int, NELEM);
-	REALLOCATE (jSeqNum, int, NELEM);
+	REALLOCATE (sourceID,     uint64_t, NELEM);
+	REALLOCATE (frameSetID,   uint64_t, NELEM);
+	REALLOCATE (ra,           double,   NELEM);
+	REALLOCATE (dec,          double,   NELEM);
+	REALLOCATE (priOrSec,     uint64_t, NELEM);
+	REALLOCATE (mergedClass,  int,      NELEM);
+	REALLOCATE (pStar,        double,   NELEM);
+	REALLOCATE (pGalaxy,      double,   NELEM);
+	REALLOCATE (jHallMag,     double,   NELEM);
+	REALLOCATE (jHallMagErr,  double,   NELEM);
+	REALLOCATE (jAperMag3,    double,   NELEM);
+	REALLOCATE (jAperMag3Err, double,   NELEM);
+	REALLOCATE (jAperMag4,    double,   NELEM);
+	REALLOCATE (jAperMag4Err, double,   NELEM);
+	REALLOCATE (jPA,          double,   NELEM);
+	REALLOCATE (jClass,       int,      NELEM);
+	REALLOCATE (jppErrBits,   int,      NELEM);
+	REALLOCATE (jSeqNum,      int,      NELEM);
       }
       if (!EndOfFile) {
@@ -267,13 +263,12 @@
   for (int i = 0; i < NstarsIn; i++) {
 
-    Rmin = MIN (Rmin, Rg[i]);
-    Rmax = MAX (Rmax, Rg[i]);
-    Dmin = MIN (Dmin, Dg[i]);
-    Dmax = MAX (Dmax, Dg[i]);
-
-    // each Gaia DR2 source corresponds to 3 measurements: Gg, Gr, Gb:
-
+    Rmin = MIN (Rmin, ra[i]);
+    Rmax = MAX (Rmax, ra[i]);
+    Dmin = MIN (Dmin, dec[i]);
+    Dmax = MAX (Dmax, dec[i]);
+
+    // we have one UKIRT UHS (J-band) measurement per object
     dvo_average_init (&stars[Nstars].average);
-    dvo_measure_init (&stars[Nstars].measure);
+    dvo_measure_init (&stars[Nstars].measure[0]);
 
     stars[Nstars].average.R = ra[i];
@@ -286,33 +281,33 @@
 
     int isPrimary   = (!priOrSec[i] || (priOrSec[i] == frameSetID[i])) ? ID_MEAS_STACK_PRIMARY : 0x00;
-    int isGalaxy    = (mergedClass[i] == 1)  ? 0x01 : 0x00;
-    int isNoise     = (mergedClass[i] == 0)  ? 0x02 : 0x00;
-    int isStar      = (mergedClass[i] == -1) ? 0x04 : 0x00;
-    int maybeStar   = (mergedClass[i] == -2) ? 0x08 : 0x00;
-    int maybeGalaxy = (mergedClass[i] == -3) ? 0x10 : 0x00;
-    int isSaturated = (mergedClass[i] == -9) ? 0x20 : 0x00;
-
-    int photFlags2 = isGalaxy | isNoise | isStar | maybeStar | maybeGalaxy | isSaturated | isPrimary;
-
-    stars[Nstars].measure[0].extID     = sourceID[i];
-    stars[Nstars].measure[0].imageID   = frameSetID[i];
-    stars[Nstars].measure[0].R         = ra[i];
-    stars[Nstars].measure[0].D         = dec[i];
-    stars[Nstars].measure[0].psfChisq  = pStar[i];
-    stars[Nstars].measure[0].extNsigma = pGalaxy[i];
-    stars[Nstars].measure[0].M         = jHallMag[i];
-    stars[Nstars].measure[0].dM        = jHallMagErr[i];
-    stars[Nstars].measure[0].Map       = jAperMag3[i];
-    stars[Nstars].measure[0].dMap      = jAperMag3Err[i];
-    stars[Nstars].measure[0].Mkron     = jAperMag4[i];
-    stars[Nstars].measure[0].dMkron    = jAperMag4Err[i];
-    stars[Nstars].measure[0].posangle  = jPA[i]; // XXX units
-    stars[Nstars].measure[0].psfQF     = jClass[i];
-
+    int photFlags2 = isPrimary;
+    switch (mergedClass[i]) {
+      case  1: photFlags2 |= 0x01; break; // Galaxy
+      case  0: photFlags2 |= 0x02; break; // Noise
+      case -1: photFlags2 |= 0x04; break; // Star
+      case -2: photFlags2 |= 0x08; break; // probably star
+      case -3: photFlags2 |= 0x10; break; // probably galaxy
+      case -9: photFlags2 |= 0x20; break; // saturated
+      default: break;
+    }
+
+    stars[Nstars].measure[0].extID      = sourceID[i];
+    stars[Nstars].measure[0].imageID    = frameSetID[i]; // XXX check for frameSetID > 31-bit int
+    stars[Nstars].measure[0].R          = ra[i];
+    stars[Nstars].measure[0].D          = dec[i];
+    stars[Nstars].measure[0].psfChisq   = pStar[i];
+    stars[Nstars].measure[0].extNsigma  = pGalaxy[i];
+    stars[Nstars].measure[0].M          = jHallMag[i];
+    stars[Nstars].measure[0].dM         = jHallMagErr[i];
+    stars[Nstars].measure[0].Map        = jAperMag3[i];
+    stars[Nstars].measure[0].dMap       = jAperMag3Err[i];
+    stars[Nstars].measure[0].Mkron      = jAperMag4[i];
+    stars[Nstars].measure[0].dMkron     = jAperMag4Err[i];
+    stars[Nstars].measure[0].posangle   = jPA[i]; // XXX units
+    stars[Nstars].measure[0].psfQF      = jClass[i];
     stars[Nstars].measure[0].photFlags  = jppErrBits[i];
-    stars[Nstars].measure[0].photFlags2 = photFlags2[i];
+    stars[Nstars].measure[0].photFlags2 = photFlags2;
     stars[Nstars].measure[0].detID      = jSeqNum[i];
-
-    stars[Nstars].measure[0].photcode  = codeJ;
+    stars[Nstars].measure[0].photcode   = codeJ;
 
     stars[Nstars].average.Nmeasure = 1;
@@ -322,30 +317,22 @@
   }
 
-  free (srcID);
-  free (Rg);
-  free (dRg);
-  free (Dg);
-  free (dDg);
-  free (plx);
-  free (dplx);
-  free (uR);
-  free (duR);
-  free (uD);
-  free (duD);
-  free (nObs);
-  free (nGood);
-  free (goFit);
-  free (chiSq);
-  free (exNoise);
-  free (exNoiseSig);
-  free (nPeriods);
-  free (dupSource);
-  free (gg);
-  free (dgg);
-  free (bg);
-  free (dbg);
-  free (rg);
-  free (drg);
-  free (procMode);
+  FREE (sourceID);
+  FREE (frameSetID);
+  FREE (ra);
+  FREE (dec);
+  FREE (priOrSec);
+  FREE (mergedClass);
+  FREE (pStar);
+  FREE (pGalaxy);
+  FREE (jHallMag);
+  FREE (jHallMagErr);
+  FREE (jAperMag3);
+  FREE (jAperMag3Err);
+  FREE (jAperMag4);
+  FREE (jAperMag4Err);
+  FREE (jPA);
+  FREE (jClass);
+  FREE (jppErrBits);
+  FREE (jSeqNum);
 
   free (buffer);
