Пример #1
0
static void dl_prepend_append_test(void) {
    printsln((String)__func__);
    List ac, ex;
    ac = dl_create();
    dl_append(ac, 1);
    dl_append(ac, 2);
    dl_append(ac, 3);
    ex = dl_create();
    dl_prepend(ex, 3);
    dl_prepend(ex, 2);
    dl_prepend(ex, 1);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);
}
Пример #2
0
dl* constellations_get_lines_radec(int c) {
    dl* list;
    const int* lines;
    int i;
    check_const_num(c);
    list = dl_new(16);
    lines = constellation_lines[c];
    for (i=0; i<constellation_nlines[c]*2; i++) {
        int star = lines[i];
        const double* radec = star_positions + star*2;
        dl_append(list, radec[0]);
        dl_append(list, radec[1]);
    }
    return list;
}
static void addscale(sl* methods, dl* scales,
                     const char* method, double scale) {
    if (methods)
        sl_append(methods, method);
    if (scales)
        dl_append(scales, scale);
}
Пример #4
0
int plotstuff_append_doubles(const char* str, dl* lst) {
	int i;
	sl* strs = sl_split(NULL, str, " ");
	for (i=0; i<sl_size(strs); i++)
		dl_append(lst, atof(sl_get(strs, i)));
	sl_free2(strs);
	return 0;
}
Пример #5
0
List dl_of_il(List list) {
    assert_argument_not_null(list);
    il_assert_element_size(list);
    List result = dl_create();
    for (IntListNode *node = list->first; node != NULL; node = node->next) {
        dl_append(result, node->value);
    }
    return result;
}
Пример #6
0
List dl_map_state(List list, DoubleIntDoubleAnyToDouble f, double x, Any state) {
    assert_function_not_null(f);
    assert_argument_not_null(list);
    dl_assert_element_size(list);
    List result = dl_create();
    int i = 0;
    for (DoubleListNode *node = list->first; node != NULL; node = node->next, i++) {
        dl_append(result, f(node->value, i, x, state));
    }
    return result;
}
Пример #7
0
List dl_fn(int n, IntDoubleToDouble init, double x) {
    assert_function_not_null(init);
    if (n < 0) {
        printf("%s: length cannot be negative (is %d)\n", __func__, n);
        exit(EXIT_FAILURE);
    }
    List result = dl_create();
    for (int i = 0; i < n; i++) {
        dl_append(result, init(i, x));
    }
    return result;
}
Пример #8
0
List dl_repeat(int n, double value) {
    if (n < 0) {
        printf("%s: length cannot be negative (is %d)\n", __func__, n);
        exit(EXIT_FAILURE);
    }
    ListHead *lh = xcalloc(1, sizeof(ListHead));
    lh->s = sizeof(value); // content size
    for (int i = 0; i < n; i++) {
        dl_append(lh, value);
    }
    return lh;
}
Пример #9
0
List dl_filter_state(List list, DoubleIntDoubleAnyToBool predicate, double x, Any state) {
    assert_function_not_null(predicate);
    assert_argument_not_null(list);
    dl_assert_element_size(list);
    List result = dl_create();
    int i = 0;
    for (DoubleListNode *node = list->first; node != NULL; node = node->next, i++) {
        if (predicate(node->value, i, x, state)) {
            dl_append(result, node->value);
        }
    }
    return result;
}
Пример #10
0
List dl_choose(List list, DoubleIntDoubleToDoubleOption f, double x) {
    assert_function_not_null(f);
    assert_argument_not_null(list);
    dl_assert_element_size(list);
    List result = dl_create();
    int i = 0;
    for (DoubleListNode *node = list->first; node != NULL; node = node->next, i++) {
        DoubleOption op = f(node->value, i, x);
        if (!op.none) {
            dl_append(result, op.some);
        }
    }
    return result;
}
Пример #11
0
List dl_range(double a, double b, double step) {
    if (fabs(step) < 0.000001) {
        printf("%s: step too small (is %g)\n", __func__, step);
        exit(EXIT_FAILURE);
    }
    ListHead *list = xcalloc(1, sizeof(ListHead));
    list->s = sizeof(double); // content size
    if (a <= b) {
        if (step < 0) {
            step = -step;
        }
        for (double x = a; x < b; x += step) {
            dl_append(list, x);
        }
    } else { /* a > b */
        if (step > 0) {
            step = -step;
        }
        for (double x = a; x > b; x += step) {
            dl_append(list, x);
        }
    }
    return list;
}
Пример #12
0
List dl_of_string(String s) {
    ListHead *list = xcalloc(1, sizeof(ListHead));
    list->s = sizeof(double); // content size
    char *t = s;
    char *endp;
    while (*t != '\0') {
        if (isdigit(*t)) {
            if (t > s && *(t - 1) == '.') t--; // check for '.'
            if (t > s && *(t - 1) == '-') t--; // check for '-'
            dl_append(list, strtod(t, &endp)); // convert digit string to int
            t = endp;
        } else {
            // assert: *t is not a digit, *t is not '\0'
            t++; // not a digit, skip
        }
    }
    return list;
}
Пример #13
0
void glPushCall(void *call) {
    displaylist_t *active = state.list.active;
    if (active) {
        dl_append(active, call);
    }
}
Пример #14
0
/*
static void walk_callback2(const anwcs_t* wcs, double ix, double iy,
                           double ra, double dec, void* token) {
	struct walk_token2* walk = token;
	dl_append(walk->radecs, ra);
	dl_append(walk->radecs, dec);
}
 */
void test_walk_outline(CuTest* tc) {
	anwcs_t* plotwcs = anwcs_create_allsky_hammer_aitoff(180., 0., 400, 200);
#if 0
    tan_t tanwcs;
    tanwcs.crval[0] = 10.;
    tanwcs.crval[1] =  0.;
    tanwcs.crpix[0] = 25.;
    tanwcs.crpix[1] = 25.;
    tanwcs.cd[0][0] = 1.;
    tanwcs.cd[0][1] = 0.;
    tanwcs.cd[1][0] = 0.;
    tanwcs.cd[1][1] = 1.;
    tanwcs.imagew = 50.;
    tanwcs.imageh = 50.;
    tanwcs.sin = 0;
#endif

	dl* rd;
    /*
     anwcs_t* wcs = anwcs_new_tan(&tanwcs);
     struct walk_token2 token2;
     double stepsize = 1000;
     */

    log_init(LOG_ALL);

    /*
     token2.radecs = dl_new(256);
     anwcs_walk_image_boundary(wcs, stepsize, walk_callback2, &token2);
     rd = token2.radecs;
     logverb("Outline: walked in %i steps\n", dl_size(rd)/2);
     // close
     dl_append(rd, dl_get(rd, 0));
     dl_append(rd, dl_get(rd, 1));
     int i;
     for (i=0; i<dl_size(rd)/2; i++) {
     double ra,dec;
     ra  = dl_get(rd, 2*i+0);
     dec = dl_get(rd, 2*i+1);
     printf("outline step %i: (%.2f, %.2f)\n", i, ra, dec);
     }
     */

    rd = dl_new(256);
    dl_append(rd, 10.);
    dl_append(rd,  0.);

    dl_append(rd, 350.);
    dl_append(rd,  0.);

    dl_append(rd, 350.);
    dl_append(rd,  10.);

    dl_append(rd,  10.);
    dl_append(rd,  10.);

    dl_append(rd,  10.);
    dl_append(rd,   0.);

    pl* lists = anwcs_walk_outline(plotwcs, rd, 100);

    printf("\n\n\n");

    printf("%zu lists\n", pl_size(lists));
    int i;
    for (i=0; i<pl_size(lists); i++) {
        dl* plotlist = pl_get(lists, i);
        printf("List %i: length %zu\n", i, dl_size(plotlist));
        int j;
        for (j=0; j<dl_size(plotlist)/2; j++) {
            printf("  %.2f, %.2f\n",
                   dl_get(plotlist, j*2+0),
                   dl_get(plotlist, j*2+1));
        }
        printf("\n");
    }

}
Пример #15
0
void plot_xy_vals(plotxy_t* args, double x, double y) {
	dl_append(args->xyvals, x);
	dl_append(args->xyvals, y);
}
Пример #16
0
void verify_get_all_matches(const double* refxys, int NR,
							const double* testxys, const double* testsigma2s, int NT,
							double effective_area,
							double distractors,
							double nsigma,
							double limit,
							il*** p_reflist,
							dl*** p_problist) {
	double* refcopy;
	kdtree_t* rtree;
	int Nleaf = 10;
	int i,j;
	double logd;
	double logbg;
	double loglimit;

	il** reflist;
	dl** problist;

	reflist  = calloc(NT, sizeof(il*));
	problist = calloc(NT, sizeof(dl*));

	// Build a tree out of the index stars in pixel space...
	// kdtree scrambles the data array so make a copy first.
	refcopy = malloc(2 * NR * sizeof(double));
	memcpy(refcopy, refxys, 2 * NR * sizeof(double));
	rtree = kdtree_build(NULL, refcopy, NR, 2, Nleaf, KDTT_DOUBLE, KD_BUILD_SPLIT);

	logbg = log(1.0 / effective_area);
	logd  = log(distractors / effective_area);
	loglimit = log(distractors / effective_area * limit);

	for (i=0; i<NT; i++) {
		const double* testxy;
		double sig2;
		kdtree_qres_t* res;

		testxy = testxys + 2*i;
		sig2 = testsigma2s[i];

		logverb("\n");
		logverb("test star %i: (%.1f,%.1f), sigma: %.1f\n", i, testxy[0], testxy[1], sqrt(sig2));

		// find all ref stars within nsigma.
		res = kdtree_rangesearch_options(rtree, testxy, sig2*nsigma*nsigma,
										 KD_OPTIONS_SORT_DISTS | KD_OPTIONS_SMALL_RADIUS);

		if (res->nres == 0) {
			kdtree_free_query(res);
			continue;
		}

		reflist[i] = il_new(4);
		problist[i] = dl_new(4);

		for (j=0; j<res->nres; j++) {
			double d2;
			int refi;
			double loggmax, logfg;

			d2 = res->sdists[j];
			refi = res->inds[j];

			// peak value of the Gaussian
			loggmax = log((1.0 - distractors) / (2.0 * M_PI * sig2 * NR));
			// value of the Gaussian
			logfg = loggmax - d2 / (2.0 * sig2);

			if (logfg < loglimit)
				continue;

			logverb("  ref star %i, dist %.2f, sigmas: %.3f, logfg: %.1f (%.1f above distractor, %.1f above bg, %.1f above keep-limit)\n",
					refi, sqrt(d2), sqrt(d2 / sig2), logfg, logfg - logd, logfg - logbg, logfg - loglimit);

			il_append(reflist[i], refi);
			dl_append(problist[i], logfg);
		}

		kdtree_free_query(res);
	}
	kdtree_free(rtree);
	free(refcopy);

	*p_reflist  = reflist;
	*p_problist = problist;
}
Пример #17
0
void plot_radec_vals(plotradec_t* args, double ra, double dec) {
	dl_append(args->radecvals, ra);
	dl_append(args->radecvals, dec);
}
Пример #18
0
static sip_t* run_test(CuTest* tc, sip_t* sip, int N, double* xy, double* radec) {
    int i;
    starxy_t* sxy;
    tweak_t* t;
    sip_t* outsip;
    il* imcorr;
    il* refcorr;
    dl* weights;
    tan_t* tan = &(sip->wcstan);

    printf("Input SIP:\n");
    sip_print_to(sip, stdout);
    fflush(NULL);

    sxy = starxy_new(N, FALSE, FALSE);
    starxy_set_xy_array(sxy, xy);

    imcorr = il_new(256);
    refcorr = il_new(256);
    weights = dl_new(256);
    for (i=0; i<N; i++) {
        il_append(imcorr, i);
        il_append(refcorr, i);
        dl_append(weights, 1.0);
    }

    t = tweak_new();
    tweak_push_wcs_tan(t, tan);

    outsip = t->sip;
    outsip->a_order = outsip->b_order = sip->a_order;
    outsip->ap_order = outsip->bp_order = sip->ap_order;

    t->weighted_fit = TRUE;
    tweak_push_ref_ad_array(t, radec, N);
    tweak_push_image_xy(t, sxy);
    tweak_push_correspondence_indices(t, imcorr, refcorr, NULL, weights);
    tweak_skip_shift(t);

    // push correspondences
    // push image xy
    // push ref ra,dec
    // push ref xy (tan)
    // push tan

    tweak_go_to(t, TWEAK_HAS_LINEAR_CD);

    printf("Output SIP:\n");
    sip_print_to(outsip, stdout);

    CuAssertDblEquals(tc, tan->imagew, outsip->wcstan.imagew, 1e-10);
    CuAssertDblEquals(tc, tan->imageh, outsip->wcstan.imageh, 1e-10);

    // should be exactly equal.
    CuAssertDblEquals(tc, tan->crpix[0], outsip->wcstan.crpix[0], 1e-10);
    CuAssertDblEquals(tc, tan->crpix[1], outsip->wcstan.crpix[1], 1e-10);

    t->sip = NULL;
    tweak_free(t);
    starxy_free(sxy);
    return outsip;
}
Пример #19
0
int main(int argc, char** args) {
	int loglvl = LOG_MSG;
	char** myargs;
	int nargs;
	int c;

	char* wcsinfn = NULL;
	char* wcsoutfn = NULL;
	int ext = 0;
	anbool scamp = FALSE;
	double xlo = 1;
	double xhi = 1000;
	double xstep = 0;
	double ylo = 1;
	double yhi = 1000;
	double ystep = 0;
	anbool forcetan = FALSE;
	dl* xylst;
	double x,y;
	double* xy;
	int Nxy;
	int W, H;

	W = H = 0;

    while ((c = getopt(argc, args, OPTIONS)) != -1) {
        switch (c) {
		case 't':
			forcetan = TRUE;
			break;
		case 'W':
			W = atoi(optarg);
			break;
		case 'H':
			H = atoi(optarg);
			break;
		case 'x':
			xlo = atof(optarg);
			break;
		case 'X':
			xhi = atof(optarg);
			break;
		case 'a':
			xstep = atof(optarg);
			break;
		case 'y':
			ylo = atof(optarg);
			break;
		case 'Y':
			yhi = atof(optarg);
			break;
		case 'b':
			ystep = atof(optarg);
			break;
		case 's':
			scamp = TRUE;
			break;
		case 'e':
			ext = atoi(optarg);
			break;
		case 'v':
			loglvl++;
			break;
		case '?':
		case 'h':
			print_help(args[0]);
			exit(0);
		}
	}
	nargs = argc - optind;
	myargs = args + optind;

	if (nargs != 2) {
		print_help(args[0]);
		exit(-1);
	}
	wcsinfn = myargs[0];
	wcsoutfn = myargs[1];

	log_init(loglvl);
	fits_use_error_system();

	logmsg("Reading WCS (with PV distortions) from %s, ext %i\n", wcsinfn, ext);
	logmsg("Writing WCS (with SIP distortions) to %s\n", wcsoutfn);

	assert(xhi >= xlo);
	assert(yhi >= ylo);
	if (xstep == 0) {
		int nsteps = MAX(1, round((xhi - xlo)/100.0));
		xstep = (xhi - xlo) / (double)nsteps;
	}
	if (ystep == 0) {
		int nsteps = MAX(1, round((yhi - ylo)/100.0));
		ystep = (yhi - ylo) / (double)nsteps;
	}
	logverb("Stepping from x = %g to %g, steps of %g\n", xlo, xhi, xstep);
	logverb("Stepping from y = %g to %g, steps of %g\n", ylo, yhi, ystep);

	xylst = dl_new(256);
	for (y=ylo; y<=(yhi+0.001); y+=ystep) {
		for (x=xlo; x<=(xhi+0.001); x+=xstep) {
			dl_append(xylst, x);
			dl_append(xylst, y);
		}
	}
	Nxy = dl_size(xylst)/2;
	xy = dl_to_array(xylst);
	dl_free(xylst);

	if (wcs_pv2sip(wcsinfn, ext, wcsoutfn, scamp, xy, Nxy, W, H,
				   forcetan)) {
		exit(-1);
	}

	free(xy);

	return 0;
}
Пример #20
0
int main(int argc, char** args) {
    int argchar;
    kdtree_t* kd;
    int Nleaf = 25;
    char* infn = NULL;
    char* outfn = NULL;
    char* tychofn = NULL;
    char* crossfn = NULL;
	char* progname = args[0];
    FILE* f;

    tycstar_t* tycstars = NULL;
    int Ntyc = 0;

	int exttype  = KDT_EXT_DOUBLE;
	int datatype = KDT_DATA_U32;
	int treetype = KDT_TREE_U32;
	int tt;
	int buildopts = 0;
	int i, N, D;

    dl* ras;
    dl* decs;
    dl* hds;

    fl* mag1s;
    fl* mag2s;
    fl* mag3s;

    int nbad = 0;
    int nox = 0;

    int* hd;
    double* xyz;

    qfits_header* hdr;

    while ((argchar = getopt (argc, args, OPTIONS)) != -1)
        switch (argchar) {
        case 'T':
            tychofn = optarg;
            break;
        case 'X':
            crossfn = optarg;
            break;
        case 'R':
            Nleaf = (int)strtoul(optarg, NULL, 0);
            break;
		case 't':
			treetype = kdtree_kdtype_parse_tree_string(optarg);
			break;
		case 'd':
			datatype = kdtree_kdtype_parse_data_string(optarg);
			break;
		case 'b':
			buildopts |= KD_BUILD_BBOX;
			break;
		case 's':
			buildopts |= KD_BUILD_SPLIT;
			break;
		case 'S':
			buildopts |= KD_BUILD_SPLITDIM;
			break;
        case '?':
            fprintf(stderr, "Unknown option `-%c'.\n", optopt);
        case 'h':
			printHelp(progname);
            return 0;
        default:
            return -1;
        }

    if (optind != argc - 2) {
        printHelp(progname);
        exit(-1);
    }

    infn = args[optind];
    outfn = args[optind+1];

	if (!(buildopts & (KD_BUILD_BBOX | KD_BUILD_SPLIT))) {
		printf("You need bounding-boxes or splitting planes!\n");
		printHelp(progname);
		exit(-1);
	}

    if (tychofn || crossfn) {
        if (!(tychofn && crossfn)) {
            printf("You need both -T <Tycho2> and -X <Crossref> to do cross-referencing.\n");
            exit(-1);
        }
    }

    if (tychofn) {
        int i, N;
        tycho2_fits* tyc;
        FILE* f;
        int nx, nox;
		int lastgrass = 0;

        tyc = tycho2_fits_open(tychofn);
        if (!tyc) {
            ERROR("Failed to open Tycho-2 catalog.");
            exit(-1);
        }
        printf("Reading Tycho-2 catalog...\n");

        N = tycho2_fits_count_entries(tyc);
        tycstars = calloc(N, sizeof(tycstar_t));

        for (i=0; i<N; i++) {
            tycho2_entry* te;
			int grass = (i*80 / N);
			if (grass != lastgrass) {
				printf(".");
				fflush(stdout);
				lastgrass = grass;
			}
			te = tycho2_fits_read_entry(tyc);
            tycstars[i].tyc1 = te->tyc1;
            tycstars[i].tyc2 = te->tyc2;
            tycstars[i].tyc3 = te->tyc3;
            tycstars[i].ra   = te->ra;
            tycstars[i].dec  = te->dec;
            tycstars[i].mag_BT = te->mag_BT;
            tycstars[i].mag_VT = te->mag_VT;
            tycstars[i].mag_HP = te->mag_HP;
        }
        tycho2_fits_close(tyc);

        printf("Sorting...\n");
        qsort(tycstars, N, sizeof(tycstar_t), compare_tycs);
        Ntyc = N;

        f = fopen(crossfn, "rb");
        if (!f) {
            SYSERROR("Failed to open cross-reference file %s", crossfn);
            exit(-1);
        }

        nx = 0;
        nox = 0;
        while (TRUE) {
            char buf[1024];
            int tyc1, tyc2, tyc3, hd, nhd, ntyc;
            char ftyc, sptype0, sptype1, sptype2;
            tycstar_t* s;

            if (!fgets(buf, sizeof(buf), f)) {
                if (ferror(f)) {
                    SYSERROR("Failed to read a line of text from the cross-reference file");
                    exit(-1);
                }
                break;
            }

            if (sscanf(buf, " %d %d %d%c %d %c%c%c %d %d",
                       &tyc1, &tyc2, &tyc3, &ftyc, &hd,
                       &sptype0, &sptype1, &sptype2, &nhd, &ntyc) != 10) {
                ERROR("Failed to parse line: \"%s\"", buf);
            }

            //printf("%i %i %i %i %i %i\n", tyc1, tyc2, tyc3, hd, nhd, ntyc);
            s = find_tycho(tycstars, Ntyc, tyc1, tyc2, tyc3);
            if (!s) {
                ERROR("Failed to find Tycho-2 star %i-%i-%i", tyc1, tyc2, tyc3);
                nox++;
            } else {
                s->hd = hd;
                s->ntyc = ntyc;
            }
            nx++;
        }
        fclose(f);

        printf("Read %i cross-references.\n", nx);
        printf("Failed to find %i cross-referenced Tycho-2 stars.\n", nox);

        printf("Sorting...\n");
        qsort(tycstars, N, sizeof(tycstar_t), compare_hds);
    }

    f = fopen(infn, "rb");
    if (!f) {
        SYSERROR("Failed to open input file %s", infn);
        exit(-1);
    }

    ras = dl_new(1024);
    decs = dl_new(1024);
    hds = il_new(1024);

    mag1s = fl_new(1024);
    mag2s = fl_new(1024);
    mag3s = fl_new(1024);

    printf("Reading HD catalog...\n");
    for (;;) {
        char buf[1024];
        double ra, dec;
        int hd;
        float mag1, mag2, mag3;

        mag1 = mag2 = mag3 = 0.0;

        if (!fgets(buf, sizeof(buf), f)) {
            if (ferror(f)) {
                SYSERROR("Failed to read a line of text from the input file");
                exit(-1);
            }
            break;
        }

        if (buf[0] == '#')
            continue;
        if (buf[0] == '\n')
            continue;

        if (sscanf(buf, " %lf| %lf| %d", &ra, &dec, &hd) < 3) {
            // ignore three invalid lines
            if (nbad > 3) {
                ERROR("Failed to parse line: \"%s\"", buf);
            }
            nbad++;
        } else {

            if (tycstars) {
                tycstar_t* s = find_hd(tycstars, Ntyc, hd);
                if (!s) {
                    //printf("Failed to find cross-ref for HD %i\n", hd);
                    nox++;
                } else {
                    ra = s->ra;
                    dec = s->dec;

                    mag1 = s->mag_VT;
                    mag2 = s->mag_BT;
                    mag3 = s->mag_HP;
                }
            }

            dl_append(ras, ra);
            dl_append(decs, dec);
            il_append(hds, hd);
            fl_append(mag1s, mag1);
            fl_append(mag2s, mag2);
            fl_append(mag3s, mag3);
        }
    }
    fclose(f);

    N = dl_size(ras);
    printf("Read %i entries and %i bad lines.\n", N, nbad);

    if (dl_size(ras) != HD_NENTRIES) {
        printf("WARNING: expected %i Henry Draper catalog entries.\n", HD_NENTRIES);
    }

    if (nox) {
        printf("Found %i HD entries with no cross-reference (expect this to be about 1%%)\n", nox);
    }

    hd = malloc(sizeof(int) * N);
    il_copy(hds, 0, N, hd);
    il_free(hds);
    for (i=0; i<N; i++)
        if (hd[i] != i+1) {
            printf("Line %i is HD %i\n", i+1, hd[i]);
            break;
        }
    // HACK  - don't allocate 'em in the first place...
    free(hd);

    xyz = malloc(sizeof(double) * 3 * N);
    for (i=0; i<N; i++) {
        radecdeg2xyzarr(dl_get(ras, i), dl_get(decs, i), xyz + 3*i);
    }

    dl_free(ras);
    dl_free(decs);

	tt = kdtree_kdtypes_to_treetype(exttype, treetype, datatype);
	D = 3;
	{
		// limits of the kdtree...
        double lo[] = {-1.0, -1.0, -1.0};
        double hi[] = { 1.0,  1.0,  1.0};
        kd = kdtree_new(N, D, Nleaf);
        kdtree_set_limits(kd, lo, hi);
	}
	printf("Building tree...\n");
	kd = kdtree_build(kd, xyz, N, D, Nleaf, tt, buildopts);

    hdr = qfits_header_default();
    qfits_header_add(hdr, "AN_FILE", "HDTREE", "Henry Draper catalog kdtree", NULL);
    BOILERPLATE_ADD_FITS_HEADERS(hdr);
    fits_add_long_history(hdr, "This file was created by the following command-line:");
    fits_add_args(hdr, args, argc);

    if (kdtree_fits_write(kd, outfn, hdr)) {
        ERROR("Failed to write kdtree");
        exit(-1);
    }

    // Write mags as tag-along table.
    {
        fitstable_t* tag;
        tag = fitstable_open_for_appending(outfn);
        if (!tag) {
            ERROR("Failed to open kd-tree file for appending");
            exit(-1);
        }

        fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_VT", "");
        fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_BT", "");
        fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_HP", "");

        if (fitstable_write_header(tag)) {
            ERROR("Failed to write tag-along header");
            exit(-1);
        }

        for (i=0; i<N; i++) {
            fitstable_write_row(tag, fl_get(mag1s, i), fl_get(mag2s, i),
                                fl_get(mag3s, i));
        }
        if (fitstable_fix_header(tag)) {
            ERROR("Failed to fix tag-along header");
            exit(-1);
        }
        if (fitstable_close(tag)) {
            ERROR("Failed to close tag-along data");
            exit(-1);
        }
    }
    fl_free(mag1s);
    fl_free(mag2s);
    fl_free(mag3s);

    printf("Done.\n");

	qfits_header_destroy(hdr);
    free(xyz);
    kdtree_free(kd);
    free(tycstars);

    return 0;
}
Пример #21
0
int main(int argc, char** args) {
	int c;
	dl* xys = dl_new(16);
	dl* radecs = dl_new(16);
	dl* otherradecs = dl_new(16);

	double* xy;
	double* xyz;
	int i, N;
	tan_t tan, tan2, tan3;
	int W=0, H=0;
	double crpix[] = { HUGE_VAL, HUGE_VAL };
	int loglvl = LOG_MSG;
	FILE* logstream = stderr;
	int order = 1;

    while ((c = getopt(argc, args, OPTIONS)) != -1) {
        switch (c) {
		case 'v':
			loglvl++;
			break;
		case 'h':
			exit(0);
		case 'o':
			order = atoi(optarg);
			break;
		case 'W':
			W = atoi(optarg);
			break;
		case 'H':
			H = atoi(optarg);
			break;
		case 'X':
			crpix[0] = atof(optarg);
			break;
		case 'Y':
			crpix[1] = atof(optarg);
			break;
		}
	}
	if (optind != argc) {
		exit(-1);
	}
	log_init(loglvl);
	log_to(logstream);
	errors_log_to(logstream);

	if (W == 0 || H == 0) {
		logerr("Need -W, -H\n");
		exit(-1);
	}
	if (crpix[0] == HUGE_VAL)
		crpix[0] = W/2.0;
	if (crpix[1] == HUGE_VAL)
		crpix[1] = H/2.0;

	while (1) {
		double x,y,ra,dec;
		if (fscanf(stdin, "%lf %lf %lf %lf\n", &x, &y, &ra, &dec) < 4)
			break;
		if (x == -1 && y == -1) {
			dl_append(otherradecs, ra);
			dl_append(otherradecs, dec);
		} else {
			dl_append(xys, x);
			dl_append(xys, y);
			dl_append(radecs, ra);
			dl_append(radecs, dec);
		}
	}
	logmsg("Read %i x,y,ra,dec tuples\n", dl_size(xys)/2);

	N = dl_size(xys)/2;
	xy = dl_to_array(xys);
	xyz = malloc(3 * N * sizeof(double));
	for (i=0; i<N; i++)
		radecdeg2xyzarr(dl_get(radecs, 2*i), dl_get(radecs, 2*i+1), xyz + i*3);
	dl_free(xys);
	dl_free(radecs);

	fit_tan_wcs(xyz, xy, N, &tan, NULL);
	tan.imagew = W;
	tan.imageh = H;

	logmsg("Computed TAN WCS:\n");
	tan_print_to(&tan, logstream);

	sip_t* sip;
	{
		tweak_t* t = tweak_new();
		starxy_t* sxy = starxy_new(N, FALSE, FALSE);
		il* imginds = il_new(256);
		il* refinds = il_new(256);

		for (i=0; i<N; i++) {
			starxy_set_x(sxy, i, xy[2*i+0]);
			starxy_set_y(sxy, i, xy[2*i+1]);
		}
		tweak_init(t);
		tweak_push_ref_xyz(t, xyz, N);
		tweak_push_image_xy(t, sxy);
		for (i=0; i<N; i++) {
			il_append(imginds, i);
			il_append(refinds, i);
		}
		// unweighted; no dist2s
		tweak_push_correspondence_indices(t, imginds, refinds, NULL, NULL);

		tweak_push_wcs_tan(t, &tan);
		t->sip->a_order = t->sip->b_order = t->sip->ap_order = t->sip->bp_order = order;

		for (i=0; i<10; i++) {
			// go to TWEAK_HAS_LINEAR_CD -> do_sip_tweak
			// t->image has the indices of corresponding image stars
			// t->ref   has the indices of corresponding catalog stars
			tweak_go_to(t, TWEAK_HAS_LINEAR_CD);
			logmsg("\n");
			sip_print(t->sip);
			t->state &= ~TWEAK_HAS_LINEAR_CD;
		}
		tan_write_to_file(&t->sip->wcstan, "kt1.wcs");
		sip = t->sip;
	}

	for (i=0; i<dl_size(otherradecs)/2; i++) {
		double ra, dec, x,y;
		ra = dl_get(otherradecs, 2*i);
		dec = dl_get(otherradecs, 2*i+1);
		if (!sip_radec2pixelxy(sip, ra, dec, &x, &y)) {
			logerr("Not in tangent plane: %g,%g\n", ra, dec);
			exit(-1);
			//continue;
		}
		printf("%g %g\n", x, y);
	}

	/*
	 blind_wcs_move_tangent_point(xyz, xy, N, crpix, &tan, &tan2);
	 blind_wcs_move_tangent_point(xyz, xy, N, crpix, &tan2, &tan3);
	 logmsg("Moved tangent point to (%g,%g):\n", crpix[0], crpix[1]);
	 tan_print_to(&tan3, logstream);
	 tan_write_to_file(&tan, "kt1.wcs");
	 tan_write_to_file(&tan3, "kt2.wcs");
	 */

	dl_free(otherradecs);
	free(xy);
	free(xyz);
	return 0;
}
Пример #22
0
static void dl_of_string_test(void) {
    printsln((String)__func__);
    List ac, ex;

    ac = dl_of_string("1, 2, 3, 4, 5, 6");
    ex = dl_range(1, 7, 1);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string("   1, 2, 3, 4, 5, 6   ");
    ex = dl_range(1, 7, 1);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string("1xx2asdfs3");
    ex = dl_range(1, 4, 1);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string("y1xx2asdfs3x");
    ex = dl_range(1, 4, 1);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string("-3, -2, -1, 0, 1");
    ex = dl_range(-3, 2, 1);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string(" -3, -2, -1 ");
    ex = dl_range(-3, 0, 1);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string(".1 -20. -3.5 1e5 -1.2e4 -1.2e-4");
    ex = dl_create();
    dl_append(ex, 0.1);
    dl_append(ex, -20.0);
    dl_append(ex, -3.5);
    dl_append(ex, 1e5);
    dl_append(ex, -1.2e4);
    dl_append(ex, -1.2e-4);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string("-.1.-2.-.3.-");
    ex = dl_create();
    dl_append(ex, -0.1);
    dl_append(ex, -2.0);
    dl_append(ex, -0.3);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string("---.1.----2.----.3.----");
    ex = dl_create();
    dl_append(ex, -0.1);
    dl_append(ex, -2.0);
    dl_append(ex, -0.3);
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);

    ac = dl_of_string("");
    ex = dl_create();
    dl_check_within(ac, ex);
    l_free(ac);
    l_free(ex);
}
Пример #23
0
static void callback_dualtree(void* v, int ind1, int ind2, double dist2) {
    struct dualtree_results* dtresults = v;
    il_append(dtresults->inds1, ind1);
    il_append(dtresults->inds2, ind2);
    dl_append(dtresults->dists, sqrt(dist2));
}
Пример #24
0
/*
 * NAME:	showfiles()
 * DESCRIPTION:	display a set of files
 */
static
int showfiles(darray *files, int flags, int options, int width)
{
  dlist list;
  int i, sz, result = 0;
  queueent *ents;
  dstring str;
  char **strs;
  void (*show)(int, queueent *, char **, int, int, int);

  if (dl_init(&list) == -1)
    {
      fprintf(stderr, "%s: not enough memory\n", argv0);
      return -1;
    }

  sz   = darr_size(files);
  ents = darr_array(files);

  dstr_init(&str);

  for (i = 0; i < sz; ++i)
    {
      if (outpath(&str, &ents[i], flags) == -1 ||
	  dl_append(&list, dstr_string(&str)) == -1)
	{
	  result = -1;
	  break;
	}
    }

  dstr_free(&str);

  strs = dl_array(&list);

  switch (options & F_MASK)
    {
    case F_LONG:
      show = show_long;
      break;

    case F_ONE:
      show = show_one;
      break;

    case F_MANY:
      show = show_many;
      break;

    case F_HORIZ:
      show = show_horiz;
      break;

    case F_COMMAS:
      show = show_commas;
      break;

    default:
      abort();
    }

  show(sz, ents, strs, flags, options, width);

  dl_free(&list);

  return result;
}