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

Last change on this file since 327 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.3 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  )
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;        Result = STRSCI( 2000000, format='(i1)' )
42;        print, result               
43;        ;                                                     6
44;        ;     prints 2 x 10!u6!n, which gets plotted as 2 x 10
45;       
46;        Result = STRSCI( -0.0001 )
47;        print, result
48;        ;                                                            4
49;        ;     prints -1.00 x 10!u-4!n, which gets plotted as 1.00 x 10
50;
51;        Result = STRSCI( 0d0, format='(f13.8)' )
52;        print, result
53;        ;
54;        ;     prints, 0.00000000
55;
56;
57; @history
58;        bmy, 28 May 1998: VERSION 1.00            B
59;           - now returns string of the form A x 10
60;        mgs, 29 May 1998:
61;           - bug fix: now allows negative numbers
62;           - keyword MANTISSA_ONLY added
63;           - default format changed to f12.2
64;        bmy, 02 Jun 1998:
65;           - renamed to STRSCI ("STRing SCIentific notation"),
66;        mgs, 03 Jun 1998:
67;           - added TRIM keyword
68;        mgs, 22 Sep 1998:
69;           - added SHORT keyword
70;           - modified handling of TRIM keyword
71;        mgs, 24 Sep 1998:
72;           - bug fix with SHORT flag
73;        bmy & mgs, 02 Jun 1999:
74;           - now can handle DATA=0.0 correctly
75;           - updated comments
76;        mgs, 03 Jun 1999:
77;           - can now also handle values lt 1 ;-)
78;           - and doesn't choke on arrays
79;
80; @version
81; $Id$
82;
83; Copyright (C) 1998, 1999 Bob Yantosca and Martin Schultz,
84; Harvard University
85; This software is provided as is without any warranty
86; whatsoever. It may be freely used, copied or distributed
87; for non-commercial purposes. This copyright notice must be
88; kept with any copy of this software. If this software shall
89; be used commercially or sold as part of a larger package,
90; please contact the author to arrange payment.
91; Bugs and comments should be directed to bmy\@io.harvard.edu
92; or mgs\@io.harvard.edu with subject "IDL routine strsci"
93;-
94FUNCTION strsci, data, FORMAT=format, POT_ONLY=pot_only, $
95             MANTISSA_ONLY=mantissa_only, SHORT=short, TRIM=trim
96;
97  compile_opt idl2, strictarrsubs
98;
99
100   ;====================================================================
101   ; Error checking / Keyword settings
102   ;====================================================================
103   ;on_error, 2
104
105   if ( n_elements( Data ) eq 0 ) then begin
106      return, ''
107   endif
108
109   if ( not Keyword_Set( Format ) ) then Format   = '(f12.2)'
110
111   POT_Only      = keyword_set( POT_Only      )
112   MANTISSA_Only = keyword_set( MANTISSA_Only )
113   Short         = Keyword_Set( Short         )
114   Trim          = Keyword_Set( Trim          )
115
116   NDat = n_elements(Data)
117   Result = strarr(NDat)
118
119   for i=0,NDat-1 do begin
120      ;====================================================================
121      ; If ABS( DATA ) > 0 then we can proceed to take the common log.
122      ; For DATA < 0, place a "-" sign in front of the number
123      ;====================================================================
124      if ( Abs( Data[i] ) ne 0.0 ) then begin
125   
126         ; take the common log and store in LOG10DATA
127         Log10Data = ALog10( Abs( Data[i] ) ) 
128   
129         ; Boolean flag if data < 0
130         sign = ( Data[i] lt 0.0 )
131   
132         ; Compute the characteristic (int part)
133         ; Add the 1d-6 to prevent roundoff errors
134         Characteristic = Fix( Log10Data + 1.0d-6 )
135         if (Log10Data lt 0) then $
136            Characteristic = Characteristic - 1
137   
138         ; Compute the Mantissa (frac part) and take its antilog.
139         Mantissa = Log10Data - Characteristic
140         Mantissa = 10.0^Mantissa
141   
142   ;  print,data[i],log10data,mantissa,characteristic,format='(3f24.14,i8)'
143   
144         ; String for the coefficient part,
145         ; The coefficient is just antilog of the Mantissa
146         ; Add the minus sign if DATA < 0.0
147         A = StrTrim( String( Mantissa, Format=Format ), 2 )
148         if ( Sign ) then A = '-' + A
149   
150         ; String for the power of 10 part
151         B = '10!u' + strtrim( string( Characteristic ), 2 ) + '!n'
152         if ( Short ) then begin
153            if ( Characteristic eq 0 ) then B = '1'
154            if ( Characteristic eq 1 ) then B = '10'
155         endif
156   
157         ; composite string
158         Result[i] = A + ' x ' + B
159         if ( Short AND B eq '1') then Result[i] = A
160   
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.