/*++++++++++++++ .IDENTIFICATION cat22.c .LANGUAGE C .AUTHOR Francois Ochsenbein [CDS] .ENVIRONMENT NOMAD-1.0 Catalogue .KEYWORDS CDS Catalogue Server .VERSION 1.0 09-Nov-2005: Read one of the 22-int file .COMMENTS List one of the files containing the > 1,000,000,000 stars. ---------------*/ #include #include /* Malloc */ #include #include #include /* O_BINARY */ #include static char usage[] = "\ Usage: cat22 [-o binfile] input_file [input_file...] \n\ "; static char help[] = "\ -o: write a binary copy\n\ input: a \"mXXXX.cat\" original file\n\ "; static int swapping; /*================================================================== Internal Utilities *==================================================================*/ static void swap1 (array, nshort) /*++++++++++++++++ .PURPOSE Swap the bytes in the array of shorts if necessary .RETURNS 0/1/2 (type of swap) .REMARKS Useful for big-endian machines -----------------*/ short *array ; /* IN: The array to convert */ int nshort ; /* IN: The number of integers */ { register int m, n ; register short *p, *e ; for (p=array, e=p+nshort; p>8) & 0xff ; m |= (n<<8) ; *p = m ; } } static void swap (array, nint) /*++++++++++++++++ .PURPOSE Swap the bytes in the array of integers if necessary .RETURNS 0/1/2 (type of swap) .REMARKS Useful for big-endian machines -----------------*/ int *array ; /* IN: The array to convert */ int nint ; /* IN: The number of integers */ { char *p, *e ; int n ; p = (char *)array ; e = p + 4*nint ; if (swapping == 1) swap1((short *)array, 2*nint) ; else if (swapping == 2) while (p < e) { n = p[0] ; p[0] = p[3] ; p[3] = n ; n = p[1] ; p[1] = p[2] ; p[2] = n ; p += 4 ; } } static char fmt22[] = "\ %10d`%04d %9d`%04d %+7d`%04d %+7d`%04d %5d,%5d %5d %5d %5d %5d %5d %5d x%08X"; static char head22[] = "\ RAmas`sig. SDmas`sig. pmRA`sig. pmDE`sig. EpRA, EpDE \ Bmag Vmag Rmag Jmag Hmag Kmag Flags "; static char IDc[] = "UMYuT"; /* USNO 2MASS YB6 UCAC2 Tycho */ /*================================================================== Main Program *==================================================================*/ main (argc, argv) int argc; char **argv; { FILE *f, *of ; char *p, *oname ; int i, rec[22], zone, id; of = stdout; oname = (char *)0; while (argc > 2) { p = *++argv; --argc; if (*p != '-') break; if (p[1] != 'o') break; p = *++argv; argc--; of = fopen(p, "w"); if (!of) perror(p); oname = p; } if (argc != 2) { fprintf(stderr, "%s%s", usage, help) ; exit(1) ; } /* Verify the Swapping ! */ { static int value ; char *v ; value = 0x010203 ; v = (char *)(&value) ; if ((v[0] == 0) && (v[1] == 1) && (v[2] == 2)) swapping = 0 ; /* No swap necessary */ else if ((v[0] == 1) && (v[1] == 0) && (v[2] == 3)) swapping = 1 ; /* Half-word swap */ else if ((v[0] == 3) && (v[1] == 2) && (v[2] == 1)) swapping = 2 ; /* Full-word swap */ else { fprintf(stderr, "****Irrationnal Byte Swap %02x%02x%02X%02x\n", v[0]&0xff, v[1]&0xff, v[2]&0xff, v[3]&0xff) ; exit(2) ; } } swapping ^= 2; /* ASSUME THIS TYPE OF SWAPPING */ fprintf(stderr, "#...swapping type=%d\n", swapping) ; memset(&rec, 0, sizeof(rec)) ; while (--argc > 0) { fprintf(stderr, "----Dealing with file: %s", *++argv) ; if (!(f = fopen(*argv, "rb"))) { perror(*argv) ; exit(1) ; } fflush(stderr) ; /* Get the Zone Number (from file xxxx.cat) */ zone = atoi(p); fprintf(of, "#%-7s ", "Seq#"); fprintf(of, "%s", head22); for (i=0; IDc[i]; i++) fprintf(of, " %c", IDc[i]); fprintf(of, "\n"); for (id=0; fread(rec, sizeof(long), 22, f) > 0 ; ) { id++ ; swap(rec, 22); fprintf(of, "#%-7d ", id); fprintf(of, fmt22, rec[0], rec[2], rec[1], rec[3], rec[4], rec[6], rec[5], rec[7], (rec[8]+50)/100, (rec[9]+50)/100, rec[10], rec[11], rec[12], rec[13], rec[14], rec[15], rec[21]); for (i=0; IDc[i]; i++) { if ((rec+16)[i]) fprintf(of, " %c%-10d", IDc[i], (rec+16)[i]); } fprintf(of, "\n"); } fprintf(stderr, " => %d objects.\n", id); close(f) ; } }