Debian Codegraph @

packages/seacas/applications/explore/exp_numstr.f

raw
1 C Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 C of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 C NTESS, the U.S. Government retains certain rights in this software.
4 C
5 C See packages/seacas/LICENSE for details
6 C=======================================================================
7 SUBROUTINE NUMSTR (NNUM, NSIG, RNUM, RSTR, LSTR)
8 C=======================================================================
9
10 C --*** NUMSTR *** (STRLIB) Convert real numbers to strings
11 C --
12 C --NUMSTR converts a set of real numbers into a consistent set of
13 C --strings. It will convert to engineering notation with all
14 C --exponents the same, if possible.
15 C --
16 C --Parameters:
17 C -- NNUM - IN - the number of real numbers in the set
18 C -- NSIG - IN - the maximum number of significant digits, max of 8
19 C -- RNUM - IN - the array of real numbers to be converted
20 C -- RSTR - OUT - the set of real number strings
21 C -- LSTR - OUT - the maximum length of the number strings
22
23 C --Routines Called:
24 C -- IENGRX - (included) Get engineering notation exponent
25
26 INTEGER NNUM
27 INTEGER NSIG
28 REAL RNUM(*)
29 CHARACTER*(*) RSTR(*)
30 INTEGER LSTR
31
32 CHARACTER*20 BLANKS
33 CHARACTER*10 SCRFMT
34 CHARACTER*20 SCRSTR
35 CHARACTER*20 TMPSTR
36 CHARACTER*15 FFMT
37
38 C --Convert all to E notation and find the minimum and maximum exponent
39 C -- MINE and MAXE are the minimum and maximum exponents
40 C -- ISIGN is the number of digits for the sign
41 C -- (0 if all positive, 1 if any number negative)
42
43 BLANKS = ' '
44
45 WRITE (SCRFMT, 10000, IOSTAT=IDUM) NSIG+7, NSIG
46 10000 FORMAT ('(0PE', I2.2, '.', I2.2, ')')
47
48 ISIGN = 0
49 MINE = 9999
50 MINE2 = 9999
51 MAXE = -9999
52 MAXES = MAXE
53 DO 100 I = 1, NNUM
54 IF (RNUM(I) .NE. 0.0) THEN
55 WRITE (SCRSTR(1:NSIG+7), SCRFMT, IOSTAT=IDUM) RNUM(I)
56 READ (SCRSTR(NSIG+5:NSIG+7), '(I3)', IOSTAT=IDUM) IE
57 IF (MINE .GT. IE) MINE2 = MINE
58 MINE = MIN (MINE, IE)
59 MAXE = MAX (MAXE, IE)
60 IF (RNUM(I) .LT. 0.0) THEN
61 ISIGN = 1
62 MAXES = MAX (MAXES, IE)
63 END IF
64 END IF
65 100 CONTINUE
66
67 C --Correct for one very small number (should be zero)
68
69 IF ((MINE2 .LT. 1000) .AND. ((MINE2 - MINE) .GE. 6)) MINE = MINE2
70
71 C --Handle all zero case
72
73 IF (MINE .GT. MAXE) THEN
74 MINE = 0
75 MAXE = 0
76 MAXES = 0
77 END IF
78
79 C --Determine the new exponent NEWEXP (use engineering notation)
80
81 NEWEXP = IENGRX (MAXE, MINE)
82 IF (ISIGN .EQ. 1) THEN
83 IF (MAX (1, MAXE - NEWEXP) .GT. MAX (1, MAXES - NEWEXP))
84 & ISIGN = 0
85 END IF
86
87 C --Check if the numbers can all be sensibly converted to a common exponent
88
89 IF (((MAXE - NEWEXP) .LE. 4)
90 & .AND. ((NEWEXP - MINE) .LE. 2)
91 & .AND. (-MINE .LT. (NSIG - MAXE))) THEN
92
93 C --Determine the new F format
94 C -- EXPDIV is the number to divide by to get the number
95 C -- without an exponent
96 C -- NWHOLE is the number of digits before the decimal
97 C -- NFRAC is the number of digits after the decimal
98 C -- NTOTAL is the total number of digits
99 C --The new exponent is tagged on the end of the F-format number
100
101 EXPDIV = 10.0 ** NEWEXP
102
103 NWHOLE = MAX (1, MAXE - NEWEXP)
104 NFRAC = MAX (0, MIN (NEWEXP - MINE + NSIG,
105 & NSIG - (MAXE - NEWEXP)))
106 NTOTAL = ISIGN + NWHOLE + 1 + NFRAC
107 IF (EXPDIV .NE. 0.0) THEN
108 WRITE (FFMT, 10010, IOSTAT=IDUM) NTOTAL, NFRAC
109 10010 FORMAT ('(F', I2.2, '.', I2.2, ')')
110 ELSE
111 WRITE (FFMT, 10020, IOSTAT=IDUM) NTOTAL
112 10020 FORMAT ('(A', I2.2, 3X, ')')
113 END IF
114
115 IF (NEWEXP .EQ. 0) THEN
116 LSTR = NTOTAL
117 ELSE IF ((NEWEXP .LE. -10) .OR. (NEWEXP .GE. 10)) THEN
118 WRITE (FFMT(8:15), 10030, IOSTAT=IDUM) NEWEXP
119 10030 FORMAT (',''E', SP, I3.2, ''')')
120 LSTR = NTOTAL + 4
121 ELSE
122 WRITE (FFMT(8:15), 10040, IOSTAT=IDUM) NEWEXP
123 10040 FORMAT (',''E', SP, I2.1, ''')')
124 LSTR = NTOTAL + 3
125 END IF
126
127 C --Convert all numbers to the new exponent by using the F format
128
129 IF (EXPDIV .NE. 0.0) THEN
130 DO 110 I = 1, NNUM
131 WRITE (RSTR(I), FFMT, IOSTAT=IDUM) RNUM(I)/EXPDIV
132 if (rstr(i)(:1) .eq. '*') then
133 C ... Roundoff occurred. Adjust format and try again...
134 IF (EXPDIV .NE. 0.0) THEN
135 WRITE (FFMT(:7), 10010, IOSTAT=IDUM) NTOTAL,
136 $ NFRAC-1
137 WRITE (RSTR(I), FFMT, IOSTAT=IDUM) RNUM(I)/EXPDIV
138 end if
139 end if
140 110 CONTINUE
141 ELSE
142 DO 120 I = 1, NNUM
143 WRITE (RSTR(I), FFMT, IOSTAT=IDUM) '********************'
144 120 CONTINUE
145 END IF
146
147 ELSE
148
149 C --Do not try to use a common exponent, but use engineering notation;
150 C --Algorithm as above
151
152 LSTR = 0
153 MINEXP = IENGRX (MINE, MINE)
154 MAXEXP = IENGRX (MAXE, MAXE)
155
156 DO 130 I = 1, NNUM
157 WRITE (SCRSTR(1:NSIG+7), SCRFMT, IOSTAT=IDUM) RNUM(I)
158 READ (SCRSTR(NSIG+5:NSIG+7), '(I3)', IOSTAT=IDUM) IE
159 ISIGN = 0
160 IF (RNUM(I) .LT. 0.0) ISIGN = 1
161
162 NEWEXP = IENGRX (IE, IE)
163
164 EXPDIV = 10.0 ** NEWEXP
165
166 NWHOLE = MAX (1, IE - NEWEXP)
167 NFRAC = MAX (0, MIN (NEWEXP - IE + NSIG,
168 & NSIG - (IE - NEWEXP)))
169 IF ((RNUM(I) .EQ. 0.0) .AND. (MINE .GE. 0))
170 & NFRAC = NFRAC - 1
171 NTOTAL = ISIGN + NWHOLE + 1 + NFRAC
172 IF (EXPDIV .NE. 0.0) THEN
173 WRITE (FFMT, 10010, IOSTAT=IDUM) NTOTAL, NFRAC
174 ELSE
175 WRITE (FFMT, 10020, IOSTAT=IDUM) NTOTAL
176 END IF
177
178 IF ((MINEXP .LE. -10) .OR. (MAXEXP .GE. 10)) THEN
179 WRITE (FFMT(8:15), 10030, IOSTAT=IDUM) NEWEXP
180 LSTR = MAX (LSTR, NTOTAL + 4)
181 ELSE
182 WRITE (FFMT(8:15), 10040, IOSTAT=IDUM) NEWEXP
183 LSTR = MAX (LSTR, NTOTAL + 3)
184 END IF
185
186 IF (EXPDIV .NE. 0.0) THEN
187 WRITE (RSTR(I), FFMT, IOSTAT=IDUM) RNUM(I)/EXPDIV
188 if (rstr(i)(:1) .eq. '*') then
189 C ... Roundoff occurred. Adjust format and try again...
190 IF (EXPDIV .NE. 0.0) THEN
191 WRITE (FFMT(:7), 10010, IOSTAT=IDUM) NTOTAL,
192 $ NFRAC-1
193 WRITE (RSTR(I), FFMT, IOSTAT=IDUM) RNUM(I)/EXPDIV
194 end if
195 end if
196 ELSE
197 WRITE (RSTR(I), FFMT, IOSTAT=IDUM) '********************'
198 END IF
199 130 CONTINUE
200
201 C --Adjust the strings so that they are right-justified at
202 C --a common length
203
204 DO 140 I = 1, NNUM
205 IB = INDEX (RSTR(I)(:LSTR), ' ')
206 IF (IB .GT. 0) THEN
207 NB = LSTR - IB + 1
208 TMPSTR = RSTR(I)(:IB-1)
209 RSTR(I) = BLANKS(:NB) // TMPSTR
210 END IF
211 140 CONTINUE
212
213 END IF
214
215 RETURN
216 END