Changeset 36484 for trunk/Ohana/src/addstar/src/findskycell.c
- Timestamp:
- Feb 6, 2014, 12:22:33 PM (12 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/addstar/src/findskycell.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/addstar/src/findskycell.c
r34844 r36484 12 12 // in an even more specific case, RA[i,zone] = RA_origin[zone] + RA_offset[zone] 13 13 14 enum {TREE_NONE, TREE_MAKE, TREE_USE}; 14 enum {TREE_NONE, TREE_MAKE, TREE_LOCAL, TREE_USE}; 15 enum {REGION_NONE, REGION_USER, REGION_MIN, REGION_MAX}; 15 16 16 17 void usage (void) { 17 18 fprintf (stderr, "USAGE: findcell -mktree (tree) (catdir) [-nx Nx] [-ny Ny]\n"); 19 fprintf (stderr, "USAGE: findcell -mklocal (file) (catdir) [-nx Nx] [-ny Ny]\n"); 18 20 fprintf (stderr, "USAGE: findcell -tree (tree) (datafile)\n"); 19 21 fprintf (stderr, " (datafile) should contain a list of RA,DEC pairs\n"); … … 22 24 23 25 int mktree (char *treefile, char *catdir); 26 int mklocal (char *treefile, char *catdir); 24 27 int apply_tree (char *treefile, char *datafile); 25 28 … … 28 31 int NY_SUB = 1; 29 32 33 double R_MIN = NAN; 34 double R_MAX = NAN; 35 double D_MIN = NAN; 36 double D_MAX = NAN; 37 int REGION_OPTION = REGION_NONE; 38 39 char *BASENAME = NULL; 40 int projectIDoff = -1; 41 int skycellIDoff = -1; 42 43 int APPEND = FALSE; 44 30 45 int main (int argc, char **argv) { 31 46 … … 42 57 if (get_argument (argc, argv, "-h")) usage (); 43 58 44 NX_SUB = NY_SUB = 1;45 59 if ((N = get_argument (argc, argv, "-nx"))) { 46 60 remove_argument (N, &argc, argv); … … 55 69 56 70 /* pixel scale (arcsec/pixel) */ 57 SCALE = 1.0;58 71 if ((N = get_argument (argc, argv, "-scale"))) { 59 72 remove_argument (N, &argc, argv); 60 73 SCALE = atof (argv[N]); 74 remove_argument (N, &argc, argv); 75 } 76 77 /* pixel scale (arcsec/pixel) */ 78 if ((N = get_argument (argc, argv, "-append"))) { 79 remove_argument (N, &argc, argv); 80 APPEND = TRUE; 81 } 82 83 // user-specified region 84 if ((N = get_argument (argc, argv, "-region"))) { 85 remove_argument (N, &argc, argv); 86 if (N > argc - 4) { 87 fprintf (stderr, "USAGE: -region requires 4 arguments (Rmin Rmax Dmin Dmax)\n"); 88 exit (1); 89 } 90 R_MIN = atof (argv[N]); remove_argument (N, &argc, argv); 91 R_MAX = atof (argv[N]); remove_argument (N, &argc, argv); 92 D_MIN = atof (argv[N]); remove_argument (N, &argc, argv); 93 D_MAX = atof (argv[N]); remove_argument (N, &argc, argv); 94 REGION_OPTION = REGION_USER; 95 } 96 97 if ((N = get_argument (argc, argv, "-region-min"))) { 98 remove_argument (N, &argc, argv); 99 REGION_OPTION = REGION_MIN; 100 } 101 102 if ((N = get_argument (argc, argv, "-region-max"))) { 103 remove_argument (N, &argc, argv); 104 REGION_OPTION = REGION_MAX; 105 } 106 107 if ((N = get_argument (argc, argv, "-basename"))) { 108 remove_argument (N, &argc, argv); 109 if (N > argc - 3) { 110 fprintf (stderr, "USAGE: -basename (name) (proj_offset) (skycell_offset)\n"); 111 exit (1); 112 } 113 BASENAME = strcreate (argv[N]); 114 remove_argument (N, &argc, argv); 115 projectIDoff = atoi(argv[N]); 116 remove_argument (N, &argc, argv); 117 skycellIDoff = atoi(argv[N]); 61 118 remove_argument (N, &argc, argv); 62 119 } … … 70 127 remove_argument (N, &argc, argv); 71 128 } 129 if ((N = get_argument (argc, argv, "-mklocal"))) { 130 MODE = TREE_LOCAL; 131 remove_argument (N, &argc, argv); 132 treefile = strcreate (argv[N]); 133 remove_argument (N, &argc, argv); 134 } 72 135 if ((N = get_argument (argc, argv, "-tree"))) { 73 136 MODE = TREE_USE; … … 83 146 if (MODE == TREE_MAKE) { 84 147 mktree (treefile, argv[1]); 148 exit (0); 149 } 150 151 if (MODE == TREE_LOCAL) { 152 mklocal (treefile, argv[1]); 85 153 exit (0); 86 154 } … … 348 416 MARKTIME("-- test %d pts: %f sec\n", Npts, dtime); 349 417 350 BoundaryTreeSave (treefile, &tree); 418 if (APPEND) { 419 int Ntess = 0; 420 TessellationTable *tess = TessellationTableLoad (treefile, &Ntess); 421 REALLOCATE (tess, TessellationTable, Ntess + 1); 422 TessellationTableInit (&tess[Ntess], 1); 423 tess[Ntess].tree = &tree; 424 tess[Ntess].type = TESS_RINGS; 425 tess[Ntess].Rmin = 0; 426 tess[Ntess].Rmax = 360; 427 tess[Ntess].Dmin = -90; 428 tess[Ntess].Dmax = +90; 429 430 if (BASENAME) { 431 tess[Ntess].Nbasename = strlen(BASENAME); 432 tess[Ntess].basename = strcreate(BASENAME); 433 tess[Ntess].projectIDoff = projectIDoff; 434 tess[Ntess].skycellIDoff = skycellIDoff; 435 } 436 437 // add basename an related here... 438 Ntess ++; 439 TessellationTableSave (treefile, tess, Ntess); 440 } else { 441 BoundaryTreeSave (treefile, &tree); 442 } 351 443 352 444 return TRUE; … … 354 446 355 447 int apply_tree (char *treefile, char *datafile) { 448 449 int Ntess = 0; 450 TessellationTable *tess = TessellationTableLoad (treefile, &Ntess); 451 if (!tess) { 452 fprintf (stderr, "error loading tessellation table file %s\n", treefile); 453 exit (2); 454 } 455 456 FILE *f = fopen (datafile, "r"); 457 if (!f) { 458 fprintf (stderr, "error opening data file %s\n", datafile); 459 exit (3); 460 } 461 462 double ra, dec; 463 int Nvalue = 0; 464 while ((Nvalue = fscanf (f, "%lf %lf", &ra, &dec)) != EOF) { 465 466 int tessID, projID, skycellID; 467 468 if (!TessellationPrimaryCellIDs (tess, Ntess, &tessID, &projID, &skycellID, ra, dec)) { 469 fprintf (stderr, "error finding cell for %f,%f\n", ra, dec); 470 continue; 471 } 472 473 fprintf (stdout, "%10.6f %10.6f : %2d %04d %03d : %s\n", ra, dec, tessID, projID, skycellID, tess[tessID].basename); 474 } 475 476 exit (0); 477 } 478 479 int apply_tree_old (char *treefile, char *datafile) { 356 480 357 481 BoundaryTree *tree = BoundaryTreeLoad (treefile); … … 405 529 } 406 530 531 // a given tess is defined by a catdir (more than one per catdir?) 532 // this function takes a catdir and generates an extension for a tess file for a local tess. 533 534 /* 535 536 LOCAL projections are defined by single projection cells. should I be supplying the info on 537 the cmd line or figure out the bounds from the catdir? 538 539 */ 540 541 int mklocal (char *treefile, char *catdir) { 542 543 int i, j, status; 544 FITS_DB db; 545 Image *image; 546 off_t Nimage; 547 double x, y, ra, dec; 548 549 if (REGION_OPTION == REGION_NONE) { 550 fprintf (stderr, "ERROR: need to define bounding region (-region Rmin Rmax Dmin Dmax | -region-min | -region-max)\n"); 551 exit (2); 552 } 553 554 if (!BASENAME) { 555 fprintf (stderr, "ERROR: need to define BASENAME -basename (name) (proj_offset) (skycell_offset)\n"); 556 exit (2); 557 } 558 559 char imagefile[DVO_MAX_PATH]; 560 snprintf (imagefile, DVO_MAX_PATH, "%s/Images.dat", catdir); 561 562 status = dvo_image_lock (&db, imagefile, 2.0, LCK_XCLD); 563 if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename); 564 565 /* load or create the image table */ 566 if (db.dbstate == LCK_EMPTY) Shutdown ("can't read image catalog %s", db.filename); 567 568 if (!dvo_image_load (&db, TRUE, FALSE)) Shutdown ("can't read image catalog %s", db.filename); 569 570 // convert database table to internal structure (binary to Image) 571 // 'image' points to the same memory as db->ftable->buffer 572 image = gfits_table_get_Image (&db.ftable, &Nimage, &db.swapped); 573 if (!image) { 574 fprintf (stderr, "ERROR: failed to read images\n"); 575 exit (2); 576 } 577 578 // generate an empty BoundaryTree 579 TessellationTable *tess = NULL; 580 581 int NtessDisk = 0; 582 if (APPEND) { 583 tess = TessellationTableLoad (treefile, &NtessDisk); 584 REALLOCATE (tess, TessellationTable, Nimage + NtessDisk); 585 } else { 586 ALLOCATE (tess, TessellationTable, Nimage + NtessDisk); 587 } 588 589 TessellationTableInit (&tess[NtessDisk], Nimage); 590 591 // find the RA,DEC of the image centers & assign to cells 592 int Ntess = NtessDisk; 593 for (i = 0; i < Nimage; i++) { 594 // user supplied values, do not try to derive from Image.dat 595 tess[Ntess].NX_SUB = NX_SUB; 596 tess[Ntess].NY_SUB = NY_SUB; 597 tess[Ntess].dPix = SCALE/3600.0; 598 599 x = 0.5*image[i].NX; 600 y = 0.5*image[i].NY; 601 XY_to_RD (&ra, &dec, x, y, &image[i].coords); 602 603 tess[Ntess].Ro = ra; 604 tess[Ntess].Do = dec; 605 tess[Ntess].Xo = x; 606 tess[Ntess].Yo = y; 607 tess[Ntess].dX = image[i].NX / NX_SUB; 608 tess[Ntess].dY = image[i].NY / NY_SUB; 609 610 // find the minimum or maximum containing region 611 if ((REGION_OPTION == REGION_MIN) || (REGION_OPTION == REGION_MAX)) { 612 // XXX short cut for now : if the projection cell bounds the equator or 0,360 boundary, this will fail: 613 double R[4], D[4]; 614 XY_to_RD (&R[0], &D[0], 0, 0, &image[i].coords); 615 XY_to_RD (&R[1], &D[1], image[i].NX, 0, &image[i].coords); 616 XY_to_RD (&R[2], &D[2], 0, image[i].NY, &image[i].coords); 617 XY_to_RD (&R[3], &D[3], image[i].NX, image[i].NY, &image[i].coords); 618 619 if (REGION_OPTION == REGION_MIN) { 620 for (j = 0; j < 4; j++) { 621 R_MIN = (R[j] < ra) ? (isfinite(R_MIN) ? MAX(R_MIN, R[j]) : R[j]) : R_MIN; 622 R_MAX = (R[j] > ra) ? (isfinite(R_MAX) ? MIN(R_MAX, R[j]) : R[j]) : R_MAX; 623 D_MIN = (D[j] < dec) ? (isfinite(D_MIN) ? MAX(D_MIN, D[j]) : D[j]) : D_MIN; 624 D_MAX = (D[j] > dec) ? (isfinite(D_MAX) ? MIN(D_MAX, D[j]) : D[j]) : D_MAX; 625 } 626 } else { 627 for (j = 0; j < 4; j++) { 628 R_MIN = (R[j] < ra) ? (isfinite(R_MIN) ? MIN(R_MIN, R[j]) : R[j]) : R_MIN; 629 R_MAX = (R[j] > ra) ? (isfinite(R_MAX) ? MAX(R_MAX, R[j]) : R[j]) : R_MAX; 630 D_MIN = (D[j] < dec) ? (isfinite(D_MIN) ? MIN(D_MIN, D[j]) : D[j]) : D_MIN; 631 D_MAX = (D[j] > dec) ? (isfinite(D_MAX) ? MAX(D_MAX, D[j]) : D[j]) : D_MAX; 632 } 633 } 634 } 635 tess[Ntess].Rmin = R_MIN; 636 tess[Ntess].Rmax = R_MAX; 637 tess[Ntess].Dmin = D_MIN; 638 tess[Ntess].Dmax = D_MAX; 639 640 tess[Ntess].type = TESS_LOCAL; 641 642 // XXX I don't really want to do the work of discovering the rule... 643 tess[Ntess].Nbasename = strlen(BASENAME); 644 tess[Ntess].basename = strcreate(BASENAME); 645 646 tess[Ntess].projectIDoff = projectIDoff; 647 tess[Ntess].skycellIDoff = skycellIDoff; 648 649 Ntess ++; 650 } 651 652 TessellationTableSave (treefile, tess, Ntess); 653 654 return TRUE; 655 } 656
Note:
See TracChangeset
for help on using the changeset viewer.
