bufr2synop 0.24.0
Functions
bufr2tac_io.c File Reference

file with the i/o code for binary bufr2tac More...

#include "bufr2tac.h"
Include dependency graph for bufr2tac_io.c:

Go to the source code of this file.

Functions

int read_bufr (unsigned char *bufr, char *filename, int *length)
 read a bufr file as an array of unsigned chars More...
 

Detailed Description

file with the i/o code for binary bufr2tac

Definition in file bufr2tac_io.c.

Function Documentation

◆ read_bufr()

int read_bufr ( unsigned char *  bufr,
char *  filename,
int *  length 
)

read a bufr file as an array of unsigned chars

Parameters
bufrpointer to an array of unsigned chars. On output it will contain the bufr
filenamestring with complete pathname of bufr file to read
lengthOn input max length allocated by caller. On output real length of bufr

Definition at line 34 of file bufr2tac_io.c.

35{
36 int aux;
37 size_t n = 0;
38 FILE *fp;
39
40 /* Open input file */
41 if ( ( fp = fopen ( filename, "r" ) ) == NULL )
42 {
43 fprintf ( stderr, "cannot open file '%s'\n", filename );
44 exit ( EXIT_FAILURE );
45 }
46
47 while ( ( aux = fgetc ( fp ) ) != EOF && ( int ) n < *length )
48 bufr[n++] = aux;
49 *length = n;
50
51 // close the file
52 fclose ( fp );
53
54 if ( n < 8 ) // we need at least 8 bytes
55 return -4;
56
57 // check if begins with BUFR
58 if ( bufr[0] != 'B' || bufr[1] != 'U' || bufr[2] != 'F' || bufr[3] != 'R' )
59 return -1;
60
61 // check if end with '7777'
62 if ( bufr[n - 4] != '7' || bufr[n - 3] != '7' || bufr[n - 2] != '7'
63 || bufr[n - 1] != '7' )
64 return -2;
65
66 //printf("%d %d\n", n, three_bytes_to_uint(bufr + 4));
67 // check about the expected size
68 if ( n != three_bytes_to_uint ( bufr + 4 ) )
69 return -3;
70
71 return 0;
72}
unsigned int three_bytes_to_uint(const unsigned char *source)
returns the integer value from an array of three bytes, most significant first

References three_bytes_to_uint().

Here is the call graph for this function: