1 | ;------------------------------------------------------------- |
---|
2 | ;+ |
---|
3 | ; |
---|
4 | ; @file_comments |
---|
5 | ; Given a number, returns a string of that B |
---|
6 | ; number in scientific notation format ( e.g. A x 10 ) |
---|
7 | ; |
---|
8 | ; @categories |
---|
9 | ; String Utilities |
---|
10 | ; |
---|
11 | ; @param DATA {in}{required} |
---|
12 | ; A floating point or integer number to be |
---|
13 | ; converted into a power of 10. |
---|
14 | ; |
---|
15 | ; @keyword FORMAT |
---|
16 | ; The format specification used in the string |
---|
17 | ; conversion for the mantissa (i.e. the |
---|
18 | ; "A" of "A x 10^B"). Default is '(f12.2)'. |
---|
19 | ; |
---|
20 | ; @keyword POT_ONLY |
---|
21 | ; Will return only the "power of 10" part of the |
---|
22 | ; string (i.e. the "10^B"). Default is to return |
---|
23 | ; the entire string (e.g. "A x 10^B" ) |
---|
24 | ; |
---|
25 | ; @keyword MANTISSA_ONLY |
---|
26 | ; return only mantissa of the string |
---|
27 | ; |
---|
28 | ; @keyword SHORT |
---|
29 | ; return 10^0 as '1' and 10^1 as '10' |
---|
30 | ; |
---|
31 | ; @keyword TRIM |
---|
32 | ; don't insert blanks (i.e. return Ax10^B) |
---|
33 | ; |
---|
34 | ; @restrictions |
---|
35 | ; This function does not "evaluate" the format statement thoroughly |
---|
36 | ; which can result in somewhat quirky strings. Example: |
---|
37 | ; print,strsci(-9.999) results in -10.0x10^0 instead of -1.0x10^1. |
---|
38 | ; |
---|
39 | ; Need a better symbol than the 'x' for the multiplier... |
---|
40 | ; |
---|
41 | ; @examples |
---|
42 | ; Result = STRSCI( 2000000, format='(i1)' ) |
---|
43 | ; print, result |
---|
44 | ; ; 6 |
---|
45 | ; ; prints 2 x 10!u6!n, which gets plotted as 2 x 10 |
---|
46 | ; |
---|
47 | ; Result = STRSCI( -0.0001 ) |
---|
48 | ; print, result |
---|
49 | ; ; 4 |
---|
50 | ; ; prints -1.00 x 10!u-4!n, which gets plotted as 1.00 x 10 |
---|
51 | ; |
---|
52 | ; Result = STRSCI( 0d0, format='(f13.8)' ) |
---|
53 | ; print, result |
---|
54 | ; ; |
---|
55 | ; ; prints, 0.00000000 |
---|
56 | ; |
---|
57 | ; |
---|
58 | ; @history |
---|
59 | ; bmy, 28 May 1998: VERSION 1.00 B |
---|
60 | ; - now returns string of the form A x 10 |
---|
61 | ; mgs, 29 May 1998: |
---|
62 | ; - bug fix: now allows negative numbers |
---|
63 | ; - keyword MANTISSA_ONLY added |
---|
64 | ; - default format changed to f12.2 |
---|
65 | ; bmy, 02 Jun 1998: |
---|
66 | ; - renamed to STRSCI ("STRing SCIentific notation"), |
---|
67 | ; mgs, 03 Jun 1998: |
---|
68 | ; - added TRIM keyword |
---|
69 | ; mgs, 22 Sep 1998: |
---|
70 | ; - added SHORT keyword |
---|
71 | ; - modified handling of TRIM keyword |
---|
72 | ; mgs, 24 Sep 1998: |
---|
73 | ; - bug fix with SHORT flag |
---|
74 | ; bmy & mgs, 02 Jun 1999: |
---|
75 | ; - now can handle DATA=0.0 correctly |
---|
76 | ; - updated comments |
---|
77 | ; mgs, 03 Jun 1999: |
---|
78 | ; - can now also handle values lt 1 ;-) |
---|
79 | ; - and doesn't choke on arrays |
---|
80 | ; |
---|
81 | ; @version |
---|
82 | ; $Id$ |
---|
83 | ; |
---|
84 | ;- |
---|
85 | ; Copyright (C) 1998, 1999 Bob Yantosca and Martin Schultz, |
---|
86 | ; Harvard University |
---|
87 | ; This software is provided as is without any warranty |
---|
88 | ; whatsoever. It may be freely used, copied or distributed |
---|
89 | ; for non-commercial purposes. This copyright notice must be |
---|
90 | ; kept with any copy of this software. If this software shall |
---|
91 | ; be used commercially or sold as part of a larger package, |
---|
92 | ; please contact the author to arrange payment. |
---|
93 | ; Bugs and comments should be directed to bmy@io.harvard.edu |
---|
94 | ; or mgs@io.harvard.edu with subject "IDL routine strsci" |
---|
95 | ;------------------------------------------------------------- |
---|
96 | |
---|
97 | |
---|
98 | function StrSci, Data, Format=Format, POT_Only=POT_Only, $ |
---|
99 | MANTISSA_ONLY=MANTISSA_ONLY,SHORT=SHORT,TRIM=TRIM |
---|
100 | ; |
---|
101 | compile_opt idl2, strictarrsubs |
---|
102 | ; |
---|
103 | |
---|
104 | ;==================================================================== |
---|
105 | ; Error checking / Keyword settings |
---|
106 | ;==================================================================== |
---|
107 | ;on_error, 2 |
---|
108 | |
---|
109 | if ( n_elements( Data ) eq 0 ) then begin |
---|
110 | return, '' |
---|
111 | endif |
---|
112 | |
---|
113 | if ( not Keyword_Set( Format ) ) then Format = '(f12.2)' |
---|
114 | |
---|
115 | POT_Only = keyword_set( POT_Only ) |
---|
116 | MANTISSA_Only = keyword_set( MANTISSA_Only ) |
---|
117 | Short = Keyword_Set( Short ) |
---|
118 | Trim = Keyword_Set( Trim ) |
---|
119 | |
---|
120 | NDat = n_elements(Data) |
---|
121 | Result = strarr(NDat) |
---|
122 | |
---|
123 | for i=0,NDat-1 do begin |
---|
124 | ;==================================================================== |
---|
125 | ; If ABS( DATA ) > 0 then we can proceed to take the common log. |
---|
126 | ; For DATA < 0, place a "-" sign in front of the number |
---|
127 | ;==================================================================== |
---|
128 | if ( Abs( Data[i] ) ne 0.0 ) then begin |
---|
129 | |
---|
130 | ; take the common log and store in LOG10DATA |
---|
131 | Log10Data = ALog10( Abs( Data[i] ) ) |
---|
132 | |
---|
133 | ; Boolean flag if data < 0 |
---|
134 | sign = ( Data[i] lt 0.0 ) |
---|
135 | |
---|
136 | ; Compute the characteristic (int part) |
---|
137 | ; Add the 1d-6 to prevent roundoff errors |
---|
138 | Characteristic = Fix( Log10Data + 1.0d-6 ) |
---|
139 | if (Log10Data lt 0) then $ |
---|
140 | Characteristic = Characteristic - 1 |
---|
141 | |
---|
142 | ; Compute the Mantissa (frac part) and take its antilog. |
---|
143 | Mantissa = Log10Data - Characteristic |
---|
144 | Mantissa = 10.0^Mantissa |
---|
145 | |
---|
146 | ; print,data[i],log10data,mantissa,characteristic,format='(3f24.14,i8)' |
---|
147 | |
---|
148 | ; String for the coefficient part, |
---|
149 | ; The coefficient is just antilog of the Mantissa |
---|
150 | ; Add the minus sign if DATA < 0.0 |
---|
151 | A = StrTrim( String( Mantissa, Format=Format ), 2 ) |
---|
152 | if ( Sign ) then A = '-' + A |
---|
153 | |
---|
154 | ; String for the power of 10 part |
---|
155 | B = '10!u' + strtrim( string( Characteristic ), 2 ) + '!n' |
---|
156 | if ( Short ) then begin |
---|
157 | if ( Characteristic eq 0 ) then B = '1' |
---|
158 | if ( Characteristic eq 1 ) then B = '10' |
---|
159 | endif |
---|
160 | |
---|
161 | ; composite string |
---|
162 | Result[i] = A + ' x ' + B |
---|
163 | if ( Short AND B eq '1') then Result[i] = A |
---|
164 | |
---|
165 | |
---|
166 | ;==================================================================== |
---|
167 | ; If DATA = 0, then we cannot take the common log, so return |
---|
168 | ; zeroes for the result strings. Use the FORMAT string. |
---|
169 | ;==================================================================== |
---|
170 | endif else begin |
---|
171 | A = String( 0d0, Format=Format ) |
---|
172 | B = A |
---|
173 | Result[i] = A |
---|
174 | |
---|
175 | endelse |
---|
176 | |
---|
177 | ;==================================================================== |
---|
178 | ; Return result to calling program (depending on keyword settings) |
---|
179 | ; Eliminate blanks if TRIM keyword is set |
---|
180 | ;==================================================================== |
---|
181 | if ( POT_Only ) then $ |
---|
182 | Result[i] = B |
---|
183 | if ( MANTISSA_Only ) then $ |
---|
184 | Result[i] = A |
---|
185 | if ( Trim ) then $ |
---|
186 | Result[i] = StrCompress( Result[i], /Remove_All ) |
---|
187 | |
---|
188 | endfor |
---|
189 | |
---|
190 | if (n_elements(Result) eq 1) then $ |
---|
191 | Result = Result[0] |
---|
192 | |
---|
193 | return, Result |
---|
194 | |
---|
195 | end |
---|