int codetree_get(codetree_t* s, unsigned int codeid, double* code) { if (s->tree->perm && !s->inverse_perm) { codetree_compute_inverse_perm(s); if (!s->inverse_perm) return -1; } if (codeid >= Ndata(s)) { fprintf(stderr, "Invalid code ID: %u >= %u.\n", codeid, Ndata(s)); return -1; } if (s->inverse_perm) kdtree_copy_data_double(s->tree, s->inverse_perm[codeid], 1, code); else kdtree_copy_data_double(s->tree, codeid, 1, code); return 0; }
int startree_get(startree_t* s, int starid, double* posn) { if (s->tree->perm && !s->inverse_perm) { startree_compute_inverse_perm(s); if (!s->inverse_perm) return -1; } if (starid >= Ndata(s)) { fprintf(stderr, "Invalid star ID: %u >= %u.\n", starid, Ndata(s)); assert(0); return -1; } if (s->inverse_perm) { kdtree_copy_data_double(s->tree, s->inverse_perm[starid], 1, posn); } else { kdtree_copy_data_double(s->tree, starid, 1, posn); } return 0; }
static PyObject* spherematch_kdtree_get_data(PyObject* self, PyObject* args) { PyArrayObject* pyX; double* X; PyObject* rtn; npy_intp dims[2]; long i; kdtree_t* kd; int k, D, N; //npy_int* I; npy_uint32* I; PyObject* pyO; PyObject* pyI; // this is the type returned by kdtree_rangesearch PyArray_Descr* dtype = PyArray_DescrFromType(NPY_UINT32); int req = NPY_C_CONTIGUOUS | NPY_ALIGNED | NPY_NOTSWAPPED | NPY_ELEMENTSTRIDES; if (!PyArg_ParseTuple(args, "lO", &i, &pyO)) { PyErr_SetString(PyExc_ValueError, "need two args: kdtree identifier (int), index array (numpy array of ints)"); return NULL; } // Nasty! kd = (kdtree_t*)i; D = kd->ndim; Py_INCREF(dtype); pyI = PyArray_FromAny(pyO, dtype, 1, 1, req, NULL); if (!pyI) { PyErr_SetString(PyExc_ValueError, "Failed to convert index array to np array of int"); Py_XDECREF(dtype); return NULL; } N = (int)PyArray_DIM(pyI, 0); dims[0] = N; dims[1] = D; pyX = (PyArrayObject*)PyArray_SimpleNew(2, dims, NPY_DOUBLE); X = PyArray_DATA(pyX); I = PyArray_DATA(pyI); for (k=0; k<N; k++) { kdtree_copy_data_double(kd, I[k], 1, X); X += D; } Py_DECREF(pyI); Py_DECREF(dtype); rtn = Py_BuildValue("O", pyX); Py_DECREF(pyX); return rtn; }
int allquads_create_quads(allquads_t* aq) { quadbuilder_t* qb; int i, N; double* xyz; qb = quadbuilder_init(); qb->quadd2_high = aq->quad_d2_upper; qb->quadd2_low = aq->quad_d2_lower; qb->check_scale_high = aq->use_d2_upper; qb->check_scale_low = aq->use_d2_lower; qb->dimquads = aq->dimquads; qb->add_quad = add_quad; qb->add_quad_token = aq; N = startree_N(aq->starkd); if (!qb->check_scale_high) { int* inds; inds = malloc(N * sizeof(int)); for (i=0; i<N; i++) inds[i] = i; xyz = malloc(3 * N * sizeof(double)); kdtree_copy_data_double(aq->starkd->tree, 0, N, xyz); qb->starxyz = xyz; qb->starinds = inds; qb->Nstars = N; quadbuilder_create(qb); free(xyz); free(inds); } else { int nq; int lastgrass = 0; /* xyz = malloc(3 * N * sizeof(double)); kdtree_copy_data_double(aq->starkd->tree, 0, N, xyz); */ // star A = i nq = aq->quads->numquads; for (i=0; i<N; i++) { double xyzA[3]; int* inds; int NR; int grass = (i*80 / N); if (grass != lastgrass) { printf("."); fflush(stdout); lastgrass = grass; } startree_get(aq->starkd, i, xyzA); startree_search_for(aq->starkd, xyzA, aq->quad_d2_upper, &xyz, NULL, &inds, &NR); /* startree_search_for(aq->starkd, xyzA, aq->quad_d2_upper, NULL, NULL, &inds, &NR); */ logverb("Star %i of %i: found %i stars in range\n", i+1, N, NR); aq->starA = i; qb->starxyz = xyz; qb->starinds = inds; qb->Nstars = NR; qb->check_AB_stars = check_AB; qb->check_AB_stars_token = aq; //qb->check_full_quad = check_full_quad; //qb->check_full_quad_token = aq; quadbuilder_create(qb); logverb("Star %i of %i: wrote %i quads for this star, total %i so far.\n", i+1, N, aq->quads->numquads - nq, aq->quads->numquads); free(inds); free(xyz); } // //free(xyz); printf("\n"); } quadbuilder_free(qb); return 0; }
int main(int argc, char** args) { int argchar; char* progname = args[0]; kdtree_t* kd; char* fn; int printData = 0; int treeData = 0; while ((argchar = getopt(argc, args, OPTIONS)) != -1) switch (argchar) { case 'h': printHelp(progname); exit(-1); case 'd': printData = 1; break; case 'n': treeData = 1; break; } if (argc - optind == 1) { fn = args[optind]; optind++; } else { printHelp(progname); exit(-1); } printf("Reading kdtree from file %s ...\n", fn); kd = kdtree_fits_read(fn, NULL, NULL); printf("Tree name: \"%s\"\n", kd->name); printf("Treetype: 0x%x\n", kd->treetype); printf("Data type: %s\n", kdtree_kdtype_to_string(kdtree_datatype(kd))); printf("Tree type: %s\n", kdtree_kdtype_to_string(kdtree_treetype(kd))); printf("External type: %s\n", kdtree_kdtype_to_string(kdtree_exttype(kd))); printf("N data points: %i\n", kd->ndata); printf("Dimensions: %i\n", kd->ndim); printf("Nodes: %i\n", kd->nnodes); printf("Leaf nodes: %i\n", kd->nbottom); printf("Non-leaf nodes: %i\n", kd->ninterior); printf("Tree levels: %i\n", kd->nlevels); printf("LR array: %s\n", (kd->lr ? "yes" : "no")); printf("Perm array: %s\n", (kd->perm ? "yes" : "no")); printf("Bounding box: %s\n", (kd->bb.any ? "yes" : "no")); printf("Split plane: %s\n", (kd->split.any ? "yes" : "no")); printf("Split dim: %s\n", (kd->splitdim ? "yes" : "no")); printf("Data: %s\n", (kd->data.any ? "yes" : "no")); if (kd->minval && kd->maxval) { int d; printf("Data ranges:\n"); for (d=0; d<kd->ndim; d++) printf(" %i: [%g, %g]\n", d, kd->minval[d], kd->maxval[d]); } printf("Running kdtree_check...\n"); if (kdtree_check(kd)) { printf("kdtree_check failed.\n"); exit(-1); } if (printData) { int i, d; int dt = kdtree_datatype(kd); double data[kd->ndim]; for (i=0; i<kd->ndata; i++) { int iarray; kdtree_copy_data_double(kd, i, 1, data); printf("data[%i] = %n(", i, &iarray); for (d=0; d<kd->ndim; d++) printf("%s%g", d?", ":"", data[d]); printf(")\n"); if (treeData) { printf("%*s(", iarray, ""); for (d=0; d<kd->ndim; d++) switch (dt) { case KDT_DATA_DOUBLE: printf("%s%g", (d?", ":""), kd->data.d[kd->ndim * i + d]); break; case KDT_DATA_FLOAT: printf("%s%g", (d?", ":""), kd->data.f[kd->ndim * i + d]); break; case KDT_DATA_U32: printf("%s%u", (d?", ":""), kd->data.u[kd->ndim * i + d]); break; case KDT_DATA_U16: printf("%s%u", (d?", ":""), (unsigned int)kd->data.s[kd->ndim * i + d]); break; } printf(")\n"); } } } kdtree_fits_close(kd); return 0; }
void test_hd_1(CuTest* tc) { hd_catalog_t* hdcat; int* invperm; int* present; int ind, i, N; double xyz[3]; double ra, dec; int strangehds[] = { 40142, 40441, 40672, 40746, 40763, 40764, 104176, 104193, 163635, 224698, 224699, 129371 }; if (!file_readable("hd.fits")) { printf("File \"hd.fits\" does not exist; test skipped.\n"); return; } hdcat = henry_draper_open("hd.fits"); CuAssertPtrNotNull(tc, hdcat); N = hdcat->kd->ndata; invperm = calloc(N, sizeof(int)); CuAssertPtrNotNull(tc, invperm); CuAssertIntEquals(tc, 0, kdtree_check(hdcat->kd)); kdtree_inverse_permutation(hdcat->kd, invperm); present = calloc(N, sizeof(int)); for (i=0; i<N; i++) { CuAssert(tc, "invperm in range", invperm[i] < N); present[invperm[i]]++; } for (i=0; i<N; i++) { CuAssertIntEquals(tc, 1, present[i]); } free(present); // Where is "HD n" ? for (i=0; i<10; i++) { bl* res; int j; ind = invperm[i]; kdtree_copy_data_double(hdcat->kd, ind, 1, xyz); xyzarr2radecdeg(xyz, &ra, &dec); printf("HD %i: RA,Dec %g, %g\n", i+1, ra, dec); res = henry_draper_get(hdcat, ra, dec, 10.0); CuAssertPtrNotNull(tc, res); for (j=0; j<bl_size(res); j++) { hd_entry_t* hd = bl_access(res, j); printf("res %i: HD %i, RA, Dec %g, %g\n", j, hd->hd, hd->ra, hd->dec); } bl_free(res); } for (i=0; i<sizeof(strangehds)/sizeof(int); i++) { ind = invperm[strangehds[i]-1]; kdtree_copy_data_double(hdcat->kd, ind, 1, xyz); xyzarr2radecdeg(xyz, &ra, &dec); printf("HD %i: RA,Dec %g, %g\n", strangehds[i], ra, dec); } free(invperm); henry_draper_close(hdcat); }