Esempio n. 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;
}
Esempio n. 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;
}
Esempio n. 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;
}
Esempio n. 4
0
int main(int argc, char *argv[]) {
    int argchar;
    char* progname = argv[0];
    char** inputfiles = NULL;
    int ninputfiles = 0;
    char* rdlsfname = NULL;
    rdlist_t* rdls = NULL;
    int i;
    int correct, incorrect;
    int firstfield = 1;
    int lastfield = -1;

    int nfields;

    int Ncenter = 0;

    char* fpsolved = NULL;
    char* tpsolved = NULL;
    int nfields_total;

    int firstfileid = 0;
    int lastfileid = 0;
    int fileid;

    matchfile** matchfiles;
    int* mfcursors;

    char* truematchfn = NULL;
    char* falsematchfn = NULL;
    matchfile* truematch = NULL;
    matchfile* falsematch = NULL;

    while ((argchar = getopt (argc, argv, OPTIONS)) != -1) {
        switch (argchar) {
        case 'h':
            printHelp(progname);
            return (HELP_ERR);
        case 'm':
            truematchfn = optarg;
            break;
        case 'M':
            falsematchfn = optarg;
            break;
        case 'A':
            firstfield = atoi(optarg);
            break;
        case 'B':
            lastfield = atoi(optarg);
            break;
        case 'C':
            Ncenter = atoi(optarg);
            break;
        case 'R':
            rdlsfname = optarg;
            break;
        case 'T':
            tpsolved = optarg;
            break;
        case 'F':
            fpsolved = optarg;
            break;
        case 'i':
            firstfileid = atoi(optarg);
            break;
        case 'I':
            lastfileid = atoi(optarg);
            break;
        default:
            return (OPT_ERR);
        }
    }
    if (optind < argc) {
        ninputfiles = argc - optind;
        inputfiles = argv + optind;
    } else {
        printHelp(progname);
        exit(-1);
    }
    if (!rdlsfname) {
        fprintf(stderr, "You must specify an RDLS file!\n");
        printHelp(progname);
        exit(-1);
    }

    if (truematchfn) {
        truematch = matchfile_open_for_writing(truematchfn);
        if (!truematch) {
            fprintf(stderr, "Failed to open file %s for writing matches.\n", truematchfn);
            exit(-1);
        }
        if (matchfile_write_headers(truematch)) {
            fprintf(stderr, "Failed to write header for %s\n", truematchfn);
            exit(-1);
        }
    }
    if (falsematchfn) {
        falsematch = matchfile_open_for_writing(falsematchfn);
        if (!falsematch) {
            fprintf(stderr, "Failed to open file %s for writing matches.\n", falsematchfn);
            exit(-1);
        }
        if (matchfile_write_headers(falsematch)) {
            fprintf(stderr, "Failed to write header for %s\n", falsematchfn);
            exit(-1);
        }
    }

    matchfiles = malloc(ninputfiles * sizeof(matchfile*));
    mfcursors = calloc(ninputfiles, sizeof(int));

    for (i=0; i<ninputfiles; i++) {
        char* fname = inputfiles[i];
        printf("Opening matchfile %s...\n", fname);
        matchfiles[i] = matchfile_open(fname);
        if (!matchfiles[i]) {
            fprintf(stderr, "Failed to open matchfile %s.\n", fname);
            exit(-1);
        }
    }

    correct = incorrect = 0;
    nfields_total = 0;

    for (i=0; i<ninputfiles; i++) {
        matchfile* mf;
        MatchObj* mo;
        mf = matchfiles[i];

        for (fileid=firstfileid; fileid<=lastfileid; fileid++) {
            char fn[1024];
            int nread = 0;
            sprintf(fn, rdlsfname, fileid);
            //printf("Reading rdls file \"%s\"...\n", fn);
            fflush(stdout);
            rdls = rdlist_open(fn);
            if (!rdls) {
                fprintf(stderr, "Couldn't read rdls file.\n");
                exit(-1);
            }

            nfields = rdlist_n_fields(rdls);
            //printf("Read %i fields from rdls file.\n", nfields);
            if ((lastfield != -1) && (nfields > lastfield)) {
                nfields = lastfield + 1;
            } else {
                lastfield = nfields;
            }

            for (; mfcursors[i]<matchfile_count(mf); mfcursors[i]++) {
                int filenum;
                int fieldnum;
                double rac, decc;
                double r2;
                double arc;
                int nrd;
                rd_t* rd;
                int k;
                anbool err = FALSE;

                mo = matchfile_read_match(mf);
                filenum = mo->fieldfile;
                if (filenum < fileid)
                    continue;
                if (filenum > fileid) {
                    matchfile_pushback_match(mf);
                    break;
                }
                fieldnum = mo->fieldnum;
                if (fieldnum < firstfield)
                    continue;
                if (fieldnum > lastfield)
                    continue;

                nread++;

                rd = rdlist_read_field_num(rdls, fieldnum, NULL);
                if (!rd) {
                    fprintf(stderr, "Failed to read RDLS entries for field %i.\n", fieldnum);
                    exit(-1);
                }
                nrd = rd_n(rd);
                if (Ncenter)
                    nrd = MIN(nrd, Ncenter);

                r2 = square(mo->radius);
                arc = deg2arcmin(mo->radius_deg);
				xyzarr2radec(mo->center, &rac, &decc);

                for (k=0; k<nrd; k++) {
                    double xyz[3];
                    double ra, dec;
                    ra  = rd_getra (rd, k);
                    dec = rd_getdec(rd, k);
                    radecdeg2xyzarr(ra, dec, xyz);
                    if (distsq_exceeds(xyz, mo->center, 3, r2 * 1.2)) {
                        printf("\nError: Field %i: match says center (%g, %g), scale %g arcmin, but\n",
                               fieldnum, rac, decc, arc);
                        printf("rdls %i is (%g, %g).\n", k, ra, dec);
                        printf("Logprob %g (%g).\n", mo->logodds, exp(mo->logodds));
                        err = TRUE;
                        break;
                    }
                }
                rd_free(rd);

                if (err) {
                    incorrect++;
                    if (falsematch) {
                        if (matchfile_write_match(falsematch, mo)) {
                            fprintf(stderr, "Failed to write match to %s\n", falsematchfn);
                            exit(-1);
                        }
                    }
                } else {
                    printf("Field %5i: correct hit: (%8.3f, %8.3f), scale %6.3f arcmin, logodds %g (%g)\n",
                           fieldnum, rac, decc, arc, mo->logodds, exp(mo->logodds));
                    correct++;
                    if (truematch) {
                        if (matchfile_write_match(truematch, mo)) {
                            fprintf(stderr, "Failed to write match to %s\n", truematchfn);
                            exit(-1);
                        }
                    }
                }
                fflush(stdout);

                if (tpsolved && !err)
                    solvedfile_set(tpsolved, nfields_total);
                if (fpsolved && err)
                    solvedfile_set(fpsolved, nfields_total);

                nfields_total++;
            }

            rdlist_close(rdls);

            printf("Read %i from %s for fileid %i\n", nread, inputfiles[i], fileid);
        }
    }

    printf("\n");
    printf("Read a total of %i correct and %i incorrect matches.\n", correct, incorrect);

    for (i=0; i<ninputfiles; i++) {
        matchfile_close(matchfiles[i]);
    }
    free(matchfiles);
    free(mfcursors);

    if (tpsolved)
        solvedfile_setsize(tpsolved, nfields_total);
    if (fpsolved)
        solvedfile_setsize(fpsolved, nfields_total);

    if (truematch) {
        if (matchfile_fix_headers(truematch) ||
            matchfile_close(truematch)) {
            fprintf(stderr, "Failed to fix header for %s\n", truematchfn);
            exit(-1);
        }
    }
    if (falsematch) {
        if (matchfile_fix_headers(falsematch) ||
            matchfile_close(falsematch)) {
            fprintf(stderr, "Failed to fix header for %s\n", falsematchfn);
            exit(-1);
        }
    }

    return 0;
}