Index: /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/Makefile
===================================================================
--- /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/Makefile	(revision 39193)
+++ /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/Makefile	(revision 39194)
@@ -23,4 +23,5 @@
 $(SRC)/cdot.$(ARCH).o		   \
 $(SRC)/cline.$(ARCH).o             \
+$(SRC)/cneedles.$(ARCH).o		   \
 $(SRC)/cplot.$(ARCH).o		   \
 $(SRC)/csystem.$(ARCH).o	   \
Index: /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/cneedles.c
===================================================================
--- /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/cneedles.c	(revision 39194)
+++ /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/cneedles.c	(revision 39194)
@@ -0,0 +1,136 @@
+# include "data.h"
+
+// generate needle plots for (x,y) (dx,dy)
+int cneedles (int argc, char **argv) {
+  
+  int i, N, kapa, Npts, valid, size;
+  opihi_flt *x, *y, *r, *d, *dR, *dD, Rmin, Rmax;
+  Vector Xvec, Yvec, *xvec, *yvec, *dxvec, *dyvec;
+  Graphdata graphmode;
+
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
+
+  float scale = 1.0;
+  if ((N = get_argument (argc, argv, "-scale"))) {
+    remove_argument (N, &argc, argv);
+    scale = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }    
+
+  valid  = (argc == 5);
+  valid |= (argc > 6) && !strcmp (argv[5], "where");
+  if (!valid) {
+    gprint (GP_ERR, "USAGE: needles <ra> <dec> <dR> <dD> [-scale scale] [style]\n");
+    gprint (GP_ERR, "   OR: needles <ra> <dec> <dR> <dD> [-scale scale] [style] where (condition)\n");
+    return (FALSE);
+  }
+
+  // tvec is used for logical test (truth vector)
+  Vector *tvec = NULL;
+  if (argc > 6) {
+    char *out = dvomath (argc - 6, &argv[6], &size, 1);
+    if (out == NULL) {
+      print_error ();
+      return FALSE;
+    }
+    if ((tvec = SelectVector (out, OLDVECTOR, TRUE)) == NULL) {
+      gprint (GP_ERR, " invalid logic result\n");
+      DeleteNamedVector (out);
+      free (out);
+      return (FALSE);
+    }
+  }
+
+  graphmode.etype = 0;
+  graphmode.ptype = 100;
+
+  Rmin = graphmode.coords.crval1 - 182.0;
+  Rmax = graphmode.coords.crval1 + 182.0;
+
+  /* find vectors */
+  if ((xvec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if ((yvec = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if ((dxvec = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if ((dyvec = SelectVector (argv[4], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+
+  REQUIRE_VECTOR_FLT (xvec, FALSE); 
+  REQUIRE_VECTOR_FLT (yvec, FALSE); 
+  REQUIRE_VECTOR_FLT (dxvec, FALSE); 
+  REQUIRE_VECTOR_FLT (dyvec, FALSE); 
+
+  if (xvec->Nelements != yvec->Nelements) { gprint (GP_ERR, "vectors are not the same length\n"); return (FALSE); }
+  if (dxvec->Nelements != xvec->Nelements) { gprint (GP_ERR, "vectors are not the same length\n"); return (FALSE); }
+  if (dyvec->Nelements != xvec->Nelements) { gprint (GP_ERR, "vectors are not the same length\n"); return (FALSE); }
+
+  if (tvec && tvec[0].Nelements != yvec[0].Nelements) {
+    gprint (GP_ERR, "logic test vector not the same length as data vectors\n");
+    DeleteVector (tvec);
+    return (FALSE);
+  }
+
+  SetVector (&Xvec, OPIHI_FLT, 2*xvec[0].Nelements);
+  SetVector (&Yvec, OPIHI_FLT, 2*xvec[0].Nelements);
+  
+  // input vectors in r,d space
+  r  =  xvec[0].elements.Flt;
+  d  =  yvec[0].elements.Flt;
+  dR = dxvec[0].elements.Flt;
+  dD = dyvec[0].elements.Flt;
+
+  // output vectors after projection & offsets
+  x = Xvec.elements.Flt;
+  y = Yvec.elements.Flt;
+  
+  Npts = 0;
+  for (i = 0; i < xvec->Nelements; i++, r++, d++, dR++, dD++) {
+    if (tvec) {
+      int skip = (tvec->type == OPIHI_FLT) ? (tvec->elements.Flt[i] == 0.0) : (tvec->elements.Int[i] == 0.0);
+      if (skip) continue;
+    }
+
+    double dec = *d;
+    double ra = ohana_normalize_angle (*r);
+    while (ra < Rmin) ra += 360.0;
+    while (ra > Rmax) ra -= 360.0;
+
+    double X1, Y1;
+    int status1 = RD_to_XY (&X1, &Y1, ra, dec, &graphmode.coords);
+
+    float rescale = cos(dec*RAD_DEG);
+    if (fabs(rescale) < 0.01) {
+      rescale = 0.01;
+    }
+    dec = dec + *dD * scale;
+    ra  = ra  + *dR * scale / rescale;
+
+    double X2, Y2;
+    int status2 = RD_to_XY (&X2, &Y2, ra, dec, &graphmode.coords);
+
+    if (!status1 || !status2) continue;
+
+    *x = X1;
+    *y = Y1;
+    x++;
+    y++;
+    Npts++;
+
+    *x = X2;
+    *y = Y2;
+    x++;
+    y++;
+    Npts++;
+  }
+  Xvec.Nelements = Npts;
+  Yvec.Nelements = Npts;
+
+  graphmode.etype = 0;
+  graphmode.ptype = 100;
+  PlotVectorPair (kapa, &Xvec, &Yvec, NULL, &graphmode);
+  
+  free (Xvec.elements.Ptr);
+  free (Yvec.elements.Ptr);
+    
+  if (tvec) DeleteVector (tvec);
+
+  return (TRUE);
+}
Index: /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/init.c
===================================================================
--- /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/init.c	(revision 39193)
+++ /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.astro/init.c	(revision 39194)
@@ -7,4 +7,5 @@
 int cdot                    PROTO((int, char **));
 int cline                   PROTO((int, char **));
+int cneedles                PROTO((int, char **));
 int cplot                   PROTO((int, char **));
 int csystem                 PROTO((int, char **));
@@ -73,4 +74,5 @@
   {1, "cdot",        cdot,         "plot point in sky coordinates"},
   {1, "cline",       cline,        "plot line connecting two sky coordinates"},
+  {1, "cneedles",    cneedles,     "plot vectors in sky coordinates"},
   {1, "cplot",       cplot,        "plot vectors in sky coordinates"},
   {1, "csystem",     csystem,      "convert between coordinate systems"},
Index: /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/Makefile
===================================================================
--- /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/Makefile	(revision 39193)
+++ /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/Makefile	(revision 39194)
@@ -97,4 +97,5 @@
 $(SRC)/medimage_commands.$(ARCH).o \
 $(SRC)/mset.$(ARCH).o		\
+$(SRC)/needles.$(ARCH).o		\
 $(SRC)/peak.$(ARCH).o		\
 $(SRC)/periodogram.$(ARCH).o	\
Index: /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/init.c	(revision 39193)
+++ /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/init.c	(revision 39194)
@@ -86,4 +86,5 @@
 int medimage_command PROTO((int, char **));
 int mset             PROTO((int, char **));
+int needles          PROTO((int, char **));
 int peak             PROTO((int, char **));
 int periodogram      PROTO((int, char **));
@@ -261,4 +262,5 @@
   {1, "mset",         mset,             "insert a vector in an image"},
   {1, "imset",        mset,             "insert a vector in an image"},
+  {1, "needles",      needles,          "plot vectors needles"},
   {1, "parity",       parity,           "set image parity"},
   {1, "peak",         peak,             "find vector peak in range"},
Index: /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/needles.c
===================================================================
--- /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/needles.c	(revision 39194)
+++ /branches/eam_branches/ipp-20151113/Ohana/src/opihi/cmd.data/needles.c	(revision 39194)
@@ -0,0 +1,121 @@
+# include "data.h"
+
+// generate needle plots for (x,y) (dx,dy)
+int needles (int argc, char **argv) {
+  
+  int kapa, N, Npts, valid, size, i;
+  Graphdata graphmode;
+  Vector *xvec, *yvec, *dxvec, *dyvec;
+
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
+
+  float scale = 1.0;
+  if ((N = get_argument (argc, argv, "-scale"))) {
+    remove_argument (N, &argc, argv);
+    scale = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }    
+
+  valid  = (argc == 5);
+  valid |= (argc > 6) && !strcmp (argv[5], "where");
+  if (!valid) {
+    gprint (GP_ERR, "USAGE: needles <x> <y> <dx> <dy> [-scale scale] [style]\n");
+    gprint (GP_ERR, "   OR: needles <x> <y> <dx> <dy> [-scale scale] [style] where (condition)\n");
+    return (FALSE);
+  }
+
+  // tvec is used for logical test (truth vector)
+  Vector *tvec = NULL;
+  char *mask = NULL;
+  if (argc > 6) {
+    char *out = dvomath (argc - 6, &argv[6], &size, 1);
+    if (out == NULL) {
+      print_error ();
+      return FALSE;
+    }
+    if ((tvec = SelectVector (out, OLDVECTOR, TRUE)) == NULL) {
+      gprint (GP_ERR, " invalid logic result\n");
+      DeleteNamedVector (out);
+      free (out);
+      return (FALSE);
+    }
+  }
+
+  graphmode.etype = 0;
+  graphmode.ptype = 100;
+  
+  /* find vectors */
+  if ((xvec  = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if ((yvec  = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if ((dxvec = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if ((dyvec = SelectVector (argv[4], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if (xvec[0].Nelements != yvec[0].Nelements) {
+    gprint (GP_ERR, "vectors %s and %s not the same length\n", argv[1], argv[2]);
+    return (FALSE);
+  }
+  if (tvec && tvec[0].Nelements != yvec[0].Nelements) {
+    gprint (GP_ERR, "logic test vector not the same length as data vectors\n");
+    return (FALSE);
+  }
+  if (dxvec->Nelements != xvec->Nelements) goto mismatch;
+  if (dyvec->Nelements != xvec->Nelements) goto mismatch;
+
+  Npts = xvec[0].Nelements;
+  if (Npts == 0) {
+    if (tvec) DeleteVector (tvec);
+    return (TRUE);
+  }
+
+  if (tvec) {
+    Npts = 0;
+    ALLOCATE (mask, char, tvec->Nelements);
+    for (i = 0; i < tvec->Nelements; i++) {
+      mask[i] = (tvec->type == OPIHI_FLT) ? (tvec->elements.Flt[i] == 0.0) : (tvec->elements.Int[i] == 0.0);
+      if (!mask[i]) Npts ++;
+    }
+    if (Npts == 0) {
+      DeleteVector (tvec);
+      free (mask);
+      return TRUE;
+    }
+  }
+  Npts = 2*Npts;
+
+  // we need to generate the 2x length vectors
+  Vector *xfull = InitVector();
+  Vector *yfull = InitVector();
+  ResetVector (xfull, OPIHI_FLT, Npts);
+  ResetVector (yfull, OPIHI_FLT, Npts);
+
+  N = 0;
+  for (i = 0; i < xvec->Nelements; i++) {
+    if (mask && mask[i]) continue;
+    xfull->elements.Flt[N] = xvec->elements.Flt[i];
+    yfull->elements.Flt[N] = yvec->elements.Flt[i];
+    N ++;
+    myAssert (N <= Npts, "oops");
+    xfull->elements.Flt[N] = xvec->elements.Flt[i] + scale*dxvec->elements.Flt[i];
+    yfull->elements.Flt[N] = yvec->elements.Flt[i] + scale*dyvec->elements.Flt[i];
+    N ++;
+    myAssert (N <= Npts, "oops");
+  }    
+  myAssert (N == Npts, "oops");
+
+  if (!KapaPrepPlot (kapa, Npts, &graphmode)) return (FALSE);
+  PlotVectorSingle (kapa, xfull, NULL, "x");
+  PlotVectorSingle (kapa, yfull, NULL, "y");
+
+  if (tvec) {
+    free (mask);
+    DeleteVector (tvec);
+  }
+
+  FreeVector (xfull);
+  FreeVector (yfull);
+
+  return (TRUE);
+
+mismatch:
+  gprint (GP_ERR, "x,y and dx,dy lengths are mismatched\n");
+  return (FALSE);
+}
Index: /branches/eam_branches/ipp-20151113/Ohana/src/opihi/include/dvomath.h
===================================================================
--- /branches/eam_branches/ipp-20151113/Ohana/src/opihi/include/dvomath.h	(revision 39193)
+++ /branches/eam_branches/ipp-20151113/Ohana/src/opihi/include/dvomath.h	(revision 39194)
@@ -147,4 +147,5 @@
 Vector       *InitVector            PROTO((void));
 void          FreeVectorArray       PROTO((Vector **vec, int Nvec));
+void          FreeVector            PROTO((Vector *vec));
 int           CopyVector            PROTO((Vector *out, Vector *in));
 int           ResetVector           PROTO((Vector *vec, char type, int Nelements));
Index: /branches/eam_branches/ipp-20151113/Ohana/src/opihi/lib.shell/VectorOps.c
===================================================================
--- /branches/eam_branches/ipp-20151113/Ohana/src/opihi/lib.shell/VectorOps.c	(revision 39193)
+++ /branches/eam_branches/ipp-20151113/Ohana/src/opihi/lib.shell/VectorOps.c	(revision 39194)
@@ -15,4 +15,14 @@
 
 // this function is NOT thread protected : it is only used in startup and/or shutdown
+void FreeVector (Vector *vec) {
+
+  if (!vec) return;
+  if (vec->elements.Int) {
+    free (vec->elements.Int);
+  }
+  free (vec);
+}
+
+// this function is NOT thread protected : it is only used in startup and/or shutdown
 void FreeVectorArray (Vector **vec, int Nvec) {
 
@@ -21,9 +31,5 @@
   if (!vec) return;
   for (i = 0; i < Nvec; i++) {
-    if (!vec[i]) continue;
-    if (vec[i]->elements.Int) {
-      free (vec[i]->elements.Int);
-    }
-    free (vec[i]);
+    FreeVector (vec[i]);
   }
   free (vec);
