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

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

english and nicer header (2a)

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1;+
2;
3; @file_comments
4; Inverse function of the 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 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;------------------------------------------------------
62;------------------------------------------------------
63FUNCTION inverse_binary, binnumb
64;
65;
66  compile_opt idl2, strictarrsubs
67;
68  s = size(binnumb, /dimensions)
69  IF n_elements(s) EQ 1 THEN numbofbit = 8 ELSE numbofbit = 8*s[1]
70  nvalues = n_elements(binnumb)/numbofbit
71  bn = reform(long(binnumb), numbofbit, nvalues)
72;
73  CASE numbofbit OF
74    8:res = byte(total(temporary(bn)*2b^reverse(indgen(numbofbit)#replicate(1b, nvalues), 1), 1))
75    16:res = fix(total(temporary(bn)*2^reverse(indgen(numbofbit)#replicate(1, nvalues), 1), 1, /double))
76    32:res = long(total(temporary(bn)*2L^reverse(indgen(numbofbit)#replicate(1L, nvalues), 1), 1, /double))
77  ENDCASE
78;
79  CASE n_elements(s) OF
80    1:res = res[0]
81    2:res = res[0]
82    3:
83    ELSE:res = reform(res, s[2:n_elements(s)-1], /over)
84  ENDCASE
85;
86  return, res
87end
Note: See TracBrowser for help on using the repository browser.