NCEPLIBS-bufr  12.1.0
restd.c
Go to the documentation of this file.
1 
7 #include "bufrlib.h"
8 
26 void
27 wrdesc(int desc, int *descary, int *ndescary, int mxdescary)
28 {
29  char errstr[129];
30 
31 /*
32 ** Is there room in descary for desc?
33 */
34  if ( ( *ndescary + 1 ) < mxdescary ) {
35  descary[(*ndescary)++] = desc;
36  }
37  else {
38  sprintf(errstr, "BUFRLIB: WRDESC - EXPANDED SECTION 3 CONTAINS"
39  " MORE THAN %d DESCRIPTORS", mxdescary);
40  bort_f(errstr);
41  }
42 
43  return;
44 }
45 
72 void
73 restd(int lun, int tddesc, int *nctddesc, int *ctddesc)
74 {
75  int maxnc, desc, ncdesc, *cdesc;
76  int i, j, inum, itbd, ictbd;
77  int iscl, iref, ibit;
78 
79  char tab, nemo[NEMO_STR_LEN+1], adn[FXY_STR_LEN+1], cunit[UNIT_STR_LEN+1], cwork[31];
80 /*
81 ** How many child descriptors does tddesc have?
82 */
83  numtbd_f(lun, tddesc, nemo, NEMO_STR_LEN+1, &tab, &itbd);
84  uptdd_f(itbd, lun, 0, &inum);
85 
86  maxnc = igetprm_f("MAXNC");
87 
88  *nctddesc = 0;
89 /*
90 ** Examine each child descriptor one at a time.
91 */
92  for ( i = 1; i <= inum; i++ ) {
93  uptdd_f(itbd, lun, i, &desc);
94  if (! istdesc_f(desc)) {
95 /*
96 ** desc is a local descriptor.
97 */
98  numtbd_f(lun, desc, nemo, NEMO_STR_LEN+1, &tab, &ictbd);
99  if ( tab == 'D' ) {
100 /*
101 ** desc is itself a local Table D descriptor, so resolve
102 ** it now via a recursive call to this same routine.
103 */
104  if (!(cdesc = malloc(maxnc * sizeof(int)))) bort_f("RESTD FAILED ALLOCATING");
105 
106  restd(lun, desc, &ncdesc, cdesc);
107 
108  if ( ( *nctddesc > 0 ) &&
109  ( ctddesc[(*nctddesc)-1] > ifxy_f(MIN_FXY_REPL) ) &&
110  ( ctddesc[(*nctddesc)-1] <= ifxy_f("101255") ) ) {
111 /*
112 ** desc is replicated using fixed replication, so write
113 ** the number of child descriptors into the X value of
114 ** the replication descriptor ctddesc[(*nctddesc)-1]
115 */
116  cadn30_f(ctddesc[(*nctddesc)-1], adn, FXY_STR_LEN+1);
117  sprintf(cwork, "%c%02d%c%c%c",
118  adn[0], ncdesc, adn[3], adn[4], adn[5]);
119  strncpy(adn, cwork, 6); adn[6] = '\0';
120  ctddesc[(*nctddesc)-1] = ifxy_f(adn);
121  }
122  else if ( ( *nctddesc > 1 ) &&
123  ( ctddesc[(*nctddesc)-2] == ifxy_f(MIN_FXY_REPL) ) ) {
124 /*
125 ** desc is replicated using delayed replication, so write
126 ** the number of child descriptors into the X value of
127 ** the replication descriptor ctddesc[(*nctddesc)-2]
128 */
129  cadn30_f(ctddesc[(*nctddesc)-2], adn, FXY_STR_LEN+1);
130  sprintf(cwork, "%c%02d%c%c%c",
131  adn[0], ncdesc, adn[3], adn[4], adn[5]);
132  strncpy(adn, cwork, 6); adn[6] = '\0';
133  ctddesc[(*nctddesc)-2] = ifxy_f(adn);
134  }
135 /*
136 ** Add the child descriptors to the output list.
137 */
138  for ( j = 0; j < ncdesc; j++ ) {
139  wrdesc(cdesc[j], ctddesc, nctddesc, maxnc);
140  }
141 
142  free(cdesc);
143  }
144  else {
145 /*
146 ** desc is a local Table B descriptor, so precede it with
147 ** a 206YYY operator in the output list.
148 */
149  nemtbb_f(lun, ictbd, cunit, UNIT_STR_LEN+1, &iscl, &iref, &ibit);
150  sprintf(cwork, "%c%c%c%03d", '2', '0', '6', ibit);
151  strncpy(adn, cwork, 6); adn[6] = '\0';
152  wrdesc(ifxy_f(adn), ctddesc, nctddesc, maxnc);
153  wrdesc(desc, ctddesc, nctddesc, maxnc);
154  }
155  }
156  else {
157 /*
158 ** desc is a standard Table B, Table D, operator or replicator
159 ** descriptor, so append it "as is" to the output list.
160 */
161  wrdesc(desc, ctddesc, nctddesc, maxnc);
162  }
163  }
164 
165  return;
166 }
int igetprm_f(char *cprmnm)
Get the current value of a parameter.
void nemtbb_f(int lun, int table_idx, char *unit_str, int unit_str_len, int *scale, int *reference, int *bits)
Get information about a Table B descriptor.
Enable a number of NCEPLIBS-bufr subprograms to be called from within the C part of the library.
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 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.
#define MIN_FXY_REPL
Character string containing minimum FXY value for a replication descriptor.
Definition: bufrlib.h:37
#define UNIT_STR_LEN
Size of a character string needed to store the units of a Table B descriptor.
Definition: bufrlib.h:49
int ifxy_f(char *cfxy)
Convert an FXY value from its 6 character representation to its WMO bit-wise representation.
#define NEMO_STR_LEN
Size of a character string needed to store a mnemonic.
Definition: bufrlib.h:46
void uptdd_f(int id, int lun, int ient, int *iret)
Get the WMO bit-wise representation of the FXY value corresponding to a child mnemonic of a Table D s...
int istdesc_f(int idn)
Check whether a descriptor is WMO-standard.
integer ibit
Bit pointer within ibay.
void wrdesc(int desc, int *descary, int *ndescary, int mxdescary)
Maintain an array of descriptors.
Definition: restd.c:27
void restd(int lun, int tddesc, int *nctddesc, int *ctddesc)
Standardize a local Table D descriptor.
Definition: restd.c:73