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