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

Last change on this file since 134 was 134, checked in by navarro, 18 years ago

change *.pro file properties (del eof-style, del executable, set keywords Id

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
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
106function StrSci, Data, Format=Format, POT_Only=POT_Only, $
107             MANTISSA_ONLY=MANTISSA_ONLY,SHORT=SHORT,TRIM=TRIM
108;
109  compile_opt idl2, strictarrsubs
110;
111
112   ;====================================================================
113   ; Error checking / Keyword settings
114   ;====================================================================
115   ;on_error, 2
116
117   if ( n_elements( Data ) eq 0 ) then begin
118      return, ''
119   endif
120
121   if ( not Keyword_Set( Format ) ) then Format   = '(f12.2)'
122
123   POT_Only      = keyword_set( POT_Only      )
124   MANTISSA_Only = keyword_set( MANTISSA_Only )
125   Short         = Keyword_Set( Short         )
126   Trim          = Keyword_Set( Trim          )
127
128   NDat = n_elements(Data)
129   Result = strarr(NDat)
130
131   for i=0,NDat-1 do begin
132      ;====================================================================
133      ; If ABS( DATA ) > 0 then we can proceed to take the common log.
134      ; For DATA < 0, place a "-" sign in front of the number
135      ;====================================================================
136      if ( Abs( Data[i] ) ne 0.0 ) then begin
137   
138         ; take the common log and store in LOG10DATA
139         Log10Data = ALog10( Abs( Data[i] ) ) 
140   
141         ; Boolean flag if data < 0
142         sign = ( Data[i] lt 0.0 )
143   
144         ; Compute the characteristic (int part)
145         ; Add the 1d-6 to prevent roundoff errors
146         Characteristic = Fix( Log10Data + 1.0d-6 )
147         if (Log10Data lt 0) then $
148            Characteristic = Characteristic - 1
149   
150         ; Compute the Mantissa (frac part) and take its antilog.
151         Mantissa = Log10Data - Characteristic
152         Mantissa = 10.0^Mantissa
153   
154   ;  print,data[i],log10data,mantissa,characteristic,format='(3f24.14,i8)'
155   
156         ; String for the coefficient part,
157         ; The coefficient is just antilog of the Mantissa
158         ; Add the minus sign if DATA < 0.0
159         A = StrTrim( String( Mantissa, Format=Format ), 2 )
160         if ( Sign ) then A = '-' + A
161   
162         ; String for the power of 10 part
163         B = '10!u' + strtrim( string( Characteristic ), 2 ) + '!n'
164         if ( Short ) then begin
165            if ( Characteristic eq 0 ) then B = '1'
166            if ( Characteristic eq 1 ) then B = '10'
167         endif
168   
169         ; composite string
170         Result[i] = A + ' x ' + B
171         if ( Short AND B eq '1') then Result[i] = A
172   
173   
174      ;====================================================================
175      ; If DATA = 0, then we cannot take the common log, so return
176      ; zeroes for the result strings.  Use the FORMAT string.
177      ;====================================================================
178      endif else begin
179         A      = String( 0d0, Format=Format )
180         B      = A
181         Result[i] = A
182   
183      endelse
184   
185      ;====================================================================
186      ; Return result to calling program (depending on keyword settings)
187      ; Eliminate blanks if TRIM keyword is set
188      ;====================================================================
189      if ( POT_Only ) then $
190         Result[i] = B
191      if ( MANTISSA_Only ) then $
192         Result[i] = A
193      if ( Trim ) then $
194         Result[i] = StrCompress( Result[i], /Remove_All )
195     
196   endfor
197
198   if (n_elements(Result) eq 1) then $
199      Result = Result[0]
200 
201   return, Result
202
203end
Note: See TracBrowser for help on using the repository browser.