NCEPLIBS-w3emc  2.11.0
w3fi59.f
Go to the documentation of this file.
1 C> @file
2 C> @brief Form and pack positive, scaled differences.
3 C> @author Robert Allard @date 1984-08-01
4 
5 C> Converts an array of single precision real numbers into
6 C> an array of positive scaled differences (number(s) - minimum value),
7 C> in integer format and packs the argument-specified number of
8 C> significant bits from each difference.
9 C>
10 C> Program history log:
11 C> - Robert Allard 1984-08-01 ALLARD
12 C> - Ralph Jones 1990-05-17 Convert to cray cft77 fortran.
13 C> - Ralph Jones 1990-05-18 Change name pakmag to w3lib name w3fi59().
14 C> - Ralph Jones 1993-07-06 Add nint to do loop 2000 so numbers are
15 C> rounded to nearest integer, not truncated.
16 C> - Mark Iredell 1994-01-05 Computation of iscale fixed with respect to
17 C> the 93-07-06 change.
18 C> - Ebisuzaki 1998-06-30 Linux port.
19 C>
20 C> @param[in] FIELD Array of floating point data for processing (real)
21 C> @param[in] NPTS Number of data values to process in field (and nwork)
22 C> where, npts > 0
23 C> @param[in] NBITS Number of significant bits of processed data to be packed
24 C> where, 0 < nbits < 32+1
25 C> @param[out] NWORK Array for integer conversion (integer)
26 C> if packing performed (see note below), the array will
27 C> contain the pre-packed, right adjusted, scaled, integer
28 C> differences upon return to the user.
29 C> (the user may equivalence field and nwork. Same size.)
30 C> @param[out] NPFLD Array for packed data (character*1)
31 C> (dimension must be at least (nbits * npts) / 64 + 1)
32 C> @param[out] ISCALE Power of 2 for restoring data, such that
33 C> datum = (difference * 2**iscale) + rmin
34 C> @param[out] LEN Number of packed bytes in npfld (set to 0 if no packing)
35 C> where, len = (nbits * npts + 7) / 8 without remainder
36 C> @param[out] RMIN Minimum value (reference value subtracted from input data)
37 C> this is a cray floating point number, it will have to be
38 C> converted to an ibm370 32 bit floating point number at
39 C> some point in your program if you are packing grib data.
40 C>
41 C> @note: Len = 0 and no packing performed if
42 C> - (1) RMAX = RMIN (a constant field)
43 C> - (2) NBITS value out of range (see input argument)
44 C> - (3) NPTS value less than 1 (see input argument)
45 C>
46 C> @author Robert Allard @date 1984-08-01
47  SUBROUTINE w3fi59(FIELD,NPTS,NBITS,NWORK,NPFLD,ISCALE,LEN,RMIN)
48 C NATURAL LOGARITHM OF 2 AND 0.5 PLUS NOMINAL SAFE EPSILON
49  parameter(alog2=0.69314718056,hpeps=0.500001)
50 C
51  REAL FIELD(*)
52 C
53  CHARACTER*1 NPFLD(*)
54  INTEGER NWORK(*)
55 C
56  DATA kzero / 0 /
57 C
58 C / / / / / /
59 C
60  len = 0
61  iscale = 0
62  IF (nbits.LE.0.OR.nbits.GT.32) GO TO 3000
63  IF (npts.LE.0) GO TO 3000
64 C
65 C FIND THE MAX-MIN VALUES IN FIELD.
66 C
67  rmax = field(1)
68  rmin = rmax
69  DO 1000 k = 2,npts
70  rmax = amax1(rmax,field(k))
71  rmin = amin1(rmin,field(k))
72  1000 CONTINUE
73 C
74 C IF A CONSTANT FIELD, RETURN WITH NO PACKING PERFORMED AND 'LEN' = 0.
75 C
76  IF (rmax.EQ.rmin) GO TO 3000
77 C
78 C DETERMINE LARGEST DIFFERENCE IN FIELD (BIGDIF).
79 C
80  bigdif = rmax - rmin
81 C
82 C ISCALE IS THE POWER OF 2 REQUIRED TO RESTORE THE PACKED DATA.
83 C ISCALE IS COMPUTED AS THE LEAST INTEGER SUCH THAT
84 C BIGDIF*2**(-ISCALE) < 2**NBITS-0.5
85 C IN ORDER TO ENSURE THAT THE PACKED INTEGERS (COMPUTED IN LOOP 2000
86 C WITH THE NEAREST INTEGER FUNCTION) STAY LESS THAN 2**NBITS.
87 C
88  iscale=nint(alog(bigdif/(2.**nbits-0.5))/alog2+hpeps)
89 C
90 C FORM DIFFERENCES, RESCALE, AND CONVERT TO INTEGER FORMAT.
91 C
92  twon = 2.0 ** (-iscale)
93  DO 2000 k = 1,npts
94  nwork(k) = nint( (field(k) - rmin) * twon )
95  2000 CONTINUE
96 C
97 C PACK THE MAGNITUDES (RIGHTMOST NBITS OF EACH WORD).
98 C
99  koff = 0
100  iskip = 0
101 C
102 C USE NCAR ARRAY BIT PACKER SBYTES (GBYTES PACKAGE)
103 C
104  CALL sbytesc(npfld,nwork,koff,nbits,iskip,npts)
105 C
106 C ADD 7 ZERO-BITS AT END OF PACKED DATA TO INSURE BYTE BOUNDARY.
107 C USE NCAR WORD BIT PACKER SBYTE
108 C
109  noff = nbits * npts
110  CALL sbytec(npfld,kzero,noff,7)
111 C
112 C DETERMINE BYTE LENGTH (LEN) OF PACKED FIELD (NPFLD).
113 C
114  len = (noff + 7) / 8
115 C
116  3000 CONTINUE
117  RETURN
118 C
119  END
subroutine sbytec(OUT, IN, ISKIP, NBYTE)
This is a wrapper for sbytesc()
Definition: sbytec.f:14
subroutine sbytesc(OUT, IN, ISKIP, NBYTE, NSKIP, N)
Store bytes - pack bits: Put arbitrary size values into a packed bit string, taking the low order bit...
Definition: sbytesc.f:17
subroutine w3fi59(FIELD, NPTS, NBITS, NWORK, NPFLD, ISCALE, LEN, RMIN)
Converts an array of single precision real numbers into an array of positive scaled differences (numb...
Definition: w3fi59.f:48