NCEPLIBS-g2c  1.6.4
reduce.c
Go to the documentation of this file.
1 
11 /*#include "f2c.h"*/
12 #include <stdlib.h>
13 #include "grib2.h"
14 typedef g2int integer;
15 typedef g2float real;
90 int
91 reduce(integer *kfildo, integer *jmin, integer *jmax,
92  integer *lbit, integer *nov, integer *lx, integer *ndg, integer *ibit,
93  integer *jbit, integer *kbit, integer *novref, integer *ibxx2,
94  integer *ier)
95 {
96  /* Initialized data */
97 
98  static integer ifeed = 12;
99 
100  /* System generated locals */
101  integer i__1, i__2;
102 
103  /* Local variables */
104  static integer newboxtp, j, l, m, jj, lxn, left;
105  static real pimp;
106  static integer move, novl;
107  static char cfeed[1];
108  static integer nboxj[31], lxnkp, iorigb, ibxx2m1, movmin,
109  ntotbt[31], ntotpr, newboxt;
110  integer *newbox, *newboxp;
111 
112 /* NEWBOX( ) AND NEWBOXP( ) were AUTOMATIC ARRAYS. */
113  newbox = (integer *)calloc(*ndg,sizeof(integer));
114  newboxp = (integer *)calloc(*ndg,sizeof(integer));
115 
116  /* Parameter adjustments */
117  --nov;
118  --lbit;
119  --jmax;
120  --jmin;
121 
122  /* Function Body */
123 
124  *ier = 0;
125  if (*lx == 1) {
126  goto L410;
127  }
128 /* IF THERE IS ONLY ONE GROUP, RETURN. */
129 
130  *(unsigned char *)cfeed = (char) ifeed;
131 
132 /* INITIALIZE NUMBER OF NEW BOXES PER GROUP TO ZERO. */
133 
134  i__1 = *lx;
135  for (l = 1; l <= i__1; ++l) {
136  newbox[l - 1] = 0;
137 /* L110: */
138  }
139 
140 /* INITIALIZE NUMBER OF TOTAL NEW BOXES PER J TO ZERO. */
141 
142  for (j = 1; j <= 31; ++j) {
143  ntotbt[j - 1] = 999999999;
144  nboxj[j - 1] = 0;
145 /* L112: */
146  }
147 
148  iorigb = (*ibit + *jbit + *kbit) * *lx;
149 /* IBIT = BITS TO PACK THE JMIN( ). */
150 /* JBIT = BITS TO PACK THE LBIT( ). */
151 /* KBIT = BITS TO PACK THE NOV( ). */
152 /* LX = NUMBER OF GROUPS. */
153  ntotbt[*kbit - 1] = iorigb;
154 /* THIS IS THE VALUE OF TOTAL BITS FOR THE ORIGINAL LX */
155 /* GROUPS, WHICH REQUIRES KBITS TO PACK THE GROUP */
156 /* LENGHTS. SETTING THIS HERE MAKES ONE LESS LOOPS */
157 /* NECESSARY BELOW. */
158 
159 /* COMPUTE BITS NOW USED FOR THE PARAMETERS DEFINED. */
160 
161 /* DETERMINE OTHER POSSIBILITES BY INCREASING LX AND DECREASING */
162 /* NOV( ) WITH VALUES GREATER THAN THRESHOLDS. ASSUME A GROUP IS */
163 /* SPLIT INTO 2 OR MORE GROUPS SO THAT KBIT IS REDUCED WITHOUT */
164 /* CHANGING IBIT OR JBIT. */
165 
166  jj = 0;
167 
168 /* Computing MIN */
169  i__1 = 30, i__2 = *kbit - 1;
170  /*for (j = min(i__1,i__2); j >= 2; --j) {*/
171  for (j = (i__1 < i__2) ? i__1 : i__2; j >= 2; --j) {
172 /* VALUES GE KBIT WILL NOT REQUIRE SPLITS. ONCE THE TOTAL */
173 /* BITS START INCREASING WITH DECREASING J, STOP. ALSO, THE */
174 /* NUMBER OF BITS REQUIRED IS KNOWN FOR KBITS = NTOTBT(KBIT). */
175 
176  newboxt = 0;
177 
178  i__1 = *lx;
179  for (l = 1; l <= i__1; ++l) {
180 
181  if (nov[l] < ibxx2[j]) {
182  newbox[l - 1] = 0;
183 /* NO SPLITS OR NEW BOXES. */
184  goto L190;
185  } else {
186  novl = nov[l];
187 
188  m = (nov[l] - 1) / (ibxx2[j] - 1) + 1;
189 /* M IS FOUND BY SOLVING THE EQUATION BELOW FOR M: */
190 /* (NOV(L)+M-1)/M LT IBXX2(J) */
191 /* M GT (NOV(L)-1)/(IBXX2(J)-1) */
192 /* SET M = (NOV(L)-1)/(IBXX2(J)-1)+1 */
193  L130:
194  novl = (nov[l] + m - 1) / m;
195 /* THE +M-1 IS NECESSARY. FOR INSTANCE, 15 WILL FIT */
196 /* INTO A BOX 4 BITS WIDE, BUT WON'T DIVIDE INTO */
197 /* TWO BOXES 3 BITS WIDE EACH. */
198 
199  if (novl < ibxx2[j]) {
200  goto L185;
201  } else {
202  ++m;
203 /* *** WRITE(KFILDO,135)L,NOV(L),NOVL,M,J,IBXX2(J) */
204 /* *** 135 FORMAT(/' AT 135--L,NOV(L),NOVL,M,J,IBXX2(J)',6I10) */
205  goto L130;
206  }
207 
208 /* THE ABOVE DO LOOP WILL NEVER COMPLETE. */
209  }
210 
211  L185:
212  newbox[l - 1] = m - 1;
213  newboxt = newboxt + m - 1;
214  L190:
215  ;
216  }
217 
218  nboxj[j - 1] = newboxt;
219  ntotpr = ntotbt[j];
220  ntotbt[j - 1] = (*ibit + *jbit) * (*lx + newboxt) + j * (*lx +
221  newboxt);
222 
223  if (ntotbt[j - 1] >= ntotpr) {
224  jj = j + 1;
225 /* THE PLUS IS USED BECAUSE J DECREASES PER ITERATION. */
226  goto L250;
227  } else {
228 
229 /* SAVE THE TOTAL NEW BOXES AND NEWBOX( ) IN CASE THIS */
230 /* IS THE J TO USE. */
231 
232  newboxtp = newboxt;
233 
234  i__1 = *lx;
235  for (l = 1; l <= i__1; ++l) {
236  newboxp[l - 1] = newbox[l - 1];
237 /* L195: */
238  }
239 
240 /* WRITE(KFILDO,197)NEWBOXT,IBXX2(J) */
241 /* 197 FORMAT(/' *****************************************' */
242 /* 1 /' THE NUMBER OF NEWBOXES PER GROUP OF THE TOTAL', */
243 /* 2 I10,' FOR GROUP MAXSIZE PLUS 1 ='I10 */
244 /* 3 /' *****************************************') */
245 /* WRITE(KFILDO,198) (NEWBOX(L),L=1,LX) */
246 /* 198 FORMAT(/' '20I6/(' '20I6)) */
247  }
248 
249 /* 205 WRITE(KFILDO,209)KBIT,IORIGB */
250 /* 209 FORMAT(/' ORIGINAL BITS WITH KBIT OF',I5,' =',I10) */
251 /* WRITE(KFILDO,210)(N,N=2,10),(IBXX2(N),N=2,10), */
252 /* 1 (NTOTBT(N),N=2,10),(NBOXJ(N),N=2,10), */
253 /* 2 (N,N=11,20),(IBXX2(N),N=11,20), */
254 /* 3 (NTOTBT(N),N=11,20),(NBOXJ(N),N=11,20), */
255 /* 4 (N,N=21,30),(IBXX2(N),N=11,20), */
256 /* 5 (NTOTBT(N),N=21,30),(NBOXJ(N),N=21,30) */
257 /* 210 FORMAT(/' THE TOTAL BYTES FOR MAXIMUM GROUP LENGTHS BY ROW'// */
258 /* 1 ' J = THE NUMBER OF BITS PER GROUP LENGTH'/ */
259 /* 2 ' IBXX2(J) = THE MAXIMUM GROUP LENGTH PLUS 1 FOR THIS J'/ */
260 /* 3 ' NTOTBT(J) = THE TOTAL BITS FOR THIS J'/ */
261 /* 4 ' NBOXJ(J) = THE NEW GROUPS FOR THIS J'/ */
262 /* 5 4(/10X,9I10)/4(/10I10)/4(/10I10)) */
263 
264 /* L200: */
265  }
266 
267 L250:
268  pimp = (iorigb - ntotbt[jj - 1]) / (real) iorigb * 100.f;
269 /* WRITE(KFILDO,252)PIMP,KBIT,JJ */
270 /* 252 FORMAT(/' PERCENT IMPROVEMENT =',F6.1, */
271 /* 1 ' BY DECREASING GROUP LENGTHS FROM',I4,' TO',I4,' BITS') */
272  if (pimp >= 2.f) {
273 
274 /* WRITE(KFILDO,255)CFEED,NEWBOXTP,IBXX2(JJ) */
275 /* 255 FORMAT(A1,/' *****************************************' */
276 /* 1 /' THE NUMBER OF NEWBOXES PER GROUP OF THE TOTAL', */
277 /* 2 I10,' FOR GROUP MAXSIZE PLUS 1 ='I10 */
278 /* 2 /' *****************************************') */
279 /* WRITE(KFILDO,256) (NEWBOXP(L),L=1,LX) */
280 /* 256 FORMAT(/' '20I6) */
281 
282 /* ADJUST GROUP LENGTHS FOR MAXIMUM LENGTH OF JJ BITS. */
283 /* THE MIN PER GROUP AND THE NUMBER OF BITS REQUIRED */
284 /* PER GROUP ARE NOT CHANGED. THIS MAY MEAN THAT A */
285 /* GROUP HAS A MIN (OR REFERENCE) THAT IS NOT ZERO. */
286 /* THIS SHOULD NOT MATTER TO THE UNPACKER. */
287 
288  lxnkp = *lx + newboxtp;
289 /* LXNKP = THE NEW NUMBER OF BOXES */
290 
291  if (lxnkp > *ndg) {
292 /* DIMENSIONS NOT LARGE ENOUGH. PROBABLY AN ERROR */
293 /* OF SOME SORT. ABORT. */
294 /* WRITE(KFILDO,257)NDG,LXNPK */
295 /* 1 2 3 4 5 6 7 X */
296 /* 257 FORMAT(/' DIMENSIONS OF JMIN, ETC. IN REDUCE =',I8, */
297 /* 1 ' NOT LARGE ENOUGH FOR THE EXPANDED NUMBER OF', */
298 /* 2 ' GROUPS =',I8,'. ABORT REDUCE.') */
299  *ier = 715;
300  goto L410;
301 /* AN ABORT CAUSES THE CALLING PROGRAM TO REEXECUTE */
302 /* WITHOUT CALLING REDUCE. */
303  }
304 
305  lxn = lxnkp;
306 /* LXN IS THE NUMBER OF THE BOX IN THE NEW SERIES BEING */
307 /* FILLED. IT DECREASES PER ITERATION. */
308  ibxx2m1 = ibxx2[jj] - 1;
309 /* IBXX2M1 IS THE MAXIMUM NUMBER OF VALUES PER GROUP. */
310 
311  for (l = *lx; l >= 1; --l) {
312 
313 /* THE VALUES IS NOV( ) REPRESENT THOSE VALUES + NOVREF. */
314 /* WHEN VALUES ARE MOVED TO ANOTHER BOX, EACH VALUE */
315 /* MOVED TO A NEW BOX REPRESENTS THAT VALUE + NOVREF. */
316 /* THIS HAS TO BE CONSIDERED IN MOVING VALUES. */
317 
318  if (newboxp[l - 1] * (ibxx2m1 + *novref) + *novref > nov[l] + *
319  novref) {
320 /* IF THE ABOVE TEST IS MET, THEN MOVING IBXX2M1 VALUES */
321 /* FOR ALL NEW BOXES WILL LEAVE A NEGATIVE NUMBER FOR */
322 /* THE LAST BOX. NOT A TOLERABLE SITUATION. */
323  movmin = (nov[l] - newboxp[l - 1] * *novref) / newboxp[l - 1];
324  left = nov[l];
325 /* LEFT = THE NUMBER OF VALUES TO MOVE FROM THE ORIGINAL */
326 /* BOX TO EACH NEW BOX EXCEPT THE LAST. LEFT IS THE */
327 /* NUMBER LEFT TO MOVE. */
328  } else {
329  movmin = ibxx2m1;
330 /* MOVMIN VALUES CAN BE MOVED FOR EACH NEW BOX. */
331  left = nov[l];
332 /* LEFT IS THE NUMBER OF VALUES LEFT TO MOVE. */
333  }
334 
335  if (newboxp[l - 1] > 0) {
336  if ((movmin + *novref) * newboxp[l - 1] + *novref <= nov[l] +
337  *novref && (movmin + *novref) * (newboxp[l - 1] + 1)
338  >= nov[l] + *novref) {
339  goto L288;
340  } else {
341 /* ***D WRITE(KFILDO,287)L,MOVMIN,NOVREF,NEWBOXP(L),NOV(L) */
342 /* ***D287 FORMAT(/' AT 287 IN REDUCE--L,MOVMIN,NOVREF,', */
343 /* ***D 1 'NEWBOXP(L),NOV(L)',5I12 */
344 /* ***D 2 ' REDUCE ABORTED.') */
345 /* WRITE(KFILDO,2870) */
346 /* 2870 FORMAT(/' AN ERROR IN REDUCE ALGORITHM. ABORT REDUCE.') */
347  *ier = 714;
348  goto L410;
349 /* AN ABORT CAUSES THE CALLING PROGRAM TO REEXECUTE */
350 /* WITHOUT CALLING REDUCE. */
351  }
352 
353  }
354 
355  L288:
356  i__1 = newboxp[l - 1] + 1;
357  for (j = 1; j <= i__1; ++j) {
358  /*move = min(movmin,left);*/
359  move = (movmin < left) ? movmin : left;
360  jmin[lxn] = jmin[l];
361  jmax[lxn] = jmax[l];
362  lbit[lxn] = lbit[l];
363  nov[lxn] = move;
364  --lxn;
365  left -= move + *novref;
366 /* THE MOVE OF MOVE VALUES REALLY REPRESENTS A MOVE OF */
367 /* MOVE + NOVREF VALUES. */
368 /* L290: */
369  }
370 
371  if (left != -(*novref)) {
372 /* *** WRITE(KFILDO,292)L,LXN,MOVE,LXNKP,IBXX2(JJ),LEFT,NOV(L), */
373 /* *** 1 MOVMIN */
374 /* *** 292 FORMAT(' AT 292 IN REDUCE--L,LXN,MOVE,LXNKP,', */
375 /* *** 1 'IBXX2(JJ),LEFT,NOV(L),MOVMIN'/8I12) */
376  }
377 
378 /* L300: */
379  }
380 
381  *lx = lxnkp;
382 /* LX IS NOW THE NEW NUMBER OF GROUPS. */
383  *kbit = jj;
384 /* KBIT IS NOW THE NEW NUMBER OF BITS REQUIRED FOR PACKING */
385 /* GROUP LENGHTS. */
386  }
387 
388 /* WRITE(KFILDO,406)CFEED,LX */
389 /* 406 FORMAT(A1,/' *****************************************' */
390 /* 1 /' THE GROUP SIZES NOV( ) AFTER REDUCTION IN SIZE', */
391 /* 2 ' FOR'I10,' GROUPS', */
392 /* 3 /' *****************************************') */
393 /* WRITE(KFILDO,407) (NOV(J),J=1,LX) */
394 /* 407 FORMAT(/' '20I6) */
395 /* WRITE(KFILDO,408)CFEED,LX */
396 /* 408 FORMAT(A1,/' *****************************************' */
397 /* 1 /' THE GROUP MINIMA JMIN( ) AFTER REDUCTION IN SIZE', */
398 /* 2 ' FOR'I10,' GROUPS', */
399 /* 3 /' *****************************************') */
400 /* WRITE(KFILDO,409) (JMIN(J),J=1,LX) */
401 /* 409 FORMAT(/' '20I6) */
402 
403 L410:
404  if ( newbox != 0 ) free(newbox);
405  if ( newboxp != 0 ) free(newboxp);
406  return 0;
407 } /* reduce_ */
Header file for NCEPLIBS-g2c library.
float g2float
Float type.
Definition: grib2.h:22
int64_t g2int
Long integer type.
Definition: grib2.h:20
g2int integer
Integer type.
Definition: pack_gp.c:8
g2int integer
Integer type.
Definition: reduce.c:14
g2float real
Float type.
Definition: reduce.c:15
int reduce(integer *kfildo, integer *jmin, integer *jmax, integer *lbit, integer *nov, integer *lx, integer *ndg, integer *ibit, integer *jbit, integer *kbit, integer *novref, integer *ibxx2, integer *ier)
Determines whether the number of groups should be increased in order to reduce the size of the large ...
Definition: reduce.c:91