;+ ; NAME: inverse_binary ; ; PURPOSE: inverse function of the binary.pro function => given a ; input array of 0/1, return its corresponding byte/integer/long ; representation ; ; CATEGORY: ; ; CALLING SEQUENCE: res = inverse_binary(binnum) ; ; INPUTS: ; binnum must be a binary type array containing only 0 and 1. ; According to binary.pro outputs, binnum array must have the ; following dimensions values: (8, t, d1, d2...) ; t gives the output type: t = 1 -> byte ; t = 2 -> integer ; t = 4 -> long ; ; (d1, d2...) are the output dimensions ; ; KEYWORD PARAMETERS: no ; ; OUTPUTS: a byte/integer/long array with (d1, d2...) dimensions ; ; COMMON BLOCKS: no ; ; RESTRICTIONS:the binary number can represent only byte/integer/long ; ; EXAMPLE: ; ; IDL> a=indgen(5) ; IDL> b=binary(a) ; IDL> help, b ; B BYTE = Array[8, 2, 5] ; IDL> print, b ; 0 0 0 0 0 0 0 0 ; 0 0 0 0 0 0 0 0 ; ; 0 0 0 0 0 0 0 0 ; 0 0 0 0 0 0 0 1 ; ; 0 0 0 0 0 0 0 0 ; 0 0 0 0 0 0 1 0 ; ; 0 0 0 0 0 0 0 0 ; 0 0 0 0 0 0 1 1 ; ; 0 0 0 0 0 0 0 0 ; 0 0 0 0 0 1 0 0 ; IDL> help, inverse_binary(b) ; INT = Array[5] ; IDL> print, inverse_binary(b) ; 0 1 2 3 4 ; ; MODIFICATION HISTORY: ; Sebastien Masson (smasson@jamstec.go.jp) ; July 2004 ;- ;------------------------------------------------------ ;------------------------------------------------------ FUNCTION inverse_binary, binnumb ; ; compile_opt idl2, strictarrsubs ; s = size(binnumb, /dimensions) IF n_elements(s) EQ 1 THEN numbofbit = 8 ELSE numbofbit = 8*s[1] nvalues = n_elements(binnumb)/numbofbit bn = reform(long(binnumb), numbofbit, nvalues) ; CASE numbofbit OF 8:res = byte(total(temporary(bn)*2b^reverse(indgen(numbofbit)#replicate(1b, nvalues), 1), 1)) 16:res = fix(total(temporary(bn)*2^reverse(indgen(numbofbit)#replicate(1, nvalues), 1), 1, /double)) 32:res = long(total(temporary(bn)*2L^reverse(indgen(numbofbit)#replicate(1L, nvalues), 1), 1, /double)) ENDCASE ; CASE n_elements(s) OF 1:res = res[0] 2:res = res[0] 3: ELSE:res = reform(res, s[2:n_elements(s)-1], /over) ENDCASE ; return, res end