[44] | 1 | ;+ |
---|
| 2 | ; NAME: inverse_binary |
---|
| 3 | ; |
---|
| 4 | ; PURPOSE: 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 | ; CATEGORY: |
---|
| 9 | ; |
---|
| 10 | ; CALLING SEQUENCE: res = inverse_binary(binnum) |
---|
| 11 | ; |
---|
| 12 | ; INPUTS: |
---|
| 13 | ; binnum must be a binary type array containing only 0 and 1. |
---|
| 14 | ; According to binary.pro outputs, binnum array must have the |
---|
| 15 | ; following dimensions values: (8, t, d1, d2...) |
---|
| 16 | ; t gives the output type: t = 1 -> byte |
---|
| 17 | ; t = 2 -> integer |
---|
| 18 | ; t = 4 -> long |
---|
| 19 | ; |
---|
| 20 | ; (d1, d2...) are the output dimensions |
---|
| 21 | ; |
---|
| 22 | ; KEYWORD PARAMETERS: no |
---|
| 23 | ; |
---|
| 24 | ; OUTPUTS: a byte/integer/long array with (d1, d2...) dimensions |
---|
| 25 | ; |
---|
| 26 | ; COMMON BLOCKS: no |
---|
| 27 | ; |
---|
| 28 | ; RESTRICTIONS:the binary number can represent only byte/integer/long |
---|
| 29 | ; |
---|
| 30 | ; EXAMPLE: |
---|
| 31 | ; |
---|
| 32 | ; IDL> a=indgen(5) |
---|
| 33 | ; IDL> b=binary(a) |
---|
| 34 | ; IDL> help, b |
---|
| 35 | ; B BYTE = Array[8, 2, 5] |
---|
| 36 | ; IDL> print, b |
---|
| 37 | ; 0 0 0 0 0 0 0 0 |
---|
| 38 | ; 0 0 0 0 0 0 0 0 |
---|
| 39 | ; |
---|
| 40 | ; 0 0 0 0 0 0 0 0 |
---|
| 41 | ; 0 0 0 0 0 0 0 1 |
---|
| 42 | ; |
---|
| 43 | ; 0 0 0 0 0 0 0 0 |
---|
| 44 | ; 0 0 0 0 0 0 1 0 |
---|
| 45 | ; |
---|
| 46 | ; 0 0 0 0 0 0 0 0 |
---|
| 47 | ; 0 0 0 0 0 0 1 1 |
---|
| 48 | ; |
---|
| 49 | ; 0 0 0 0 0 0 0 0 |
---|
| 50 | ; 0 0 0 0 0 1 0 0 |
---|
| 51 | ; IDL> help, inverse_binary(b) |
---|
| 52 | ; <Expression> INT = Array[5] |
---|
| 53 | ; IDL> print, inverse_binary(b) |
---|
| 54 | ; 0 1 2 3 4 |
---|
| 55 | ; |
---|
| 56 | ; MODIFICATION HISTORY: |
---|
| 57 | ; Sebastien Masson (smasson@jamstec.go.jp) |
---|
| 58 | ; July 2004 |
---|
| 59 | ;- |
---|
| 60 | ;------------------------------------------------------ |
---|
| 61 | ;------------------------------------------------------ |
---|
| 62 | FUNCTION inverse_binary, binnumb |
---|
| 63 | ; |
---|
[114] | 64 | ; |
---|
| 65 | compile_opt idl2, strictarrsubs |
---|
| 66 | ; |
---|
[44] | 67 | s = size(binnumb, /dimensions) |
---|
| 68 | IF n_elements(s) EQ 1 THEN numbofbit = 8 ELSE numbofbit = 8*s[1] |
---|
| 69 | nvalues = n_elements(binnumb)/numbofbit |
---|
| 70 | bn = reform(long(binnumb), numbofbit, nvalues) |
---|
| 71 | ; |
---|
| 72 | CASE numbofbit OF |
---|
| 73 | 8:res = byte(total(temporary(bn)*2b^reverse(indgen(numbofbit)#replicate(1b, nvalues), 1), 1)) |
---|
| 74 | 16:res = fix(total(temporary(bn)*2^reverse(indgen(numbofbit)#replicate(1, nvalues), 1), 1, /double)) |
---|
| 75 | 32:res = long(total(temporary(bn)*2L^reverse(indgen(numbofbit)#replicate(1L, nvalues), 1), 1, /double)) |
---|
| 76 | ENDCASE |
---|
| 77 | ; |
---|
| 78 | CASE n_elements(s) OF |
---|
| 79 | 1:res = res[0] |
---|
| 80 | 2:res = res[0] |
---|
| 81 | 3: |
---|
| 82 | ELSE:res = reform(res, s[2:n_elements(s)-1], /over) |
---|
| 83 | ENDCASE |
---|
| 84 | ; |
---|
| 85 | return, res |
---|
| 86 | end |
---|