/***************************************************************************** * * swapscans.c * * DESCRIPTION: * Byte-swap Climsat scan-formatted data. * * LAST UPDATE: 31 May 2000 * * USAGE: * swapscans * * ARGUMENTS: * * Name Type Description * ---- ---- ----------- * infile string Input file, Climsat-formatted scan data * outfile string Output file, Climsat-formatted scan data * * FLAGS: * none * * AUTHOR: Don Anderson, CIRES/CDC, May 2000 * * MODIFICATIONS: * * NOTES: * *****************************************************************************/ #include #include #include #include #include #include "/home/climsat/src/utils/common_structures" #define HDRSZ 5000 /* Size of header */ /** Function prototypes **/ int hdrswap ( char*, char* ) ; int dataswap ( char*, char* ) ; int read_hdr ( FILE *, struct std_hdr * ) ; short shrtswap ( short ) ; long longswap ( long ) ; /** Main **/ void main ( int argc, char *argv[] ) { long buffsz ; /* Data buffer size */ long filesize ; /* Size in bytes of input file */ short flds ; /* Swapped no. of fields */ short hiflds ; /* Swapped no. of hi fields */ short hipix ; /* Swapped no. of hi pixels */ int i, s ; /* Generic counters */ FILE *ifile ; /* Pointer to file to read */ char ifile_str[80] ; /* Input netCDF filename string */ long itime ; /* Time in seconds from ??? */ FILE *ofile ; /* Pointer to file to write */ char ofile_str[80] ; /* Output filename string */ FILE readfile ; /* Pointer to file to test read */ char *ihdrbuff ; /* Buffer to hold in header data */ char *idatbuff ; /* Buffer to hold 1 scan of in data */ char *ohdrbuff ; /* Buffer to hold out header data */ char *odatbuff ; /* Buffer to hold 1 scan of out data */ short pix ; /* Swapped no. of pixels */ long reclen ; /* Record length */ struct std_hdr hdr ; /* Structure holding header data */ long scancnt ; /* Number of scans */ struct stat info1 ; /* Information on input file */ /** Initialize values **/ if ( argc < 3 ) { printf ( "Error: insufficient arguments\n" ) ; printf ( "Usage: swapscans \n" ) ; exit (1) ; } else { strcpy ( ifile_str, argv[1] ) ; strcpy ( ofile_str, argv[2] ) ; } printf ("Infile is %s\n", ifile_str); printf ("Outfile is %s\n", ofile_str); /** Open input file to read and output file to write **/ ifile = fopen ( ifile_str, "r" ) ; ofile = fopen ( ofile_str, "w" ) ; /** Determine number of scans in file **/ read_hdr ( ifile, &hdr ) ; flds = shrtswap ( hdr.fields ) ; hiflds = shrtswap ( hdr.hifields ) ; pix = shrtswap ( hdr.pixperscan ) ; hipix = shrtswap ( hdr.hipixperscan ) ; printf ("Fields %d %d\n", flds, hiflds ) ; printf ("Pixels %d %d\n", pix, hipix ) ; reclen = ( flds + hiflds + 4 ) * 2 * pix + ( hiflds + 4 ) * 2 * ( hipix / 2 ) + ( hiflds + 4 ) * 2 * hipix ; printf ("Record length is %ld\n", reclen) ; stat ( ifile_str, &info1 ) ; filesize = info1.st_size ; scancnt = ( filesize - HDRSZ ) / reclen ; printf ("Scan count is %d\n", scancnt ) ; /** Process the header **/ ihdrbuff = (char *) malloc ( HDRSZ ) ; ohdrbuff = (char *) malloc ( HDRSZ ) ; fseek ( ifile, 0, SEEK_SET ) ; fread ( ihdrbuff, HDRSZ, 1, ifile ) ; hdrswap ( ihdrbuff, ohdrbuff ) ; fwrite ( ohdrbuff, HDRSZ, 1, ofile ) ; /** Process the data **/ buffsz = ( 1 * sizeof(long)) + ( 5 * sizeof(short)) ; idatbuff = (char *) malloc ( buffsz ) ; odatbuff = (char *) malloc ( buffsz ) ; for ( i=0; i<(scancnt*(pix+hipix)); i++ ) { fread ( idatbuff, buffsz, 1, ifile ) ; dataswap ( idatbuff, odatbuff ) ; fwrite ( odatbuff, buffsz, 1, ofile ) ; } /** Close open files **/ if ( ifile ) fclose ( ifile ) ; if ( ofile ) fclose ( ofile ) ; free ( ihdrbuff ) ; free ( ohdrbuff ) ; free ( idatbuff ) ; free ( odatbuff ) ; return ; } short shrtswap ( short inval ) { char bytes[2] ; char tchar ; short swapval ; memcpy ( bytes, &inval, 2 ) ; tchar=bytes[0] ; bytes[0]=bytes[1] ; bytes[1]=tchar ; memcpy ( &swapval, bytes, 2 ) ; return ( swapval ) ; } long longswap ( long inval ) { char bytes[4] ; char tchar0 ; char tchar1 ; char tchar2 ; char tchar3 ; long swapval ; memcpy ( bytes, &inval, 4 ) ; tchar0=bytes[0] ; tchar1=bytes[1] ; tchar2=bytes[2] ; tchar3=bytes[3] ; bytes[0]=tchar3 ; bytes[1]=tchar2 ; bytes[2]=tchar1 ; bytes[3]=tchar0 ; memcpy ( &swapval, bytes, 4 ) ; return ( swapval ) ; } int hdrswap ( char *datain, char *dataout ) { int i, j, k, pos = 0 ; int n[13]={120,6,2,120,2,120,2,120,2,120,2,120,4228} ; int s[13]={1,2,4,1,4,1,4,1,4,1,4,1,1} ; for ( i=0; i<13; i++ ) { for ( j=0; jfilename, 1, 80, infile )) != 80) { error_flag = -2 ; goto outta_here ; } fread ( hdr->satellite, 1, 20, infile ) ; fread ( hdr->sensor, 1, 20, infile ) ; fread ( &hdr->satid, 2, 1, infile ) ; fread ( &hdr->fields, 2, 1, infile ) ; fread ( &hdr->pixperscan, 2, 1, infile ) ; fread ( &hdr->hifields, 2, 1, infile ) ; fread ( &hdr->hipixperscan, 2, 1, infile ) ; fread ( &hdr->missing_val, 2, 1, infile ) ; fields = hdr->fields + hdr->hifields ; /** Allocate space for variable-length records **/ hdr->scale = ( float * ) malloc ( ( hdr->fields + hdr->hifields ) * sizeof ( float ) ) ; hdr->offset = ( float * ) malloc ( ( hdr->fields + hdr->hifields ) * sizeof ( float ) ) ; hdr->units = ( struct units_text * ) malloc ( ( hdr->fields + hdr->hifields ) * sizeof ( struct units_text ) ) ; hdr->description = ( struct describe_text * ) malloc ( ( hdr->fields + hdr->hifields ) * sizeof ( struct describe_text ) ) ; if ( hdr->scale == NULL || hdr->offset == NULL || hdr->units == NULL || hdr->description == NULL ) { error_flag = -1 ; goto outta_here ; } for ( i = 0 ; i < ( hdr->fields + hdr->hifields ) ; i++ ) { fread ( &hdr->scale[i], 4, 1, infile ) ; fread ( &hdr->offset[i], 4, 1, infile ) ; fread ( &hdr->units[i], 40, 1, infile ) ; fread ( &hdr->description[i], 80, 1, infile ) ; } /** Read past additional bytes to get to 5000 **/ if ( ! fread ( buff, 5000 - 132 - ( fields * 128 ), 1, infile ) ) error_flag = -2 ; outta_here : return ( error_flag ) ; }