source: trunk/SRC/ToBeReviewed/STRING/strsci.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

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