static void add_headers(qfits_header* hdr, char** argv, int argc, qfits_header* startreehdr, anbool circle, int npasses) { int i; BOILERPLATE_ADD_FITS_HEADERS(hdr); qfits_header_add(hdr, "HISTORY", "This file was created by the program \"hpquads\".", NULL, NULL); qfits_header_add(hdr, "HISTORY", "hpquads command line:", NULL, NULL); fits_add_args(hdr, argv, argc); qfits_header_add(hdr, "HISTORY", "(end of hpquads command line)", NULL, NULL); qfits_header_add(startreehdr, "HISTORY", "** History entries copied from the input file:", NULL, NULL); fits_copy_all_headers(startreehdr, hdr, "HISTORY"); qfits_header_add(startreehdr, "HISTORY", "** End of history entries.", NULL, NULL); qfits_header_add(hdr, "CXDX", "T", "All codes have the property cx<=dx.", NULL); qfits_header_add(hdr, "CXDXLT1", "T", "All codes have the property cx+dx<=1.", NULL); qfits_header_add(hdr, "MIDHALF", "T", "All codes have the property cx+dx<=1.", NULL); an_fits_copy_header(startreehdr, hdr, "ALLSKY"); qfits_header_add(hdr, "CIRCLE", (circle ? "T" : "F"), (circle ? "Stars C,D live in the circle defined by AB." : "Stars C,D live in the box defined by AB."), NULL); // add placeholders... for (i=0; i<npasses; i++) { char key[64]; sprintf(key, "PASS%i", i+1); qfits_header_add(hdr, key, "-1", "placeholder", NULL); } }
startree_t* startree_build(fitstable_t* intable, const char* racol, const char* deccol, // keep RA,Dec in the tag-along table? //anbool keep_radec, // KDT_DATA_*, KDT_TREE_* int datatype, int treetype, // KD_BUILD_* int buildopts, int Nleaf, char** args, int argc) { double* ra = NULL; double* dec = NULL; double* xyz = NULL; int N; startree_t* starkd = NULL; int tt; int d; double low[3]; double high[3]; qfits_header* hdr; qfits_header* inhdr; int i; if (!racol) racol = "RA"; if (!deccol) deccol = "DEC"; if (!datatype) datatype = KDT_DATA_U32; if (!treetype) treetype = KDT_TREE_U32; if (!buildopts) buildopts = KD_BUILD_SPLIT; if (!Nleaf) Nleaf = 25; ra = fitstable_read_column(intable, racol, TFITS_BIN_TYPE_D); if (!ra) { ERROR("Failed to read RA from column %s", racol); goto bailout; } dec = fitstable_read_column(intable, deccol, TFITS_BIN_TYPE_D); if (!dec) { ERROR("Failed to read RA from column %s", racol); goto bailout; } N = fitstable_nrows(intable); xyz = malloc(N * 3 * sizeof(double)); if (!xyz) { SYSERROR("Failed to malloc xyz array to build startree"); goto bailout; } radecdeg2xyzarrmany(ra, dec, xyz, N); free(ra); ra = NULL; free(dec); dec = NULL; starkd = startree_new(); if (!starkd) { ERROR("Failed to allocate startree"); goto bailout; } tt = kdtree_kdtypes_to_treetype(KDT_EXT_DOUBLE, treetype, datatype); starkd->tree = kdtree_new(N, 3, Nleaf); for (d=0; d<3; d++) { low[d] = -1.0; high[d] = 1.0; } kdtree_set_limits(starkd->tree, low, high); logverb("Building star kdtree...\n"); starkd->tree = kdtree_build(starkd->tree, xyz, N, 3, Nleaf, tt, buildopts); if (!starkd->tree) { ERROR("Failed to build star kdtree"); startree_close(starkd); starkd = NULL; goto bailout; } starkd->tree->name = strdup(STARTREE_NAME); inhdr = fitstable_get_primary_header(intable); hdr = startree_header(starkd); an_fits_copy_header(inhdr, hdr, "HEALPIX"); an_fits_copy_header(inhdr, hdr, "HPNSIDE"); an_fits_copy_header(inhdr, hdr, "ALLSKY"); an_fits_copy_header(inhdr, hdr, "JITTER"); an_fits_copy_header(inhdr, hdr, "CUTNSIDE"); an_fits_copy_header(inhdr, hdr, "CUTMARG"); an_fits_copy_header(inhdr, hdr, "CUTDEDUP"); an_fits_copy_header(inhdr, hdr, "CUTNSWEP"); //fits_copy_header(inhdr, hdr, "CUTBAND"); //fits_copy_header(inhdr, hdr, "CUTMINMG"); //fits_copy_header(inhdr, hdr, "CUTMAXMG"); BOILERPLATE_ADD_FITS_HEADERS(hdr); qfits_header_add(hdr, "HISTORY", "This file was created by the command-line:", NULL, NULL); fits_add_args(hdr, args, argc); qfits_header_add(hdr, "HISTORY", "(end of command line)", NULL, NULL); qfits_header_add(hdr, "HISTORY", "** History entries copied from the input file:", NULL, NULL); fits_copy_all_headers(inhdr, hdr, "HISTORY"); qfits_header_add(hdr, "HISTORY", "** End of history entries.", NULL, NULL); for (i=1;; i++) { char key[16]; int n; sprintf(key, "SWEEP%i", i); n = qfits_header_getint(inhdr, key, -1); if (n == -1) break; an_fits_copy_header(inhdr, hdr, key); } bailout: if (ra) free(ra); if (dec) free(dec); if (xyz) free(xyz); return starkd; }
int unpermute_quads(quadfile* quadin, codetree* treein, quadfile* quadout, codetree** p_treeout, char** args, int argc) { int i; qfits_header* codehdr; qfits_header* hdr; int healpix; int hpnside; int codehp = -1; qfits_header* qouthdr; qfits_header* qinhdr; codetree* treeout; anbool allsky; codehdr = codetree_header(treein); healpix = quadin->healpix; hpnside = quadin->hpnside; allsky = qfits_header_getboolean(codehdr, "ALLSKY", 0); if (allsky) logverb("Index is all-sky\n"); else { codehp = qfits_header_getint(codehdr, "HEALPIX", -1); if (codehp == -1) ERROR("Warning, input code kdtree didn't have a HEALPIX header"); else if (codehp != healpix) { ERROR("Quadfile says it's healpix %i, but code kdtree says %i", healpix, codehp); return -1; } } quadout->healpix = healpix; quadout->hpnside = hpnside; quadout->indexid = quadin->indexid; quadout->numstars = quadin->numstars; quadout->dimquads = quadin->dimquads; quadout->index_scale_upper = quadin->index_scale_upper; quadout->index_scale_lower = quadin->index_scale_lower; qouthdr = quadfile_get_header(quadout); qinhdr = quadfile_get_header(quadin); boilerplate_add_fits_headers(qouthdr); qfits_header_add(qouthdr, "HISTORY", "This file was created by the program \"unpermute-quads\".", NULL, NULL); qfits_header_add(qouthdr, "HISTORY", "unpermute-quads command line:", NULL, NULL); fits_add_args(qouthdr, args, argc); qfits_header_add(qouthdr, "HISTORY", "(end of unpermute-quads command line)", NULL, NULL); qfits_header_add(qouthdr, "HISTORY", "** unpermute-quads: history from input:", NULL, NULL); fits_copy_all_headers(qinhdr, qouthdr, "HISTORY"); qfits_header_add(qouthdr, "HISTORY", "** unpermute-quads end of history from input.", NULL, NULL); qfits_header_add(qouthdr, "COMMENT", "** unpermute-quads: comments from input:", NULL, NULL); fits_copy_all_headers(qinhdr, qouthdr, "COMMENT"); qfits_header_add(qouthdr, "COMMENT", "** unpermute-quads: end of comments from input.", NULL, NULL); fits_copy_header(qinhdr, qouthdr, "CXDX"); fits_copy_header(qinhdr, qouthdr, "CXDXLT1"); fits_copy_header(qinhdr, qouthdr, "CIRCLE"); fits_copy_header(qinhdr, qouthdr, "ALLSKY"); if (quadfile_write_header(quadout)) { ERROR("Failed to write quadfile header"); return -1; } for (i=0; i<codetree_N(treein); i++) { unsigned int stars[quadin->dimquads]; int ind = codetree_get_permuted(treein, i); if (quadfile_get_stars(quadin, ind, stars)) { ERROR("Failed to read quad entry"); return -1; } if (quadfile_write_quad(quadout, stars)) { ERROR("Failed to write quad entry"); return -1; } } if (quadfile_fix_header(quadout)) { ERROR("Failed to fix quadfile header"); return -1; } treeout = codetree_new(); treeout->tree = malloc(sizeof(kdtree_t)); memcpy(treeout->tree, treein->tree, sizeof(kdtree_t)); treeout->tree->perm = NULL; hdr = codetree_header(treeout); fits_copy_header(qinhdr, hdr, "HEALPIX"); fits_copy_header(qinhdr, hdr, "HPNSIDE"); fits_copy_header(qinhdr, hdr, "ALLSKY"); boilerplate_add_fits_headers(hdr); qfits_header_add(hdr, "HISTORY", "This file was created by the program \"unpermute-quads\".", NULL, NULL); qfits_header_add(hdr, "HISTORY", "unpermute-quads command line:", NULL, NULL); fits_add_args(hdr, args, argc); qfits_header_add(hdr, "HISTORY", "(end of unpermute-quads command line)", NULL, NULL); qfits_header_add(hdr, "HISTORY", "** unpermute-quads: history from input ckdt:", NULL, NULL); fits_copy_all_headers(codehdr, hdr, "HISTORY"); qfits_header_add(hdr, "HISTORY", "** unpermute-quads end of history from input ckdt.", NULL, NULL); qfits_header_add(hdr, "COMMENT", "** unpermute-quads: comments from input ckdt:", NULL, NULL); fits_copy_all_headers(codehdr, hdr, "COMMENT"); qfits_header_add(hdr, "COMMENT", "** unpermute-quads: end of comments from input ckdt.", NULL, NULL); fits_copy_header(codehdr, hdr, "CXDX"); fits_copy_header(codehdr, hdr, "CXDXLT1"); fits_copy_header(codehdr, hdr, "CIRCLE"); *p_treeout = treeout; 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; }
int main(int argc, char *argv[]) { char* progname = argv[0]; int argidx, argchar; char *idxfname = NULL; char *quadfname = NULL; il** quadlist; quadfile* quads; qidxfile* qidx; int q; int i; int numused; qfits_header* quadhdr; qfits_header* qidxhdr; int dimquads; anbool check = FALSE; int loglvl = LOG_MSG; if (argc <= 2) { printHelp(progname); exit(-1); } while ((argchar = getopt (argc, argv, OPTIONS)) != -1) switch (argchar) { case 'c': check = TRUE; break; case 'v': loglvl++; break; case 'i': quadfname = optarg; break; case 'o': idxfname = optarg; break; case '?': fprintf(stderr, "Unknown option `-%c'.\n", optopt); case 'h': printHelp(progname); exit(-1); default: return (OPT_ERR); } log_init(loglvl); if (optind < argc) { for (argidx = optind; argidx < argc; argidx++) fprintf (stderr, "Non-option argument %s\n", argv[argidx]); printHelp(progname); exit(-1); } logmsg("quadidx: indexing quads in \"%s\"...\n", quadfname); logmsg("will write to file \"%s\".\n", idxfname); quads = quadfile_open(quadfname); if (!quads) { ERROR("Couldn't open quads file \"%s\"", quadfname); exit(-1); } logmsg("%u quads, %u stars.\n", quads->numquads, quads->numstars); if (check) { logmsg("Running quadfile_check()...\n"); if (quadfile_check(quads)) { ERROR("quadfile_check() failed"); exit(-1); } logmsg("Check passed.\n"); } quadlist = calloc(quads->numstars, sizeof(il*)); if (!quadlist) { SYSERROR("Failed to allocate list of quad contents"); exit(-1); } dimquads = quadfile_dimquads(quads); for (q=0; q<quads->numquads; q++) { unsigned int inds[dimquads]; quadfile_get_stars(quads, q, inds); // append this quad index to the lists of each of its stars. for (i=0; i<dimquads; i++) { il* list; int starind = inds[i]; list = quadlist[starind]; // create the list if necessary if (!list) { list = il_new(10); quadlist[starind] = list; } il_append(list, q); } } // first count numused: // how many stars are members of quads. numused = 0; for (i=0; i<quads->numstars; i++) { il* list = quadlist[i]; if (!list) continue; numused++; } logmsg("%u stars used\n", numused); qidx = qidxfile_open_for_writing(idxfname, quads->numstars, quads->numquads); if (!qidx) { logmsg("Couldn't open outfile qidx file %s.\n", idxfname); exit(-1); } quadhdr = quadfile_get_header(quads); qidxhdr = qidxfile_get_header(qidx); an_fits_copy_header(quadhdr, qidxhdr, "INDEXID"); an_fits_copy_header(quadhdr, qidxhdr, "HEALPIX"); BOILERPLATE_ADD_FITS_HEADERS(qidxhdr); qfits_header_add(qidxhdr, "HISTORY", "This file was created by the program \"quadidx\".", NULL, NULL); qfits_header_add(qidxhdr, "HISTORY", "quadidx command line:", NULL, NULL); fits_add_args(qidxhdr, argv, argc); qfits_header_add(qidxhdr, "HISTORY", "(end of quadidx command line)", NULL, NULL); qfits_header_add(qidxhdr, "HISTORY", "** History entries copied from the input file:", NULL, NULL); fits_copy_all_headers(quadhdr, qidxhdr, "HISTORY"); qfits_header_add(qidxhdr, "HISTORY", "** End of history entries.", NULL, NULL); if (qidxfile_write_header(qidx)) { logmsg("Couldn't write qidx header (%s).\n", idxfname); exit(-1); } for (i=0; i<quads->numstars; i++) { int thisnumq; //int thisstar; int* stars; // bad variable name - list of quads this star is in. il* list = quadlist[i]; if (list) { thisnumq = (uint)il_size(list); stars = malloc(thisnumq * sizeof(uint)); il_copy(list, 0, thisnumq, (int*)stars); } else { thisnumq = 0; stars = NULL; } //thisstar = i; if (qidxfile_write_star(qidx, stars, thisnumq)) { logmsg("Couldn't write star to qidx file (%s).\n", idxfname); exit(-1); } if (list) { free(stars); il_free(list); quadlist[i] = NULL; } } free(quadlist); quadfile_close(quads); if (qidxfile_close(qidx)) { logmsg("Failed to close qidx file.\n"); exit(-1); } logmsg(" done.\n"); return 0; }