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

Last change on this file since 241 was 232, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • 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;-
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      ;====================================================================
164      ; If DATA = 0, then we cannot take the common log, so return
165      ; zeroes for the result strings.  Use the FORMAT string.
166      ;====================================================================
167      endif else begin
168         A      = String( 0d0, Format=Format )
169         B      = A
170         Result[i] = A
171   
172      endelse
173   
174      ;====================================================================
175      ; Return result to calling program (depending on keyword settings)
176      ; Eliminate blanks if TRIM keyword is set
177      ;====================================================================
178      if ( POT_Only ) then $
179         Result[i] = B
180      if ( MANTISSA_Only ) then $
181         Result[i] = A
182      if ( Trim ) then $
183         Result[i] = StrCompress( Result[i], /Remove_All )
184     
185   endfor
186
187   if (n_elements(Result) eq 1) then $
188      Result = Result[0]
189 
190   return, Result
191
192end
Note: See TracBrowser for help on using the repository browser.