/****************************************************************************** * * read_hdr * * Fills the passed std_hdr header structure with header data * read from the specified file. * * LAST UPDATE: 26 November 1997 * * ARGUMENTS: * * Name Type Description * ---- ---- ----------- * infile *FILE Opened input data file pointer * hdr *struct std_hdr Pointer to std_hdr structure * * ORIGINAL AUTHORS: * Don Anderson and Wesley Berg, CIRES/CDC, November 1997 * * RETURNS: 0 If successful * -1 If error allocating space * -2 If error returned from fread * * NOTES: * (1) The passed file descriptor ("infile") must be for a file which * has already been opened * (2) Binary read (fread) stops at the 1000th byte of the file * (end of header) * (3) Example of use: * To print the 0th field description from the calling routine, * printf ("Description of field 0 is %s\n", hdrstruct.description[0]); * ***************************************************************************/ #include #include "common_structures" int read_hdr ( FILE *infile, struct std_hdr *hdr ) { char buff[5000] ; /* Buffer for filler bytes */ int error_flag ; /* Error flag */ short fields ; /* Number of fields */ int i,n ; /* Generic counter */ error_flag = 0 ; if ((n=fread ( hdr->filename, 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 ) ; }