static anbool is_duplicate(int hp, double ra, double dec, int Nside, intmap_t* starlists, double* ras, double* decs, double dedupr2) { double xyz[3]; int neigh[9]; int nn; double xyz2[3]; int k; size_t j; radecdeg2xyzarr(ra, dec, xyz); // Check this healpix... neigh[0] = hp; // Check neighbouring healpixes... (+1 is to skip over this hp) nn = 1 + healpix_get_neighbours(hp, neigh+1, Nside); for (k=0; k<nn; k++) { int otherhp = neigh[k]; bl* lst = intmap_find(starlists, otherhp, FALSE); if (!lst) continue; for (j=0; j<bl_size(lst); j++) { int otherindex; bl_get(lst, j, &otherindex); radecdeg2xyzarr(ras[otherindex], decs[otherindex], xyz2); if (!distsq_exceeds(xyz, xyz2, 3, dedupr2)) return TRUE; } } return FALSE; }
double distsq_between_radecdeg(double ra1, double dec1, double ra2, double dec2) { double xyz1[3]; double xyz2[3]; radecdeg2xyzarr(ra1, dec1, xyz1); radecdeg2xyzarr(ra2, dec2, xyz2); return distsq(xyz1, xyz2, 3); }
bl* henry_draper_get(hd_catalog_t* hdcat, double racenter, double deccenter, double r_arcsec) { double r2; double xyz[3]; kdtree_qres_t* q; bl* res; int i; hd_entry_t hd; radecdeg2xyzarr(racenter, deccenter, xyz); r2 = arcsec2distsq(r_arcsec); q = kdtree_rangesearch(hdcat->kd, xyz, r2); if (!q) { return NULL; } res = bl_new(256, sizeof(hd_entry_t)); for (i=0; i<q->nres; i++) { double* pt = q->results.d + i*3; xyzarr2radecdeg(pt, &(hd.ra), &(hd.dec)); hd.hd = q->inds[i] + 1; bl_append(res, &hd); } kdtree_free_query(q); return res; }
void startree_search_for_radec(const startree_t* s, double ra, double dec, double radius, double** xyzresults, double** radecresults, int** starinds, int* nresults) { double xyz[3]; double r2; radecdeg2xyzarr(ra, dec, xyz); r2 = deg2distsq(radius); startree_search_for(s, xyz, r2, xyzresults, radecresults, starinds, nresults); }
anbool tan_xyzarr2iwc(const tan_t* tan, const double* xyz, double* iwcx, double* iwcy) { double xyzcrval[3]; // FIXME be robust near the poles // Calculate intermediate world coordinates (x,y) on the tangent plane radecdeg2xyzarr(tan->crval[0], tan->crval[1], xyzcrval); if (!star_coords(xyz, xyzcrval, !tan->sin, iwcx, iwcy)) return FALSE; *iwcx = rad2deg(*iwcx); *iwcy = rad2deg(*iwcy); return TRUE; }
int fit_sip_wcs(const double* starxyz, const double* fieldxy, const double* weights, int M, const tan_t* tanin1, int sip_order, int inv_order, sip_t* sipout) { int sip_coeffs; double xyzcrval[3]; double cdinv[2][2]; double sx, sy, sU, sV, su, sv; int N; int i, j, p, q, order; double totalweight; int rtn; gsl_matrix *mA; gsl_vector *b1, *b2, *x1, *x2; gsl_vector *r1=NULL, *r2=NULL; tan_t tanin2; int ngood; const tan_t* tanin = &tanin2; // We need at least the linear terms to compute CD. if (sip_order < 1) sip_order = 1; // convenience: allow the user to call like: // fit_sip_wcs(... &(sipout.wcstan), ..., sipout); memcpy(&tanin2, tanin1, sizeof(tan_t)); memset(sipout, 0, sizeof(sip_t)); memcpy(&(sipout->wcstan), tanin, sizeof(tan_t)); sipout->a_order = sipout->b_order = sip_order; sipout->ap_order = sipout->bp_order = inv_order; // The SIP coefficients form an (order x order) upper triangular // matrix missing the 0,0 element. sip_coeffs = (sip_order + 1) * (sip_order + 2) / 2; N = sip_coeffs; if (M < N) { ERROR("Too few correspondences for the SIP order specified (%i < %i)\n", M, N); return -1; } mA = gsl_matrix_alloc(M, N); b1 = gsl_vector_alloc(M); b2 = gsl_vector_alloc(M); assert(mA); assert(b1); assert(b2); /* * We use a clever trick to estimate CD, A, and B terms in two * seperated least squares fits, then finding A and B by multiplying * the found parameters by CD inverse. * * Rearranging the SIP equations (see sip.h) we get the following * matrix operation to compute x and y in world intermediate * coordinates, which is convienently written in a way which allows * least squares estimation of CD and terms related to A and B. * * First use the x's to find the first set of parametetrs * * +--------------------- Intermediate world coordinates in DEGREES * | +--------- Pixel coordinates u and v in PIXELS * | | +--- Polynomial u,v terms in powers of PIXELS * v v v * ( x1 ) ( 1 u1 v1 p1 ) (sx ) * ( x2 ) = ( 1 u2 v2 p2 ) * (cd11 ) : * ( x3 ) ( 1 u3 v3 p3 ) (cd12 ) : * ( ...) ( ... ) (cd11*A + cd12*B ) : * cd11 is a scalar, degrees per pixel * cd12 is a scalar, degrees per pixel * cd11*A and cs12*B are mixture of SIP terms (A,B) and CD matrix * (cd11,cd12) * * Then find cd21 and cd22 with the y's * * ( y1 ) ( 1 u1 v1 p1 ) (sy ) * ( y2 ) = ( 1 u2 v2 p2 ) * (cd21 ) : * ( y3 ) ( 1 u3 v3 p3 ) (cd22 ) : * ( ...) ( ... ) (cd21*A + cd22*B ) : (Y4) * y2: scalar, degrees per pixel * y3: scalar, degrees per pixel * Y4: mixture of SIP terms (A,B) and CD matrix (cd21,cd22) * * These are both standard least squares problems which we solve with * QR decomposition, ie * min_{cd,A,B} || x - [1,u,v,p]*[s;cd;cdA+cdB]||^2 with * x reference, cd,A,B unrolled parameters. * * We get back (for x) a vector of optimal * [sx;cd11;cd12; cd11*A + cd12*B] * Now we can pull out sx, cd11 and cd12 from the beginning of this vector, * and call the rest of the vector [cd11*A] + [cd12*B]; * similarly for the y fit, we get back a vector of optimal * [sy;cd21;cd22; cd21*A + cd22*B] * once we have all those we can figure out A and B as follows * -1 * A' = [cd11 cd12] * [cd11*A' + cd12*B'] * B' [cd21 cd22] [cd21*A' + cd22*B'] * * which recovers the A and B's. * */ /* * Dustin's interpretation of the above: * We want to solve: * * min || b[M-by-1] - A[M-by-N] x[N-by-1] ||_2 * * M = the number of correspondences. * N = the number of SIP terms. * * And we want an overdetermined system, so M >= N. * * [ 1 u_1 v_1 u_1^2 u_1 v_1 v_1^2 ... ] * mA = [ 1 u_2 v_2 u_2^2 u_2 v_2 v_2^2 ... ] * [ ...... ] * * Where (u_i, v_i) are *undistorted* pixel positions minus CRPIX. * * The answers we want are: * * [ sx ] * x1 = [ cd11 ] * [ cd12 ] * [ (A) (B) ] * [ cd11*(A) + cd12*(B) ] * [ (A) (B) ] * * [ sy ] * x2 = [ cd21 ] * [ cd22 ] * [ (A) (B) ] * [ cd21*(A) + cd22*(B) ] * [ (A) (B) ] * * And the target vectors are the intermediate world coords of the * reference stars, in degrees. * * [ ix_1 ] * b1 = [ ix_2 ] * [ ... ] * * [ iy_1 ] * b2 = [ iy_2 ] * [ ... ] * * * (where A and B are tall vectors of SIP coefficients of order 2 * and above) * */ // Fill in matrix mA: radecdeg2xyzarr(tanin->crval[0], tanin->crval[1], xyzcrval); totalweight = 0.0; ngood = 0; for (i=0; i<M; i++) { double x=0, y=0; double weight = 1.0; double u; double v; Unused anbool ok; u = fieldxy[2*i + 0] - tanin->crpix[0]; v = fieldxy[2*i + 1] - tanin->crpix[1]; // B contains Intermediate World Coordinates (in degrees) // tangent-plane projection ok = star_coords(starxyz + 3*i, xyzcrval, TRUE, &x, &y); if (!ok) { logverb("Skipping star that cannot be projected to tangent plane\n"); continue; } gsl_vector_set(b1, ngood, weight * rad2deg(x)); gsl_vector_set(b2, ngood, weight * rad2deg(y)); if (weights) { weight = weights[i]; assert(weight >= 0.0); assert(weight <= 1.0); totalweight += weight; if (weight == 0.0) continue; } /* The coefficients are stored in this order: * p q * (0,0) = 1 <- order 0 * (1,0) = u <- order 1 * (0,1) = v * (2,0) = u^2 <- order 2 * (1,1) = uv * (0,2) = v^2 * ... */ j = 0; for (order=0; order<=sip_order; order++) { for (q=0; q<=order; q++) { p = order - q; assert(j >= 0); assert(j < N); assert(p >= 0); assert(q >= 0); assert(p + q <= sip_order); gsl_matrix_set(mA, ngood, j, weight * pow(u, (double)p) * pow(v, (double)q)); j++; } } assert(j == N); // The shift - aka (0,0) - SIP coefficient must be 1. assert(gsl_matrix_get(mA, i, 0) == 1.0 * weight); assert(fabs(gsl_matrix_get(mA, i, 1) - u * weight) < 1e-12); assert(fabs(gsl_matrix_get(mA, i, 2) - v * weight) < 1e-12); ngood++; } if (ngood == 0) { ERROR("No stars projected within the image\n"); return -1; } if (weights) logverb("Total weight: %g\n", totalweight); if (ngood < M) { _gsl_vector_view sub_b1 = gsl_vector_subvector(b1, 0, ngood); _gsl_vector_view sub_b2 = gsl_vector_subvector(b2, 0, ngood); _gsl_matrix_view sub_mA = gsl_matrix_submatrix(mA, 0, 0, ngood, N); rtn = gslutils_solve_leastsquares_v(&(sub_mA.matrix), 2, &(sub_b1.vector), &x1, NULL, &(sub_b2.vector), &x2, NULL); } else { // Solve the equation. rtn = gslutils_solve_leastsquares_v(mA, 2, b1, &x1, NULL, b2, &x2, NULL); } if (rtn) { ERROR("Failed to solve SIP matrix equation!"); return -1; } // Row 0 of X are the shift (p=0, q=0) terms. // Row 1 of X are the terms that multiply "u". // Row 2 of X are the terms that multiply "v". // Grab CD. sipout->wcstan.cd[0][0] = gsl_vector_get(x1, 1); sipout->wcstan.cd[0][1] = gsl_vector_get(x1, 2); sipout->wcstan.cd[1][0] = gsl_vector_get(x2, 1); sipout->wcstan.cd[1][1] = gsl_vector_get(x2, 2); // Compute inv(CD) i = invert_2by2_arr((const double*)(sipout->wcstan.cd), (double*)cdinv); assert(i == 0); // Grab the shift. sx = gsl_vector_get(x1, 0); sy = gsl_vector_get(x2, 0); // Extract the SIP coefficients. // (this includes the 0 and 1 order terms, which we later overwrite) j = 0; for (order=0; order<=sip_order; order++) { for (q=0; q<=order; q++) { p = order - q; assert(j >= 0); assert(j < N); assert(p >= 0); assert(q >= 0); assert(p + q <= sip_order); sipout->a[p][q] = cdinv[0][0] * gsl_vector_get(x1, j) + cdinv[0][1] * gsl_vector_get(x2, j); sipout->b[p][q] = cdinv[1][0] * gsl_vector_get(x1, j) + cdinv[1][1] * gsl_vector_get(x2, j); j++; } } assert(j == N); // We have already dealt with the shift and linear terms, so zero them out // in the SIP coefficient matrix. sipout->a[0][0] = 0.0; sipout->a[0][1] = 0.0; sipout->a[1][0] = 0.0; sipout->b[0][0] = 0.0; sipout->b[0][1] = 0.0; sipout->b[1][0] = 0.0; sip_compute_inverse_polynomials(sipout, 0, 0, 0, 0, 0, 0); sU = cdinv[0][0] * sx + cdinv[0][1] * sy; sV = cdinv[1][0] * sx + cdinv[1][1] * sy; logverb("Applying shift of sx,sy = %g,%g deg (%g,%g pix) to CRVAL and CD.\n", sx, sy, sU, sV); sip_calc_inv_distortion(sipout, sU, sV, &su, &sv); debug("sx = %g, sy = %g\n", sx, sy); debug("sU = %g, sV = %g\n", sU, sV); debug("su = %g, sv = %g\n", su, sv); wcs_shift(&(sipout->wcstan), -su, -sv); if (r1) gsl_vector_free(r1); if (r2) gsl_vector_free(r2); gsl_matrix_free(mA); gsl_vector_free(b1); gsl_vector_free(b2); gsl_vector_free(x1); gsl_vector_free(x2); return 0; }
int main(int argc, char** args) { int c; char* wcsfn = NULL; char* outfn = NULL; char* infn = NULL; sip_t sip; double scale = 1.0; anbool pngformat = TRUE; char* hdpath = NULL; anbool HD = FALSE; cairos_t thecairos; cairos_t* cairos = &thecairos; cairo_surface_t* target = NULL; cairo_t* cairot = NULL; cairo_surface_t* surfbg = NULL; cairo_t* cairobg = NULL; cairo_surface_t* surfshapes = NULL; cairo_t* cairoshapes = NULL; cairo_surface_t* surfshapesmask = NULL; cairo_t* cairoshapesmask = NULL; cairo_surface_t* surffg = NULL; cairo_t* cairo = NULL; double lw = 2.0; // circle linewidth. double cw = 2.0; double ngc_fraction = 0.02; // NGC linewidth double nw = 2.0; // leave a gap short of connecting the points. double endgap = 5.0; // circle radius. double crad = endgap; double fontsize = 14.0; double label_offset = 15.0; int W = 0, H = 0; unsigned char* img = NULL; anbool NGC = FALSE, constell = FALSE; anbool bright = FALSE; anbool common_only = FALSE; anbool print_common_only = FALSE; int Nbright = 0; double ra, dec, px, py; int i, N; anbool justlist = FALSE; anbool only_messier = FALSE; anbool grid = FALSE; double gridspacing = 0.0; double gridcolor[3] = { 0.2, 0.2, 0.2 }; int loglvl = LOG_MSG; char halign = 'L'; char valign = 'C'; sl* json = NULL; anbool whitetext = FALSE; while ((c = getopt(argc, args, OPTIONS)) != -1) { switch (c) { case 'V': valign = optarg[0]; break; case 'O': halign = optarg[0]; break; case 'F': ngc_fraction = atof(optarg); break; case 'h': print_help(args[0]); exit(0); case 'J': json = sl_new(4); break; case 'G': gridspacing = atof(optarg); break; case 'g': { char *tail = NULL; gridcolor[0] = strtod(optarg,&tail); if (*tail) { tail++; gridcolor[1] = strtod(tail,&tail); } if (*tail) { tail++; gridcolor[2] = strtod(tail,&tail); } } break; case 'D': HD = TRUE; break; case 'd': hdpath = optarg; break; case 'M': only_messier = TRUE; break; case 'n': nw = atof(optarg); break; case 'f': fontsize = atof(optarg); break; case 'L': justlist = TRUE; outfn = NULL; break; case 'x': whitetext = TRUE; break; case 'v': loglvl++; break; break; case 'j': print_common_only = TRUE; break; case 'c': common_only = TRUE; break; case 'b': Nbright = atoi(optarg); break; case 'B': bright = TRUE; break; case 'N': NGC = TRUE; break; case 'C': constell = TRUE; break; case 'p': pngformat = FALSE; break; case 's': scale = atof(optarg); break; case 'o': outfn = optarg; break; case 'i': infn = optarg; break; case 'w': wcsfn = optarg; break; case 'W': W = atoi(optarg); break; case 'H': H = atoi(optarg); break; } } log_init(loglvl); log_to(stderr); fits_use_error_system(); if (optind != argc) { print_help(args[0]); exit(-1); } if (!(outfn || justlist) || !wcsfn) { logerr("Need (-o or -L) and -w args.\n"); print_help(args[0]); exit(-1); } // read WCS. logverb("Trying to parse SIP/TAN header from %s...\n", wcsfn); if (!file_exists(wcsfn)) { ERROR("No such file: \"%s\"", wcsfn); exit(-1); } if (sip_read_header_file(wcsfn, &sip)) { logverb("Got SIP header.\n"); } else { ERROR("Failed to parse SIP/TAN header from %s", wcsfn); exit(-1); } if (!(NGC || constell || bright || HD || grid)) { logerr("Neither constellations, bright stars, HD nor NGC/IC overlays selected!\n"); print_help(args[0]); exit(-1); } if (gridspacing > 0.0) grid = TRUE; // adjust for scaling... lw /= scale; cw /= scale; nw /= scale; crad /= scale; endgap /= scale; fontsize /= scale; label_offset /= scale; if (!W || !H) { W = sip.wcstan.imagew; H = sip.wcstan.imageh; } if (!(infn || (W && H))) { logerr("Image width/height unspecified, and no input image given.\n"); exit(-1); } if (infn) { cairoutils_fake_ppm_init(); img = cairoutils_read_ppm(infn, &W, &H); if (!img) { ERROR("Failed to read input image %s", infn); exit(-1); } cairoutils_rgba_to_argb32(img, W, H); } else if (!justlist) { // Allocate a black image. img = calloc(4 * W * H, 1); if (!img) { SYSERROR("Failed to allocate a blank image on which to plot!"); exit(-1); } } if (HD && !hdpath) { logerr("If you specify -D (plot Henry Draper objs), you also have to give -d (path to Henry Draper catalog)\n"); exit(-1); } if (!justlist) { /* Cairo layers: -background: surfbg / cairobg --> gets drawn first, in black, masked by surfshapesmask -shapes: surfshapes / cairoshapes --> gets drawn second, masked by surfshapesmask -foreground/text: surffg / cairo --> gets drawn last. */ surffg = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, W, H); cairo = cairo_create(surffg); cairo_set_line_join(cairo, CAIRO_LINE_JOIN_BEVEL); cairo_set_antialias(cairo, CAIRO_ANTIALIAS_GRAY); cairo_set_source_rgba(cairo, 1.0, 1.0, 1.0, 1.0); cairo_scale(cairo, scale, scale); //cairo_select_font_face(cairo, "helvetica", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_select_font_face(cairo, "DejaVu Sans Mono Book", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size(cairo, fontsize); surfshapes = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, W, H); cairoshapes = cairo_create(surfshapes); cairo_set_line_join(cairoshapes, CAIRO_LINE_JOIN_BEVEL); cairo_set_antialias(cairoshapes, CAIRO_ANTIALIAS_GRAY); cairo_set_source_rgba(cairoshapes, 1.0, 1.0, 1.0, 1.0); cairo_scale(cairoshapes, scale, scale); cairo_select_font_face(cairoshapes, "DejaVu Sans Mono Book", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size(cairoshapes, fontsize); surfshapesmask = cairo_image_surface_create(CAIRO_FORMAT_A8, W, H); cairoshapesmask = cairo_create(surfshapesmask); cairo_set_line_join(cairoshapesmask, CAIRO_LINE_JOIN_BEVEL); cairo_set_antialias(cairoshapesmask, CAIRO_ANTIALIAS_GRAY); cairo_set_source_rgba(cairoshapesmask, 1.0, 1.0, 1.0, 1.0); cairo_scale(cairoshapesmask, scale, scale); cairo_select_font_face(cairoshapesmask, "DejaVu Sans Mono Book", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size(cairoshapesmask, fontsize); cairo_paint(cairoshapesmask); cairo_stroke(cairoshapesmask); surfbg = cairo_image_surface_create(CAIRO_FORMAT_A8, W, H); cairobg = cairo_create(surfbg); cairo_set_line_join(cairobg, CAIRO_LINE_JOIN_BEVEL); cairo_set_antialias(cairobg, CAIRO_ANTIALIAS_GRAY); cairo_set_source_rgba(cairobg, 0, 0, 0, 1); cairo_scale(cairobg, scale, scale); cairo_select_font_face(cairobg, "DejaVu Sans Mono Book", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size(cairobg, fontsize); cairos->bg = cairobg; cairos->fg = cairo; cairos->shapes = cairoshapes; cairos->shapesmask = cairoshapesmask; cairos->imgW = (float)W/scale; cairos->imgH = (float)H/scale; // } if (grid) { double ramin, ramax, decmin, decmax; double ra, dec; double rastep = gridspacing / 60.0; double decstep = gridspacing / 60.0; // how many line segments int N = 10; double px, py; int i; cairo_set_source_rgba(cairo, gridcolor[0], gridcolor[1], gridcolor[2], 1.0); sip_get_radec_bounds(&sip, 100, &ramin, &ramax, &decmin, &decmax); logverb("Plotting grid lines from RA=%g to %g in steps of %g; Dec=%g to %g in steps of %g\n", ramin, ramax, rastep, decmin, decmax, decstep); for (dec = decstep * floor(decmin / decstep); dec<=decmax; dec+=decstep) { logverb(" dec=%g\n", dec); for (i=0; i<=N; i++) { ra = ramin + ((double)i / (double)N) * (ramax - ramin); if (!sip_radec2pixelxy(&sip, ra, dec, &px, &py)) continue; // first time, move_to; else line_to ((ra == ramin) ? cairo_move_to : cairo_line_to)(cairo, px, py); } cairo_stroke(cairo); } for (ra = rastep * floor(ramin / rastep); ra <= ramax; ra += rastep) { //for (dec=decmin; dec<=decmax; dec += (decmax - decmin)/(double)N) { logverb(" ra=%g\n", ra); for (i=0; i<=N; i++) { dec = decmin + ((double)i / (double)N) * (decmax - decmin); if (!sip_radec2pixelxy(&sip, ra, dec, &px, &py)) continue; // first time, move_to; else line_to ((dec == decmin) ? cairo_move_to : cairo_line_to)(cairo, px, py); } cairo_stroke(cairo); } cairo_set_source_rgba(cairo, 1.0, 1.0, 1.0, 1.0); } } if (constell) { N = constellations_n(); logverb("Checking %i constellations.\n", N); for (c=0; c<N; c++) { const char* shortname = NULL; const char* longname; il* lines; il* uniqstars; il* inboundstars; float r,g,b; int Ninbounds; int Nunique; cairo_text_extents_t textents; double cmass[3]; uniqstars = constellations_get_unique_stars(c); inboundstars = il_new(16); Nunique = il_size(uniqstars); debug("%s: %zu unique stars.\n", shortname, il_size(uniqstars)); // Count the number of unique stars belonging to this contellation // that are within the image bounds Ninbounds = 0; for (i=0; i<il_size(uniqstars); i++) { int star; star = il_get(uniqstars, i); constellations_get_star_radec(star, &ra, &dec); debug("star %i: ra,dec (%g,%g)\n", il_get(uniqstars, i), ra, dec); if (!sip_radec2pixelxy(&sip, ra, dec, &px, &py)) continue; if (px < 0 || py < 0 || px*scale > W || py*scale > H) continue; Ninbounds++; il_append(inboundstars, star); } il_free(uniqstars); debug("%i are in-bounds.\n", Ninbounds); // Only draw this constellation if at least 2 of its stars // are within the image bounds. if (Ninbounds < 2) { il_free(inboundstars); continue; } // Set the color based on the location of the first in-bounds star. // This is a hack -- we have two different constellation // definitions with different numbering schemes! if (!justlist && (il_size(inboundstars) > 0)) { // This is helpful for videos: ensuring that the same // color is chosen for a constellation in each frame. int star = il_get(inboundstars, 0); constellations_get_star_radec(star, &ra, &dec); if (whitetext) { r = g = b = 1; } else { color_for_radec(ra, dec, &r, &g, &b); } cairo_set_source_rgba(cairoshapes, r,g,b,0.8); cairo_set_line_width(cairoshapes, cw); cairo_set_source_rgba(cairo, r,g,b,0.8); cairo_set_line_width(cairo, cw); } // Draw circles around each star. // Find center of mass (of the in-bounds stars) cmass[0] = cmass[1] = cmass[2] = 0.0; for (i=0; i<il_size(inboundstars); i++) { double xyz[3]; int star = il_get(inboundstars, i); constellations_get_star_radec(star, &ra, &dec); if (!sip_radec2pixelxy(&sip, ra, dec, &px, &py)) continue; if (px < 0 || py < 0 || px*scale > W || py*scale > H) continue; if (!justlist) { cairo_arc(cairobg, px, py, crad+1.0, 0.0, 2.0*M_PI); cairo_stroke(cairobg); cairo_arc(cairoshapes, px, py, crad, 0.0, 2.0*M_PI); cairo_stroke(cairoshapes); } radecdeg2xyzarr(ra, dec, xyz); cmass[0] += xyz[0]; cmass[1] += xyz[1]; cmass[2] += xyz[2]; } cmass[0] /= il_size(inboundstars); cmass[1] /= il_size(inboundstars); cmass[2] /= il_size(inboundstars); xyzarr2radecdeg(cmass, &ra, &dec); il_free(inboundstars); if (!sip_radec2pixelxy(&sip, ra, dec, &px, &py)) continue; shortname = constellations_get_shortname(c); longname = constellations_get_longname(c); assert(shortname && longname); logverb("%s at (%g, %g)\n", longname, px, py); if (Ninbounds == Nunique) { printf("The constellation %s (%s)\n", longname, shortname); } else { printf("Part of the constellation %s (%s)\n", longname, shortname); } if (justlist) continue; // If the label will be off-screen, move it back on. cairo_text_extents(cairo, shortname, &textents); if (px < 0) px = 0; if (py < textents.height) py = textents.height; if ((px + textents.width)*scale > W) px = W/scale - textents.width; if ((py+textents.height)*scale > H) py = H/scale - textents.height; logverb("%s at (%g, %g)\n", shortname, px, py); add_text(cairos, longname, px, py, halign, valign); // Draw the lines. cairo_set_line_width(cairo, lw); lines = constellations_get_lines(c); for (i=0; i<il_size(lines)/2; i++) { int star1, star2; double ra1, dec1, ra2, dec2; double px1, px2, py1, py2; double dx, dy; double dist; double gapfrac; star1 = il_get(lines, i*2+0); star2 = il_get(lines, i*2+1); constellations_get_star_radec(star1, &ra1, &dec1); constellations_get_star_radec(star2, &ra2, &dec2); if (!sip_radec2pixelxy(&sip, ra1, dec1, &px1, &py1) || !sip_radec2pixelxy(&sip, ra2, dec2, &px2, &py2)) continue; dx = px2 - px1; dy = py2 - py1; dist = hypot(dx, dy); gapfrac = endgap / dist; cairo_move_to(cairoshapes, px1 + dx*gapfrac, py1 + dy*gapfrac); cairo_line_to(cairoshapes, px1 + dx*(1.0-gapfrac), py1 + dy*(1.0-gapfrac)); cairo_stroke(cairoshapes); } il_free(lines); } logverb("done constellations.\n"); } if (bright) { double dy = 0; cairo_font_extents_t extents; pl* brightstars = pl_new(16); if (!justlist) { cairo_set_source_rgba(cairoshapes, 0.75, 0.75, 0.75, 0.8); cairo_font_extents(cairo, &extents); dy = extents.ascent * 0.5; cairo_set_line_width(cairoshapes, cw); } N = bright_stars_n(); logverb("Checking %i bright stars.\n", N); for (i=0; i<N; i++) { const brightstar_t* bs = bright_stars_get(i); if (!sip_radec2pixelxy(&sip, bs->ra, bs->dec, &px, &py)) continue; if (px < 0 || py < 0 || px*scale > W || py*scale > H) continue; if (!(bs->name && strlen(bs->name))) continue; if (common_only && !(bs->common_name && strlen(bs->common_name))) continue; if (strcmp(bs->common_name, "Maia") == 0) continue; pl_append(brightstars, bs); } // keep only the Nbright brightest? if (Nbright && (pl_size(brightstars) > Nbright)) { pl_sort(brightstars, sort_by_mag); pl_remove_index_range(brightstars, Nbright, pl_size(brightstars)-Nbright); } for (i=0; i<pl_size(brightstars); i++) { char* text; const brightstar_t* bs = pl_get(brightstars, i); if (!sip_radec2pixelxy(&sip, bs->ra, bs->dec, &px, &py)) continue; if (bs->common_name && strlen(bs->common_name)) if (print_common_only || common_only) text = strdup(bs->common_name); else asprintf_safe(&text, "%s (%s)", bs->common_name, bs->name); else text = strdup(bs->name); logverb("%s at (%g, %g)\n", text, px, py); if (json) { sl* names = sl_new(4); char* namearr; if (bs->common_name && strlen(bs->common_name)) sl_append(names, bs->common_name); if (bs->name) sl_append(names, bs->name); namearr = sl_join(names, "\", \""); sl_appendf(json, "{ \"type\" : \"star\", " " \"pixelx\": %g, " " \"pixely\": %g, " " \"name\" : \"%s\", " " \"names\" : [ \"%s\" ] } " , px, py, (bs->common_name && strlen(bs->common_name)) ? bs->common_name : bs->name, namearr); free(namearr); sl_free2(names); } if (bs->common_name && strlen(bs->common_name)) printf("The star %s (%s)\n", bs->common_name, bs->name); else printf("The star %s\n", bs->name); if (!justlist) { float r,g,b; // set color based on RA,Dec to match constellations above. if (whitetext) { r = g = b = 1; } else { color_for_radec(bs->ra, bs->dec, &r, &g, &b); } cairo_set_source_rgba(cairoshapes, r,g,b,0.8); cairo_set_source_rgba(cairo, r,g,b, 0.8); } if (!justlist) add_text(cairos, text, px + label_offset, py + dy, halign, valign); free(text); if (!justlist) { // plot a black circle behind the light circle... cairo_arc(cairobg, px, py, crad+1.0, 0.0, 2.0*M_PI); cairo_stroke(cairobg); cairo_arc(cairoshapes, px, py, crad, 0.0, 2.0*M_PI); cairo_stroke(cairoshapes); } } pl_free(brightstars); } if (NGC) { double imscale; double imsize; double dy = 0; cairo_font_extents_t extents; if (!justlist) { cairo_set_source_rgb(cairoshapes, 1.0, 1.0, 1.0); cairo_set_source_rgb(cairo, 1.0, 1.0, 1.0); cairo_set_line_width(cairo, nw); cairo_font_extents(cairo, &extents); dy = extents.ascent * 0.5; } // arcsec/pixel imscale = sip_pixel_scale(&sip); // arcmin imsize = imscale * (imin(W, H) / scale) / 60.0; N = ngc_num_entries(); logverb("Checking %i NGC/IC objects.\n", N); for (i=0; i<N; i++) { ngc_entry* ngc = ngc_get_entry(i); sl* str; sl* names; double pixsize; float ara, adec; char* text; if (!ngc) break; if (ngc->size < imsize * ngc_fraction) continue; if (ngcic_accurate_get_radec(ngc->is_ngc, ngc->id, &ara, &adec) == 0) { ngc->ra = ara; ngc->dec = adec; } if (!sip_radec2pixelxy(&sip, ngc->ra, ngc->dec, &px, &py)) continue; if (px < 0 || py < 0 || px*scale > W || py*scale > H) continue; str = sl_new(4); //sl_appendf(str, "%s %i", (ngc->is_ngc ? "NGC" : "IC"), ngc->id); names = ngc_get_names(ngc, NULL); if (names) { int n; for (n=0; n<sl_size(names); n++) { if (only_messier && strncmp(sl_get(names, n), "M ", 2)) continue; sl_append(str, sl_get(names, n)); } } sl_free2(names); text = sl_implode(str, " / "); printf("%s\n", text); pixsize = ngc->size * 60.0 / imscale; if (!justlist) { // black circle behind the white one... cairo_arc(cairobg, px, py, pixsize/2.0+1.0, 0.0, 2.0*M_PI); cairo_stroke(cairobg); cairo_move_to(cairoshapes, px + pixsize/2.0, py); cairo_arc(cairoshapes, px, py, pixsize/2.0, 0.0, 2.0*M_PI); debug("size: %f arcsec, pixsize: %f pixels\n", ngc->size, pixsize); cairo_stroke(cairoshapes); add_text(cairos, text, px + label_offset, py + dy, halign, valign); } if (json) { char* namelist = sl_implode(str, "\", \""); sl_appendf(json, "{ \"type\" : \"ngc\", " " \"names\" : [ \"%s\" ], " " \"pixelx\" : %g, " " \"pixely\" : %g, " " \"radius\" : %g }" , namelist, px, py, pixsize/2.0); free(namelist); } free(text); sl_free2(str); } } if (HD) { double rac, decc, ra2, dec2; double arcsec; hd_catalog_t* hdcat; bl* hdlist; int i; if (!justlist) cairo_set_source_rgb(cairo, 1.0, 1.0, 1.0); logverb("Reading HD catalog: %s\n", hdpath); hdcat = henry_draper_open(hdpath); if (!hdcat) { ERROR("Failed to open HD catalog"); exit(-1); } logverb("Got %i HD stars\n", henry_draper_n(hdcat)); sip_pixelxy2radec(&sip, W/(2.0*scale), H/(2.0*scale), &rac, &decc); sip_pixelxy2radec(&sip, 0.0, 0.0, &ra2, &dec2); arcsec = arcsec_between_radecdeg(rac, decc, ra2, dec2); // Fudge arcsec *= 1.1; hdlist = henry_draper_get(hdcat, rac, decc, arcsec); logverb("Found %zu HD stars within range (%g arcsec of RA,Dec %g,%g)\n", bl_size(hdlist), arcsec, rac, decc); for (i=0; i<bl_size(hdlist); i++) { double px, py; char* txt; hd_entry_t* hd = bl_access(hdlist, i); if (!sip_radec2pixelxy(&sip, hd->ra, hd->dec, &px, &py)) { continue; } if (px < 0 || py < 0 || px*scale > W || py*scale > H) { logverb(" HD %i at RA,Dec (%g, %g) -> pixel (%.1f, %.1f) is out of bounds\n", hd->hd, hd->ra, hd->dec, px, py); continue; } asprintf_safe(&txt, "HD %i", hd->hd); if (!justlist) { cairo_text_extents_t textents; cairo_text_extents(cairo, txt, &textents); cairo_arc(cairobg, px, py, crad+1.0, 0.0, 2.0*M_PI); cairo_stroke(cairobg); cairo_arc(cairoshapes, px, py, crad, 0.0, 2.0*M_PI); cairo_stroke(cairoshapes); px -= (textents.width * 0.5); py -= (crad + 4.0); add_text(cairos, txt, px, py, halign, valign); } if (json) sl_appendf(json, "{ \"type\" : \"hd\"," " \"pixelx\": %g, " " \"pixely\": %g, " " \"name\" : \"HD %i\" }" , px, py, hd->hd); printf("%s\n", txt); free(txt); } bl_free(hdlist); henry_draper_close(hdcat); } if (json) { FILE* fout = stderr; char* annstr = sl_implode(json, ",\n"); fprintf(fout, "{ \n"); fprintf(fout, " \"status\": \"solved\",\n"); fprintf(fout, " \"git-revision\": %s,\n", AN_GIT_REVISION); fprintf(fout, " \"git-date\": \"%s\",\n", AN_GIT_DATE); fprintf(fout, " \"annotations\": [\n%s\n]\n", annstr); fprintf(fout, "}\n"); free(annstr); } sl_free2(json); json = NULL; if (justlist) return 0; target = cairo_image_surface_create_for_data(img, CAIRO_FORMAT_ARGB32, W, H, W*4); cairot = cairo_create(target); cairo_set_source_rgba(cairot, 0, 0, 0, 1); // Here's where you set the background surface's properties... cairo_set_source_surface(cairot, surfbg, 0, 0); cairo_mask_surface(cairot, surfshapesmask, 0, 0); cairo_stroke(cairot); // Add on the shapes. cairo_set_source_surface(cairot, surfshapes, 0, 0); //cairo_mask_surface(cairot, surfshapes, 0, 0); cairo_mask_surface(cairot, surfshapesmask, 0, 0); cairo_stroke(cairot); // Add on the foreground. cairo_set_source_surface(cairot, surffg, 0, 0); cairo_mask_surface(cairot, surffg, 0, 0); cairo_stroke(cairot); // Convert image for output... cairoutils_argb32_to_rgba(img, W, H); if (pngformat) { if (cairoutils_write_png(outfn, img, W, H)) { ERROR("Failed to write PNG"); exit(-1); } } else { if (cairoutils_write_ppm(outfn, img, W, H)) { ERROR("Failed to write PPM"); exit(-1); } } cairo_surface_destroy(target); cairo_surface_destroy(surfshapesmask); cairo_surface_destroy(surffg); cairo_surface_destroy(surfbg); cairo_surface_destroy(surfshapes); cairo_destroy(cairo); cairo_destroy(cairot); cairo_destroy(cairobg); cairo_destroy(cairoshapes); cairo_destroy(cairoshapesmask); free(img); return 0; }
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** args) { int argchar; kdtree_t* kd; int Nleaf = 25; char* infn = NULL; char* outfn = NULL; char* tychofn = NULL; char* crossfn = NULL; char* progname = args[0]; FILE* f; tycstar_t* tycstars = NULL; int Ntyc = 0; int exttype = KDT_EXT_DOUBLE; int datatype = KDT_DATA_U32; int treetype = KDT_TREE_U32; int tt; int buildopts = 0; int i, N, D; dl* ras; dl* decs; dl* hds; fl* mag1s; fl* mag2s; fl* mag3s; int nbad = 0; int nox = 0; int* hd; double* xyz; qfits_header* hdr; while ((argchar = getopt (argc, args, OPTIONS)) != -1) switch (argchar) { case 'T': tychofn = optarg; break; case 'X': crossfn = optarg; break; case 'R': Nleaf = (int)strtoul(optarg, NULL, 0); break; case 't': treetype = kdtree_kdtype_parse_tree_string(optarg); break; case 'd': datatype = kdtree_kdtype_parse_data_string(optarg); break; case 'b': buildopts |= KD_BUILD_BBOX; break; case 's': buildopts |= KD_BUILD_SPLIT; break; case 'S': buildopts |= KD_BUILD_SPLITDIM; break; case '?': fprintf(stderr, "Unknown option `-%c'.\n", optopt); case 'h': printHelp(progname); return 0; default: return -1; } if (optind != argc - 2) { printHelp(progname); exit(-1); } infn = args[optind]; outfn = args[optind+1]; if (!(buildopts & (KD_BUILD_BBOX | KD_BUILD_SPLIT))) { printf("You need bounding-boxes or splitting planes!\n"); printHelp(progname); exit(-1); } if (tychofn || crossfn) { if (!(tychofn && crossfn)) { printf("You need both -T <Tycho2> and -X <Crossref> to do cross-referencing.\n"); exit(-1); } } if (tychofn) { int i, N; tycho2_fits* tyc; FILE* f; int nx, nox; int lastgrass = 0; tyc = tycho2_fits_open(tychofn); if (!tyc) { ERROR("Failed to open Tycho-2 catalog."); exit(-1); } printf("Reading Tycho-2 catalog...\n"); N = tycho2_fits_count_entries(tyc); tycstars = calloc(N, sizeof(tycstar_t)); for (i=0; i<N; i++) { tycho2_entry* te; int grass = (i*80 / N); if (grass != lastgrass) { printf("."); fflush(stdout); lastgrass = grass; } te = tycho2_fits_read_entry(tyc); tycstars[i].tyc1 = te->tyc1; tycstars[i].tyc2 = te->tyc2; tycstars[i].tyc3 = te->tyc3; tycstars[i].ra = te->ra; tycstars[i].dec = te->dec; tycstars[i].mag_BT = te->mag_BT; tycstars[i].mag_VT = te->mag_VT; tycstars[i].mag_HP = te->mag_HP; } tycho2_fits_close(tyc); printf("Sorting...\n"); qsort(tycstars, N, sizeof(tycstar_t), compare_tycs); Ntyc = N; f = fopen(crossfn, "rb"); if (!f) { SYSERROR("Failed to open cross-reference file %s", crossfn); exit(-1); } nx = 0; nox = 0; while (TRUE) { char buf[1024]; int tyc1, tyc2, tyc3, hd, nhd, ntyc; char ftyc, sptype0, sptype1, sptype2; tycstar_t* s; if (!fgets(buf, sizeof(buf), f)) { if (ferror(f)) { SYSERROR("Failed to read a line of text from the cross-reference file"); exit(-1); } break; } if (sscanf(buf, " %d %d %d%c %d %c%c%c %d %d", &tyc1, &tyc2, &tyc3, &ftyc, &hd, &sptype0, &sptype1, &sptype2, &nhd, &ntyc) != 10) { ERROR("Failed to parse line: \"%s\"", buf); } //printf("%i %i %i %i %i %i\n", tyc1, tyc2, tyc3, hd, nhd, ntyc); s = find_tycho(tycstars, Ntyc, tyc1, tyc2, tyc3); if (!s) { ERROR("Failed to find Tycho-2 star %i-%i-%i", tyc1, tyc2, tyc3); nox++; } else { s->hd = hd; s->ntyc = ntyc; } nx++; } fclose(f); printf("Read %i cross-references.\n", nx); printf("Failed to find %i cross-referenced Tycho-2 stars.\n", nox); printf("Sorting...\n"); qsort(tycstars, N, sizeof(tycstar_t), compare_hds); } f = fopen(infn, "rb"); if (!f) { SYSERROR("Failed to open input file %s", infn); exit(-1); } ras = dl_new(1024); decs = dl_new(1024); hds = il_new(1024); mag1s = fl_new(1024); mag2s = fl_new(1024); mag3s = fl_new(1024); printf("Reading HD catalog...\n"); for (;;) { char buf[1024]; double ra, dec; int hd; float mag1, mag2, mag3; mag1 = mag2 = mag3 = 0.0; if (!fgets(buf, sizeof(buf), f)) { if (ferror(f)) { SYSERROR("Failed to read a line of text from the input file"); exit(-1); } break; } if (buf[0] == '#') continue; if (buf[0] == '\n') continue; if (sscanf(buf, " %lf| %lf| %d", &ra, &dec, &hd) < 3) { // ignore three invalid lines if (nbad > 3) { ERROR("Failed to parse line: \"%s\"", buf); } nbad++; } else { if (tycstars) { tycstar_t* s = find_hd(tycstars, Ntyc, hd); if (!s) { //printf("Failed to find cross-ref for HD %i\n", hd); nox++; } else { ra = s->ra; dec = s->dec; mag1 = s->mag_VT; mag2 = s->mag_BT; mag3 = s->mag_HP; } } dl_append(ras, ra); dl_append(decs, dec); il_append(hds, hd); fl_append(mag1s, mag1); fl_append(mag2s, mag2); fl_append(mag3s, mag3); } } fclose(f); N = dl_size(ras); printf("Read %i entries and %i bad lines.\n", N, nbad); if (dl_size(ras) != HD_NENTRIES) { printf("WARNING: expected %i Henry Draper catalog entries.\n", HD_NENTRIES); } if (nox) { printf("Found %i HD entries with no cross-reference (expect this to be about 1%%)\n", nox); } hd = malloc(sizeof(int) * N); il_copy(hds, 0, N, hd); il_free(hds); for (i=0; i<N; i++) if (hd[i] != i+1) { printf("Line %i is HD %i\n", i+1, hd[i]); break; } // HACK - don't allocate 'em in the first place... free(hd); xyz = malloc(sizeof(double) * 3 * N); for (i=0; i<N; i++) { radecdeg2xyzarr(dl_get(ras, i), dl_get(decs, i), xyz + 3*i); } dl_free(ras); dl_free(decs); tt = kdtree_kdtypes_to_treetype(exttype, treetype, datatype); D = 3; { // limits of the kdtree... double lo[] = {-1.0, -1.0, -1.0}; double hi[] = { 1.0, 1.0, 1.0}; kd = kdtree_new(N, D, Nleaf); kdtree_set_limits(kd, lo, hi); } printf("Building tree...\n"); kd = kdtree_build(kd, xyz, N, D, Nleaf, tt, buildopts); hdr = qfits_header_default(); qfits_header_add(hdr, "AN_FILE", "HDTREE", "Henry Draper catalog kdtree", NULL); BOILERPLATE_ADD_FITS_HEADERS(hdr); fits_add_long_history(hdr, "This file was created by the following command-line:"); fits_add_args(hdr, args, argc); if (kdtree_fits_write(kd, outfn, hdr)) { ERROR("Failed to write kdtree"); exit(-1); } // Write mags as tag-along table. { fitstable_t* tag; tag = fitstable_open_for_appending(outfn); if (!tag) { ERROR("Failed to open kd-tree file for appending"); exit(-1); } fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_VT", ""); fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_BT", ""); fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_HP", ""); if (fitstable_write_header(tag)) { ERROR("Failed to write tag-along header"); exit(-1); } for (i=0; i<N; i++) { fitstable_write_row(tag, fl_get(mag1s, i), fl_get(mag2s, i), fl_get(mag3s, i)); } if (fitstable_fix_header(tag)) { ERROR("Failed to fix tag-along header"); exit(-1); } if (fitstable_close(tag)) { ERROR("Failed to close tag-along data"); exit(-1); } } fl_free(mag1s); fl_free(mag2s); fl_free(mag3s); printf("Done.\n"); qfits_header_destroy(hdr); free(xyz); kdtree_free(kd); free(tycstars); return 0; }
anbool tan_radec2iwc(const tan_t* tan, double ra, double dec, double* iwcx, double* iwcy) { double xyz[3]; radecdeg2xyzarr(ra, dec, xyz); return tan_xyzarr2iwc(tan, xyz, iwcx, iwcy); }
static void plot_targets(cairo_t* cairo, plot_args_t* pargs, plotann_t* ann) { int i; double cra, cdec; plotstuff_get_radec_center_and_radius(pargs, &cra, &cdec, NULL); for (i=0; i<bl_size(ann->targets); i++) { target_t* tar = bl_access(ann->targets, i); double px,py; double cx,cy; double dx,dy, r; double ex,ey; double ly, ry, tx, bx; double distdeg; anbool okquadrant; char* txt; logverb("Target: \"%s\" at (%g,%g)\n", tar->name, tar->ra, tar->dec); okquadrant = plotstuff_radec2xy(pargs, tar->ra, tar->dec, &px, &py); px -= 1; py -= 1; if (okquadrant && px >= 0 && px < pargs->W && py >= 0 && py < pargs->H) { // inside the image! logverb("Target \"%s\" is inside the image, at pixel (%g,%g)\n", tar->name, px, py); plotstuff_stack_marker(pargs, px, py); plotstuff_stack_text(pargs, cairo, tar->name, px, py); continue; } // outside the image: find intersection point. cx = pargs->W / 2.0; cy = pargs->H / 2.0; if (okquadrant) { logverb("Target \"%s\" is outside the image, at pixel (%g,%g)\n", tar->name, px, py); dx = px - cx; dy = py - cy; } else { double cxyz[3]; double txyz[3]; double vec[3]; int j; double ra,dec; logverb("Target \"%s\" is way outside the image.\n", tar->name); // fallback. radecdeg2xyzarr(cra, cdec, cxyz); radecdeg2xyzarr(tar->ra, tar->dec, txyz); for (j=0; j<3; j++) vec[j] = cxyz[j] + 0.1 * txyz[j]; normalize_3(vec); xyzarr2radecdeg(vec, &ra, &dec); okquadrant = plotstuff_radec2xy(pargs, ra, dec, &px, &py); assert(okquadrant); dx = px - cx; dy = py - cy; if ((dx*dx + dy*dy) < (cx*cx + cy*cy)) { double scale = 3.0 * sqrt(cx*cx + cy*cy) / sqrt(dx*dx + dy*dy); dx *= scale; dy *= scale; } } ly = (-(pargs->W/2.0) / dx) * dy + cy; ry = ( (pargs->W/2.0) / dx) * dy + cy; bx = (-(pargs->H/2.0) / dy) * dx + cx; tx = ( (pargs->H/2.0) / dy) * dx + cx; logverb("ly %g, ry %g, bx %g, tx %g\n", ly, ry, bx, tx); if (px < cx && ly >= 0 && ly < pargs->H) { ex = 0.0; ey = ly; } else if (px >= cx && ry >= 0 && ry < pargs->H) { ex = pargs->W - 1; ey = ry; } else if (py < cy && bx >= 0 && bx < pargs->W) { ex = bx; ey = 0; } else if (py >= cy && tx >= 0 && tx < pargs->W) { ex = tx; ey = pargs->H - 1; } else { logverb("None of the edges are in bounds: px,py=(%g,%g); ly=%g, ry=%g, bx=%g, tx=%g\n", px,py,ly,ry,bx,tx); continue; } dx = ex - cx; dy = ey - cy; r = sqrt(dx*dx + dy*dy); px = (r-100.0) / r * dx + cx; py = (r-100.0) / r * dy + cy; plotstuff_stack_arrow(pargs, px, py, ex, ey); logverb("Arrow from (%g,%g) to (%g,%g)\n", px, py, ex, ey); distdeg = deg_between_radecdeg(cra, cdec, tar->ra, tar->dec); asprintf_safe(&txt, "%s: %.1f deg", tar->name, distdeg); plotstuff_stack_text(pargs, cairo, txt, px, py); } }
static void plot_constellations(cairo_t* cairo, plot_args_t* pargs, plotann_t* ann) { int i, N; double ra,dec,radius; double xyzf[3]; // Find the field center and radius anwcs_get_radec_center_and_radius(pargs->wcs, &ra, &dec, &radius); logverb("Plotting constellations: field center %g,%g, radius %g\n", ra, dec, radius); radecdeg2xyzarr(ra, dec, xyzf); radius = deg2dist(radius); N = constellations_n(); for (i=0; i<N; i++) { int j, k; // Find the approximate center and radius of this constellation // and see if it overlaps with the field. il* stars = constellations_get_unique_stars(i); double xyzj[3]; double xyzc[3]; double maxr2 = 0; dl* rds; xyzc[0] = xyzc[1] = xyzc[2] = 0.0; xyzj[0] = xyzj[1] = xyzj[2] = 0.0; for (j=0; j<il_size(stars); j++) { constellations_get_star_radec(il_get(stars, j), &ra, &dec); radecdeg2xyzarr(ra, dec, xyzj); for (k=0; k<3; k++) xyzc[k] += xyzj[k]; } normalize_3(xyzc); for (j=0; j<il_size(stars); j++) { constellations_get_star_radec(il_get(stars, j), &ra, &dec); maxr2 = MAX(maxr2, distsq(xyzc, xyzj, 3)); } il_free(stars); maxr2 = square(sqrt(maxr2) + radius); if (distsq(xyzf, xyzc, 3) > maxr2) { xyzarr2radecdeg(xyzc, &ra, &dec); logverb("Constellation %s (center %g,%g, radius %g) out of bounds\n", constellations_get_shortname(i), ra, dec, dist2deg(sqrt(maxr2) - radius)); logverb(" dist from field center to constellation center is %g deg\n", distsq2deg(distsq(xyzf, xyzc, 3))); logverb(" max radius: %g\n", distsq2deg(maxr2)); continue; } if (ann->constellation_pastel) { float r,g,b; xyzarr2radecdeg(xyzc, &ra, &dec); color_for_radec(ra, dec, &r,&g,&b); plotstuff_set_rgba2(pargs, r,g,b, 0.8); plotstuff_builtin_apply(cairo, pargs); } // Phew, plot it. if (ann->constellation_lines) { rds = constellations_get_lines_radec(i); logverb("Constellation %s: plotting %zu lines\n", constellations_get_shortname(i), dl_size(rds)/4); for (j=0; j<dl_size(rds)/4; j++) { double r1,d1,r2,d2; double r3,d3,r4,d4; double off = ann->constellation_lines_offset; r1 = dl_get(rds, j*4+0); d1 = dl_get(rds, j*4+1); r2 = dl_get(rds, j*4+2); d2 = dl_get(rds, j*4+3); if (anwcs_find_discontinuity(pargs->wcs, r1, d1, r2, d2, &r3, &d3, &r4, &d4)) { logverb("Discontinuous: %g,%g -- %g,%g\n", r1, d1, r2, d2); logverb(" %g,%g == %g,%g\n", r3,d3, r4,d4); plot_offset_line_rd(NULL, pargs, r1,d1,r3,d3, off, 0.); plot_offset_line_rd(NULL, pargs, r4,d4,r2,d2, 0., off); } else { plot_offset_line_rd(NULL, pargs, r1,d1,r2,d2, off, off); } plotstuff_stroke(pargs); } dl_free(rds); } if (ann->constellation_labels || ann->constellation_markers) { // Put the label at the center of mass of the stars that // are in-bounds int Nin = 0; stars = constellations_get_unique_stars(i); xyzc[0] = xyzc[1] = xyzc[2] = 0.0; logverb("Labeling %s: %zu stars\n", constellations_get_shortname(i), il_size(stars)); for (j=0; j<il_size(stars); j++) { constellations_get_star_radec(il_get(stars, j), &ra, &dec); if (!anwcs_radec_is_inside_image(pargs->wcs, ra, dec)) continue; if (ann->constellation_markers) plotstuff_marker_radec(pargs, ra, dec); radecdeg2xyzarr(ra, dec, xyzj); for (k=0; k<3; k++) xyzc[k] += xyzj[k]; Nin++; } logverb(" %i stars in-bounds\n", Nin); if (ann->constellation_labels && Nin) { const char* label; normalize_3(xyzc); xyzarr2radecdeg(xyzc, &ra, &dec); if (ann->constellation_labels_long) label = constellations_get_longname(i); else label = constellations_get_shortname(i); plotstuff_text_radec(pargs, ra, dec, label); } il_free(stars); } } }
int main(int argc, char *argv[]) { int argchar; char* progname = argv[0]; char** inputfiles = NULL; int ninputfiles = 0; char* rdlsfname = NULL; rdlist_t* rdls = NULL; int i; int correct, incorrect; int firstfield = 1; int lastfield = -1; int nfields; int Ncenter = 0; char* fpsolved = NULL; char* tpsolved = NULL; int nfields_total; int firstfileid = 0; int lastfileid = 0; int fileid; matchfile** matchfiles; int* mfcursors; char* truematchfn = NULL; char* falsematchfn = NULL; matchfile* truematch = NULL; matchfile* falsematch = NULL; while ((argchar = getopt (argc, argv, OPTIONS)) != -1) { switch (argchar) { case 'h': printHelp(progname); return (HELP_ERR); case 'm': truematchfn = optarg; break; case 'M': falsematchfn = optarg; break; case 'A': firstfield = atoi(optarg); break; case 'B': lastfield = atoi(optarg); break; case 'C': Ncenter = atoi(optarg); break; case 'R': rdlsfname = optarg; break; case 'T': tpsolved = optarg; break; case 'F': fpsolved = optarg; break; case 'i': firstfileid = atoi(optarg); break; case 'I': lastfileid = atoi(optarg); break; default: return (OPT_ERR); } } if (optind < argc) { ninputfiles = argc - optind; inputfiles = argv + optind; } else { printHelp(progname); exit(-1); } if (!rdlsfname) { fprintf(stderr, "You must specify an RDLS file!\n"); printHelp(progname); exit(-1); } if (truematchfn) { truematch = matchfile_open_for_writing(truematchfn); if (!truematch) { fprintf(stderr, "Failed to open file %s for writing matches.\n", truematchfn); exit(-1); } if (matchfile_write_headers(truematch)) { fprintf(stderr, "Failed to write header for %s\n", truematchfn); exit(-1); } } if (falsematchfn) { falsematch = matchfile_open_for_writing(falsematchfn); if (!falsematch) { fprintf(stderr, "Failed to open file %s for writing matches.\n", falsematchfn); exit(-1); } if (matchfile_write_headers(falsematch)) { fprintf(stderr, "Failed to write header for %s\n", falsematchfn); exit(-1); } } matchfiles = malloc(ninputfiles * sizeof(matchfile*)); mfcursors = calloc(ninputfiles, sizeof(int)); for (i=0; i<ninputfiles; i++) { char* fname = inputfiles[i]; printf("Opening matchfile %s...\n", fname); matchfiles[i] = matchfile_open(fname); if (!matchfiles[i]) { fprintf(stderr, "Failed to open matchfile %s.\n", fname); exit(-1); } } correct = incorrect = 0; nfields_total = 0; for (i=0; i<ninputfiles; i++) { matchfile* mf; MatchObj* mo; mf = matchfiles[i]; for (fileid=firstfileid; fileid<=lastfileid; fileid++) { char fn[1024]; int nread = 0; sprintf(fn, rdlsfname, fileid); //printf("Reading rdls file \"%s\"...\n", fn); fflush(stdout); rdls = rdlist_open(fn); if (!rdls) { fprintf(stderr, "Couldn't read rdls file.\n"); exit(-1); } nfields = rdlist_n_fields(rdls); //printf("Read %i fields from rdls file.\n", nfields); if ((lastfield != -1) && (nfields > lastfield)) { nfields = lastfield + 1; } else { lastfield = nfields; } for (; mfcursors[i]<matchfile_count(mf); mfcursors[i]++) { int filenum; int fieldnum; double rac, decc; double r2; double arc; int nrd; rd_t* rd; int k; anbool err = FALSE; mo = matchfile_read_match(mf); filenum = mo->fieldfile; if (filenum < fileid) continue; if (filenum > fileid) { matchfile_pushback_match(mf); break; } fieldnum = mo->fieldnum; if (fieldnum < firstfield) continue; if (fieldnum > lastfield) continue; nread++; rd = rdlist_read_field_num(rdls, fieldnum, NULL); if (!rd) { fprintf(stderr, "Failed to read RDLS entries for field %i.\n", fieldnum); exit(-1); } nrd = rd_n(rd); if (Ncenter) nrd = MIN(nrd, Ncenter); r2 = square(mo->radius); arc = deg2arcmin(mo->radius_deg); xyzarr2radec(mo->center, &rac, &decc); for (k=0; k<nrd; k++) { double xyz[3]; double ra, dec; ra = rd_getra (rd, k); dec = rd_getdec(rd, k); radecdeg2xyzarr(ra, dec, xyz); if (distsq_exceeds(xyz, mo->center, 3, r2 * 1.2)) { printf("\nError: Field %i: match says center (%g, %g), scale %g arcmin, but\n", fieldnum, rac, decc, arc); printf("rdls %i is (%g, %g).\n", k, ra, dec); printf("Logprob %g (%g).\n", mo->logodds, exp(mo->logodds)); err = TRUE; break; } } rd_free(rd); if (err) { incorrect++; if (falsematch) { if (matchfile_write_match(falsematch, mo)) { fprintf(stderr, "Failed to write match to %s\n", falsematchfn); exit(-1); } } } else { printf("Field %5i: correct hit: (%8.3f, %8.3f), scale %6.3f arcmin, logodds %g (%g)\n", fieldnum, rac, decc, arc, mo->logodds, exp(mo->logodds)); correct++; if (truematch) { if (matchfile_write_match(truematch, mo)) { fprintf(stderr, "Failed to write match to %s\n", truematchfn); exit(-1); } } } fflush(stdout); if (tpsolved && !err) solvedfile_set(tpsolved, nfields_total); if (fpsolved && err) solvedfile_set(fpsolved, nfields_total); nfields_total++; } rdlist_close(rdls); printf("Read %i from %s for fileid %i\n", nread, inputfiles[i], fileid); } } printf("\n"); printf("Read a total of %i correct and %i incorrect matches.\n", correct, incorrect); for (i=0; i<ninputfiles; i++) { matchfile_close(matchfiles[i]); } free(matchfiles); free(mfcursors); if (tpsolved) solvedfile_setsize(tpsolved, nfields_total); if (fpsolved) solvedfile_setsize(fpsolved, nfields_total); if (truematch) { if (matchfile_fix_headers(truematch) || matchfile_close(truematch)) { fprintf(stderr, "Failed to fix header for %s\n", truematchfn); exit(-1); } } if (falsematch) { if (matchfile_fix_headers(falsematch) || matchfile_close(falsematch)) { fprintf(stderr, "Failed to fix header for %s\n", falsematchfn); exit(-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; }
// RA,Dec in degrees to Pixels. anbool tan_radec2pixelxy(const tan_t* tan, double a, double d, double *px, double *py) { double xyzpt[3]; radecdeg2xyzarr(a,d,xyzpt); return tan_xyzarr2pixelxy(tan, xyzpt, px, py); }
int plot_index_plot(const char* command, cairo_t* cairo, plot_args_t* pargs, void* baton) { plotindex_t* args = (plotindex_t*)baton; int i; double ra, dec, radius; double xyz[3]; double r2; pad_qidxes(args); plotstuff_builtin_apply(cairo, pargs); if (plotstuff_get_radec_center_and_radius(pargs, &ra, &dec, &radius)) { ERROR("Failed to get RA,Dec center and radius"); return -1; } radecdeg2xyzarr(ra, dec, xyz); r2 = deg2distsq(radius); logmsg("Field RA,Dec,radius = (%g,%g), %g deg\n", ra, dec, radius); logmsg("distsq: %g\n", r2); for (i=0; i<pl_size(args->indexes); i++) { index_t* index = pl_get(args->indexes, i); int j, N; int DQ; double px,py; if (args->stars) { // plot stars double* radecs = NULL; startree_search_for(index->starkd, xyz, r2, NULL, &radecs, NULL, &N); if (N) { assert(radecs); } logmsg("Found %i stars in range in index %s\n", N, index->indexname); for (j=0; j<N; j++) { logverb(" RA,Dec (%g,%g) -> x,y (%g,%g)\n", radecs[2*j], radecs[2*j+1], px, py); if (!plotstuff_radec2xy(pargs, radecs[j*2], radecs[j*2+1], &px, &py)) { ERROR("Failed to convert RA,Dec %g,%g to pixels\n", radecs[j*2], radecs[j*2+1]); continue; } cairoutils_draw_marker(cairo, pargs->marker, px, py, pargs->markersize); cairo_stroke(cairo); } free(radecs); } if (args->quads) { DQ = index_get_quad_dim(index); qidxfile* qidx = pl_get(args->qidxes, i); if (qidx) { int* stars; int Nstars; il* quadlist = il_new(256); // find stars in range. startree_search_for(index->starkd, xyz, r2, NULL, NULL, &stars, &Nstars); logmsg("Found %i stars in range of index %s\n", N, index->indexname); logmsg("Using qidx file.\n"); // find quads that each star is a member of. for (j=0; j<Nstars; j++) { uint32_t* quads; int Nquads; int k; if (qidxfile_get_quads(qidx, stars[j], &quads, &Nquads)) { ERROR("Failed to get quads for star %i\n", stars[j]); return -1; } for (k=0; k<Nquads; k++) il_insert_unique_ascending(quadlist, quads[k]); } for (j=0; j<il_size(quadlist); j++) { plotquad(cairo, pargs, args, index, il_get(quadlist, j), DQ); } } else { // plot quads N = index_nquads(index); for (j=0; j<N; j++) { plotquad(cairo, pargs, args, index, j, DQ); } } } } return 0; }
sip_t* tweak2(const double* fieldxy, int Nfield, double fieldjitter, int W, int H, const double* indexradec, int Nindex, double indexjitter, const double* quadcenter, double quadR2, double distractors, double logodds_bail, int sip_order, int sip_invorder, const sip_t* startwcs, sip_t* destwcs, int** newtheta, double** newodds, double* crpix, double* p_logodds, int* p_besti, int* testperm, int startorder) { int order; sip_t* sipout; int* indexin; double* indexpix; double* fieldsigma2s; double* weights; double* matchxyz; double* matchxy; int i, Nin=0; double logodds = 0; int besti = -1; int* theta = NULL; double* odds = NULL; int* refperm = NULL; double qc[2]; memcpy(qc, quadcenter, 2*sizeof(double)); if (destwcs) sipout = destwcs; else sipout = sip_create(); indexin = malloc(Nindex * sizeof(int)); indexpix = malloc(2 * Nindex * sizeof(double)); fieldsigma2s = malloc(Nfield * sizeof(double)); weights = malloc(Nfield * sizeof(double)); matchxyz = malloc(Nfield * 3 * sizeof(double)); matchxy = malloc(Nfield * 2 * sizeof(double)); // FIXME --- hmmm, how do the annealing steps and iterating up to // higher orders interact? assert(startwcs); memcpy(sipout, startwcs, sizeof(sip_t)); logverb("tweak2: starting orders %i, %i\n", sipout->a_order, sipout->ap_order); if (!sipout->wcstan.imagew) sipout->wcstan.imagew = W; if (!sipout->wcstan.imageh) sipout->wcstan.imageh = H; logverb("Tweak2: starting from WCS:\n"); if (log_get_level() >= LOG_VERB) sip_print_to(sipout, stdout); for (order=startorder; order <= sip_order; order++) { int step; int STEPS = 100; // variance growth rate wrt radius. double gamma = 1.0; //logverb("Starting tweak2 order=%i\n", order); for (step=0; step<STEPS; step++) { double iscale; double ijitter; double ra, dec; double R2; int Nmatch; int nmatch, nconf, ndist; double pix2; double totalweight; // clean up from last round (we do it here so that they're // valid when we leave the loop) free(theta); free(odds); free(refperm); // Anneal gamma = pow(0.9, step); if (step == STEPS-1) gamma = 0.0; logverb("Annealing: order %i, step %i, gamma = %g\n", order, step, gamma); debug("Using input WCS:\n"); if (log_get_level() > LOG_VERB) sip_print_to(sipout, stdout); // Project reference sources into pixel space; keep the ones inside image bounds. Nin = 0; for (i=0; i<Nindex; i++) { anbool ok; double x,y; ra = indexradec[2*i + 0]; dec = indexradec[2*i + 1]; ok = sip_radec2pixelxy(sipout, ra, dec, &x, &y); if (!ok) continue; if (!sip_pixel_is_inside_image(sipout, x, y)) continue; indexpix[Nin*2+0] = x; indexpix[Nin*2+1] = y; indexin[Nin] = i; Nin++; } logverb("%i reference sources within the image.\n", Nin); //logverb("CRPIX is (%g,%g)\n", sip.wcstan.crpix[0], sip.wcstan.crpix[1]); if (Nin == 0) { sip_free(sipout); free(matchxy); free(matchxyz); free(weights); free(fieldsigma2s); free(indexpix); free(indexin); return NULL; } iscale = sip_pixel_scale(sipout); ijitter = indexjitter / iscale; //logverb("With pixel scale of %g arcsec/pixel, index adds jitter of %g pix.\n", iscale, ijitter); /* CHECK for (i=0; i<Nin; i++) { double x,y; int ii = indexin[i]; sip_radec2pixelxy(sipout, indexradec[2*ii+0], indexradec[2*ii+1], &x, &y); logverb("indexin[%i]=%i; (%.1f,%.1f) -- (%.1f,%.1f)\n", i, ii, indexpix[i*2+0], indexpix[i*2+1], x, y); } */ for (i=0; i<Nfield; i++) { R2 = distsq(qc, fieldxy + 2*i, 2); fieldsigma2s[i] = (square(fieldjitter) + square(ijitter)) * (1.0 + gamma * R2/quadR2); } if (order == 1 && step == 0 && TWEAK_DEBUG_PLOTS) { TWEAK_DEBUG_PLOT("init", W, H, Nfield, fieldxy, fieldsigma2s, Nin, indexpix, *p_besti, *newtheta, sipout->wcstan.crpix, testperm, qc); } /* logodds = verify_star_lists(indexpix, Nin, fieldxy, fieldsigma2s, Nfield, W*H, distractors, logodds_bail, HUGE_VAL, &besti, &odds, &theta, NULL, &testperm); */ pix2 = square(fieldjitter); logodds = verify_star_lists_ror(indexpix, Nin, fieldxy, fieldsigma2s, Nfield, pix2, gamma, qc, quadR2, W, H, distractors, logodds_bail, HUGE_VAL, &besti, &odds, &theta, NULL, &testperm, &refperm); logverb("Logodds: %g\n", logodds); verify_count_hits(theta, besti, &nmatch, &nconf, &ndist); logverb("%i matches, %i distractors, %i conflicts (at best log-odds); %i field sources, %i index sources\n", nmatch, ndist, nconf, Nfield, Nin); verify_count_hits(theta, Nfield-1, &nmatch, &nconf, &ndist); logverb("%i matches, %i distractors, %i conflicts (all sources)\n", nmatch, ndist, nconf); if (log_get_level() >= LOG_VERB) { matchobj_log_hit_miss(theta, testperm, besti+1, Nfield, LOG_VERB, "Hit/miss: "); } /* logverb("\nAfter verify():\n"); for (i=0; i<Nin; i++) { double x,y; int ii = indexin[refperm[i]]; sip_radec2pixelxy(sipout, indexradec[2*ii+0], indexradec[2*ii+1], &x, &y); logverb("indexin[%i]=%i; (%.1f,%.1f) -- (%.1f,%.1f)\n", i, ii, indexpix[i*2+0], indexpix[i*2+1], x, y); } */ if (TWEAK_DEBUG_PLOTS) { char name[32]; sprintf(name, "o%is%02ipre", order, step); TWEAK_DEBUG_PLOT(name, W, H, Nfield, fieldxy, fieldsigma2s, Nin, indexpix, besti, theta, sipout->wcstan.crpix, testperm, qc); } Nmatch = 0; debug("Weights:"); for (i=0; i<Nfield; i++) { double ra,dec; if (theta[i] < 0) continue; assert(theta[i] < Nin); int ii = indexin[refperm[theta[i]]]; assert(ii < Nindex); assert(ii >= 0); ra = indexradec[ii*2+0]; dec = indexradec[ii*2+1]; radecdeg2xyzarr(ra, dec, matchxyz + Nmatch*3); memcpy(matchxy + Nmatch*2, fieldxy + i*2, 2*sizeof(double)); weights[Nmatch] = verify_logodds_to_weight(odds[i]); debug(" %.2f", weights[Nmatch]); Nmatch++; /* logverb("match img (%.1f,%.1f) -- ref (%.1f, %.1f), odds %g, wt %.3f\n", fieldxy[i*2+0], fieldxy[i*2+1], indexpix[theta[i]*2+0], indexpix[theta[i]*2+1], odds[i], weights[Nmatch-1]); double xx,yy; sip_radec2pixelxy(sipout, ra, dec, &xx, &yy); logverb("check: (%.1f, %.1f)\n", xx, yy); */ } debug("\n"); if (Nmatch < 2) { logverb("No matches -- aborting tweak attempt\n"); free(theta); sip_free(sipout); free(matchxy); free(matchxyz); free(weights); free(fieldsigma2s); free(indexpix); free(indexin); return NULL; } // Update the "quad center" to be the weighted average matched star posn. qc[0] = qc[1] = 0.0; totalweight = 0.0; for (i=0; i<Nmatch; i++) { qc[0] += (weights[i] * matchxy[2*i+0]); qc[1] += (weights[i] * matchxy[2*i+1]); totalweight += weights[i]; } qc[0] /= totalweight; qc[1] /= totalweight; logverb("Moved quad center to (%.1f, %.1f)\n", qc[0], qc[1]); // sipout->a_order = sipout->b_order = order; sipout->ap_order = sipout->bp_order = sip_invorder; logverb("tweak2: setting orders %i, %i\n", sipout->a_order, sipout->ap_order); if (crpix) { tan_t temptan; logverb("Moving tangent point to given CRPIX (%g,%g)\n", crpix[0], crpix[1]); fit_tan_wcs_move_tangent_point_weighted(matchxyz, matchxy, weights, Nmatch, crpix, &sipout->wcstan, &temptan); fit_tan_wcs_move_tangent_point_weighted(matchxyz, matchxy, weights, Nmatch, crpix, &temptan, &sipout->wcstan); } int doshift = 1; fit_sip_wcs(matchxyz, matchxy, weights, Nmatch, &(sipout->wcstan), order, sip_invorder, doshift, sipout); debug("Got SIP:\n"); if (log_get_level() > LOG_VERB) sip_print_to(sipout, stdout); sipout->wcstan.imagew = W; sipout->wcstan.imageh = H; } } //logverb("Final logodds: %g\n", logodds); // Now, recompute final logodds after turning 'gamma' on again (?) // FIXME -- this counts the quad stars in the logodds... { double gamma = 1.0; double iscale; double ijitter; double ra, dec; double R2; int nmatch, nconf, ndist; double pix2; free(theta); free(odds); free(refperm); gamma = 1.0; // Project reference sources into pixel space; keep the ones inside image bounds. Nin = 0; for (i=0; i<Nindex; i++) { anbool ok; double x,y; ra = indexradec[2*i + 0]; dec = indexradec[2*i + 1]; ok = sip_radec2pixelxy(sipout, ra, dec, &x, &y); if (!ok) continue; if (!sip_pixel_is_inside_image(sipout, x, y)) continue; indexpix[Nin*2+0] = x; indexpix[Nin*2+1] = y; indexin[Nin] = i; Nin++; } logverb("%i reference sources within the image.\n", Nin); iscale = sip_pixel_scale(sipout); ijitter = indexjitter / iscale; for (i=0; i<Nfield; i++) { R2 = distsq(qc, fieldxy + 2*i, 2); fieldsigma2s[i] = (square(fieldjitter) + square(ijitter)) * (1.0 + gamma * R2/quadR2); } pix2 = square(fieldjitter); logodds = verify_star_lists_ror(indexpix, Nin, fieldxy, fieldsigma2s, Nfield, pix2, gamma, qc, quadR2, W, H, distractors, logodds_bail, HUGE_VAL, &besti, &odds, &theta, NULL, &testperm, &refperm); logverb("Logodds: %g\n", logodds); verify_count_hits(theta, besti, &nmatch, &nconf, &ndist); logverb("%i matches, %i distractors, %i conflicts (at best log-odds); %i field sources, %i index sources\n", nmatch, ndist, nconf, Nfield, Nin); verify_count_hits(theta, Nfield-1, &nmatch, &nconf, &ndist); logverb("%i matches, %i distractors, %i conflicts (all sources)\n", nmatch, ndist, nconf); if (log_get_level() >= LOG_VERB) { matchobj_log_hit_miss(theta, testperm, besti+1, Nfield, LOG_VERB, "Hit/miss: "); } if (TWEAK_DEBUG_PLOTS) { TWEAK_DEBUG_PLOT("final", W, H, Nfield, fieldxy, fieldsigma2s, Nin, indexpix, besti, theta, sipout->wcstan.crpix, testperm, qc); } } if (newtheta) { // undo the "indexpix" inside-image-bounds cut. (*newtheta) = malloc(Nfield * sizeof(int)); for (i=0; i<Nfield; i++) { int nt; if (theta[i] < 0) nt = theta[i]; else nt = indexin[refperm[theta[i]]]; (*newtheta)[i] = nt; } } free(theta); free(refperm); if (newodds) *newodds = odds; else free(odds); logverb("Tweak2: final WCS:\n"); if (log_get_level() >= LOG_VERB) sip_print_to(sipout, stdout); if (p_logodds) *p_logodds = logodds; if (p_besti) *p_besti = besti; free(indexin); free(indexpix); free(fieldsigma2s); free(weights); free(matchxyz); free(matchxy); return sipout; }