int main(int argc, char** args) { int argchar; char* progname = args[0]; kdtree_t* kd; char* fn; int printData = 0; int treeData = 0; while ((argchar = getopt(argc, args, OPTIONS)) != -1) switch (argchar) { case 'h': printHelp(progname); exit(-1); case 'd': printData = 1; break; case 'n': treeData = 1; break; } if (argc - optind == 1) { fn = args[optind]; optind++; } else { printHelp(progname); exit(-1); } printf("Reading kdtree from file %s ...\n", fn); kd = kdtree_fits_read(fn, NULL, NULL); printf("Tree name: \"%s\"\n", kd->name); printf("Treetype: 0x%x\n", kd->treetype); printf("Data type: %s\n", kdtree_kdtype_to_string(kdtree_datatype(kd))); printf("Tree type: %s\n", kdtree_kdtype_to_string(kdtree_treetype(kd))); printf("External type: %s\n", kdtree_kdtype_to_string(kdtree_exttype(kd))); printf("N data points: %i\n", kd->ndata); printf("Dimensions: %i\n", kd->ndim); printf("Nodes: %i\n", kd->nnodes); printf("Leaf nodes: %i\n", kd->nbottom); printf("Non-leaf nodes: %i\n", kd->ninterior); printf("Tree levels: %i\n", kd->nlevels); printf("LR array: %s\n", (kd->lr ? "yes" : "no")); printf("Perm array: %s\n", (kd->perm ? "yes" : "no")); printf("Bounding box: %s\n", (kd->bb.any ? "yes" : "no")); printf("Split plane: %s\n", (kd->split.any ? "yes" : "no")); printf("Split dim: %s\n", (kd->splitdim ? "yes" : "no")); printf("Data: %s\n", (kd->data.any ? "yes" : "no")); if (kd->minval && kd->maxval) { int d; printf("Data ranges:\n"); for (d=0; d<kd->ndim; d++) printf(" %i: [%g, %g]\n", d, kd->minval[d], kd->maxval[d]); } printf("Running kdtree_check...\n"); if (kdtree_check(kd)) { printf("kdtree_check failed.\n"); exit(-1); } if (printData) { int i, d; int dt = kdtree_datatype(kd); double data[kd->ndim]; for (i=0; i<kd->ndata; i++) { int iarray; kdtree_copy_data_double(kd, i, 1, data); printf("data[%i] = %n(", i, &iarray); for (d=0; d<kd->ndim; d++) printf("%s%g", d?", ":"", data[d]); printf(")\n"); if (treeData) { printf("%*s(", iarray, ""); for (d=0; d<kd->ndim; d++) switch (dt) { case KDT_DATA_DOUBLE: printf("%s%g", (d?", ":""), kd->data.d[kd->ndim * i + d]); break; case KDT_DATA_FLOAT: printf("%s%g", (d?", ":""), kd->data.f[kd->ndim * i + d]); break; case KDT_DATA_U32: printf("%s%u", (d?", ":""), kd->data.u[kd->ndim * i + d]); break; case KDT_DATA_U16: printf("%s%u", (d?", ":""), (unsigned int)kd->data.s[kd->ndim * i + d]); break; } printf(")\n"); } } } kdtree_fits_close(kd); return 0; }
int unpermute_stars(startree_t* treein, quadfile_t* qfin, startree_t** p_treeout, quadfile_t* qfout, anbool dosweeps, anbool check, char** args, int argc) { startree_t* treeout; int i; int N; int healpix = -1; int hpnside = 0; int starhp = -1; int lastgrass; qfits_header* qouthdr; qfits_header* qinhdr; anbool allsky; assert(p_treeout); N = startree_N(treein); allsky = qfits_header_getboolean(startree_header(treein), "ALLSKY", 0); if (allsky) logverb("Star kd-tree is all-sky\n"); else { starhp = qfits_header_getint(startree_header(treein), "HEALPIX", -1); if (starhp == -1) ERROR("Warning, input star kdtree didn't have a HEALPIX header.\n"); hpnside = qfits_header_getint(startree_header(treein), "HPNSIDE", 1); healpix = starhp; logverb("Star kd-tree covers healpix %i, nside %i\n", healpix, hpnside); } qfout->healpix = healpix; qfout->hpnside = hpnside; qfout->numstars = qfin->numstars; qfout->dimquads = qfin->dimquads; qfout->index_scale_upper = qfin->index_scale_upper; qfout->index_scale_lower = qfin->index_scale_lower; qfout->indexid = qfin->indexid; qouthdr = quadfile_get_header(qfout); qinhdr = quadfile_get_header(qfin); an_fits_copy_header(qinhdr, qouthdr, "ALLSKY"); BOILERPLATE_ADD_FITS_HEADERS(qouthdr); qfits_header_add(qouthdr, "HISTORY", "This file was created by the program \"unpermute-stars\".", NULL, NULL); qfits_header_add(qouthdr, "HISTORY", "unpermute-stars command line:", NULL, NULL); fits_add_args(qouthdr, args, argc); qfits_header_add(qouthdr, "HISTORY", "(end of unpermute-stars command line)", NULL, NULL); qfits_header_add(qouthdr, "HISTORY", "** unpermute-stars: history from input:", NULL, NULL); fits_copy_all_headers(qinhdr, qouthdr, "HISTORY"); qfits_header_add(qouthdr, "HISTORY", "** unpermute-stars: end of history from input.", NULL, NULL); qfits_header_add(qouthdr, "COMMENT", "** unpermute-stars: comments from input:", NULL, NULL); fits_copy_all_headers(qinhdr, qouthdr, "COMMENT"); qfits_header_add(qouthdr, "COMMENT", "** unpermute-stars: end of comments from input.", NULL, NULL); if (quadfile_write_header(qfout)) { ERROR("Failed to write quadfile header.\n"); return -1; } logmsg("Writing quads...\n"); startree_compute_inverse_perm(treein); if (check) { logmsg("Running quadfile_check()...\n"); if (quadfile_check(qfin)) { ERROR("quadfile_check() failed"); return -1; } logmsg("Check passed.\n"); logmsg("Checking inverse permutation...\n"); if (startree_check_inverse_perm(treein)) { ERROR("check failed!"); return -1; } logmsg("Running startree kdtree_check()...\n"); if (kdtree_check(treein->tree)) { ERROR("kdtree_check() failed"); return -1; } logmsg("Check passed.\n"); } lastgrass = 0; for (i=0; i<qfin->numquads; i++) { int j; unsigned int stars[qfin->dimquads]; if (i*80/qfin->numquads != lastgrass) { logmsg("."); fflush(stdout); lastgrass = i*80/qfin->numquads; } if (quadfile_get_stars(qfin, i, stars)) { ERROR("Failed to read quadfile entry.\n"); return -1; } for (j=0; j<qfin->dimquads; j++) stars[j] = treein->inverse_perm[stars[j]]; if (quadfile_write_quad(qfout, stars)) { ERROR("Failed to write quadfile entry.\n"); return -1; } } logmsg("\n"); if (quadfile_fix_header(qfout)) { ERROR("Failed to fix quadfile header"); return -1; } treeout = startree_new(); treeout->tree = malloc(sizeof(kdtree_t)); memcpy(treeout->tree, treein->tree, sizeof(kdtree_t)); treeout->tree->perm = NULL; an_fits_copy_header(startree_header(treein), startree_header(treeout), "HEALPIX"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "HPNSIDE"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "ALLSKY"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "JITTER"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "CUTNSIDE"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "CUTMARG"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "CUTBAND"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "CUTDEDUP"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "CUTNSWEP"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "CUTMINMG"); an_fits_copy_header(startree_header(treein), startree_header(treeout), "CUTMAXMG"); qfits_header_add(startree_header(treeout), "HISTORY", "unpermute-stars command line:", NULL, NULL); fits_add_args(startree_header(treeout), args, argc); qfits_header_add(startree_header(treeout), "HISTORY", "(end of unpermute-stars command line)", NULL, NULL); qfits_header_add(startree_header(treeout), "HISTORY", "** unpermute-stars: history from input:", NULL, NULL); fits_copy_all_headers(startree_header(treein), startree_header(treeout), "HISTORY"); qfits_header_add(startree_header(treeout), "HISTORY", "** unpermute-stars: end of history from input.", NULL, NULL); qfits_header_add(startree_header(treeout), "COMMENT", "** unpermute-stars: comments from input:", NULL, NULL); fits_copy_all_headers(startree_header(treein), startree_header(treeout), "COMMENT"); qfits_header_add(startree_header(treeout), "COMMENT", "** unpermute-stars: end of comments from input.", NULL, NULL); if (dosweeps) { // copy sweepX headers. for (i=1;; i++) { char key[16]; int n; sprintf(key, "SWEEP%i", i); n = qfits_header_getint(treein->header, key, -1); if (n == -1) break; an_fits_copy_header(treein->header, treeout->header, key); } // compute sweep array. treeout->sweep = malloc(N * sizeof(uint8_t)); for (i=0; i<N; i++) { int ind = treein->tree->perm[i]; // Stars are sorted first by sweep and then by brightness within // the sweep. Instead of just storing the sweep number, we can // store a quantization of the total-ordered rank. treeout->sweep[i] = (uint8_t)floor((float)256.0 * (float)ind / (float)N); } } *p_treeout = treeout; return 0; }
void test_hd_1(CuTest* tc) { hd_catalog_t* hdcat; int* invperm; int* present; int ind, i, N; double xyz[3]; double ra, dec; int strangehds[] = { 40142, 40441, 40672, 40746, 40763, 40764, 104176, 104193, 163635, 224698, 224699, 129371 }; if (!file_readable("hd.fits")) { printf("File \"hd.fits\" does not exist; test skipped.\n"); return; } hdcat = henry_draper_open("hd.fits"); CuAssertPtrNotNull(tc, hdcat); N = hdcat->kd->ndata; invperm = calloc(N, sizeof(int)); CuAssertPtrNotNull(tc, invperm); CuAssertIntEquals(tc, 0, kdtree_check(hdcat->kd)); kdtree_inverse_permutation(hdcat->kd, invperm); present = calloc(N, sizeof(int)); for (i=0; i<N; i++) { CuAssert(tc, "invperm in range", invperm[i] < N); present[invperm[i]]++; } for (i=0; i<N; i++) { CuAssertIntEquals(tc, 1, present[i]); } free(present); // Where is "HD n" ? for (i=0; i<10; i++) { bl* res; int j; ind = invperm[i]; kdtree_copy_data_double(hdcat->kd, ind, 1, xyz); xyzarr2radecdeg(xyz, &ra, &dec); printf("HD %i: RA,Dec %g, %g\n", i+1, ra, dec); res = henry_draper_get(hdcat, ra, dec, 10.0); CuAssertPtrNotNull(tc, res); for (j=0; j<bl_size(res); j++) { hd_entry_t* hd = bl_access(res, j); printf("res %i: HD %i, RA, Dec %g, %g\n", j, hd->hd, hd->ra, hd->dec); } bl_free(res); } for (i=0; i<sizeof(strangehds)/sizeof(int); i++) { ind = invperm[strangehds[i]-1]; kdtree_copy_data_double(hdcat->kd, ind, 1, xyz); xyzarr2radecdeg(xyz, &ra, &dec); printf("HD %i: RA,Dec %g, %g\n", strangehds[i], ra, dec); } free(invperm); henry_draper_close(hdcat); }