head 1.2; access ; symbols ; locks ; strict; comment @ * @; 1.2 date 94.01.17.11.09.48; author hebo; state Exp; branches ; next 1.1; 1.1 date 92.06.15.09.14.38; author hebo; state Exp; branches ; next 1.0; 1.0 date 92.06.11.15.42.36; author hebo; state Exp; branches ; next ; desc @Program to read raw OASIS file, extract binary data from spectroradiometer. @ 1.2 log @Misc changes @ text @/****************************************************************************/ /* Copyright 1991 MBARI */ /****************************************************************************/ /* $Header: getspect.c,v 1.1 92/06/15 09:14:38 hebo Exp $ */ /* Summary : Program to extract spectro data from an OASIS uuencoded file */ /* Filename : getspect.c */ /* Author : Robert Herlien (rah) */ /* Project : OASIS Mooring */ /* $Revision: 1.1 $ */ /* Created : 12/05/91 */ /****************************************************************************/ #include /* Standard I/O */ #include /* For exit() */ #include /* MBARI type definitions */ #include /* MBARI constants */ #include /* OASIS controller definitions */ #include /* Time */ #include /* for memcpy() */ #include /* for strncpy() */ #define BUFSIZE 2048 /* Size of sample buffer */ #define NAMESIZE 512 /* Space allocated for file names */ #define UUBUFSIZE 512 /* Size of uu decode buffer */ #define OASIS_DIR "/usr/local/oasis/raw" #define DEC(c) (((c) - ' ') & 0x3f) /* uudecode macro */ /********************************/ /* External Data */ /********************************/ Extern char *optarg; /* Option argument from getopt() */ Extern Int optind; /* Option index from getopt() */ Extern Int opterr; /* getopt() error flag */ /********************************/ /* Global Data */ /********************************/ Global Byte buffer[BUFSIZE]; /* Decoded data buffer */ Global Byte uubuf[UUBUFSIZE]; /* Buffer for raw uuencoded data */ Global char infile[NAMESIZE]; /* Input file name */ Global LogRecHdr hdr; /* Logging record header */ /********************************/ /* Forward Declarations */ /********************************/ Void decode_file( char *s ); Nat16 getIntelword( Byte *p ); Nat32 getIntellong( Byte *p ); FILE *uuopen( char *name ); Int uuread( Byte *buf, Int len, FILE *fd ); Int uugetline( Byte *buf, Int len, FILE *fd ); /************************************************************************/ /* Function : main */ /* Purpose : Main routine */ /* Inputs : argc, argv */ /* Outputs : none */ /************************************************************************/ Void main( Int argc, char **argv ) { Int i; if ( argc < 2 ) { fprintf(stderr, "Usage: %s input_file\n", argv[0] ); fprintf(stderr, " where input_file is a raw oasis data file\n" ); fprintf(stderr, " Writes binary data to standard out.\n" ); fprintf(stderr, " Otherwise, you'll get garbage on screen.\n" ); exit( 1 ); } for ( i = optind; i < argc; i++ ) decode_file( argv[i] ); exit( 0 ); } /* main() */ /************************************************************************/ /* Function : decode_file */ /* Purpose : Decode one data file, print results to stdout */ /* Inputs : File name */ /* Outputs : none */ /************************************************************************/ Void decode_file( char *filename ) { Int cc, len, type; FILE *infd; Status rtn; if ( (infd = uuopen(filename)) == (FILE *)NULL ) { sprintf( infile, "%s/%s", OASIS_DIR, filename ); fprintf(stderr, "Trying %s\n", infile); if ( (infd = uuopen(infile)) == (FILE *)NULL ) exit( 1 ); } while ( (cc = uugetline(buffer, 9, infd)) != EOF ) { if ( cc == 0 ) continue; if ( cc != 9 ) fprintf(stderr, "Bad record header in %s\n", infile); hdr.log_type = buffer[0]; hdr.log_nmbr = getIntelword(&buffer[1]); hdr.log_len = getIntelword(&buffer[3]); hdr.log_time = getIntellong(&buffer[5]); cc = hdr.log_len; if ( cc > BUFSIZE ) { cc = BUFSIZE; fprintf(stderr, "Record too long in file %s. Truncating\n", infile); } if ( (len = uuread(buffer, cc, infd)) != cc) fprintf(stderr, "Bad record in file %s.\n", infile); if ( hdr.log_type == SPECTRO ) { if ( (buffer[0] != 'S') || (getIntelword(&buffer[1]) != len) ) fprintf(stderr, "Bad data format for spectro record.\n" ); else { fwrite( buffer, 1, len, stdout ); printf( "\r\n" ); } } } fclose( infd ); } /* main() */ /************************************************************************/ /* Function : getIntelword */ /* Purpose : Get a word in Intel format from data stream */ /* Inputs : Ptr to data stream */ /* Outputs : Word */ /************************************************************************/ Nat16 getIntelword( Byte *p ) { Nat16 rtn; rtn = (Nat16)(*p); rtn += (Nat16)(p[1] << 8); return( rtn ); } /* getIntelword() */ /************************************************************************/ /* Function : getIntellong */ /* Purpose : Get a longword in Intel format from data stream */ /* Inputs : Ptr to data stream */ /* Outputs : Long */ /************************************************************************/ Nat32 getIntellong( Byte *p ) { Nat32 rtn; rtn = (Nat32)getIntelword(p); rtn += ((Nat32)getIntelword(&p[2]) << 16); return( rtn ); } /* getIntellong() */ /************************************************************************/ /* Function : uuopen */ /* Purpose : Open a uuencoded file, check its "begin" line */ /* Inputs : Name of file */ /* Outputs : FILE pointer */ /************************************************************************/ FILE *uuopen( char *name ) { FILE *fd; char *p; if ( (fd = fopen(name, "rb")) == (FILE *)NULL ) fprintf(stderr, "Cannot open %s\n", name); else { p = (char *)uubuf; while ( fgets(p, sizeof(uubuf), fd) != NULL ) { while ( isspace(*p) ) p++; if (strncmp(p, "begin ", 6) == 0) return( fd ); } fprintf(stderr, "No begin line in %s\n", name); } return( (FILE *)NULL ); } /* uuopen() */ /************************************************************************/ /* Function : uudecode */ /* Purpose : Decode a group of 3 binary bytes from 4 input characters*/ /* Inputs : Ptr to input chars, ptr to output, number of bytes */ /* Outputs : None */ /************************************************************************/ Void uudecode( Byte *in, Byte *out, Int len ) { Byte *p; p =out; if ( len >= 1 ) *p++ = (DEC(*in) << 2) | (DEC(in[1]) >> 4); if ( len >= 2 ) *p++ = (DEC(in[1]) << 4) | (DEC(in[2]) >> 2); if ( len >= 3 ) *p = (DEC(in[2]) << 6) | (DEC(in[3])); } /* uudecode() */ /************************************************************************/ /* Function : uugetline */ /* Purpose : Read and decode one line from a uuencoded file */ /* Inputs : Buffer for resulting data, size of buffer, FILE ptr */ /* Outputs : Number characters read, UUERROR if error, or EOF */ /************************************************************************/ Int uugetline( Byte *buf, Int len, FILE *fd ) { Int uulen, i; Byte *p, *q; char *p1; if ( fgets((char *)uubuf, sizeof(uubuf), fd) == NULL ) return( EOF ); if (strncmp((char *)uubuf, "end", 3) == 0) return( EOF ); uulen = DEC(uubuf[0]); if ( (p1 = strchr((char *)uubuf, '\n')) != NULL ) *p1 = '\0'; if ( (p1 = strchr((char *)uubuf, '\r')) != NULL ) *p1 = '\0'; if ( strlen((char *)uubuf) != (((uulen + 2)/3 * 4) + 1) ) return( UUERROR ); p = &uubuf[1]; q = buf; for ( i = uulen; i > 0; i -= 3 ) { uudecode( p, q, i ); p += 4; q += 3; } return( uulen ); } /* uugetline() */ /************************************************************************/ /* Function : uuread */ /* Purpose : Read and decode len bytes from a uuencoded file */ /* Inputs : Buffer for resulting data, size of buffer, FILE ptr */ /* Outputs : Number characters read, UUERROR if error, or EOF */ /* Comment : OASIS records should always begin and end at new line */ /* This function calls uugetline() for the appropriate */ /* number of bytes, and returns UUERROR if size wrong */ /************************************************************************/ Int uuread( Byte *buf, Int len, FILE *fd ) { Int i, left; Byte *p; for ( p = buf, left = len; left > 0; ) { if ( (i = uugetline(p, left, fd)) <= 0 ) return( i ); left -= i; p += i; if ( left < 0 ) return( UUERROR ); } return( len ); } /* uuread() */ @ 1.1 log @Output to stdout instead of file. Error msgs to stderr. @ text @d4 1 a4 1 /* $Header: getspect.c,v 1.0 92/06/15 09:08:10 hebo Exp $ */ d9 1 a9 1 /* $Revision: 1.0 $ */ d31 9 d46 1 d53 1 d55 1 d62 4 a65 4 /* function : main */ /* purpose : Main routine */ /* inputs : argc, argv */ /* outputs : none */ d68 1 a68 1 main( int argc, char **argv ) d70 1 a70 3 Int cc, len, type; FILE *infd; Status rtn; d81 22 a102 2 strncpy( infile, argv[1], NAMESIZE ); if ( (infd = uuopen(infile)) == (FILE *)NULL ) d104 1 a104 1 sprintf( infile, "%s/%s", OASIS_DIR, argv[1] ); d110 1 a110 1 while ( (cc = uugetline(buffer, 3, infd)) != EOF ) d115 2 a116 2 if ( cc != 3 ) fprintf(stderr, "Bad record header in file %s.\n", infile); d118 5 a122 2 type = (Nat32)(buffer[0]); cc = getIntelword( &buffer[1] ); d133 1 a133 1 if ( type == SPECTRO ) a146 1 exit( 0 ); d167 18 @ 1.0 log @Initial revision @ text @d4 1 d6 1 a6 1 /* Filename : spectro.c */ d9 1 a9 1 /* $Revision: 1.1 $ */ d59 1 a59 1 FILE *infd, *outfd; d62 1 a62 1 if ( argc < 3 ) d64 4 a67 3 printf("Usage: %s input_file output_file\n", argv[0] ); printf(" where input_file is a raw oasis data file\n" ); printf(" and output_file is the binary file to create\n"); d75 1 a75 1 printf("Trying %s\n", infile); a79 6 if ( (outfd = fopen(argv[2], "wb")) == (FILE *)NULL ) { printf("Cannot create %s.\n", argv[2] ); exit( 1 ); } d86 1 a86 1 printf("Bad record header in file %s.\n", infile); d93 2 a94 1 printf("Record too long in file %s. Truncating\n", infile); d98 1 a98 1 printf("Bad record in file %s.\n", infile); d102 7 a108 2 fwrite( buffer, 1, len, outfd ); fwrite( "\r\n", 1, 2, outfd ); a112 1 fclose( outfd ); d150 1 a150 1 printf("Cannot open %s\n", name); d161 1 a161 1 printf("No begin line in %s\n", name); @