Beispiel #1
0
int merge_index_open_files(const char* quadfn, const char* ckdtfn, const char* skdtfn,
						   quadfile** quad, codetree** code, startree_t** star) {
	logmsg("Reading code tree from %s ...\n", ckdtfn);
	*code = codetree_open(ckdtfn);
	if (!*code) {
		ERROR("Failed to read code kdtree from %s", ckdtfn);
		return -1;
	}
    logmsg("Ok.\n");

	logmsg("Reading star tree from %s ...\n", skdtfn);
	*star = startree_open(skdtfn);
	if (!*star) {
		ERROR("Failed to read star kdtree from %s", skdtfn);
		return -1;
	}
    logmsg("Ok.\n");

	logmsg("Reading quads from %s ...\n", quadfn);
	*quad = quadfile_open(quadfn);
	if (!*quad) {
		ERROR("Failed to read quads from %s", quadfn);
		return -1;
	}
    logmsg("Ok.\n");
	return 0;
}
Beispiel #2
0
int unpermute_quads_files(const char* quadinfn, const char* ckdtinfn,
						  const char* quadoutfn, const char* ckdtoutfn,
						  char** args, int argc) {
    quadfile* quadin;
	quadfile* quadout;
	codetree* treein;
	codetree* treeout;

	logmsg("Reading code tree from %s ...\n", ckdtinfn);
	treein = codetree_open(ckdtinfn);
	if (!treein) {
		ERROR("Failed to read code kdtree from %s", ckdtinfn);
		return -1;
	}

	logmsg("Reading quads from %s ...\n", quadinfn);
	quadin = quadfile_open(quadinfn);
	if (!quadin) {
		ERROR("Failed to read quads from %s", quadinfn);
		return -1;
	}

	logmsg("Writing quads to %s ...\n", quadoutfn);
	quadout = quadfile_open_for_writing(quadoutfn);
	if (!quadout) {
		ERROR("Failed to write quads to %s", quadoutfn);
		return -1;
	}

	if (unpermute_quads(quadin, treein, quadout, &treeout, args, argc)) {
		return -1;
	}

	if (quadfile_close(quadout)) {
		ERROR("Failed to close output quadfile");
		return -1;
	}

	quadfile_close(quadin);

	logmsg("Writing code kdtree to %s ...\n", ckdtoutfn);
	if (codetree_write_to_file(treeout, ckdtoutfn) ||
		codetree_close(treeout)) {
		ERROR("Failed to write code kdtree");
		return -1;
	}

    free(treein->tree);
    treein->tree = NULL;
    codetree_close(treein);
	return 0;
}
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;
}
Beispiel #4
0
int unpermute_stars_files(const char* skdtinfn, const char* quadinfn,
						  const char* skdtoutfn, const char* quadoutfn,
						  anbool dosweeps, anbool check,
						  char** args, int argc) {
    quadfile_t* qfin;
	quadfile_t* qfout;
	startree_t* treein;
	startree_t* treeout;
	fitstable_t* tagout = NULL;
	fitstable_t* tagin;
	int rtn;

	logmsg("Reading star tree from %s ...\n", skdtinfn);
	treein = startree_open(skdtinfn);
	if (!treein) {
		ERROR("Failed to read star kdtree from %s.\n", skdtinfn);
		return -1;
	}

	logmsg("Reading quadfile from %s ...\n", quadinfn);
	qfin = quadfile_open(quadinfn);
	if (!qfin) {
		ERROR("Failed to read quadfile from %s.\n", quadinfn);
		return -1;
	}

	logmsg("Writing quadfile to %s ...\n", quadoutfn);
	qfout = quadfile_open_for_writing(quadoutfn);
	if (!qfout) {
		ERROR("Failed to write quadfile to %s.\n", quadoutfn);
		return -1;
	}

	rtn = unpermute_stars(treein, qfin, &treeout, qfout,
						  dosweeps, check, args, argc);
	if (rtn)
		return rtn;

	if (quadfile_close(qfout)) {
		ERROR("Failed to close output quadfile.\n");
		return -1;
	}

	logmsg("Writing star kdtree to %s ...\n", skdtoutfn);
	if (startree_write_to_file(treeout, skdtoutfn)) {
		ERROR("Failed to write star kdtree.\n");
		return -1;
	}

	if (startree_has_tagalong(treein)) {
		logmsg("Permuting tag-along table...\n");
		tagin = startree_get_tagalong(treein);
		if (tagin) {
			tagout = fitstable_open_for_appending(skdtoutfn);
			tagout->table = fits_copy_table(tagin->table);
			tagout->table->nr = 0;
			if (unpermute_stars_tagalong(treein, tagout)) {
				ERROR("Failed to permute tag-along table");
				return -1;
			}
			if (fitstable_close(tagout)) {
				ERROR("Failed to close tag-along data");
				return -1;
			}
		}
	}

	quadfile_close(qfin);
	startree_close(treein);
	free(treeout->sweep);
    free(treeout->tree);
    treeout->tree = NULL;
	startree_close(treeout);

	return 0;
}
Beispiel #5
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;
}