pl* matchfile_get_matches_for_field(matchfile* mf, int field) {
	pl* list = pl_new(256);
	for (;;) {
		MatchObj* mo = matchfile_read_match(mf);
		MatchObj* copy;
		if (!mo) break;
		if (mo->fieldnum != field) {
			// push back the newly-read entry...
			matchfile_pushback_match(mf);
			break;
		}
		copy = malloc(sizeof(MatchObj));
		memcpy(copy, mo, sizeof(MatchObj));
		pl_append(list, copy);
	}
	return list;
}
Exemple #2
0
int main(int argc, char *argv[]) {
    int argchar;
	char* progname = argv[0];
	char** inputfiles = NULL;
	int ninputfiles = 0;
	int i;
	int firstfield=0, lastfield=INT_MAX-1;
	int firstfieldfile=1, lastfieldfile=INT_MAX-1;
	matchfile** mfs;
	MatchObj** mos;
	anbool* eofs;
	anbool* eofieldfile;
	int nread = 0;
	int f;
	int fieldfile;
	int totalsolved, totalunsolved;
	int mode = MODE_BEST;
	double logodds_tosolve = -HUGE_VAL;
	anbool agree = FALSE;

	MatchObj* bestmo;
	bl* keepers;
	bl* leftovers = NULL;

    while ((argchar = getopt (argc, argv, OPTIONS)) != -1) {
		switch (argchar) {
		case 'S':
			solvedfile = optarg;
			break;
		case 's':
			solvedserver = optarg;
			break;
		case 'F':
			mode = MODE_FIRST;
			break;
		case 'a':
			mode = MODE_ALL;
			break;
		case 'r':
			ratio_tosolve = atof(optarg);
			logodds_tosolve = log(ratio_tosolve);
			break;
		case 'f':
			ninfield_tosolve = atoi(optarg);
			break;
		case 'M':
			agreefname = optarg;
			break;
		case 'L':
			leftoverfname = optarg;
			break;
		case 'I':
			firstfieldfile = atoi(optarg);
			break;
		case 'J':
			lastfieldfile = atoi(optarg);
			break;
		case 'A':
			firstfield = atoi(optarg);
			break;
		case 'B':
			lastfield = atoi(optarg);
			break;
		case 'h':
		default:
			printHelp(progname);
			exit(-1);
		}
	}
	if (optind < argc) {
		ninputfiles = argc - optind;
		inputfiles = argv + optind;
	} else {
		printHelp(progname);
		exit(-1);
	}

	if (lastfield < firstfield) {
		fprintf(stderr, "Last field (-B) must be at least as big as first field (-A)\n");
		exit(-1);
	}

	if (solvedserver)
		if (solvedclient_set_server(solvedserver)) {
			fprintf(stderr, "Failed to set solved server.\n");
			exit(-1);
		}

	if (leftoverfname) {
		leftovermf = matchfile_open_for_writing(leftoverfname);
		if (!leftovermf) {
			fprintf(stderr, "Failed to open file %s to write leftover matches.\n", leftoverfname);
			exit(-1);
		}
		BOILERPLATE_ADD_FITS_HEADERS(leftovermf->header);
		qfits_header_add(leftovermf->header, "HISTORY", "This file was created by the program \"agreeable\".", NULL, NULL);
		if (matchfile_write_headers(leftovermf)) {
			fprintf(stderr, "Failed to write leftovers matchfile header.\n");
			exit(-1);
		}
		leftovers = bl_new(256, sizeof(MatchObj));
	}
	if (agreefname) {
		agreemf = matchfile_open_for_writing(agreefname);
		if (!agreemf) {
			fprintf(stderr, "Failed to open file %s to write agreeing matches.\n", agreefname);
			exit(-1);
		}
		BOILERPLATE_ADD_FITS_HEADERS(agreemf->header);
		qfits_header_add(agreemf->header, "HISTORY", "This file was created by the program \"agreeable\".", NULL, NULL);
		if (matchfile_write_headers(agreemf)) {
			fprintf(stderr, "Failed to write agreeing matchfile header.\n");
			exit(-1);
		}
		agree = TRUE;
	}

	solved = il_new(256);
	unsolved = il_new(256);

	keepers = bl_new(256, sizeof(MatchObj));

	totalsolved = totalunsolved = 0;

	mos =  calloc(ninputfiles, sizeof(MatchObj*));
	eofs = calloc(ninputfiles, sizeof(anbool));
	eofieldfile = malloc(ninputfiles * sizeof(anbool));
	mfs = malloc(ninputfiles * sizeof(matchfile*));

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

	// we assume the matchfiles are sorted by field id and number.
	for (fieldfile=firstfieldfile; fieldfile<=lastfieldfile; fieldfile++) {
		anbool alldone = TRUE;

		memset(eofieldfile, 0, ninputfiles * sizeof(anbool));

		for (f=firstfield; f<=lastfield; f++) {
			int fieldnum = f;
			anbool donefieldfile;
			anbool solved_it;
			bl* writematches = NULL;

			// quit if we've reached the end of all the input files.
			alldone = TRUE;
			for (i=0; i<ninputfiles; i++)
				if (!eofs[i]) {
					alldone = FALSE;
					break;
				}
			if (alldone)
				break;

			// move on to the next fieldfile if all the input files have been
			// exhausted.
			donefieldfile = TRUE;
			for (i=0; i<ninputfiles; i++)
				if (!eofieldfile[i] && !eofs[i]) {
					donefieldfile = FALSE;
					break;
				}
			if (donefieldfile)
				break;

			// start a new field.
			fprintf(stderr, "File %i, Field %i.\n", fieldfile, f);
			solved_it = FALSE;
			bestmo = NULL;

			for (i=0; i<ninputfiles; i++) {
				int nr = 0;
				int ns = 0;

				while (1) {
					if (eofs[i])
						break;
					if (!mos[i])
						mos[i] = matchfile_read_match(mfs[i]);
					if (unlikely(!mos[i])) {
						eofs[i] = TRUE;
						break;
					}

					// skip past entries that are out of range...
					if ((mos[i]->fieldfile < firstfieldfile) ||
						(mos[i]->fieldfile > lastfieldfile) ||
						(mos[i]->fieldnum < firstfield) ||
						(mos[i]->fieldnum > lastfield)) {
						mos[i] = NULL;
						ns++;
						continue;
					}

					if (mos[i]->fieldfile > fieldfile)
						eofieldfile[i] = TRUE;

					if (mos[i]->fieldfile != fieldfile)
						break;

					assert(mos[i]->fieldnum >= fieldnum);
					if (mos[i]->fieldnum != fieldnum)
						break;
					nread++;
					if (nread % 10000 == 9999) {
						fprintf(stderr, ".");
						fflush(stderr);
					}

					// if we've already found a solution, skip past the
					// remaining matches in this file...
					if (solved_it && (mode == MODE_FIRST)) {
						ns++;
						mos[i] = NULL;
						continue;
					}

					nr++;

					if ((mos[i]->logodds >= logodds_tosolve)  &&
						(mos[i]->nindex >= ninfield_tosolve)) {
						solved_it = TRUE;
						// (note, we get a pointer to its position in the list)
						mos[i] = bl_append(keepers, mos[i]);
						if (!bestmo || mos[i]->logodds > bestmo->logodds)
							bestmo = mos[i];
					} else if (leftovers) {
						bl_append(leftovers, mos[i]);
					}

					mos[i] = NULL;

				}
				if (nr || ns)
					fprintf(stderr, "File %s: read %i matches, skipped %i matches.\n", inputfiles[i], nr, ns);
			}

			// which matches do we want to write out?
			if (agree) {
				writematches = bl_new(256, sizeof(MatchObj));

				switch (mode) {
				case MODE_BEST:
				case MODE_FIRST:
					if (bestmo)
						bl_append(writematches, bestmo);
					break;
				case MODE_ALL:
					bl_append_list(writematches, keepers);
					break;
				}
			}

			write_field(writematches, leftovers, fieldfile, fieldnum);

			if (agree)
				bl_free(writematches);

			if (leftovers)
				bl_remove_all(leftovers);
			if (keepers)
				bl_remove_all(keepers);

			fprintf(stderr, "This file: %i fields solved, %i unsolved.\n", il_size(solved), il_size(unsolved));
			fprintf(stderr, "Grand total: %i solved, %i unsolved.\n", totalsolved + il_size(solved), totalunsolved + il_size(unsolved));
		}
		totalsolved += il_size(solved);
		totalunsolved += il_size(unsolved);
		
		il_remove_all(solved);
		il_remove_all(unsolved);

		if (alldone)
			break;
	}

	for (i=0; i<ninputfiles; i++)
		matchfile_close(mfs[i]);
	free(mfs);
	free(mos);
	free(eofs);

	fprintf(stderr, "\nRead %i matches.\n", nread);
	fflush(stderr);

	if (keepers)
		bl_free(keepers);
	if (leftovers)
		bl_free(leftovers);

	il_free(solved);
	il_free(unsolved);

	if (leftovermf) {
		matchfile_fix_headers(leftovermf);
		matchfile_close(leftovermf);
	}

	if (agreemf) {
		matchfile_fix_headers(agreemf);
		matchfile_close(agreemf);
	}

	return 0;
}
Exemple #3
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;
}
Exemple #4
0
static int plot_index_overlay(augment_xylist_t* axy, const char* me,
                              const char* indxylsfn, const char* redgreenfn,
							  double plotscale, const char* bgfn) {
    sl* cmdline = sl_new(16);
    char* cmd;
    matchfile* mf;
    MatchObj* mo;
    int i;
    anbool ctrlc;
	char* imgfn;

	assert(axy->matchfn);
    mf = matchfile_open(axy->matchfn);
    if (!mf) {
        ERROR("Failed to read matchfile %s", axy->matchfn);
        return -1;
    }
    // just read the first match...
    mo = matchfile_read_match(mf);
    if (!mo) {
        ERROR("Failed to read a match from matchfile %s", axy->matchfn);
        return -1;
    }

    // sources + index overlay
	imgfn = axy->pnmfn;

	if (bgfn) {
		append_executable(cmdline, "jpegtopnm", me);
		append_escape(cmdline, bgfn);
		sl_append(cmdline, "|");
		imgfn = "-";
	} else {
		if (axy->imagefn && plotscale != 1.0) {
			append_executable(cmdline, "pnmscale", me);
			sl_appendf(cmdline, "%f", plotscale);
			append_escape(cmdline, axy->pnmfn);
			sl_append(cmdline, "|");
			imgfn = "-";
		}
	}

    append_executable(cmdline, "plotxy", me);
    if (imgfn) {
        sl_append(cmdline, "-I");
        append_escape(cmdline, imgfn);
    } else {
		sl_appendf(cmdline, "-W %i -H %i", (int)(plotscale * axy->W), (int)(plotscale * axy->H));
	}
    sl_append(cmdline, "-i");
    append_escape(cmdline, axy->axyfn);
    if (axy->xcol) {
        sl_append(cmdline, "-X");
        append_escape(cmdline, axy->xcol);
    }
    if (axy->ycol) {
        sl_append(cmdline, "-Y");
        append_escape(cmdline, axy->ycol);
    }
	if (plotscale != 1.0) {
		sl_append(cmdline, "-S");
		sl_appendf(cmdline, "%f", plotscale);
	}
    sl_append(cmdline, "-C red -w 2 -r 6 -N 200 -x 1 -y 1");
    sl_append(cmdline, "-P");
    sl_append(cmdline, "|");
    append_executable(cmdline, "plotxy", me);
    sl_append(cmdline, "-i");
    append_escape(cmdline, indxylsfn);
    sl_append(cmdline, "-I - -w 2 -r 4 -C green -x 1 -y 1");
	if (plotscale != 1.0) {
		sl_append(cmdline, "-S");
		sl_appendf(cmdline, "%f", plotscale);
	}

    // if we solved by verifying an existing WCS, there is no quad.
    if (mo->dimquads) {
        sl_append(cmdline, " -P |");
        append_executable(cmdline, "plotquad", me);
        sl_append(cmdline, "-I -");
        sl_append(cmdline, "-C green");
        sl_append(cmdline, "-w 2");
        sl_appendf(cmdline, "-d %i", mo->dimquads);
		if (plotscale != 1.0) {
			sl_append(cmdline, "-s");
			sl_appendf(cmdline, "%f", plotscale);
		}
        for (i=0; i<(2 * mo->dimquads); i++)
            sl_appendf(cmdline, " %g", mo->quadpix[i]);
    }

    matchfile_close(mf);
			
    sl_append(cmdline, ">");
    append_escape(cmdline, redgreenfn);
    
    cmd = sl_implode(cmdline, " ");
    sl_free2(cmdline);
    logverb("Running:\n  %s\n", cmd);
    if (run_command(cmd, &ctrlc)) {
        ERROR("Plotting commands %s; exiting.", (ctrlc ? "were cancelled" : "failed"));
        return -1;
    }
    free(cmd);
    return 0;
}