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 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 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* 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; }