int sip_write_to_file(const sip_t* sip, const char* fn) { FILE* fid; int res; if ((sip->a_order == 0) && (sip->b_order == 0) && (sip->ap_order == 0) && (sip->bp_order == 0)) return tan_write_to_file(&(sip->wcstan), fn); fid = fopen(fn, "wb"); if (!fid) { SYSERROR("Failed to open file \"%s\" to write WCS header", fn); return -1; } res = sip_write_to(sip, fid); if (res) { ERROR("Failed to write FITS header to file \"%s\"", fn); return -1; } if (fclose(fid)) { SYSERROR("Failed to close file \"%s\" after writing WCS header", fn); return -1; } return 0; }
int main(int argc, char** args) { int c; dl* xys = dl_new(16); dl* radecs = dl_new(16); dl* otherradecs = dl_new(16); double* xy; double* xyz; int i, N; tan_t tan, tan2, tan3; int W=0, H=0; double crpix[] = { HUGE_VAL, HUGE_VAL }; int loglvl = LOG_MSG; FILE* logstream = stderr; int order = 1; while ((c = getopt(argc, args, OPTIONS)) != -1) { switch (c) { case 'v': loglvl++; break; case 'h': exit(0); case 'o': order = atoi(optarg); break; case 'W': W = atoi(optarg); break; case 'H': H = atoi(optarg); break; case 'X': crpix[0] = atof(optarg); break; case 'Y': crpix[1] = atof(optarg); break; } } if (optind != argc) { exit(-1); } log_init(loglvl); log_to(logstream); errors_log_to(logstream); if (W == 0 || H == 0) { logerr("Need -W, -H\n"); exit(-1); } if (crpix[0] == HUGE_VAL) crpix[0] = W/2.0; if (crpix[1] == HUGE_VAL) crpix[1] = H/2.0; while (1) { double x,y,ra,dec; if (fscanf(stdin, "%lf %lf %lf %lf\n", &x, &y, &ra, &dec) < 4) break; if (x == -1 && y == -1) { dl_append(otherradecs, ra); dl_append(otherradecs, dec); } else { dl_append(xys, x); dl_append(xys, y); dl_append(radecs, ra); dl_append(radecs, dec); } } logmsg("Read %i x,y,ra,dec tuples\n", dl_size(xys)/2); N = dl_size(xys)/2; xy = dl_to_array(xys); xyz = malloc(3 * N * sizeof(double)); for (i=0; i<N; i++) radecdeg2xyzarr(dl_get(radecs, 2*i), dl_get(radecs, 2*i+1), xyz + i*3); dl_free(xys); dl_free(radecs); fit_tan_wcs(xyz, xy, N, &tan, NULL); tan.imagew = W; tan.imageh = H; logmsg("Computed TAN WCS:\n"); tan_print_to(&tan, logstream); sip_t* sip; { tweak_t* t = tweak_new(); starxy_t* sxy = starxy_new(N, FALSE, FALSE); il* imginds = il_new(256); il* refinds = il_new(256); for (i=0; i<N; i++) { starxy_set_x(sxy, i, xy[2*i+0]); starxy_set_y(sxy, i, xy[2*i+1]); } tweak_init(t); tweak_push_ref_xyz(t, xyz, N); tweak_push_image_xy(t, sxy); for (i=0; i<N; i++) { il_append(imginds, i); il_append(refinds, i); } // unweighted; no dist2s tweak_push_correspondence_indices(t, imginds, refinds, NULL, NULL); tweak_push_wcs_tan(t, &tan); t->sip->a_order = t->sip->b_order = t->sip->ap_order = t->sip->bp_order = order; for (i=0; i<10; i++) { // go to TWEAK_HAS_LINEAR_CD -> do_sip_tweak // t->image has the indices of corresponding image stars // t->ref has the indices of corresponding catalog stars tweak_go_to(t, TWEAK_HAS_LINEAR_CD); logmsg("\n"); sip_print(t->sip); t->state &= ~TWEAK_HAS_LINEAR_CD; } tan_write_to_file(&t->sip->wcstan, "kt1.wcs"); sip = t->sip; } for (i=0; i<dl_size(otherradecs)/2; i++) { double ra, dec, x,y; ra = dl_get(otherradecs, 2*i); dec = dl_get(otherradecs, 2*i+1); if (!sip_radec2pixelxy(sip, ra, dec, &x, &y)) { logerr("Not in tangent plane: %g,%g\n", ra, dec); exit(-1); //continue; } printf("%g %g\n", x, y); } /* blind_wcs_move_tangent_point(xyz, xy, N, crpix, &tan, &tan2); blind_wcs_move_tangent_point(xyz, xy, N, crpix, &tan2, &tan3); logmsg("Moved tangent point to (%g,%g):\n", crpix[0], crpix[1]); tan_print_to(&tan3, logstream); tan_write_to_file(&tan, "kt1.wcs"); tan_write_to_file(&tan3, "kt2.wcs"); */ dl_free(otherradecs); free(xy); free(xyz); return 0; }
int main(int argc, char** args) { int c; char* xylsfn = NULL; char* rdlsfn = NULL; char* corrfn = NULL; char* outfn = NULL; char* xcol = NULL; char* ycol = NULL; char* rcol = NULL; char* dcol = NULL; xylist_t* xyls = NULL; rdlist_t* rdls = NULL; rd_t rd; starxy_t xy; int fieldnum = 1; int N; double* fieldxy = NULL; double* xyz = NULL; sip_t wcs; int rtn = -1; int loglvl = LOG_MSG; int siporder = 0; int W=0, H=0; anbool crpix_center = FALSE; int i; int doshift = 1; fits_use_error_system(); while ((c = getopt(argc, args, OPTIONS)) != -1) { switch (c) { case 'h': print_help(args[0]); exit(0); case 'W': W = atoi(optarg); break; case 'H': H = atoi(optarg); break; case 'C': crpix_center = TRUE; break; case 's': siporder = atoi(optarg); break; case 'c': corrfn = optarg; break; case 'r': rdlsfn = optarg; break; case 'R': rcol = optarg; break; case 'D': dcol = optarg; break; case 'x': xylsfn = optarg; break; case 'X': xcol = optarg; break; case 'Y': ycol = optarg; break; case 'o': outfn = optarg; break; case 'v': loglvl++; break; } } if (optind != argc) { print_help(args[0]); exit(-1); } if (! ((xylsfn && rdlsfn) || corrfn) || !outfn) { print_help(args[0]); exit(-1); } log_init(loglvl); if (corrfn) { xylsfn = corrfn; rdlsfn = corrfn; if (!xcol) xcol = "FIELD_X"; if (!ycol) ycol = "FIELD_Y"; if (!rcol) rcol = "INDEX_RA"; if (!dcol) dcol = "INDEX_DEC"; } // 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); // read RDLS. rdls = rdlist_open(rdlsfn); if (!rdls) { ERROR("Failed to read an RA,Dec list from file %s", rdlsfn); goto bailout; } if (rcol) rdlist_set_raname(rdls, rcol); if (dcol) rdlist_set_decname(rdls, dcol); if (!xylist_read_field_num(xyls, fieldnum, &xy)) { ERROR("Failed to read xyls file %s, field %i", xylsfn, fieldnum); goto bailout; } if (!rdlist_read_field_num(rdls, fieldnum, &rd)) { ERROR("Failed to read rdls field %i", fieldnum); goto bailout; } N = starxy_n(&xy); if (rd_n(&rd) != N) { ERROR("X,Y list and RA,Dec list must have the same number of entries, " "but found %i vs %i", N, rd_n(&rd)); goto bailout; } logverb("Read %i points from %s and %s\n", N, rdlsfn, xylsfn); xyz = (double*)malloc(sizeof(double) * 3 * N); if (!xyz) { ERROR("Failed to allocate %i xyz coords", N); goto bailout; } radecdeg2xyzarrmany(rd.ra, rd.dec, xyz, N); fieldxy = starxy_to_xy_array(&xy, NULL); if (!fieldxy) { ERROR("Failed to allocate %i xy coords", N); goto bailout; } logverb("Fitting WCS\n"); if (siporder == 0) { if (fit_tan_wcs(xyz, fieldxy, N, &(wcs.wcstan), NULL)) { ERROR("Failed to fit for TAN WCS"); goto bailout; } } else { if (W == 0) { for (i=0; i<N; i++) { W = MAX(W, (int)ceil(fieldxy[2*i + 0])); } } if (H == 0) { for (i=0; i<N; i++) { H = MAX(H, (int)ceil(fieldxy[2*i + 1])); } } logverb("Image size = %i x %i pix\n", W, H); fit_sip_wcs_2(xyz, fieldxy, NULL, N, siporder, siporder+1, W, H, crpix_center, NULL, doshift, &wcs); } if (siporder <= 1) { if (tan_write_to_file(&(wcs.wcstan), outfn)) { ERROR("Failed to write TAN WCS header to file \"%s\"", outfn); goto bailout; } } else { if (sip_write_to_file(&wcs, outfn)) { ERROR("Failed to write SIP WCS header to file \"%s\"", outfn); goto bailout; } } logverb("Wrote WCS to %s\n", outfn); starxy_free_data(&xy); rd_free_data(&rd); rtn = 0; bailout: if (rdls) rdlist_close(rdls); if (xyls) xylist_close(xyls); if (fieldxy) free(fieldxy); if (xyz) free(xyz); return rtn; }