source: trunk/ToBeReviewed/MATRICE/cmapply.pro @ 31

Last change on this file since 31 was 31, checked in by pinsard, 18 years ago

upgrade of MATRICE according to cerbere.lodyc.jussieu.fr: /usr/home/smasson/SAXO_RD/ : files

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1;+
2; NAME:
3;   CMAPPLY
4;
5; AUTHOR:
6;   Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770
7;   craigm@lheamail.gsfc.nasa.gov
8;
9; PURPOSE:
10;   Applies a function to specified dimensions of an array
11;
12; MAJOR TOPICS:
13;   Arrays
14;
15; CALLING SEQUENCE:
16;   XX = CMAPPLY(OP, ARRAY, DIMS, [/DOUBLE], [TYPE=TYPE])
17;
18; DESCRIPTION:
19;   CMAPPLY will apply one of a few select functions to specified
20;   dimensions of an array.  Unlike some IDL functions, you *do* have
21;   a choice of which dimensions that are to be "collapsed" by this
22;   function.  Iterative loops are avoided where possible, for
23;   performance reasons.
24;
25;   The possible functions are:             (and number of loop iterations:)
26;     +     - Performs a sum (as in TOTAL)       number of collapsed dimensions
27;     AND   - Finds LOGICAL "AND" (not bitwise)  same
28;     OR    - Finds LOGICAL "OR"  (not bitwise)  same
29;     *     - Performs a product                 LOG_2[no. of collapsed elts.]
30;
31;     MIN   - Finds the minimum value            smaller of no. of collapsed
32;     MAX   - Finds the maximum value            or output elements
33;
34;     USER  - Applies user-defined function      no. of output elements
35;
36;
37;   It is possible to perform user-defined operations arrays using
38;   CMAPPLY.  The OP parameter is set to 'USER:FUNCTNAME', where
39;   FUNCTNAME is the name of a user-defined function.  The user
40;   defined function should be defined such that it accepts a single
41;   parameter, a vector, and returns a single scalar value.  Here is a
42;   prototype for the function definition:
43;
44;      FUNCTION FUNCTNAME, x, KEYWORD1=key1, ...
45;         scalar = ... function of x or keywords ...
46;         RETURN, scalar
47;      END
48;
49;   The function may accept keywords.  Keyword values are passed in to
50;   CMAPPLY through the FUNCTARGS keywords parameter, and passed to
51;   the user function via the _EXTRA mechanism.  Thus, while the
52;   definition of the user function is highly constrained in the
53;   number of positional parameters, there is absolute freedom in
54;   passing keyword parameters.
55;
56;   It's worth noting however, that the implementation of user-defined
57;   functions is not particularly optimized for speed.  Users are
58;   encouraged to implement their own array if the number of output
59;   elements is large.
60;
61;
62; INPUTS:
63;
64;   OP - The operation to perform, as a string.  May be upper or lower
65;        case.
66;
67;        If a user-defined operation is to be passed, then OP is of
68;        the form, 'USER:FUNCTNAME', where FUNCTNAME is the name of
69;        the user-defined function.
70;
71;   ARRAY - An array of values to be operated on.  Must not be of type
72;           STRING (7) or STRUCTURE (8).
73;
74; OPTIONAL INPUTS:
75;
76;   DIMS - An array of dimensions that are to be "collapsed", where
77;          the the first dimension starts with 1 (ie, same convention
78;          as IDL function TOTAL).  Whereas TOTAL only allows one
79;          dimension to be added, you can specify multiple dimensions
80;          to CMAPPLY.  Order does not matter, since all operations
81;          are associative and transitive.  NOTE: the dimensions refer
82;          to the *input* array, not the output array.  IDL allows a
83;          maximum of 8 dimensions.
84;          DEFAULT: 1 (ie, first dimension)
85;
86; KEYWORDS:
87;
88;   DOUBLE - Set this if you wish the internal computations to be done
89;            in double precision if necessary.  If ARRAY is double
90;            precision (real or complex) then DOUBLE=1 is implied.
91;            DEFAULT: not set
92;
93;   TYPE - Set this to the IDL code of the desired output type (refer
94;          to documentation of SIZE()).  Internal results will be
95;          rounded to the nearest integer if the output type is an
96;          integer type.
97;          DEFAULT: same is input type
98;
99;   FUNCTARGS - If OP is 'USER:...', then the contents of this keyword
100;               are passed to the user function using the _EXTRA
101;               mechanism.  This way you can pass additional data to
102;               your user-supplied function, via keywords, without
103;               using common blocks.
104;               DEFAULT: undefined (i.e., no keywords passed by _EXTRA)
105;
106; RETURN VALUE:
107;
108;   An array of the required TYPE, whose elements are the result of
109;   the requested operation.  Depending on the operation and number of
110;   elements in the input array, the result may be vulnerable to
111;   overflow or underflow.
112;
113; EXAMPLES:
114;   Shows how CMAPPLY can be used to total the second dimension of the
115;   array called IN.  This is equivalent to OUT = TOTAL(IN, 2)
116;
117;   IDL> IN  = INDGEN(5,5)
118;   IDL> OUT = CMAPPLY('+', IN, [2])
119;   IDL> HELP, OUT
120;   OUT             INT       = Array[5]
121;
122;   Second example.  Input is assumed to be an 5x100 array of 1's and
123;   0's indicating the status of 5 detectors at 100 points in time.
124;   The desired output is an array of 100 values, indicating whether
125;   all 5 detectors are on (=1) at one time.  Use the logical AND
126;   operation.
127;
128;   IDL> IN = detector_status             ; 5x100 array
129;   IDL> OUT = CMAPPLY('AND', IN, [1])    ; collapses 1st dimension
130;   IDL> HELP, OUT
131;   OUT             BYTE      = Array[100]
132;
133;   (note that MIN could also have been used in this particular case,
134;   although there would have been more loop iterations).
135;
136;   Third example.  Shows sum over first and third dimensions in an
137;   array with dimensions 4x4x4:
138;
139;   IDL> IN = INDGEN(4,4,4)
140;   IDL> OUT = CMAPPLY('+', IN, [1,3])
141;   IDL> PRINT, OUT
142;        408     472     536     600
143;
144;   Fourth example. A user-function (MEDIAN) is used:
145;
146;   IDL> IN = RANDOMN(SEED,10,10,5)
147;   IDL> OUT = CMAPPLY('USER:MEDIAN', IN, 3)
148;   IDL> HELP, OUT
149;   OUT             FLOAT     = Array[10, 10]
150;
151;   (OUT(i,j) is the median value of IN(i,j,*))
152;
153; MODIFICATION HISTORY:
154;   Mar 1998, Written, CM
155;   Changed usage message to not bomb, 24 Mar 2000, CM
156;   Signficant rewrite for *, MIN and MAX (inspired by Todd Clements
157;     <Todd_Clements@alumni.hmc.edu>); FOR loop indices are now type
158;     LONG; copying terms are liberalized, CM, 22, Aug 2000
159;   More efficient MAX/MIN (inspired by Alex Schuster), CM, 25 Jan
160;     2002
161;   Make new MAX/MIN actually work with 3d arrays, CM, 08 Feb 2002
162;   Add user-defined functions, ON_ERROR, CM, 09 Feb 2002
163;   Correct bug in MAX/MIN initialization of RESULT, CM, 05 Dec 2002
164;
165;  $Id$
166;
167;-
168; Copyright (C) 1998, 2000, 2002, Craig Markwardt
169; This software is provided as is without any warranty whatsoever.
170; Permission to use, copy, modify, and distribute modified or
171; unmodified copies is granted, provided this copyright and disclaimer
172; are included unchanged.
173;-
174
175;; Utility function, adapted from CMPRODUCT
176function cmapply_product, x
177  sz = size(x)
178  n = sz(1)
179
180  while n GT 1 do begin
181      if (n mod 2) EQ 1 then x(0,*) = x(0,*) * x(n-1,*)
182      n2 = floor(n/2)
183      x = x(0:n2-1,*) * x(n2:*,*)
184      n = n2
185  endwhile
186  return, reform(x(0,*), /overwrite)
187end
188
189;; Utility function, used to collect collaped dimensions
190pro cmapply_redim, newarr, dimapply, dimkeep, nkeep, totcol, totkeep
191  sz = size(newarr)
192  ;; First task: rearrange dimensions so that the dimensions
193  ;; that are "kept" (ie, uncollapsed) are at the back
194  dimkeep = where(histogram(dimapply,min=1,max=sz(0)) ne 1, nkeep)
195  if nkeep EQ 0 then return
196
197  newarr = transpose(temporary(newarr), [dimapply-1, dimkeep])
198  ;; totcol is the total number of collapsed elements
199  totcol = sz(dimapply(0))
200  for i = 1, n_elements(dimapply)-1 do totcol = totcol * sz(dimapply(i))
201  totkeep = sz(dimkeep(0)+1)
202  for i = 1, n_elements(dimkeep)-1 do totkeep = totkeep * sz(dimkeep(i)+1)
203
204  ;; this new array has two dimensions:
205  ;;   * the first, all elements that will be collapsed
206  ;;   * the second, all dimensions that will be preserved
207  ;; (the ordering is so that all elements to be collapsed are
208  ;;  adjacent in memory)
209  newarr = reform(newarr, [totcol, totkeep], /overwrite)
210end
211
212;; Main function
213function cmapply, op, array, dimapply, double=dbl, type=type, $
214                  functargs=functargs, nocatch=nocatch
215
216  if n_params() LT 2 then begin
217      message, "USAGE: XX = CMAPPLY('OP',ARRAY,2)", /info
218      message, '       where OP is +, *, AND, OR, MIN, MAX', /info
219      return, -1L
220  endif
221  if NOT keyword_set(nocatch) then $
222    on_error, 2 $
223  else $
224    on_error, 0
225
226  ;; Parameter checking
227  ;; 1) the dimensions of the array
228  sz = size(array)
229  if sz(0) EQ 0 then $
230    message, 'ERROR: ARRAY must be an array!'
231
232  ;; 2) The type of the array
233  if sz(sz(0)+1) EQ 0 OR sz(sz(0)+1) EQ 7 OR sz(sz(0)+1) EQ 8 then $
234    message, 'ERROR: Cannot apply to UNDEFINED, STRING, or STRUCTURE'
235  if n_elements(type) EQ 0 then type = sz(sz(0)+1)
236
237  ;; 3) The type of the operation
238  szop = size(op)
239  if szop(szop(0)+1) NE 7 then $
240    message, 'ERROR: operation OP was not a string'
241
242  ;; 4) The dimensions to apply (default is to apply to first dim)
243  if n_params() EQ 2 then dimapply = 1
244  dimapply = [ dimapply ]
245  dimapply = dimapply(sort(dimapply))   ; Sort in ascending order
246  napply = n_elements(dimapply)
247
248  ;; 5) Use double precision if requested or if needed
249  if n_elements(dbl) EQ 0 then begin
250      dbl=0
251      if type EQ 5 OR type EQ 9 then dbl=1
252  endif
253
254  newop = strupcase(op)
255  newarr = array
256  newarr = reform(newarr, sz(1:sz(0)), /overwrite)
257  case 1 of
258
259      ;; *** Addition
260      (newop EQ '+'): begin
261          for i = 0L, napply-1 do begin
262              newarr = total(temporary(newarr), dimapply(i)-i, double=dbl)
263          endfor
264      end
265
266      ;; *** Multiplication
267      (newop EQ '*'): begin ;; Multiplication (by summation of logarithms)
268          cmapply_redim, newarr, dimapply, dimkeep, nkeep, totcol, totkeep
269          if nkeep EQ 0 then begin
270              newarr = reform(newarr, n_elements(newarr), 1, /overwrite)
271              return, (cmapply_product(newarr))(0)
272          endif
273
274          result = cmapply_product(newarr)
275          result = reform(result, sz(dimkeep+1), /overwrite)
276          return, result
277      end
278
279      ;; *** LOGICAL AND or OR
280      ((newop EQ 'AND') OR (newop EQ 'OR')): begin
281          newarr = temporary(newarr) NE 0
282          totelt = 1L
283          for i = 0L, napply-1 do begin
284              newarr = total(temporary(newarr), dimapply(i)-i)
285              totelt = totelt * sz(dimapply(i))
286          endfor
287          if newop EQ 'AND' then return, (round(newarr) EQ totelt)
288          if newop EQ 'OR'  then return, (round(newarr) NE 0)
289      end
290
291      ;; Operations requiring a little more attention over how to
292      ;; iterate
293      ((newop EQ 'MAX') OR (newop EQ 'MIN')): begin
294          cmapply_redim, newarr, dimapply, dimkeep, nkeep, totcol, totkeep
295          if nkeep EQ 0 then begin
296              if newop EQ 'MAX' then return, max(newarr)
297              if newop EQ 'MIN' then return, min(newarr)
298          endif
299         
300          ;; Next task: create result array
301          result = make_array(totkeep, type=type)
302         
303          ;; Now either iterate over the number of output elements, or
304          ;; the number of collapsed elements, whichever is smaller.
305          if totcol LT totkeep then begin
306              ;; Iterate over the number of collapsed elements
307              result(0) = reform(newarr(0,*),totkeep,/overwrite)
308              case newop of
309                  'MAX': for i = 1L, totcol-1 do $
310                    result(0) = result > newarr(i,*)
311                  'MIN': for i = 1L, totcol-1 do $
312                    result(0) = result < newarr(i,*)
313              endcase
314          endif else begin
315              ;; Iterate over the number of output elements
316              case newop of
317                  'MAX': for i = 0L, totkeep-1 do result(i) = max(newarr(*,i))
318                  'MIN': for i = 0L, totkeep-1 do result(i) = min(newarr(*,i))
319              endcase
320          endelse
321
322          result = reform(result, sz(dimkeep+1), /overwrite)
323          return, result
324      end
325
326      ;; User function
327      (strmid(newop,0,4) EQ 'USER'): begin
328          functname = strmid(newop,5)
329          if functname EQ '' then $
330            message, 'ERROR: '+newop+' is not a valid operation'
331
332          cmapply_redim, newarr, dimapply, dimkeep, nkeep, totcol, totkeep
333          if nkeep EQ 0 then begin
334              if n_elements(functargs) GT 0 then $
335                return, call_function(functname, newarr, _EXTRA=functargs)
336              return, call_function(functname, newarr)
337          endif
338         
339          ;; Next task: create result array
340          result = make_array(totkeep, type=type)
341         
342          ;; Iterate over the number of output elements
343          if n_elements(functargs) GT 0 then begin
344              for i = 0L, totkeep-1 do $
345                result(i) = call_function(functname, newarr(*,i), _EXTRA=functargs)
346          endif else begin
347              for i = 0L, totkeep-1 do $
348                result(i) = call_function(functname, newarr(*,i))
349          endelse
350
351          result = reform(result, sz(dimkeep+1), /overwrite)
352          return, result
353      end
354
355             
356  endcase
357
358  newsz = size(newarr)
359  if type EQ newsz(newsz(0)+1) then return, newarr
360
361  ;; Cast the result into the desired type, if necessary
362  castfns = ['UNDEF', 'BYTE', 'FIX', 'LONG', 'FLOAT', $
363             'DOUBLE', 'COMPLEX', 'UNDEF', 'UNDEF', 'DCOMPLEX' ]
364  if type GE 1 AND type LE 3 then $
365    return, call_function(castfns(type), round(newarr)) $
366  else $
367    return, call_function(castfns(type), newarr)
368end
369 
Note: See TracBrowser for help on using the repository browser.