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

Last change on this file since 374 was 374, checked in by pinsard, 16 years ago

improvements of headers (examples and alignments)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 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;
59; @history
60;        bmy, 28 May 1998: VERSION 1.00            B
61;           - now returns string of the form A x 10
62;        mgs, 29 May 1998:
63;           - bug fix: now allows negative numbers
64;           - keyword MANTISSA_ONLY added
65;           - default format changed to f12.2
66;        bmy, 02 Jun 1998:
67;           - renamed to STRSCI ("STRing SCIentific notation"),
68;        mgs, 03 Jun 1998:
69;           - added TRIM keyword
70;        mgs, 22 Sep 1998:
71;           - added SHORT keyword
72;           - modified handling of TRIM keyword
73;        mgs, 24 Sep 1998:
74;           - bug fix with SHORT flag
75;        bmy & mgs, 02 Jun 1999:
76;           - now can handle DATA=0.0 correctly
77;           - updated comments
78;        mgs, 03 Jun 1999:
79;           - can now also handle values lt 1 ;-)
80;           - and doesn't choke on arrays
81;
82; @version
83; $Id$
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;-
96FUNCTION strsci, data, FORMAT=format, POT_ONLY=pot_only, $
97             MANTISSA_ONLY=mantissa_only, SHORT=short, TRIM=trim
98;
99  compile_opt idl2, strictarrsubs
100;
101
102   ;====================================================================
103   ; Error checking / Keyword settings
104   ;====================================================================
105   ;on_error, 2
106
107   if ( n_elements( Data ) eq 0 ) then begin
108      return, ''
109   endif
110
111   if ( not Keyword_Set( Format ) ) then Format   = '(f12.2)'
112
113   POT_Only      = keyword_set( POT_Only      )
114   MANTISSA_Only = keyword_set( MANTISSA_Only )
115   Short         = Keyword_Set( Short         )
116   Trim          = Keyword_Set( Trim          )
117
118   NDat = n_elements(Data)
119   Result = strarr(NDat)
120
121   for i=0,NDat-1 do begin
122      ;====================================================================
123      ; If ABS( DATA ) > 0 then we can proceed to take the common log.
124      ; For DATA < 0, place a "-" sign in front of the number
125      ;====================================================================
126      if ( Abs( Data[i] ) ne 0.0 ) then begin
127   
128         ; take the common log and store in LOG10DATA
129         Log10Data = ALog10( Abs( Data[i] ) ) 
130   
131         ; Boolean flag if data < 0
132         sign = ( Data[i] lt 0.0 )
133   
134         ; Compute the characteristic (int part)
135         ; Add the 1d-6 to prevent roundoff errors
136         Characteristic = Fix( Log10Data + 1.0d-6 )
137         if (Log10Data lt 0) then $
138            Characteristic = Characteristic - 1
139   
140         ; Compute the Mantissa (frac part) and take its antilog.
141         Mantissa = Log10Data - Characteristic
142         Mantissa = 10.0^Mantissa
143   
144   ;  print,data[i],log10data,mantissa,characteristic,format='(3f24.14,i8)'
145   
146         ; String for the coefficient part,
147         ; The coefficient is just antilog of the Mantissa
148         ; Add the minus sign if DATA < 0.0
149         A = StrTrim( String( Mantissa, Format=Format ), 2 )
150         if ( Sign ) then A = '-' + A
151   
152         ; String for the power of 10 part
153         B = '10!u' + strtrim( string( Characteristic ), 2 ) + '!n'
154         if ( Short ) then begin
155            if ( Characteristic eq 0 ) then B = '1'
156            if ( Characteristic eq 1 ) then B = '10'
157         endif
158   
159         ; composite string
160         Result[i] = A + ' x ' + B
161         if ( Short AND B eq '1') then Result[i] = A
162   
163   
164      ;====================================================================
165      ; If DATA = 0, then we cannot take the common log, so return
166      ; zeroes for the result strings.  Use the FORMAT string.
167      ;====================================================================
168      endif else begin
169         A      = String( 0d0, Format=Format )
170         B      = A
171         Result[i] = A
172   
173      endelse
174   
175      ;====================================================================
176      ; Return result to calling program (depending on keyword settings)
177      ; Eliminate blanks if TRIM keyword is set
178      ;====================================================================
179      if ( POT_Only ) then $
180         Result[i] = B
181      if ( MANTISSA_Only ) then $
182         Result[i] = A
183      if ( Trim ) then $
184         Result[i] = StrCompress( Result[i], /Remove_All )
185     
186   endfor
187
188   if (n_elements(Result) eq 1) then $
189      Result = Result[0]
190 
191   return, Result
192
193end
Note: See TracBrowser for help on using the repository browser.