NCEPLIBS-bufr  11.5.0
 All Data Structures Files Functions Variables Pages
crbmg.c
Go to the documentation of this file.
1 
5 #include "bufrlib.h"
6 #include "cobfl.h"
7 
46 void crbmg( char *bmg, f77int *mxmb, f77int *nmb, f77int *iret )
47 {
48  f77int i1 = 1, i2 = 2, i3 = 3, i4 = 4, i24 = 24;
49  f77int wkint[2];
50  f77int iben, isbyt, iwid;
51 
52  char errstr[129];
53 
54  unsigned short i, nsecs;
55  unsigned int lsec;
56 /*
57 ** Make sure that a file is open for reading.
58 */
59  if ( pbf[0] == NULL ) {
60  sprintf( errstr, "BUFRLIB: CRBMG - NO FILE IS OPEN FOR READING" );
61  bort( errstr, ( f77int ) strlen( errstr ) );
62  }
63 /*
64 ** Initialize the first 4 characters of the output array to blanks.
65 */
66  if ( *mxmb < 4 ) {
67  *iret = 1;
68  return;
69  }
70  strncpy( bmg, " ", 4);
71 /*
72 ** Look for the start of the next BUFR message.
73 */
74  while ( ichkstr( "BUFR", bmg, &i4, 4, 4 ) != 0 ) {
75  memmove( bmg, &bmg[1], 3 );
76  if ( ( *iret = rbytes( bmg, mxmb, 3, 1 ) ) != 0 ) return;
77  }
78 /*
79 ** Read the next 4 bytes and determine the BUFR edition number that was used
80 ** to encode the message.
81 */
82  if ( ( *iret = rbytes( bmg, mxmb, 4, 4 ) ) != 0 ) return;
83  memcpy( wkint, bmg, 8 );
84  iben = iupbs01( wkint, "BEN", 3 );
85 
86  if ( iben >= 2 ) {
87 /*
88 ** Get the length of the BUFR message.
89 */
90  *nmb = iupbs01( wkint, "LENM", 4 );
91 /*
92 ** Read the remainder of the BUFR message.
93 */
94  if ( ( *iret = rbytes( bmg, mxmb, 8, *nmb-8 ) ) != 0 ) return;
95  }
96  else {
97 /*
98 ** Read the remainder of the BUFR message and then convert it to BUFR
99 ** edition 2. The message length isn't encoded in Section 0, so we need
100 ** to compute it by unpacking and summing the lengths of the individual
101 ** sections.
102 */
103  lsec = 4; /* length of Section 0 */
104 /*
105 ** Get the length of Section 1 and add it to the total.
106 */
107  gets1loc( "LEN1", &iben, &isbyt, &iwid, &wkint[0], 4 );
108  *nmb = lsec + iupm( &bmg[lsec+isbyt-1], &iwid, 3 );
109 /*
110 ** Read up through the end of Section 1.
111 */
112  if ( ( *iret = rbytes( bmg, mxmb, 8, *nmb-8 ) ) != 0 ) return;
113 /*
114 ** Is there a Section 2?
115 */
116  gets1loc( "ISC2", &iben, &isbyt, &iwid, &wkint[0], 4 );
117  nsecs = iupm( &bmg[lsec+isbyt-1], &iwid, 1 ) + 2;
118 /*
119 ** Read up through the end of Section 4.
120 */
121  for ( i = 1; i <= nsecs; i++ ) {
122  if ( ( *iret = rbytes( bmg, mxmb, *nmb, 3 ) ) != 0 ) return;
123  lsec = iupm( &bmg[*nmb], &i24, 3 );
124  if ( ( *iret = rbytes( bmg, mxmb, *nmb+3, lsec-3 ) ) != 0 ) return;
125  *nmb += lsec;
126  }
127 /*
128 ** Read Section 5.
129 */
130  if ( ( *iret = rbytes( bmg, mxmb, *nmb, 4 ) ) != 0 ) return;
131  *nmb += 4;
132 /*
133 ** Expand Section 0 from 4 bytes to 8 bytes, then encode the message length
134 ** and new edition number (i.e. 2) into the new (expanded) Section 0.
135 */
136  if ( *nmb + 4 > *mxmb ) {
137  *iret = 1;
138  return;
139  }
140  memmove( &bmg[8], &bmg[4], *nmb-4 );
141  *nmb += 4;
142  ipkm( &bmg[4], &i3, nmb, 3 );
143  ipkm( &bmg[7], &i1, &i2, 1 );
144  }
145 /*
146 ** Check that the "7777" is in the expected location.
147 */
148  *iret = ( ( ichkstr( "7777", &bmg[*nmb-4], &i4, 4, 4 ) == 0 ) ? 0 : 2 );
149 
150  return;
151 }
Define signatures to enable a number of BUFRLIB subprograms to be called directly from C application ...
FILE * pbf[2]
File pointers.
function iupm(CBAY, NBITS)
THIS FUNCTION UNPACKS AND RETURNS A BINARY INTEGER WORD CONTAINED WITHIN NBITS BITS OF A CHARACTER ST...
Definition: iupm.f:40
function ichkstr(STR, CHR, N)
THIS FUNCTION COMPARES A SPECIFIED NUMBER OF CHARACTERS FROM AN INPUT CHARACTER ARRAY AGAINST THE SAM...
Definition: ichkstr.f:32
subroutine gets1loc(S1MNEM, IBEN, ISBYT, IWID, IRET)
THIS SUBROUTINE RETURNS THE LOCATION (I.E.
Definition: gets1loc.f:80
Define signatures and declare variables for reading or writing BUFR messages via a C language interfa...
f77int rbytes(char *, f77int *, f77int, f77int)
This function reads a specified number of bytes from the system file that was opened via the most rec...
Definition: rbytes.c:38
void crbmg(char *, f77int *, f77int *, f77int *)
This subroutine reads the next BUFR message from the system file that was opened via the most recent ...
Definition: crbmg.c:46
subroutine ipkm(CBAY, NBYT, N)
This subroutine encodes an integer value within a specified number of bytes of a character string...
Definition: ipkm.f:29
subroutine bort(STR)
This subroutine calls subroutine errwrt() to log an error message, then calls subroutine bort_exit() ...
Definition: bort.f:23
function iupbs01(MBAY, S01MNEM)
This function returns a specified value from within Section 0 or Section 1 of a BUFR message...
Definition: iupbs01.f:72