Index: /branches/eam_branches/ipp-20150419/Ohana/src/libohana/Makefile
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/libohana/Makefile	(revision 38281)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/libohana/Makefile	(revision 38282)
@@ -30,4 +30,5 @@
 $(SRC)/ohana_allocate.$(ARCH).o  \
 $(SRC)/sorts.$(ARCH).o		 \
+$(SRC)/bisection.$(ARCH).o		 \
 $(SRC)/string.$(ARCH).o		 \
 $(SRC)/findexec.$(ARCH).o	 \
Index: /branches/eam_branches/ipp-20150419/Ohana/src/libohana/include/ohana.h
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/libohana/include/ohana.h	(revision 38281)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/libohana/include/ohana.h	(revision 38282)
@@ -478,3 +478,6 @@
 void VSHtermsForLM (VSHterms *terms, double R, double D, int l, int m);
 
-# endif
+/* in bisection.c */
+int ohana_bisection_double (double *values, int Nvalues, double threshold);
+
+# endif
Index: /branches/eam_branches/ipp-20150419/Ohana/src/libohana/src/bisection.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/libohana/src/bisection.c	(revision 38282)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/libohana/src/bisection.c	(revision 38282)
@@ -0,0 +1,33 @@
+# include <ohana.h>
+
+// return the index of the last value < threshold 
+int ohana_bisection_double (double *values, int Nvalues, double threshold) {
+
+  int Nlo = 0; 
+  int Nhi = Nvalues - 1;
+
+  if (Nvalues < 1) return (-1);
+  if (values[Nlo] > threshold) return (-1);
+
+  if (Nvalues < 2) return (0);
+  if (values[Nhi] < threshold) return (Nhi);
+
+  int N;
+  while (Nhi - Nlo > 4) {
+    N = 0.5*(Nlo + Nhi);
+    if (values[N] < threshold) {
+      Nlo = MAX(N, 0);
+    } else {
+      Nhi = MIN(N + 1, Nvalues - 1);
+    }
+  }
+  // values[Nlo] < threshold 
+  // values[Nhi] >= threshold 
+
+  for (N = Nlo; N < Nhi; N++) {
+    if (values[N] >= threshold) {
+      return (N-1);
+    }
+  }
+  return (N);
+}
Index: /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/Makefile
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/Makefile	(revision 38281)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/Makefile	(revision 38282)
@@ -25,4 +25,5 @@
 $(SRC)/book.$(ARCH).o		\
 $(SRC)/book_commands.$(ARCH).o	\
+$(SRC)/bisection.$(ARCH).o		\
 $(SRC)/cast.$(ARCH).o		\
 $(SRC)/center.$(ARCH).o	\
Index: /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/bisection.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/bisection.c	(revision 38282)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/bisection.c	(revision 38282)
@@ -0,0 +1,37 @@
+# include "data.h"
+
+int bisection (int argc, char **argv) {
+  
+  int N;
+  Vector *vec;
+
+  int QUIET = FALSE;
+  if ((N = get_argument (argc, argv, "-q"))) {
+    QUIET = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  if (argc != 3) {
+    gprint (GP_ERR, "USAGE: bisection <vector> (threshold)\n");
+    gprint (GP_ERR, "  return the vector index for which the vector is last below the threshold via bisection\n");
+    gprint (GP_ERR, "  vector must be sorted\n");
+    return (FALSE);
+  }
+  
+  if ((vec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  double threshold = atof (argv[2]);
+  
+  int isFlt = (vec[0].type == OPIHI_FLT);
+
+  if (isFlt) {
+    N = ohana_bisection_double (vec[0].elements.Flt, vec[0].Nelements, threshold);
+  } else {
+    // N = ohana_bisection_int (vec[0].elements.Flt, vec[0].Nelements);
+  }
+
+  set_variable ("bisecbin", N);
+
+  if (!QUIET) gprint (GP_LOG, "bin %d for theshold %f\n", N, threshold);
+
+  return (TRUE);
+}
Index: /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/init.c	(revision 38281)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/opihi/cmd.data/init.c	(revision 38282)
@@ -9,6 +9,6 @@
 int box              PROTO((int, char **));
 int book_command     PROTO((int, char **));
+int bisection        PROTO((int, char **));
 int center           PROTO((int, char **));
-int parity           PROTO((int, char **));
 int cast             PROTO((int, char **));
 int circstats        PROTO((int, char **));
@@ -89,4 +89,5 @@
 int plot             PROTO((int, char **));
 int dot              PROTO((int, char **));
+int parity           PROTO((int, char **));
 int point            PROTO((int, char **));
 int ps               PROTO((int, char **));
@@ -175,4 +176,5 @@
   {1, "applyfit2d",   applyfit2d,       "apply 2-d fit to new vector"},
   {1, "applyfit3d",   applyfit3d,       "apply 3-d fit to new vector"},
+  {1, "bisection",    bisection,        "use bisection to find threshold in vector"},
   {1, "book",         book_command,     "commands to manipulate book/page/word data"},
   {1, "box",          box,              "draw a box on the plot"},
Index: /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/include/setastrom.h
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/include/setastrom.h	(revision 38281)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/include/setastrom.h	(revision 38282)
@@ -49,8 +49,10 @@
 char        *DCR_FILE;
 char        *CAM_FILE;
+char        *TYC_FILE;
 
 int          KH_RESET;
 int          DCR_RESET;
 int          CAM_RESET;
+int          TYC_RESET;
 
 SkyRegion    UserPatch;
Index: /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/initialize_setastrom.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/initialize_setastrom.c	(revision 38281)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/initialize_setastrom.c	(revision 38282)
@@ -7,4 +7,5 @@
     fprintf (stderr, "    -DCR (file) : supply DCR correction splines\n");
     fprintf (stderr, "    -CAM (file) : supply camera-static correction file\n");
+    fprintf (stderr, "    -TYC (file) : supply tycho-fix file (no pm stars)\n");
     fprintf (stderr, "    -v : verbose mode\n");
     fprintf (stderr, "    -region Rmin Rmax Dmin Dmax\n");
@@ -91,4 +92,17 @@
   }
 
+  TYC_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-TYC"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    TYC_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+  TYC_RESET = FALSE;
+  if ((N = get_argument (argc, argv, "-TYC-reset"))) {
+    remove_argument (N, &argc, argv);
+    TYC_RESET = TRUE;
+  }
+
   SINGLE_CPT = NULL;
   if ((N = get_argument (argc, argv, "-cpt"))) {
@@ -160,6 +174,6 @@
   }
 
-  if (!KH_FILE && !DCR_FILE && !CAM_FILE) {
-    fprintf (stderr, "WARNING: none of -CAM, -KH, -DCR supplied\n");
+  if (!KH_FILE && !DCR_FILE && !CAM_FILE && !TYC_FILE) {
+    fprintf (stderr, "WARNING: none of -CAM, -KH, -DCR, -TYC supplied\n");
   }
 
@@ -175,4 +189,5 @@
   fprintf (stderr, "    -DCR (file) : supply DCR correction splines\n");
   fprintf (stderr, "    -CAM (file) : supply camera-static correction file\n");
+  fprintf (stderr, "    -TYC (file) : supply tycho-fix file (no pm stars)\n");
   fprintf (stderr, "    -region Rmin Rmax Dmin Dmax\n");
   fprintf (stderr, "    -update-catformat (format) : change database schema on output\n");
@@ -247,4 +262,17 @@
     remove_argument (N, &argc, argv);
     CAM_RESET = TRUE;
+  }
+
+  TYC_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-TYC"))) {
+    remove_argument (N, &argc, argv);
+    char *tmpfile = strcreate (argv[N]);
+    TYC_FILE = abspath (tmpfile, DVO_MAX_PATH);
+    remove_argument (N, &argc, argv);
+  }
+  TYC_RESET = FALSE;
+  if ((N = get_argument (argc, argv, "-TYC-reset"))) {
+    remove_argument (N, &argc, argv);
+    TYC_RESET = TRUE;
   }
 
@@ -308,6 +336,6 @@
   if (!CATDIR) usage_setastrom_client();
 
-  if (!KH_FILE && !DCR_FILE && !CAM_FILE) {
-    fprintf (stderr, "WARNING: none of -CAM, -KH, -DCR supplied\n");
+  if (!KH_FILE && !DCR_FILE && !CAM_FILE && !TYC_FILE) {
+    fprintf (stderr, "WARNING: none of -CAM, -KH, -DCR, -TYC supplied\n");
   }
 
Index: /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/repair_tycho_setastrom.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/repair_tycho_setastrom.c	(revision 38282)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/repair_tycho_setastrom.c	(revision 38282)
@@ -0,0 +1,10 @@
+# include "setastrom.h"
+
+int repair_tycho_setastrom (Catalog *catalog) {
+
+
+  // first select the subset of objects which lie in this catalog:
+
+  
+
+}
Index: /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/setastrom.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/setastrom.c	(revision 38281)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/setastrom.c	(revision 38282)
@@ -8,6 +8,5 @@
   initialize_setastrom (argc, argv);
 
-  // argv[1] holds the correction file
-  status = update_dvo_setastrom (argv[1]);
+  status = update_dvo_setastrom ();
 
   if (!status) exit (1);
Index: /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/tyc_correction.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/tyc_correction.c	(revision 38282)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/tyc_correction.c	(revision 38282)
@@ -0,0 +1,47 @@
+# include "setastrom.h"
+
+static int Ntycho = 0;
+static double *tychoR = NULL;
+static double *tychoD = NULL;
+
+int load_tyc_correction (char *filename) {
+
+  FILE *f = fopen (filename, "r");
+  if (!f) myAbort ("file not found");
+
+  double *tychoR, *tychoD;
+
+  int NTYCHO = 10000;
+
+  ALLOCATE (tychoR, double, NTYCHO);
+  ALLOCATE (tychoD, double, NTYCHO);
+
+  while (fscanf (f, "%lf %lf", &tychoR[Ntycho], &tychoD[Ntycho]) != EOF) {
+    Ntycho ++;
+
+    if (Ntycho >= NTYCHO) {
+      NTYCHO += 10000;
+      REALLOCATE (tychoR, double, NTYCHO);
+      REALLOCATE (tychoD, double, NTYCHO);
+    }
+  }
+
+  dsortpair (tychoR, tychoD, Ntycho); 
+
+  return TRUE;
+}
+
+int get_tyc_correction (double **R, double **D, int *N) {
+
+  *R = NULL;
+  *D = NULL;
+  *N = 0;
+
+  if (!tychoR) return FALSE;
+
+  *R = tychoR;
+  *D = tychoD;
+  *N = Ntycho;
+  return TRUE;
+}
+
Index: /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/update_dvo_setastrom.c
===================================================================
--- /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/update_dvo_setastrom.c	(revision 38281)
+++ /branches/eam_branches/ipp-20150419/Ohana/src/uniphot/src/update_dvo_setastrom.c	(revision 38282)
@@ -20,4 +20,5 @@
   if (DCR_FILE) if (!load_dcr_correction (DCR_FILE)) { fprintf (stderr, "failed to load DCR correction %s\n", DCR_FILE); exit (1); }
   if (CAM_FILE) if (!load_cam_correction (CAM_FILE)) { fprintf (stderr, "failed to load CAM correction %s\n", CAM_FILE); exit (1); }
+  if (TYC_FILE) if (!load_tyc_correction (TYC_FILE)) { fprintf (stderr, "failed to load TYC correction %s\n", TYC_FILE); exit (1); }
 
   // determine the populated SkyRegions overlapping the requested area (default depth)
@@ -56,4 +57,7 @@
     }
 
+    if (TYC_FILE) {
+      repair_tycho_setastrom (&catalog, skylist[0].regions[i]);
+    }
     update_catalog_setastrom (&catalog);
 
