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