int fitstable_append_to(fitstable_t* intable, FILE* fid) { fitstable_t* outtable; qfits_header* tmphdr; outtable = fitstable_open_for_appending_to(fid); fitstable_clear_table(intable); fitstable_add_fits_columns_as_struct(intable); fitstable_copy_columns(intable, outtable); outtable->table = fits_copy_table(intable->table); outtable->table->nr = 0; // swap in the input header. tmphdr = outtable->header; outtable->header = intable->header; if (fitstable_write_header(outtable)) { ERROR("Failed to write output table header"); return -1; } if (fitstable_copy_rows_data(intable, NULL, fitstable_nrows(intable), outtable)) { ERROR("Failed to copy rows from input table to output"); return -1; } if (fitstable_fix_header(outtable)) { ERROR("Failed to fix output table header"); return -1; } outtable->header = tmphdr; // clear this so that fitstable_close() doesn't fclose() it. outtable->fid = NULL; fitstable_close(outtable); return 0; }
static fitstable_t* _fitstable_open(const char* fn) { fitstable_t* tab; tab = fitstable_new(); if (!tab) { ERROR("Failed to allocate new FITS table structure"); goto bailout; } tab->extension = 1; tab->fn = strdup_safe(fn); tab->anq = anqfits_open(fn); if (!tab->anq) { ERROR("Failed to open FITS file \"%s\"", fn); goto bailout; } tab->primheader = anqfits_get_header(tab->anq, 0); if (!tab->primheader) { ERROR("Failed to read primary FITS header from %s", fn); goto bailout; } return tab; bailout: if (tab) { fitstable_close(tab); } return NULL; }
fitstable_t* fitstable_open_for_appending(const char* fn) { fitstable_t* tab = open_for_writing(fn, "r+b", NULL); if (!tab) return tab; if (fseeko(tab->fid, 0, SEEK_END)) { SYSERROR("Failed to seek to end of file"); fitstable_close(tab); return NULL; } tab->primheader = anqfits_get_header2(fn, 0); if (!tab->primheader) { ERROR("Failed to read primary FITS header from %s", fn); fitstable_close(tab); return NULL; } return tab; }
fitstable_t* fitstable_open_extension_2(const char* fn, int ext) { fitstable_t* tab = _fitstable_open(fn); if (!tab) return tab; if (fitstable_open_extension(tab, ext)) { fitstable_close(tab); return NULL; } return tab; }
fitstable_t* fitstable_open_for_appending_to(FILE* fid) { fitstable_t* tab = open_for_writing(NULL, NULL, fid); if (!tab) return tab; if (fseeko(tab->fid, 0, SEEK_END)) { SYSERROR("Failed to seek to end of file"); fitstable_close(tab); return NULL; } return tab; }
fitstable_t* fitstable_open(const char* fn) { fitstable_t* tab = _fitstable_open(fn); if (!tab) { return NULL; } if (fitstable_open_extension(tab, tab->extension)) { ERROR("Failed to open extension %i in file %s", tab->extension, fn); goto bailout; } return tab; bailout: if (tab) { fitstable_close(tab); } return NULL; }
/* uint64_t startree_get_starid(const startree_t* s, int ind) { if (!s->starids) return 0; return s->starids[ind]; } */ int startree_close(startree_t* s) { if (!s) return 0; if (s->inverse_perm) free(s->inverse_perm); if (s->header) qfits_header_destroy(s->header); if (s->tree) { if (s->writing) { kdtree_free(s->tree); free(s->sweep); } else kdtree_fits_close(s->tree); } if (s->tagalong) fitstable_close(s->tagalong); free(s); return 0; }
fitstable_t* fitstable_open_in_memory() { fitstable_t* tab; tab = fitstable_new(); if (!tab) { ERROR("Failed to allocate new FITS table structure"); goto bailout; } tab->fn = NULL; tab->fid = NULL; tab->primheader = qfits_table_prim_header_default(); tab->inmemory = TRUE; tab->extensions = bl_new(16, sizeof(fitsext_t)); return tab; bailout: if (tab) { fitstable_close(tab); } return NULL; }
static fitstable_t* open_for_writing(const char* fn, const char* mode, FILE* fid) { fitstable_t* tab; tab = fitstable_new(); if (!tab) goto bailout; tab->fn = strdup_safe(fn); if (fid) tab->fid = fid; else { tab->fid = fopen(fn, mode); if (!tab->fid) { SYSERROR("Couldn't open output file %s for writing", fn); goto bailout; } } return tab; bailout: if (tab) { fitstable_close(tab); } return NULL; }
int main(int argc, char** argv) { int argchar; allquads_t* aq; int loglvl = LOG_MSG; int i, N; char* catfn = NULL; startree_t* starkd; fitstable_t* cat; char* racol = NULL; char* deccol = NULL; int datatype = KDT_DATA_DOUBLE; int treetype = KDT_TREE_DOUBLE; int buildopts = 0; int Nleaf = 0; char* indexfn = NULL; //index_params_t* p; aq = allquads_init(); aq->skdtfn = "allquads.skdt"; aq->codefn = "allquads.code"; aq->quadfn = "allquads.quad"; while ((argchar = getopt (argc, argv, OPTIONS)) != -1) switch (argchar) { case 'v': loglvl++; break; case 'd': aq->dimquads = atoi(optarg); break; case 'I': aq->id = atoi(optarg); break; case 'h': print_help(argv[0]); exit(0); case 'i': catfn = optarg; break; case 'o': indexfn = optarg; break; case 'u': aq->quad_d2_upper = arcmin2distsq(atof(optarg)); aq->use_d2_upper = TRUE; break; case 'l': aq->quad_d2_lower = arcmin2distsq(atof(optarg)); aq->use_d2_lower = TRUE; break; default: return -1; } log_init(loglvl); if (!catfn || !indexfn) { printf("Specify in & out filenames, bonehead!\n"); print_help(argv[0]); exit( -1); } if (optind != argc) { print_help(argv[0]); printf("\nExtra command-line args were given: "); for (i=optind; i<argc; i++) { printf("%s ", argv[i]); } printf("\n"); exit(-1); } if (!aq->id) logmsg("Warning: you should set the unique-id for this index (with -I).\n"); if (aq->dimquads > DQMAX) { ERROR("Quad dimension %i exceeds compiled-in max %i.\n", aq->dimquads, DQMAX); exit(-1); } aq->dimcodes = dimquad2dimcode(aq->dimquads); // Read reference catalog, write star kd-tree logmsg("Building star kdtree: reading %s, writing to %s\n", catfn, aq->skdtfn); logverb("Reading star catalogue..."); cat = fitstable_open(catfn); if (!cat) { ERROR("Couldn't read catalog"); exit(-1); } logmsg("Got %i stars\n", fitstable_nrows(cat)); starkd = startree_build(cat, racol, deccol, datatype, treetype, buildopts, Nleaf, argv, argc); if (!starkd) { ERROR("Failed to create star kdtree"); exit(-1); } logmsg("Star kd-tree contains %i data points in dimension %i\n", startree_N(starkd), startree_D(starkd)); N = startree_N(starkd); for (i=0; i<N; i++) { double ra,dec; int ok; ok = startree_get_radec(starkd, i, &ra, &dec); logmsg(" data %i: ok %i, RA,Dec %g, %g\n", i, ok, ra, dec); } if (startree_write_to_file(starkd, aq->skdtfn)) { ERROR("Failed to write star kdtree"); exit(-1); } startree_close(starkd); fitstable_close(cat); logmsg("Wrote star kdtree to %s\n", aq->skdtfn); logmsg("Running allquads...\n"); if (allquads_open_outputs(aq)) { exit(-1); } if (allquads_create_quads(aq)) { exit(-1); } if (allquads_close(aq)) { exit(-1); } logmsg("allquads: wrote %s, %s\n", aq->quadfn, aq->codefn); // build-index: //build_index_defaults(&p); // codetree /* if (step_codetree(p, codes, &codekd, codefn, &ckdtfn, tempfiles)) return -1; */ char* ckdtfn=NULL; char* tempdir = NULL; ckdtfn = create_temp_file("ckdt", tempdir); logmsg("Creating code kdtree, reading %s, writing to %s\n", aq->codefn, ckdtfn); if (codetree_files(aq->codefn, ckdtfn, 0, 0, 0, 0, argv, argc)) { ERROR("codetree failed"); return -1; } char* skdt2fn=NULL; char* quad2fn=NULL; // unpermute-stars logmsg("Unpermute-stars...\n"); skdt2fn = create_temp_file("skdt2", tempdir); quad2fn = create_temp_file("quad2", tempdir); logmsg("Unpermuting stars from %s and %s to %s and %s\n", aq->skdtfn, aq->quadfn, skdt2fn, quad2fn); if (unpermute_stars_files(aq->skdtfn, aq->quadfn, skdt2fn, quad2fn, TRUE, FALSE, argv, argc)) { ERROR("Failed to unpermute-stars"); return -1; } allquads_free(aq); // unpermute-quads /* if (step_unpermute_quads(p, quads2, codekd, &quads3, &codekd2, quad2fn, ckdtfn, &quad3fn, &ckdt2fn, tempfiles)) return -1; */ char* quad3fn=NULL; char* ckdt2fn=NULL; ckdt2fn = create_temp_file("ckdt2", tempdir); quad3fn = create_temp_file("quad3", tempdir); logmsg("Unpermuting quads from %s and %s to %s and %s\n", quad2fn, ckdtfn, quad3fn, ckdt2fn); if (unpermute_quads_files(quad2fn, ckdtfn, quad3fn, ckdt2fn, argv, argc)) { ERROR("Failed to unpermute-quads"); return -1; } // index /* if (step_merge_index(p, codekd2, quads3, starkd2, p_index, ckdt2fn, quad3fn, skdt2fn, indexfn)) return -1; */ quadfile_t* quad; codetree_t* code; startree_t* star; logmsg("Merging %s and %s and %s to %s\n", quad3fn, ckdt2fn, skdt2fn, indexfn); if (merge_index_open_files(quad3fn, ckdt2fn, skdt2fn, &quad, &code, &star)) { ERROR("Failed to open index files for merging"); return -1; } if (merge_index(quad, code, star, indexfn)) { ERROR("Failed to write merged index"); return -1; } codetree_close(code); startree_close(star); quadfile_close(quad); printf("Done.\n"); free(ckdtfn); free(skdt2fn); free(quad2fn); free(ckdt2fn); free(quad3fn); return 0; }
int main(int argc, char *argv[]) { int argchar; char* progname = argv[0]; sl* infns = sl_new(16); char* outfnpat = NULL; char* racol = "RA"; char* deccol = "DEC"; char* tempdir = "/tmp"; anbool gzip = FALSE; sl* cols = sl_new(16); int loglvl = LOG_MSG; int nside = 1; double margin = 0.0; int NHP; double md; char* backref = NULL; fitstable_t* intable; fitstable_t** outtables; char** myargs; int nmyargs; int i; while ((argchar = getopt (argc, argv, OPTIONS)) != -1) switch (argchar) { case 'b': backref = optarg; break; case 't': tempdir = optarg; break; case 'c': sl_append(cols, optarg); break; case 'g': gzip = TRUE; break; case 'o': outfnpat = optarg; break; case 'r': racol = optarg; break; case 'd': deccol = optarg; break; case 'n': nside = atoi(optarg); break; case 'm': margin = atof(optarg); break; case 'v': loglvl++; break; case '?': fprintf(stderr, "Unknown option `-%c'.\n", optopt); case 'h': printHelp(progname); return 0; default: return -1; } if (sl_size(cols) == 0) { sl_free2(cols); cols = NULL; } nmyargs = argc - optind; myargs = argv + optind; for (i=0; i<nmyargs; i++) sl_append(infns, myargs[i]); if (!sl_size(infns)) { printHelp(progname); printf("Need input filenames!\n"); exit(-1); } log_init(loglvl); fits_use_error_system(); NHP = 12 * nside * nside; logmsg("%i output healpixes\n", NHP); outtables = calloc(NHP, sizeof(fitstable_t*)); assert(outtables); md = deg2dist(margin); /** About the mincaps/maxcaps: These have a center and radius-squared, describing the region inside a small circle on the sphere. The "mincaps" describe the regions that are definitely owned by a single healpix -- ie, more than MARGIN distance from any edge. That is, the mincap is the small circle centered at (0.5, 0.5) in the healpix and with radius = the distance to the closest healpix boundary, MINUS the margin distance. Below, we first check whether a new star is within the "mincap" of any healpix. If so, we stick it in that healpix and continue. Otherwise, we check all the "maxcaps" -- these are the healpixes it could *possibly* be in. We then refine with healpix_within_range_of_xyz. The maxcap distance is the distance to the furthest boundary point, PLUS the margin distance. */ cap_t* mincaps = malloc(NHP * sizeof(cap_t)); cap_t* maxcaps = malloc(NHP * sizeof(cap_t)); for (i=0; i<NHP; i++) { // center double r2; double xyz[3]; double* cxyz; double step = 1e-3; double v; double r2b, r2a; cxyz = mincaps[i].xyz; healpix_to_xyzarr(i, nside, 0.5, 0.5, mincaps[i].xyz); memcpy(maxcaps[i].xyz, cxyz, 3 * sizeof(double)); logverb("Center of HP %i: (%.3f, %.3f, %.3f)\n", i, cxyz[0], cxyz[1], cxyz[2]); // radius-squared: // max is the easy one: max of the four corners (I assume) r2 = 0.0; healpix_to_xyzarr(i, nside, 0.0, 0.0, xyz); logverb(" HP %i corner 1: (%.3f, %.3f, %.3f), distsq %.3f\n", i, xyz[0], xyz[1], xyz[2], distsq(xyz, cxyz, 3)); r2 = MAX(r2, distsq(xyz, cxyz, 3)); healpix_to_xyzarr(i, nside, 1.0, 0.0, xyz); logverb(" HP %i corner 1: (%.3f, %.3f, %.3f), distsq %.3f\n", i, xyz[0], xyz[1], xyz[2], distsq(xyz, cxyz, 3)); r2 = MAX(r2, distsq(xyz, cxyz, 3)); healpix_to_xyzarr(i, nside, 0.0, 1.0, xyz); logverb(" HP %i corner 1: (%.3f, %.3f, %.3f), distsq %.3f\n", i, xyz[0], xyz[1], xyz[2], distsq(xyz, cxyz, 3)); r2 = MAX(r2, distsq(xyz, cxyz, 3)); healpix_to_xyzarr(i, nside, 1.0, 1.0, xyz); logverb(" HP %i corner 1: (%.3f, %.3f, %.3f), distsq %.3f\n", i, xyz[0], xyz[1], xyz[2], distsq(xyz, cxyz, 3)); r2 = MAX(r2, distsq(xyz, cxyz, 3)); logverb(" max distsq: %.3f\n", r2); logverb(" margin dist: %.3f\n", md); maxcaps[i].r2 = square(sqrt(r2) + md); logverb(" max cap distsq: %.3f\n", maxcaps[i].r2); r2a = r2; r2 = 1.0; r2b = 0.0; for (v=0; v<=1.0; v+=step) { healpix_to_xyzarr(i, nside, 0.0, v, xyz); r2 = MIN(r2, distsq(xyz, cxyz, 3)); r2b = MAX(r2b, distsq(xyz, cxyz, 3)); healpix_to_xyzarr(i, nside, 1.0, v, xyz); r2 = MIN(r2, distsq(xyz, cxyz, 3)); r2b = MAX(r2b, distsq(xyz, cxyz, 3)); healpix_to_xyzarr(i, nside, v, 0.0, xyz); r2 = MIN(r2, distsq(xyz, cxyz, 3)); r2b = MAX(r2b, distsq(xyz, cxyz, 3)); healpix_to_xyzarr(i, nside, v, 1.0, xyz); r2 = MIN(r2, distsq(xyz, cxyz, 3)); r2b = MAX(r2b, distsq(xyz, cxyz, 3)); } mincaps[i].r2 = square(MAX(0, sqrt(r2) - md)); logverb("\nhealpix %i: min rad %g\n", i, sqrt(r2)); logverb("healpix %i: max rad %g\n", i, sqrt(r2a)); logverb("healpix %i: max rad(b) %g\n", i, sqrt(r2b)); assert(r2a >= r2b); } if (backref) { fitstable_t* tab = fitstable_open_for_writing(backref); int maxlen = 0; char* buf; for (i=0; i<sl_size(infns); i++) { char* infn = sl_get(infns, i); maxlen = MAX(maxlen, strlen(infn)); } fitstable_add_write_column_array(tab, fitscolumn_char_type(), maxlen, "filename", NULL); fitstable_add_write_column(tab, fitscolumn_i16_type(), "index", NULL); if (fitstable_write_primary_header(tab) || fitstable_write_header(tab)) { ERROR("Failed to write header of backref table \"%s\"", backref); exit(-1); } buf = malloc(maxlen+1); assert(buf); for (i=0; i<sl_size(infns); i++) { char* infn = sl_get(infns, i); int16_t ind; memset(buf, 0, maxlen); strcpy(buf, infn); ind = i; if (fitstable_write_row(tab, buf, &ind)) { ERROR("Failed to write row %i of backref table: %s = %i", i, buf, ind); exit(-1); } } if (fitstable_fix_header(tab) || fitstable_close(tab)) { ERROR("Failed to fix header & close backref table"); exit(-1); } logmsg("Wrote backref table %s\n", backref); free(buf); } for (i=0; i<sl_size(infns); i++) { char* infn = sl_get(infns, i); char* originfn = infn; int r, NR; tfits_type any, dubl; il* hps = NULL; bread_t* rowbuf; int R; char* tempfn = NULL; char* padrowdata = NULL; int ii; logmsg("Reading input \"%s\"...\n", infn); if (gzip) { char* cmd; int rtn; tempfn = create_temp_file("hpsplit", tempdir); asprintf_safe(&cmd, "gunzip -cd %s > %s", infn, tempfn); logmsg("Running: \"%s\"\n", cmd); rtn = run_command_get_outputs(cmd, NULL, NULL); if (rtn) { ERROR("Failed to run command: \"%s\"", cmd); exit(-1); } free(cmd); infn = tempfn; } intable = fitstable_open(infn); if (!intable) { ERROR("Couldn't read catalog %s", infn); exit(-1); } NR = fitstable_nrows(intable); logmsg("Got %i rows\n", NR); any = fitscolumn_any_type(); dubl = fitscolumn_double_type(); fitstable_add_read_column_struct(intable, dubl, 1, 0, any, racol, TRUE); fitstable_add_read_column_struct(intable, dubl, 1, sizeof(double), any, deccol, TRUE); fitstable_use_buffered_reading(intable, 2*sizeof(double), 1000); R = fitstable_row_size(intable); rowbuf = buffered_read_new(R, 1000, NR, refill_rowbuffer, intable); if (fitstable_read_extension(intable, 1)) { ERROR("Failed to find RA and DEC columns (called \"%s\" and \"%s\" in the FITS file)", racol, deccol); exit(-1); } for (r=0; r<NR; r++) { int hp = -1; double ra, dec; int j; double* rd; void* rowdata; void* rdata; if (r && ((r % 100000) == 0)) { logmsg("Reading row %i of %i\n", r, NR); } //printf("reading RA,Dec for row %i\n", r); rd = fitstable_next_struct(intable); ra = rd[0]; dec = rd[1]; logverb("row %i: ra,dec %g,%g\n", r, ra, dec); if (margin == 0) { hp = radecdegtohealpix(ra, dec, nside); logverb(" --> healpix %i\n", hp); } else { double xyz[3]; anbool gotit = FALSE; double d2; if (!hps) hps = il_new(4); radecdeg2xyzarr(ra, dec, xyz); for (j=0; j<NHP; j++) { d2 = distsq(xyz, mincaps[j].xyz, 3); if (d2 <= mincaps[j].r2) { logverb(" -> in mincap %i (dist %g vs %g)\n", j, sqrt(d2), sqrt(mincaps[j].r2)); il_append(hps, j); gotit = TRUE; break; } } if (!gotit) { for (j=0; j<NHP; j++) { d2 = distsq(xyz, maxcaps[j].xyz, 3); if (d2 <= maxcaps[j].r2) { logverb(" -> in maxcap %i (dist %g vs %g)\n", j, sqrt(d2), sqrt(maxcaps[j].r2)); if (healpix_within_range_of_xyz(j, nside, xyz, margin)) { logverb(" -> and within range.\n"); il_append(hps, j); } } } } //hps = healpix_rangesearch_radec(ra, dec, margin, nside, hps); logverb(" --> healpixes: ["); for (j=0; j<il_size(hps); j++) logverb(" %i", il_get(hps, j)); logverb(" ]\n"); } //printf("Reading rowdata for row %i\n", r); rowdata = buffered_read(rowbuf); assert(rowdata); j=0; while (1) { if (hps) { if (j >= il_size(hps)) break; hp = il_get(hps, j); j++; } assert(hp < NHP); assert(hp >= 0); if (!outtables[hp]) { char* outfn; fitstable_t* out; // MEMLEAK the output filename. You'll live. asprintf_safe(&outfn, outfnpat, hp); logmsg("Opening output file \"%s\"...\n", outfn); out = fitstable_open_for_writing(outfn); if (!out) { ERROR("Failed to open output table \"%s\"", outfn); exit(-1); } // Set the output table structure. if (cols) { fitstable_add_fits_columns_as_struct3(intable, out, cols, 0); } else fitstable_add_fits_columns_as_struct2(intable, out); if (backref) { tfits_type i16type; tfits_type i32type; // R = fitstable_row_size(intable); int off = R; i16type = fitscolumn_i16_type(); i32type = fitscolumn_i32_type(); fitstable_add_read_column_struct(out, i16type, 1, off, i16type, "backref_file", TRUE); off += sizeof(int16_t); fitstable_add_read_column_struct(out, i32type, 1, off, i32type, "backref_index", TRUE); } //printf("Output table:\n"); //fitstable_print_columns(out); if (fitstable_write_primary_header(out) || fitstable_write_header(out)) { ERROR("Failed to write output file headers for \"%s\"", outfn); exit(-1); } outtables[hp] = out; } if (backref) { int16_t brfile; int32_t brind; if (!padrowdata) { padrowdata = malloc(R + sizeof(int16_t) + sizeof(int32_t)); assert(padrowdata); } // convert to FITS endian brfile = htons(i); brind = htonl(r); // add backref data to rowdata memcpy(padrowdata, rowdata, R); memcpy(padrowdata + R, &brfile, sizeof(int16_t)); memcpy(padrowdata + R + sizeof(int16_t), &brind, sizeof(int32_t)); rdata = padrowdata; } else { rdata = rowdata; } if (cols) { if (fitstable_write_struct_noflip(outtables[hp], rdata)) { ERROR("Failed to copy a row of data from input table \"%s\" to output healpix %i", infn, hp); } } else { if (fitstable_write_row_data(outtables[hp], rdata)) { ERROR("Failed to copy a row of data from input table \"%s\" to output healpix %i", infn, hp); } } if (!hps) break; } if (hps) il_remove_all(hps); } buffered_read_free(rowbuf); // wack... buffered_read_free() just frees its internal buffer, // not the "rowbuf" struct itself. // who wrote this crazy code? Oh, me of 5 years ago. Jerk. free(rowbuf); fitstable_close(intable); il_free(hps); if (tempfn) { logverb("Removing temp file %s\n", tempfn); if (unlink(tempfn)) { SYSERROR("Failed to unlink() temp file \"%s\"", tempfn); } tempfn = NULL; } // fix headers so that the files are valid at this point. for (ii=0; ii<NHP; ii++) { if (!outtables[ii]) continue; off_t offset = ftello(outtables[ii]->fid); if (fitstable_fix_header(outtables[ii])) { ERROR("Failed to fix header for healpix %i after reading input file \"%s\"", ii, originfn); exit(-1); } fseeko(outtables[ii]->fid, offset, SEEK_SET); } if (padrowdata) { free(padrowdata); padrowdata = NULL; } } for (i=0; i<NHP; i++) { if (!outtables[i]) continue; if (fitstable_fix_header(outtables[i]) || fitstable_fix_primary_header(outtables[i]) || fitstable_close(outtables[i])) { ERROR("Failed to close output table for healpix %i", i); exit(-1); } } free(outtables); sl_free2(infns); sl_free2(cols); free(mincaps); free(maxcaps); return 0; }
int main(int argc, char **argv) { int argchar; double ra=HUGE_VAL, dec=HUGE_VAL, radius=HUGE_VAL; int loglvl = LOG_MSG; char** myargs; int nmyargs; int i; char* outfn = NULL; fitstable_t* table = NULL; while ((argchar = getopt (argc, argv, OPTIONS)) != -1) switch (argchar) { case 'o': outfn = optarg; break; case 'r': ra = atof(optarg); break; case 'd': dec = atof(optarg); break; case 'R': radius = atof(optarg); break; case 'v': loglvl++; break; case '?': fprintf(stderr, "Unknown option `-%c'.\n", optopt); case 'h': default: printHelp(argv[0]); return -1; } log_init(loglvl); nmyargs = argc - optind; myargs = argv + optind; if (nmyargs < 1) { printHelp(argv[0]); exit(-1); } if (ra == HUGE_VAL || dec == HUGE_VAL || radius == HUGE_VAL) { printHelp(argv[0]); exit(-1); } if (outfn) { table = fitstable_open_for_writing(outfn); if (!table) { ERROR("Failed to open output table"); exit(-1); } if (fitstable_write_primary_header(table)) { ERROR("Failed to write primary header of output table"); exit(-1); } } for (i=0; i<nmyargs; i++) { char* indexfn = myargs[i]; index_t index; sl* cols; int* inds; double* radecs; int N; int j; fitstable_t* tagtable = NULL; logmsg("Reading index \"%s\"...\n", indexfn); if (!index_load(indexfn, 0, &index)) { ERROR("Failed to read index \"%s\"", indexfn); continue; } logmsg("Index %s: id %i, healpix %i (nside %i), %i stars, %i quads, dimquads=%i, scales %g to %g arcmin.\n", index.indexname, index.indexid, index.healpix, index.hpnside, index.nstars, index.nquads, index.dimquads, arcsec2arcmin(index.index_scale_lower), arcsec2arcmin(index.index_scale_upper)); cols = startree_get_tagalong_column_names(index.starkd, NULL); { char* colstr = sl_join(cols, ", "); logmsg("Tag-along columns: %s\n", colstr); free(colstr); } logmsg("Searching for stars around RA,Dec (%g, %g), radius %g deg.\n", ra, dec, radius); startree_search_for_radec(index.starkd, ra, dec, radius, NULL, &radecs, &inds, &N); logmsg("Found %i stars\n", N); if (table) { int tagsize; int rowsize; char* rowbuf = NULL; if (i > 0) { fitstable_next_extension(table); fitstable_clear_table(table); } tagtable = startree_get_tagalong(index.starkd); if (tagtable) { fitstable_add_fits_columns_as_struct(tagtable); logverb("Input tag-along table:\n"); if (log_get_level() >= LOG_VERB) fitstable_print_columns(tagtable); fitstable_copy_columns(tagtable, table); } tagsize = fitstable_get_struct_size(table); debug("tagsize=%i\n", tagsize); // Add RA,Dec at the end of the row... fitstable_add_write_column_struct(table, fitscolumn_double_type(), 1, tagsize, fitscolumn_double_type(), "RA", "degrees"); fitstable_add_write_column_struct(table, fitscolumn_double_type(), 1, tagsize + sizeof(double), fitscolumn_double_type(), "DEC", "degrees"); rowsize = fitstable_get_struct_size(table); assert(rowsize == tagsize + 2*sizeof(double)); debug("rowsize=%i\n", rowsize); rowbuf = malloc(rowsize); logverb("Output table:\n"); if (log_get_level() >= LOG_VERB) fitstable_print_columns(table); if (fitstable_write_header(table)) { ERROR("Failed to write header of output table"); exit(-1); } for (j=0; j<N; j++) { if (tagtable) { if (fitstable_read_struct(tagtable, inds[j], rowbuf)) { ERROR("Failed to read row %i of tag-along table", inds[j]); exit(-1); } } // Add RA,Dec to end of struct... memcpy(rowbuf + tagsize, radecs+2*j+0, sizeof(double)); memcpy(rowbuf + tagsize + sizeof(double), radecs+2*j+1, sizeof(double)); if (fitstable_write_struct(table, rowbuf)) { ERROR("Failed to write row %i of output", j); exit(-1); } } free(rowbuf); if (fitstable_fix_header(table)) { ERROR("Failed to fix header of output table"); exit(-1); } } sl_free2(cols); free(radecs); free(inds); index_close(&index); } if (table) { if (fitstable_close(table)) { ERROR("Failed to close output table"); exit(-1); } } return 0; }
int loadpsffile(char *passfile, PSF_DATA_STRUCT *psfds, float f_cutoff) { char filename[2][255]={"front.fits","back.fits"}; char buffer[255]; int i, j, f, ncurr; float *psfscale; fitstable_t* tab; qfits_header* hdr; tfits_type flt = fitscolumn_float_type(); float ***psfdata; int nE, nMu; if ( !(psfdata=(float ***) calloc(2,sizeof(float **))) ) { printf("# Cannot allocate psfdata in loadpsffile() %s:%d\n",__FILE__,__LINE__); return -1; } if ( !(psfdata[0]=(float **) calloc(NPSF_DATA,sizeof(float *))) ) { printf("# Cannot allocate psfdata[0] in loadpsffile() %s:%d\n",__FILE__,__LINE__); return -1; } if ( !(psfdata[1]=(float **) calloc(NPSF_DATA,sizeof(float *))) ) { printf("# Cannot allocate psfdata[1] in loadpsffile() %s:%d\n",__FILE__,__LINE__); return -1; } psfds->psfdata=psfdata; psfds->f_cutoff=f_cutoff; if (debug>2) { printf(" "); for (i=0;i<NPSF_DATA;i++) { printf(" %10s",psf_colname[i]); } printf("\n"); } for (f=0;f<2;f++) { sprintf(buffer,"psf_%s_%s",passfile,filename[f]); printf("# loading PSF file: %s\n",buffer); if ( (tab = fitstable_open(buffer))==NULL) { printf("# Cannot open %s in loadpsffile() %s:%d\n",buffer,__FILE__,__LINE__); return -1; } hdr = fitstable_get_primary_header(tab); ncurr = fitstable_nrows(tab); for (i=ENERG_LO;i<=GTAIL;i++) { if ( (psfdata[f][i]= (float *) fitstable_read_column_array(tab, psf_colname[i], flt))==NULL) { printf("# Cannot file column %s (file=%s) in loadpsffile() %s:%d\n",psf_colname[i],buffer,__FILE__,__LINE__); fitstable_close(tab); return -1; } } int D = fitstable_get_array_size(tab, psf_colname[NTAIL]); psfds->nE = nE = fitstable_get_array_size(tab, psf_colname[ENERG_LO]); psfds->nMu = nMu = fitstable_get_array_size(tab, psf_colname[CTHETA_LO]); for (i=FCORE;i<=RMAX2;i++) { if (!(psfdata[f][i]=(float *) calloc(D,sizeof(float)))) { printf("# Cannot allocate psfdata[%d][%d] in loadpsffile() %s:%d\n",f,i,__FILE__,__LINE__); return -1; } } for (j=0;j<D;j++) { if (debug>2) printf("[%2d] %2d",f,j); score_g=psfdata[f][SCORE][j]; gcore_g=psfdata[f][GCORE][j]; stail_g=psfdata[f][STAIL][j]; gtail_g=psfdata[f][GTAIL][j]; psfdata[f][FACTORTAIL][j]=factortail_g=gtail_g*stail_g*stail_g*2; psfdata[f][FACTORCORE][j]=factorcore_g=gcore_g*score_g*score_g*2; fcore_g=stail_g/score_g; psfdata[f][FCORE][j]=fcore_g=1/(1+psfdata[f][NTAIL][j]*fcore_g*fcore_g); psfdata[f][NORMCORE][j]=fcore_g/(2*M_PI*score_g*score_g)*(1-1/gcore_g); psfdata[f][NORMTAIL][j]=(1-fcore_g)/(2*M_PI*stail_g*stail_g)*(1-1/gtail_g); ; psfdata[f][RMAX2][j]=root(f_cutoff); if (debug>2) { for (i=0;i<2;i++) { printf(" %10g",log10(psfdata[f][i][j%nE])); } for (i=2;i<4;i++) { printf(" %10g",psfdata[f][i][(j/nE)%nMu]); } for (i=4;i<NPSF_DATA;i++) { printf(" %10g",psfdata[f][i][j]); } printf(" %10g\n",ickingfunk(psfdata[f][RMAX2][j])); } } if (f==0) { psfds->lemin=log10(psfdata[f][ENERG_LO][0]); psfds->lestep=log10(psfdata[f][ENERG_HI][0])-log10(psfdata[f][ENERG_LO][0]); psfds->mumin=psfdata[f][CTHETA_LO][0]; psfds->mustep=psfdata[f][CTHETA_HI][0]-psfdata[f][CTHETA_LO][0]; fitstable_open_next_extension(tab); hdr = fitstable_get_primary_header(tab); ncurr = fitstable_nrows(tab); if ( (psfscale= (float *) fitstable_read_column_array(tab, "PSFSCALE", flt))==NULL) { printf("# Cannot read PSFSCALE (file=%s) in loadpsffile() %s:%d\n",buffer,__FILE__,__LINE__); fitstable_close(tab); return -1; } c0front2=psfscale[0]*psfscale[0]; c1front2=psfscale[1]*psfscale[1]; c0back2=psfscale[2]*psfscale[2]; c1back2=psfscale[3]*psfscale[3]; minus2beta=2*psfscale[4]; if (debug>1) { printf("# %g %g %g %g %g\n",c0front2,c1front2,c0back2,c1back2,minus2beta); } } fitstable_close(tab); } free((void *) psfscale); return 0; }
int unpermute_stars_files(const char* skdtinfn, const char* quadinfn, const char* skdtoutfn, const char* quadoutfn, anbool dosweeps, anbool check, char** args, int argc) { quadfile_t* qfin; quadfile_t* qfout; startree_t* treein; startree_t* treeout; fitstable_t* tagout = NULL; fitstable_t* tagin; int rtn; logmsg("Reading star tree from %s ...\n", skdtinfn); treein = startree_open(skdtinfn); if (!treein) { ERROR("Failed to read star kdtree from %s.\n", skdtinfn); return -1; } logmsg("Reading quadfile from %s ...\n", quadinfn); qfin = quadfile_open(quadinfn); if (!qfin) { ERROR("Failed to read quadfile from %s.\n", quadinfn); return -1; } logmsg("Writing quadfile to %s ...\n", quadoutfn); qfout = quadfile_open_for_writing(quadoutfn); if (!qfout) { ERROR("Failed to write quadfile to %s.\n", quadoutfn); return -1; } rtn = unpermute_stars(treein, qfin, &treeout, qfout, dosweeps, check, args, argc); if (rtn) return rtn; if (quadfile_close(qfout)) { ERROR("Failed to close output quadfile.\n"); return -1; } logmsg("Writing star kdtree to %s ...\n", skdtoutfn); if (startree_write_to_file(treeout, skdtoutfn)) { ERROR("Failed to write star kdtree.\n"); return -1; } if (startree_has_tagalong(treein)) { logmsg("Permuting tag-along table...\n"); tagin = startree_get_tagalong(treein); if (tagin) { tagout = fitstable_open_for_appending(skdtoutfn); tagout->table = fits_copy_table(tagin->table); tagout->table->nr = 0; if (unpermute_stars_tagalong(treein, tagout)) { ERROR("Failed to permute tag-along table"); return -1; } if (fitstable_close(tagout)) { ERROR("Failed to close tag-along data"); return -1; } } } quadfile_close(qfin); startree_close(treein); free(treeout->sweep); free(treeout->tree); treeout->tree = NULL; startree_close(treeout); return 0; }
int loadphotondata(char *filename, char *passfile) { fitstable_t* tab; qfits_header* hdr; tfits_type flt = fitscolumn_float_type(), chtype = fitscolumn_char_type(), dbltype=fitscolumn_double_type(); float *localphotondata[NPHOTON_LOADDATA], *galdisrsp=NULL, *isodisrsp=NULL; double *localphotontime; char *conversion_type=NULL; char buffer[255]; int i, j, ncurr, ndiff, nresp=0, conv_type, psf_cnt, psf_ind; double rmax2, ehold, rad, r2hold; if ( (tab = fitstable_open(filename))==NULL) { printf("# Cannot open %s in loadphotondata() %s:%d\n",filename,__FILE__,__LINE__); return -1; } hdr = fitstable_get_primary_header(tab); hdr = fitstable_get_header(tab); ncurr = fitstable_nrows(tab); if (debug) printf("# filename= %s ncurr= %d\n",filename,ncurr); /* load in the energy, theta, ra and dec */ for (i=0; i<NPHOTON_LOADDATA; i++) { if ( (localphotondata[i]= (float *) fitstable_read_column_array(tab, photon_colname[i], flt))==NULL) { printf("# vector for %s is NULL in loadphotondata() %s:%d\n",photon_colname[i],__FILE__,__LINE__); for (j=0; j<i; j++) { SAFEFREE( localphotondata[i]); } fitstable_close(tab); return -1; } } /* load in conversion type */ if ( (conversion_type= (char *) fitstable_read_column(tab, "CONVERSION_TYPE", chtype))==NULL) { printf("# array for conversion_type is NULL in loadphotondata() %s:%d\n",__FILE__,__LINE__); for (i=0; i<NPHOTON_LOADDATA; i++) { SAFEFREE( localphotondata[i]); } fitstable_close(tab); return -1; } /* load in photon arrival time */ if ( (localphotontime= (double *) fitstable_read_column(tab, "TIME", dbltype))==NULL) { printf("# array for localphotontime is NULL in loadphotondata() %s:%d\n",__FILE__,__LINE__); for (i=0; i<NPHOTON_LOADDATA; i++) { SAFEFREE( localphotondata[i]); } SAFEFREE( conversion_type); fitstable_close(tab); return -1; } /* load in the diffuse response functions that match the passfile */ ndiff = qfits_header_getint(hdr, "NDIFRSP",-1); for (i=0; i<ndiff; i++) { sprintf(buffer,"DIFRSP%d",i); char *respname = qfits_header_getstr(hdr, buffer); if (debug) printf("# %d %s = %s\n",i,buffer,respname); if (strcasestr(respname,passfile)) { if (strcasestr(respname,"gll")) { if (debug) printf("# galactic is %s\n",buffer); if ( (galdisrsp= (float *) fitstable_read_column_array(tab, buffer, flt))==NULL) { printf("# array for %s is NULL in loadphotondata() %s:%d\n",buffer,__FILE__,__LINE__); for (j=0; j<i; j++) { SAFEFREE( localphotondata[i]); } SAFEFREE( conversion_type); SAFEFREE(localphotontime); SAFEFREE( galdisrsp); SAFEFREE( isodisrsp); fitstable_close(tab); return -1; } nresp++; } if (strcasestr(respname,"iso")) { if (debug) printf("# iso is %s\n",buffer); if ( (isodisrsp= (float *) fitstable_read_column_array(tab, buffer, flt))==NULL) { printf("# array for %s is NULL in loadphotondata() %s:%d\n",buffer,__FILE__,__LINE__); for (j=0; j<i; j++) { SAFEFREE( localphotondata[i]); } SAFEFREE( conversion_type); SAFEFREE(localphotontime); SAFEFREE( galdisrsp); SAFEFREE( isodisrsp); fitstable_close(tab); return -1; } nresp++; } } } if (nresp<2) { printf("Could not find the matching response functions in loadphotondata() %s:%d.\n",__FILE__,__LINE__); return -1; } if (alloc_globals(ncurr+ntot)) { SAFEFREE(localphotontime); SAFEFREE(conversion_type); SAFEFREE(data); for (i=0; i<NPHOTON_LOADDATA; i++) { SAFEFREE(localphotondata[i]); } return -1; } fitstable_close(tab); j=0; for (i=0; i<ncurr; i++) { ehold=localphotondata[ENERGY][i]; if (ehold>=e0 && ehold<=e1) { double ra, dec; __sincospi((ra=localphotondata[RA][i])/180.0,data+d*(j+ntot)+1,data+d*(j+ntot)); __sincospi((dec=localphotondata[DEC][i])/180.0,data+d*(j+ntot)+2,&rad); data[d*(ntot+j)]*=rad; data[d*(ntot+j)+1]*=rad; if (ehold<energy_min) energy_min=ehold; /* calculate RMAX2 for this photon */ conv_type=conversion_type[i]; rmax2=photondata[COSTHETA][j+ntot]=cos(localphotondata[THETA][i]*M_PI/180.0); if (debug>3) printf("# costheta= %g\n",rmax2); #if 1 /* fast way --- evenly spaced in log energy and costheta -- no interpolation */ // rmax2=psfds.psfdata[conv_type][RMAX2][(int) ((rmax2-psfds.mumin)/psfds.mustep)*psfds.nE + (int) ((log10(ehold)-psfds.lemin)/psfds.lestep)]; rmax2=rmax2_funk(psfds,conv_type,rmax2,ehold); #else /* slow way --- find the right bin -- no interpolation */ for (psf_cnt=0; psf_cnt<psfds.nE; psf_cnt++) { if (ehold>psfds.psfdata[conv_type][ENERG_LO][psf_cnt] && ehold<=psfds.psfdata[conv_type][ENERG_HI][psf_cnt]) { psf_ind=psf_cnt; break; } } for (psf_cnt=0; psf_cnt<psfds.nMu; psf_cnt++) { if (rmax2>psfds.psfdata[conv_type][CTHETA_LO][psf_cnt] && rmax2<=psfds.psfdata[conv_type][CTHETA_HI][psf_cnt]) { psf_ind+=psf_cnt*psfds.nE; break; } } rmax2=psfds.psfdata[conv_type][RMAX2][psf_ind]; #endif if (debug>3) printf("# rmax2= %g\n",rmax2); if (conv_type) { /* back conversion */ r2hold=SPE_squared_back(ehold)*rmax2; data[d*(ntot+j)+3]=-(r2hold>4 ? 4 : r2hold); } else { /* front conversion */ r2hold=SPE_squared_front(ehold)*rmax2; data[d*(ntot+j)+3]=(r2hold>4 ? 4 : r2hold); } if (debug>3) printf("# %g rmax2= %g %g %g\n",ehold,data[d*(ntot+j)+3],SPE_squared_front(ehold),localphotondata[THETA][i]); photondata[ENERGY][j+ntot]=ehold; photondata[DIFRSP_GAL][j+ntot]=galdisrsp[i]; photondata[DIFRSP_ISO][j+ntot]=isodisrsp[i]; r2hold=aeffds.aeffdata[conv_type][AEFF_EFF_AREA] [(int) ((photondata[COSTHETA][j+ntot]-aeffds.mumin)/aeffds.mustep)*aeffds.nE + (int) ((log10(ehold)-aeffds.lemin)/aeffds.lestep)]; if (r2hold<EFFAREAMIN) r2hold=EFFAREAMIN; photondata[EFFAREA][j+ntot]=r2hold; /* calculate the effective area integral */ r2hold=calcefft(ra,dec,ehold,&aeffds,<cubeds); if (r2hold<EFFAREATMIN) r2hold=EFFAREATMIN; photondata[EFFAREAT][j+ntot]=r2hold; photontime[j+ntot]=localphotontime[i]; j++; } } for (i=0; i<NPHOTON_LOADDATA; i++) { SAFEFREE(localphotondata[i]); } SAFEFREE( galdisrsp); SAFEFREE( isodisrsp); SAFEFREE(conversion_type); SAFEFREE(localphotontime); ntot+=j; if (debug) printf("# ntot= %d j= %d\n",ntot,j); if (j!=ncurr) { if (alloc_globals(ncurr+ntot)) { SAFEFREE(data); for (i=0; i<NPHOTON_LOADDATA; i++) { SAFEFREE(localphotondata[i]); } return -1; } } return 0; }
int loadltcube(char *filename, LTCUBE_DATA_STRUCT *ltcubeds) { fitstable_t* tab; qfits_header* hdr; tfits_type flt = fitscolumn_float_type(); if ( (tab = fitstable_open(filename))==NULL) { printf("# Cannot open %s in loadltcube() %s:%d\n",filename,__FILE__,__LINE__); return -1; } fitstable_open_next_extension(tab); hdr = fitstable_get_header(tab); strncpy(ltcubeds->ordering,qfits_header_getstr(hdr, "ORDERING"),sizeof(ltcubeds->ordering)); strncpy(ltcubeds->coordsys,qfits_header_getstr(hdr, "COORDSYS"),sizeof(ltcubeds->coordsys)); strncpy(ltcubeds->thetabin,qfits_header_getstr(hdr, "THETABIN"),sizeof(ltcubeds->thetabin)); ltcubeds->nside=qfits_header_getint(hdr,"NSIDE",-1); ltcubeds->firstpix=qfits_header_getint(hdr,"FIRSTPIX",-1); ltcubeds->lastpix=qfits_header_getint(hdr,"LASTPIX",-1); if (debug) { printf("ORDERING= %s; COORDSYS= %s; THETABIN= %s; NSIDE= %d FIRSTPIX= %d LASTPIX= %d\n", ltcubeds->ordering, ltcubeds->coordsys, ltcubeds->thetabin, ltcubeds->nside, ltcubeds->firstpix, ltcubeds->lastpix); } /* load in livetime cube data */ ltcubeds->nMu = fitstable_get_array_size(tab, "COSBINS"); ltcubeds->ncosbins = fitstable_nrows(tab); printf("# nrows= %d\n",ltcubeds->ncosbins); if ( (ltcubeds->cosbins= (float *) fitstable_read_column_array(tab, "COSBINS", flt))==NULL) { printf("# cannot load vector COSBINS from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__); fitstable_close(tab); return -1; } fitstable_open_next_extension(tab); if ( (ltcubeds->cthetamin= (float *) fitstable_read_column(tab, "CTHETA_MIN", flt))==NULL) { printf("# cannot load vector CTHETA_MIN from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__); fitstable_close(tab); return -1; } if ( (ltcubeds->cthetamax= (float *) fitstable_read_column(tab, "CTHETA_MAX", flt))==NULL) { printf("# cannot load vector CTHETA_MAX from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__); fitstable_close(tab); return -1; } if ( (ltcubeds->aeffmubin = (int *) calloc(ltcubeds->nMu,sizeof(int)))==NULL) { printf("# cannot allocate vector AEFFMUBIN in loadltcude() %s:%d\n",__FILE__,__LINE__); fitstable_close(tab); return -1; } ltcubeds->aeffmubin[0]=-99; if (debug>2) { int i, j; printf("# nMu= %d\n",ltcubeds->nMu); for (i=0;i<ltcubeds->nMu;i++) { printf("%3d %10g %10g\n",i,ltcubeds->cthetamin[i],ltcubeds->cthetamax[i]); } for (j=0;j<10;j++) { printf("j= %d",j); for (i=0;i<ltcubeds->nMu;i++) { printf(" %10g",ltcubeds->cosbins[j*ltcubeds->nMu+i]); } printf("\n"); } } printf("# loaded %s\n",filename); return 0; }
int nomad_fits_close(nomad_fits* nomad) { return fitstable_close(nomad); }
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; }
int main(int argc, char *argv[]) { int argchar; char* progname = argv[0]; index_t* index; fitstable_t* table; char* indexfn = NULL; char* outfn = NULL; int i, D, N; while ((argchar = getopt (argc, argv, OPTIONS)) != -1) { switch (argchar) { case 'i': indexfn = optarg; break; case 'o': outfn = optarg; break; default: case 'h': printHelp(progname); exit(0); } } if (!(indexfn && outfn)) { printHelp(progname); exit(-1); } index = index_load(indexfn, 0, NULL); if (!index) { ERROR("Failed to open index"); exit(-1); } table = fitstable_open_for_writing(outfn); if (!table) { ERROR("Failed to open output file for writing"); exit(-1); } D = index_get_quad_dim(index); fitstable_add_write_column_array(table, fitscolumn_i32_type(), D, "quads", ""); if (fitstable_write_primary_header(table) || fitstable_write_header(table)) { ERROR("Failed to write headers"); exit(-1); } N = index_nquads(index); for (i=0; i<N; i++) { unsigned int quad[D]; quadfile_get_stars(index->quads, i, quad); if (fitstable_write_row(table, quad)) { ERROR("Failed to write quad %i", i); exit(-1); } } if (fitstable_fix_header(table)) { ERROR("Failed to fix quad header"); exit(-1); } fitstable_next_extension(table); fitstable_clear_table(table); // write star RA,Dec s. fitstable_add_write_column(table, fitscolumn_double_type(), "RA", "deg"); fitstable_add_write_column(table, fitscolumn_double_type(), "Dec", "deg"); if (fitstable_write_header(table)) { ERROR("Failed to write star header"); exit(-1); } N = index_nstars(index); for (i=0; i<N; i++) { double xyz[3]; double ra, dec; startree_get(index->starkd, i, xyz); xyzarr2radecdeg(xyz, &ra, &dec); if (fitstable_write_row(table, &ra, &dec)) { ERROR("Failed to write star %i", i); exit(-1); } } if (fitstable_fix_header(table) || fitstable_close(table)) { ERROR("Failed to fix star header and close"); exit(-1); } index_close(index); return 0; }
int resort_xylist(const char* infn, const char* outfn, const char* fluxcol, const char* backcol, anbool ascending) { FILE* fin = NULL; FILE* fout = NULL; double *flux = NULL, *back = NULL; int *perm1 = NULL, *perm2 = NULL; anbool *used = NULL; int start, size, nextens, ext; int (*compare)(const void*, const void*); fitstable_t* tab = NULL; anqfits_t* anq = NULL; if (ascending) compare = compare_doubles_asc; else compare = compare_doubles_desc; if (!fluxcol) fluxcol = "FLUX"; if (!backcol) backcol = "BACKGROUND"; fin = fopen(infn, "rb"); if (!fin) { SYSERROR("Failed to open input file %s", infn); return -1; } fout = fopen(outfn, "wb"); if (!fout) { SYSERROR("Failed to open output file %s", outfn); goto bailout; } // copy the main header exactly. anq = anqfits_open(infn); if (!anq) { ERROR("Failed to open file \"%s\"", infn); goto bailout; } start = anqfits_header_start(anq, 0); size = anqfits_header_size (anq, 0); if (pipe_file_offset(fin, start, size, fout)) { ERROR("Failed to copy primary FITS header."); goto bailout; } nextens = anqfits_n_ext(anq); tab = fitstable_open(infn); if (!tab) { ERROR("Failed to open FITS table in file %s", infn); goto bailout; } for (ext=1; ext<nextens; ext++) { int hdrstart, hdrsize, datstart; int i, N; int rowsize; hdrstart = anqfits_header_start(anq, ext); hdrsize = anqfits_header_size (anq, ext); datstart = anqfits_data_start (anq, ext); if (!anqfits_is_table(anq, ext)) { ERROR("Extention %i isn't a table. Skipping", ext); continue; } // Copy the header as-is. if (pipe_file_offset(fin, hdrstart, hdrsize, fout)) { ERROR("Failed to copy the header of extension %i", ext); goto bailout; } if (fitstable_read_extension(tab, ext)) { ERROR("Failed to read FITS table from extension %i", ext); goto bailout; } rowsize = fitstable_row_size(tab); // read FLUX column as doubles. flux = fitstable_read_column(tab, fluxcol, TFITS_BIN_TYPE_D); if (!flux) { ERROR("Failed to read FLUX column from extension %i", ext); goto bailout; } // BACKGROUND back = fitstable_read_column(tab, backcol, TFITS_BIN_TYPE_D); if (!back) { ERROR("Failed to read BACKGROUND column from extension %i", ext); goto bailout; } debug("First 10 rows of input table:\n"); for (i=0; i<10; i++) debug("flux %g, background %g\n", flux[i], back[i]); N = fitstable_nrows(tab); // set back = flux + back (ie, non-background-subtracted flux) for (i=0; i<N; i++) back[i] += flux[i]; // Sort by flux... perm1 = permuted_sort(flux, sizeof(double), compare, NULL, N); // Sort by non-background-subtracted flux... perm2 = permuted_sort(back, sizeof(double), compare, NULL, N); used = malloc(N * sizeof(anbool)); memset(used, 0, N * sizeof(anbool)); // Check sort... for (i=0; i<N-1; i++) { if (ascending) { assert(flux[perm1[i]] <= flux[perm1[i+1]]); assert(back[perm2[i]] <= back[perm2[i+1]]); } else { assert(flux[perm1[i]] >= flux[perm1[i+1]]); assert(back[perm2[i]] >= back[perm2[i+1]]); } } for (i=0; i<N; i++) { int j; int inds[] = { perm1[i], perm2[i] }; for (j=0; j<2; j++) { int index = inds[j]; assert(index < N); if (used[index]) continue; used[index] = TRUE; debug("adding index %i: %s %g\n", index, j==0 ? "flux" : "bgsub", j==0 ? flux[index] : back[index]); if (pipe_file_offset(fin, datstart + index * rowsize, rowsize, fout)) { ERROR("Failed to copy row %i", index); goto bailout; } } } for (i=0; i<N; i++) assert(used[i]); if (fits_pad_file(fout)) { ERROR("Failed to add padding to extension %i", ext); goto bailout; } free(flux); flux = NULL; free(back); back = NULL; free(perm1); perm1 = NULL; free(perm2); perm2 = NULL; free(used); used = NULL; } fitstable_close(tab); tab = NULL; if (fclose(fout)) { SYSERROR("Failed to close output file %s", outfn); return -1; } fclose(fin); return 0; bailout: if (tab) fitstable_close(tab); if (fout) fclose(fout); if (fin) fclose(fin); free(flux); free(back); free(perm1); free(perm2); free(used); return -1; }
int matchfile_close(matchfile* nomad) { return fitstable_close(nomad); }