IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 36468


Ignore:
Timestamp:
Feb 1, 2014, 6:05:42 AM (12 years ago)
Author:
eugene
Message:

build tree table for local tess

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20131211/Ohana/src/addstar/src/findskycell.c

    r34844 r36468  
    405405}
    406406
     407// a given tess is defined by a catdir (more than one per catdir?)
     408// this function takes a catdir and generates an extension for a tess file for a local tess.
     409
     410int mklocal (char *treefile, char *catdir) {
     411
     412  int i, j, zone, band, status;
     413  FITS_DB db;
     414  Image *image;
     415  off_t Nimage;
     416  double x, y, ra, dec;
     417
     418  char imagefile[DVO_MAX_PATH];
     419  snprintf (imagefile, DVO_MAX_PATH, "%s/Images.dat", catdir);
     420
     421  status = dvo_image_lock (&db, imagefile, 2.0, LCK_XCLD);
     422  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
     423
     424  /* load or create the image table */
     425  if (db.dbstate == LCK_EMPTY) Shutdown ("can't read image catalog %s", db.filename);
     426
     427  if (!dvo_image_load (&db, TRUE, FALSE)) Shutdown ("can't read image catalog %s", db.filename);
     428
     429  // convert database table to internal structure (binary to Image)
     430  // 'image' points to the same memory as db->ftable->buffer
     431  image = gfits_table_get_Image (&db.ftable, &Nimage, &db.swapped);
     432  if (!image) {
     433      fprintf (stderr, "ERROR: failed to read images\n");
     434      exit (2);
     435  }
     436 
     437  // generate an empty BoundaryTree
     438  TessallationBoundary tess;
     439 
     440  // user supplied values, do not try to derive from Image.dat
     441  tree.NX_SUB = NX_SUB;
     442  tree.NY_SUB = NY_SUB;
     443  tree.dPix = SCALE/3600.0;
     444
     445  // derive these or assign these?
     446  tree.Dmin = NAN;
     447  tree.Dmax = NAN;
     448  tree.Rmin = NAN;
     449  tree.Rmax = NAN;
     450
     451  // find the RA,DEC of the image centers & assign to cells
     452  for (i = 0; i < Nimage; i++) {
     453    x = 0.5*image[i].NX;
     454    y = 0.5*image[i].NY;
     455    XY_to_RD (&ra, &dec, x, y, &image[i].coords);
     456
     457    if (!BoundaryTreeCellCoords (&tree, &zone, &band, ra, dec)) {
     458      fprintf (stderr, "mismatch!\n");
     459      continue;
     460    }
     461    // fprintf (stderr, "%d  %f %f  %f  %f %f  %d %d\n", i, x, y, tree.RA_offset[zone], ra, dec, zone, band);
     462   
     463    if (band >= tree.NBAND[zone]) {
     464      int start = tree.NBAND[zone];
     465      tree.NBAND[zone] = band + 10;
     466      REALLOCATE (tree.ra[zone],   double, tree.NBAND[zone]);
     467      REALLOCATE (tree.dec[zone],  double, tree.NBAND[zone]);
     468      REALLOCATE (tree.Xo[zone],   double, tree.NBAND[zone]);
     469      REALLOCATE (tree.Yo[zone],   double, tree.NBAND[zone]);
     470      REALLOCATE (tree.dX[zone],      int, tree.NBAND[zone]);
     471      REALLOCATE (tree.dY[zone],      int, tree.NBAND[zone]);
     472      REALLOCATE (tree.cell[zone],    int, tree.NBAND[zone]);
     473      REALLOCATE (tree.name[zone], char *, tree.NBAND[zone]);
     474      for (j = start; j < tree.NBAND[zone]; j++) {
     475        tree.ra[zone][j] = NAN;
     476        tree.dec[zone][j] = NAN;
     477        tree.Xo[zone][band] = NAN;
     478        tree.Yo[zone][band] = NAN;
     479        tree.dX[zone][band] = -1;
     480        tree.dY[zone][band] = -1;
     481        tree.cell[zone][j] = -1;
     482        ALLOCATE (tree.name[zone][j], char, BOUNDARY_TREE_NAME_LENGTH);
     483      }
     484    }
     485    tree.ra[zone][band] = ra;
     486    tree.dec[zone][band] = dec;
     487    tree.Xo[zone][band] = x;
     488    tree.Yo[zone][band] = y;
     489    tree.dX[zone][band] = image[i].NX / NX_SUB;
     490    tree.dY[zone][band] = image[i].NY / NY_SUB;
     491   
     492    tree.cell[zone][band] = i;
     493
     494    // what are the min and max DEC values for this zone? (test center and corners of top and bottom edge) 
     495
     496    if (dec > 0.0) {
     497      // min DEC at bottom of the cell at the center
     498      x = 0.5*image[i].NX;
     499      y = 0.0*image[i].NY;
     500      XY_to_RD (&ra, &dec, x, y, &image[i].coords);
     501      tree.DEC_min_raw[zone] = MIN(tree.DEC_min_raw[zone], dec);
     502 
     503      // max DEC : find the intersection between the RA boundary and the top of the cell
     504      double ra_band_min = tree.RA_origin[zone] + tree.RA_offset[zone] * band;
     505 
     506      // does the parity matter?
     507      x = 0.0*image[i].NX;
     508      y = 1.0*image[i].NY;
     509 
     510      int Niter;
     511      for (Niter = 0; Niter < 3; Niter ++) {
     512        XY_to_RD (&ra, &dec, x, y, &image[i].coords);
     513        RD_to_XY (&x, &y, ra_band_min, dec, &image[i].coords);
     514        y = 1.0*image[i].NY;
     515      }
     516      tree.DEC_max_raw[zone] = MAX(tree.DEC_max_raw[zone], dec);
     517    } else {
     518      // max DEC at top of the cell at the center
     519      x = 0.5*image[i].NX;
     520      y = 1.0*image[i].NY;
     521      XY_to_RD (&ra, &dec, x, y, &image[i].coords);
     522      tree.DEC_max_raw[zone] = MAX(tree.DEC_max_raw[zone], dec);
     523 
     524      // max DEC : find the intersection between the RA boundary and the bottom of the cell
     525      double ra_band_min = tree.RA_origin[zone] + tree.RA_offset[zone] * band;
     526 
     527      // does the parity matter? (NO)
     528      x = 0.0*image[i].NX;
     529      y = 0.0*image[i].NY;
     530 
     531      int Niter;
     532      for (Niter = 0; Niter < 3; Niter ++) {
     533        XY_to_RD (&ra, &dec, x, y, &image[i].coords);
     534        RD_to_XY (&x, &y, ra_band_min, dec, &image[i].coords);
     535        y = 0.0*image[i].NY;
     536      }
     537      tree.DEC_min_raw[zone] = MIN(tree.DEC_min_raw[zone], dec);
     538    }
     539    memcpy (tree.name[zone][band], image[i].name, BOUNDARY_TREE_NAME_LENGTH);
     540  }
     541
     542  // figure out the max band value for each zone?
     543  for (zone = 0; zone < tree.Nzone; zone++) {
     544    int found_last = FALSE;
     545    int last_band = -1;
     546    for (band = 0; band < tree.NBAND[zone]; band++) {
     547      // all cells should be filled
     548      if (tree.cell[zone][band] < 0) {
     549        if (!found_last) {
     550          found_last = TRUE;
     551          last_band = band;
     552        }
     553      } else {
     554        if (found_last) {
     555          fprintf (stderr, "error: empty cell (%d,%d) after last band (%d)\n", zone, band, last_band);
     556        }
     557      }
     558    }
     559    if (last_band == -1) {
     560      last_band = tree.NBAND[zone];
     561    }
     562    tree.Nband[zone] = last_band;
     563    // fprintf (stderr, "last_band : %d, Nband: %d\n", last_band, tree.Nband[zone]);
     564  }
     565
     566  // figure out the max band value for each zone?
     567  int Nm = 0;
     568  tree.DEC_min[Nm] = -90.0;
     569  tree.DEC_max[Nm] = 0.5*(tree.DEC_max_raw[Nm] + tree.DEC_min_raw[Nm + 1]);
     570
     571  int Np = tree.Nzone - 1;
     572  tree.DEC_min[Np] = 0.5*(tree.DEC_min_raw[Np] + tree.DEC_max_raw[Np - 1]);
     573  tree.DEC_max[Np] = +90.0;
     574
     575  for (zone = 1; zone < tree.Nzone - 1; zone++) {
     576    tree.DEC_min[zone] = 0.5*(tree.DEC_min_raw[zone] + tree.DEC_max_raw[zone - 1]);
     577    tree.DEC_max[zone] = 0.5*(tree.DEC_max_raw[zone] + tree.DEC_min_raw[zone + 1]);
     578  }
     579
     580  struct timeval start, stop;
     581  gettimeofday (&start, (void *) NULL);
     582
     583  int Npts = 10000000;
     584
     585  // test : find skycell for NN random points on the sky
     586  long A = time(NULL);
     587  long B = A + 10000;
     588  srand48(B);
     589  for (i = 0; i < Npts; i++) {
     590    ra  = 360.0 * drand48();
     591    dec = 180.0 * drand48() - 90.0;
     592    if (!BoundaryTreeCellCoords (&tree, &zone, &band, ra, dec)) {
     593      fprintf (stderr, "failure for %f,%f\n", ra, dec);
     594    }
     595  }
     596  MARKTIME("-- test %d pts: %f sec\n", Npts, dtime);
     597
     598  BoundaryTreeSave (treefile, &tree);
     599
     600  return TRUE;
     601}
     602
Note: See TracChangeset for help on using the changeset viewer.