int codefile_fix_header(codefile_t* cf) { qfits_header* hdr; fitsbin_t* fb = cf->fb; fitsbin_chunk_t* chunk = codes_chunk(cf); chunk->itemsize = cf->dimcodes * sizeof(double); chunk->nrows = cf->numcodes; hdr = codefile_get_header(cf); // fill in the real values... fits_header_mod_int(hdr, "DIMCODES", cf->dimcodes, "Number of values in a code."); fits_header_mod_int(hdr, "NCODES", cf->numcodes, "Number of codes."); fits_header_mod_int(hdr, "NSTARS", cf->numstars, "Number of stars."); fits_header_mod_double(hdr, "SCALE_U", cf->index_scale_upper, "Upper-bound index scale (radians)."); fits_header_mod_double(hdr, "SCALE_L", cf->index_scale_lower, "Lower-bound index scale (radians)."); fits_header_mod_int(hdr, "INDEXID", cf->indexid, "Index unique ID."); fits_header_mod_int(hdr, "HEALPIX", cf->healpix, "Healpix of this index."); fits_header_mod_int(hdr, "HPNSIDE", cf->hpnside, "Nside of the healpixelization"); if (fitsbin_fix_primary_header(fb) || fitsbin_fix_chunk_header(fb, chunk)) { ERROR("Failed to fix code header"); return -1; } return 0; }
int fitstable_fix_header(fitstable_t* t) { // update NAXIS2 to reflect the number of rows written. fits_header_mod_int(t->header, "NAXIS2", t->table->nr, NULL); if (in_memory(t)) return 0; if (fitsfile_fix_header(t->fid, t->header, &t->table_offset, &t->end_table_offset, t->extension, t->fn)) { return -1; } return 0; //fits_pad_file(t->fid); }
int hpquads(startree_t* starkd, codefile_t* codes, quadfile_t* quads, int Nside, double scale_min_arcmin, double scale_max_arcmin, int dimquads, int passes, int Nreuses, int Nloosen, int id, anbool scanoccupied, void* sort_data, int (*sort_func)(const void*, const void*), int sort_size, char** args, int argc) { hpquads_t myhpquads; hpquads_t* me = &myhpquads; int i; int pass; anbool circle = TRUE; double radius2; il* hptotry; int Nhptotry = 0; int nquads; double hprad; double quadscale; int skhp, sknside; qfits_header* qhdr; qfits_header* chdr; int N; int dimcodes; int quadsize; int NHP; memset(me, 0, sizeof(hpquads_t)); if (Nside > HP_MAX_INT_NSIDE) { ERROR("Error: maximum healpix Nside = %i", HP_MAX_INT_NSIDE); return -1; } if (Nreuses > 255) { ERROR("Error, reuse (-r) must be less than 256"); return -1; } me->Nside = Nside; me->dimquads = dimquads; NHP = 12 * Nside * Nside; dimcodes = dimquad2dimcode(dimquads); quadsize = sizeof(unsigned int) * dimquads; logmsg("Nside=%i. Nside^2=%i. Number of healpixes=%i. Healpix side length ~ %g arcmin.\n", me->Nside, me->Nside*me->Nside, NHP, healpix_side_length_arcmin(me->Nside)); me->sort_data = sort_data; me->sort_func = sort_func; me->sort_size = sort_size; tic(); me->starkd = starkd; N = startree_N(me->starkd); logmsg("Star tree contains %i objects.\n", N); // get the "HEALPIX" header from the skdt... skhp = qfits_header_getint(startree_header(me->starkd), "HEALPIX", -1); if (skhp == -1) { if (!qfits_header_getboolean(startree_header(me->starkd), "ALLSKY", FALSE)) { logmsg("Warning: skdt does not contain \"HEALPIX\" header. Code and quad files will not contain this header either.\n"); } } // likewise "HPNSIDE" sknside = qfits_header_getint(startree_header(me->starkd), "HPNSIDE", 1); if (sknside && Nside % sknside) { logerr("Error: Nside (-n) must be a multiple of the star kdtree healpixelisation: %i\n", sknside); return -1; } if (!scanoccupied && (N*(skhp == -1 ? 1 : sknside*sknside*12) < NHP)) { logmsg("\n\n"); logmsg("NOTE, your star kdtree is sparse (has only a fraction of the stars expected)\n"); logmsg(" so you probably will get much faster results by setting the \"-E\" command-line\n"); logmsg(" flag.\n"); logmsg("\n\n"); } quads->dimquads = me->dimquads; codes->dimcodes = dimcodes; quads->healpix = skhp; codes->healpix = skhp; quads->hpnside = sknside; codes->hpnside = sknside; if (id) { quads->indexid = id; codes->indexid = id; } qhdr = quadfile_get_header(quads); chdr = codefile_get_header(codes); add_headers(qhdr, args, argc, startree_header(me->starkd), circle, passes); add_headers(chdr, args, argc, startree_header(me->starkd), circle, passes); if (quadfile_write_header(quads)) { ERROR("Couldn't write headers to quad file"); return -1; } if (codefile_write_header(codes)) { ERROR("Couldn't write headers to code file"); return -1; } quads->numstars = codes->numstars = N; me->quad_dist2_upper = arcmin2distsq(scale_max_arcmin); me->quad_dist2_lower = arcmin2distsq(scale_min_arcmin); codes->index_scale_upper = quads->index_scale_upper = distsq2rad(me->quad_dist2_upper); codes->index_scale_lower = quads->index_scale_lower = distsq2rad(me->quad_dist2_lower); me->nuses = calloc(N, sizeof(unsigned char)); // hprad = sqrt(2) * (healpix side length / 2.) hprad = arcmin2dist(healpix_side_length_arcmin(Nside)) * M_SQRT1_2; quadscale = 0.5 * sqrt(me->quad_dist2_upper); // 1.01 for a bit of safety. we'll look at a few extra stars. radius2 = square(1.01 * (hprad + quadscale)); me->radius2 = radius2; logmsg("Healpix radius %g arcsec, quad scale %g arcsec, total %g arcsec\n", distsq2arcsec(hprad*hprad), distsq2arcsec(quadscale*quadscale), distsq2arcsec(radius2)); hptotry = il_new(1024); if (scanoccupied) { logmsg("Scanning %i input stars...\n", N); for (i=0; i<N; i++) { double xyz[3]; int j; if (startree_get(me->starkd, i, xyz)) { ERROR("Failed to get star %i", i); return -1; } j = xyzarrtohealpix(xyz, Nside); il_insert_unique_ascending(hptotry, j); if (log_get_level() > LOG_VERB) { double ra,dec; if (startree_get_radec(me->starkd, i, &ra, &dec)) { ERROR("Failed to get RA,Dec for star %i\n", i); return -1; } logdebug("star %i: RA,Dec %g,%g; xyz %g,%g,%g; hp %i\n", i, ra, dec, xyz[0], xyz[1], xyz[2], j); } } logmsg("Will check %zu healpixes.\n", il_size(hptotry)); if (log_get_level() > LOG_VERB) { logdebug("Checking healpixes: [ "); for (i=0; i<il_size(hptotry); i++) logdebug("%i ", il_get(hptotry, i)); logdebug("]\n"); } } else { if (skhp == -1) { // Try all healpixes. il_free(hptotry); hptotry = NULL; Nhptotry = NHP; } else { // The star kdtree may itself be healpixed int starhp, starx, stary; // In that case, the healpixes we are interested in form a rectangle // within a big healpix. These are the coords (in [0, Nside)) of // that rectangle. int x0, x1, y0, y1; int x, y; healpix_decompose_xy(skhp, &starhp, &starx, &stary, sknside); x0 = starx * (Nside / sknside); x1 = (starx+1) * (Nside / sknside); y0 = stary * (Nside / sknside); y1 = (stary+1) * (Nside / sknside); for (y=y0; y<y1; y++) { for (x=x0; x<x1; x++) { int j = healpix_compose_xy(starhp, x, y, Nside); il_append(hptotry, j); } } assert(il_size(hptotry) == (Nside/sknside) * (Nside/sknside)); } } if (hptotry) Nhptotry = il_size(hptotry); me->quadlist = bl_new(65536, quadsize); if (Nloosen) me->retryhps = il_new(1024); for (pass=0; pass<passes; pass++) { char key[64]; int nthispass; logmsg("Pass %i of %i.\n", pass+1, passes); logmsg("Trying %i healpixes.\n", Nhptotry); nthispass = build_quads(me, Nhptotry, hptotry, Nreuses); logmsg("Made %i quads (out of %i healpixes) this pass.\n", nthispass, Nhptotry); logmsg("Made %i quads so far.\n", (me->bigquadlist ? bt_size(me->bigquadlist) : 0) + (int)bl_size(me->quadlist)); sprintf(key, "PASS%i", pass+1); fits_header_mod_int(chdr, key, nthispass, "quads created in this pass"); fits_header_mod_int(qhdr, key, nthispass, "quads created in this pass"); logmsg("Merging quads...\n"); if (!me->bigquadlist) me->bigquadlist = bt_new(quadsize, 256); for (i=0; i<bl_size(me->quadlist); i++) { void* q = bl_access(me->quadlist, i); bt_insert2(me->bigquadlist, q, FALSE, compare_quads, &me->dimquads); } bl_remove_all(me->quadlist); } il_free(hptotry); hptotry = NULL; if (Nloosen) { int R; for (R=Nreuses+1; R<=Nloosen; R++) { il* trylist; int nthispass; logmsg("Loosening reuse maximum to %i...\n", R); logmsg("Trying %zu healpixes.\n", il_size(me->retryhps)); if (!il_size(me->retryhps)) break; trylist = me->retryhps; me->retryhps = il_new(1024); nthispass = build_quads(me, il_size(trylist), trylist, R); logmsg("Made %i quads (out of %zu healpixes) this pass.\n", nthispass, il_size(trylist)); il_free(trylist); for (i=0; i<bl_size(me->quadlist); i++) { void* q = bl_access(me->quadlist, i); bt_insert2(me->bigquadlist, q, FALSE, compare_quads, &me->dimquads); } bl_remove_all(me->quadlist); } } if (me->retryhps) il_free(me->retryhps); kdtree_free_query(me->res); me->res = NULL; me->inds = NULL; me->stars = NULL; free(me->nuses); me->nuses = NULL; logmsg("Writing quads...\n"); // add the quads from the big-quadlist nquads = bt_size(me->bigquadlist); for (i=0; i<nquads; i++) { unsigned int* q = bt_access(me->bigquadlist, i); quad_write(codes, quads, q, me->starkd, me->dimquads, dimcodes); } // add the quads that were made during the final round. for (i=0; i<bl_size(me->quadlist); i++) { unsigned int* q = bl_access(me->quadlist, i); quad_write(codes, quads, q, me->starkd, me->dimquads, dimcodes); } // fix output file headers. if (quadfile_fix_header(quads)) { ERROR("Failed to fix quadfile headers"); return -1; } if (codefile_fix_header(codes)) { ERROR("Failed to fix codefile headers"); return -1; } bl_free(me->quadlist); bt_free(me->bigquadlist); toc(); logmsg("Done.\n"); return 0; }
int main(int argc, char** args) { int argchar; char* progname = args[0]; char* outfn = NULL; char* outwcsfn = NULL; int outwcsext = 0; anwcs_t* outwcs; sl* inimgfns = sl_new(16); sl* inwcsfns = sl_new(16); sl* inwtfns = sl_new(16); il* inimgexts = il_new(16); il* inwcsexts = il_new(16); il* inwtexts = il_new(16); int i; int loglvl = LOG_MSG; int order = 3; coadd_t* coadd; lanczos_args_t largs; double sigma = 0.0; anbool nearest = FALSE; anbool divweight = FALSE; int plane = 0; while ((argchar = getopt(argc, args, OPTIONS)) != -1) switch (argchar) { case '?': case 'h': printHelp(progname); exit(0); case 'D': divweight = TRUE; break; case 'p': plane = atoi(optarg); break; case 'N': nearest = TRUE; break; case 's': sigma = atof(optarg); break; case 'v': loglvl++; break; case 'e': outwcsext = atoi(optarg); break; case 'w': outwcsfn = optarg; break; case 'o': outfn = optarg; break; case 'O': order = atoi(optarg); break; } log_init(loglvl); fits_use_error_system(); args += optind; argc -= optind; if (argc == 0 || argc % 6) { printHelp(progname); exit(-1); } for (i=0; i<argc/6; i++) { sl_append(inimgfns, args[6*i+0]); il_append(inimgexts, atoi(args[6*i+1])); sl_append(inwtfns, args[6*i+2]); il_append(inwtexts, atoi(args[6*i+3])); sl_append(inwcsfns, args[6*i+4]); il_append(inwcsexts, atoi(args[6*i+5])); } logmsg("Reading output WCS file %s\n", outwcsfn); outwcs = anwcs_open(outwcsfn, outwcsext); if (!outwcs) { ERROR("Failed to read WCS from file: %s ext %i\n", outwcsfn, outwcsext); exit(-1); } logmsg("Output image will be %i x %i\n", (int)anwcs_imagew(outwcs), (int)anwcs_imageh(outwcs)); coadd = coadd_new(anwcs_imagew(outwcs), anwcs_imageh(outwcs)); coadd->wcs = outwcs; if (nearest) { coadd->resample_func = nearest_resample_f; coadd->resample_token = NULL; } else { coadd->resample_func = lanczos_resample_f; largs.order = order; coadd->resample_token = &largs; } for (i=0; i<sl_size(inimgfns); i++) { anqfits_t* anq; anqfits_t* wanq; float* img; float* wt = NULL; anwcs_t* inwcs; char* fn; int ext; float overallwt = 1.0; int W, H; fn = sl_get(inimgfns, i); ext = il_get(inimgexts, i); logmsg("Reading input image \"%s\" ext %i\n", fn, ext); anq = anqfits_open(fn); if (!anq) { ERROR("Failed to open file \"%s\"\n", fn); exit(-1); } img = anqfits_readpix(anq, ext, 0, 0, 0, 0, plane, PTYPE_FLOAT, NULL, &W, &H); if (!img) { ERROR("Failed to read image from ext %i of %s\n", ext, fn); exit(-1); } anqfits_close(anq); logmsg("Read image: %i x %i.\n", W, H); if (sigma > 0.0) { int k0, nk; float* kernel; logmsg("Smoothing by Gaussian with sigma=%g\n", sigma); kernel = convolve_get_gaussian_kernel_f(sigma, 4, &k0, &nk); convolve_separable_f(img, W, H, kernel, k0, nk, img, NULL); free(kernel); } fn = sl_get(inwcsfns, i); ext = il_get(inwcsexts, i); logmsg("Reading input WCS file \"%s\" ext %i\n", fn, ext); inwcs = anwcs_open(fn, ext); if (!inwcs) { ERROR("Failed to read WCS from file \"%s\" ext %i\n", fn, ext); exit(-1); } if (anwcs_pixel_scale(inwcs) == 0) { ERROR("Pixel scale from the WCS file is zero. Usually this means the image has no valid WCS header.\n"); exit(-1); } if (anwcs_imagew(inwcs) != W || anwcs_imageh(inwcs) != H) { ERROR("Size mismatch between image and WCS!"); exit(-1); } fn = sl_get(inwtfns, i); ext = il_get(inwtexts, i); if (streq(fn, "none")) { logmsg("Not using weight image.\n"); wt = NULL; } else if (file_exists(fn)) { logmsg("Reading input weight image \"%s\" ext %i\n", fn, ext); wanq = anqfits_open(fn); if (!wanq) { ERROR("Failed to open file \"%s\"\n", fn); exit(-1); } int wtW, wtH; wt = anqfits_readpix(anq, ext, 0, 0, 0, 0, 0, PTYPE_FLOAT, NULL, &wtW, &wtH); if (!wt) { ERROR("Failed to read image from ext %i of %s\n", ext, fn); exit(-1); } anqfits_close(wanq); logmsg("Read image: %i x %i.\n", wtW, wtH); if (wtW != W || wtH != H) { ERROR("Size mismatch between image and weight!"); exit(-1); } } else { char* endp; overallwt = strtod(fn, &endp); if (endp == fn) { ERROR("Weight: \"%s\" is neither a file nor a double.\n", fn); exit(-1); } logmsg("Parsed weight value \"%g\"\n", overallwt); } if (divweight && wt) { int j; logmsg("Dividing image by weight image...\n"); for (j=0; j<(W*H); j++) img[j] /= wt[j]; } coadd_add_image(coadd, img, wt, overallwt, inwcs); anwcs_free(inwcs); free(img); if (wt) free(wt); } // logmsg("Writing output: %s\n", outfn); coadd_divide_by_weight(coadd, 0.0); /* if (fits_write_float_image_hdr(coadd->img, coadd->W, coadd->H, outfn)) { ERROR("Failed to write output image %s", outfn); exit(-1); } */ /* if (fits_write_float_image(coadd->img, coadd->W, coadd->H, outfn)) { ERROR("Failed to write output image %s", outfn); exit(-1); } */ { qfitsdumper qoutimg; qfits_header* hdr; hdr = anqfits_get_header2(outwcsfn, outwcsext); if (!hdr) { ERROR("Failed to read WCS file \"%s\" ext %i\n", outwcsfn, outwcsext); exit(-1); } fits_header_mod_int(hdr, "NAXIS", 2, NULL); fits_header_set_int(hdr, "NAXIS1", coadd->W, "image width"); fits_header_set_int(hdr, "NAXIS2", coadd->H, "image height"); fits_header_modf(hdr, "BITPIX", "-32", "32-bit floats"); memset(&qoutimg, 0, sizeof(qoutimg)); qoutimg.filename = outfn; qoutimg.npix = coadd->W * coadd->H; qoutimg.fbuf = coadd->img; qoutimg.ptype = PTYPE_FLOAT; qoutimg.out_ptype = BPP_IEEE_FLOAT; if (fits_write_header_and_image(NULL, &qoutimg, coadd->W)) { ERROR("Failed to write FITS image to file \"%s\"", outfn); exit(-1); } qfits_header_destroy(hdr); } coadd_free(coadd); sl_free2(inimgfns); sl_free2(inwcsfns); sl_free2(inwtfns); il_free(inimgexts); il_free(inwcsexts); il_free(inwtexts); anwcs_free(outwcs); return 0; }
// This runs after "astrometry-engine" is run on the file. static void after_solved(augment_xylist_t* axy, solve_field_args_t* sf, anbool makeplots, const char* me, anbool verbose, const char* tempdir, sl* tempdirs, sl* tempfiles, double plotscale, const char* bgfn) { sip_t wcs; double ra, dec, fieldw, fieldh; char rastr[32], decstr[32]; char* fieldunits; // print info about the field. logmsg("Field: %s\n", axy->imagefn ? axy->imagefn : axy->xylsfn); if (file_exists(axy->wcsfn)) { double orient; if (axy->wcs_last_mod) { time_t t = file_get_last_modified_time(axy->wcsfn); if (t == axy->wcs_last_mod) { logmsg("Warning: there was already a WCS file, and its timestamp has not changed.\n"); } } if (!sip_read_header_file(axy->wcsfn, &wcs)) { ERROR("Failed to read WCS header from file %s", axy->wcsfn); exit(-1); } sip_get_radec_center(&wcs, &ra, &dec); sip_get_radec_center_hms_string(&wcs, rastr, decstr); sip_get_field_size(&wcs, &fieldw, &fieldh, &fieldunits); orient = sip_get_orientation(&wcs); logmsg("Field center: (RA,Dec) = (%3.6f, %3.6f) deg.\n", ra, dec); logmsg("Field center: (RA H:M:S, Dec D:M:S) = (%s, %s).\n", rastr, decstr); logmsg("Field size: %g x %g %s\n", fieldw, fieldh, fieldunits); logmsg("Field rotation angle: up is %g degrees E of N\n", orient); } else { logmsg("Did not solve (or no WCS file was written).\n"); } // create new FITS file... if (axy->fitsimgfn && sf->newfitsfn && file_exists(axy->wcsfn)) { logmsg("Creating new FITS file \"%s\"...\n", sf->newfitsfn); if (new_wcs(axy->fitsimgfn, axy->wcsfn, sf->newfitsfn, TRUE)) { ERROR("Failed to create FITS image with new WCS headers"); exit(-1); } } // write list of index stars in image coordinates if (sf->indxylsfn && file_exists(axy->wcsfn) && file_exists(axy->rdlsfn)) { assert(axy->wcsfn); assert(axy->rdlsfn); // index rdls to xyls. if (wcs_rd2xy(axy->wcsfn, 0, axy->rdlsfn, sf->indxylsfn, NULL, NULL, FALSE, FALSE, NULL)) { ERROR("Failed to project index stars into field coordinates using wcs-rd2xy"); exit(-1); } } if (makeplots && file_exists(sf->indxylsfn) && file_readable(axy->matchfn) && file_readable(axy->wcsfn)) { logmsg("Creating index object overlay plot...\n"); if (plot_index_overlay(axy, me, sf->indxylsfn, sf->redgreenfn, plotscale, bgfn)) { ERROR("Plot index overlay failed."); } } if (makeplots && file_readable(axy->wcsfn)) { logmsg("Creating annotation plot...\n"); if (plot_annotations(axy, me, verbose, sf->ngcfn, plotscale, bgfn)) { ERROR("Plot annotations failed."); } } if (axy->imagefn && sf->kmzfn && file_exists(axy->wcsfn)) { logmsg("Writing kmz file...\n"); if (write_kmz(axy, sf->kmzfn, tempdir, tempdirs, tempfiles)) { ERROR("Failed to write KMZ."); exit(-1); } } if (sf->scampfn && file_exists(axy->wcsfn)) { //char* hdrfile = NULL; qfits_header* imageheader = NULL; starxy_t* xy; xylist_t* xyls; xyls = xylist_open(axy->axyfn); if (!xyls) { ERROR("Failed to read xylist to write SCAMP catalog"); exit(-1); } if (axy->xcol) xylist_set_xname(xyls, axy->xcol); if (axy->ycol) xylist_set_yname(xyls, axy->ycol); //xylist_set_include_flux(xyls, FALSE); xylist_set_include_background(xyls, FALSE); xy = xylist_read_field(xyls, NULL); xylist_close(xyls); if (axy->fitsimgfn) { //hdrfile = axy->fitsimgfn; imageheader = anqfits_get_header2(axy->fitsimgfn, 0); } if (axy->xylsfn) { char val[32]; //hdrfile = axy->xylsfn; imageheader = anqfits_get_header2(axy->xylsfn, 0); // Set NAXIS=2, NAXIS1=IMAGEW, NAXIS2=IMAGEH fits_header_mod_int(imageheader, "NAXIS", 2, NULL); sprintf(val, "%i", axy->W); qfits_header_add_after(imageheader, "NAXIS", "NAXIS1", val, "image width", NULL); sprintf(val, "%i", axy->H); qfits_header_add_after(imageheader, "NAXIS1", "NAXIS2", val, "image height", NULL); //fits_header_add_int(imageheader, "NAXIS1", axy->W, NULL); //fits_header_add_int(imageheader, "NAXIS2", axy->H, NULL); logverb("Using NAXIS 1,2 = %i,%i\n", axy->W, axy->H); } if (scamp_write_field(imageheader, &wcs, xy, sf->scampfn)) { ERROR("Failed to write SCAMP catalog"); exit(-1); } starxy_free(xy); if (imageheader) qfits_header_destroy(imageheader); } if (sf->scampconfigfn) { if (scamp_write_config_file(axy->scampfn, sf->scampconfigfn)) { ERROR("Failed to write SCAMP config file snippet to %s", sf->scampconfigfn); exit(-1); } } }