qidxfile* qidxfile_open_for_writing(const char* fn, int nstars, int nquads) { qidxfile* qf; qfits_header* hdr; qf = new_qidxfile(fn, TRUE); if (!qf) goto bailout; qf->numstars = nstars; qf->numquads = nquads; hdr = fitsbin_get_primary_header(qf->fb); fits_add_endian(hdr); fits_header_add_int(hdr, "NSTARS", qf->numstars, "Number of stars used."); fits_header_add_int(hdr, "NQUADS", qf->numquads, "Number of quads used."); qfits_header_add(hdr, "AN_FILE", "QIDX", "This is a quad index file.", NULL); qfits_header_add(hdr, "COMMENT", "The data table of this file has two parts:", NULL, NULL); qfits_header_add(hdr, "COMMENT", " -the index", NULL, NULL); qfits_header_add(hdr, "COMMENT", " -the heap", NULL, NULL); fits_add_long_comment(hdr, "The index contains two uint32 values for each star: the offset and " "length, in the heap, of the list of quads to which it belongs. " "The offset and length are in units of uint32s, not bytes. " "Offset 0 is the first uint32 in the heap. " "The heap is ordered and tightly packed. " "The heap is a flat list of quad indices (uint32s)."); return qf; bailout: if (qf) qidxfile_close(qf); return NULL; }
void plot_index_free(plot_args_t* plotargs, void* baton) { plotindex_t* args = (plotindex_t*)baton; int i; for (i=0; i<pl_size(args->indexes); i++) { index_t* index = pl_get(args->indexes, i); index_free(index); } pl_free(args->indexes); for (i=0; i<pl_size(args->qidxes); i++) { qidxfile* qidx = pl_get(args->qidxes, i); qidxfile_close(qidx); } pl_free(args->qidxes); free(args); }
qidxfile* qidxfile_open(const char* fn) { qidxfile* qf = NULL; qf = new_qidxfile(fn, FALSE); if (!qf) goto bailout; if (fitsbin_read(qf->fb)) { ERROR("Failed to find qidx table.\n"); goto bailout; } qf->index = fitsbin_get_chunk(qf->fb, CHUNK_QIDX)->data; qf->heap = qf->index + 2 * qf->numstars; return qf; bailout: if (qf) qidxfile_close(qf); return NULL; }
int main(int argc, char *argv[]) { int argidx, argchar; char *qidxfname = NULL; char *quadfname = NULL; quadfile* quad; qidxfile* qidx; int q, s; int dimquads; while ((argchar = getopt (argc, argv, OPTIONS)) != -1) switch (argchar) { case 'f': qidxfname = mk_qidxfn(optarg); quadfname = mk_quadfn(optarg); break; case '?': fprintf(stderr, "Unknown option `-%c'.\n", optopt); case 'h': fprintf(stderr, HelpString); return (HELP_ERR); default: return (OPT_ERR); } if (optind < argc) { for (argidx = optind; argidx < argc; argidx++) fprintf (stderr, "Non-option argument %s\n", argv[argidx]); fprintf(stderr, HelpString); return (OPT_ERR); } quad = quadfile_open(quadfname, 0); if (!quad) { fprintf(stderr, "Couldn't open quads file %s.\n", quadfname); exit(-1); } qidx = qidxfile_open(qidxfname, 0); if (!qidx) { fprintf(stderr, "Couldn't open qidx file %s.\n", qidxfname); exit(-1); } if (quad->numquads != qidx->numquads) { fprintf(stderr, "Number of quads does not agree: %i vs %i\n", quad->numquads, qidx->numquads); exit(-1); } dimquads = quadfile_dimquads(quad); printf("Checking stars...\n"); for (s=0; s<qidx->numstars; s++) { uint32_t* quads; int nquads; int j; qidxfile_get_quads(qidx, s, &quads, &nquads); for (j=0; j<nquads; j++) { int star[dimquads]; int k, n; quadfile_get_stars(quad, quads[j], star); n = 0; for (k=0; k<dimquads; k++) { if (star[k] == s) n++; } if (n != 1) { fprintf(stderr, "Star %i, quad %i: found %i instances of the quad in the qidx (not 1)\n", s, quads[j], n); fprintf(stderr, " found: "); for (k=0; k<dimquads; k++) { fprintf(stderr, "%i ", star[k]); } fprintf(stderr, "\n"); } } } printf("Checking quads...\n"); for (q=0; q<quad->numquads; q++) { int star[dimquads]; uint32_t* quads; int nquads; int j; quadfile_get_stars(quad, q, star); for (j=0; j<dimquads; j++) { int k; int n; qidxfile_get_quads(qidx, star[j], &quads, &nquads); n = 0; for (k=0; k<nquads; k++) { if (quads[k] == q) n++; } if (n != 1) { fprintf(stderr, "Quad %i, star %i: found %i instances of the quad in the qidx (not 1)\n", q, star[j], n); fprintf(stderr, " found: "); for (k=0; k<nquads; k++) { fprintf(stderr, "%i ", quads[k]); } fprintf(stderr, "\n"); } } } quadfile_close(quad); qidxfile_close(qidx); 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; }