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