Index: /branches/eam_branches/ipp-20121130/Ohana/src/addstar/src/findskycell.c
===================================================================
--- /branches/eam_branches/ipp-20121130/Ohana/src/addstar/src/findskycell.c	(revision 34826)
+++ /branches/eam_branches/ipp-20121130/Ohana/src/addstar/src/findskycell.c	(revision 34827)
@@ -24,7 +24,11 @@
 int apply_tree (char *treefile, char *datafile);
 
+float SCALE = 1.0;
+int NX_SUB = 1;
+int NY_SUB = 1;
+
 int main (int argc, char **argv) {
 
-  int N, NX_SUB, NY_SUB;
+  int N;
   char *treefile = NULL;
 
@@ -47,4 +51,12 @@
     remove_argument (N, &argc, argv);
     NY_SUB = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  /* pixel scale (arcsec/pixel) */
+  SCALE = 1.0;
+  if ((N = get_argument (argc, argv, "-scale"))) {
+    remove_argument (N, &argc, argv);
+    SCALE = atof (argv[N]);
     remove_argument (N, &argc, argv);
   }
@@ -121,4 +133,7 @@
 
   tree.Nzone = 46;
+  tree.NX_SUB = NX_SUB;
+  tree.NY_SUB = NY_SUB;
+  tree.dPix = SCALE/3600.0;
 
   ALLOCATE (tree.Nband, int, tree.Nzone);
@@ -134,4 +149,8 @@
   ALLOCATE (tree.ra,   double *, tree.Nzone);
   ALLOCATE (tree.dec,  double *, tree.Nzone);
+  ALLOCATE (tree.Xo,      int *, tree.Nzone);
+  ALLOCATE (tree.Yo,      int *, tree.Nzone);
+  ALLOCATE (tree.dX,      int *, tree.Nzone);
+  ALLOCATE (tree.dY,      int *, tree.Nzone);
   ALLOCATE (tree.cell,    int *, tree.Nzone);
   ALLOCATE (tree.name,  char **, tree.Nzone);
@@ -164,4 +183,8 @@
     ALLOCATE (tree.ra[zone],   double, tree.NBAND[zone]);
     ALLOCATE (tree.dec[zone],  double, tree.NBAND[zone]);
+    ALLOCATE (tree.Xo[zone],      int, tree.NBAND[zone]);
+    ALLOCATE (tree.Yo[zone],      int, tree.NBAND[zone]);
+    ALLOCATE (tree.dX[zone],      int, tree.NBAND[zone]);
+    ALLOCATE (tree.dY[zone],      int, tree.NBAND[zone]);
     ALLOCATE (tree.cell[zone], int,    tree.NBAND[zone]);
     ALLOCATE (tree.name[zone], char *, tree.NBAND[zone]);
@@ -169,4 +192,8 @@
       tree.ra[zone][band] = NAN;
       tree.dec[zone][band] = NAN;
+      tree.Xo[zone][band] = -1;
+      tree.Yo[zone][band] = -1;
+      tree.dX[zone][band] = -1;
+      tree.dY[zone][band] = -1;
       tree.cell[zone][band] = -1;
       ALLOCATE (tree.name[zone][band], char, BOUNDARY_TREE_NAME_LENGTH);
@@ -191,9 +218,17 @@
       REALLOCATE (tree.ra[zone],   double, tree.NBAND[zone]);
       REALLOCATE (tree.dec[zone],  double, tree.NBAND[zone]);
-      REALLOCATE (tree.cell[zone], int,    tree.NBAND[zone]);
+      REALLOCATE (tree.Xo[zone],      int, tree.NBAND[zone]);
+      REALLOCATE (tree.Yo[zone],      int, tree.NBAND[zone]);
+      REALLOCATE (tree.dX[zone],      int, tree.NBAND[zone]);
+      REALLOCATE (tree.dY[zone],      int, tree.NBAND[zone]);
+      REALLOCATE (tree.cell[zone],    int, tree.NBAND[zone]);
       REALLOCATE (tree.name[zone], char *, tree.NBAND[zone]);
       for (j = start; j < tree.NBAND[zone]; j++) {
 	tree.ra[zone][j] = NAN;
 	tree.dec[zone][j] = NAN;
+	tree.Xo[zone][band] = -1;
+	tree.Yo[zone][band] = -1;
+	tree.dX[zone][band] = -1;
+	tree.dY[zone][band] = -1;
 	tree.cell[zone][j] = -1;
 	ALLOCATE (tree.name[zone][j], char, BOUNDARY_TREE_NAME_LENGTH);
@@ -204,4 +239,7 @@
     tree.Xo[zone][band] = x;
     tree.Yo[zone][band] = y;
+    tree.dX[zone][band] = image[i].NX / NX_SUB;
+    tree.dY[zone][band] = image[i].NY / NY_SUB;
+    
     tree.cell[zone][band] = i;
 
@@ -349,11 +387,20 @@
     // convert R,D to X,Y with hard-wired projection and scale, orientation?
     
-    TreeCellProjection (&x, &y, ra, dec, tree, zone, band);
-
-    xi = x / tree->NX_SUB;
-    yi = y / tree->NY_SUB;
-
+    double x = 0.0;
+    double y = 0.0;
+    BoundaryTreeProjection (&x, &y, ra, dec, tree, zone, band);
+    fprintf (stdout, "%6.1f %6.1f\n", x, y);
+
+    int xi = x / tree->dX[zone][band];
+    int yi = y / tree->dY[zone][band];
     
-
+    char format[24], skycellname[128];
+    int Ndigit = (int)(log10(tree->NX_SUB*tree->NY_SUB)) + 1 ;
+    snprintf (format, 24, "%s.%%0%dd", tree->name[zone][band], Ndigit);
+
+    int N = xi + tree->NX_SUB * yi;
+    snprintf (skycellname, 128, format, N);
+
+    fprintf (stderr, "skycell: %s\n", skycellname);
   }
 
Index: /branches/eam_branches/ipp-20121130/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /branches/eam_branches/ipp-20121130/Ohana/src/libdvo/include/dvo.h	(revision 34826)
+++ /branches/eam_branches/ipp-20121130/Ohana/src/libdvo/include/dvo.h	(revision 34827)
@@ -327,8 +327,12 @@
   char  ***name; // projection cell name
   
-  int NX_SUB, NY_SUB;
+  int NX_SUB;
+  int NY_SUB;
+  double dPix;
 
   int **Xo;
   int **Yo;
+  int **dX;
+  int **dY;
 } BoundaryTree;
 
@@ -706,4 +710,5 @@
 int BoundaryTreeSave(char *filename, BoundaryTree *tree);
 BoundaryTree *BoundaryTreeLoad(char *filename);
+int BoundaryTreeProjection (double *x, double *y, double r, double d, BoundaryTree *tree, int zone, int band);
 
 void dvo_average_init (Average *average);
Index: /branches/eam_branches/ipp-20121130/Ohana/src/libdvo/src/BoundaryTree.c
===================================================================
--- /branches/eam_branches/ipp-20121130/Ohana/src/libdvo/src/BoundaryTree.c	(revision 34826)
+++ /branches/eam_branches/ipp-20121130/Ohana/src/libdvo/src/BoundaryTree.c	(revision 34827)
@@ -47,4 +47,8 @@
   gfits_scan (&header, "DEC_ORI", "%lf", 1, &tree->DEC_origin);
   gfits_scan (&header, "DEC_OFF", "%lf", 1, &tree->DEC_offset);
+
+  gfits_scan (&header, "NX_SUB", "%d", 1, &tree->NX_SUB);
+  gfits_scan (&header, "NY_SUB", "%d", 1, &tree->NY_SUB);
+  gfits_scan (&header, "PIXSCALE", "%lf", 1, &tree->dPix);
 
   ftable.header = &theader;
@@ -75,10 +79,18 @@
     ALLOCATE (tree->ra,   double *, tree->Nzone);
     ALLOCATE (tree->dec,  double *, tree->Nzone);
-    ALLOCATE (tree->cell, int *, tree->Nzone);
-    ALLOCATE (tree->name, char **, tree->Nzone);
+    ALLOCATE (tree->Xo,      int *, tree->Nzone);
+    ALLOCATE (tree->Yo,      int *, tree->Nzone);
+    ALLOCATE (tree->dX,      int *, tree->Nzone);
+    ALLOCATE (tree->dY,      int *, tree->Nzone);
+    ALLOCATE (tree->cell,    int *, tree->Nzone);
+    ALLOCATE (tree->name,  char **, tree->Nzone);
     for (i = 0; i < tree->Nzone; i++) {
       ALLOCATE (tree->ra[i],   double, tree->Nband[i]);
       ALLOCATE (tree->dec[i],  double, tree->Nband[i]);
-      ALLOCATE (tree->cell[i], int,    tree->Nband[i]);
+      ALLOCATE (tree->Xo[i],      int, tree->Nband[i]);
+      ALLOCATE (tree->Yo[i],      int, tree->Nband[i]);
+      ALLOCATE (tree->dX[i],      int, tree->Nband[i]);
+      ALLOCATE (tree->dY[i],      int, tree->Nband[i]);
+      ALLOCATE (tree->cell[i],    int, tree->Nband[i]);
       ALLOCATE (tree->name[i], char *, tree->Nband[i]);
       for (j = 0; j < tree->Nband[i]; j++) {
@@ -102,4 +114,8 @@
     GET_COLUMN_NEW(band,  "BAND",        int);
     GET_COLUMN_NEW(index, "INDEX",       int);
+    GET_COLUMN_NEW(Xo,    "X_CENT",      int);
+    GET_COLUMN_NEW(Yo,    "Y_CENT",      int);
+    GET_COLUMN_NEW(dX,    "X_GRID",      int);
+    GET_COLUMN_NEW(dY,    "Y_GRID",      int);
     GET_COLUMN_NEW(name,  "NAME",        char); // XXX how is this done?
     gfits_free_header (&theader);
@@ -113,4 +129,8 @@
       tree->ra[nz][nb] = R[i];
       tree->dec[nz][nb] = D[i];
+      tree->Xo[nz][nb] = Xo[i];
+      tree->Yo[nz][nb] = Yo[i];
+      tree->dX[nz][nb] = dX[i];
+      tree->dY[nz][nb] = dY[i];
       tree->cell[nz][nb] = i; // XXX ?
       memcpy(tree->name[nz][nb], &name[i*BOUNDARY_TREE_NAME_LENGTH], BOUNDARY_TREE_NAME_LENGTH);
@@ -121,4 +141,8 @@
     free (zone  );
     free (band  );
+    free (Xo    );
+    free (Yo    );
+    free (dX    );
+    free (dY    );
     free (index );
     free (name  );
@@ -165,4 +189,8 @@
   gfits_modify (&header, "DEC_ORI", "%lf", 1, tree->DEC_origin);
   gfits_modify (&header, "DEC_OFF", "%lf", 1, tree->DEC_offset);
+
+  gfits_modify (&header, "NX_SUB", "%d", 1, tree->NX_SUB);
+  gfits_modify (&header, "NY_SUB", "%d", 1, tree->NY_SUB);
+  gfits_modify (&header, "PIXSCALE", "%lf", 1, tree->dPix);
 
   gfits_fwrite_header  (f, &header);
@@ -223,4 +251,8 @@
     gfits_define_bintable_column (&theader, "J", "BAND", "band sequence number", "none", 1.0, 0.0);
     gfits_define_bintable_column (&theader, "J", "INDEX","cell index", "none", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "X_CENT", "projection cell center pixel", "none", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "Y_CENT", "projection cell center pixel", "none", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "X_GRID", "skycell grid spacing", "none", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "Y_GRID", "skycell grid spacing", "none", 1.0, 0.0);
     gfits_define_bintable_column (&theader, fmt, "NAME", "cell name", "none", 1.0, 0.0);
 
@@ -240,4 +272,8 @@
     int    *band          ; ALLOCATE (band,  int,    Ncell);
     int    *index         ; ALLOCATE (index, int,    Ncell);
+    int    *Xo            ; ALLOCATE (Xo,    int,    Ncell);
+    int    *Yo            ; ALLOCATE (Yo,    int,    Ncell);
+    int    *dX            ; ALLOCATE (dX,    int,    Ncell);
+    int    *dY            ; ALLOCATE (dY,    int,    Ncell);
     char   *name          ; ALLOCATE (name,  char,   Ncell*BOUNDARY_TREE_NAME_LENGTH);
 
@@ -251,4 +287,8 @@
 	R[i]     = tree->ra[nz][nb];
 	D[i]     = tree->dec[nz][nb];
+	Xo[i]    = tree->Xo[nz][nb];
+	Yo[i]    = tree->Yo[nz][nb];
+	dX[i]    = tree->dX[nz][nb];
+	dY[i]    = tree->dY[nz][nb];
 	zone[i]  = nz;
 	band[i]  = nb;
@@ -265,4 +305,8 @@
     gfits_set_bintable_column (&theader, &ftable, "BAND",  band,  Ncell);
     gfits_set_bintable_column (&theader, &ftable, "INDEX", index, Ncell);
+    gfits_set_bintable_column (&theader, &ftable, "X_CENT", Xo,   Ncell);
+    gfits_set_bintable_column (&theader, &ftable, "Y_CENT", Yo,   Ncell);
+    gfits_set_bintable_column (&theader, &ftable, "X_GRID", dX,   Ncell);
+    gfits_set_bintable_column (&theader, &ftable, "Y_GRID", dY,   Ncell);
     gfits_set_bintable_column (&theader, &ftable, "NAME",  name,  Ncell);
 
@@ -272,4 +316,8 @@
     free (band  );
     free (index );
+    free (Xo    );
+    free (Yo    );
+    free (dX    );
+    free (dY    );
     free (name  );
 
@@ -322,5 +370,5 @@
 // need Ro, Do, Xo, Yo, dPix
 
-int TreeCellProjection (double *x, double *y, double r, double d, BoundaryTree *tree, int zone, int band) {
+int BoundaryTreeProjection (double *x, double *y, double r, double d, BoundaryTree *tree, int zone, int band) {
 
     double Xo = tree->Yo[zone][band];
@@ -332,19 +380,20 @@
     // this block only depends on Ro, Do
 
-    sdp  = sin(RAD_DEG*Do);
-    cdp  = cos(RAD_DEG*Do);
-    salp = sin(RAD_DEG*(ra - Ro));
-    calp = cos(RAD_DEG*(ra - Ro));
-    sdel = sin(RAD_DEG*dec);
-    cdel = cos(RAD_DEG*dec);
+    double sdp  = sin(RAD_DEG*Do);
+    double cdp  = cos(RAD_DEG*Do);
+    double salp = sin(RAD_DEG*(r - Ro));
+    double calp = cos(RAD_DEG*(r - Ro));
+    double sdel = sin(RAD_DEG*d);
+    double cdel = cos(RAD_DEG*d);
     
-    stht = sdel*sdp + cdel*cdp*calp;    /* sin(theta) */
-    sphi = cdel*salp;                   /* = cos(theta)*sin(phi) */
-    cphi = cdel*sdp*calp - sdel*cdp;    /* = cos(theta)*cos(phi) */
+    double stht = sdel*sdp + cdel*cdp*calp;    /* sin(theta) */
+    double sphi = cdel*salp;                   /* = cos(theta)*sin(phi) */
+    double cphi = cdel*sdp*calp - sdel*cdp;    /* = cos(theta)*cos(phi) */
 
     // defines the TAN projection (one of zenithal projections available, libdvo/src/coordops.c
     // R = cot (theta) = cos(theta) / sin(theta)
+    double L, M;
     if (stht == 0) {
-	Rc = hypot(sphi, cphi);
+	double Rc = hypot(sphi, cphi);
 	L = 180.0 * sphi / Rc;
 	M = 180.0 * cphi / Rc;
@@ -362,11 +411,11 @@
     // Yo = (coords[0].pc1_1*M - coords[0].pc2_1*L) / Ro;
 
-    Xo = L;
-    Yo = M;
+    double X = L;
+    double Y = M;
 
     // scale is dPix
 
-    *x = Xo / dPix + Xo;
-    *y = Yo / dPix + Yo;
+    *x = Xo - X / dPix;
+    *y = Yo + Y / dPix;
     
     return TRUE;
