Index: trunk/Ohana/src/opihi/cmd.astro/Makefile
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/Makefile	(revision 39181)
+++ trunk/Ohana/src/opihi/cmd.astro/Makefile	(revision 39225)
@@ -23,4 +23,5 @@
 $(SRC)/cdot.$(ARCH).o		   \
 $(SRC)/cline.$(ARCH).o             \
+$(SRC)/cneedles.$(ARCH).o		   \
 $(SRC)/cplot.$(ARCH).o		   \
 $(SRC)/csystem.$(ARCH).o	   \
@@ -76,4 +77,5 @@
 $(SRC)/vshimage.$(ARCH).o         \
 $(SRC)/shimage.$(ARCH).o         \
+$(SRC)/wcs.$(ARCH).o         \
 $(SRC)/imsub.$(ARCH).o		   \
 $(SRC)/imfit.$(ARCH).o		   \
Index: trunk/Ohana/src/opihi/cmd.astro/cneedles.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/cneedles.c	(revision 39225)
+++ trunk/Ohana/src/opihi/cmd.astro/cneedles.c	(revision 39225)
@@ -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: trunk/Ohana/src/opihi/cmd.astro/drizzle.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/drizzle.c	(revision 39181)
+++ trunk/Ohana/src/opihi/cmd.astro/drizzle.c	(revision 39225)
@@ -141,4 +141,6 @@
 
 	  if (Vmk && Vmk[Ni]) continue;
+	  if (!isfinite(Vin[Ni])) continue;
+
 	  Vout[No] += Vin[Ni];
 	  Vwt[No] ++;
@@ -195,4 +197,5 @@
 
 	  if (Vmk && Vmk[Ni]) continue;
+	  if (!isfinite(Vin[Ni])) continue;
 	  Vout[No] += Vin[Ni];
 	  Vwt[No] ++;
Index: trunk/Ohana/src/opihi/cmd.astro/init.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/init.c	(revision 39181)
+++ trunk/Ohana/src/opihi/cmd.astro/init.c	(revision 39225)
@@ -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 **));
@@ -66,4 +67,5 @@
 int vshimage                PROTO((int, char **));
 int shimage                 PROTO((int, char **));
+int wcs                     PROTO((int, char **));
 
 static Command cmds[] = {  
@@ -74,4 +76,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"},
@@ -131,4 +134,5 @@
   {1, "vshimage",    vshimage,     "generate images for vector spherical harmonic terms"},
   {1, "shimage",     shimage,      "generate images for spherical harmonic terms"},
+  {1, "wcs",         wcs,          "set the wcs for the given image"},
 }; 
 
Index: trunk/Ohana/src/opihi/cmd.astro/region.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/region.c	(revision 39181)
+++ trunk/Ohana/src/opihi/cmd.astro/region.c	(revision 39225)
@@ -74,7 +74,14 @@
   }
 
+  float Angle = 0.0;
+  if ((N = get_argument (argc, argv, "-angle"))) {
+    remove_argument (N, &argc, argv);
+    Angle = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
   if ((argc != 4) && (argc != 5)) {
     gprint (GP_ERR, "USAGE: region Ra Dec Radius [projection] [orientation]\n");
-    gprint (GP_ERR, "  [-image] [-ew] [+ew] [-ns] [+ns] [-no-clear]\n");
+    gprint (GP_ERR, "  [-image] [-ew] [+ew] [-ns] [+ns] [-no-clear] [-angle theta]\n");
     gprint (GP_ERR, " current: %f %f (%f x %f) (%s)\n", 
 	     graphmode.coords.crval1, graphmode.coords.crval2, 
@@ -111,6 +118,11 @@
   graphmode.coords.crval2 = Dec;
 
-  graphmode.coords.pc1_1 = (graphmode.flipeast) ? -1 : 1;
-  graphmode.coords.pc2_2 = (graphmode.flipnorth) ? -1 : 1;
+  float pc1_1 = (graphmode.flipeast)  ? -1 : 1;
+  float pc2_2 = (graphmode.flipnorth) ? -1 : 1;
+
+  graphmode.coords.pc1_1 =  cos(Angle*RAD_DEG)*pc1_1;
+  graphmode.coords.pc1_2 =  sin(Angle*RAD_DEG)*pc2_2;
+  graphmode.coords.pc2_1 = -sin(Angle*RAD_DEG)*pc1_1;
+  graphmode.coords.pc2_2 =  cos(Angle*RAD_DEG)*pc2_2;
 
   /* ask kapa for coordinate limits, to get the right aspect ratio */
Index: trunk/Ohana/src/opihi/cmd.astro/wcs.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/wcs.c	(revision 39225)
+++ trunk/Ohana/src/opihi/cmd.astro/wcs.c	(revision 39225)
@@ -0,0 +1,93 @@
+# include "astro.h"
+
+int wcs (int argc, char **argv) {
+  
+  int N;
+
+  int flipeast = TRUE;
+  if ((N = get_argument (argc, argv, "-ew"))) {
+    remove_argument (N, &argc, argv);
+    flipeast = TRUE;
+  }
+  if ((N = get_argument (argc, argv, "+ew"))) {
+    remove_argument (N, &argc, argv);
+    flipeast = FALSE;
+  }
+
+  int flipnorth = FALSE;
+  if ((N = get_argument (argc, argv, "-ns"))) {
+    remove_argument (N, &argc, argv);
+    flipnorth = TRUE;
+  }
+  if ((N = get_argument (argc, argv, "+ns"))) {
+    remove_argument (N, &argc, argv);
+    flipnorth = FALSE;
+  }
+
+  float Angle = 0.0;
+  if ((N = get_argument (argc, argv, "-angle"))) {
+    remove_argument (N, &argc, argv);
+    Angle = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  if ((argc != 5) && (argc != 6)) {
+    gprint (GP_ERR, "USAGE: wcs (buffer) Ra Dec platescale [projection] [orientation]\n");
+    gprint (GP_ERR, "  [-ew] [+ew] [-ns] [+ns] [-angle theta]\n");
+    return (FALSE);
+  }
+  
+  double Ra, Dec;
+  if (!ohana_str_to_radec (&Ra, &Dec, argv[2], argv[3])) return (FALSE);
+  float platescale = atof (argv[4]); // arcsec per pixel
+
+  Buffer *buf = NULL;
+  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) {
+    gprint (GP_ERR, "cannot define buffer %s\n", argv[1]);
+    return FALSE;
+  }
+
+  Coords coords;
+  InitCoords (&coords, "DEC--TAN");
+
+  if (argc == 6) {
+    if (!strcasecmp (argv[5], "TAN")) 
+      strcpy (coords.ctype, "DEC--TAN");
+    if (!strcasecmp (argv[5], "SIN")) 
+      strcpy (coords.ctype, "DEC--SIN");
+    if (!strcasecmp (argv[5], "ARC")) 
+      strcpy (coords.ctype, "DEC--ARC");
+    if (!strcasecmp (argv[5], "STG")) 
+      strcpy (coords.ctype, "DEC--STG");
+    if (!strcasecmp (argv[5], "ZEA"))
+      strcpy (coords.ctype, "DEC--ZEA");
+    if (!strcasecmp (argv[5], "AIT")) 
+      strcpy (coords.ctype, "DEC--AIT");
+    if (!strcasecmp (argv[5], "GLS")) 
+      strcpy (coords.ctype, "DEC--GLS");
+    if (!strcasecmp (argv[5], "PAR")) 
+      strcpy (coords.ctype, "DEC--PAR");
+  }
+  
+  coords.crval1 = Ra;
+  coords.crval2 = Dec;
+
+  // reference pixel is the image center
+  coords.crpix1 = 0.5 * buf[0].header.Naxis[0]; // Ohana / IPP center of a pixel is X.5,X.5
+  coords.crpix2 = 0.5 * buf[0].header.Naxis[1]; // Ohana / IPP center of a pixel is X.5,X.5
+
+  float pc1_1 = flipeast  ? -1 : 1;
+  float pc2_2 = flipnorth ? -1 : 1;
+
+  coords.pc1_1 =  cos(Angle*RAD_DEG)*pc1_1;
+  coords.pc1_2 =  sin(Angle*RAD_DEG)*pc2_2;
+  coords.pc2_1 = -sin(Angle*RAD_DEG)*pc1_1;
+  coords.pc2_2 =  cos(Angle*RAD_DEG)*pc2_2;
+
+  coords.cdelt1 = platescale / 3600.0;
+  coords.cdelt2 = platescale / 3600.0;
+
+  PutCoords (&coords, &buf[0].header);
+  return (TRUE);
+}
+
