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