Example #1
0
static rd_t* get_rd(plotradec_t* args, rd_t* myrd) {
	rdlist_t* rdls = NULL;
	rd_t* rd = NULL;
	if (args->fn) {
		// Open rdlist.
		rdls = rdlist_open(args->fn);
		if (!rdls) {
			ERROR("Failed to open rdlist from file \"%s\"", args->fn);
			return NULL;
		}
		if (args->racol)
			rdlist_set_raname(rdls, args->racol);
		if (args->deccol)
			rdlist_set_decname(rdls, args->deccol);

		// Find number of entries in rdlist.
		rd = rdlist_read_field_num(rdls, args->ext, NULL);
		//freerd = rd;
		rdlist_close(rdls);
		if (!rd) {
			ERROR("Failed to read FITS extension %i from file %s.\n", args->ext, args->fn);
			return NULL;
		}
	} else {
		assert(dl_size(args->radecvals));
		rd_from_dl(myrd, args->radecvals);
		rd = myrd;
	}
	return rd;
}
Example #2
0
int wcs_rd2xy(const char* wcsfn, int wcsext,
			  const char* rdlsfn, const char* xylsfn,
              const char* racol, const char* deccol,
			  anbool forcetan, anbool forcewcslib,
              il* fields) {
	xylist_t* xyls = NULL;
	rdlist_t* rdls = NULL;
	anwcs_t* wcs = NULL;
	int i;
    anbool alloced_fields = FALSE;
    int rtn = -1;

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

	// read RDLS.
	rdls = rdlist_open(rdlsfn);
	if (!rdls) {
		ERROR("Failed to read an RA,Dec list from file %s", rdlsfn);
        goto bailout;
	}
	if (racol)
        rdlist_set_raname(rdls, racol);
	if (deccol)
		rdlist_set_decname(rdls, deccol);

	// write XYLS.
	xyls = xylist_open_for_writing(xylsfn);
	if (!xyls) {
		ERROR("Failed to open file %s to write XYLS", xylsfn);
        goto bailout;
	}
	if (xylist_write_primary_header(xyls)) {
		ERROR("Failed to write header to XYLS file %s", xylsfn);
        goto bailout;
	}

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

	for (i=0; i<il_size(fields); i++) {
		int fieldnum = il_get(fields, i);
		int j;
        starxy_t xy;
        rd_t rd;

        if (!rdlist_read_field_num(rdls, fieldnum, &rd)) {
			ERROR("Failed to read rdls file \"%s\" field %i", rdlsfn, fieldnum);
            goto bailout;
        }

        starxy_alloc_data(&xy, rd_n(&rd), FALSE, FALSE);

		if (xylist_write_header(xyls)) {
			ERROR("Failed to write xyls field header");
            goto bailout;
		}

		for (j=0; j<rd_n(&rd); j++) {
			double x, y, ra, dec;
            ra  = rd_getra (&rd, j);
            dec = rd_getdec(&rd, j);
			if (anwcs_radec2pixelxy(wcs, ra, dec, &x, &y)) {
				ERROR("Point RA,Dec = (%g,%g) projects to the opposite side of the sphere", ra, dec);
				starxy_set(&xy, j, NAN, NAN);
				continue;
			}
            starxy_set(&xy, j, x, y);
		}
        if (xylist_write_field(xyls, &xy)) {
            ERROR("Failed to write xyls field");
            goto bailout;
        }
		if (xylist_fix_header(xyls)) {
            ERROR("Failed to fix xyls field header");
            goto bailout;
		}
        xylist_next_field(xyls);

        starxy_free_data(&xy);
        rd_free_data(&rd);
	}

	if (xylist_fix_primary_header(xyls) ||
		xylist_close(xyls)) {
		ERROR("Failed to fix header of XYLS file");
        goto bailout;
	}
    xyls = NULL;

	if (rdlist_close(rdls)) {
		ERROR("Failed to close RDLS file");
        goto bailout;
	}
    rdls = 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;
}
Example #3
0
int main(int argc, char** args) {
	int c;
	char* xylsfn = NULL;
	char* rdlsfn = NULL;
	char* corrfn = NULL;
	char* outfn = NULL;
	char* xcol = NULL;
	char* ycol = NULL;
	char* rcol = NULL;
	char* dcol = NULL;

	xylist_t* xyls = NULL;
	rdlist_t* rdls = NULL;
	rd_t rd;
	starxy_t xy;
	int fieldnum = 1;
	int N;
	double* fieldxy = NULL;
	double* xyz = NULL;
	sip_t wcs;
	int rtn = -1;
    int loglvl = LOG_MSG;
    int siporder = 0;
    int W=0, H=0;
    anbool crpix_center = FALSE;
    int i;
    int doshift = 1;

	fits_use_error_system();

    while ((c = getopt(argc, args, OPTIONS)) != -1) {
        switch (c) {
        case 'h':
			print_help(args[0]);
			exit(0);
        case 'W':
            W = atoi(optarg);
            break;
        case 'H':
            H = atoi(optarg);
            break;
        case 'C':
            crpix_center = TRUE;
            break;
        case 's':
            siporder = atoi(optarg);
            break;
		case 'c':
			corrfn = optarg;
			break;
		case 'r':
			rdlsfn = optarg;
			break;
		case 'R':
			rcol = optarg;
			break;
		case 'D':
			dcol = optarg;
			break;
		case 'x':
			xylsfn = optarg;
			break;
		case 'X':
			xcol = optarg;
			break;
		case 'Y':
			ycol = optarg;
			break;
		case 'o':
			outfn = optarg;
			break;
        case 'v':
            loglvl++;
            break;
		}
	}
	if (optind != argc) {
		print_help(args[0]);
		exit(-1);
	}
	if (! ((xylsfn && rdlsfn) || corrfn) || !outfn) {
		print_help(args[0]);
		exit(-1);
	}
    log_init(loglvl);

	if (corrfn) {
		xylsfn = corrfn;
		rdlsfn = corrfn;
		if (!xcol)
			xcol = "FIELD_X";
		if (!ycol)
			ycol = "FIELD_Y";
		if (!rcol)
			rcol = "INDEX_RA";
		if (!dcol)
			dcol = "INDEX_DEC";
	}

	// 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);

	// read RDLS.
	rdls = rdlist_open(rdlsfn);
	if (!rdls) {
		ERROR("Failed to read an RA,Dec list from file %s", rdlsfn);
        goto bailout;
	}
	if (rcol)
        rdlist_set_raname(rdls, rcol);
	if (dcol)
		rdlist_set_decname(rdls, dcol);

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

	N = starxy_n(&xy);
	if (rd_n(&rd) != N) {
		ERROR("X,Y list and RA,Dec list must have the same number of entries, "
			  "but found %i vs %i", N, rd_n(&rd));
		goto bailout;
	}
	logverb("Read %i points from %s and %s\n", N, rdlsfn, xylsfn);

	xyz = (double*)malloc(sizeof(double) * 3 * N);
	if (!xyz) {
		ERROR("Failed to allocate %i xyz coords", N);
		goto bailout;
	}
	radecdeg2xyzarrmany(rd.ra, rd.dec, xyz, N);

	fieldxy = starxy_to_xy_array(&xy, NULL);
	if (!fieldxy) {
		ERROR("Failed to allocate %i xy coords", N);
		goto bailout;
	}

	logverb("Fitting WCS\n");
    if (siporder == 0) {
        if (fit_tan_wcs(xyz, fieldxy, N, &(wcs.wcstan), NULL)) {
            ERROR("Failed to fit for TAN WCS");
            goto bailout;
        }
    } else {
        if (W == 0) {
            for (i=0; i<N; i++) {
                W = MAX(W, (int)ceil(fieldxy[2*i + 0]));
            }
        }
        if (H == 0) {
            for (i=0; i<N; i++) {
                H = MAX(H, (int)ceil(fieldxy[2*i + 1]));
            }
        }
        logverb("Image size = %i x %i pix\n", W, H);

        fit_sip_wcs_2(xyz, fieldxy, NULL, N,
                      siporder, siporder+1, W, H,
                      crpix_center, NULL, doshift, &wcs);
    }

    if (siporder <= 1) {
        if (tan_write_to_file(&(wcs.wcstan), outfn)) {
            ERROR("Failed to write TAN WCS header to file \"%s\"", outfn);
            goto bailout;
        }
    } else {
        if (sip_write_to_file(&wcs, outfn)) {
            ERROR("Failed to write SIP WCS header to file \"%s\"", outfn);
            goto bailout;
        }
    }
	logverb("Wrote WCS to %s\n", outfn);

	starxy_free_data(&xy);
	rd_free_data(&rd);

	rtn = 0;

 bailout:
    if (rdls)
        rdlist_close(rdls);
    if (xyls)
        xylist_close(xyls);
	if (fieldxy)
		free(fieldxy);
	if (xyz)
		free(xyz);

	return rtn;
}