static void dl_repeat_test(void) { printsln((String)__func__); List list; list = dl_repeat(3, 0); check_within_d(dl_get(list, 0), 0, EPSILON); check_within_d(dl_get(list, 1), 0, EPSILON); check_within_d(dl_get(list, 2), 0, EPSILON); l_free(list); list = dl_repeat(2, -1); check_within_d(dl_get(list, 0), -1, EPSILON); check_within_d(dl_get(list, 1), -1, EPSILON); l_free(list); list = dl_repeat(0, 2); check_expect_i(l_length(list), 0); l_free(list); }
void test_dl_push(CuTest* tc) { dl* bl; bl = dl_new(2); CuAssertIntEquals(tc, il_size(bl), 0); dl_push(bl, 42.0); dl_push(bl, 43.0); dl_push(bl, 47.0); dl_push(bl, 49.0); dl_set(bl, 0, 0.0); dl_set(bl, 1, 1.0); dl_set(bl, 2, 2.0); CuAssertIntEquals(tc, il_size(bl), 4); CuAssert(tc, "dl", 0.0 == dl_get(bl, 0)); CuAssert(tc, "dl", 1.0 == dl_get(bl, 1)); CuAssert(tc, "dl", 2.0 == dl_get(bl, 2)); CuAssertIntEquals(tc, dl_check_consistency(bl), 0); dl_free(bl); }
int main(int argc, char *argv[]) { char* progname = argv[0]; int argchar; char* infn; sl* methods = NULL; dl* scales = NULL; int i; int loglvl = LOG_MSG; while ((argchar = getopt (argc, argv, OPTIONS)) != -1) switch (argchar) { case '?': case 'h': printHelp(progname); return 0; case 'v': loglvl++; break; default: return -1; } if (optind != (argc - 1)) { printHelp(progname); exit(-1); } infn = argv[optind]; log_init(loglvl); fits_use_error_system(); if (fits_guess_scale(infn, &methods, &scales)) exit(-1); for (i=0; i<sl_size(methods); i++) { printf("scale %s %g\n", sl_get(methods, i), dl_get(scales, i)); } sl_free2(methods); dl_free(scales); return 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; }
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; }
static void plot_constellations(cairo_t* cairo, plot_args_t* pargs, plotann_t* ann) { int i, N; double ra,dec,radius; double xyzf[3]; // Find the field center and radius anwcs_get_radec_center_and_radius(pargs->wcs, &ra, &dec, &radius); logverb("Plotting constellations: field center %g,%g, radius %g\n", ra, dec, radius); radecdeg2xyzarr(ra, dec, xyzf); radius = deg2dist(radius); N = constellations_n(); for (i=0; i<N; i++) { int j, k; // Find the approximate center and radius of this constellation // and see if it overlaps with the field. il* stars = constellations_get_unique_stars(i); double xyzj[3]; double xyzc[3]; double maxr2 = 0; dl* rds; xyzc[0] = xyzc[1] = xyzc[2] = 0.0; xyzj[0] = xyzj[1] = xyzj[2] = 0.0; for (j=0; j<il_size(stars); j++) { constellations_get_star_radec(il_get(stars, j), &ra, &dec); radecdeg2xyzarr(ra, dec, xyzj); for (k=0; k<3; k++) xyzc[k] += xyzj[k]; } normalize_3(xyzc); for (j=0; j<il_size(stars); j++) { constellations_get_star_radec(il_get(stars, j), &ra, &dec); maxr2 = MAX(maxr2, distsq(xyzc, xyzj, 3)); } il_free(stars); maxr2 = square(sqrt(maxr2) + radius); if (distsq(xyzf, xyzc, 3) > maxr2) { xyzarr2radecdeg(xyzc, &ra, &dec); logverb("Constellation %s (center %g,%g, radius %g) out of bounds\n", constellations_get_shortname(i), ra, dec, dist2deg(sqrt(maxr2) - radius)); logverb(" dist from field center to constellation center is %g deg\n", distsq2deg(distsq(xyzf, xyzc, 3))); logverb(" max radius: %g\n", distsq2deg(maxr2)); continue; } if (ann->constellation_pastel) { float r,g,b; xyzarr2radecdeg(xyzc, &ra, &dec); color_for_radec(ra, dec, &r,&g,&b); plotstuff_set_rgba2(pargs, r,g,b, 0.8); plotstuff_builtin_apply(cairo, pargs); } // Phew, plot it. if (ann->constellation_lines) { rds = constellations_get_lines_radec(i); logverb("Constellation %s: plotting %zu lines\n", constellations_get_shortname(i), dl_size(rds)/4); for (j=0; j<dl_size(rds)/4; j++) { double r1,d1,r2,d2; double r3,d3,r4,d4; double off = ann->constellation_lines_offset; r1 = dl_get(rds, j*4+0); d1 = dl_get(rds, j*4+1); r2 = dl_get(rds, j*4+2); d2 = dl_get(rds, j*4+3); if (anwcs_find_discontinuity(pargs->wcs, r1, d1, r2, d2, &r3, &d3, &r4, &d4)) { logverb("Discontinuous: %g,%g -- %g,%g\n", r1, d1, r2, d2); logverb(" %g,%g == %g,%g\n", r3,d3, r4,d4); plot_offset_line_rd(NULL, pargs, r1,d1,r3,d3, off, 0.); plot_offset_line_rd(NULL, pargs, r4,d4,r2,d2, 0., off); } else { plot_offset_line_rd(NULL, pargs, r1,d1,r2,d2, off, off); } plotstuff_stroke(pargs); } dl_free(rds); } if (ann->constellation_labels || ann->constellation_markers) { // Put the label at the center of mass of the stars that // are in-bounds int Nin = 0; stars = constellations_get_unique_stars(i); xyzc[0] = xyzc[1] = xyzc[2] = 0.0; logverb("Labeling %s: %zu stars\n", constellations_get_shortname(i), il_size(stars)); for (j=0; j<il_size(stars); j++) { constellations_get_star_radec(il_get(stars, j), &ra, &dec); if (!anwcs_radec_is_inside_image(pargs->wcs, ra, dec)) continue; if (ann->constellation_markers) plotstuff_marker_radec(pargs, ra, dec); radecdeg2xyzarr(ra, dec, xyzj); for (k=0; k<3; k++) xyzc[k] += xyzj[k]; Nin++; } logverb(" %i stars in-bounds\n", Nin); if (ann->constellation_labels && Nin) { const char* label; normalize_3(xyzc); xyzarr2radecdeg(xyzc, &ra, &dec); if (ann->constellation_labels_long) label = constellations_get_longname(i); else label = constellations_get_shortname(i); plotstuff_text_radec(pargs, ra, dec, label); } il_free(stars); } } }
/* 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"); } }
int plotstuff_plot_stack(plot_args_t* pargs, cairo_t* cairo) { int i, j; int layer; anbool morelayers; logverb("Plotting %zu stacked plot commands.\n", bl_size(pargs->cairocmds)); morelayers = TRUE; for (layer=0;; layer++) { if (!morelayers) break; morelayers = FALSE; for (i=0; i<bl_size(pargs->cairocmds); i++) { cairocmd_t* cmd = bl_access(pargs->cairocmds, i); if (cmd->layer > layer) morelayers = TRUE; if (cmd->layer != layer) continue; cairo_set_rgba(cairo, cmd->rgba); switch (cmd->type) { case CIRCLE: cairo_move_to(cairo, cmd->x + cmd->radius, cmd->y); cairo_arc(cairo, cmd->x, cmd->y, cmd->radius, 0, 2*M_PI); break; case MARKER: { double oldmarkersize = pargs->markersize; int oldmarker = pargs->marker; pargs->markersize = cmd->markersize; pargs->marker = cmd->marker; plotstuff_marker(pargs, cmd->x, cmd->y); pargs->markersize = oldmarkersize; pargs->marker = oldmarker; } break; case TEXT: cairo_move_to(cairo, cmd->x, cmd->y); cairo_show_text(cairo, cmd->text); break; case LINE: case ARROW: plotstuff_move_to(pargs, cmd->x, cmd->y); plotstuff_line_to(pargs, cmd->x2, cmd->y2); { double dx = cmd->x - cmd->x2; double dy = cmd->y - cmd->y2; double angle = atan2(dy, dx); double dang = 30. * M_PI/180.0; double arrowlen = 20; plotstuff_line_to(pargs, cmd->x2 + cos(angle+dang)*arrowlen, cmd->y2 + sin(angle+dang)*arrowlen); plotstuff_move_to(pargs, cmd->x2, cmd->y2); plotstuff_line_to(pargs, cmd->x2 + cos(angle-dang)*arrowlen, cmd->y2 + sin(angle-dang)*arrowlen); } break; case RECTANGLE: ERROR("Unimplemented!"); return -1; case POLYGON: if (!cmd->xy) break; for (j=0; j<dl_size(cmd->xy)/2; j++) (j == 0 ? cairo_move_to : cairo_line_to)(cairo, dl_get(cmd->xy, 2*j+0), dl_get(cmd->xy, 2*j+1)); if (cmd->fill) cairo_fill(cairo); break; } cairo_stroke(cairo); } } for (i=0; i<bl_size(pargs->cairocmds); i++) { cairocmd_t* cmd = bl_access(pargs->cairocmds, i); cairocmd_clear(cmd); } bl_remove_all(pargs->cairocmds); return 0; }
static PyObject* spherematch_match(PyObject* self, PyObject* args) { size_t i, N; long p1, p2; kdtree_t *kd1, *kd2; double rad; struct dualtree_results dtresults; PyArrayObject* inds; npy_intp dims[2]; PyArrayObject* dists; anbool notself; anbool permute; PyObject* rtn; // So that ParseTuple("b") with a C "anbool" works assert(sizeof(anbool) == sizeof(unsigned char)); if (!PyArg_ParseTuple(args, "lldbb", &p1, &p2, &rad, ¬self, &permute)) { PyErr_SetString(PyExc_ValueError, "spherematch_c.match: need five args: two kdtree identifiers (ints), search radius (float), notself (boolean), permuted (boolean)"); return NULL; } //printf("Notself = %i\n", (int)notself); // Nasty! kd1 = (kdtree_t*)p1; kd2 = (kdtree_t*)p2; dtresults.inds1 = il_new(256); dtresults.inds2 = il_new(256); dtresults.dists = dl_new(256); dualtree_rangesearch(kd1, kd2, 0.0, rad, notself, NULL, callback_dualtree, &dtresults, NULL, NULL); N = il_size(dtresults.inds1); dims[0] = N; dims[1] = 2; inds = (PyArrayObject*)PyArray_SimpleNew(2, dims, PyArray_INT); dims[1] = 1; dists = (PyArrayObject*)PyArray_SimpleNew(2, dims, PyArray_DOUBLE); for (i=0; i<N; i++) { int index; int* iptr; double* dptr; iptr = PyArray_GETPTR2(inds, i, 0); index = il_get(dtresults.inds1, i); if (permute) index = kdtree_permute(kd1, index); *iptr = index; iptr = PyArray_GETPTR2(inds, i, 1); index = il_get(dtresults.inds2, i); if (permute) index = kdtree_permute(kd2, index); *iptr = index; dptr = PyArray_GETPTR2(dists, i, 0); *dptr = dl_get(dtresults.dists, i); } il_free(dtresults.inds1); il_free(dtresults.inds2); dl_free(dtresults.dists); rtn = Py_BuildValue("(OO)", inds, dists); Py_DECREF(inds); Py_DECREF(dists); return rtn; }