nomad_fits* nomad_fits_open_for_writing(char* fn) { nomad_fits* cat; qfits_header* hdr; cat = fitstable_open_for_writing(fn); if (!cat) return NULL; add_columns(cat, TRUE); hdr = fitstable_get_primary_header(cat); qfits_header_add(hdr, "NOMAD", "T", "This is a NOMAD 1.0 catalog.", NULL); qfits_header_add(hdr, "AN_FILE", AN_FILETYPE_NOMAD, "Astrometry.net file type", NULL); qfits_header_add(hdr, "COMMENT", "The FLAGS variable is composed of 15 boolean values packed into 2 bytes.", NULL, NULL); qfits_header_add(hdr, "COMMENT", " Byte 0:", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x80: UBBIT / usnob_fail", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x40: TMBIT / twomass_fail", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x20: TYBIT / tycho_astrometry", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x10: XRBIT / alt_radec", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x08: IUCBIT / alt_ucac", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x04: ITYBIT / alt_tycho", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x02: OMAGBIT / blue_o", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x01: EMAGBIT / red_e", NULL, NULL); qfits_header_add(hdr, "COMMENT", " Byte 1:", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x80: TMONLY / twomass_only", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x40: HIPAST / hipp_astrometry", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x20: SPIKE / diffraction", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x10: TYCONF / confusion", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x08: BSCONF / bright_confusion", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x04: BSART / bright_artifact", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x02: USEME / standard", NULL, NULL); qfits_header_add(hdr, "COMMENT", " 0x01: unused", NULL, NULL); qfits_header_add(hdr, "COMMENT", " Note that the ITMBIT and EXCAT bits were not set for any entry in the ", NULL, NULL); qfits_header_add(hdr, "COMMENT", " released NOMAD catalog, so were not included here.", NULL, NULL); return cat; }
matchfile* matchfile_open_for_writing(char* fn) { matchfile* mf; qfits_header* hdr; mf = fitstable_open_for_writing(fn); if (!mf) return NULL; add_columns(mf, TRUE); hdr = fitstable_get_primary_header(mf); qfits_header_add(hdr, "AN_FILE", AN_FILETYPE_MATCH, "Astrometry.net file type", NULL); return mf; }
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 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; }