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