source: trunk/WRF.COMMON/WRFV3/external/io_grib2/g2lib/pack_gp.F @ 2759

Last change on this file since 2759 was 2759, checked in by aslmd, 2 years ago

adding unmodified code from WRFV3.0.1.1, expurged from useless data +1M size

File size: 41.6 KB
Line 
1      SUBROUTINE PACK_GP(KFILDO,IC,NXY,IS523,MINPK,INC,MISSP,MISSS,
2     1                   JMIN,JMAX,LBIT,NOV,NDG,LX,IBIT,JBIT,KBIT,
3     2                   NOVREF,LBITREF,IER)           
4C
5C        FEBRUARY 1994   GLAHN   TDL   MOS-2000
6C        JUNE     1995   GLAHN   MODIFIED FOR LMISS ERROR.
7C        JULY     1996   GLAHN   ADDED MISSS
8C        FEBRUARY 1997   GLAHN   REMOVED 4 REDUNDANT TESTS FOR
9C                                MISSP.EQ.0; INSERTED A TEST TO BETTER
10C                                HANDLE A STRING OF 9999'S
11C        FEBRUARY 1997   GLAHN   ADDED LOOPS TO ELIMINATE TEST FOR
12C                                MISSS WHEN MISSS = 0
13C        MARCH    1997   GLAHN   CORRECTED FOR SECONDARY MISSING VALUE
14C        MARCH    1997   GLAHN   CORRECTED FOR USE OF LOCAL VALUE
15C                                OF MINPK
16C        MARCH    1997   GLAHN   CORRECTED FOR SECONDARY MISSING VALUE
17C        MARCH    1997   GLAHN   CHANGED CALCULATING NUMBER OF BITS
18C                                THROUGH EXPONENTS TO AN ARRAY (IMPROVED
19C                                OVERALL PACKING PERFORMANCE BY ABOUT
20C                                35 PERCENT!).  ALLOWED 0 BITS FOR
21C                                PACKING JMIN( ), LBIT( ), AND NOV( ).
22C        MAY      1997   GLAHN   A NUMBER OF CHANGES FOR EFFICIENCY.
23C                                MOD FUNCTIONS ELIMINATED AND ONE
24C                                IFTHEN ADDED.  JOUNT REMOVED.
25C                                RECOMPUTATION OF BITS NOT MADE UNLESS
26C                                NECESSARY AFTER MOVING POINTS FROM
27C                                ONE GROUP TO ANOTHER.  NENDB ADJUSTED
28C                                TO ELIMINATE POSSIBILITY OF VERY
29C                                SMALL GROUP AT THE END.
30C                                ABOUT 8 PERCENT IMPROVEMENT IN
31C                                OVERALL PACKING.  ISKIPA REMOVED;
32C                                THERE IS ALWAYS A GROUP B THAT CAN
33C                                BECOME GROUP A.  CONTROL ON SIZE
34C                                OF GROUP B (STATEMENT BELOW 150)
35C                                ADDED.  ADDED ADDA, AND USE
36C                                OF GE AND LE INSTEAD OF GT AND LT
37C                                IN LOOPS BETWEEN 150 AND 160.
38C                                IBITBS ADDED TO SHORTEN TRIPS
39C                                THROUGH LOOP.
40C        MARCH    2000   GLAHN   MODIFIED FOR GRIB2; CHANGED NAME FROM
41C                                PACKGP
42C        JANUARY  2001   GLAHN   COMMENTS; IER = 706 SUBSTITUTED FOR
43C                                STOPS; ADDED RETURN1; REMOVED STATEMENT
44C                                NUMBER 110; ADDED IER AND * RETURN
45C        NOVEMBER 2001   GLAHN   CHANGED SOME DIAGNOSTIC FORMATS TO
46C                                ALLOW PRINTING LARGER NUMBERS
47C        NOVEMBER 2001   GLAHN   ADDED MISSLX( ) TO PUT MAXIMUM VALUE
48C                                INTO JMIN( ) WHEN ALL VALUES MISSING
49C                                TO AGREE WITH GRIB STANDARD.
50C        NOVEMBER 2001   GLAHN   CHANGED TWO TESTS ON MISSP AND MISSS
51C                                EQ 0 TO TESTS ON IS523.  HOWEVER,
52C                                MISSP AND MISSS CANNOT IN GENERAL BE
53C                                = 0.
54C        NOVEMBER 2001   GLAHN   ADDED CALL TO REDUCE; DEFINED ITEST
55C                                BEFORE LOOPS TO REDUCE COMPUTATION;
56C                                STARTED LARGE GROUP WHEN ALL SAME
57C                                VALUE
58C        DECEMBER 2001   GLAHN   MODIFIED AND ADDED A FEW COMMENTS
59C        JANUARY  2002   GLAHN   REMOVED LOOP BEFORE 150 TO DETERMINE
60C                                A GROUP OF ALL SAME VALUE
61C        JANUARY  2002   GLAHN   CHANGED MALLOW FROM 9999999 TO 2**30+1,
62C                                AND MADE IT A PARAMETER
63C        MARCH    2002   GLAHN   ADDED NON FATAL IER = 716, 717;
64C                                REMOVED NENDB=NXY ABOVE 150;
65C                                ADDED IERSAV=0; COMMENTS
66C
67C        PURPOSE
68C            DETERMINES GROUPS OF VARIABLE SIZE, BUT AT LEAST OF
69C            SIZE MINPK, THE ASSOCIATED MAX (JMAX( )) AND MIN (JMIN( )),
70C            THE NUMBER OF BITS NECESSARY TO HOLD THE VALUES IN EACH
71C            GROUP (LBIT( )), THE NUMBER OF VALUES IN EACH GROUP
72C            (NOV( )), THE NUMBER OF BITS NECESSARY TO PACK THE JMIN( )
73C            VALUES (IBIT), THE NUMBER OF BITS NECESSARY TO PACK THE
74C            LBIT( ) VALUES (JBIT), AND THE NUMBER OF BITS NECESSARY
75C            TO PACK THE NOV( ) VALUES (KBIT).  THE ROUTINE IS DESIGNED
76C            TO DETERMINE THE GROUPS SUCH THAT A SMALL NUMBER OF BITS
77C            IS NECESSARY TO PACK THE DATA WITHOUT EXCESSIVE
78C            COMPUTATIONS.  IF ALL VALUES IN THE GROUP ARE ZERO, THE
79C            NUMBER OF BITS TO USE IN PACKING IS DEFINED AS ZERO WHEN
80C            THERE CAN BE NO MISSING VALUES; WHEN THERE CAN BE MISSING
81C            VALUES, THE NUMBER OF BITS MUST BE AT LEAST 1 TO HAVE
82C            THE CAPABILITY TO RECOGNIZE THE MISSING VALUE.  HOWEVER,
83C            IF ALL VALUES IN A GROUP ARE MISSING, THE NUMBER OF BITS
84C            NEEDED IS 0, AND THE UNPACKER RECOGNIZES THIS.
85C            ALL VARIABLES ARE INTEGER.  EVEN THOUGH THE GROUPS ARE
86C            INITIALLY OF SIZE MINPK OR LARGER, AN ADJUSTMENT BETWEEN
87C            TWO GROUPS (THE LOOKBACK PROCEDURE) MAY MAKE A GROUP
88C            SMALLER THAN MINPK.  THE CONTROL ON GROUP SIZE IS THAT
89C            THE SUM OF THE SIZES OF THE TWO CONSECUTIVE GROUPS, EACH OF
90C            SIZE MINPK OR LARGER, IS NOT DECREASED.  WHEN DETERMINING
91C            THE NUMBER OF BITS NECESSARY FOR PACKING, THE LARGEST
92C            VALUE THAT CAN BE ACCOMMODATED IN, SAY, MBITS, IS
93C            2**MBITS-1; THIS LARGEST VALUE (AND THE NEXT SMALLEST
94C            VALUE) IS RESERVED FOR THE MISSING VALUE INDICATOR (ONLY)
95C            WHEN IS523 NE 0.  IF THE DIMENSION NDG
96C            IS NOT LARGE ENOUGH TO HOLD ALL THE GROUPS, THE LOCAL VALUE
97C            OF MINPK IS INCREASED BY 50 PERCENT.  THIS IS REPEATED
98C            UNTIL NDG WILL SUFFICE.  A DIAGNOSTIC IS PRINTED WHENEVER
99C            THIS HAPPENS, WHICH SHOULD BE VERY RARELY.  IF IT HAPPENS
100C            OFTEN, NDG IN SUBROUTINE PACK SHOULD BE INCREASED AND
101C            A CORRESPONDING INCREASE IN SUBROUTINE UNPACK MADE.
102C            CONSIDERABLE CODE IS PROVIDED SO THAT NO MORE CHECKING
103C            FOR MISSING VALUES WITHIN LOOPS IS DONE THAN NECESSARY;
104C            THE ADDED EFFICIENCY OF THIS IS RELATIVELY MINOR,
105C            BUT DOES NO HARM.  FOR GRIB2, THE REFERENCE VALUE FOR
106C            THE LENGTH OF GROUPS IN NOV( ) AND FOR THE NUMBER OF
107C            BITS NECESSARY TO PACK GROUP VALUES ARE DETERMINED,
108C            AND SUBTRACTED BEFORE JBIT AND KBIT ARE DETERMINED.
109C
110C            WHEN 1 OR MORE GROUPS ARE LARGE COMPARED TO THE OTHERS,
111C            THE WIDTH OF ALL GROUPS MUST BE AS LARGE AS THE LARGEST.
112C            A SUBROUTINE REDUCE BREAKS UP LARGE GROUPS INTO 2 OR
113C            MORE TO REDUCE TOTAL BITS REQUIRED.  IF REDUCE SHOULD
114C            ABORT, PACK_GP WILL BE EXECUTED AGAIN WITHOUT THE CALL
115C            TO REDUCE.
116C
117C        DATA SET USE
118C           KFILDO - UNIT NUMBER FOR OUTPUT (PRINT) FILE. (OUTPUT)
119C
120C        VARIABLES IN CALL SEQUENCE
121C              KFILDO = UNIT NUMBER FOR OUTPUT (PRINT) FILE.  (INPUT)
122C               IC( ) = ARRAY TO HOLD DATA FOR PACKING.  THE VALUES
123C                       DO NOT HAVE TO BE POSITIVE AT THIS POINT, BUT
124C                       MUST BE IN THE RANGE -2**30 TO +2**30 (THE
125C                       THE VALUE OF MALLOW).  THESE INTEGER VALUES
126C                       WILL BE RETAINED EXACTLY THROUGH PACKING AND
127C                       UNPACKING.  (INPUT)
128C                 NXY = NUMBER OF VALUES IN IC( ).  ALSO TREATED
129C                       AS ITS DIMENSION.  (INPUT)
130C              IS523  = missing value management
131C                       0=data contains no missing values
132C                       1=data contains Primary missing values
133C                       2=data contains Primary and secondary missing values
134C                       (INPUT)
135C               MINPK = THE MINIMUM SIZE OF EACH GROUP, EXCEPT POSSIBLY
136C                       THE LAST ONE.  (INPUT)
137C                 INC = THE NUMBER OF VALUES TO ADD TO AN ALREADY
138C                       EXISTING GROUP IN DETERMINING WHETHER OR NOT
139C                       TO START A NEW GROUP.  IDEALLY, THIS WOULD BE
140C                       1, BUT EACH TIME INC VALUES ARE ATTEMPTED, THE
141C                       MAX AND MIN OF THE NEXT MINPK VALUES MUST BE
142C                       FOUND.  THIS IS "A LOOP WITHIN A LOOP," AND
143C                       A SLIGHTLY LARGER VALUE MAY GIVE ABOUT AS GOOD
144C                       RESULTS WITH SLIGHTLY LESS COMPUTATIONAL TIME.
145C                       IF INC IS LE 0, 1 IS USED, AND A DIAGNOSTIC IS
146C                       OUTPUT.  NOTE:  IT IS EXPECTED THAT INC WILL
147C                       EQUAL 1.  THE CODE USES INC PRIMARILY IN THE
148C                       LOOPS STARTING AT STATEMENT 180.  IF INC
149C                       WERE 1, THERE WOULD NOT NEED TO BE LOOPS
150C                       AS SUCH.  HOWEVER, KINC (THE LOCAL VALUE OF
151C                       INC) IS SET GE 1 WHEN NEAR THE END OF THE DATA
152C                       TO FORESTALL A VERY SMALL GROUP AT THE END.
153C                       (INPUT)
154C               MISSP = WHEN MISSING POINTS CAN BE PRESENT IN THE DATA,
155C                       THEY WILL HAVE THE VALUE MISSP OR MISSS.
156C                       MISSP IS THE PRIMARY MISSING VALUE AND  MISSS
157C                       IS THE SECONDARY MISSING VALUE .  THESE MUST
158C                       NOT BE VALUES THAT WOULD OCCUR WITH SUBTRACTING
159C                       THE MINIMUM (REFERENCE) VALUE OR SCALING.
160C                       FOR EXAMPLE, MISSP = 0 WOULD NOT BE ADVISABLE.
161C                       (INPUT)
162C               MISSS = SECONDARY MISSING VALUE INDICATOR (SEE MISSP).
163C                       (INPUT)
164C             JMIN(J) = THE MINIMUM OF EACH GROUP (J=1,LX).  (OUTPUT)
165C             JMAX(J) = THE MAXIMUM OF EACH GROUP (J=1,LX).  THIS IS
166C                       NOT REALLY NEEDED, BUT SINCE THE MAX OF EACH
167C                       GROUP MUST BE FOUND, SAVING IT HERE IS CHEAP
168C                       IN CASE THE USER WANTS IT.  (OUTPUT)
169C             LBIT(J) = THE NUMBER OF BITS NECESSARY TO PACK EACH GROUP
170C                       (J=1,LX).  IT IS ASSUMED THE MINIMUM OF EACH
171C                       GROUP WILL BE REMOVED BEFORE PACKING, AND THE
172C                       VALUES TO PACK WILL, THEREFORE, ALL BE POSITIVE.
173C                       HOWEVER, IC( ) DOES NOT NECESSARILY CONTAIN
174C                       ALL POSITIVE VALUES.  IF THE OVERALL MINIMUM
175C                       HAS BEEN REMOVED (THE USUAL CASE), THEN IC( )
176C                       WILL CONTAIN ONLY POSITIVE VALUES.  (OUTPUT)
177C              NOV(J) = THE NUMBER OF VALUES IN EACH GROUP (J=1,LX).
178C                       (OUTPUT)
179C                 NDG = THE DIMENSION OF JMIN( ), JMAX( ), LBIT( ), AND
180C                       NOV( ).  (INPUT)
181C                  LX = THE NUMBER OF GROUPS DETERMINED.  (OUTPUT)
182C                IBIT = THE NUMBER OF BITS NECESSARY TO PACK THE JMIN(J)
183C                       VALUES, J=1,LX.  (OUTPUT)
184C                JBIT = THE NUMBER OF BITS NECESSARY TO PACK THE LBIT(J)
185C                       VALUES, J=1,LX.  (OUTPUT)
186C                KBIT = THE NUMBER OF BITS NECESSARY TO PACK THE NOV(J)
187C                       VALUES, J=1,LX.  (OUTPUT)
188C              NOVREF = REFERENCE VALUE FOR NOV( ).  (OUTPUT)
189C             LBITREF = REFERENCE VALUE FOR LBIT( ).  (OUTPUT)
190C                 IER = ERROR RETURN.
191C                       706 = VALUE WILL NOT PACK IN 30 BITS--FATAL
192C                       714 = ERROR IN REDUCE--NON-FATAL
193C                       715 = NGP NOT LARGE ENOUGH IN REDUCE--NON-FATAL
194C                       716 = MINPK INCEASED--NON-FATAL
195C                       717 = INC SET = 1--NON-FATAL
196C                       (OUTPUT)
197C                   * = ALTERNATE RETURN WHEN IER NE 0 AND FATAL ERROR.
198C
199C        INTERNAL VARIABLES
200C               CFEED = CONTAINS THE CHARACTER REPRESENTATION
201C                       OF A PRINTER FORM FEED.
202C               IFEED = CONTAINS THE INTEGER VALUE OF A PRINTER
203C                       FORM FEED.
204C                KINC = WORKING COPY OF INC.  MAY BE MODIFIED.
205C                MINA = MINIMUM VALUE IN GROUP A.
206C                MAXA = MAXIMUM VALUE IN GROUP A.
207C               NENDA = THE PLACE IN IC( ) WHERE GROUP A ENDS.
208C              KSTART = THE PLACE IN IC( ) WHERE GROUP A STARTS.
209C               IBITA = NUMBER OF BITS NEEDED TO HOLD VALUES IN GROUP A.
210C                MINB = MINIMUM VALUE IN GROUP B.
211C                MAXB = MAXIMUM VALUE IN GROUP B.
212C               NENDB = THE PLACE IN IC( ) WHERE GROUP B ENDS.
213C               IBITB = NUMBER OF BITS NEEDED TO HOLD VALUES IN GROUP B.
214C                MINC = MINIMUM VALUE IN GROUP C.
215C                MAXC = MAXIMUM VALUE IN GROUP C.
216C              KTOTAL = COUNT OF NUMBER OF VALUES IN IC( ) PROCESSED.
217C               NOUNT = NUMBER OF VALUES ADDED TO GROUP A.
218C               LMISS = 0 WHEN IS523 = 0.  WHEN PACKING INTO A
219C                       SPECIFIC NUMBER OF BITS, SAY MBITS,
220C                       THE MAXIMUM VALUE THAT CAN BE HANDLED IS
221C                       2**MBITS-1.  WHEN IS523 = 1, INDICATING
222C                       PRIMARY MISSING VALUES, THIS MAXIMUM VALUE
223C                       IS RESERVED TO HOLD THE PRIMARY MISSING VALUE
224C                       INDICATOR AND LMISS = 1.  WHEN IS523 = 2,
225C                       THE VALUE JUST BELOW THE MAXIMUM (I.E.,
226C                       2**MBITS-2) IS RESERVED TO HOLD THE SECONDARY
227C                       MISSING VALUE INDICATOR AND LMISS = 2.
228C              LMINPK = LOCAL VALUE OF MINPK.  THIS WILL BE ADJUSTED
229C                       UPWARD WHENEVER NDG IS NOT LARGE ENOUGH TO HOLD
230C                       ALL THE GROUPS.
231C              MALLOW = THE LARGEST ALLOWABLE VALUE FOR PACKING.
232C              MISLLA = SET TO 1 WHEN ALL VALUES IN GROUP A ARE MISSING.
233C                       THIS IS USED TO DISTINGUISH BETWEEN A REAL
234C                       MINIMUM WHEN ALL VALUES ARE NOT MISSING
235C                       AND A MINIMUM THAT HAS BEEN SET TO ZERO WHEN
236C                       ALL VALUES ARE MISSING.  0 OTHERWISE.
237C                       NOTE THAT THIS DOES NOT DISTINGUISH BETWEEN
238C                       PRIMARY AND SECONDARY MISSINGS WHEN SECONDARY
239C                       MISSINGS ARE PRESENT.  THIS MEANS THAT
240C                       LBIT( ) WILL NOT BE ZERO WITH THE RESULTING
241C                       COMPRESSION EFFICIENCY WHEN SECONDARY MISSINGS
242C                       ARE PRESENT.  ALSO NOTE THAT A CHECK HAS BEEN
243C                       MADE EARLIER TO DETERMINE THAT SECONDARY
244C                       MISSINGS ARE REALLY THERE.
245C              MISLLB = SET TO 1 WHEN ALL VALUES IN GROUP B ARE MISSING.
246C                       THIS IS USED TO DISTINGUISH BETWEEN A REAL
247C                       MINIMUM WHEN ALL VALUES ARE NOT MISSING
248C                       AND A MINIMUM THAT HAS BEEN SET TO ZERO WHEN
249C                       ALL VALUES ARE MISSING.  0 OTHERWISE.
250C              MISLLC = PERFORMS THE SAME FUNCTION FOR GROUP C THAT
251C                       MISLLA AND MISLLB DO FOR GROUPS B AND C,
252C                       RESPECTIVELY.
253C            IBXX2(J) = AN ARRAY THAT WHEN THIS ROUTINE IS FIRST ENTERED
254C                       IS SET TO 2**J, J=0,30. IBXX2(30) = 2**30, WHICH
255C                       IS THE LARGEST VALUE PACKABLE, BECAUSE 2**31
256C                       IS LARGER THAN THE INTEGER WORD SIZE.
257C              IFIRST = SET BY DATA STATEMENT TO 0.  CHANGED TO 1 ON
258C                       FIRST
259C                       ENTRY WHEN IBXX2( ) IS FILLED.
260C               MINAK = KEEPS TRACK OF THE LOCATION IN IC( ) WHERE THE
261C                       MINIMUM VALUE IN GROUP A IS LOCATED.
262C               MAXAK = DOES THE SAME AS MINAK, EXCEPT FOR THE MAXIMUM.
263C               MINBK = THE SAME AS MINAK FOR GROUP B.
264C               MAXBK = THE SAME AS MAXAK FOR GROUP B.
265C               MINCK = THE SAME AS MINAK FOR GROUP C.
266C               MAXCK = THE SAME AS MAXAK FOR GROUP C.
267C                ADDA = KEEPS TRACK WHETHER OR NOT AN ATTEMPT TO ADD
268C                       POINTS TO GROUP A WAS MADE.  IF SO, THEN ADDA
269C                       KEEPS FROM TRYING TO PUT ONE BACK INTO B.
270C                       (LOGICAL)
271C              IBITBS = KEEPS CURRENT VALUE IF IBITB SO THAT LOOP
272C                       ENDING AT 166 DOESN'T HAVE TO START AT
273C                       IBITB = 0 EVERY TIME.
274C           MISSLX(J) = MALLOW EXCEPT WHEN A GROUP IS ALL ONE VALUE (AND
275C                       LBIT(J) = 0) AND THAT VALUE IS MISSING.  IN
276C                       THAT CASE, MISSLX(J) IS MISSP OR MISSS.  THIS
277C                       GETS INSERTED INTO JMIN(J) LATER AS THE
278C                       MISSING INDICATOR; IT CAN'T BE PUT IN UNTIL
279C                       THE END, BECAUSE JMIN( ) IS USED TO CALCULATE
280C                       THE MAXIMUM NUMBER OF BITS (IBITS) NEEDED TO
281C                       PACK JMIN( ).
282C        1         2         3         4         5         6         7 X
283C
284C        NON SYSTEM SUBROUTINES CALLED
285C           NONE
286C
287      PARAMETER (MALLOW=2**30+1)
288C
289      CHARACTER*1 CFEED
290      LOGICAL ADDA
291C
292      DIMENSION IC(NXY)
293      DIMENSION JMIN(NDG),JMAX(NDG),LBIT(NDG),NOV(NDG)
294      DIMENSION MISSLX(NDG)
295C        MISSLX( ) IS AN AUTOMATIC ARRAY.
296      DIMENSION IBXX2(0:30)
297C
298      SAVE IBXX2
299C
300      DATA IFEED/12/
301      DATA IFIRST/0/
302C
303      IER=0
304      IERSAV=0
305C     CALL TIMPR(KFILDO,KFILDO,'START PACK_GP        ')
306      CFEED=CHAR(IFEED)
307C
308      IRED=0
309C        IRED IS A FLAG.  WHEN ZERO, REDUCE WILL BE CALLED.
310C        IF REDUCE ABORTS, IRED = 1 AND IS NOT CALLED.  IN
311C        THIS CASE PACK_GP EXECUTES AGAIN EXCEPT FOR REDUCE.
312C
313      IF(INC.LE.0)THEN
314         IERSAV=717
315C        WRITE(KFILDO,101)INC
316C101     FORMAT(/' ****INC ='I8,' NOT CORRECT IN PACK_GP.  1 IS USED.')
317      ENDIF
318C
319C        THERE WILL BE A RESTART OF PACK_GP IF SUBROUTINE REDUCE
320C        ABORTS.  THIS SHOULD NOT HAPPEN, BUT IF IT DOES, PACK_GP
321C        WILL COMPLETE WITHOUT SUBROUTINE REDUCE.  A NON FATAL
322C        DIAGNOSTIC RETURN IS PROVIDED.
323C
324 102  KINC=MAX(INC,1)
325      LMINPK=MINPK
326C
327C         CALCULATE THE POWERS OF 2 THE FIRST TIME ENTERED.
328C
329      IF(IFIRST.EQ.0)THEN
330         IFIRST=1
331         IBXX2(0)=1
332C
333         DO 104 J=1,30
334         IBXX2(J)=IBXX2(J-1)*2
335 104     CONTINUE
336C
337      ENDIF
338C
339C        THERE WILL BE A RESTART AT 105 IS NDG IS NOT LARGE ENOUGH.
340C        A NON FATAL DIAGNOSTIC RETURN IS PROVIDED.
341C
342 105  KSTART=1
343      KTOTAL=0
344      LX=0
345      ADDA=.FALSE.
346      LMISS=0
347      IF(IS523.EQ.1)LMISS=1
348      IF(IS523.EQ.2)LMISS=2
349C
350C        *************************************
351C
352C        THIS SECTION COMPUTES STATISTICS FOR GROUP A.  GROUP A IS
353C        A GROUP OF SIZE LMINPK.
354C
355C        *************************************
356C
357      IBITA=0
358      MINA=MALLOW
359      MAXA=-MALLOW
360      MINAK=MALLOW
361      MAXAK=-MALLOW
362C
363C        FIND THE MIN AND MAX OF GROUP A.  THIS WILL INITIALLY BE OF
364C        SIZE LMINPK (IF THERE ARE STILL LMINPK VALUES IN IC( )), BUT
365C        WILL INCREASE IN SIZE IN INCREMENTS OF INC UNTIL A NEW
366C        GROUP IS STARTED.  THE DEFINITION OF GROUP A IS DONE HERE
367C        ONLY ONCE (UPON INITIAL ENTRY), BECAUSE A GROUP B CAN ALWAYS
368C        BECOME A NEW GROUP A AFTER A IS PACKED, EXCEPT IF LMINPK
369C        HAS TO BE INCREASED BECAUSE NDG IS TOO SMALL.  THEREFORE,
370C        THE SEPARATE LOOPS FOR MISSING AND NON-MISSING HERE BUYS
371C        ALMOST NOTHING.
372C
373      NENDA=MIN(KSTART+LMINPK-1,NXY)
374      IF(NXY-NENDA.LE.LMINPK/2)NENDA=NXY
375C        ABOVE STATEMENT GUARANTEES THE LAST GROUP IS GT LMINPK/2 BY
376C        MAKING THE ACTUAL GROUP LARGER.  IF A PROVISION LIKE THIS IS
377C        NOT INCLUDED, THERE WILL MANY TIMES BE A VERY SMALL GROUP
378C        AT THE END.  USE SEPARATE LOOPS FOR MISSING AND NO MISSING
379C        VALUES FOR EFFICIENCY.
380C
381C        DETERMINE WHETHER THERE IS A LONG STRING OF THE SAME VALUE
382C        UNLESS NENDA = NXY.  THIS MAY ALLOW A LARGE GROUP A TO
383C        START WITH, AS WITH MISSING VALUES.   SEPARATE LOOPS FOR
384C        MISSING OPTIONS.  THIS SECTION IS ONLY EXECUTED ONCE,
385C        IN DETERMINING THE FIRST GROUP.  IT HELPS FOR AN ARRAY
386C        OF MOSTLY MISSING VALUES OR OF ONE VALUE, SUCH AS
387C        RADAR OR PRECIP DATA.
388C
389      IF(NENDA.NE.NXY.AND.IC(KSTART).EQ.IC(KSTART+1))THEN
390C           NO NEED TO EXECUTE IF FIRST TWO VALUES ARE NOT EQUAL.
391C
392         IF(IS523.EQ.0)THEN
393C              THIS LOOP IS FOR NO MISSING VALUES.
394C
395            DO 111 K=KSTART+1,NXY
396C
397               IF(IC(K).NE.IC(KSTART))THEN
398                  NENDA=MAX(NENDA,K-1)
399                  GO TO 114
400               ENDIF
401C
402 111        CONTINUE
403C
404            NENDA=NXY
405C              FALL THROUGH THE LOOP MEANS ALL VALUES ARE THE SAME.
406C
407         ELSEIF(IS523.EQ.1)THEN
408C              THIS LOOP IS FOR PRIMARY MISSING VALUES ONLY.
409C
410            DO 112 K=KSTART+1,NXY
411C       
412               IF(IC(K).NE.MISSP)THEN
413C
414                  IF(IC(K).NE.IC(KSTART))THEN
415                     NENDA=MAX(NENDA,K-1)
416                     GO TO 114
417                  ENDIF
418C
419               ENDIF
420C
421 112        CONTINUE
422C
423            NENDA=NXY
424C              FALL THROUGH THE LOOP MEANS ALL VALUES ARE THE SAME.
425C
426         ELSE
427C              THIS LOOP IS FOR PRIMARY AND SECONDARY MISSING VALUES.
428C
429            DO 113 K=KSTART+1,NXY
430C       
431               IF(IC(K).NE.MISSP.AND.IC(K).NE.MISSS)THEN
432C
433                  IF(IC(K).NE.IC(KSTART))THEN
434                     NENDA=MAX(NENDA,K-1)
435                     GO TO 114
436                  ENDIF
437C
438               ENDIF
439C
440 113        CONTINUE
441C
442            NENDA=NXY
443C              FALL THROUGH THE LOOP MEANS ALL VALUES ARE THE SAME.
444         ENDIF
445C
446      ENDIF
447C
448 114  IF(IS523.EQ.0)THEN
449C
450         DO 115 K=KSTART,NENDA
451         IF(IC(K).LT.MINA)THEN
452            MINA=IC(K)
453            MINAK=K
454         ENDIF
455         IF(IC(K).GT.MAXA)THEN
456            MAXA=IC(K)
457            MAXAK=K
458         ENDIF
459 115     CONTINUE
460C
461      ELSEIF(IS523.EQ.1)THEN
462C
463         DO 117 K=KSTART,NENDA
464         IF(IC(K).EQ.MISSP)GO TO 117
465         IF(IC(K).LT.MINA)THEN
466            MINA=IC(K)
467            MINAK=K
468         ENDIF
469         IF(IC(K).GT.MAXA)THEN
470            MAXA=IC(K)
471            MAXAK=K
472         ENDIF
473 117     CONTINUE
474C
475      ELSE
476C
477         DO 120 K=KSTART,NENDA
478         IF(IC(K).EQ.MISSP.OR.IC(K).EQ.MISSS)GO TO 120
479         IF(IC(K).LT.MINA)THEN
480            MINA=IC(K)
481            MINAK=K
482         ENDIF
483         IF(IC(K).GT.MAXA)THEN
484            MAXA=IC(K)
485            MAXAK=K
486         ENDIF
487 120     CONTINUE
488C
489      ENDIF
490C
491      KOUNTA=NENDA-KSTART+1
492C
493C        INCREMENT KTOTAL AND FIND THE BITS NEEDED TO PACK THE A GROUP.
494C
495      KTOTAL=KTOTAL+KOUNTA
496      MISLLA=0
497      IF(MINA.NE.MALLOW)GO TO 125
498C        ALL MISSING VALUES MUST BE ACCOMMODATED.
499      MINA=0
500      MAXA=0
501      MISLLA=1
502      IBITB=0
503      IF(IS523.NE.2)GO TO 130
504C        WHEN ALL VALUES ARE MISSING AND THERE ARE NO
505C        SECONDARY MISSING VALUES, IBITA = 0.
506C        OTHERWISE, IBITA MUST BE CALCULATED.
507C
508 125  ITEST=MAXA-MINA+LMISS
509
510      DO 126 IBITA=0,30
511      IF(ITEST.LT.IBXX2(IBITA))GO TO 130
512C***        THIS TEST IS THE SAME AS:
513C***     IF(MAXA-MINA.LT.IBXX2(IBITA)-LMISS)GO TO 130
514 126  CONTINUE
515C
516C     WRITE(KFILDO,127)MAXA,MINA
517C127  FORMAT(' ****ERROR IN PACK_GP.  VALUE WILL NOT PACK IN 30 BITS.',
518C    1       '  MAXA ='I13,'  MINA ='I13,'.  ERROR AT 127.')
519      IER=706
520      GO TO 900
521C
522 130  CONTINUE
523C
524C***D     WRITE(KFILDO,131)KOUNTA,KTOTAL,MINA,MAXA,IBITA,MISLLA
525C***D131  FORMAT(' AT 130, KOUNTA ='I8,'  KTOTAL ='I8,'  MINA ='I8,
526C***D    1       '  MAXA ='I8,'  IBITA ='I3,'  MISLLA ='I3)
527C
528 133  IF(KTOTAL.GE.NXY)GO TO 200
529C
530C        *************************************
531C
532C        THIS SECTION COMPUTES STATISTICS FOR GROUP B.  GROUP B IS A
533C        GROUP OF SIZE LMINPK IMMEDIATELY FOLLOWING GROUP A.
534C
535C        *************************************
536C
537 140  MINB=MALLOW
538      MAXB=-MALLOW
539      MINBK=MALLOW
540      MAXBK=-MALLOW
541      IBITBS=0
542      MSTART=KTOTAL+1
543C
544C        DETERMINE WHETHER THERE IS A LONG STRING OF THE SAME VALUE.
545C        THIS WORKS WHEN THERE ARE NO MISSING VALUES.
546C
547      NENDB=1
548C
549      IF(MSTART.LT.NXY)THEN
550C
551         IF(IS523.EQ.0)THEN
552C              THIS LOOP IS FOR NO MISSING VALUES.
553C
554            DO 145 K=MSTART+1,NXY
555C
556               IF(IC(K).NE.IC(MSTART))THEN
557                  NENDB=K-1
558                  GO TO 150
559               ENDIF
560C
561 145        CONTINUE
562C
563            NENDB=NXY
564C              FALL THROUGH THE LOOP MEANS ALL REMAINING VALUES
565C              ARE THE SAME.
566         ENDIF
567C
568      ENDIF
569C         
570 150  NENDB=MAX(NENDB,MIN(KTOTAL+LMINPK,NXY))
571C**** 150  NENDB=MIN(KTOTAL+LMINPK,NXY)
572C
573      IF(NXY-NENDB.LE.LMINPK/2)NENDB=NXY
574C        ABOVE STATEMENT GUARANTEES THE LAST GROUP IS GT LMINPK/2 BY
575C        MAKING THE ACTUAL GROUP LARGER.  IF A PROVISION LIKE THIS IS
576C        NOT INCLUDED, THERE WILL MANY TIMES BE A VERY SMALL GROUP
577C        AT THE END.  USE SEPARATE LOOPS FOR MISSING AND NO MISSING
578C
579C        USE SEPARATE LOOPS FOR MISSING AND NO MISSING VALUES
580C        FOR EFFICIENCY.
581C
582      IF(IS523.EQ.0)THEN
583C             
584         DO 155 K=MSTART,NENDB
585         IF(IC(K).LE.MINB)THEN
586            MINB=IC(K)
587C              NOTE LE, NOT LT.  LT COULD BE USED BUT THEN A
588C              RECOMPUTE OVER THE WHOLE GROUP WOULD BE NEEDED
589C              MORE OFTEN.  SAME REASONING FOR GE AND OTHER
590C              LOOPS BELOW.
591            MINBK=K
592         ENDIF
593         IF(IC(K).GE.MAXB)THEN
594            MAXB=IC(K)
595            MAXBK=K
596         ENDIF
597 155     CONTINUE
598C
599      ELSEIF(IS523.EQ.1)THEN
600C
601         DO 157 K=MSTART,NENDB
602         IF(IC(K).EQ.MISSP)GO TO 157
603         IF(IC(K).LE.MINB)THEN
604            MINB=IC(K)
605            MINBK=K
606         ENDIF
607         IF(IC(K).GE.MAXB)THEN
608            MAXB=IC(K)
609            MAXBK=K
610         ENDIF
611 157     CONTINUE
612C
613      ELSE
614C
615         DO 160 K=MSTART,NENDB
616         IF(IC(K).EQ.MISSP.OR.IC(K).EQ.MISSS)GO TO 160
617         IF(IC(K).LE.MINB)THEN
618            MINB=IC(K)
619            MINBK=K
620         ENDIF
621         IF(IC(K).GE.MAXB)THEN
622            MAXB=IC(K)
623            MAXBK=K
624         ENDIF
625 160     CONTINUE
626C
627      ENDIF
628C
629      KOUNTB=NENDB-KTOTAL
630      MISLLB=0
631      IF(MINB.NE.MALLOW)GO TO 165
632C        ALL MISSING VALUES MUST BE ACCOMMODATED.
633      MINB=0
634      MAXB=0
635      MISLLB=1
636      IBITB=0
637C
638      IF(IS523.NE.2)GO TO 170
639C        WHEN ALL VALUES ARE MISSING AND THERE ARE NO SECONDARY
640C        MISSING VALUES, IBITB = 0.  OTHERWISE, IBITB MUST BE
641C        CALCULATED.
642C
643 165  DO 166 IBITB=IBITBS,30
644         IF(MAXB-MINB.LT.IBXX2(IBITB)-LMISS)GO TO 170
645 166  CONTINUE
646C
647C     WRITE(KFILDO,167)MAXB,MINB
648C167  FORMAT(' ****ERROR IN PACK_GP.  VALUE WILL NOT PACK IN 30 BITS.',
649C    1       '  MAXB ='I13,'  MINB ='I13,'.  ERROR AT 167.')
650      IER=706
651      GO TO 900
652C
653C        COMPARE THE BITS NEEDED TO PACK GROUP B WITH THOSE NEEDED
654C        TO PACK GROUP A.  IF IBITB GE IBITA, TRY TO ADD TO GROUP A.
655C        IF NOT, TRY TO ADD A'S POINTS TO B, UNLESS ADDITION TO A
656C        HAS BEEN DONE.  THIS LATTER IS CONTROLLED WITH ADDA.
657C
658 170  CONTINUE
659C
660C***D     WRITE(KFILDO,171)KOUNTA,KTOTAL,MINA,MAXA,IBITA,MISLLA,
661C***D    1                               MINB,MAXB,IBITB,MISLLB
662C***D171  FORMAT(' AT 171, KOUNTA ='I8,'  KTOTAL ='I8,'  MINA ='I8,
663C***D    1       '  MAXA ='I8,'  IBITA ='I3,'  MISLLA ='I3,
664C***D    2       '  MINB ='I8,'  MAXB ='I8,'  IBITB ='I3,'  MISLLB ='I3) 
665C
666      IF(IBITB.GE.IBITA)GO TO 180
667      IF(ADDA)GO TO 200
668C
669C        *************************************
670C
671C        GROUP B REQUIRES LESS BITS THAN GROUP A.  PUT AS MANY OF A'S
672C        POINTS INTO B AS POSSIBLE WITHOUT EXCEEDING THE NUMBER OF
673C        BITS NECESSARY TO PACK GROUP B.
674C
675C        *************************************
676C
677      KOUNTS=KOUNTA
678C        KOUNTA REFERS TO THE PRESENT GROUP A.
679      MINTST=MINB
680      MAXTST=MAXB
681      MINTSTK=MINBK
682      MAXTSTK=MAXBK
683C
684C        USE SEPARATE LOOPS FOR MISSING AND NO MISSING VALUES
685C        FOR EFFICIENCY.
686C
687      IF(IS523.EQ.0)THEN
688C
689         DO 1715 K=KTOTAL,KSTART,-1
690C           START WITH THE END OF THE GROUP AND WORK BACKWARDS.
691         IF(IC(K).LT.MINB)THEN
692            MINTST=IC(K)
693            MINTSTK=K
694         ELSEIF(IC(K).GT.MAXB)THEN
695            MAXTST=IC(K)
696            MAXTSTK=K
697         ENDIF
698         IF(MAXTST-MINTST.GE.IBXX2(IBITB))GO TO 174
699C           NOTE THAT FOR THIS LOOP, LMISS = 0.
700         MINB=MINTST
701         MAXB=MAXTST
702         MINBK=MINTSTK
703         MAXBK=MAXTSTK
704         KOUNTA=KOUNTA-1
705C           THERE IS ONE LESS POINT NOW IN A.
706 1715    CONTINUE 
707C
708      ELSEIF(IS523.EQ.1)THEN           
709C
710         DO 1719 K=KTOTAL,KSTART,-1
711C           START WITH THE END OF THE GROUP AND WORK BACKWARDS.
712         IF(IC(K).EQ.MISSP)GO TO 1718
713         IF(IC(K).LT.MINB)THEN
714            MINTST=IC(K)
715            MINTSTK=K
716         ELSEIF(IC(K).GT.MAXB)THEN
717            MAXTST=IC(K)
718            MAXTSTK=K
719         ENDIF
720         IF(MAXTST-MINTST.GE.IBXX2(IBITB)-LMISS)GO TO 174
721C           FOR THIS LOOP, LMISS = 1.
722         MINB=MINTST
723         MAXB=MAXTST
724         MINBK=MINTSTK
725         MAXBK=MAXTSTK
726         MISLLB=0
727C           WHEN THE POINT IS NON MISSING, MISLLB SET = 0.
728 1718    KOUNTA=KOUNTA-1
729C           THERE IS ONE LESS POINT NOW IN A.
730 1719    CONTINUE 
731C
732      ELSE             
733C
734         DO 173 K=KTOTAL,KSTART,-1
735C           START WITH THE END OF THE GROUP AND WORK BACKWARDS.
736         IF(IC(K).EQ.MISSP.OR.IC(K).EQ.MISSS)GO TO 1729
737         IF(IC(K).LT.MINB)THEN
738            MINTST=IC(K)
739            MINTSTK=K
740         ELSEIF(IC(K).GT.MAXB)THEN
741            MAXTST=IC(K)
742            MAXTSTK=K
743         ENDIF
744         IF(MAXTST-MINTST.GE.IBXX2(IBITB)-LMISS)GO TO 174
745C           FOR THIS LOOP, LMISS = 2.
746         MINB=MINTST
747         MAXB=MAXTST
748         MINBK=MINTSTK
749         MAXBK=MAXTSTK
750         MISLLB=0
751C           WHEN THE POINT IS NON MISSING, MISLLB SET = 0.
752 1729    KOUNTA=KOUNTA-1
753C           THERE IS ONE LESS POINT NOW IN A.
754 173     CONTINUE 
755C
756      ENDIF
757C
758C        AT THIS POINT, KOUNTA CONTAINS THE NUMBER OF POINTS TO CLOSE
759C        OUT GROUP A WITH.  GROUP B NOW STARTS WITH KSTART+KOUNTA AND
760C        ENDS WITH NENDB.  MINB AND MAXB HAVE BEEN ADJUSTED AS
761C        NECESSARY TO REFLECT GROUP B (EVEN THOUGH THE NUMBER OF BITS
762C        NEEDED TO PACK GROUP B HAVE NOT INCREASED, THE END POINTS
763C        OF THE RANGE MAY HAVE).
764C
765 174  IF(KOUNTA.EQ.KOUNTS)GO TO 200
766C        ON TRANSFER, GROUP A WAS NOT CHANGED.  CLOSE IT OUT.
767C
768C        ONE OR MORE POINTS WERE TAKEN OUT OF A.  RANGE AND IBITA
769C        MAY HAVE TO BE RECOMPUTED; IBITA COULD BE LESS THAN
770C        ORIGINALLY COMPUTED.  IN FACT, GROUP A CAN NOW CONTAIN
771C        ONLY ONE POINT AND BE PACKED WITH ZERO BITS
772C        (UNLESS MISSS NE 0).
773C
774      NOUTA=KOUNTS-KOUNTA
775      KTOTAL=KTOTAL-NOUTA
776      KOUNTB=KOUNTB+NOUTA
777      IF(NENDA-NOUTA.GT.MINAK.AND.NENDA-NOUTA.GT.MAXAK)GO TO 200
778C        WHEN THE ABOVE TEST IS MET, THE MIN AND MAX OF THE
779C        CURRENT GROUP A WERE WITHIN THE OLD GROUP A, SO THE
780C        RANGE AND IBITA DO NOT NEED TO BE RECOMPUTED.
781C        NOTE THAT MINAK AND MAXAK ARE NO LONGER NEEDED.
782      IBITA=0
783      MINA=MALLOW
784      MAXA=-MALLOW
785C
786C        USE SEPARATE LOOPS FOR MISSING AND NO MISSING VALUES
787C        FOR EFFICIENCY.
788C
789      IF(IS523.EQ.0)THEN
790C
791         DO 1742 K=KSTART,NENDA-NOUTA
792         IF(IC(K).LT.MINA)THEN
793            MINA=IC(K)
794         ENDIF
795         IF(IC(K).GT.MAXA)THEN
796            MAXA=IC(K)
797         ENDIF
798 1742    CONTINUE
799C
800      ELSEIF(IS523.EQ.1)THEN
801C
802         DO 1744 K=KSTART,NENDA-NOUTA
803         IF(IC(K).EQ.MISSP)GO TO 1744
804         IF(IC(K).LT.MINA)THEN
805            MINA=IC(K)
806         ENDIF
807         IF(IC(K).GT.MAXA)THEN
808            MAXA=IC(K)
809         ENDIF
810 1744    CONTINUE
811C
812      ELSE
813C
814         DO 175 K=KSTART,NENDA-NOUTA
815         IF(IC(K).EQ.MISSP.OR.IC(K).EQ.MISSS)GO TO 175
816         IF(IC(K).LT.MINA)THEN
817            MINA=IC(K)
818         ENDIF
819         IF(IC(K).GT.MAXA)THEN
820            MAXA=IC(K)
821         ENDIF
822 175     CONTINUE
823C
824      ENDIF
825C
826      MISLLA=0
827      IF(MINA.NE.MALLOW)GO TO 1750
828C        ALL MISSING VALUES MUST BE ACCOMMODATED.
829      MINA=0
830      MAXA=0
831      MISLLA=1
832      IF(IS523.NE.2)GO TO 177
833C        WHEN ALL VALUES ARE MISSING AND THERE ARE NO SECONDARY
834C        MISSING VALUES IBITA = 0 AS ORIGINALLY SET.  OTHERWISE,
835C        IBITA MUST BE CALCULATED.
836C
837 1750 ITEST=MAXA-MINA+LMISS
838C
839      DO 176 IBITA=0,30
840      IF(ITEST.LT.IBXX2(IBITA))GO TO 177
841C***        THIS TEST IS THE SAME AS:
842C***         IF(MAXA-MINA.LT.IBXX2(IBITA)-LMISS)GO TO 177
843 176  CONTINUE
844C
845C     WRITE(KFILDO,1760)MAXA,MINA
846C1760 FORMAT(' ****ERROR IN PACK_GP.  VALUE WILL NOT PACK IN 30 BITS.',
847C    1       '  MAXA ='I13,'  MINA ='I13,'.  ERROR AT 1760.')
848      IER=706
849      GO TO 900
850C
851 177  CONTINUE
852      GO TO 200
853C
854C        *************************************
855C
856C        AT THIS POINT, GROUP B REQUIRES AS MANY BITS TO PACK AS GROUPA.
857C        THEREFORE, TRY TO ADD INC POINTS TO GROUP A WITHOUT INCREASING
858C        IBITA.  THIS AUGMENTED GROUP IS CALLED GROUP C.
859C
860C        *************************************
861C
862 180  IF(MISLLA.EQ.1)THEN
863         MINC=MALLOW
864         MINCK=MALLOW
865         MAXC=-MALLOW
866         MAXCK=-MALLOW
867      ELSE
868         MINC=MINA
869         MAXC=MAXA
870         MINCK=MINAK
871         MAXCK=MINAK
872      ENDIF
873C
874      NOUNT=0
875      IF(NXY-(KTOTAL+KINC).LE.LMINPK/2)KINC=NXY-KTOTAL
876C        ABOVE STATEMENT CONSTRAINS THE LAST GROUP TO BE NOT LESS THAN
877C        LMINPK/2 IN SIZE.  IF A PROVISION LIKE THIS IS NOT INCLUDED,
878C        THERE WILL MANY TIMES BE A VERY SMALL GROUP AT THE END.
879C
880C        USE SEPARATE LOOPS FOR MISSING AND NO MISSING VALUES
881C        FOR EFFICIENCY.  SINCE KINC IS USUALLY 1, USING SEPARATE
882C        LOOPS HERE DOESN'T BUY MUCH.  A MISSING VALUE WILL ALWAYS
883C        TRANSFER BACK TO GROUP A.
884C
885      IF(IS523.EQ.0)THEN
886C
887         DO 185 K=KTOTAL+1,MIN(KTOTAL+KINC,NXY)
888         IF(IC(K).LT.MINC)THEN
889            MINC=IC(K)
890            MINCK=K
891         ENDIF
892         IF(IC(K).GT.MAXC)THEN
893            MAXC=IC(K)
894            MAXCK=K
895         ENDIF
896         NOUNT=NOUNT+1
897 185     CONTINUE
898C
899      ELSEIF(IS523.EQ.1)THEN
900C
901         DO 187 K=KTOTAL+1,MIN(KTOTAL+KINC,NXY)
902         IF(IC(K).EQ.MISSP)GO TO 186
903         IF(IC(K).LT.MINC)THEN
904            MINC=IC(K)
905            MINCK=K
906         ENDIF
907         IF(IC(K).GT.MAXC)THEN
908            MAXC=IC(K)
909            MAXCK=K
910         ENDIF
911 186     NOUNT=NOUNT+1
912 187     CONTINUE
913C
914      ELSE
915C
916         DO 190 K=KTOTAL+1,MIN(KTOTAL+KINC,NXY)
917         IF(IC(K).EQ.MISSP.OR.IC(K).EQ.MISSS)GO TO 189
918         IF(IC(K).LT.MINC)THEN
919            MINC=IC(K)
920            MINCK=K
921         ENDIF
922         IF(IC(K).GT.MAXC)THEN
923            MAXC=IC(K)
924            MAXCK=K
925         ENDIF
926 189     NOUNT=NOUNT+1
927 190     CONTINUE
928C
929      ENDIF
930C
931C***D     WRITE(KFILDO,191)KOUNTA,KTOTAL,MINA,MAXA,IBITA,MISLLA,
932C***D    1   MINC,MAXC,NOUNT,IC(KTOTAL),IC(KTOTAL+1)
933C***D191  FORMAT(' AT 191, KOUNTA ='I8,'  KTOTAL ='I8,'  MINA ='I8,
934C***D    1       '  MAXA ='I8,'  IBITA ='I3,'  MISLLA ='I3,
935C***D    2       '  MINC ='I8,'  MAXC ='I8,
936C***D    3       '  NOUNT ='I5,'  IC(KTOTAL) ='I9,'  IC(KTOTAL+1) =',I9)
937C
938C        IF THE NUMBER OF BITS NEEDED FOR GROUP C IS GT IBITA,
939C        THEN THIS GROUP A IS A GROUP TO PACK.
940C
941      IF(MINC.EQ.MALLOW)THEN
942         MINC=MINA
943         MAXC=MAXA
944         MINCK=MINAK
945         MAXCK=MAXAK
946         MISLLC=1
947         GO TO 195
948C           WHEN THE NEW VALUE(S) ARE MISSING, THEY CAN ALWAYS
949C           BE ADDED.
950C
951      ELSE
952         MISLLC=0
953      ENDIF
954C
955      IF(MAXC-MINC.GE.IBXX2(IBITA)-LMISS) GO TO 200
956C
957C        THE BITS NECESSARY FOR GROUP C HAS NOT INCREASED FROM THE
958C        BITS NECESSARY FOR GROUP A.  ADD THIS POINT(S) TO GROUP A.
959C        COMPUTE THE NEXT GROUP B, ETC., UNLESS ALL POINTS HAVE BEEN
960C        USED.
961C
962 195  KTOTAL=KTOTAL+NOUNT
963      KOUNTA=KOUNTA+NOUNT
964      MINA=MINC
965      MAXA=MAXC
966      MINAK=MINCK
967      MAXAK=MAXCK
968      MISLLA=MISLLC
969      ADDA=.TRUE.
970      IF(KTOTAL.GE.NXY)GO TO 200
971C
972      IF(MINBK.GT.KTOTAL.AND.MAXBK.GT.KTOTAL)THEN
973         MSTART=NENDB+1
974C           THE MAX AND MIN OF GROUP B WERE NOT FROM THE POINTS
975C           REMOVED, SO THE WHOLE GROUP DOES NOT HAVE TO BE LOOKED
976C           AT TO DETERMINE THE NEW MAX AND MIN.  RATHER START
977C           JUST BEYOND THE OLD NENDB.
978         IBITBS=IBITB
979         NENDB=1
980         GO TO 150
981      ELSE
982         GO TO 140
983      ENDIF
984C
985C        *************************************
986C
987C        GROUP A IS TO BE PACKED.  STORE VALUES IN JMIN( ), JMAX( ),
988C        LBIT( ), AND NOV( ).
989C
990C        *************************************
991C
992 200  LX=LX+1
993      IF(LX.LE.NDG)GO TO 205
994      LMINPK=LMINPK+LMINPK/2
995C     WRITE(KFILDO,201)NDG,LMINPK,LX
996C201  FORMAT(' ****NDG ='I5,' NOT LARGE ENOUGH.',
997C    1       '  LMINPK IS INCREASED TO 'I3,' FOR THIS FIELD.'/
998C    2       '  LX = 'I10)
999      IERSAV=716
1000      GO TO 105
1001C
1002 205  JMIN(LX)=MINA
1003      JMAX(LX)=MAXA
1004      LBIT(LX)=IBITA
1005      NOV(LX)=KOUNTA
1006      KSTART=KTOTAL+1
1007C
1008      IF(MISLLA.EQ.0)THEN
1009         MISSLX(LX)=MALLOW
1010      ELSE
1011         MISSLX(LX)=IC(KTOTAL)
1012C           IC(KTOTAL) WAS THE LAST VALUE PROCESSED.  IF MISLLA NE 0,
1013C           THIS MUST BE THE MISSING VALUE FOR THIS GROUP.
1014      ENDIF
1015C
1016C***D     WRITE(KFILDO,206)MISLLA,IC(KTOTAL),KTOTAL,LX,JMIN(LX),JMAX(LX),
1017C***D    1                 LBIT(LX),NOV(LX),MISSLX(LX)
1018C***D206  FORMAT(' AT 206,  MISLLA ='I2,'  IC(KTOTAL) ='I5,'  KTOTAL ='I8,
1019C***D    1       '  LX ='I6,'  JMIN(LX) ='I8,'  JMAX(LX) ='I8,
1020C***D    2       '  LBIT(LX) ='I5,'  NOV(LX) ='I8,'  MISSLX(LX) =',I7)
1021C
1022      IF(KTOTAL.GE.NXY)GO TO 209
1023C
1024C        THE NEW GROUP A WILL BE THE PREVIOUS GROUP B.  SET LIMITS, ETC.
1025C
1026      IBITA=IBITB
1027      MINA=MINB
1028      MAXA=MAXB
1029      MINAK=MINBK
1030      MAXAK=MAXBK
1031      MISLLA=MISLLB
1032      NENDA=NENDB
1033      KOUNTA=KOUNTB
1034      KTOTAL=KTOTAL+KOUNTA
1035      ADDA=.FALSE.
1036      GO TO 133
1037C
1038C        *************************************
1039C
1040C        CALCULATE IBIT, THE NUMBER OF BITS NEEDED TO HOLD THE GROUP
1041C        MINIMUM VALUES.
1042C
1043C        *************************************
1044C
1045 209  IBIT=0
1046C
1047      DO 220 L=1,LX
1048 210  IF(JMIN(L).LT.IBXX2(IBIT))GO TO 220
1049      IBIT=IBIT+1
1050      GO TO 210
1051 220  CONTINUE
1052C
1053C        INSERT THE VALUE IN JMIN( ) TO BE USED FOR ALL MISSING
1054C        VALUES WHEN LBIT( ) = 0.  WHEN SECONDARY MISSING
1055C        VALUES CAN BE PRESENT, LBIT(L) WILL NOT = 0.
1056C
1057      IF(IS523.EQ.1)THEN
1058C
1059         DO 226 L=1,LX
1060C   
1061         IF(LBIT(L).EQ.0)THEN
1062C
1063            IF(MISSLX(L).EQ.MISSP)THEN
1064               JMIN(L)=IBXX2(IBIT)-1
1065            ENDIF
1066C
1067         ENDIF
1068C
1069 226     CONTINUE
1070C
1071      ENDIF
1072C
1073C        *************************************
1074C
1075C        CALCULATE JBIT, THE NUMBER OF BITS NEEDED TO HOLD THE BITS
1076C        NEEDED TO PACK THE VALUES IN THE GROUPS.  BUT FIND AND
1077C        REMOVE THE REFERENCE VALUE FIRST.
1078C
1079C        *************************************
1080C
1081C     WRITE(KFILDO,228)CFEED,LX
1082C228  FORMAT(A1,/' *****************************************'
1083C    1          /' THE GROUP WIDTHS LBIT( ) FOR ',I8,' GROUPS'
1084C    2          /' *****************************************')
1085C     WRITE(KFILDO,229) (LBIT(J),J=1,MIN(LX,100))
1086C229  FORMAT(/' '20I6)
1087C
1088      LBITREF=LBIT(1)
1089C
1090      DO 230 K=1,LX
1091      IF(LBIT(K).LT.LBITREF)LBITREF=LBIT(K)
1092 230  CONTINUE
1093C
1094      IF(LBITREF.NE.0)THEN
1095C
1096         DO 240 K=1,LX
1097         LBIT(K)=LBIT(K)-LBITREF
1098 240     CONTINUE
1099C
1100      ENDIF
1101C
1102C     WRITE(KFILDO,241)CFEED,LBITREF
1103C241  FORMAT(A1,/' *****************************************'
1104C    1          /' THE GROUP WIDTHS LBIT( ) AFTER REMOVING REFERENCE ',
1105C    2             I8,
1106C    3          /' *****************************************')
1107C     WRITE(KFILDO,242) (LBIT(J),J=1,MIN(LX,100))
1108C242  FORMAT(/' '20I6)
1109C
1110      JBIT=0
1111C
1112      DO 320 K=1,LX
1113 310  IF(LBIT(K).LT.IBXX2(JBIT))GO TO 320
1114      JBIT=JBIT+1
1115      GO TO 310
1116 320  CONTINUE
1117C
1118C        *************************************
1119C
1120C        CALCULATE KBIT, THE NUMBER OF BITS NEEDED TO HOLD THE NUMBER
1121C        OF VALUES IN THE GROUPS.  BUT FIND AND REMOVE THE
1122C        REFERENCE FIRST.
1123C
1124C        *************************************
1125C
1126C     WRITE(KFILDO,321)CFEED,LX
1127C321  FORMAT(A1,/' *****************************************'
1128C    1          /' THE GROUP SIZES NOV( ) FOR ',I8,' GROUPS'
1129C    2          /' *****************************************')
1130C     WRITE(KFILDO,322) (NOV(J),J=1,MIN(LX,100))
1131C322  FORMAT(/' '20I6)
1132C
1133      NOVREF=NOV(1)
1134C
1135      DO 400 K=1,LX
1136      IF(NOV(K).LT.NOVREF)NOVREF=NOV(K)
1137 400  CONTINUE
1138C
1139      IF(NOVREF.GT.0)THEN
1140C
1141         DO 405 K=1,LX
1142         NOV(K)=NOV(K)-NOVREF
1143 405     CONTINUE
1144C
1145      ENDIF
1146C
1147C     WRITE(KFILDO,406)CFEED,NOVREF
1148C406  FORMAT(A1,/' *****************************************'
1149C    1          /' THE GROUP SIZES NOV( ) AFTER REMOVING REFERENCE ',I8,
1150C    2          /' *****************************************')
1151C     WRITE(KFILDO,407) (NOV(J),J=1,MIN(LX,100))
1152C407  FORMAT(/' '20I6)
1153C     WRITE(KFILDO,408)CFEED
1154C408  FORMAT(A1,/' *****************************************'
1155C    1          /' THE GROUP REFERENCES JMIN( )'
1156C    2          /' *****************************************')
1157C     WRITE(KFILDO,409) (JMIN(J),J=1,MIN(LX,100))
1158C409  FORMAT(/' '20I6)
1159C
1160      KBIT=0
1161C
1162      DO 420 K=1,LX
1163 410  IF(NOV(K).LT.IBXX2(KBIT))GO TO 420
1164      KBIT=KBIT+1
1165      GO TO 410
1166 420  CONTINUE
1167C
1168C        DETERMINE WHETHER THE GROUP SIZES SHOULD BE REDUCED
1169C        FOR SPACE EFFICIENCY.
1170C
1171      IF(IRED.EQ.0)THEN
1172         CALL REDUCE(KFILDO,JMIN,JMAX,LBIT,NOV,LX,NDG,IBIT,JBIT,KBIT,
1173     1               NOVREF,IBXX2,IER)
1174C
1175         IF(IER.EQ.714.OR.IER.EQ.715)THEN
1176C              REDUCE HAS ABORTED.  REEXECUTE PACK_GP WITHOUT REDUCE.
1177C              PROVIDE FOR A NON FATAL RETURN FROM REDUCE. 
1178            IERSAV=IER
1179            IRED=1
1180            IER=0
1181            GO TO 102
1182         ENDIF
1183C
1184      ENDIF         
1185C
1186C     CALL TIMPR(KFILDO,KFILDO,'END   PACK_GP        ')
1187      IF(IERSAV.NE.0)THEN
1188         IER=IERSAV
1189         RETURN
1190      ENDIF
1191C
1192C 900  IF(IER.NE.0)RETURN1
1193C
1194 900  RETURN
1195      END
Note: See TracBrowser for help on using the repository browser.