NCEPLIBS-bufr  12.1.0
stseq.c
Go to the documentation of this file.
1 
8 #include "bufrlib.h"
9 #include "mstabs.h"
10 
27 int
28 cmpia(const void *pf1, const void *pf2)
29 {
30  int *mypf1 = ( int * ) pf1;
31  int *mypf2 = ( int * ) pf2;
32 
33  if ( *mypf1 == *mypf2 ) return 0;
34 
35  return ( *mypf1 < *mypf2 ? -1 : 1 );
36 }
37 
53 void
54 nummtb(int *idn, char *tab, int *ipt)
55 {
56  int *pifxyn, *pbs, nmt;
57 
58  char adn[FXY_STR_LEN+1], errstr[129];
59 
60  if ( *idn >= ifxy_f(MIN_FXY_TABLED) ) {
61  *tab = 'D';
62  pifxyn = &idfxyn_c[0];
63  nmt = nmtd_c;
64  }
65  else {
66  *tab = 'B';
67  pifxyn = &ibfxyn_c[0];
68  nmt = nmtb_c;
69  }
70 
71  pbs = bsearch(idn, pifxyn, (size_t) nmt, sizeof(int),
72  (int (*) (const void *, const void *)) cmpia);
73  if ( pbs == NULL ) {
74  cadn30_f(*idn, adn, FXY_STR_LEN+1);
75  sprintf(errstr, "BUFRLIB: NUMMTB - COULD NOT FIND DESCRIPTOR "
76  "%s IN MASTER TABLE %c", adn, *tab);
77  bort_f(errstr);
78  }
79  *ipt = pbs - pifxyn;
80 
81  return;
82 }
83 
106 void
107 stseq(int lun, int *irepct, int idn, char *nemo,
108  char *cseq, int *cdesc, int ncdesc)
109 {
110  int i, j, nb, nd, ix, iy, iret, nbits;
111  int rpidn, pkint, ilen, imxcd, imxnf, ipt, *rpdesc;
112 
113  char tab, adn[FXY_STR_LEN+1], adn2[FXY_STR_LEN+1], units[10], errstr[129];
114  char nemo2[NEMO_STR_LEN+1], rpseq[56], card[80], ctmp[4], cblk = ' ', czero = '0';
115 
116 /*
117 ** The following variable is declared as static so that it automatically initializes
118 ** to zero and remains unchanged between recursive calls to this subroutine.
119 */
120  static int naf;
121 
122 /*
123 ** Is idn already listed as an entry in the internal Table D?
124 ** If so, then there's no need to proceed any further.
125 */
126  numtbd_f(lun, idn, nemo2, NEMO_STR_LEN+1, &tab, &iret);
127  if ( ( iret > 0 ) && ( tab == 'D' ) ) return;
128 
129 /*
130 ** Start a new Table D entry for idn.
131 */
132  tab = 'D';
133  nd = igetntbi_f(lun, &tab);
134  cadn30_f(idn, adn, FXY_STR_LEN+1);
135  stntbi_f(nd, lun, adn, nemo, cseq);
136 
137 /*
138 ** Now, go through the list of child descriptors corresponding to idn.
139 */
140  imxcd = igetprm_f("MAXCD");
141  imxnf = igetprm_f("MXNAF");
142 
143  for ( i = 0; i < ncdesc; i++ ) {
144  cadn30_f(cdesc[i], adn, FXY_STR_LEN+1); adn[6] = '\0';
145  strncpy(ctmp, &adn[1], 2); ctmp[2] = '\0';
146  strnum_f(ctmp, &ix, &iret);
147  strncpy(ctmp, &adn[3], 4); /* trailing null will be included in this copy */
148  strnum_f(ctmp, &iy, &iret);
149  if ( adn[0] == '3' ) {
150 /*
151 ** cdesc[i] is itself a Table D descriptor, so locate it within the
152 ** master table D and then store the contents within the internal
153 ** Table D via a recursive call to this same routine.
154 */
155  nummtb(&cdesc[i], &tab, &ipt);
156  if ( naf > 0 ) {
157 /*
158 ** There are associated fields in effect which will modify this
159 ** descriptor when storing it within the internal Table D. So
160 ** create a new sequence to store the contents of this descriptor
161 ** along with its associated fields.
162 */
163  rpidn = igettdi_f(lun);
164 
165  sprintf(rpseq, "REPLICATION SEQUENCE %.3d", ++(*irepct));
166  memset(&rpseq[24], (int) cblk, 31);
167  sprintf(nemo2, "RPSEQ%.3d", *irepct);
168 
169  stseq(lun, irepct, rpidn, nemo2, rpseq,
170  &idefxy_c[icvidx(ipt,0,imxcd)],
171  ndelem_c[ipt]);
172  pkint = rpidn;
173 
174  }
175  else {
176 /*
177 ** Store cdesc[i] as is directly within the internal Table D.
178 */
179  stseq(lun, irepct, cdesc[i], &cdmnem_c[ipt][0],
180  &cdseq_c[ipt][0],
181  &idefxy_c[icvidx(ipt,0,imxcd)],
182  ndelem_c[ipt]);
183  pkint = cdesc[i];
184  }
185  }
186  else if ( adn[0] == '2' ) {
187 /*
188 ** cdesc[i] is an operator descriptor.
189 */
190  if ( ( ( ix >= 4 ) && ( ix <= 6 ) ) || ( imrkopr_f(adn) ) ) {
191 /*
192 ** This is a 204YYY, 205YYY, 206YYY operator, or else a 223255,
193 ** 224255, 225255 or 232255 marker operator. In any case,
194 ** generate a Table B mnemonic to hold the corresponding data.
195 */
196  strncpy(nemo2, adn, 6);
197  memset(&nemo2[6], (int) cblk, 2);
198  nemo2[8] = '\0';
199 
200  if ( ( ix == 4 ) && ( iy == 0 ) ) {
201 /*
202 ** Cancel the most-recently added associated field.
203 */
204  if ( naf-- <= 0 ) {
205  sprintf(errstr, "BUFRLIB: STSEQ - TOO MANY ASSOCIATED"
206  " FIELD CANCELLATION OPERATORS");
207  bort_f(errstr);
208  }
209  }
210  else {
211 /*
212 ** Is nemo2 already listed as an entry within the internal
213 ** Table B?
214 */
215  nemtab_f(lun, nemo2, &pkint, &tab, &iret);
216  if ( ( iret == 0 ) || ( tab != 'B' ) ) {
217 /*
218 ** No, so create and store a new Table B entry for nemo2.
219 */
220  tab = 'B';
221  nb = igetntbi_f(lun, &tab);
222 
223  if ( ix == 4 ) {
224  sprintf(rpseq, "Associated field of %3d bits", iy);
225  nbits = iy;
226  strcpy(units, "NUMERIC");
227  }
228  else if ( ix == 5 ) {
229  sprintf(rpseq, "Text string of %3d bytes", iy);
230  nbits = iy*8;
231  strcpy(units, "CCITT IA5");
232  }
233  else if ( ix == 6 ) {
234  sprintf(rpseq, "Local descriptor of %3d bits", iy);
235  nbits = iy;
236  if ( nbits > 32 ) {
237  strcpy(units, "CCITT IA5");
238  }
239  else {
240  strcpy(units, "NUMERIC");
241  }
242  }
243  else { // 2-XX-255 marker operator
244  if ( ix == 23 ) {
245  sprintf(rpseq, "Substituted value");
246  }
247  else if ( ix == 24 ) {
248  sprintf(rpseq, "First-order statistical value");
249  }
250  else if ( ix == 25 ) {
251  sprintf(rpseq, "Difference statistical value");
252  }
253  else if ( ix == 32 ) {
254  sprintf(rpseq, "Replaced/retained value");
255  }
256  /* For now, set a default bit width and units. */
257  nbits = 8;
258  strcpy(units, "NUMERIC");
259  }
260  ilen = ( int ) strlen(rpseq);
261  memset(&rpseq[ilen], (int) cblk, 55 - ilen);
262 /*
263 ** Note that 49152 = 3*(2**14), so subtracting 49152 in the
264 ** following statement changes a WMO Table D bit-wise FXY value into
265 ** a WMO Table B bit-wise FXY value.
266 */
267  pkint = (igettdi_f(lun) - 49152);
268  cadn30_f(pkint, adn2, FXY_STR_LEN+1);
269 
270  stntbi_f(nb, lun, adn2, nemo2, rpseq);
271 
272  /* Initialize card to all blanks. */
273  memset(card, (int) cblk, sizeof( card ));
274 
275  strncpy(&card[2], nemo2, 8);
276  memcpy(&card[16], &czero, 1);
277  memcpy(&card[30], &czero, 1);
278  sprintf(&card[33], "%4d", nbits);
279  strcpy(&card[40], units);
280  card[40+strlen(units)] = cblk; /* overwrite trailing null */
281  elemdx_f(card, lun);
282  }
283  if ( ix == 4 ) {
284 /*
285 ** Add an associated field.
286 */
287  if ( naf >= imxnf ) {
288  sprintf(errstr, "BUFRLIB: STSEQ - TOO MANY ASSOCIATED"
289  " FIELDS ARE IN EFFECT AT THE SAME TIME");
290  bort_f(errstr);
291  }
292  iafpk[naf++] = pkint;
293  }
294  }
295  if ( ix == 6 ) {
296 /*
297 ** Skip over the local descriptor placeholder.
298 */
299  if ( ++i >= ncdesc ) {
300  sprintf(errstr, "BUFRLIB: STSEQ - COULD NOT FIND LOCAL"
301  " DESCRIPTOR PLACEHOLDER FOR %s", adn);
302  bort_f(errstr);
303  }
304  }
305  }
306  else {
307  pkint = cdesc[i];
308  }
309  }
310  else if ( adn[0] == '1' ) {
311 /*
312 ** cdesc[i] is a replication descriptor, so create a sequence
313 ** consisting of the set of replicated descriptors and then immediately
314 ** store that sequence within the internal Table D via a recursive call
315 ** to this same routine.
316 **
317 ** See module @ref modv_vars for the source of the FXY values referenced
318 ** in the following block. Note we are guaranteed that 0 <= iy <= 255,
319 ** since adn was generated using subroutine cadn30_f().
320 */
321  if ( iy == 0 ) { /* delayed replication */
322  if ( ( i+1 ) >= ncdesc ) {
323  sprintf(errstr, "BUFRLIB: STSEQ - COULD NOT FIND DELAYED "
324  "DESCRIPTOR REPLICATION FACTOR FOR %s", adn);
325  bort_f(errstr);
326  }
327  else if ( cdesc[i+1] == ifxy_f("031002") ) {
328  pkint = ifxy_f("360001");
329  }
330  else if ( cdesc[i+1] == ifxy_f("031001") ) {
331  pkint = ifxy_f("360002");
332  }
333  else if ( cdesc[i+1] == ifxy_f("031000") ) {
334  pkint = ifxy_f("360004");
335  }
336  else {
337  sprintf(errstr, "BUFRLIB: STSEQ - UNKNOWN DELAYED "
338  "DESCRIPTOR REPLICATION FACTOR FOR %s", adn);
339  bort_f(errstr);
340  }
341  i += 2;
342  }
343  else { /* regular replication */
344  pkint = ifxy_f(MIN_FXY_REPL) + iy;
345  i++;
346  }
347 /*
348 ** Store this replication descriptor within the table D entry for
349 ** this parent.
350 */
351  pktdd_f(nd, lun, pkint, &iret);
352  if ( iret < 0 ) {
353  strncpy(nemo2, nemo, 8);
354  nemo2[8] = '\0';
355  sprintf(errstr, "BUFRLIB: STSEQ - BAD RETURN FROM PKTDD WHEN "
356  "STORING REPLICATOR FOR PARENT MNEMONIC %s", nemo2);
357  bort_f(errstr);
358  }
359 /*
360 ** Note we are guaranteed that 0 < ix <= 63 since adn was generated
361 ** using subroutine cadn30_f().
362 */
363  if ( ix > ( ncdesc - i ) ) {
364  sprintf(errstr, "BUFRLIB: STSEQ - NOT ENOUGH REMAINING CHILD "
365  "DESCRIPTORS TO COMPLETE REPLICATION FOR %s", adn);
366  bort_f(errstr);
367  }
368  else if ( ( ix == 1 ) && ( cdesc[i] >= ifxy_f(MIN_FXY_TABLED) ) ) {
369 /*
370 ** The only thing being replicated is a single Table D descriptor,
371 ** so there's no need to invent a new sequence for this replication
372 ** (this is a special case!)
373 */
374  nummtb(&cdesc[i], &tab, &ipt);
375  stseq(lun, irepct, cdesc[i], &cdmnem_c[ipt][0],
376  &cdseq_c[ipt][0],
377  &idefxy_c[icvidx(ipt,0,imxcd)],
378  ndelem_c[ipt]);
379  pkint = cdesc[i];
380  }
381  else {
382 /*
383 ** Store the ix descriptors to be replicated in a local list, then
384 ** get an FXY value to use with this list and generate a unique
385 ** mnemonic and description as well.
386 */
387 
388  if ( ( rpdesc = malloc( imxcd * sizeof(int) ) ) == NULL ) {
389  sprintf(errstr, "BUFRLIB: STSEQ - UNABLE TO ALLOCATE SPACE"
390  " FOR RPDESC");
391  bort_f(errstr);
392  }
393 
394  for ( j = 0; j < ix; j++ ) {
395  rpdesc[j] = cdesc[i+j];
396  }
397 
398  rpidn = igettdi_f(lun);
399 
400  sprintf(rpseq, "REPLICATION SEQUENCE %.3d", ++(*irepct));
401  memset(&rpseq[24], (int) cblk, 31);
402  sprintf(nemo2, "RPSEQ%.3d", *irepct);
403 
404  stseq(lun, irepct, rpidn, nemo2, rpseq, rpdesc, ix);
405 
406  free(rpdesc);
407 
408  pkint = rpidn;
409  i += ix - 1;
410  }
411  }
412  else {
413 /*
414 ** cdesc[i] is a Table B descriptor.
415 **
416 ** Is cdesc[i] already listed as an entry in the internal Table B?
417 */
418  numtbd_f(lun, cdesc[i], nemo2, NEMO_STR_LEN+1, &tab, &iret);
419  if ( ( iret == 0 ) || ( tab != 'B' ) ) {
420 /*
421 ** No, so search for it within the master table B.
422 */
423  nummtb(&cdesc[i], &tab, &ipt);
424 /*
425 ** Start a new Table B entry for cdesc[i].
426 */
427  nb = igetntbi_f(lun, &tab);
428  cadn30_f(cdesc[i], adn2, FXY_STR_LEN+1);
429  stntbi_f(nb, lun, adn2, &cbmnem_c[ipt][0], &cbelem_c[ipt][0]);
430 
431  /* Initialize card to all blanks. */
432  memset(card, (int) cblk, sizeof( card ));
433 
434  strncpy(&card[2], &cbmnem_c[ipt][0], 8);
435  strncpy(&card[13], &cbscl_c[ipt][0], 4);
436  strncpy(&card[19], &cbsref_c[ipt][0], 12);
437  strncpy(&card[33], &cbbw_c[ipt][0], 4);
438  strncpy(&card[40], &cbunit_c[ipt][0], 24);
439  elemdx_f(card, lun);
440  }
441  pkint = cdesc[i];
442  }
443  if (strncmp( adn, "204", 3) != 0 ) {
444 /*
445 ** Store this child descriptor within the table D entry for this
446 ** parent, preceding it with any associated fields that are currently
447 ** in effect.
448 **
449 ** Note that associated fields are only applied to Table B descriptors,
450 ** except for those in Class 31.
451 */
452  if ( ( naf > 0 ) && ( pkint <= ifxy_f(MAX_FXY_TABLEB) ) &&
453  ( ( pkint < ifxy_f("031000") ) ||
454  ( pkint > ifxy_f("031255") ) ) ) {
455  for ( j = 0; j < naf; j++ ) {
456  pktdd_f(nd, lun, iafpk[j], &iret);
457  if ( iret < 0 ) {
458  sprintf(errstr, "BUFRLIB: STSEQ - BAD RETURN FROM PKTDD "
459  "WHEN STORING ASSOCIATED FIELDS");
460  bort_f(errstr);
461  }
462  }
463  }
464 /*
465 ** Store the child descriptor.
466 */
467  pktdd_f(nd, lun, pkint, &iret);
468  if ( iret < 0 ) {
469  strncpy(nemo2, nemo, 8);
470  nemo2[8] = '\0';
471  sprintf(errstr, "BUFRLIB: STSEQ - BAD RETURN FROM PKTDD WHEN "
472  "STORING CHILD FOR PARENT MNEMONIC %s", nemo2);
473  bort_f(errstr);
474  }
475  }
476  }
477 }
int igetprm_f(char *cprmnm)
Get the current value of a parameter.
void nemtab_f(int lun, const char *mnemonic, int *descriptor, char *table_type, int *table_idx)
Get information about a descriptor.
Enable a number of NCEPLIBS-bufr subprograms to be called from within the C part of the library.
int igettdi_f(int iflag)
Get the next usable Table D index for the current master table, or reset the index.
void cadn30_f(int idn, char *adn, int adn_str_len)
Convert an FXY value from its WMO bit-wise representation to its six-character representation.
#define FXY_STR_LEN
Size of a character string needed to store an FXY value.
Definition: bufrlib.h:34
void elemdx_f(char *card, int lun)
Decode the scale factor, reference value, bit width, and units from a Table B mnemonic definition.
int imrkopr_f(char *nemo)
Check whether a specified mnemonic is a Table C marker operator.
void numtbd_f(int lun, int idn, char *nemo, int nemo_str_len, char *tab, int *iret)
Search for a Table B or Table D descriptor within the internal DX BUFR tables.
void bort_f(char *errstr)
Log one error message and abort application program.
void stntbi_f(int n, int lun, char *numb, char *nemo, char *celsq)
Store a new entry within the internal BUFR Table B or D.
#define MIN_FXY_REPL
Character string containing minimum FXY value for a replication descriptor.
Definition: bufrlib.h:37
#define MIN_FXY_TABLED
Character string containing minimum FXY value for a Table D descriptor.
Definition: bufrlib.h:40
int ifxy_f(char *cfxy)
Convert an FXY value from its 6 character representation to its WMO bit-wise representation.
void strnum_f(char *str, int *num, int *iret)
Decode an integer from a character string.
#define NEMO_STR_LEN
Size of a character string needed to store a mnemonic.
Definition: bufrlib.h:46
#define MAX_FXY_TABLEB
Character string containing maximum FXY value for a Table B descriptor.
Definition: bufrlib.h:43
int icvidx(int ii, int jj, int numjj)
Computes a unique 1-dimensional array index from 2-dimensional indices.
Definition: icvidx.c:22
void pktdd_f(int id, int lun, int idn, int *iret)
Store information about a child mnemonic within the internal arrays.
int igetntbi_f(int lun, char *table_type)
Get the next index for storing an entry within an internal DX BUFR table.
Declare variables for internal storage of master Table B and Table D entries.
int * idefxy_c
WMO bit-wise representations of child descriptors for master Table D sequences; copied from Fortran i...
char(* cbsref_c)[12]
Master Table B reference values; copied from Fortran cbsref array.
char(* cbelem_c)[120]
Master Table B element names; copied from Fortran cbelem array.
int * idfxyn_c
WMO bit-wise representations of master Table D FXY numbers; copied from Fortran idfxyn array.
char(* cdmnem_c)[8]
Master Table D mnemonics; copied from Fortran cdmnem array.
char(* cdseq_c)[120]
Master Table D sequence names; copied from Fortran cdseq array.
char(* cbscl_c)[4]
Master Table B scale factors; copied from Fortran cbscl array.
char(* cbunit_c)[24]
Master Table B units; copied from Fortran cbunit array.
char(* cbmnem_c)[8]
Master Table B mnemonics; copied from Fortran cbmnem array.
char(* cbbw_c)[4]
Master Table B bit widths; copied from Fortran cbbw array.
int nmtd_c
Number of master Table D entries; copied from Fortran nmtd variable.
int * iafpk
WMO bit-wise representations of associated fields within master Table D sequences.
int * ibfxyn_c
WMO bit-wise representations of master Table B FXY numbers; copied from Fortran ibfxyn array.
int nmtb_c
Number of master Table B entries; copied from Fortran nmtb variable.
int * ndelem_c
Number of child descriptors for master Table D sequences; copied from Fortran ndelem array.
void nummtb(int *idn, char *tab, int *ipt)
Search for an entry in the BUFR master table.
Definition: stseq.c:54
void stseq(int lun, int *irepct, int idn, char *nemo, char *cseq, int *cdesc, int ncdesc)
Store information about a standard Table D descriptor within internal DX BUFR tables.
Definition: stseq.c:107
int cmpia(const void *pf1, const void *pf2)
Define a comparison between two integers.
Definition: stseq.c:28