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* outfn = NULL; char* infn; int overwrite = 0; int loglvl = LOG_MSG; anbool do_u8 = TRUE; int downsample = 0; int downsample_as_reqd = 0; int extension = 0; int plane = 0; simplexy_t sparams; simplexy_t* params = &sparams; memset(params, 0, sizeof(simplexy_t)); while ((argchar = getopt (argc, argv, OPTIONS)) != -1) switch (argchar) { case 'L': params->Lorder = atoi(optarg); break; case 'w': params->dpsf = atof(optarg); break; case 'a': params->saddle = atof(optarg); break; case 'm': params->maxsize = atoi(optarg); break; case 'g': params->sigma = atof(optarg); break; case 'b': params->nobgsub = TRUE; break; case 'G': params->nobgsub = TRUE; params->globalbg = atof(optarg); break; case 'P': plane = atoi(optarg); break; case 's': params->halfbox = atoi(optarg); break; case 'p': params->plim = atof(optarg); break; case 'B': params->bgimgfn = optarg; break; case 'S': params->bgsubimgfn = optarg; break; case 'M': params->maskimgfn = optarg; break; case 'U': params->smoothimgfn = optarg; break; case 'C': params->blobimgfn = optarg; break; case 'e': extension = atoi(optarg); break; case 'D': downsample_as_reqd = atoi(optarg); break; case 'H': downsample = 2; break; case 'd': downsample = atoi(optarg); break; case '8': do_u8 = FALSE; break; case 'v': loglvl++; break; case 'O': overwrite = 1; break; case 'o': outfn = strdup(optarg); break; case '?': case 'h': printHelp(); exit(0); } if (optind != argc - 1) { printHelp(); exit(-1); } infn = argv[optind]; log_init(loglvl); logverb("infile=%s\n", infn); if (!outfn) { // Create xylist filename (by trimming '.fits') asprintf_safe(&outfn, "%.*s.xy.fits", (int)(strlen(infn)-5), infn); logverb("outfile=%s\n", outfn); } if (overwrite && file_exists(outfn)) { logverb("Deleting existing output file \"%s\"...\n", outfn); if (unlink(outfn)) { SYSERROR("Failed to delete existing output file \"%s\"", outfn); exit(-1); } } if (downsample) logverb("Downsampling by %i\n", downsample); if (image2xy_files(infn, outfn, do_u8, downsample, downsample_as_reqd, extension, plane, params)) { ERROR("image2xy failed."); exit(-1); } free(outfn); 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; }
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); } }
tan_t* tan_read_header(const qfits_header* hdr, tan_t* dest) { tan_t tan; double nil = -1e300; char* ct1; char* ct2; int swap; int W, H; anbool is_sin; memset(&tan, 0, sizeof(tan_t)); ct1 = fits_get_dupstring(hdr, "CTYPE1"); ct2 = fits_get_dupstring(hdr, "CTYPE2"); swap = check_tan_ctypes(ct1, ct2, &is_sin); if (swap == -1) { ERROR("TAN header: expected CTYPE1 = RA---TAN, CTYPE2 = DEC--TAN " "(or vice versa), or RA---SIN, DEC--SIN or vice versa; " "got CTYPE1 = \"%s\", CYTPE2 = \"%s\"\n", ct1, ct2); } free(ct1); free(ct2); if (swap == -1) return NULL; sip_get_image_size(hdr, &W, &H); tan.imagew = W; tan.imageh = H; { const char* keys[] = { "CRVAL1", "CRVAL2", "CRPIX1", "CRPIX2", "CD1_1", "CD1_2", "CD2_1", "CD2_2" }; double* vals[] = { &(tan.crval[0]), &(tan.crval[1]), &(tan.crpix[0]), &(tan.crpix[1]), &(tan.cd[0][0]), &(tan.cd[0][1]), &(tan.cd[1][0]), &(tan.cd[1][1]) }; int i; for (i=0; i<4; i++) { *(vals[i]) = qfits_header_getdouble(hdr, keys[i], nil); if (*(vals[i]) == nil) { ERROR("TAN header: missing or invalid value for \"%s\"", keys[i]); return NULL; } } // Try CD int gotcd = 1; char* complaint = NULL; for (i=4; i<8; i++) { *(vals[i]) = qfits_header_getdouble(hdr, keys[i], nil); if (*(vals[i]) == nil) { asprintf_safe(&complaint, "TAN header: missing or invalid value for key \"%s\"", keys[i]); gotcd = 0; break; } } if (!gotcd) { double cdelt1,cdelt2; // Try CDELT char* key = "CDELT1"; cdelt1 = qfits_header_getdouble(hdr, key, nil); if (cdelt1 == nil) { ERROR("%s; also tried but didn't find \"%s\"", complaint, key); free(complaint); return NULL; } key = "CDELT2"; cdelt2 = qfits_header_getdouble(hdr, key, nil); if (cdelt2 == nil) { ERROR("%s; also tried but didn't find \"%s\"", complaint, key); free(complaint); return NULL; } tan.cd[0][0] = cdelt1; tan.cd[0][1] = 0.0; tan.cd[1][0] = 0.0; tan.cd[1][1] = cdelt2; } } if (swap == 1) { double tmp; tmp = tan.crval[0]; tan.crval[0] = tan.crval[1]; tan.crval[1] = tmp; // swap CD1_1 <-> CD2_1 tmp = tan.cd[0][0]; tan.cd[0][0] = tan.cd[1][0]; tan.cd[1][0] = tmp; // swap CD1_2 <-> CD2_2 tmp = tan.cd[0][1]; tan.cd[0][1] = tan.cd[1][1]; tan.cd[1][1] = tmp; } tan.sin = is_sin; if (!dest) dest = malloc(sizeof(tan_t)); memcpy(dest, &tan, sizeof(tan_t)); return dest; }
int main(int argc, char** args) { int c; anbool help = FALSE; char* outdir = NULL; char* cmd; int i, j, f; int inputnum; int rtn; sl* engineargs; int nbeargs; anbool fromstdin = FALSE; anbool overwrite = FALSE; anbool cont = FALSE; anbool skip_solved = FALSE; anbool makeplots = TRUE; double plotscale = 1.0; char* inbgfn = NULL; char* bgfn = NULL; char* me; anbool verbose = FALSE; int loglvl = LOG_MSG; char* outbase = NULL; anbool usecurl = TRUE; bl* opts; augment_xylist_t theallaxy; augment_xylist_t* allaxy = &theallaxy; int nmyopts; char* removeopts = "ixo\x01"; char* newfits; char* kmz = NULL; char* scamp = NULL; char* scampconfig = NULL; char* index_xyls; anbool just_augment = FALSE; anbool engine_batch = FALSE; bl* batchaxy = NULL; bl* batchsf = NULL; sl* outfiles; sl* tempfiles; // these are deleted after the outer loop over input files sl* tempfiles2; sl* tempdirs; anbool timestamp = FALSE; anbool tempaxy = FALSE; errors_print_on_exit(stderr); fits_use_error_system(); me = find_executable(args[0], NULL); engineargs = sl_new(16); append_executable(engineargs, "astrometry-engine", me); // output filenames. outfiles = sl_new(16); tempfiles = sl_new(4); tempfiles2 = sl_new(4); tempdirs = sl_new(4); rtn = 0; nmyopts = sizeof(options)/sizeof(an_option_t); opts = opts_from_array(options, nmyopts, NULL); augment_xylist_add_options(opts); // remove duplicate short options. for (i=0; i<nmyopts; i++) { an_option_t* opt1 = bl_access(opts, i); for (j=nmyopts; j<bl_size(opts); j++) { an_option_t* opt2 = bl_access(opts, j); if (opt2->shortopt == opt1->shortopt) bl_remove_index(opts, j); } } // remove unwanted augment-xylist options. for (i=0; i<strlen(removeopts); i++) { for (j=nmyopts; j<bl_size(opts); j++) { an_option_t* opt2 = bl_access(opts, j); if (opt2->shortopt == removeopts[i]) bl_remove_index(opts, j); } } // which options are left? /*{ char options[256]; memset(options, 0, 256); printf("options:\n"); for (i=0; i<bl_size(opts); i++) { an_option_t* opt = bl_access(opts, i); printf(" %c (%i) %s\n", opt->shortopt, (int)opt->shortopt, opt->name); options[(int)((opt->shortopt + 256) % 256)] = 1; } printf("Remaining short opts:\n"); for (i=0; i<256; i++) { if (!options[i]) printf(" %c (%i, 0x%x)\n", (char)i, i, i); } }*/ augment_xylist_init(allaxy); // default output filename patterns. allaxy->axyfn = "%s.axy"; allaxy->matchfn = "%s.match"; allaxy->rdlsfn = "%s.rdls"; allaxy->solvedfn = "%s.solved"; allaxy->wcsfn = "%s.wcs"; allaxy->corrfn = "%s.corr"; newfits = "%s.new"; index_xyls = "%s-indx.xyls"; while (1) { int res; c = opts_getopt(opts, argc, args); //printf("option %c (%i)\n", c, (int)c); if (c == -1) break; switch (c) { case '\x91': allaxy->axyfn = optarg; break; case '\x90': tempaxy = TRUE; break; case '\x88': timestamp = TRUE; break; case '\x84': plotscale = atof(optarg); break; case '\x85': inbgfn = optarg; break; case '\x87': allaxy->assume_fits_image = TRUE; break; case '(': engine_batch = TRUE; break; case '@': just_augment = TRUE; break; case 'U': index_xyls = optarg; break; case 'n': scampconfig = optarg; break; case 'i': scamp = optarg; break; case 'Z': kmz = optarg; break; case 'N': newfits = optarg; break; case 'h': help = TRUE; break; case 'v': sl_append(engineargs, "--verbose"); verbose = TRUE; allaxy->verbosity++; loglvl++; break; case 'D': outdir = optarg; break; case 'o': outbase = optarg; break; case 'b': case '\x89': sl_append(engineargs, "--config"); append_escape(engineargs, optarg); break; case 'f': fromstdin = TRUE; break; case 'O': overwrite = TRUE; break; case 'p': makeplots = FALSE; break; case 'G': usecurl = FALSE; break; case 'K': cont = TRUE; break; case 'J': skip_solved = TRUE; break; default: res = augment_xylist_parse_option(c, optarg, allaxy); if (res) { rtn = -1; goto dohelp; } } } if ((optind == argc) && !fromstdin) { printf("ERROR: You didn't specify any files to process.\n"); help = TRUE; } if (help) { dohelp: print_help(args[0], opts); exit(rtn); } bl_free(opts); // --dont-augment: advertised as just write xy file, // so quit after doing that. if (allaxy->dont_augment) { just_augment = TRUE; } log_init(loglvl); if (timestamp) log_set_timestamp(TRUE); if (kmz && starts_with(kmz, "-")) logmsg("Do you really want to save KMZ to the file named \"%s\" ??\n", kmz); if (starts_with(newfits, "-")) { logmsg("Do you really want to save the new FITS file to the file named \"%s\" ??\n", newfits); } if (engine_batch) { batchaxy = bl_new(16, sizeof(augment_xylist_t)); batchsf = bl_new(16, sizeof(solve_field_args_t)); } // Allow (some of the) default filenames to be disabled by setting them to "none". allaxy->matchfn = none_is_null(allaxy->matchfn); allaxy->rdlsfn = none_is_null(allaxy->rdlsfn); allaxy->solvedfn = none_is_null(allaxy->solvedfn); allaxy->solvedinfn = none_is_null(allaxy->solvedinfn); allaxy->wcsfn = none_is_null(allaxy->wcsfn); allaxy->corrfn = none_is_null(allaxy->corrfn); newfits = none_is_null(newfits); index_xyls = none_is_null(index_xyls); if (outdir) { if (mkdir_p(outdir)) { ERROR("Failed to create output directory %s", outdir); exit(-1); } } // number of engine args not specific to a particular file nbeargs = sl_size(engineargs); f = optind; inputnum = 0; while (1) { char* infile = NULL; anbool isxyls; char* reason; int len; char* base; char* basedir; char* basefile = NULL; char *objsfn=NULL; char *ppmfn=NULL; char* downloadfn = NULL; char* suffix = NULL; sl* cmdline; anbool ctrlc; anbool isurl; augment_xylist_t theaxy; augment_xylist_t* axy = &theaxy; int j; solve_field_args_t thesf; solve_field_args_t* sf = &thesf; anbool want_pnm = FALSE; // reset augment-xylist args. memcpy(axy, allaxy, sizeof(augment_xylist_t)); memset(sf, 0, sizeof(solve_field_args_t)); if (fromstdin) { char fnbuf[1024]; if (!fgets(fnbuf, sizeof(fnbuf), stdin)) { if (ferror(stdin)) SYSERROR("Failed to read a filename from stdin"); break; } len = strlen(fnbuf); if (fnbuf[len-1] == '\n') fnbuf[len-1] = '\0'; infile = fnbuf; logmsg("Reading input file \"%s\"...\n", infile); } else { if (f == argc) break; infile = args[f]; f++; logmsg("Reading input file %i of %i: \"%s\"...\n", f - optind, argc - optind, infile); } inputnum++; cmdline = sl_new(16); if (!engine_batch) { // Remove arguments that might have been added in previous trips through this loop sl_remove_from(engineargs, nbeargs); } // Choose the base path/filename for output files. if (outbase) asprintf_safe(&basefile, outbase, inputnum, infile); else basefile = basename_safe(infile); //logverb("Base filename: %s\n", basefile); isurl = (!file_exists(infile) && (starts_with(infile, "http://") || starts_with(infile, "ftp://"))); if (outdir) basedir = strdup(outdir); else { if (isurl) basedir = strdup("."); else basedir = dirname_safe(infile); } //logverb("Base directory: %s\n", basedir); asprintf_safe(&base, "%s/%s", basedir, basefile); //logverb("Base name for output files: %s\n", base); // trim .gz, .bz2 // hmm, we drop the suffix in this case... len = strlen(base); if (ends_with(base, ".gz")) base[len-3] = '\0'; else if (ends_with(base, ".bz2")) base[len-4] = '\0'; len = strlen(base); // trim .xx / .xxx / .xxxx if (len >= 5) { for (j=3; j<=5; j++) { if (base[len - j] == '/') break; if (base[len - j] == '.') { base[len - j] = '\0'; suffix = base + len - j + 1; break; } } } logverb("Base: \"%s\", basefile \"%s\", basedir \"%s\", suffix \"%s\"\n", base, basefile, basedir, suffix); if (tempaxy) { axy->axyfn = create_temp_file("axy", axy->tempdir); sl_append_nocopy(tempfiles2, axy->axyfn); } else axy->axyfn = sl_appendf(outfiles, axy->axyfn, base); if (axy->matchfn) axy->matchfn = sl_appendf(outfiles, axy->matchfn, base); if (axy->rdlsfn) axy->rdlsfn = sl_appendf(outfiles, axy->rdlsfn, base); if (axy->solvedfn) axy->solvedfn = sl_appendf(outfiles, axy->solvedfn, base); if (axy->wcsfn) axy->wcsfn = sl_appendf(outfiles, axy->wcsfn, base); if (axy->corrfn) axy->corrfn = sl_appendf(outfiles, axy->corrfn, base); if (axy->cancelfn) axy->cancelfn = sl_appendf(outfiles, axy->cancelfn, base); if (axy->keepxylsfn) axy->keepxylsfn = sl_appendf(outfiles, axy->keepxylsfn, base); if (axy->pnmfn) axy->pnmfn = sl_appendf(outfiles, axy->pnmfn, base); if (newfits) sf->newfitsfn = sl_appendf(outfiles, newfits, base); if (kmz) sf->kmzfn = sl_appendf(outfiles, kmz, base); if (index_xyls) sf->indxylsfn = sl_appendf(outfiles, index_xyls, base); if (scamp) sf->scampfn = sl_appendf(outfiles, scamp, base); if (scampconfig) sf->scampconfigfn = sl_appendf(outfiles, scampconfig, base); if (makeplots) { objsfn = sl_appendf(outfiles, "%s-objs.png", base); sf->redgreenfn = sl_appendf(outfiles, "%s-indx.png", base); sf->ngcfn = sl_appendf(outfiles, "%s-ngc.png", base); } if (isurl) { if (suffix) downloadfn = sl_appendf(outfiles, "%s.%s", base, suffix); else downloadfn = sl_appendf(outfiles, "%s", base); } if (axy->solvedinfn) asprintf_safe(&axy->solvedinfn, axy->solvedinfn, base); // Do %s replacement on --verify-wcs entries... if (sl_size(axy->verifywcs)) { sl* newlist = sl_new(4); for (j=0; j<sl_size(axy->verifywcs); j++) sl_appendf(newlist, sl_get(axy->verifywcs, j), base); axy->verifywcs = newlist; } // ... and plot-bg if (inbgfn) asprintf_safe(&bgfn, inbgfn, base); if (axy->solvedinfn && axy->solvedfn && streq(axy->solvedfn, axy->solvedinfn)) { // solved input and output files are the same: don't delete the input! sl_remove_string(outfiles, axy->solvedfn); free(axy->solvedfn); axy->solvedfn = axy->solvedinfn; } free(basedir); free(basefile); if (skip_solved) { char* tocheck[] = { axy->solvedinfn, axy->solvedfn }; for (j=0; j<sizeof(tocheck)/sizeof(char*); j++) { if (!tocheck[j]) continue; logverb("Checking for solved file %s\n", tocheck[j]); if (file_exists(tocheck[j])) { logmsg("Solved file exists: %s; skipping this input file.\n", tocheck[j]); goto nextfile; } else { logverb("File \"%s\" does not exist.\n", tocheck[j]); } } } // Check for overlap between input and output filenames for (i = 0; i < sl_size(outfiles); i++) { char* fn = sl_get(outfiles, i); if (streq(fn, infile)) { logmsg("Output filename \"%s\" is the same as your input file.\n" "Refusing to continue.\n" "You can either choose a different output filename, or\n" "rename your input file to have a different extension.\n", fn); goto nextfile; } } // Check for (and possibly delete) existing output filenames. for (i = 0; i < sl_size(outfiles); i++) { char* fn = sl_get(outfiles, i); if (!file_exists(fn)) continue; if (cont) { } else if (overwrite) { if (unlink(fn)) { SYSERROR("Failed to delete an already-existing output file \"%s\"", fn); exit(-1); } } else { logmsg("Output file already exists: \"%s\".\n" "Use the --overwrite flag to overwrite existing files,\n" " or the --continue flag to not overwrite existing files but still try solving.\n", fn); logmsg("Continuing to next input file.\n"); goto nextfile; } } // if we're making "redgreen" plot, we need: if (sf->redgreenfn) { // -- index xylist if (!sf->indxylsfn) { sf->indxylsfn = create_temp_file("indxyls", axy->tempdir); sl_append_nocopy(tempfiles, sf->indxylsfn); } // -- match file. if (!axy->matchfn) { axy->matchfn = create_temp_file("match", axy->tempdir); sl_append_nocopy(tempfiles, axy->matchfn); } } // if index xyls file is needed, we need: if (sf->indxylsfn) { // -- wcs if (!axy->wcsfn) { axy->wcsfn = create_temp_file("wcs", axy->tempdir); sl_append_nocopy(tempfiles, axy->wcsfn); } // -- rdls if (!axy->rdlsfn) { axy->rdlsfn = create_temp_file("rdls", axy->tempdir); sl_append_nocopy(tempfiles, axy->rdlsfn); } } // Download URL... if (isurl) { sl_append(cmdline, usecurl ? "curl" : "wget"); if (!verbose) sl_append(cmdline, usecurl ? "--silent" : "--quiet"); sl_append(cmdline, usecurl ? "--output" : "-O"); append_escape(cmdline, downloadfn); append_escape(cmdline, infile); cmd = sl_implode(cmdline, " "); logmsg("Downloading...\n"); if (run_command(cmd, &ctrlc)) { ERROR("%s command %s", sl_get(cmdline, 0), (ctrlc ? "was cancelled" : "failed")); exit(-1); } sl_remove_all(cmdline); free(cmd); infile = downloadfn; } if (makeplots) want_pnm = TRUE; if (axy->assume_fits_image) { axy->imagefn = infile; if (axy->pnmfn) want_pnm = TRUE; } else { logverb("Checking if file \"%s\" ext %i is xylist or image: ", infile, axy->extension); fflush(NULL); reason = NULL; isxyls = xylist_is_file_xylist(infile, axy->extension, axy->xcol, axy->ycol, &reason); logverb(isxyls ? "xyls\n" : "image\n"); if (!isxyls) logverb(" (not xyls because: %s)\n", reason); free(reason); fflush(NULL); if (isxyls) axy->xylsfn = infile; else { axy->imagefn = infile; want_pnm = TRUE; } } if (want_pnm && !axy->pnmfn) { ppmfn = create_temp_file("ppm", axy->tempdir); sl_append_nocopy(tempfiles, ppmfn); axy->pnmfn = ppmfn; axy->force_ppm = TRUE; } axy->keep_fitsimg = (newfits || scamp); if (augment_xylist(axy, me)) { ERROR("augment-xylist failed"); exit(-1); } if (just_augment) goto nextfile; if (makeplots) { // Check that the plotting executables were built... char* exec = find_executable("plotxy", me); free(exec); if (!exec) { logmsg("Couldn't find \"plotxy\" executable - maybe you didn't build the plotting programs?\n"); logmsg("Disabling plots.\n"); makeplots = FALSE; } } if (makeplots) { // source extraction overlay if (plot_source_overlay(axy, me, objsfn, plotscale, bgfn)) makeplots = FALSE; } append_escape(engineargs, axy->axyfn); if (file_readable(axy->wcsfn)) axy->wcs_last_mod = file_get_last_modified_time(axy->wcsfn); else axy->wcs_last_mod = 0; if (!engine_batch) { run_engine(engineargs); after_solved(axy, sf, makeplots, me, verbose, axy->tempdir, tempdirs, tempfiles, plotscale, bgfn); } else { bl_append(batchaxy, axy); bl_append(batchsf, sf ); } fflush(NULL); // clean up and move on to the next file. nextfile: free(base); sl_free2(cmdline); if (!engine_batch) { free(axy->fitsimgfn); free(axy->solvedinfn); free(bgfn); // erm. if (axy->verifywcs != allaxy->verifywcs) sl_free2(axy->verifywcs); sl_remove_all(outfiles); if (!axy->no_delete_temp) delete_temp_files(tempfiles, tempdirs); } errors_print_stack(stdout); errors_clear_stack(); logmsg("\n"); } if (engine_batch) { run_engine(engineargs); for (i=0; i<bl_size(batchaxy); i++) { augment_xylist_t* axy = bl_access(batchaxy, i); solve_field_args_t* sf = bl_access(batchsf, i); after_solved(axy, sf, makeplots, me, verbose, axy->tempdir, tempdirs, tempfiles, plotscale, bgfn); errors_print_stack(stdout); errors_clear_stack(); logmsg("\n"); free(axy->fitsimgfn); free(axy->solvedinfn); // erm. if (axy->verifywcs != allaxy->verifywcs) sl_free2(axy->verifywcs); } if (!allaxy->no_delete_temp) delete_temp_files(tempfiles, tempdirs); bl_free(batchaxy); bl_free(batchsf); } if (!allaxy->no_delete_temp) delete_temp_files(tempfiles2, NULL); sl_free2(outfiles); sl_free2(tempfiles); sl_free2(tempfiles2); sl_free2(tempdirs); sl_free2(engineargs); free(me); augment_xylist_free_contents(allaxy); return 0; }