int plotstuff_set_wcs_file(plot_args_t* pargs, const char* filename, int ext) { anwcs_t* wcs = anwcs_open(filename, ext); if (!wcs) { ERROR("Failed to read WCS file \"%s\", extension %i", filename, ext); return -1; } return plotstuff_set_wcs(pargs, wcs); }
int plot_xy_set_wcs_filename(plotxy_t* args, const char* fn, int ext) { anwcs_free(args->wcs); args->wcs = anwcs_open(fn, ext); if (!args->wcs) { ERROR("Failed to read WCS file \"%s\"", fn); return -1; } return 0; }
int wcs_xy2rd(const char* wcsfn, int ext, const char* xylsfn, const char* rdlsfn, const char* xcol, const char* ycol, int forcetan, int forcewcslib, il* fields) { rdlist_t* rdls = NULL; xylist_t* xyls = NULL; anwcs_t* wcs = NULL; int i; int rtn = -1; anbool alloced_fields = FALSE; // read WCS. if (forcewcslib) { wcs = anwcs_open_wcslib(wcsfn, ext); } else if (forcetan) { wcs = anwcs_open_tan(wcsfn, ext); } else { wcs = anwcs_open(wcsfn, ext); } if (!wcs) { ERROR("Failed to read WCS file \"%s\", extension %i", wcsfn, ext); return -1; } // read XYLS. xyls = xylist_open(xylsfn); if (!xyls) { ERROR("Failed to read an xylist from file %s", xylsfn); goto bailout; } xylist_set_include_flux(xyls, FALSE); xylist_set_include_background(xyls, FALSE); if (xcol) xylist_set_xname(xyls, xcol); if (ycol) xylist_set_yname(xyls, ycol); // write RDLS. rdls = rdlist_open_for_writing(rdlsfn); if (!rdls) { ERROR("Failed to open file %s to write RDLS.\n", rdlsfn); goto bailout; } if (rdlist_write_primary_header(rdls)) { ERROR("Failed to write header to RDLS file %s.\n", rdlsfn); goto bailout; } if (!fields) { alloced_fields = TRUE; fields = il_new(16); } if (!il_size(fields)) { // add all fields. int NF = xylist_n_fields(xyls); for (i=1; i<=NF; i++) il_append(fields, i); } logverb("Processing %zu extensions...\n", il_size(fields)); for (i=0; i<il_size(fields); i++) { int fieldind = il_get(fields, i); starxy_t xy; rd_t rd; int j; if (!xylist_read_field_num(xyls, fieldind, &xy)) { ERROR("Failed to read xyls file %s, field %i", xylsfn, fieldind); goto bailout; } if (rdlist_write_header(rdls)) { ERROR("Failed to write rdls field header to %s", rdlsfn); goto bailout; } rd_alloc_data(&rd, starxy_n(&xy)); for (j=0; j<starxy_n(&xy); j++) { double x, y, ra, dec; x = starxy_getx(&xy, j); y = starxy_gety(&xy, j); anwcs_pixelxy2radec(wcs, x, y, &ra, &dec); rd_setra (&rd, j, ra); rd_setdec(&rd, j, dec); } if (rdlist_write_field(rdls, &rd)) { ERROR("Failed to write rdls field to %s", rdlsfn); goto bailout; } rd_free_data(&rd); starxy_free_data(&xy); if (rdlist_fix_header(rdls)) { ERROR("Failed to fix rdls field header for %s", rdlsfn); goto bailout; } rdlist_next_field(rdls); } if (rdlist_fix_primary_header(rdls) || rdlist_close(rdls)) { ERROR("Failed to fix header of RDLS file %s", rdlsfn); goto bailout; } rdls = NULL; if (xylist_close(xyls)) { ERROR("Failed to close XYLS file %s", xylsfn); goto bailout; } xyls = NULL; rtn = 0; bailout: if (alloced_fields) il_free(fields); if (rdls) rdlist_close(rdls); if (xyls) xylist_close(xyls); if (wcs) anwcs_free(wcs); return rtn; }
int wcs_rd2xy(const char* wcsfn, int wcsext, const char* rdlsfn, const char* xylsfn, const char* racol, const char* deccol, anbool forcetan, anbool forcewcslib, il* fields) { xylist_t* xyls = NULL; rdlist_t* rdls = NULL; anwcs_t* wcs = NULL; int i; anbool alloced_fields = FALSE; int rtn = -1; // read WCS. if (forcewcslib) { wcs = anwcs_open_wcslib(wcsfn, wcsext); } else if (forcetan) { wcs = anwcs_open_tan(wcsfn, wcsext); } else { wcs = anwcs_open(wcsfn, wcsext); } if (!wcs) { ERROR("Failed to read WCS file \"%s\", extension %i", wcsfn, wcsext); return -1; } // read RDLS. rdls = rdlist_open(rdlsfn); if (!rdls) { ERROR("Failed to read an RA,Dec list from file %s", rdlsfn); goto bailout; } if (racol) rdlist_set_raname(rdls, racol); if (deccol) rdlist_set_decname(rdls, deccol); // write XYLS. xyls = xylist_open_for_writing(xylsfn); if (!xyls) { ERROR("Failed to open file %s to write XYLS", xylsfn); goto bailout; } if (xylist_write_primary_header(xyls)) { ERROR("Failed to write header to XYLS file %s", xylsfn); goto bailout; } if (!fields) { alloced_fields = TRUE; fields = il_new(16); } if (!il_size(fields)) { // add all fields. int NF = rdlist_n_fields(rdls); for (i=1; i<=NF; i++) il_append(fields, i); } for (i=0; i<il_size(fields); i++) { int fieldnum = il_get(fields, i); int j; starxy_t xy; rd_t rd; if (!rdlist_read_field_num(rdls, fieldnum, &rd)) { ERROR("Failed to read rdls file \"%s\" field %i", rdlsfn, fieldnum); goto bailout; } starxy_alloc_data(&xy, rd_n(&rd), FALSE, FALSE); if (xylist_write_header(xyls)) { ERROR("Failed to write xyls field header"); goto bailout; } for (j=0; j<rd_n(&rd); j++) { double x, y, ra, dec; ra = rd_getra (&rd, j); dec = rd_getdec(&rd, j); if (anwcs_radec2pixelxy(wcs, ra, dec, &x, &y)) { ERROR("Point RA,Dec = (%g,%g) projects to the opposite side of the sphere", ra, dec); starxy_set(&xy, j, NAN, NAN); continue; } starxy_set(&xy, j, x, y); } if (xylist_write_field(xyls, &xy)) { ERROR("Failed to write xyls field"); goto bailout; } if (xylist_fix_header(xyls)) { ERROR("Failed to fix xyls field header"); goto bailout; } xylist_next_field(xyls); starxy_free_data(&xy); rd_free_data(&rd); } if (xylist_fix_primary_header(xyls) || xylist_close(xyls)) { ERROR("Failed to fix header of XYLS file"); goto bailout; } xyls = NULL; if (rdlist_close(rdls)) { ERROR("Failed to close RDLS file"); goto bailout; } rdls = NULL; rtn = 0; bailout: if (alloced_fields) il_free(fields); if (rdls) rdlist_close(rdls); if (xyls) xylist_close(xyls); if (wcs) anwcs_free(wcs); return rtn; }
int main(int argc, char** args) { int c; char* rdlsfn = NULL; char* wcsfn = NULL; char* xylsfn = NULL; char* rcol = NULL; char* dcol = NULL; anbool forcetan = FALSE; il* fields; int ext = 0; double ra=HUGE_VAL, dec=HUGE_VAL; anbool wcslib = FALSE; int loglvl = LOG_MSG; fields = il_new(16); while ((c = getopt(argc, args, OPTIONS)) != -1) { switch (c) { case 'v': loglvl++; break; case 'L': wcslib = TRUE; break; case 'r': ra = atof(optarg); break; case 'd': dec = atof(optarg); break; case 'e': ext = atoi(optarg); break; case 'h': print_help(args[0]); exit(0); case 't': forcetan = TRUE; break; case 'o': xylsfn = optarg; break; case 'i': rdlsfn = optarg; break; case 'w': wcsfn = optarg; break; case 'f': il_append(fields, atoi(optarg)); break; case 'R': rcol = optarg; break; case 'D': dcol = optarg; break; } } log_init(loglvl); if (optind != argc) { print_help(args[0]); exit(-1); } if (!(wcsfn && ((rdlsfn && xylsfn) || ((ra != HUGE_VAL) && (dec != HUGE_VAL))))) { print_help(args[0]); exit(-1); } if (!rdlsfn) { double x,y; anwcs_t* wcs = NULL; // read WCS. if (wcslib) { wcs = anwcs_open_wcslib(wcsfn, ext); } else if (forcetan) { wcs = anwcs_open_tan(wcsfn, ext); } else { wcs = anwcs_open(wcsfn, ext); } if (!wcs) { ERROR("Failed to read WCS file"); exit(-1); } logverb("Read WCS:\n"); if (log_get_level() >= LOG_VERB) { anwcs_print(wcs, log_get_fid()); } // convert immediately. if (anwcs_radec2pixelxy(wcs, ra, dec, &x, &y)) { ERROR("The given RA,Dec is on the opposite side of the sky."); exit(-1); } printf("RA,Dec (%.10f, %.10f) -> pixel (%.10f, %.10f)\n", ra, dec, x, y); anwcs_free(wcs); exit(0); } if (wcs_rd2xy(wcsfn, ext, rdlsfn, xylsfn, rcol, dcol, forcetan, wcslib, fields)) { ERROR("wcs-rd2xy failed"); exit(-1); } 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; }
int main(int argc, char** args) { int c; char* rdlsfn = NULL; char* wcsfn = NULL; char* xylsfn = NULL; char* xcol = NULL; char* ycol = NULL; anbool forcetan = FALSE; anbool forcewcslib = FALSE; anbool forcewcstools = FALSE; anbool printhms = FALSE; il* fields; int ext = 0; double x, y; int loglvl = LOG_MSG; x = y = HUGE_VAL; fields = il_new(16); while ((c = getopt(argc, args, OPTIONS)) != -1) { switch (c) { case 'v': loglvl++; break; case 's': printhms = TRUE; break; case 'L': forcewcslib = TRUE; break; case 'T': forcewcstools = TRUE; break; case 'x': x = atof(optarg); break; case 'y': y = atof(optarg); break; case 'e': ext = atoi(optarg); break; case 'h': print_help(args[0]); exit(0); case 't': forcetan = TRUE; break; case 'o': rdlsfn = optarg; break; case 'i': xylsfn = optarg; break; case 'w': wcsfn = optarg; break; case 'f': il_append(fields, atoi(optarg)); break; case 'X': xcol = optarg; break; case 'Y': ycol = optarg; break; } } log_init(loglvl); if (optind != argc) { print_help(args[0]); exit(-1); } if (!(wcsfn && ((rdlsfn && xylsfn) || ((x != HUGE_VAL) && (y != HUGE_VAL))))) { print_help(args[0]); exit(-1); } if (!xylsfn) { double ra,dec; anwcs_t* wcs = NULL; // read WCS. if (forcewcslib) { wcs = anwcs_open_wcslib(wcsfn, ext); } else if (forcewcstools) { wcs = anwcs_open_wcstools(wcsfn, ext); } else if (forcetan) { wcs = anwcs_open_tan(wcsfn, ext); } else { wcs = anwcs_open(wcsfn, ext); } if (!wcs) { ERROR("Failed to read WCS file \"%s\", extension %i", wcsfn, ext); exit(-1); } logverb("Read WCS:\n"); if (log_get_level() >= LOG_VERB) { anwcs_print(wcs, log_get_fid()); } // convert immediately. anwcs_pixelxy2radec(wcs, x, y, &ra, &dec); printf("Pixel (%.10f, %.10f) -> RA,Dec (%.10f, %.10f)\n", x, y, ra, dec); if (printhms) { char str[32]; ra2hmsstring(ra, str); printf(" RA,Dec (%20s, ", str); dec2dmsstring(dec, str); printf("%20s)\n", str); } anwcs_free(wcs); exit(0); } if (wcs_xy2rd(wcsfn, ext, xylsfn, rdlsfn, xcol, ycol, forcetan, forcewcslib, fields)) { ERROR("wcs-xy2rd failed"); exit(-1); } return 0; }
int main(int argc, char** args) { int ext = 0,c; double ra,dec; double sol[2]; const gsl_multiroot_fsolver_type *T; gsl_multiroot_fsolver *s; int status; size_t iter=0; const size_t n=2; gsl_multiroot_function f={&fvec,n,NULL}; gsl_vector *x = gsl_vector_alloc(n); char *wcsfn1=NULL, *wcsfn2=NULL; while ((c = getopt(argc, args, OPTIONS)) != -1) { switch(c) { case 'v': loglvl++; break; case 'h': print_help(args[0]); exit(0); case '1': wcsfn1 = optarg; break; case '2': wcsfn2 = optarg; break; } } log_init(loglvl); if (optind != argc) { print_help(args[0]); exit(-1); } if (!(wcsfn1) || !(wcsfn2)) { print_help(args[0]); exit(-1); } /* open the two wcs systems */ wcs1 = anwcs_open(wcsfn1, ext); if (!wcs1) { ERROR("Failed to read WCS file"); exit(-1); } logverb("Read WCS:\n"); if (log_get_level() >= LOG_VERB) { anwcs_print(wcs1, log_get_fid()); } wcs2 = anwcs_open(wcsfn2, ext); if (!wcs2) { ERROR("Failed to read WCS file"); exit(-1); } logverb("Read WCS:\n"); if (log_get_level() >= LOG_VERB) { anwcs_print(wcs2, log_get_fid()); } /* setup the solver, start in the middle */ gsl_vector_set(x,0,anwcs_imagew(wcs1)/2.0); gsl_vector_set(x,1,anwcs_imageh(wcs1)/2.0); T = gsl_multiroot_fsolver_hybrids; s = gsl_multiroot_fsolver_alloc (T,2); gsl_multiroot_fsolver_set(s,&f,x); print_state(iter,s); do { iter++; status = gsl_multiroot_fsolver_iterate(s); print_state(iter,s); if (status) break; status = gsl_multiroot_test_residual(s->f,1e-7); } while (status == GSL_CONTINUE && iter < 1000); sol[0]=gsl_vector_get(s->x,0); sol[1]=gsl_vector_get(s->x,1); /* write some diagnostics on stderr */ /* transform to ra/dec */ anwcs_pixelxy2radec(wcs1, sol[0], sol[1], &ra, &dec); if (loglvl > LOG_MSG) fprintf(stderr,"Pixel (%.10f, %.10f) -> RA,Dec (%.10f, %.10f)\n", sol[0], sol[1], ra, dec); /* transform to x/y with second wcs center of rotation should stay the same x/y */ anwcs_radec2pixelxy(wcs2, ra, dec, &sol[0], &sol[1]); if (loglvl > LOG_MSG) fprintf(stderr,"RA,Dec (%.10f, %.10f) -> Pixel (%.10f, %.10f) \n", ra, dec, sol[0], sol[1]); /* write the solution */ fprintf(stdout,"%f\n",sol[0]); fprintf(stdout,"%f\n",sol[1]); return(0); }