Beispiel #1
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;
}
Beispiel #2
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;
}
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;
}
Beispiel #4
0
int main(int argc, char **argv) {
    int argchar;
	double ra=HUGE_VAL, dec=HUGE_VAL, radius=HUGE_VAL;
	int loglvl = LOG_MSG;
	char** myargs;
	int nmyargs;
	int i;
	char* outfn = NULL;
	fitstable_t* table = NULL;

    while ((argchar = getopt (argc, argv, OPTIONS)) != -1)
        switch (argchar) {
		case 'o':
			outfn = optarg;
			break;
		case 'r':
			ra = atof(optarg);
			break;
		case 'd':
			dec = atof(optarg);
			break;
		case 'R':
			radius = atof(optarg);
			break;
		case 'v':
			loglvl++;
			break;
        case '?':
            fprintf(stderr, "Unknown option `-%c'.\n", optopt);
		case 'h':
		default:
			printHelp(argv[0]);
			return -1;
		}
	log_init(loglvl);
	nmyargs = argc - optind;
	myargs = argv + optind;

	if (nmyargs < 1) {
		printHelp(argv[0]);
		exit(-1);
	}
	if (ra == HUGE_VAL || dec == HUGE_VAL || radius == HUGE_VAL) {
		printHelp(argv[0]);
		exit(-1);
	}

	if (outfn) {
		table = fitstable_open_for_writing(outfn);
		if (!table) {
			ERROR("Failed to open output table");
			exit(-1);
		}
		if (fitstable_write_primary_header(table)) {
			ERROR("Failed to write primary header of output table");
			exit(-1);
		}
	}

	for (i=0; i<nmyargs; i++) {
		char* indexfn = myargs[i];
		index_t index;
		sl* cols;
		int* inds;
		double* radecs;
		int N;
		int j;
		fitstable_t* tagtable = NULL;

		logmsg("Reading index \"%s\"...\n", indexfn);
		if (!index_load(indexfn, 0, &index)) {
			ERROR("Failed to read index \"%s\"", indexfn);
			continue;
		}

		logmsg("Index %s: id %i, healpix %i (nside %i), %i stars, %i quads, dimquads=%i, scales %g to %g arcmin.\n",
			   index.indexname, index.indexid, index.healpix, index.hpnside,
			   index.nstars, index.nquads, index.dimquads,
			   arcsec2arcmin(index.index_scale_lower),
			   arcsec2arcmin(index.index_scale_upper));

		cols = startree_get_tagalong_column_names(index.starkd, NULL);
		{
			char* colstr = sl_join(cols, ", ");
			logmsg("Tag-along columns: %s\n", colstr);
			free(colstr);
		}

		logmsg("Searching for stars around RA,Dec (%g, %g), radius %g deg.\n",
			   ra, dec, radius);
		startree_search_for_radec(index.starkd, ra, dec, radius,
								  NULL, &radecs, &inds, &N);
		logmsg("Found %i stars\n", N);

		if (table) {
			int tagsize;
			int rowsize;
			char* rowbuf = NULL;

			if (i > 0) {
				fitstable_next_extension(table);
				fitstable_clear_table(table);
			}

			tagtable = startree_get_tagalong(index.starkd);
			if (tagtable) {
				fitstable_add_fits_columns_as_struct(tagtable);
				logverb("Input tag-along table:\n");
				if (log_get_level() >= LOG_VERB)
					fitstable_print_columns(tagtable);
				fitstable_copy_columns(tagtable, table);
			}
			tagsize = fitstable_get_struct_size(table);
			debug("tagsize=%i\n", tagsize);
			// Add RA,Dec at the end of the row...
			fitstable_add_write_column_struct(table, fitscolumn_double_type(), 1, tagsize, fitscolumn_double_type(), "RA", "degrees");
			fitstable_add_write_column_struct(table, fitscolumn_double_type(), 1, tagsize + sizeof(double), fitscolumn_double_type(), "DEC", "degrees");
			rowsize = fitstable_get_struct_size(table);
			assert(rowsize == tagsize + 2*sizeof(double));
			debug("rowsize=%i\n", rowsize);
			rowbuf = malloc(rowsize);

			logverb("Output table:\n");
			if (log_get_level() >= LOG_VERB)
				fitstable_print_columns(table);

			if (fitstable_write_header(table)) {
				ERROR("Failed to write header of output table");
				exit(-1);
			}

			for (j=0; j<N; j++) {
				if (tagtable) {
					if (fitstable_read_struct(tagtable, inds[j], rowbuf)) {
						ERROR("Failed to read row %i of tag-along table", inds[j]);
						exit(-1);
					}
				}
				// Add RA,Dec to end of struct...
				memcpy(rowbuf + tagsize, radecs+2*j+0, sizeof(double));
				memcpy(rowbuf + tagsize + sizeof(double), radecs+2*j+1, sizeof(double));
				if (fitstable_write_struct(table, rowbuf)) {
					ERROR("Failed to write row %i of output", j);
					exit(-1);
				}
			}
			free(rowbuf);

			if (fitstable_fix_header(table)) {
				ERROR("Failed to fix header of output table");
				exit(-1);
			}

		}

		sl_free2(cols);
		free(radecs);
		free(inds);

		index_close(&index);
	}

	if (table) {
		if (fitstable_close(table)) {
			ERROR("Failed to close output table");
			exit(-1);
		}
	}

	return 0;
}