int main(int argc, char** args) { int c; FILE* fconst = NULL; uint32_t nstars; size_t mapsize; void* map; unsigned char* hip; FILE* fhip = NULL; int i; pl* cstars; il* alluniqstars; sl* shortnames; while ((c = getopt(argc, args, OPTIONS)) != -1) { switch (c) { case 'h': print_help(args[0]); exit(0); } } if (optind != argc) { print_help(args[0]); exit(-1); } for (i=0; i<sizeof(const_dirs)/sizeof(char*); i++) { char fn[256]; snprintf(fn, sizeof(fn), "%s/%s", const_dirs[i], constfn); fprintf(stderr, "render_constellation: Trying file: %s\n", fn); fconst = fopen(fn, "rb"); if (fconst) break; } if (!fconst) { fprintf(stderr, "render_constellation: couldn't open any constellation files.\n"); return -1; } for (i=0; i<sizeof(hip_dirs)/sizeof(char*); i++) { char fn[256]; snprintf(fn, sizeof(fn), "%s/%s", hip_dirs[i], hipparcos_fn); fprintf(stderr, "render_constellation: Trying hip file: %s\n", fn); fhip = fopen(fn, "rb"); if (fhip) break; } if (!fhip) { fprintf(stderr, "render_constellation: unhip\n"); return -1; } // first 32-bit int: if (fread(&nstars, 4, 1, fhip) != 1) { fprintf(stderr, "render_constellation: failed to read nstars.\n"); return -1; } v32_letoh(&nstars); fprintf(stderr, "render_constellation: Found %i Hipparcos stars\n", nstars); mapsize = nstars * HIP_SIZE + HIP_OFFSET; map = mmap(0, mapsize, PROT_READ, MAP_SHARED, fileno(fhip), 0); hip = ((unsigned char*)map) + HIP_OFFSET; // for each constellation, its il* of lines. cstars = pl_new(16); alluniqstars = il_new(16); shortnames = sl_new(16); for (c=0;; c++) { char shortname[16]; int nlines; int i; il* stars; if (feof(fconst)) break; if (fscanf(fconst, "%s %d ", shortname, &nlines) != 2) { fprintf(stderr, "failed to parse name+nlines (constellation %i)\n", c); fprintf(stderr, "file offset: %i (%x)\n", (int)ftello(fconst), (int)ftello(fconst)); return -1; } //fprintf(stderr, "Name: %s. Nlines %i.\n", shortname, nlines); stars = il_new(16); sl_append(shortnames, shortname); pl_append(cstars, stars); for (i=0; i<nlines; i++) { int star1, star2; if (fscanf(fconst, " %d %d", &star1, &star2) != 2) { fprintf(stderr, "failed parse star1+star2\n"); return -1; } il_insert_unique_ascending(alluniqstars, star1); il_insert_unique_ascending(alluniqstars, star2); il_append(stars, star1); il_append(stars, star2); } fscanf(fconst, "\n"); } fprintf(stderr, "render_constellations: Read %i constellations.\n", c); printf("static const int constellations_N = %i;\n", sl_size(shortnames)); /* for (c=0; c<sl_size(shortnames); c++) { printf("static const char* shortname_%i = \"%s\";\n", c, sl_get(shortnames, c)); } printf("static const char* shortnames[] = {"); for (c=0; c<sl_size(shortnames); c++) { printf("shortname_%i,", c); } printf("};\n"); */ printf("static const char* shortnames[] = {"); for (c=0; c<sl_size(shortnames); c++) { printf("\"%s\",", sl_get(shortnames, c)); } printf("};\n"); printf("static const int constellation_nlines[] = {"); for (c=0; c<pl_size(cstars); c++) { il* stars = pl_get(cstars, c); printf("%i,", il_size(stars)/2); } printf("};\n"); for (c=0; c<pl_size(cstars); c++) { il* stars = pl_get(cstars, c); printf("static const int constellation_lines_%i[] = {", c); for (i=0; i<il_size(stars); i++) { int s = il_get(stars, i); int ms = il_index_of(alluniqstars, s); printf("%s%i", (i?",":""), ms); } printf("};\n"); } printf("static const int* constellation_lines[] = {"); for (c=0; c<pl_size(cstars); c++) { printf("constellation_lines_%i,", c); } printf("};\n"); printf("static const int stars_N = %i;\n", il_size(alluniqstars)); printf("static const double star_positions[] = {"); for (i=0; i<il_size(alluniqstars); i++) { int s = il_get(alluniqstars, i); double ra, dec; hip_get_radec(hip, s, &ra, &dec); printf("%g,%g,", ra, dec); } printf("};\n"); munmap(map, mapsize); fclose(fconst); fclose(fhip); return 0; }
int main(int argc, char *argv[]) { int argchar; char* infn = NULL; char* outfn = NULL; anbool tostdout = FALSE; FILE* fin = NULL; FILE* fout = NULL; il* exts; il* sizes; int i; char* progname = argv[0]; int Next; anqfits_t* anq; exts = il_new(16); sizes = il_new(16); while ((argchar = getopt (argc, argv, OPTIONS)) != -1) switch (argchar) { case 'e': il_append(exts, atoi(optarg)); break; case 's': il_append(sizes, atoi(optarg)); break; case 'i': infn = optarg; break; case 'o': outfn = optarg; break; case '?': case 'h': printHelp(progname); return 0; default: return -1; } log_init(LOG_MSG); if (!infn || !outfn || !il_size(exts) || (il_size(exts) != il_size(sizes))) { printHelp(progname); exit(-1); } if (infn) { fin = fopen(infn, "rb"); if (!fin) { SYSERROR("Failed to open input file %s", infn); exit(-1); } } anq = anqfits_open(infn); if (!anq) { ERROR("Failed to open input file %s", infn); exit(-1); } Next = anqfits_n_ext(anq); if (Next == -1) { ERROR("Couldn't determine how many extensions are in file %s", infn); exit(-1); } else { logverb("File %s contains %i FITS extensions.\n", infn, Next); } for (i=0; i<il_size(exts); i++) { int e = il_get(exts, i); int s = il_get(sizes, i); if (e < 0 || e >= Next) { logerr("Extension %i is not valid: must be in [%i, %i]\n", e, 0, Next); exit(-1); } if (s != 2 && s != 4 && s != 8) { logerr("Invalid byte size %i: must be 2, 4, or 8.\n", s); exit(-1); } } if (!strcmp(outfn, "-")) tostdout = TRUE; if (tostdout) fout = stdout; else { fout = fopen(outfn, "wb"); if (!fout) { SYSERROR("Failed to open output file %s", outfn); exit(-1); } } for (i=0; i<Next; i++) { int hdrstart, hdrlen, datastart, datalen; int ind; int size; ind = il_index_of(exts, i); if (ind == -1) { size = 0; } else { size = il_get(sizes, ind); } hdrstart = anqfits_header_start(anq, i); hdrlen = anqfits_header_size (anq, i); datastart = anqfits_data_start(anq, i); datalen = anqfits_data_size (anq, i); if (hdrlen) { if (pipe_file_offset(fin, hdrstart, hdrlen, fout)) { ERROR("Failed to write header for extension %i", i); exit(-1); } } if (!datalen) continue; if (size) { int Nitems = datalen / size; int j; char buf[size]; logmsg("Extension %i: flipping words of length %i bytes.\n", i, size); for (j=0; j<Nitems; j++) { if (fread(buf, size, 1, fin) != 1) { SYSERROR("Failed to read data element %i from extension %i", j, i); exit(-1); } endian_swap(buf, size); if (fwrite(buf, size, 1, fout) != 1) { SYSERROR("Failed to write data element %i to extension %i", j, i); exit(-1); } } } else { logmsg("Extension %i: copying verbatim.\n", i); // passthrough if (pipe_file_offset(fin, datastart, datalen, fout)) { ERROR("Failed to write data for extension %i", i); exit(-1); } } } fclose(fin); anqfits_close(anq); if (!tostdout) fclose(fout); il_free(exts); il_free(sizes); return 0; }