source: trunk/SRC/ToBeReviewed/LECTURE/inverse_binary.pro @ 231

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

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1;+
2;
3; @file_comments
4; Inverse function of the <pro>binary</pro> function => given a
5; input array of 0/1, return its corresponding byte/integer/long
6; representation
7;
8; @categories
9;
10; @param BINNUMB {in}{required}
11; Must be a binary type array containing only 0 and 1.
12; According to <pro>binary</pro> outputs, binnum array must have the
13; following dimensions values: (8, t, d1, d2...)
14; t gives the output type: t = 1 -> byte
15;                             t = 2 -> integer
16;                             t = 4 -> long
17;
18;  (d1, d2...) are the output dimensions
19;
20;
21; @returns
22; A byte/integer/long array with (d1, d2...) dimensions
23;
24; @restrictions
25; The binary number can represent only byte/integer/long
26;
27; @examples
28;
29;     IDL> a=indgen(5)
30;     IDL> b=binary(a)
31;     IDL> help, b
32;     B               BYTE      = Array[8, 2, 5]
33;     IDL> print, b
34;        0   0   0   0   0   0   0   0
35;        0   0   0   0   0   0   0   0
36;
37;        0   0   0   0   0   0   0   0
38;        0   0   0   0   0   0   0   1
39;
40;        0   0   0   0   0   0   0   0
41;        0   0   0   0   0   0   1   0
42;
43;        0   0   0   0   0   0   0   0
44;        0   0   0   0   0   0   1   1
45;
46;        0   0   0   0   0   0   0   0
47;        0   0   0   0   0   1   0   0
48;     IDL> help, inverse_binary(b)
49;     <Expression>    INT       = Array[5]
50;     IDL> print, inverse_binary(b)
51;            0       1       2       3       4
52;
53; @history
54;      Sebastien Masson (smasson\@jamstec.go.jp)
55;      July 2004
56;
57; @version
58; $Id$
59;
60;-
61;
62FUNCTION inverse_binary, binnumb
63;
64  compile_opt idl2, strictarrsubs
65;
66  s = size(binnumb, /dimensions)
67  IF n_elements(s) EQ 1 THEN numbofbit = 8 ELSE numbofbit = 8*s[1]
68  nvalues = n_elements(binnumb)/numbofbit
69  bn = reform(long(binnumb), numbofbit, nvalues)
70;
71  CASE numbofbit OF
72    8:res = byte(total(temporary(bn)*2b^reverse(indgen(numbofbit)#replicate(1b, nvalues), 1), 1))
73    16:res = fix(total(temporary(bn)*2^reverse(indgen(numbofbit)#replicate(1, nvalues), 1), 1, /double))
74    32:res = long(total(temporary(bn)*2L^reverse(indgen(numbofbit)#replicate(1L, nvalues), 1), 1, /double))
75  ENDCASE
76;
77  CASE n_elements(s) OF
78    1:res = res[0]
79    2:res = res[0]
80    3:
81    ELSE:res = reform(res, s[2:n_elements(s)-1], /over)
82  ENDCASE
83;
84  return, res
85end
Note: See TracBrowser for help on using the repository browser.