示例#1
0
int unpermute_stars_tagalong(startree_t* treein,
							 fitstable_t* tagout) {
	fitstable_t* tagin;
	qfits_header* tmphdr;
	int N;
	tagin = startree_get_tagalong(treein);
	if (!tagin) {
		ERROR("No input tag-along table");
		return -1;
	}
	N = startree_N(treein);
	assert(fitstable_nrows(tagin) == N);
	fitstable_clear_table(tagin);
	fitstable_add_fits_columns_as_struct(tagin);
	fitstable_copy_columns(tagin, tagout);
	tmphdr = tagout->header;
	tagout->header = tagin->header;
	if (fitstable_write_header(tagout)) {
		ERROR("Failed to write tag-along table header");
		return -1;
	}
	if (fitstable_copy_rows_data(tagin, (int*)treein->tree->perm, N, tagout)) {
		ERROR("Failed to copy tag-along table rows from input to output");
		return -1;
	}
	if (fitstable_fix_header(tagout)) {
		ERROR("Failed to fix tag-along table header");
		return -1;
	}
	tagout->header = tmphdr;
	return 0;
}
示例#2
0
int fitstable_append_to(fitstable_t* intable, FILE* fid) {
	fitstable_t* outtable;
	qfits_header* tmphdr;
	outtable = fitstable_open_for_appending_to(fid);
	fitstable_clear_table(intable);
	fitstable_add_fits_columns_as_struct(intable);
	fitstable_copy_columns(intable, outtable);
	outtable->table = fits_copy_table(intable->table);
	outtable->table->nr = 0;
	// swap in the input header.
	tmphdr = outtable->header;
	outtable->header = intable->header;
	if (fitstable_write_header(outtable)) {
		ERROR("Failed to write output table header");
		return -1;
	}
	if (fitstable_copy_rows_data(intable, NULL, fitstable_nrows(intable), outtable)) {
		ERROR("Failed to copy rows from input table to output");
		return -1;
	}
	if (fitstable_fix_header(outtable)) {
		ERROR("Failed to fix output table header");
		return -1;
	}
	outtable->header = tmphdr;
	// clear this so that fitstable_close() doesn't fclose() it.
	outtable->fid = NULL;
	fitstable_close(outtable);
	return 0;
}
示例#3
0
int startree_write_tagalong_table(fitstable_t* intab, fitstable_t* outtab,
								  const char* racol, const char* deccol) {
	int i, R, NB, N;
	char* buf;
	qfits_header* hdr;
	
	fitstable_clear_table(intab);
	fitstable_add_fits_columns_as_struct(intab);
	fitstable_copy_columns(intab, outtab);
	if (!racol)
		racol = "RA";
	if (!deccol)
		deccol = "DEC";
	fitstable_remove_column(outtab, racol);
	fitstable_remove_column(outtab, deccol);
    fitstable_read_extension(intab, 1);
	hdr = fitstable_get_header(outtab);
	qfits_header_add(hdr, "AN_FILE", AN_FILETYPE_TAGALONG, "Extra data for stars", NULL);
	if (fitstable_write_header(outtab)) {
		ERROR("Failed to write tag-along data header");
		return -1;
	}
	R = fitstable_row_size(intab);
	NB = 1000;
	logverb("Input row size: %i, output row size: %i\n", R, fitstable_row_size(outtab));
	buf = malloc(NB * R);
	N = fitstable_nrows(intab);
	
	for (i=0; i<N; i+=NB) {
		int nr = NB;
		if (i+NB > N)
			nr = N - i;
		if (fitstable_read_structs(intab, buf, R, i, nr)) {
			ERROR("Failed to read tag-along data from catalog");
			return -1;
		}
		if (fitstable_write_structs(outtab, buf, R, nr)) {
			ERROR("Failed to write tag-along data");
			return -1;
		}
	}
	free(buf);
	if (fitstable_fix_header(outtab)) {
		ERROR("Failed to fix tag-along data header");
		return -1;
	}
	return 0;
}
示例#4
0
int fitstable_read_nrows_data(fitstable_t* table, int row0, int nrows,
							  void* dest) {
	int R;
	off_t off;
	assert(table);
	assert(row0 >= 0);
	assert((row0 + nrows) <= fitstable_nrows(table));
	assert(dest);
	R = fitstable_row_size(table);
	if (in_memory(table)) {
		int i;
		char* cdest = dest;
		for (i=0; i<nrows; i++)
			memcpy(cdest, bl_access(table->rows, row0 + i), R);
		return 0;
	}
	if (!table->readfid) {
		table->readfid = fopen(table->fn, "rb");
		if (!table->readfid) {
			SYSERROR("Failed to open FITS table %s for reading", table->fn);
			return -1;
		}

        assert(table->anq);
        off_t start;
        start = anqfits_data_start(table->anq, table->extension);
        table->end_table_offset = start;
	}
	off = get_row_offset(table, row0);
	if (fseeko(table->readfid, off, SEEK_SET)) {
		SYSERROR("Failed to fseeko() to read a row");
		return -1;
	}
	if (fread(dest, 1, R*nrows, table->readfid) != (R*nrows)) {
		SYSERROR("Failed to read %i rows starting from %i, from %s", nrows, row0, table->fn);
		return -1;
	}
	return 0;
}
示例#5
0
int main(int argc, char** argv) {
    int argchar;
    allquads_t* aq;
    int loglvl = LOG_MSG;
    int i, N;
    char* catfn = NULL;

    startree_t* starkd;
    fitstable_t* cat;

    char* racol = NULL;
    char* deccol = NULL;
    int datatype = KDT_DATA_DOUBLE;
    int treetype = KDT_TREE_DOUBLE;
    int buildopts = 0;
    int Nleaf = 0;

    char* indexfn = NULL;
    //index_params_t* p;

    aq = allquads_init();
    aq->skdtfn = "allquads.skdt";
    aq->codefn = "allquads.code";
    aq->quadfn = "allquads.quad";

    while ((argchar = getopt (argc, argv, OPTIONS)) != -1)
        switch (argchar) {
        case 'v':
            loglvl++;
            break;
        case 'd':
            aq->dimquads = atoi(optarg);
            break;
        case 'I':
            aq->id = atoi(optarg);
            break;
        case 'h':
            print_help(argv[0]);
            exit(0);
        case 'i':
            catfn = optarg;
            break;
        case 'o':
            indexfn = optarg;
            break;
        case 'u':
            aq->quad_d2_upper = arcmin2distsq(atof(optarg));
            aq->use_d2_upper = TRUE;
            break;
        case 'l':
            aq->quad_d2_lower = arcmin2distsq(atof(optarg));
            aq->use_d2_lower = TRUE;
            break;
        default:
            return -1;
        }

    log_init(loglvl);

    if (!catfn || !indexfn) {
        printf("Specify in & out filenames, bonehead!\n");
        print_help(argv[0]);
        exit( -1);
    }

    if (optind != argc) {
        print_help(argv[0]);
        printf("\nExtra command-line args were given: ");
        for (i=optind; i<argc; i++) {
            printf("%s ", argv[i]);
        }
        printf("\n");
        exit(-1);
    }

    if (!aq->id)
        logmsg("Warning: you should set the unique-id for this index (with -I).\n");

    if (aq->dimquads > DQMAX) {
        ERROR("Quad dimension %i exceeds compiled-in max %i.\n", aq->dimquads, DQMAX);
        exit(-1);
    }
    aq->dimcodes = dimquad2dimcode(aq->dimquads);

    // Read reference catalog, write star kd-tree
    logmsg("Building star kdtree: reading %s, writing to %s\n", catfn, aq->skdtfn);
    
    logverb("Reading star catalogue...");
    cat = fitstable_open(catfn);
    if (!cat) {
        ERROR("Couldn't read catalog");
        exit(-1);
    }
    logmsg("Got %i stars\n", fitstable_nrows(cat));
    starkd = startree_build(cat, racol, deccol, datatype, treetype,
                            buildopts, Nleaf, argv, argc);
    if (!starkd) {
        ERROR("Failed to create star kdtree");
        exit(-1);
    }

    logmsg("Star kd-tree contains %i data points in dimension %i\n",
           startree_N(starkd), startree_D(starkd));
    N = startree_N(starkd);
    for (i=0; i<N; i++) {
        double ra,dec;
        int ok;
        ok = startree_get_radec(starkd, i, &ra, &dec);
        logmsg("  data %i: ok %i, RA,Dec %g, %g\n", i, ok, ra, dec);
    }

    if (startree_write_to_file(starkd, aq->skdtfn)) {
        ERROR("Failed to write star kdtree");
        exit(-1);
    }
    startree_close(starkd);
    fitstable_close(cat);
    logmsg("Wrote star kdtree to %s\n", aq->skdtfn);

    logmsg("Running allquads...\n");
    if (allquads_open_outputs(aq)) {
        exit(-1);
    }

    if (allquads_create_quads(aq)) {
        exit(-1);
    }

    if (allquads_close(aq)) {
        exit(-1);
    }

    logmsg("allquads: wrote %s, %s\n", aq->quadfn, aq->codefn);

    // build-index:
    //build_index_defaults(&p);

    // codetree
    /*
     if (step_codetree(p, codes, &codekd,
     codefn, &ckdtfn, tempfiles))
     return -1;
     */
    char* ckdtfn=NULL;
    char* tempdir = NULL;

    ckdtfn = create_temp_file("ckdt", tempdir);
    logmsg("Creating code kdtree, reading %s, writing to %s\n", aq->codefn, ckdtfn);
    if (codetree_files(aq->codefn, ckdtfn, 0, 0, 0, 0, argv, argc)) {
        ERROR("codetree failed");
        return -1;
    }

    char* skdt2fn=NULL;
    char* quad2fn=NULL;

    // unpermute-stars
    logmsg("Unpermute-stars...\n");
    skdt2fn = create_temp_file("skdt2", tempdir);
    quad2fn = create_temp_file("quad2", tempdir);

    logmsg("Unpermuting stars from %s and %s to %s and %s\n",
           aq->skdtfn, aq->quadfn, skdt2fn, quad2fn);
    if (unpermute_stars_files(aq->skdtfn, aq->quadfn, skdt2fn, quad2fn,
                              TRUE, FALSE, argv, argc)) {
        ERROR("Failed to unpermute-stars");
        return -1;
    }

    allquads_free(aq);

    // unpermute-quads
    /*
     if (step_unpermute_quads(p, quads2, codekd, &quads3, &codekd2,
     quad2fn, ckdtfn, &quad3fn, &ckdt2fn, tempfiles))
     return -1;
     */
    char* quad3fn=NULL;
    char* ckdt2fn=NULL;

    ckdt2fn = create_temp_file("ckdt2", tempdir);
    quad3fn = create_temp_file("quad3", tempdir);
    logmsg("Unpermuting quads from %s and %s to %s and %s\n",
           quad2fn, ckdtfn, quad3fn, ckdt2fn);
    if (unpermute_quads_files(quad2fn, ckdtfn,
                              quad3fn, ckdt2fn, argv, argc)) {
        ERROR("Failed to unpermute-quads");
        return -1;
    }

    // index
    /*
     if (step_merge_index(p, codekd2, quads3, starkd2, p_index,
     ckdt2fn, quad3fn, skdt2fn, indexfn))
     return -1;
     */
    quadfile_t* quad;
    codetree_t* code;
    startree_t* star;

    logmsg("Merging %s and %s and %s to %s\n",
           quad3fn, ckdt2fn, skdt2fn, indexfn);
    if (merge_index_open_files(quad3fn, ckdt2fn, skdt2fn,
                               &quad, &code, &star)) {
        ERROR("Failed to open index files for merging");
        return -1;
    }
    if (merge_index(quad, code, star, indexfn)) {
        ERROR("Failed to write merged index");
        return -1;
    }
    codetree_close(code);
    startree_close(star);
    quadfile_close(quad);

    printf("Done.\n");

    free(ckdtfn);
    free(skdt2fn);
    free(quad2fn);
    free(ckdt2fn);
    free(quad3fn);

    return 0;
}
示例#6
0
int main(int argc, char *argv[]) {
    int argchar;
	char* progname = argv[0];
	sl* infns = sl_new(16);
	char* outfnpat = NULL;
	char* racol = "RA";
	char* deccol = "DEC";
	char* tempdir = "/tmp";
	anbool gzip = FALSE;
	sl* cols = sl_new(16);
	int loglvl = LOG_MSG;
	int nside = 1;
	double margin = 0.0;
	int NHP;
	double md;
	char* backref = NULL;
	
	fitstable_t* intable;
	fitstable_t** outtables;

	char** myargs;
	int nmyargs;
	int i;

    while ((argchar = getopt (argc, argv, OPTIONS)) != -1)
        switch (argchar) {
		case 'b':
			backref = optarg;
			break;
		case 't':
			tempdir = optarg;
			break;
		case 'c':
			sl_append(cols, optarg);
			break;
		case 'g':
			gzip = TRUE;
			break;
		case 'o':
			outfnpat = optarg;
			break;
		case 'r':
			racol = optarg;
			break;
		case 'd':
			deccol = optarg;
			break;
		case 'n':
			nside = atoi(optarg);
			break;
		case 'm':
			margin = atof(optarg);
			break;
		case 'v':
			loglvl++;
			break;
        case '?':
            fprintf(stderr, "Unknown option `-%c'.\n", optopt);
        case 'h':
			printHelp(progname);
            return 0;
        default:
            return -1;
        }

	if (sl_size(cols) == 0) {
		sl_free2(cols);
		cols = NULL;
	}

	nmyargs = argc - optind;
	myargs = argv + optind;

	for (i=0; i<nmyargs; i++)
		sl_append(infns, myargs[i]);
	
	if (!sl_size(infns)) {
		printHelp(progname);
		printf("Need input filenames!\n");
		exit(-1);
	}
	log_init(loglvl);
	fits_use_error_system();

	NHP = 12 * nside * nside;
	logmsg("%i output healpixes\n", NHP);
	outtables = calloc(NHP, sizeof(fitstable_t*));
	assert(outtables);

	md = deg2dist(margin);

	/**
	 About the mincaps/maxcaps:

	 These have a center and radius-squared, describing the region
	 inside a small circle on the sphere.

	 The "mincaps" describe the regions that are definitely owned by a
	 single healpix -- ie, more than MARGIN distance from any edge.
	 That is, the mincap is the small circle centered at (0.5, 0.5) in
	 the healpix and with radius = the distance to the closest healpix
	 boundary, MINUS the margin distance.

	 Below, we first check whether a new star is within the "mincap"
	 of any healpix.  If so, we stick it in that healpix and continue.

	 Otherwise, we check all the "maxcaps" -- these are the healpixes
	 it could *possibly* be in.  We then refine with
	 healpix_within_range_of_xyz.  The maxcap distance is the distance
	 to the furthest boundary point, PLUS the margin distance.
	 */


	cap_t* mincaps = malloc(NHP * sizeof(cap_t));
	cap_t* maxcaps = malloc(NHP * sizeof(cap_t));
	for (i=0; i<NHP; i++) {
		// center
		double r2;
		double xyz[3];
		double* cxyz;
		double step = 1e-3;
		double v;
		double r2b, r2a;

		cxyz = mincaps[i].xyz;
		healpix_to_xyzarr(i, nside, 0.5, 0.5, mincaps[i].xyz);
		memcpy(maxcaps[i].xyz, cxyz, 3 * sizeof(double));
		logverb("Center of HP %i: (%.3f, %.3f, %.3f)\n", i, cxyz[0], cxyz[1], cxyz[2]);

		// radius-squared:
		// max is the easy one: max of the four corners (I assume)
		r2 = 0.0;
		healpix_to_xyzarr(i, nside, 0.0, 0.0, xyz);
		logverb("  HP %i corner 1: (%.3f, %.3f, %.3f), distsq %.3f\n", i, xyz[0], xyz[1], xyz[2], distsq(xyz, cxyz, 3));
		r2 = MAX(r2, distsq(xyz, cxyz, 3));
		healpix_to_xyzarr(i, nside, 1.0, 0.0, xyz);
		logverb("  HP %i corner 1: (%.3f, %.3f, %.3f), distsq %.3f\n", i, xyz[0], xyz[1], xyz[2], distsq(xyz, cxyz, 3));
		r2 = MAX(r2, distsq(xyz, cxyz, 3));
		healpix_to_xyzarr(i, nside, 0.0, 1.0, xyz);
		logverb("  HP %i corner 1: (%.3f, %.3f, %.3f), distsq %.3f\n", i, xyz[0], xyz[1], xyz[2], distsq(xyz, cxyz, 3));
		r2 = MAX(r2, distsq(xyz, cxyz, 3));
		healpix_to_xyzarr(i, nside, 1.0, 1.0, xyz);
		logverb("  HP %i corner 1: (%.3f, %.3f, %.3f), distsq %.3f\n", i, xyz[0], xyz[1], xyz[2], distsq(xyz, cxyz, 3));
		r2 = MAX(r2, distsq(xyz, cxyz, 3));
		logverb("  max distsq: %.3f\n", r2);
		logverb("  margin dist: %.3f\n", md);
		maxcaps[i].r2 = square(sqrt(r2) + md);
		logverb("  max cap distsq: %.3f\n", maxcaps[i].r2);
		r2a = r2;

		r2 = 1.0;
		r2b = 0.0;
		for (v=0; v<=1.0; v+=step) {
			healpix_to_xyzarr(i, nside, 0.0, v, xyz);
			r2 = MIN(r2, distsq(xyz, cxyz, 3));
			r2b = MAX(r2b, distsq(xyz, cxyz, 3));
			healpix_to_xyzarr(i, nside, 1.0, v, xyz);
			r2 = MIN(r2, distsq(xyz, cxyz, 3));
			r2b = MAX(r2b, distsq(xyz, cxyz, 3));
			healpix_to_xyzarr(i, nside, v, 0.0, xyz);
			r2 = MIN(r2, distsq(xyz, cxyz, 3));
			r2b = MAX(r2b, distsq(xyz, cxyz, 3));
			healpix_to_xyzarr(i, nside, v, 1.0, xyz);
			r2 = MIN(r2, distsq(xyz, cxyz, 3));
			r2b = MAX(r2b, distsq(xyz, cxyz, 3));
		}
		mincaps[i].r2 = square(MAX(0, sqrt(r2) - md));
		logverb("\nhealpix %i: min rad    %g\n", i, sqrt(r2));
		logverb("healpix %i: max rad    %g\n", i, sqrt(r2a));
		logverb("healpix %i: max rad(b) %g\n", i, sqrt(r2b));
		assert(r2a >= r2b);
	}

	if (backref) {
		fitstable_t* tab = fitstable_open_for_writing(backref);
		int maxlen = 0;
		char* buf;
		for (i=0; i<sl_size(infns); i++) {
			char* infn = sl_get(infns, i);
			maxlen = MAX(maxlen, strlen(infn));
		}
		fitstable_add_write_column_array(tab, fitscolumn_char_type(), maxlen,
										 "filename", NULL);
		fitstable_add_write_column(tab, fitscolumn_i16_type(), "index", NULL);
		if (fitstable_write_primary_header(tab) ||
			fitstable_write_header(tab)) {
			ERROR("Failed to write header of backref table \"%s\"", backref);
			exit(-1);
		}
		buf = malloc(maxlen+1);
		assert(buf);

		for (i=0; i<sl_size(infns); i++) {
			char* infn = sl_get(infns, i);
			int16_t ind;
			memset(buf, 0, maxlen);
			strcpy(buf, infn);
			ind = i;
			if (fitstable_write_row(tab, buf, &ind)) {
				ERROR("Failed to write row %i of backref table: %s = %i",
					  i, buf, ind);
				exit(-1);
			}
		}
		if (fitstable_fix_header(tab) ||
			fitstable_close(tab)) {
			ERROR("Failed to fix header & close backref table");
			exit(-1);
		}
		logmsg("Wrote backref table %s\n", backref);
		free(buf);
	}

	for (i=0; i<sl_size(infns); i++) {
		char* infn = sl_get(infns, i);
		char* originfn = infn;
		int r, NR;
		tfits_type any, dubl;
		il* hps = NULL;
		bread_t* rowbuf;
		int R;
		char* tempfn = NULL;
		char* padrowdata = NULL;
		int ii;

		logmsg("Reading input \"%s\"...\n", infn);

		if (gzip) {
			char* cmd;
			int rtn;
			tempfn = create_temp_file("hpsplit", tempdir);
			asprintf_safe(&cmd, "gunzip -cd %s > %s", infn, tempfn);
			logmsg("Running: \"%s\"\n", cmd);
			rtn = run_command_get_outputs(cmd, NULL, NULL);
			if (rtn) {
				ERROR("Failed to run command: \"%s\"", cmd);
				exit(-1);
			}
			free(cmd);
			infn = tempfn;
		}

		intable = fitstable_open(infn);
		if (!intable) {
			ERROR("Couldn't read catalog %s", infn);
			exit(-1);
		}
		NR = fitstable_nrows(intable);
		logmsg("Got %i rows\n", NR);

		any = fitscolumn_any_type();
		dubl = fitscolumn_double_type();

		fitstable_add_read_column_struct(intable, dubl, 1, 0, any, racol, TRUE);
		fitstable_add_read_column_struct(intable, dubl, 1, sizeof(double), any, deccol, TRUE);

		fitstable_use_buffered_reading(intable, 2*sizeof(double), 1000);

		R = fitstable_row_size(intable);
		rowbuf = buffered_read_new(R, 1000, NR, refill_rowbuffer, intable);

		if (fitstable_read_extension(intable, 1)) {
			ERROR("Failed to find RA and DEC columns (called \"%s\" and \"%s\" in the FITS file)", racol, deccol);
			exit(-1);
		}

		for (r=0; r<NR; r++) {
			int hp = -1;
			double ra, dec;
			int j;
			double* rd;
			void* rowdata;
			void* rdata;

			if (r && ((r % 100000) == 0)) {
			  logmsg("Reading row %i of %i\n", r, NR);
			}

			//printf("reading RA,Dec for row %i\n", r);
			rd = fitstable_next_struct(intable);
			ra = rd[0];
			dec = rd[1];

			logverb("row %i: ra,dec %g,%g\n", r, ra, dec);
			if (margin == 0) {
				hp = radecdegtohealpix(ra, dec, nside);
				logverb("  --> healpix %i\n", hp);
			} else {

				double xyz[3];
				anbool gotit = FALSE;
				double d2;
				if (!hps)
					hps = il_new(4);
				radecdeg2xyzarr(ra, dec, xyz);
				for (j=0; j<NHP; j++) {
					d2 = distsq(xyz, mincaps[j].xyz, 3);
					if (d2 <= mincaps[j].r2) {
						logverb("  -> in mincap %i  (dist %g vs %g)\n", j, sqrt(d2), sqrt(mincaps[j].r2));
						il_append(hps, j);
						gotit = TRUE;
						break;
					}
				}
				if (!gotit) {
					for (j=0; j<NHP; j++) {
						d2 = distsq(xyz, maxcaps[j].xyz, 3);
						if (d2 <= maxcaps[j].r2) {
							logverb("  -> in maxcap %i  (dist %g vs %g)\n", j, sqrt(d2), sqrt(maxcaps[j].r2));
							if (healpix_within_range_of_xyz(j, nside, xyz, margin)) {
								logverb("  -> and within range.\n");
								il_append(hps, j);
							}
						}
					}
				}

				//hps = healpix_rangesearch_radec(ra, dec, margin, nside, hps);

				logverb("  --> healpixes: [");
				for (j=0; j<il_size(hps); j++)
					logverb(" %i", il_get(hps, j));
				logverb(" ]\n");
			}

			//printf("Reading rowdata for row %i\n", r);
			rowdata = buffered_read(rowbuf);
			assert(rowdata);


			j=0;
			while (1) {
				if (hps) {
					if (j >= il_size(hps))
						break;
					hp = il_get(hps, j);
					j++;
				}
				assert(hp < NHP);
				assert(hp >= 0);

				if (!outtables[hp]) {
					char* outfn;
					fitstable_t* out;

					// MEMLEAK the output filename.  You'll live.
					asprintf_safe(&outfn, outfnpat, hp);
					logmsg("Opening output file \"%s\"...\n", outfn);
					out = fitstable_open_for_writing(outfn);
					if (!out) {
						ERROR("Failed to open output table \"%s\"", outfn);
						exit(-1);
					}
					// Set the output table structure.
					if (cols) {
					  fitstable_add_fits_columns_as_struct3(intable, out, cols, 0);
					} else
						fitstable_add_fits_columns_as_struct2(intable, out);

					if (backref) {
						tfits_type i16type;
						tfits_type i32type;
						// R = fitstable_row_size(intable);
						int off = R;
						i16type = fitscolumn_i16_type();
						i32type = fitscolumn_i32_type();
						fitstable_add_read_column_struct(out, i16type, 1, off,
														 i16type, "backref_file", TRUE);
						off += sizeof(int16_t);
						fitstable_add_read_column_struct(out, i32type, 1, off,
														 i32type, "backref_index", TRUE);
					}

					//printf("Output table:\n");
					//fitstable_print_columns(out);

					if (fitstable_write_primary_header(out) ||
						fitstable_write_header(out)) {
						ERROR("Failed to write output file headers for \"%s\"", outfn);
						exit(-1);
					}
					outtables[hp] = out;
				}

				if (backref) {
					int16_t brfile;
					int32_t brind;
					if (!padrowdata) {
						padrowdata = malloc(R + sizeof(int16_t) + sizeof(int32_t));
						assert(padrowdata);
					}
					// convert to FITS endian
					brfile = htons(i);
					brind  = htonl(r);
					// add backref data to rowdata
					memcpy(padrowdata, rowdata, R);
					memcpy(padrowdata + R, &brfile, sizeof(int16_t));
					memcpy(padrowdata + R + sizeof(int16_t), &brind, sizeof(int32_t));
					rdata = padrowdata;
				} else {
					rdata = rowdata;
				}

				if (cols) {
				  if (fitstable_write_struct_noflip(outtables[hp], rdata)) {
				    ERROR("Failed to copy a row of data from input table \"%s\" to output healpix %i", infn, hp);
				  }
				} else {
				  if (fitstable_write_row_data(outtables[hp], rdata)) {
				    ERROR("Failed to copy a row of data from input table \"%s\" to output healpix %i", infn, hp);
				  }
				}

				if (!hps)
					break;
			}
			if (hps)
				il_remove_all(hps);

		}
		buffered_read_free(rowbuf);
		// wack... buffered_read_free() just frees its internal buffer,
		// not the "rowbuf" struct itself.
		// who wrote this crazy code?  Oh, me of 5 years ago.  Jerk.
		free(rowbuf);

		fitstable_close(intable);
		il_free(hps);

		if (tempfn) {
			logverb("Removing temp file %s\n", tempfn);
			if (unlink(tempfn)) {
				SYSERROR("Failed to unlink() temp file \"%s\"", tempfn);
			}
			tempfn = NULL;
		}

		// fix headers so that the files are valid at this point.
		for (ii=0; ii<NHP; ii++) {
		  if (!outtables[ii])
		    continue;
		  off_t offset = ftello(outtables[ii]->fid);
		  if (fitstable_fix_header(outtables[ii])) {
		    ERROR("Failed to fix header for healpix %i after reading input file \"%s\"", ii, originfn);
		    exit(-1);
		  }
		  fseeko(outtables[ii]->fid, offset, SEEK_SET);
		}

		if (padrowdata) {
			free(padrowdata);
			padrowdata = NULL;
		}

	}

	for (i=0; i<NHP; i++) {
		if (!outtables[i])
			continue;
		if (fitstable_fix_header(outtables[i]) ||
			fitstable_fix_primary_header(outtables[i]) ||
			fitstable_close(outtables[i])) {
			ERROR("Failed to close output table for healpix %i", i);
			exit(-1);
		}
	}

	free(outtables);
	sl_free2(infns);
	sl_free2(cols);

	free(mincaps);
	free(maxcaps);

    return 0;
}
示例#7
0
int resort_xylist(const char* infn, const char* outfn,
                  const char* fluxcol, const char* backcol,
                  anbool ascending) {
	FILE* fin = NULL;
	FILE* fout = NULL;
    double *flux = NULL, *back = NULL;
    int *perm1 = NULL, *perm2 = NULL;
    anbool *used = NULL;
    int start, size, nextens, ext;
    int (*compare)(const void*, const void*);
    fitstable_t* tab = NULL;
    anqfits_t* anq = NULL;

    if (ascending)
        compare = compare_doubles_asc;
    else
        compare = compare_doubles_desc;

    if (!fluxcol)
        fluxcol = "FLUX";
    if (!backcol)
        backcol = "BACKGROUND";

    fin = fopen(infn, "rb");
    if (!fin) {
        SYSERROR("Failed to open input file %s", infn);
        return -1;
    }

    fout = fopen(outfn, "wb");
    if (!fout) {
        SYSERROR("Failed to open output file %s", outfn);
        goto bailout;
    }

	// copy the main header exactly.
    anq = anqfits_open(infn);
    if (!anq) {
        ERROR("Failed to open file \"%s\"", infn);
        goto bailout;
    }
    start = anqfits_header_start(anq, 0);
    size  = anqfits_header_size (anq, 0);

    if (pipe_file_offset(fin, start, size, fout)) {
        ERROR("Failed to copy primary FITS header.");
        goto bailout;
    }

	nextens = anqfits_n_ext(anq);

    tab = fitstable_open(infn);
    if (!tab) {
        ERROR("Failed to open FITS table in file %s", infn);
        goto bailout;
    }

	for (ext=1; ext<nextens; ext++) {
		int hdrstart, hdrsize, datstart;
		int i, N;
        int rowsize;

        hdrstart = anqfits_header_start(anq, ext);
        hdrsize  = anqfits_header_size (anq, ext);
        datstart = anqfits_data_start  (anq, ext);

        if (!anqfits_is_table(anq, ext)) {
            ERROR("Extention %i isn't a table. Skipping", ext);
			continue;
		}
        // Copy the header as-is.
        if (pipe_file_offset(fin, hdrstart, hdrsize, fout)) {
            ERROR("Failed to copy the header of extension %i", ext);
			goto bailout;
        }

        if (fitstable_read_extension(tab, ext)) {
            ERROR("Failed to read FITS table from extension %i", ext);
            goto bailout;
        }
        rowsize = fitstable_row_size(tab);

        // read FLUX column as doubles.
        flux = fitstable_read_column(tab, fluxcol, TFITS_BIN_TYPE_D);
        if (!flux) {
            ERROR("Failed to read FLUX column from extension %i", ext);
            goto bailout;
        }
        // BACKGROUND
        back = fitstable_read_column(tab, backcol, TFITS_BIN_TYPE_D);
        if (!back) {
            ERROR("Failed to read BACKGROUND column from extension %i", ext);
            goto bailout;
        }

		debug("First 10 rows of input table:\n");
		for (i=0; i<10; i++)
			debug("flux %g, background %g\n", flux[i], back[i]);

        N = fitstable_nrows(tab);

        // set back = flux + back (ie, non-background-subtracted flux)
		for (i=0; i<N; i++)
            back[i] += flux[i];

        // Sort by flux...
		perm1 = permuted_sort(flux, sizeof(double), compare, NULL, N);

        // Sort by non-background-subtracted flux...
		perm2 = permuted_sort(back, sizeof(double), compare, NULL, N);

        used = malloc(N * sizeof(anbool));
        memset(used, 0, N * sizeof(anbool));

		// Check sort...
        for (i=0; i<N-1; i++) {
			if (ascending) {
				assert(flux[perm1[i]] <= flux[perm1[i+1]]);
				assert(back[perm2[i]] <= back[perm2[i+1]]);
			} else {
				assert(flux[perm1[i]] >= flux[perm1[i+1]]);
				assert(back[perm2[i]] >= back[perm2[i+1]]);
			}
		}

        for (i=0; i<N; i++) {
            int j;
            int inds[] = { perm1[i], perm2[i] };
            for (j=0; j<2; j++) {
                int index = inds[j];
				assert(index < N);
                if (used[index])
                    continue;
                used[index] = TRUE;
				debug("adding index %i: %s %g\n", index, j==0 ? "flux" : "bgsub", j==0 ? flux[index] : back[index]);
                if (pipe_file_offset(fin, datstart + index * rowsize, rowsize, fout)) {
                    ERROR("Failed to copy row %i", index);
                    goto bailout;
                }
            }
        }

        for (i=0; i<N; i++)
			assert(used[i]);

		if (fits_pad_file(fout)) {
			ERROR("Failed to add padding to extension %i", ext);
            goto bailout;
		}

        free(flux);
        flux = NULL;
        free(back);
        back = NULL;
        free(perm1);
        perm1 = NULL;
        free(perm2);
        perm2 = NULL;
        free(used);
        used = NULL;
    }

    fitstable_close(tab);
    tab = NULL;

	if (fclose(fout)) {
		SYSERROR("Failed to close output file %s", outfn);
        return -1;
    }
	fclose(fin);
    return 0;

 bailout:
    if (tab)
        fitstable_close(tab);
    if (fout)
        fclose(fout);
    if (fin)
        fclose(fin);
    free(flux);
    free(back);
    free(perm1);
    free(perm2);
    free(used);
	return -1;
}
int uniformize_catalog(fitstable_t* intable, fitstable_t* outtable,
					   const char* racol, const char* deccol,
					   const char* sortcol, anbool sort_ascending,
					   double sort_min_cut,
					   // ?  Or do this cut in a separate process?
					   int bighp, int bignside,
					   int nmargin,
					   // uniformization nside.
					   int Nside,
					   double dedup_radius,
					   int nsweeps,
					   char** args, int argc) {
	anbool allsky;
	intmap_t* starlists;
	int NHP;
	anbool dense = FALSE;
	double dedupr2 = 0.0;
	tfits_type dubl;
	int N;
	int* inorder = NULL;
	int* outorder = NULL;
	int outi;
	double *ra = NULL, *dec = NULL;
	il* myhps = NULL;
	int i,j,k;
	int nkeep = nsweeps;
	int noob = 0;
	int ndup = 0;
	struct oh_token token;
	int* npersweep = NULL;
	qfits_header* outhdr = NULL;
	double *sortval = NULL;

	if (bignside == 0)
		bignside = 1;
	allsky = (bighp == -1);

    if (Nside % bignside) {
        ERROR("Fine healpixelization Nside must be a multiple of the coarse healpixelization Nside");
        return -1;
    }
	if (Nside > HP_MAX_INT_NSIDE) {
		ERROR("Error: maximum healpix Nside = %i", HP_MAX_INT_NSIDE);
		return -1;
	}

	NHP = 12 * Nside * Nside;
	logverb("Healpix Nside: %i, # healpixes on the whole sky: %i\n", Nside, NHP);
	if (!allsky) {
		logverb("Creating index for healpix %i, nside %i\n", bighp, bignside);
		logverb("Number of healpixes: %i\n", ((Nside/bignside)*(Nside/bignside)));
	}
	logverb("Healpix side length: %g arcmin.\n", healpix_side_length_arcmin(Nside));

	dubl = fitscolumn_double_type();
	if (!racol)
		racol = "RA";
	ra = fitstable_read_column(intable, racol, dubl);
	if (!ra) {
		ERROR("Failed to find RA column (%s) in table", racol);
		return -1;
	}
	if (!deccol)
		deccol = "DEC";
	dec = fitstable_read_column(intable, deccol, dubl);
	if (!dec) {
		ERROR("Failed to find DEC column (%s) in table", deccol);
		free(ra);
		return -1;
	}

	N = fitstable_nrows(intable);
	logverb("Have %i objects\n", N);

	// FIXME -- argsort and seek around the input table, and append to
	// starlists in order; OR read from the input table in sequence and
	// sort in the starlists?
	if (sortcol) {
		logverb("Sorting by %s...\n", sortcol);
		sortval = fitstable_read_column(intable, sortcol, dubl);
		if (!sortval) {
			ERROR("Failed to read sorting column \"%s\"", sortcol);
			free(ra);
			free(dec);
			return -1;
		}
		inorder = permuted_sort(sortval, sizeof(double),
								sort_ascending ? compare_doubles_asc : compare_doubles_desc,
								NULL, N);
		if (sort_min_cut > -HUGE_VAL) {
			logverb("Cutting to %s > %g...\n", sortcol, sort_min_cut);
			// Cut objects with sortval < sort_min_cut.
			if (sort_ascending) {
				// skipped objects are at the front -- find the first obj
				// to keep
				for (i=0; i<N; i++)
					if (sortval[inorder[i]] > sort_min_cut)
						break;
				// move the "inorder" indices down.
				if (i)
					memmove(inorder, inorder+i, (N-i)*sizeof(int));
				N -= i;
			} else {
				// skipped objects are at the end -- find the last obj to keep.
				for (i=N-1; i>=0; i--)
					if (sortval[inorder[i]] > sort_min_cut)
						break;
				N = i+1;
			}
			logverb("Cut to %i objects\n", N);
		}
		//free(sortval);
	}

	token.nside = bignside;
	token.finenside = Nside;
	token.hp = bighp;

	if (!allsky && nmargin) {
		int bigbighp, bighpx, bighpy;
		//int ninside;
		il* seeds = il_new(256);
		logverb("Finding healpixes in range...\n");
        healpix_decompose_xy(bighp, &bigbighp, &bighpx, &bighpy, bignside);
		//ninside = (Nside/bignside)*(Nside/bignside);
		// Prime the queue with the fine healpixes that are on the
		// boundary of the big healpix.
		for (i=0; i<((Nside / bignside) - 1); i++) {
			// add (i,0), (i,max), (0,i), and (0,max) healpixes
            int xx = i + bighpx * (Nside / bignside);
            int yy = i + bighpy * (Nside / bignside);
            int y0 =     bighpy * (Nside / bignside);
			// -1 prevents us from double-adding the corners.
            int y1 =(1 + bighpy)* (Nside / bignside) - 1;
            int x0 =     bighpx * (Nside / bignside);
            int x1 =(1 + bighpx)* (Nside / bignside) - 1;
            assert(xx < Nside);
            assert(yy < Nside);
            assert(x0 < Nside);
            assert(x1 < Nside);
            assert(y0 < Nside);
            assert(y1 < Nside);
			il_append(seeds, healpix_compose_xy(bigbighp, xx, y0, Nside));
			il_append(seeds, healpix_compose_xy(bigbighp, xx, y1, Nside));
			il_append(seeds, healpix_compose_xy(bigbighp, x0, yy, Nside));
			il_append(seeds, healpix_compose_xy(bigbighp, x1, yy, Nside));
		}
        logmsg("Number of boundary healpixes: %zu (Nside/bignside = %i)\n", il_size(seeds), Nside/bignside);

		myhps = healpix_region_search(-1, seeds, Nside, NULL, NULL,
									  outside_healpix, &token, nmargin);
		logmsg("Number of margin healpixes: %zu\n", il_size(myhps));
		il_free(seeds);

		il_sort(myhps, TRUE);
		// DEBUG
		il_check_consistency(myhps);
		il_check_sorted_ascending(myhps, TRUE);
	}

	dedupr2 = arcsec2distsq(dedup_radius);
	starlists = intmap_new(sizeof(int32_t), nkeep, 0, dense);

	logverb("Placing stars in grid cells...\n");
	for (i=0; i<N; i++) {
		int hp;
		bl* lst;
		int32_t j32;
		anbool oob;
		if (inorder) {
			j = inorder[i];
			//printf("Placing star %i (%i): sort value %s = %g, RA,Dec=%g,%g\n", i, j, sortcol, sortval[j], ra[j], dec[j]);
		} else
			j = i;
		
		hp = radecdegtohealpix(ra[j], dec[j], Nside);
		//printf("HP %i\n", hp);
		// in bounds?
		oob = FALSE;
		if (myhps) {
			oob = (outside_healpix(hp, &token) && !il_sorted_contains(myhps, hp));
		} else if (!allsky) {
			oob = (outside_healpix(hp, &token));
		}
		if (oob) {
			//printf("out of bounds.\n");
			noob++;
			continue;
		}

		lst = intmap_find(starlists, hp, TRUE);
		/*
		 printf("list has %i existing entries.\n", bl_size(lst));
		 for (k=0; k<bl_size(lst); k++) {
		 bl_get(lst, k, &j32);
		 printf("  %i: index %i, %s = %g\n", k, j32, sortcol, sortval[j32]);
		 }
		 */

		// is this list full?
		if (nkeep && (bl_size(lst) >= nkeep)) {
			// Here we assume we're working in sorted order: once the list is full we're done.
			//printf("Skipping: list is full.\n");
			continue;
		}

		if ((dedupr2 > 0.0) &&
			is_duplicate(hp, ra[j], dec[j], Nside, starlists, ra, dec, dedupr2)) {
			//printf("Skipping: duplicate\n");
			ndup++;
			continue;
		}

		// Add the new star (by index)
		j32 = j;
		bl_append(lst, &j32);
	}
	logverb("%i outside the healpix\n", noob);
	logverb("%i duplicates\n", ndup);

	il_free(myhps);
	myhps = NULL;
	free(inorder);
	inorder = NULL;
	free(ra);
	ra = NULL;
	free(dec);
	dec = NULL;

	outorder = malloc(N * sizeof(int));
	outi = 0;

	npersweep = calloc(nsweeps, sizeof(int));

	for (k=0; k<nsweeps; k++) {
		int starti = outi;
		int32_t j32;
		for (i=0;; i++) {
			bl* lst;
			int hp;
			if (!intmap_get_entry(starlists, i, &hp, &lst))
				break;
			if (bl_size(lst) <= k)
				continue;
			bl_get(lst, k, &j32);
			outorder[outi] = j32;
			//printf("sweep %i, cell #%i, hp %i, star %i, %s = %g\n", k, i, hp, j32, sortcol, sortval[j32]);
			outi++;
		}
		logmsg("Sweep %i: %i stars\n", k+1, outi - starti);
		npersweep[k] = outi - starti;

		if (sortcol) {
			// Re-sort within this sweep.
			permuted_sort(sortval, sizeof(double),
						  sort_ascending ? compare_doubles_asc : compare_doubles_desc,
						  outorder + starti, npersweep[k]);
			/*
			 for (i=0; i<npersweep[k]; i++) {
			 printf("  within sweep %i: star %i, j=%i, %s=%g\n",
			 k, i, outorder[starti + i], sortcol, sortval[outorder[starti + i]]);
			 }
			 */
		}

	}
	intmap_free(starlists);
	starlists = NULL;

	//////
	free(sortval);
	sortval = NULL;

	logmsg("Total: %i stars\n", outi);
	N = outi;

	outhdr = fitstable_get_primary_header(outtable);
    if (allsky)
        qfits_header_add(outhdr, "ALLSKY", "T", "All-sky catalog.", NULL);
    BOILERPLATE_ADD_FITS_HEADERS(outhdr);
    qfits_header_add(outhdr, "HISTORY", "This file was generated by the command-line:", NULL, NULL);
    fits_add_args(outhdr, args, argc);
    qfits_header_add(outhdr, "HISTORY", "(end of command line)", NULL, NULL);
	fits_add_long_history(outhdr, "uniformize-catalog args:");
	fits_add_long_history(outhdr, "  RA,Dec columns: %s,%s", racol, deccol);
	fits_add_long_history(outhdr, "  sort column: %s", sortcol);
	fits_add_long_history(outhdr, "  sort direction: %s", sort_ascending ? "ascending" : "descending");
	if (sort_ascending)
		fits_add_long_history(outhdr, "    (ie, for mag-like sort columns)");
	else
		fits_add_long_history(outhdr, "    (ie, for flux-like sort columns)");
	fits_add_long_history(outhdr, "  uniformization nside: %i", Nside);
	fits_add_long_history(outhdr, "    (ie, side length ~ %g arcmin)", healpix_side_length_arcmin(Nside));
	fits_add_long_history(outhdr, "  deduplication scale: %g arcsec", dedup_radius);
	fits_add_long_history(outhdr, "  number of sweeps: %i", nsweeps);

    fits_header_add_int(outhdr, "NSTARS", N, "Number of stars.");
    fits_header_add_int(outhdr, "HEALPIX", bighp, "Healpix covered by this catalog, with Nside=HPNSIDE");
    fits_header_add_int(outhdr, "HPNSIDE", bignside, "Nside of HEALPIX.");
	fits_header_add_int(outhdr, "CUTNSIDE", Nside, "uniformization scale (healpix nside)");
	fits_header_add_int(outhdr, "CUTMARG", nmargin, "margin size, in healpixels");
	//qfits_header_add(outhdr, "CUTBAND", cutband, "band on which the cut was made", NULL);
	fits_header_add_double(outhdr, "CUTDEDUP", dedup_radius, "deduplication radius [arcsec]");
	fits_header_add_int(outhdr, "CUTNSWEP", nsweeps, "number of sweeps");
	//fits_header_add_double(outhdr, "CUTMINMG", minmag, "minimum magnitude");
	//fits_header_add_double(outhdr, "CUTMAXMG", maxmag, "maximum magnitude");
	for (k=0; k<nsweeps; k++) {
		char key[64];
		sprintf(key, "SWEEP%i", (k+1));
        fits_header_add_int(outhdr, key, npersweep[k], "# stars added");
	}
	free(npersweep);

	if (fitstable_write_primary_header(outtable)) {
		ERROR("Failed to write primary header");
		return -1;
	}

	// Write output.
	fitstable_add_fits_columns_as_struct2(intable, outtable);
	if (fitstable_write_header(outtable)) {
		ERROR("Failed to write output table header");
		return -1;
	}
	logmsg("Writing output...\n");
	logverb("Row size: %i\n", fitstable_row_size(intable));
	if (fitstable_copy_rows_data(intable, outorder, N, outtable)) {
		ERROR("Failed to copy rows from input table to output");
		return -1;
	}
	if (fitstable_fix_header(outtable)) {
		ERROR("Failed to fix output table header");
		return -1;
	}
	free(outorder);
	return 0;
}
示例#9
0
int matchfile_count(matchfile* mf) {
	return fitstable_nrows(mf);
}
示例#10
0
int nomad_fits_count_entries(nomad_fits* cat) {
	return fitstable_nrows(cat);
}
示例#11
0
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;
}
示例#12
0
int loadltcube(char *filename, LTCUBE_DATA_STRUCT *ltcubeds) {
  fitstable_t* tab;
  qfits_header* hdr;
  tfits_type flt = fitscolumn_float_type();
  
  if ( (tab = fitstable_open(filename))==NULL) {
    printf("# Cannot open %s in loadltcube() %s:%d\n",filename,__FILE__,__LINE__);
    return -1;
  }
  fitstable_open_next_extension(tab);
  hdr = fitstable_get_header(tab);
  strncpy(ltcubeds->ordering,qfits_header_getstr(hdr, "ORDERING"),sizeof(ltcubeds->ordering));
  strncpy(ltcubeds->coordsys,qfits_header_getstr(hdr, "COORDSYS"),sizeof(ltcubeds->coordsys));
  strncpy(ltcubeds->thetabin,qfits_header_getstr(hdr, "THETABIN"),sizeof(ltcubeds->thetabin));

  ltcubeds->nside=qfits_header_getint(hdr,"NSIDE",-1);
  ltcubeds->firstpix=qfits_header_getint(hdr,"FIRSTPIX",-1);
  ltcubeds->lastpix=qfits_header_getint(hdr,"LASTPIX",-1);
  if (debug) { printf("ORDERING= %s; COORDSYS= %s; THETABIN= %s; NSIDE= %d FIRSTPIX= %d LASTPIX= %d\n",
		      ltcubeds->ordering,
		      ltcubeds->coordsys,
		      ltcubeds->thetabin,
		      ltcubeds->nside,
		      ltcubeds->firstpix,
		      ltcubeds->lastpix);
  }


  /* load in livetime cube data */
  ltcubeds->nMu = fitstable_get_array_size(tab, "COSBINS");
  ltcubeds->ncosbins = fitstable_nrows(tab);
  printf("# nrows= %d\n",ltcubeds->ncosbins);
  if ( (ltcubeds->cosbins= (float *) fitstable_read_column_array(tab, "COSBINS", flt))==NULL) {
    printf("# cannot load vector COSBINS from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__);
    fitstable_close(tab);
    return -1;
  }

  fitstable_open_next_extension(tab);

  if ( (ltcubeds->cthetamin= (float *) fitstable_read_column(tab, "CTHETA_MIN", flt))==NULL) {
    printf("# cannot load vector CTHETA_MIN from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__);
    fitstable_close(tab);
    return -1;
  }

  if ( (ltcubeds->cthetamax= (float *) fitstable_read_column(tab, "CTHETA_MAX", flt))==NULL) {
    printf("# cannot load vector CTHETA_MAX from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__);
    fitstable_close(tab);
    return -1;
  }

  if ( (ltcubeds->aeffmubin = (int *) calloc(ltcubeds->nMu,sizeof(int)))==NULL) {
    printf("# cannot allocate vector AEFFMUBIN in loadltcude() %s:%d\n",__FILE__,__LINE__);
    fitstable_close(tab);
    return -1;
  }

  ltcubeds->aeffmubin[0]=-99;

  if (debug>2) {
    int i, j;
    printf("# nMu= %d\n",ltcubeds->nMu);
    for (i=0;i<ltcubeds->nMu;i++) {
      printf("%3d %10g %10g\n",i,ltcubeds->cthetamin[i],ltcubeds->cthetamax[i]);
    }
    for (j=0;j<10;j++) {
      printf("j= %d",j);
      for (i=0;i<ltcubeds->nMu;i++) {
	printf(" %10g",ltcubeds->cosbins[j*ltcubeds->nMu+i]);
      }
      printf("\n");
    }
  }

  printf("# loaded %s\n",filename);
  return 0;
}
示例#13
0
int
loadphotondata(char *filename, char *passfile) {
    fitstable_t* tab;
    qfits_header* hdr;
    tfits_type flt = fitscolumn_float_type(), chtype = fitscolumn_char_type(), dbltype=fitscolumn_double_type();
    float *localphotondata[NPHOTON_LOADDATA], *galdisrsp=NULL, *isodisrsp=NULL;
    double *localphotontime;
    char *conversion_type=NULL;
    char buffer[255];
    int i, j, ncurr, ndiff, nresp=0, conv_type, psf_cnt, psf_ind;
    double rmax2, ehold, rad, r2hold;

    if ( (tab = fitstable_open(filename))==NULL) {
        printf("# Cannot open %s in loadphotondata() %s:%d\n",filename,__FILE__,__LINE__);
        return -1;
    }
    hdr = fitstable_get_primary_header(tab);
    hdr = fitstable_get_header(tab);
    ncurr = fitstable_nrows(tab);
    if (debug) printf("# filename= %s ncurr= %d\n",filename,ncurr);
    /* load in the energy, theta, ra and dec */
    for (i=0; i<NPHOTON_LOADDATA; i++) {
        if ( (localphotondata[i]=
                    (float *) fitstable_read_column_array(tab,
                            photon_colname[i], flt))==NULL) {
            printf("# vector for %s is NULL in loadphotondata() %s:%d\n",photon_colname[i],__FILE__,__LINE__);
            for (j=0; j<i; j++) {
                SAFEFREE( localphotondata[i]);
            }
            fitstable_close(tab);
            return -1;
        }
    }

    /* load in conversion type */
    if ( (conversion_type= (char *) fitstable_read_column(tab, "CONVERSION_TYPE", chtype))==NULL) {
        printf("# array for conversion_type is NULL in loadphotondata() %s:%d\n",__FILE__,__LINE__);
        for (i=0; i<NPHOTON_LOADDATA; i++) {
            SAFEFREE( localphotondata[i]);
        }
        fitstable_close(tab);
        return -1;
    }

    /* load in photon arrival time */
    if ( (localphotontime= (double *) fitstable_read_column(tab, "TIME", dbltype))==NULL) {
        printf("# array for localphotontime is NULL in loadphotondata() %s:%d\n",__FILE__,__LINE__);
        for (i=0; i<NPHOTON_LOADDATA; i++) {
            SAFEFREE( localphotondata[i]);
        }
        SAFEFREE( conversion_type);
        fitstable_close(tab);
        return -1;
    }

    /* load in the diffuse response functions that match the passfile */
    ndiff = qfits_header_getint(hdr, "NDIFRSP",-1);
    for (i=0; i<ndiff; i++) {
        sprintf(buffer,"DIFRSP%d",i);
        char *respname = qfits_header_getstr(hdr, buffer);
        if (debug) printf("# %d %s = %s\n",i,buffer,respname);
        if (strcasestr(respname,passfile)) {
            if (strcasestr(respname,"gll")) {
                if (debug) printf("# galactic is %s\n",buffer);
                if ( (galdisrsp=
                            (float *) fitstable_read_column_array(tab,
                                    buffer, flt))==NULL) {
                    printf("# array for %s is NULL in loadphotondata() %s:%d\n",buffer,__FILE__,__LINE__);
                    for (j=0; j<i; j++) {
                        SAFEFREE( localphotondata[i]);
                    }
                    SAFEFREE( conversion_type);
                    SAFEFREE(localphotontime);
                    SAFEFREE( galdisrsp);
                    SAFEFREE( isodisrsp);
                    fitstable_close(tab);
                    return -1;
                }
                nresp++;
            }
            if (strcasestr(respname,"iso")) {
                if (debug) printf("# iso is %s\n",buffer);
                if ( (isodisrsp=
                            (float *) fitstable_read_column_array(tab,
                                    buffer, flt))==NULL) {
                    printf("# array for %s is NULL in loadphotondata() %s:%d\n",buffer,__FILE__,__LINE__);
                    for (j=0; j<i; j++) {
                        SAFEFREE( localphotondata[i]);
                    }
                    SAFEFREE( conversion_type);
                    SAFEFREE(localphotontime);
                    SAFEFREE( galdisrsp);
                    SAFEFREE( isodisrsp);
                    fitstable_close(tab);
                    return -1;
                }
                nresp++;
            }
        }
    }

    if (nresp<2) {
        printf("Could not find the matching response functions in loadphotondata() %s:%d.\n",__FILE__,__LINE__);
        return -1;
    }

    if (alloc_globals(ncurr+ntot)) {
        SAFEFREE(localphotontime);
        SAFEFREE(conversion_type);
        SAFEFREE(data);
        for (i=0; i<NPHOTON_LOADDATA; i++) {
            SAFEFREE(localphotondata[i]);
        }
        return -1;
    }
    fitstable_close(tab);

    j=0;
    for (i=0; i<ncurr; i++) {
        ehold=localphotondata[ENERGY][i];
        if (ehold>=e0 && ehold<=e1) {
            double ra, dec;
            __sincospi((ra=localphotondata[RA][i])/180.0,data+d*(j+ntot)+1,data+d*(j+ntot));
            __sincospi((dec=localphotondata[DEC][i])/180.0,data+d*(j+ntot)+2,&rad);
            data[d*(ntot+j)]*=rad;
            data[d*(ntot+j)+1]*=rad;
            if (ehold<energy_min) energy_min=ehold;
            /* calculate RMAX2 for this photon */
            conv_type=conversion_type[i];
            rmax2=photondata[COSTHETA][j+ntot]=cos(localphotondata[THETA][i]*M_PI/180.0);
            if (debug>3) printf("# costheta= %g\n",rmax2);
#if 1
            /* fast way --- evenly spaced in log energy and costheta -- no interpolation */
            //      rmax2=psfds.psfdata[conv_type][RMAX2][(int) ((rmax2-psfds.mumin)/psfds.mustep)*psfds.nE + (int) ((log10(ehold)-psfds.lemin)/psfds.lestep)];
            rmax2=rmax2_funk(psfds,conv_type,rmax2,ehold);
#else
            /* slow way --- find the right bin -- no interpolation */
            for (psf_cnt=0; psf_cnt<psfds.nE; psf_cnt++) {
                if (ehold>psfds.psfdata[conv_type][ENERG_LO][psf_cnt] && ehold<=psfds.psfdata[conv_type][ENERG_HI][psf_cnt]) {
                    psf_ind=psf_cnt;
                    break;
                }
            }
            for (psf_cnt=0; psf_cnt<psfds.nMu; psf_cnt++) {
                if (rmax2>psfds.psfdata[conv_type][CTHETA_LO][psf_cnt] && rmax2<=psfds.psfdata[conv_type][CTHETA_HI][psf_cnt]) {
                    psf_ind+=psf_cnt*psfds.nE;
                    break;
                }
            }
            rmax2=psfds.psfdata[conv_type][RMAX2][psf_ind];
#endif
            if (debug>3) printf("# rmax2= %g\n",rmax2);
            if (conv_type) {
                /* back conversion */
                r2hold=SPE_squared_back(ehold)*rmax2;
                data[d*(ntot+j)+3]=-(r2hold>4 ? 4 : r2hold);
            } else {
                /* front conversion */
                r2hold=SPE_squared_front(ehold)*rmax2;
                data[d*(ntot+j)+3]=(r2hold>4 ? 4 : r2hold);
            }
            if (debug>3) printf("# %g rmax2= %g %g %g\n",ehold,data[d*(ntot+j)+3],SPE_squared_front(ehold),localphotondata[THETA][i]);

            photondata[ENERGY][j+ntot]=ehold;
            photondata[DIFRSP_GAL][j+ntot]=galdisrsp[i];
            photondata[DIFRSP_ISO][j+ntot]=isodisrsp[i];
            r2hold=aeffds.aeffdata[conv_type][AEFF_EFF_AREA]
                   [(int) ((photondata[COSTHETA][j+ntot]-aeffds.mumin)/aeffds.mustep)*aeffds.nE + (int) ((log10(ehold)-aeffds.lemin)/aeffds.lestep)];
            if (r2hold<EFFAREAMIN) r2hold=EFFAREAMIN;
            photondata[EFFAREA][j+ntot]=r2hold;

            /* calculate the effective area integral */
            r2hold=calcefft(ra,dec,ehold,&aeffds,&ltcubeds);
            if (r2hold<EFFAREATMIN) r2hold=EFFAREATMIN;
            photondata[EFFAREAT][j+ntot]=r2hold;

            photontime[j+ntot]=localphotontime[i];
            j++;
        }
    }

    for (i=0; i<NPHOTON_LOADDATA; i++) {
        SAFEFREE(localphotondata[i]);
    }
    SAFEFREE( galdisrsp);
    SAFEFREE( isodisrsp);
    SAFEFREE(conversion_type);
    SAFEFREE(localphotontime);

    ntot+=j;
    if (debug) printf("# ntot= %d j= %d\n",ntot,j);
    if (j!=ncurr) {
        if (alloc_globals(ncurr+ntot)) {
            SAFEFREE(data);
            for (i=0; i<NPHOTON_LOADDATA; i++) {
                SAFEFREE(localphotondata[i]);
            }
            return -1;
        }
    }

    return 0;
}
示例#14
0
int
loadpsffile(char *passfile, PSF_DATA_STRUCT *psfds, float f_cutoff) {
  char filename[2][255]={"front.fits","back.fits"};
  char buffer[255];
  int i, j, f, ncurr;
  float *psfscale;
  fitstable_t* tab;
  qfits_header* hdr;
  tfits_type flt = fitscolumn_float_type();
  float ***psfdata;
  int nE, nMu;

  if ( !(psfdata=(float ***) calloc(2,sizeof(float **))) ) {
    printf("# Cannot allocate psfdata in loadpsffile() %s:%d\n",__FILE__,__LINE__);
    return -1;
  }

  if ( !(psfdata[0]=(float **) calloc(NPSF_DATA,sizeof(float *))) ) {
    printf("# Cannot allocate psfdata[0] in loadpsffile() %s:%d\n",__FILE__,__LINE__);
    return -1;
  }

  if ( !(psfdata[1]=(float **) calloc(NPSF_DATA,sizeof(float *))) ) {
    printf("# Cannot allocate psfdata[1] in loadpsffile() %s:%d\n",__FILE__,__LINE__);
    return -1;
  }


  psfds->psfdata=psfdata;
  psfds->f_cutoff=f_cutoff;

  if (debug>2) {
    printf("      ");
    for (i=0;i<NPSF_DATA;i++) {
      printf(" %10s",psf_colname[i]);
    }
    printf("\n");
  }

  for (f=0;f<2;f++) {
    sprintf(buffer,"psf_%s_%s",passfile,filename[f]);
    printf("# loading PSF file: %s\n",buffer);
    if ( (tab = fitstable_open(buffer))==NULL) {
      printf("# Cannot open %s in loadpsffile() %s:%d\n",buffer,__FILE__,__LINE__);
      return -1;
    }
    hdr = fitstable_get_primary_header(tab);
    ncurr = fitstable_nrows(tab);
    for (i=ENERG_LO;i<=GTAIL;i++) {
      if ( (psfdata[f][i]= (float *) fitstable_read_column_array(tab, psf_colname[i], flt))==NULL) {
	printf("# Cannot file column %s (file=%s) in loadpsffile() %s:%d\n",psf_colname[i],buffer,__FILE__,__LINE__);
	fitstable_close(tab);
	return -1;
      }
    }
    int D = fitstable_get_array_size(tab, psf_colname[NTAIL]);
    psfds->nE = nE = fitstable_get_array_size(tab, psf_colname[ENERG_LO]);
    psfds->nMu = nMu = fitstable_get_array_size(tab, psf_colname[CTHETA_LO]);
    for (i=FCORE;i<=RMAX2;i++) {
      if (!(psfdata[f][i]=(float *) calloc(D,sizeof(float)))) {
	printf("# Cannot allocate psfdata[%d][%d] in loadpsffile() %s:%d\n",f,i,__FILE__,__LINE__);
	return -1;
      }
    }

    for (j=0;j<D;j++) {
      if (debug>2) printf("[%2d] %2d",f,j);
      score_g=psfdata[f][SCORE][j];
      gcore_g=psfdata[f][GCORE][j];
      stail_g=psfdata[f][STAIL][j];
      gtail_g=psfdata[f][GTAIL][j];
      psfdata[f][FACTORTAIL][j]=factortail_g=gtail_g*stail_g*stail_g*2;
      psfdata[f][FACTORCORE][j]=factorcore_g=gcore_g*score_g*score_g*2;
      fcore_g=stail_g/score_g;
      psfdata[f][FCORE][j]=fcore_g=1/(1+psfdata[f][NTAIL][j]*fcore_g*fcore_g);
      psfdata[f][NORMCORE][j]=fcore_g/(2*M_PI*score_g*score_g)*(1-1/gcore_g);
      psfdata[f][NORMTAIL][j]=(1-fcore_g)/(2*M_PI*stail_g*stail_g)*(1-1/gtail_g); ;
      psfdata[f][RMAX2][j]=root(f_cutoff);
      if (debug>2) {
	for (i=0;i<2;i++) {
	  printf(" %10g",log10(psfdata[f][i][j%nE]));
	}
	for (i=2;i<4;i++) {
	  printf(" %10g",psfdata[f][i][(j/nE)%nMu]);
	}
	for (i=4;i<NPSF_DATA;i++) {
	  printf(" %10g",psfdata[f][i][j]);
	}
	printf(" %10g\n",ickingfunk(psfdata[f][RMAX2][j]));
      }
    }
    if (f==0) {
      psfds->lemin=log10(psfdata[f][ENERG_LO][0]);
      psfds->lestep=log10(psfdata[f][ENERG_HI][0])-log10(psfdata[f][ENERG_LO][0]);
      psfds->mumin=psfdata[f][CTHETA_LO][0];
      psfds->mustep=psfdata[f][CTHETA_HI][0]-psfdata[f][CTHETA_LO][0];
      fitstable_open_next_extension(tab);
      hdr = fitstable_get_primary_header(tab);
      ncurr = fitstable_nrows(tab);
      if ( (psfscale= (float *) fitstable_read_column_array(tab, "PSFSCALE", flt))==NULL) {
	printf("# Cannot read PSFSCALE (file=%s) in loadpsffile() %s:%d\n",buffer,__FILE__,__LINE__);
	fitstable_close(tab);
	return -1;
      }
      c0front2=psfscale[0]*psfscale[0];
      c1front2=psfscale[1]*psfscale[1];
      c0back2=psfscale[2]*psfscale[2];
      c1back2=psfscale[3]*psfscale[3];
      minus2beta=2*psfscale[4];
      if (debug>1) { printf("# %g %g %g %g %g\n",c0front2,c1front2,c0back2,c1back2,minus2beta); }
    }
    fitstable_close(tab);
  }
  free((void *) psfscale);
  return 0;
}