NCEPLIBS-g2c  1.6.4
pack_gp.c
Go to the documentation of this file.
1 
5 /*#include "f2c.h"*/
6 #include <stdlib.h>
7 #include "grib2.h"
8 typedef g2int integer;
9 typedef g2int logical;
10 #define TRUE_ (1)
11 #define FALSE_ (0)
255 int
256 pack_gp(integer *kfildo, integer *ic, integer *nxy,
257  integer *is523, integer *minpk, integer *inc, integer *missp, integer
258  *misss, integer *jmin, integer *jmax, integer *lbit, integer *nov,
259  integer *ndg, integer *lx, integer *ibit, integer *jbit, integer *
260  kbit, integer *novref, integer *lbitref, integer *ier)
261 {
262  /* Initialized data */
263 
264  const integer mallow = 1073741825; /* MALLOW=2**30+1 */
265  static integer ifeed = 12;
266  static integer ifirst = 0;
267 
268  /* System generated locals */
269  integer i__1, i__2, i__3;
270 
271  /* Local variables */
272  static integer j, k, l;
273  static logical adda;
274  static integer ired, kinc, mina, maxa, minb, maxb, minc, maxc, ibxx2[31];
275  static char cfeed[1];
276  static integer nenda, nendb, ibita, ibitb, minak, minbk, maxak, maxbk,
277  minck, maxck, nouta, lmiss, itest, nount;
278  extern /* Subroutine */ int reduce(integer *, integer *, integer *,
279  integer *, integer *, integer *, integer *, integer *, integer *,
280  integer *, integer *, integer *, integer *);
281  static integer ibitbs, mislla, misllb, misllc, iersav, lminpk, ktotal,
282  kounta, kountb, kstart, mstart, mintst, maxtst,
283  kounts, mintstk, maxtstk;
284  integer *misslx;
285 
286 
287 
288 /* NON SYSTEM SUBROUTINES CALLED */
289 /* NONE */
290 
291 
292 
293 /* MISSLX( ) was AN AUTOMATIC ARRAY. */
294  misslx = (integer *)calloc(*ndg,sizeof(integer));
295 
296 
297  /* Parameter adjustments */
298  --ic;
299  --nov;
300  --lbit;
301  --jmax;
302  --jmin;
303 
304  /* Function Body */
305 
306  *ier = 0;
307  iersav = 0;
308 /* CALL TIMPR(KFILDO,KFILDO,'START PACK_GP ') */
309  *(unsigned char *)cfeed = (char) ifeed;
310 
311  ired = 0;
312 /* IRED IS A FLAG. WHEN ZERO, REDUCE WILL BE CALLED. */
313 /* IF REDUCE ABORTS, IRED = 1 AND IS NOT CALLED. IN */
314 /* THIS CASE PACK_GP EXECUTES AGAIN EXCEPT FOR REDUCE. */
315 
316  if (*inc <= 0) {
317  iersav = 717;
318 /* WRITE(KFILDO,101)INC */
319 /* 101 FORMAT(/' ****INC ='I8,' NOT CORRECT IN PACK_GP. 1 IS USED.') */
320  }
321 
322 /* THERE WILL BE A RESTART OF PACK_GP IF SUBROUTINE REDUCE */
323 /* ABORTS. THIS SHOULD NOT HAPPEN, BUT IF IT DOES, PACK_GP */
324 /* WILL COMPLETE WITHOUT SUBROUTINE REDUCE. A NON FATAL */
325 /* DIAGNOSTIC RETURN IS PROVIDED. */
326 
327 L102:
328  /*kinc = max(*inc,1);*/
329  kinc = (*inc > 1) ? *inc : 1;
330  lminpk = *minpk;
331 
332 /* CALCULATE THE POWERS OF 2 THE FIRST TIME ENTERED. */
333 
334  if (ifirst == 0) {
335  ifirst = 1;
336  ibxx2[0] = 1;
337 
338  for (j = 1; j <= 30; ++j) {
339  ibxx2[j] = ibxx2[j - 1] << 1;
340 /* L104: */
341  }
342 
343  }
344 
345 /* THERE WILL BE A RESTART AT 105 IS NDG IS NOT LARGE ENOUGH. */
346 /* A NON FATAL DIAGNOSTIC RETURN IS PROVIDED. */
347 
348 L105:
349  kstart = 1;
350  ktotal = 0;
351  *lx = 0;
352  adda = FALSE_;
353  lmiss = 0;
354  if (*is523 == 1) {
355  lmiss = 1;
356  }
357  if (*is523 == 2) {
358  lmiss = 2;
359  }
360 
361 /* ************************************* */
362 
363 /* THIS SECTION COMPUTES STATISTICS FOR GROUP A. GROUP A IS */
364 /* A GROUP OF SIZE LMINPK. */
365 
366 /* ************************************* */
367 
368  ibita = 0;
369  mina = mallow;
370  maxa = -mallow;
371  minak = mallow;
372  maxak = -mallow;
373 
374 /* FIND THE MIN AND MAX OF GROUP A. THIS WILL INITIALLY BE OF */
375 /* SIZE LMINPK (IF THERE ARE STILL LMINPK VALUES IN IC( )), BUT */
376 /* WILL INCREASE IN SIZE IN INCREMENTS OF INC UNTIL A NEW */
377 /* GROUP IS STARTED. THE DEFINITION OF GROUP A IS DONE HERE */
378 /* ONLY ONCE (UPON INITIAL ENTRY), BECAUSE A GROUP B CAN ALWAYS */
379 /* BECOME A NEW GROUP A AFTER A IS PACKED, EXCEPT IF LMINPK */
380 /* HAS TO BE INCREASED BECAUSE NDG IS TOO SMALL. THEREFORE, */
381 /* THE SEPARATE LOOPS FOR MISSING AND NON-MISSING HERE BUYS */
382 /* ALMOST NOTHING. */
383 
384 /* Computing MIN */
385  i__1 = kstart + lminpk - 1;
386  /*nenda = min(i__1,*nxy);*/
387  nenda = (i__1 < *nxy) ? i__1 : *nxy;
388  if (*nxy - nenda <= lminpk / 2) {
389  nenda = *nxy;
390  }
391 /* ABOVE STATEMENT GUARANTEES THE LAST GROUP IS GT LMINPK/2 BY */
392 /* MAKING THE ACTUAL GROUP LARGER. IF A PROVISION LIKE THIS IS */
393 /* NOT INCLUDED, THERE WILL MANY TIMES BE A VERY SMALL GROUP */
394 /* AT THE END. USE SEPARATE LOOPS FOR MISSING AND NO MISSING */
395 /* VALUES FOR EFFICIENCY. */
396 
397 /* DETERMINE WHETHER THERE IS A LONG STRING OF THE SAME VALUE */
398 /* UNLESS NENDA = NXY. THIS MAY ALLOW A LARGE GROUP A TO */
399 /* START WITH, AS WITH MISSING VALUES. SEPARATE LOOPS FOR */
400 /* MISSING OPTIONS. THIS SECTION IS ONLY EXECUTED ONCE, */
401 /* IN DETERMINING THE FIRST GROUP. IT HELPS FOR AN ARRAY */
402 /* OF MOSTLY MISSING VALUES OR OF ONE VALUE, SUCH AS */
403 /* RADAR OR PRECIP DATA. */
404 
405  if (nenda != *nxy && ic[kstart] == ic[kstart + 1]) {
406 /* NO NEED TO EXECUTE IF FIRST TWO VALUES ARE NOT EQUAL. */
407 
408  if (*is523 == 0) {
409 /* THIS LOOP IS FOR NO MISSING VALUES. */
410 
411  i__1 = *nxy;
412  for (k = kstart + 1; k <= i__1; ++k) {
413 
414  if (ic[k] != ic[kstart]) {
415 /* Computing MAX */
416  i__2 = nenda, i__3 = k - 1;
417  /*nenda = max(i__2,i__3);*/
418  nenda = (i__2 > i__3) ? i__2 : i__3;
419  goto L114;
420  }
421 
422 /* L111: */
423  }
424 
425  nenda = *nxy;
426 /* FALL THROUGH THE LOOP MEANS ALL VALUES ARE THE SAME. */
427 
428  } else if (*is523 == 1) {
429 /* THIS LOOP IS FOR PRIMARY MISSING VALUES ONLY. */
430 
431  i__1 = *nxy;
432  for (k = kstart + 1; k <= i__1; ++k) {
433 
434  if (ic[k] != *missp) {
435 
436  if (ic[k] != ic[kstart]) {
437 /* Computing MAX */
438  i__2 = nenda, i__3 = k - 1;
439  /*nenda = max(i__2,i__3);*/
440  nenda = (i__2 > i__3) ? i__2 : i__3;
441  goto L114;
442  }
443 
444  }
445 
446 /* L112: */
447  }
448 
449  nenda = *nxy;
450 /* FALL THROUGH THE LOOP MEANS ALL VALUES ARE THE SAME. */
451 
452  } else {
453 /* THIS LOOP IS FOR PRIMARY AND SECONDARY MISSING VALUES. */
454 
455  i__1 = *nxy;
456  for (k = kstart + 1; k <= i__1; ++k) {
457 
458  if (ic[k] != *missp && ic[k] != *misss) {
459 
460  if (ic[k] != ic[kstart]) {
461 /* Computing MAX */
462  i__2 = nenda, i__3 = k - 1;
463  /*nenda = max(i__2,i__3);*/
464  nenda = (i__2 > i__3) ? i__2 : i__3;
465  goto L114;
466  }
467 
468  }
469 
470 /* L113: */
471  }
472 
473  nenda = *nxy;
474 /* FALL THROUGH THE LOOP MEANS ALL VALUES ARE THE SAME. */
475  }
476 
477  }
478 
479 L114:
480  if (*is523 == 0) {
481 
482  i__1 = nenda;
483  for (k = kstart; k <= i__1; ++k) {
484  if (ic[k] < mina) {
485  mina = ic[k];
486  minak = k;
487  }
488  if (ic[k] > maxa) {
489  maxa = ic[k];
490  maxak = k;
491  }
492 /* L115: */
493  }
494 
495  } else if (*is523 == 1) {
496 
497  i__1 = nenda;
498  for (k = kstart; k <= i__1; ++k) {
499  if (ic[k] == *missp) {
500  goto L117;
501  }
502  if (ic[k] < mina) {
503  mina = ic[k];
504  minak = k;
505  }
506  if (ic[k] > maxa) {
507  maxa = ic[k];
508  maxak = k;
509  }
510  L117:
511  ;
512  }
513 
514  } else {
515 
516  i__1 = nenda;
517  for (k = kstart; k <= i__1; ++k) {
518  if (ic[k] == *missp || ic[k] == *misss) {
519  goto L120;
520  }
521  if (ic[k] < mina) {
522  mina = ic[k];
523  minak = k;
524  }
525  if (ic[k] > maxa) {
526  maxa = ic[k];
527  maxak = k;
528  }
529  L120:
530  ;
531  }
532 
533  }
534 
535  kounta = nenda - kstart + 1;
536 
537 /* INCREMENT KTOTAL AND FIND THE BITS NEEDED TO PACK THE A GROUP. */
538 
539  ktotal += kounta;
540  mislla = 0;
541  if (mina != mallow) {
542  goto L125;
543  }
544 /* ALL MISSING VALUES MUST BE ACCOMMODATED. */
545  mina = 0;
546  maxa = 0;
547  mislla = 1;
548  ibitb = 0;
549  if (*is523 != 2) {
550  goto L130;
551  }
552 /* WHEN ALL VALUES ARE MISSING AND THERE ARE NO */
553 /* SECONDARY MISSING VALUES, IBITA = 0. */
554 /* OTHERWISE, IBITA MUST BE CALCULATED. */
555 
556 L125:
557  itest = maxa - mina + lmiss;
558 
559  for (ibita = 0; ibita <= 30; ++ibita) {
560  if (itest < ibxx2[ibita]) {
561  goto L130;
562  }
563 /* *** THIS TEST IS THE SAME AS: */
564 /* *** IF(MAXA-MINA.LT.IBXX2(IBITA)-LMISS)GO TO 130 */
565 /* L126: */
566  }
567 
568 /* WRITE(KFILDO,127)MAXA,MINA */
569 /* 127 FORMAT(' ****ERROR IN PACK_GP. VALUE WILL NOT PACK IN 30 BITS.', */
570 /* 1 ' MAXA ='I13,' MINA ='I13,'. ERROR AT 127.') */
571  *ier = 706;
572  goto L900;
573 
574 L130:
575 
576 /* ***D WRITE(KFILDO,131)KOUNTA,KTOTAL,MINA,MAXA,IBITA,MISLLA */
577 /* ***D131 FORMAT(' AT 130, KOUNTA ='I8,' KTOTAL ='I8,' MINA ='I8, */
578 /* ***D 1 ' MAXA ='I8,' IBITA ='I3,' MISLLA ='I3) */
579 
580 L133:
581  if (ktotal >= *nxy) {
582  goto L200;
583  }
584 
585 /* ************************************* */
586 
587 /* THIS SECTION COMPUTES STATISTICS FOR GROUP B. GROUP B IS A */
588 /* GROUP OF SIZE LMINPK IMMEDIATELY FOLLOWING GROUP A. */
589 
590 /* ************************************* */
591 
592 L140:
593  minb = mallow;
594  maxb = -mallow;
595  minbk = mallow;
596  maxbk = -mallow;
597  ibitbs = 0;
598  mstart = ktotal + 1;
599 
600 /* DETERMINE WHETHER THERE IS A LONG STRING OF THE SAME VALUE. */
601 /* THIS WORKS WHEN THERE ARE NO MISSING VALUES. */
602 
603  nendb = 1;
604 
605  if (mstart < *nxy) {
606 
607  if (*is523 == 0) {
608 /* THIS LOOP IS FOR NO MISSING VALUES. */
609 
610  i__1 = *nxy;
611  for (k = mstart + 1; k <= i__1; ++k) {
612 
613  if (ic[k] != ic[mstart]) {
614  nendb = k - 1;
615  goto L150;
616  }
617 
618 /* L145: */
619  }
620 
621  nendb = *nxy;
622 /* FALL THROUGH THE LOOP MEANS ALL REMAINING VALUES */
623 /* ARE THE SAME. */
624  }
625 
626  }
627 
628 L150:
629 /* Computing MAX */
630 /* Computing MIN */
631  i__3 = ktotal + lminpk;
632  /*i__1 = nendb, i__2 = min(i__3,*nxy);*/
633  i__1 = nendb, i__2 = (i__3 < *nxy) ? i__3 : *nxy;
634  /*nendb = max(i__1,i__2);*/
635  nendb = (i__1 > i__2) ? i__1 : i__2;
636 /* **** 150 NENDB=MIN(KTOTAL+LMINPK,NXY) */
637 
638  if (*nxy - nendb <= lminpk / 2) {
639  nendb = *nxy;
640  }
641 /* ABOVE STATEMENT GUARANTEES THE LAST GROUP IS GT LMINPK/2 BY */
642 /* MAKING THE ACTUAL GROUP LARGER. IF A PROVISION LIKE THIS IS */
643 /* NOT INCLUDED, THERE WILL MANY TIMES BE A VERY SMALL GROUP */
644 /* AT THE END. USE SEPARATE LOOPS FOR MISSING AND NO MISSING */
645 
646 /* USE SEPARATE LOOPS FOR MISSING AND NO MISSING VALUES */
647 /* FOR EFFICIENCY. */
648 
649  if (*is523 == 0) {
650 
651  i__1 = nendb;
652  for (k = mstart; k <= i__1; ++k) {
653  if (ic[k] <= minb) {
654  minb = ic[k];
655 /* NOTE LE, NOT LT. LT COULD BE USED BUT THEN A */
656 /* RECOMPUTE OVER THE WHOLE GROUP WOULD BE NEEDED */
657 /* MORE OFTEN. SAME REASONING FOR GE AND OTHER */
658 /* LOOPS BELOW. */
659  minbk = k;
660  }
661  if (ic[k] >= maxb) {
662  maxb = ic[k];
663  maxbk = k;
664  }
665 /* L155: */
666  }
667 
668  } else if (*is523 == 1) {
669 
670  i__1 = nendb;
671  for (k = mstart; k <= i__1; ++k) {
672  if (ic[k] == *missp) {
673  goto L157;
674  }
675  if (ic[k] <= minb) {
676  minb = ic[k];
677  minbk = k;
678  }
679  if (ic[k] >= maxb) {
680  maxb = ic[k];
681  maxbk = k;
682  }
683  L157:
684  ;
685  }
686 
687  } else {
688 
689  i__1 = nendb;
690  for (k = mstart; k <= i__1; ++k) {
691  if (ic[k] == *missp || ic[k] == *misss) {
692  goto L160;
693  }
694  if (ic[k] <= minb) {
695  minb = ic[k];
696  minbk = k;
697  }
698  if (ic[k] >= maxb) {
699  maxb = ic[k];
700  maxbk = k;
701  }
702  L160:
703  ;
704  }
705 
706  }
707 
708  kountb = nendb - ktotal;
709  misllb = 0;
710  if (minb != mallow) {
711  goto L165;
712  }
713 /* ALL MISSING VALUES MUST BE ACCOMMODATED. */
714  minb = 0;
715  maxb = 0;
716  misllb = 1;
717  ibitb = 0;
718 
719  if (*is523 != 2) {
720  goto L170;
721  }
722 /* WHEN ALL VALUES ARE MISSING AND THERE ARE NO SECONDARY */
723 /* MISSING VALUES, IBITB = 0. OTHERWISE, IBITB MUST BE */
724 /* CALCULATED. */
725 
726 L165:
727  for (ibitb = ibitbs; ibitb <= 30; ++ibitb) {
728  if (maxb - minb < ibxx2[ibitb] - lmiss) {
729  goto L170;
730  }
731 /* L166: */
732  }
733 
734 /* WRITE(KFILDO,167)MAXB,MINB */
735 /* 167 FORMAT(' ****ERROR IN PACK_GP. VALUE WILL NOT PACK IN 30 BITS.', */
736 /* 1 ' MAXB ='I13,' MINB ='I13,'. ERROR AT 167.') */
737  *ier = 706;
738  goto L900;
739 
740 /* COMPARE THE BITS NEEDED TO PACK GROUP B WITH THOSE NEEDED */
741 /* TO PACK GROUP A. IF IBITB GE IBITA, TRY TO ADD TO GROUP A. */
742 /* IF NOT, TRY TO ADD A'S POINTS TO B, UNLESS ADDITION TO A */
743 /* HAS BEEN DONE. THIS LATTER IS CONTROLLED WITH ADDA. */
744 
745 L170:
746 
747 /* ***D WRITE(KFILDO,171)KOUNTA,KTOTAL,MINA,MAXA,IBITA,MISLLA, */
748 /* ***D 1 MINB,MAXB,IBITB,MISLLB */
749 /* ***D171 FORMAT(' AT 171, KOUNTA ='I8,' KTOTAL ='I8,' MINA ='I8, */
750 /* ***D 1 ' MAXA ='I8,' IBITA ='I3,' MISLLA ='I3, */
751 /* ***D 2 ' MINB ='I8,' MAXB ='I8,' IBITB ='I3,' MISLLB ='I3) */
752 
753  if (ibitb >= ibita) {
754  goto L180;
755  }
756  if (adda) {
757  goto L200;
758  }
759 
760 /* ************************************* */
761 
762 /* GROUP B REQUIRES LESS BITS THAN GROUP A. PUT AS MANY OF A'S */
763 /* POINTS INTO B AS POSSIBLE WITHOUT EXCEEDING THE NUMBER OF */
764 /* BITS NECESSARY TO PACK GROUP B. */
765 
766 /* ************************************* */
767 
768  kounts = kounta;
769 /* KOUNTA REFERS TO THE PRESENT GROUP A. */
770  mintst = minb;
771  maxtst = maxb;
772  mintstk = minbk;
773  maxtstk = maxbk;
774 
775 /* USE SEPARATE LOOPS FOR MISSING AND NO MISSING VALUES */
776 /* FOR EFFICIENCY. */
777 
778  if (*is523 == 0) {
779 
780  i__1 = kstart;
781  for (k = ktotal; k >= i__1; --k) {
782 /* START WITH THE END OF THE GROUP AND WORK BACKWARDS. */
783  if (ic[k] < minb) {
784  mintst = ic[k];
785  mintstk = k;
786  } else if (ic[k] > maxb) {
787  maxtst = ic[k];
788  maxtstk = k;
789  }
790  if (maxtst - mintst >= ibxx2[ibitb]) {
791  goto L174;
792  }
793 /* NOTE THAT FOR THIS LOOP, LMISS = 0. */
794  minb = mintst;
795  maxb = maxtst;
796  minbk = mintstk;
797  maxbk = maxtstk;
798  --kounta;
799 /* THERE IS ONE LESS POINT NOW IN A. */
800 /* L1715: */
801  }
802 
803  } else if (*is523 == 1) {
804 
805  i__1 = kstart;
806  for (k = ktotal; k >= i__1; --k) {
807 /* START WITH THE END OF THE GROUP AND WORK BACKWARDS. */
808  if (ic[k] == *missp) {
809  goto L1718;
810  }
811  if (ic[k] < minb) {
812  mintst = ic[k];
813  mintstk = k;
814  } else if (ic[k] > maxb) {
815  maxtst = ic[k];
816  maxtstk = k;
817  }
818  if (maxtst - mintst >= ibxx2[ibitb] - lmiss) {
819  goto L174;
820  }
821 /* FOR THIS LOOP, LMISS = 1. */
822  minb = mintst;
823  maxb = maxtst;
824  minbk = mintstk;
825  maxbk = maxtstk;
826  misllb = 0;
827 /* WHEN THE POINT IS NON MISSING, MISLLB SET = 0. */
828  L1718:
829  --kounta;
830 /* THERE IS ONE LESS POINT NOW IN A. */
831 /* L1719: */
832  }
833 
834  } else {
835 
836  i__1 = kstart;
837  for (k = ktotal; k >= i__1; --k) {
838 /* START WITH THE END OF THE GROUP AND WORK BACKWARDS. */
839  if (ic[k] == *missp || ic[k] == *misss) {
840  goto L1729;
841  }
842  if (ic[k] < minb) {
843  mintst = ic[k];
844  mintstk = k;
845  } else if (ic[k] > maxb) {
846  maxtst = ic[k];
847  maxtstk = k;
848  }
849  if (maxtst - mintst >= ibxx2[ibitb] - lmiss) {
850  goto L174;
851  }
852 /* FOR THIS LOOP, LMISS = 2. */
853  minb = mintst;
854  maxb = maxtst;
855  minbk = mintstk;
856  maxbk = maxtstk;
857  misllb = 0;
858 /* WHEN THE POINT IS NON MISSING, MISLLB SET = 0. */
859  L1729:
860  --kounta;
861 /* THERE IS ONE LESS POINT NOW IN A. */
862 /* L173: */
863  }
864 
865  }
866 
867 /* AT THIS POINT, KOUNTA CONTAINS THE NUMBER OF POINTS TO CLOSE */
868 /* OUT GROUP A WITH. GROUP B NOW STARTS WITH KSTART+KOUNTA AND */
869 /* ENDS WITH NENDB. MINB AND MAXB HAVE BEEN ADJUSTED AS */
870 /* NECESSARY TO REFLECT GROUP B (EVEN THOUGH THE NUMBER OF BITS */
871 /* NEEDED TO PACK GROUP B HAVE NOT INCREASED, THE END POINTS */
872 /* OF THE RANGE MAY HAVE). */
873 
874 L174:
875  if (kounta == kounts) {
876  goto L200;
877  }
878 /* ON TRANSFER, GROUP A WAS NOT CHANGED. CLOSE IT OUT. */
879 
880 /* ONE OR MORE POINTS WERE TAKEN OUT OF A. RANGE AND IBITA */
881 /* MAY HAVE TO BE RECOMPUTED; IBITA COULD BE LESS THAN */
882 /* ORIGINALLY COMPUTED. IN FACT, GROUP A CAN NOW CONTAIN */
883 /* ONLY ONE POINT AND BE PACKED WITH ZERO BITS */
884 /* (UNLESS MISSS NE 0). */
885 
886  nouta = kounts - kounta;
887  ktotal -= nouta;
888  kountb += nouta;
889  if (nenda - nouta > minak && nenda - nouta > maxak) {
890  goto L200;
891  }
892 /* WHEN THE ABOVE TEST IS MET, THE MIN AND MAX OF THE */
893 /* CURRENT GROUP A WERE WITHIN THE OLD GROUP A, SO THE */
894 /* RANGE AND IBITA DO NOT NEED TO BE RECOMPUTED. */
895 /* NOTE THAT MINAK AND MAXAK ARE NO LONGER NEEDED. */
896  ibita = 0;
897  mina = mallow;
898  maxa = -mallow;
899 
900 /* USE SEPARATE LOOPS FOR MISSING AND NO MISSING VALUES */
901 /* FOR EFFICIENCY. */
902 
903  if (*is523 == 0) {
904 
905  i__1 = nenda - nouta;
906  for (k = kstart; k <= i__1; ++k) {
907  if (ic[k] < mina) {
908  mina = ic[k];
909  }
910  if (ic[k] > maxa) {
911  maxa = ic[k];
912  }
913 /* L1742: */
914  }
915 
916  } else if (*is523 == 1) {
917 
918  i__1 = nenda - nouta;
919  for (k = kstart; k <= i__1; ++k) {
920  if (ic[k] == *missp) {
921  goto L1744;
922  }
923  if (ic[k] < mina) {
924  mina = ic[k];
925  }
926  if (ic[k] > maxa) {
927  maxa = ic[k];
928  }
929  L1744:
930  ;
931  }
932 
933  } else {
934 
935  i__1 = nenda - nouta;
936  for (k = kstart; k <= i__1; ++k) {
937  if (ic[k] == *missp || ic[k] == *misss) {
938  goto L175;
939  }
940  if (ic[k] < mina) {
941  mina = ic[k];
942  }
943  if (ic[k] > maxa) {
944  maxa = ic[k];
945  }
946  L175:
947  ;
948  }
949 
950  }
951 
952  mislla = 0;
953  if (mina != mallow) {
954  goto L1750;
955  }
956 /* ALL MISSING VALUES MUST BE ACCOMMODATED. */
957  mina = 0;
958  maxa = 0;
959  mislla = 1;
960  if (*is523 != 2) {
961  goto L177;
962  }
963 /* WHEN ALL VALUES ARE MISSING AND THERE ARE NO SECONDARY */
964 /* MISSING VALUES IBITA = 0 AS ORIGINALLY SET. OTHERWISE, */
965 /* IBITA MUST BE CALCULATED. */
966 
967 L1750:
968  itest = maxa - mina + lmiss;
969 
970  for (ibita = 0; ibita <= 30; ++ibita) {
971  if (itest < ibxx2[ibita]) {
972  goto L177;
973  }
974 /* *** THIS TEST IS THE SAME AS: */
975 /* *** IF(MAXA-MINA.LT.IBXX2(IBITA)-LMISS)GO TO 177 */
976 /* L176: */
977  }
978 
979 /* WRITE(KFILDO,1760)MAXA,MINA */
980 /* 1760 FORMAT(' ****ERROR IN PACK_GP. VALUE WILL NOT PACK IN 30 BITS.', */
981 /* 1 ' MAXA ='I13,' MINA ='I13,'. ERROR AT 1760.') */
982  *ier = 706;
983  goto L900;
984 
985 L177:
986  goto L200;
987 
988 /* ************************************* */
989 
990 /* AT THIS POINT, GROUP B REQUIRES AS MANY BITS TO PACK AS GROUPA. */
991 /* THEREFORE, TRY TO ADD INC POINTS TO GROUP A WITHOUT INCREASING */
992 /* IBITA. THIS AUGMENTED GROUP IS CALLED GROUP C. */
993 
994 /* ************************************* */
995 
996 L180:
997  if (mislla == 1) {
998  minc = mallow;
999  minck = mallow;
1000  maxc = -mallow;
1001  maxck = -mallow;
1002  } else {
1003  minc = mina;
1004  maxc = maxa;
1005  minck = minak;
1006  maxck = minak;
1007  }
1008 
1009  nount = 0;
1010  if (*nxy - (ktotal + kinc) <= lminpk / 2) {
1011  kinc = *nxy - ktotal;
1012  }
1013 /* ABOVE STATEMENT CONSTRAINS THE LAST GROUP TO BE NOT LESS THAN */
1014 /* LMINPK/2 IN SIZE. IF A PROVISION LIKE THIS IS NOT INCLUDED, */
1015 /* THERE WILL MANY TIMES BE A VERY SMALL GROUP AT THE END. */
1016 
1017 /* USE SEPARATE LOOPS FOR MISSING AND NO MISSING VALUES */
1018 /* FOR EFFICIENCY. SINCE KINC IS USUALLY 1, USING SEPARATE */
1019 /* LOOPS HERE DOESN'T BUY MUCH. A MISSING VALUE WILL ALWAYS */
1020 /* TRANSFER BACK TO GROUP A. */
1021 
1022  if (*is523 == 0) {
1023 
1024 /* Computing MIN */
1025  i__2 = ktotal + kinc;
1026  /*i__1 = min(i__2,*nxy);*/
1027  i__1 = (i__2 < *nxy) ? i__2 : *nxy;
1028  for (k = ktotal + 1; k <= i__1; ++k) {
1029  if (ic[k] < minc) {
1030  minc = ic[k];
1031  minck = k;
1032  }
1033  if (ic[k] > maxc) {
1034  maxc = ic[k];
1035  maxck = k;
1036  }
1037  ++nount;
1038 /* L185: */
1039  }
1040 
1041  } else if (*is523 == 1) {
1042 
1043 /* Computing MIN */
1044  i__2 = ktotal + kinc;
1045  /*i__1 = min(i__2,*nxy);*/
1046  i__1 = (i__2 < *nxy) ? i__2 : *nxy;
1047  for (k = ktotal + 1; k <= i__1; ++k) {
1048  if (ic[k] == *missp) {
1049  goto L186;
1050  }
1051  if (ic[k] < minc) {
1052  minc = ic[k];
1053  minck = k;
1054  }
1055  if (ic[k] > maxc) {
1056  maxc = ic[k];
1057  maxck = k;
1058  }
1059  L186:
1060  ++nount;
1061 /* L187: */
1062  }
1063 
1064  } else {
1065 
1066 /* Computing MIN */
1067  i__2 = ktotal + kinc;
1068  /*i__1 = min(i__2,*nxy);*/
1069  i__1 = (i__2 < *nxy) ? i__2 : *nxy;
1070  for (k = ktotal + 1; k <= i__1; ++k) {
1071  if (ic[k] == *missp || ic[k] == *misss) {
1072  goto L189;
1073  }
1074  if (ic[k] < minc) {
1075  minc = ic[k];
1076  minck = k;
1077  }
1078  if (ic[k] > maxc) {
1079  maxc = ic[k];
1080  maxck = k;
1081  }
1082  L189:
1083  ++nount;
1084 /* L190: */
1085  }
1086 
1087  }
1088 
1089 /* ***D WRITE(KFILDO,191)KOUNTA,KTOTAL,MINA,MAXA,IBITA,MISLLA, */
1090 /* ***D 1 MINC,MAXC,NOUNT,IC(KTOTAL),IC(KTOTAL+1) */
1091 /* ***D191 FORMAT(' AT 191, KOUNTA ='I8,' KTOTAL ='I8,' MINA ='I8, */
1092 /* ***D 1 ' MAXA ='I8,' IBITA ='I3,' MISLLA ='I3, */
1093 /* ***D 2 ' MINC ='I8,' MAXC ='I8, */
1094 /* ***D 3 ' NOUNT ='I5,' IC(KTOTAL) ='I9,' IC(KTOTAL+1) =',I9) */
1095 
1096 /* IF THE NUMBER OF BITS NEEDED FOR GROUP C IS GT IBITA, */
1097 /* THEN THIS GROUP A IS A GROUP TO PACK. */
1098 
1099  if (minc == mallow) {
1100  minc = mina;
1101  maxc = maxa;
1102  minck = minak;
1103  maxck = maxak;
1104  misllc = 1;
1105  goto L195;
1106 /* WHEN THE NEW VALUE(S) ARE MISSING, THEY CAN ALWAYS */
1107 /* BE ADDED. */
1108 
1109  } else {
1110  misllc = 0;
1111  }
1112 
1113  if (maxc - minc >= ibxx2[ibita] - lmiss) {
1114  goto L200;
1115  }
1116 
1117 /* THE BITS NECESSARY FOR GROUP C HAS NOT INCREASED FROM THE */
1118 /* BITS NECESSARY FOR GROUP A. ADD THIS POINT(S) TO GROUP A. */
1119 /* COMPUTE THE NEXT GROUP B, ETC., UNLESS ALL POINTS HAVE BEEN */
1120 /* USED. */
1121 
1122 L195:
1123  ktotal += nount;
1124  kounta += nount;
1125  mina = minc;
1126  maxa = maxc;
1127  minak = minck;
1128  maxak = maxck;
1129  mislla = misllc;
1130  adda = TRUE_;
1131  if (ktotal >= *nxy) {
1132  goto L200;
1133  }
1134 
1135  if (minbk > ktotal && maxbk > ktotal) {
1136  mstart = nendb + 1;
1137 /* THE MAX AND MIN OF GROUP B WERE NOT FROM THE POINTS */
1138 /* REMOVED, SO THE WHOLE GROUP DOES NOT HAVE TO BE LOOKED */
1139 /* AT TO DETERMINE THE NEW MAX AND MIN. RATHER START */
1140 /* JUST BEYOND THE OLD NENDB. */
1141  ibitbs = ibitb;
1142  nendb = 1;
1143  goto L150;
1144  } else {
1145  goto L140;
1146  }
1147 
1148 /* ************************************* */
1149 
1150 /* GROUP A IS TO BE PACKED. STORE VALUES IN JMIN( ), JMAX( ), */
1151 /* LBIT( ), AND NOV( ). */
1152 
1153 /* ************************************* */
1154 
1155 L200:
1156  ++(*lx);
1157  if (*lx <= *ndg) {
1158  goto L205;
1159  }
1160  lminpk += lminpk / 2;
1161 /* WRITE(KFILDO,201)NDG,LMINPK,LX */
1162 /* 201 FORMAT(' ****NDG ='I5,' NOT LARGE ENOUGH.', */
1163 /* 1 ' LMINPK IS INCREASED TO 'I3,' FOR THIS FIELD.'/ */
1164 /* 2 ' LX = 'I10) */
1165  iersav = 716;
1166  goto L105;
1167 
1168 L205:
1169  jmin[*lx] = mina;
1170  jmax[*lx] = maxa;
1171  lbit[*lx] = ibita;
1172  nov[*lx] = kounta;
1173  kstart = ktotal + 1;
1174 
1175  if (mislla == 0) {
1176  misslx[*lx - 1] = mallow;
1177  } else {
1178  misslx[*lx - 1] = ic[ktotal];
1179 /* IC(KTOTAL) WAS THE LAST VALUE PROCESSED. IF MISLLA NE 0, */
1180 /* THIS MUST BE THE MISSING VALUE FOR THIS GROUP. */
1181  }
1182 
1183 /* ***D WRITE(KFILDO,206)MISLLA,IC(KTOTAL),KTOTAL,LX,JMIN(LX),JMAX(LX), */
1184 /* ***D 1 LBIT(LX),NOV(LX),MISSLX(LX) */
1185 /* ***D206 FORMAT(' AT 206, MISLLA ='I2,' IC(KTOTAL) ='I5,' KTOTAL ='I8, */
1186 /* ***D 1 ' LX ='I6,' JMIN(LX) ='I8,' JMAX(LX) ='I8, */
1187 /* ***D 2 ' LBIT(LX) ='I5,' NOV(LX) ='I8,' MISSLX(LX) =',I7) */
1188 
1189  if (ktotal >= *nxy) {
1190  goto L209;
1191  }
1192 
1193 /* THE NEW GROUP A WILL BE THE PREVIOUS GROUP B. SET LIMITS, ETC. */
1194 
1195  ibita = ibitb;
1196  mina = minb;
1197  maxa = maxb;
1198  minak = minbk;
1199  maxak = maxbk;
1200  mislla = misllb;
1201  nenda = nendb;
1202  kounta = kountb;
1203  ktotal += kounta;
1204  adda = FALSE_;
1205  goto L133;
1206 
1207 /* ************************************* */
1208 
1209 /* CALCULATE IBIT, THE NUMBER OF BITS NEEDED TO HOLD THE GROUP */
1210 /* MINIMUM VALUES. */
1211 
1212 /* ************************************* */
1213 
1214 L209:
1215  *ibit = 0;
1216 
1217  i__1 = *lx;
1218  for (l = 1; l <= i__1; ++l) {
1219  L210:
1220  if (jmin[l] < ibxx2[*ibit]) {
1221  goto L220;
1222  }
1223  ++(*ibit);
1224  goto L210;
1225  L220:
1226  ;
1227  }
1228 
1229 /* INSERT THE VALUE IN JMIN( ) TO BE USED FOR ALL MISSING */
1230 /* VALUES WHEN LBIT( ) = 0. WHEN SECONDARY MISSING */
1231 /* VALUES CAN BE PRESENT, LBIT(L) WILL NOT = 0. */
1232 
1233  if (*is523 == 1) {
1234 
1235  i__1 = *lx;
1236  for (l = 1; l <= i__1; ++l) {
1237 
1238  if (lbit[l] == 0) {
1239 
1240  if (misslx[l - 1] == *missp) {
1241  jmin[l] = ibxx2[*ibit] - 1;
1242  }
1243 
1244  }
1245 
1246 /* L226: */
1247  }
1248 
1249  }
1250 
1251 /* ************************************* */
1252 
1253 /* CALCULATE JBIT, THE NUMBER OF BITS NEEDED TO HOLD THE BITS */
1254 /* NEEDED TO PACK THE VALUES IN THE GROUPS. BUT FIND AND */
1255 /* REMOVE THE REFERENCE VALUE FIRST. */
1256 
1257 /* ************************************* */
1258 
1259 /* WRITE(KFILDO,228)CFEED,LX */
1260 /* 228 FORMAT(A1,/' *****************************************' */
1261 /* 1 /' THE GROUP WIDTHS LBIT( ) FOR ',I8,' GROUPS' */
1262 /* 2 /' *****************************************') */
1263 /* WRITE(KFILDO,229) (LBIT(J),J=1,MIN(LX,100)) */
1264 /* 229 FORMAT(/' '20I6) */
1265 
1266  *lbitref = lbit[1];
1267 
1268  i__1 = *lx;
1269  for (k = 1; k <= i__1; ++k) {
1270  if (lbit[k] < *lbitref) {
1271  *lbitref = lbit[k];
1272  }
1273 /* L230: */
1274  }
1275 
1276  if (*lbitref != 0) {
1277 
1278  i__1 = *lx;
1279  for (k = 1; k <= i__1; ++k) {
1280  lbit[k] -= *lbitref;
1281 /* L240: */
1282  }
1283 
1284  }
1285 
1286 /* WRITE(KFILDO,241)CFEED,LBITREF */
1287 /* 241 FORMAT(A1,/' *****************************************' */
1288 /* 1 /' THE GROUP WIDTHS LBIT( ) AFTER REMOVING REFERENCE ', */
1289 /* 2 I8, */
1290 /* 3 /' *****************************************') */
1291 /* WRITE(KFILDO,242) (LBIT(J),J=1,MIN(LX,100)) */
1292 /* 242 FORMAT(/' '20I6) */
1293 
1294  *jbit = 0;
1295 
1296  i__1 = *lx;
1297  for (k = 1; k <= i__1; ++k) {
1298  L310:
1299  if (lbit[k] < ibxx2[*jbit]) {
1300  goto L320;
1301  }
1302  ++(*jbit);
1303  goto L310;
1304  L320:
1305  ;
1306  }
1307 
1308 /* ************************************* */
1309 
1310 /* CALCULATE KBIT, THE NUMBER OF BITS NEEDED TO HOLD THE NUMBER */
1311 /* OF VALUES IN THE GROUPS. BUT FIND AND REMOVE THE */
1312 /* REFERENCE FIRST. */
1313 
1314 /* ************************************* */
1315 
1316 /* WRITE(KFILDO,321)CFEED,LX */
1317 /* 321 FORMAT(A1,/' *****************************************' */
1318 /* 1 /' THE GROUP SIZES NOV( ) FOR ',I8,' GROUPS' */
1319 /* 2 /' *****************************************') */
1320 /* WRITE(KFILDO,322) (NOV(J),J=1,MIN(LX,100)) */
1321 /* 322 FORMAT(/' '20I6) */
1322 
1323  *novref = nov[1];
1324 
1325  i__1 = *lx;
1326  for (k = 1; k <= i__1; ++k) {
1327  if (nov[k] < *novref) {
1328  *novref = nov[k];
1329  }
1330 /* L400: */
1331  }
1332 
1333  if (*novref > 0) {
1334 
1335  i__1 = *lx;
1336  for (k = 1; k <= i__1; ++k) {
1337  nov[k] -= *novref;
1338 /* L405: */
1339  }
1340 
1341  }
1342 
1343 /* WRITE(KFILDO,406)CFEED,NOVREF */
1344 /* 406 FORMAT(A1,/' *****************************************' */
1345 /* 1 /' THE GROUP SIZES NOV( ) AFTER REMOVING REFERENCE ',I8, */
1346 /* 2 /' *****************************************') */
1347 /* WRITE(KFILDO,407) (NOV(J),J=1,MIN(LX,100)) */
1348 /* 407 FORMAT(/' '20I6) */
1349 /* WRITE(KFILDO,408)CFEED */
1350 /* 408 FORMAT(A1,/' *****************************************' */
1351 /* 1 /' THE GROUP REFERENCES JMIN( )' */
1352 /* 2 /' *****************************************') */
1353 /* WRITE(KFILDO,409) (JMIN(J),J=1,MIN(LX,100)) */
1354 /* 409 FORMAT(/' '20I6) */
1355 
1356  *kbit = 0;
1357 
1358  i__1 = *lx;
1359  for (k = 1; k <= i__1; ++k) {
1360  L410:
1361  if (nov[k] < ibxx2[*kbit]) {
1362  goto L420;
1363  }
1364  ++(*kbit);
1365  goto L410;
1366  L420:
1367  ;
1368  }
1369 
1370 /* DETERMINE WHETHER THE GROUP SIZES SHOULD BE REDUCED */
1371 /* FOR SPACE EFFICIENCY. */
1372 
1373  if (ired == 0) {
1374  reduce(kfildo, &jmin[1], &jmax[1], &lbit[1], &nov[1], lx, ndg, ibit,
1375  jbit, kbit, novref, ibxx2, ier);
1376 
1377  if (*ier == 714 || *ier == 715) {
1378 /* REDUCE HAS ABORTED. REEXECUTE PACK_GP WITHOUT REDUCE. */
1379 /* PROVIDE FOR A NON FATAL RETURN FROM REDUCE. */
1380  iersav = *ier;
1381  ired = 1;
1382  *ier = 0;
1383  goto L102;
1384  }
1385 
1386  }
1387 
1388  if ( misslx != 0 ) {
1389  free(misslx);
1390  misslx=0;
1391  }
1392 /* CALL TIMPR(KFILDO,KFILDO,'END PACK_GP ') */
1393  if (iersav != 0) {
1394  *ier = iersav;
1395  return 0;
1396  }
1397 
1398 /* 900 IF(IER.NE.0)RETURN1 */
1399 
1400 L900:
1401  if ( misslx != 0 ) free(misslx);
1402  return 0;
1403 } /* pack_gp__ */
Header file for NCEPLIBS-g2c library.
int64_t g2int
Long integer type.
Definition: grib2.h:20
int pack_gp(integer *kfildo, integer *ic, integer *nxy, integer *is523, integer *minpk, integer *inc, integer *missp, integer *misss, integer *jmin, integer *jmax, integer *lbit, integer *nov, integer *ndg, integer *lx, integer *ibit, integer *jbit, integer *kbit, integer *novref, integer *lbitref, integer *ier)
Determines groups of variable size, but at least of size minpk, the associated max (jmax( )) and min ...
Definition: pack_gp.c:256
g2int integer
Integer type.
Definition: pack_gp.c:8
g2int logical
Logical type.
Definition: pack_gp.c:9
#define TRUE_
True.
Definition: pack_gp.c:10
#define FALSE_
False.
Definition: pack_gp.c:11
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