updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / var / external / bufr / cread.c
blobed2f05088e2758c51adb9759e8e62ad8f3f0c164
1 /*C$$$ SUBPROGRAM DOCUMENTATION BLOCK
3 C SUBPROGRAM: CREAD
4 C PRGMMR: WOOLLEN ORG: NP20 DATE: 2012-09-15
6 C ABSTRACT: CREAD IS A PACKAGE OF C LANGUAGE I/O ROUTINES WHICH
7 C ARE DESIGNED TO OPERATE BUFRLIB INPUT AND OUTPUT
8 C FUNCTIONS IN A LESS RESTRICTIVE WAY COMPARED TO
9 C THOSE AVAILABLE IN STANDARD FORTRAN IMPLEMENTATIONS.
10 C THE PACKAGE CONSISTS OF THREE FILE OPEN ROUTINES,
11 C ONE FILE CLOSE ROUTINE, TWO FILE POSITIONING
12 C ROUTINES, ONE READ BUFR AND ONE WRITE BUFR ROUTINE.
13 C ARRAYS OF FILE CONNECTION DESCRIPTORS AND FILE
14 C POSITION POINTERS PROVIDE THE CONNECTION TO THE
15 C BUFRLIB INTERNAL FILE STATUS INDICATORS. THE
16 C BUFRLIB FILE CONNECTION INDEX LUN, OBTAINED BY
17 C CALLS TO STATUS, IS USED TO REFERENCE THE CREAD
18 C DESCRIPTOR AND POINTER ARRAYS.
20 C PROGRAM HISTORY LOG:
21 C 2012-09-15 J. WOOLLEN -- ORIGINAL AUTHOR
23 C USAGE: CALL openrb(nfile,ufile) - open ufile for binary reading
24 C CALL openwb(nfile,ufile) - open ufile for binary writing
25 C CALL openab(nfile,ufile) - open ufile for binary appending
26 C CALL backbufr(nfile) - backspace file nfile 1 message
27 C CALL cewind(nfile) - rewind file nfile to beginning
28 C CALL closfb(nfile) - disconnect file nfile from c
29 C CALL crdbufr(nfile,bufr,maxbyt) - read next bufr message from file nfile into bufr
30 C CALL cwrbufr(nfile,bufr,nwrd) - write bufr message from bufr into file nfile
32 C INPUT ARGUMENTS:
33 c nfile - integer bufrlib file connection index
34 C ufile - full file path/filename
35 c bufr - in crdbufr: char array to read a bufr message into
36 c maxbyt - in crdbufr: maximum number of bytes allowed to read
37 c bufr - in cwrbufr: integer array to write a bufr message from
38 c nwrd - in cwrbufr: number of words to write for bufr message
40 C OUTPUT ARGUMENTS:
41 c crdbufr - return code from reading
42 c -3 - sec0 message length > maxbyt
43 c -2 - error reading bufr message
44 c -1 - no more more messages in file
45 c 0 - read a bufr message
47 C REMARKS:
48 C THIS ROUTINE CALLS: IUPBS01
50 C THIS ROUTINE IS CALLED BY:
52 C ATTRIBUTES:
53 C LANGUAGE: C
54 C MACHINE: PORTABLE TO ALL PLATFORMS
56 C$$$*/
58 #include "bufrlib.h"
60 /* The following arrays are dimensioned one larger than NFILES because of the difference in array
61 indexing between Fortran and C. In each of the following C functions, the value passed in for
62 nfile will a be Fortran index ranging from 1 to NFILES, so we need to allow for this same range
63 of values in C, which would otherwise expect the array indices to range from 0 to NFILES-1. */
64 FILE *pb[NFILES+1]; fpos_t lstpos[NFILES+1];
66 void openrb (nfile,ufile) f77int *nfile; char *ufile; { pb[*nfile] = fopen( ufile , "rb " ); }
67 void openwb (nfile,ufile) f77int *nfile; char *ufile; { pb[*nfile] = fopen( ufile , "wb " ); }
68 void openab (nfile,ufile) f77int *nfile; char *ufile; { pb[*nfile] = fopen( ufile , "a+b" ); }
69 void backbufr (nfile ) f77int *nfile; { fsetpos(pb[*nfile],&lstpos[*nfile]);}
70 void cewind (nfile ) f77int *nfile; { rewind(pb[*nfile]); }
71 void closfb (nfile ) f77int *nfile; { fclose(pb[*nfile]); }
73 f77int crdbufr (nfile,bufr,mxbyt)
74 f77int *nfile; f77int *mxbyt; char *bufr;
75 { f77int nbyt; f77int nb; f77int wkint[2]; fpos_t nxtpos;
76 fgetpos(pb[*nfile],&lstpos[*nfile]);
77 nb = sizeof(*bufr); bufr[0]=bufr[1];
78 while ( strncmp(bufr,"BUFR",4)!=0)
79 { memmove(bufr,&bufr[1],3);
80 if(fread(bufr+3,nb,1,pb[*nfile])!=1) return -1;
82 fgetpos(pb[*nfile],&nxtpos); if(fread(bufr+4,nb,4,pb[*nfile])!=4) return -1;
83 memcpy(wkint,bufr,8); nbyt=iupbs01(wkint,"LENM",4)-8;
84 if(nbyt+8>*mxbyt) {fsetpos(pb[*nfile],&nxtpos);return -3;};
85 if(fread(bufr+8,nb,nbyt,pb[*nfile])!=nbyt) {fsetpos(pb[*nfile],&nxtpos);return -2;};
86 if(strncmp(bufr+nbyt+4,"7777",4)!=0) {fsetpos(pb[*nfile],&nxtpos);return -2;};
87 return 0;
90 void cwrbufr (nfile,bufr,nwrd)
91 f77int *nfile; f77int *nwrd; f77int *bufr;
92 { f77int nb; nb = sizeof(*bufr);
93 fwrite(bufr,nb,*nwrd,pb[*nfile]);