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