Esempio n. 1
0
int wcs_xy2rd(const char* wcsfn, int ext,
			  const char* xylsfn, const char* rdlsfn,
              const char* xcol, const char* ycol,
			  int forcetan,
			  int forcewcslib,
              il* fields) {
	rdlist_t* rdls = NULL;
	xylist_t* xyls = NULL;
	anwcs_t* wcs = NULL;
	int i;
    int rtn = -1;
    anbool alloced_fields = FALSE;

	// read WCS.
	if (forcewcslib) {
		wcs = anwcs_open_wcslib(wcsfn, ext);
	} else if (forcetan) {
		wcs = anwcs_open_tan(wcsfn, ext);
	} else {
		wcs = anwcs_open(wcsfn, ext);
	}
	if (!wcs) {
		ERROR("Failed to read WCS file \"%s\", extension %i", wcsfn, ext);
		return -1;
	}

	// read XYLS.
	xyls = xylist_open(xylsfn);
	if (!xyls) {
		ERROR("Failed to read an xylist from file %s", xylsfn);
		goto bailout;
	}
    xylist_set_include_flux(xyls, FALSE);
    xylist_set_include_background(xyls, FALSE);
	if (xcol)
		xylist_set_xname(xyls, xcol);
	if (ycol)
		xylist_set_yname(xyls, ycol);

	// write RDLS.
	rdls = rdlist_open_for_writing(rdlsfn);
	if (!rdls) {
		ERROR("Failed to open file %s to write RDLS.\n", rdlsfn);
		goto bailout;
	}
	if (rdlist_write_primary_header(rdls)) {
		ERROR("Failed to write header to RDLS file %s.\n", rdlsfn);
		goto bailout;
	}

    if (!fields) {
        alloced_fields = TRUE;
        fields = il_new(16);
    }
	if (!il_size(fields)) {
		// add all fields.
		int NF = xylist_n_fields(xyls);
		for (i=1; i<=NF; i++)
			il_append(fields, i);
	}

	logverb("Processing %zu extensions...\n", il_size(fields));
	for (i=0; i<il_size(fields); i++) {
		int fieldind = il_get(fields, i);
        starxy_t xy;
        rd_t rd;
		int j;

        if (!xylist_read_field_num(xyls, fieldind, &xy)) {
			ERROR("Failed to read xyls file %s, field %i", xylsfn, fieldind);
			goto bailout;
        }

		if (rdlist_write_header(rdls)) {
			ERROR("Failed to write rdls field header to %s", rdlsfn);
			goto bailout;
		}

        rd_alloc_data(&rd, starxy_n(&xy));

		for (j=0; j<starxy_n(&xy); j++) {
            double x, y, ra, dec;
            x = starxy_getx(&xy, j);
            y = starxy_gety(&xy, j);
			anwcs_pixelxy2radec(wcs, x, y, &ra, &dec);
            rd_setra (&rd, j, ra);
            rd_setdec(&rd, j, dec);
		}

        if (rdlist_write_field(rdls, &rd)) {
            ERROR("Failed to write rdls field to %s", rdlsfn);
			goto bailout;
        }
        rd_free_data(&rd);
        starxy_free_data(&xy);

		if (rdlist_fix_header(rdls)) {
			ERROR("Failed to fix rdls field header for %s", rdlsfn);
			goto bailout;
		}

        rdlist_next_field(rdls);
	}

	if (rdlist_fix_primary_header(rdls) ||
		rdlist_close(rdls)) {
		ERROR("Failed to fix header of RDLS file %s", rdlsfn);
		goto bailout;
	}
	rdls = NULL;

	if (xylist_close(xyls)) {
		ERROR("Failed to close XYLS file %s", xylsfn);
		goto bailout;
	}
	xyls = NULL;
	rtn = 0;

 bailout:
    if (alloced_fields)
        il_free(fields);
    if (rdls)
        rdlist_close(rdls);
    if (xyls)
        xylist_close(xyls);
	if (wcs)
		anwcs_free(wcs);
    return rtn;
}
Esempio n. 2
0
int main(int argc, char** args) {
    int argchar;
	char* basename;
	char* outfn = NULL;
	index_t* index;
	quadfile* qf;
    rdlist_t* rdls;
	startree_t* skdt = NULL;
    anbool addradius = FALSE;
	int i;
    int radcolumn = -1;

    while ((argchar = getopt (argc, args, OPTIONS)) != -1)
        switch (argchar) {
        case 'R':
            addradius = TRUE;
            break;
		case 'r':
			outfn = optarg;
			break;
		case 'h':
			print_help(args[0]);
			exit(0);
		}

	if (!outfn || (optind == argc)) {
		print_help(args[0]);
		exit(-1);
	}

    rdls = rdlist_open_for_writing(outfn);
    if (!rdls) {
        fprintf(stderr, "Failed to open RDLS file %s for output.\n", outfn);
        exit(-1);
    }
    if (rdlist_write_primary_header(rdls)) {
        fprintf(stderr, "Failed to write RDLS header.\n");
        exit(-1);
    }

    if (addradius) {
        radcolumn = rdlist_add_tagalong_column(rdls, fitscolumn_double_type(),
                                               1, fitscolumn_double_type(),
                                               "QUADRADIUS", "deg");
    }

	for (; optind<argc; optind++) {
		//int Nstars;
        int dimquads;

		basename = args[optind];
		printf("Reading files with basename %s\n", basename);

		index = index_load(basename, 0, NULL);
		if (!index) {
			fprintf(stderr, "Failed to read index with base name \"%s\"\n", basename);
			exit(-1);
		}

		qf = index->quads;
        skdt = index->starkd;
        //Nstars = startree_N(skdt);
        
        if (rdlist_write_header(rdls)) {
            fprintf(stderr, "Failed to write new RDLS field header.\n");
            exit(-1);
        }

        dimquads = quadfile_dimquads(qf);

		printf("Reading quads...\n");
		for (i=0; i<qf->numquads; i++) {
			unsigned int stars[dimquads];
            double axyz[3], bxyz[3];
            double midab[3];
            double radec[2];
			if (!(i % 200000)) {
				printf(".");
				fflush(stdout);
			}
			quadfile_get_stars(qf, i, stars);
            startree_get(skdt, stars[0], axyz);
            startree_get(skdt, stars[1], bxyz);
            star_midpoint(midab, axyz, bxyz);
            xyzarr2radecdegarr(midab, radec);

            if (rdlist_write_one_radec(rdls, radec[0], radec[1])) {
                fprintf(stderr, "Failed to write a RA,Dec entry.\n");
                exit(-1);
            }

            if (addradius) {
                double rad = arcsec2deg(distsq2arcsec(distsq(midab, axyz, 3)));
                if (rdlist_write_tagalong_column(rdls, radcolumn, i, 1, &rad, 0)) {
                    fprintf(stderr, "Failed to write quad radius.\n");
                    exit(-1);
                }
            }
		}
		printf("\n");

		index_close(index);

        if (rdlist_fix_header(rdls)) {
            fprintf(stderr, "Failed to fix RDLS field header.\n");
            exit(-1);
        }

		rdlist_next_field(rdls);
	}

    if (rdlist_fix_primary_header(rdls) ||
        rdlist_close(rdls)) {
        fprintf(stderr, "Failed to close RDLS file.\n");
        exit(-1);
    }

	return 0;
}
Esempio n. 3
0
int main(int argc, char** args) {
    int argchar;
	char* outfn = NULL;
	char* fn;
    rdlist_t* rdls;
	startree_t* skdt = NULL;
	int i;
	int loglvl = LOG_MSG;

    while ((argchar = getopt (argc, args, OPTIONS)) != -1)
        switch (argchar) {
		case 'v':
			loglvl++;
			break;
		case 'r':
			outfn = optarg;
			break;
		case 'h':
			print_help(args[0]);
			exit(0);
		}

	log_init(loglvl);
	if (!outfn || (optind == argc)) {
		print_help(args[0]);
		exit(-1);
	}

    rdls = rdlist_open_for_writing(outfn);
    if (!rdls) {
        ERROR("Failed to open RDLS file %s for output", outfn);
        exit(-1);
    }
    if (rdlist_write_primary_header(rdls)) {
        ERROR("Failed to write RDLS header");
        exit(-1);
    }

	for (; optind<argc; optind++) {
		int Nstars;
        fn = args[optind];
        logmsg("Opening star kdtree %s...\n", fn);
        skdt = startree_open(fn);
        if (!skdt) {
            ERROR("Failed to read star kdtree %s", fn);
            exit(-1);
        }
        Nstars = startree_N(skdt);

        if (rdlist_write_header(rdls)) {
            ERROR("Failed to write new RDLS field header");
            exit(-1);
        }

		logmsg("Reading stars...\n");
		for (i=0; i<Nstars; i++) {
            double xyz[3];
            double radec[2];
			if (!(i % 200000)) {
				printf(".");
				fflush(stdout);
			}
            startree_get(skdt, i, xyz);
            xyzarr2radecdegarr(xyz, radec);
            if (rdlist_write_one_radec(rdls, radec[0], radec[1])) {
                ERROR("Failed to write a RA,Dec entry");
                exit(-1);
            }
		}
		printf("\n");

        startree_close(skdt);

        if (rdlist_fix_header(rdls)) {
            ERROR("Failed to fix RDLS field header");
            exit(-1);
        }
	}

    if (rdlist_fix_primary_header(rdls) ||
        rdlist_close(rdls)) {
        ERROR("Failed to close RDLS file");
        exit(-1);
    }

	return 0;
}
Esempio n. 4
0
int main(int argc, char **argv) {
    int argchar;
	startree_t* starkd;
	double ra=0.0, dec=0.0, radius=0.0;
	sl* tag = sl_new(4);
	anbool tagall = FALSE;
	char* starfn = NULL;
	int loglvl = LOG_MSG;
	char** myargs;
	int nmyargs;
	anbool getinds = FALSE;
	double* radec;
	int* inds;
	int N;
	int i;
	char* rdfn = NULL;
	pl* tagdata = pl_new(16);
	il* tagsizes = il_new(16);
	fitstable_t* tagalong = NULL;

    while ((argchar = getopt (argc, argv, OPTIONS)) != -1)
        switch (argchar) {
		case 'o':
			rdfn = optarg;
			break;
		case 'I':
			getinds = TRUE;
			break;
		case 'r':
			ra = atof(optarg);
			break;
		case 'd':
			dec = atof(optarg);
			break;
		case 'R':
			radius = atof(optarg);
			break;
		case 't':
			sl_append(tag, optarg);
			break;
		case 'T':
			tagall = TRUE;
			break;
		case 'v':
			loglvl++;
			break;
        case '?':
            fprintf(stderr, "Unknown option `-%c'.\n", optopt);
		case 'h':
			printHelp(argv[0]);
			break;
		default:
			return -1;
		}

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

	if (nmyargs != 1) {
		ERROR("Got %i arguments; expected 1.\n", nmyargs);
		printHelp(argv[0]);
		exit(-1);
	}
	starfn = myargs[0];

	log_init(loglvl);

	starkd = startree_open(starfn);
	if (!starkd) {
		ERROR("Failed to open star kdtree");
		exit(-1);
	}

	logmsg("Searching kdtree %s at RA,Dec = (%g,%g), radius %g deg.\n",
		   starfn, ra, dec, radius);

	startree_search_for_radec(starkd, ra, dec, radius,
							  NULL, &radec, &inds, &N);

	logmsg("Got %i results.\n", N);

	if (!N)
		goto done;

	if (tagall) {
		int j, M;
		M = startree_get_tagalong_N_columns(starkd); 
		for (j=0; j<M; j++)
			sl_append(tag, startree_get_tagalong_column_name(starkd, j));
	}

	if (sl_size(tag)) {
		tagalong = startree_get_tagalong(starkd);
		if (!tagalong) {
			ERROR("Failed to find tag-along table in index");
			exit(-1);
		}
	}

	if (rdfn) {
		rdlist_t* rd = rdlist_open_for_writing(rdfn);
		il* colnums = il_new(16);

		if (!rd) {
			ERROR("Failed to open output file %s", rdfn);
			exit(-1);
		}
		if (rdlist_write_primary_header(rd)) {
			ERROR("Failed to write header to output file %s", rdfn);
			exit(-1);
		}

		for (i=0; i<sl_size(tag); i++) {
			const char* col = sl_get(tag, i);
			char* units;
			tfits_type type;
			int arraysize;
			void* data;
			int colnum;
			int itemsize;

			if (fitstable_find_fits_column(tagalong, col, &units, &type, &arraysize)) {
				ERROR("Failed to find column \"%s\" in index", col);
				exit(-1);
			}
			itemsize = fits_get_atom_size(type) * arraysize;
			data = fitstable_read_column_array_inds(tagalong, col, type, inds, N, NULL);
			if (!data) {
				ERROR("Failed to read data for column \"%s\" in index", col);
				exit(-1);
			}
			colnum = rdlist_add_tagalong_column(rd, type, arraysize, type, col, NULL);

			il_append(colnums, colnum);
			il_append(tagsizes, itemsize);
			pl_append(tagdata, data);
		}
		if (rdlist_write_header(rd)) {
			ERROR("Failed to write header to output file %s", rdfn);
			exit(-1);
		}

		for (i=0; i<N; i++) {
			if (rdlist_write_one_radec(rd, radec[i*2+0], radec[i*2+1])) {
				ERROR("Failed to write RA,Dec to output file %s", rdfn);
				exit(-1);
			}
		}
		for (i=0; i<sl_size(tag); i++) {
			int col = il_get(colnums, i);
			void* data = pl_get(tagdata, i);
			int itemsize = il_get(tagsizes, i);

			if (rdlist_write_tagalong_column(rd, col, 0, N, data, itemsize)) {
				ERROR("Failed to write tag-along data column %s", sl_get(tag, i));
				exit(-1);
			}
		}
		if (rdlist_fix_header(rd) ||
			rdlist_fix_primary_header(rd) ||
			rdlist_close(rd)) {
			ERROR("Failed to close output file %s", rdfn);
			exit(-1);
		}
		il_free(colnums);

	} else {
		// Header
		printf("# RA, Dec");
		if (getinds)
			printf(", index");
		for (i=0; i<sl_size(tag); i++)
			printf(", %s", sl_get(tag, i));
		printf("\n");

		for (i=0; i<sl_size(tag); i++) {
			const char* col = sl_get(tag, i);
			char* units;
			tfits_type type;
			int arraysize;
			void* data;
			int itemsize;

			if (fitstable_find_fits_column(tagalong, col, &units, &type, &arraysize)) {
				ERROR("Failed to find column \"%s\" in index", col);
				exit(-1);
			}
			itemsize = fits_get_atom_size(type) * arraysize;
			data = fitstable_read_column_array_inds(tagalong, col, type, inds, N, NULL);
			if (!data) {
				ERROR("Failed to read data for column \"%s\" in index", col);
				exit(-1);
			}
			il_append(tagsizes, itemsize);
			pl_append(tagdata, data);
		}

		for (i=0; i<N; i++) {
			//int j;
			printf("%g, %g", radec[i*2+0], radec[i*2+1]);
			if (getinds)
				printf(", %i", inds[i]);

			//// FIXME -- print tag-along data of generic type.
			/*
			 for (j=0; j<pl_size(tagdata); j++) {
			 double* data = pl_get(tagdata, j);
			 printf(", %g", data[i]);
			 }
			 */

			printf("\n");
		}
	}

 done:
	free(radec);
	free(inds);
	for (i=0; i<pl_size(tagdata); i++)
		free(pl_get(tagdata, i));
	pl_free(tagdata);
	il_free(tagsizes);

	return 0;
}