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