;+
; Project     :	SOHO - CDS
;
; Name        :	EXTVBWLBLOCK
;
; Purpose     :	Extract data from a VBWL compressed block
;
; Category    :	EGSE
;
; Explanation :	
;
; Syntax      :	Result = EXTVBWLBLOCK(BLOCKSIZE,DATA,WORDPOS,BITPOS,GOODDATA)
;
; Examples    :	
;
; Inputs      :	BLOCKSIZE = The size of the VBWL block before compression.
;		DATA	  = Integer*2 array containing the VBWL compressed
;			    data.
;		WORDPOS	  = The position of the start of the VBWL compressed
;			    block in the DATA array.
;		BITPOS	  = The position of the first bit of the block within
;			    the first word.
;
; Opt. Inputs :	None.
;
; Outputs     :	The result of the function is the decompressed block.
;
; Opt. Outputs:	GOODDATA  = An array with 1 for each pixel successfully
;			    recovered, or 0 otherwise.
;
; Keywords    :	None.
;
; Calls       :	EXTBITS, UNMAP
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	If you try to extract past the end of the DATA array, then 0 is
;		returned for the remaining values, and GOODDATA is set to 0 for
;		the relevant pixels.
;
; Prev. Hist. :	Original by M K Carter, RAL
;
; History     :	EGSE version by C D Pike, RAL 7-Oct-93
;
; Contact     :	CDPIKE
;-
;
FUNCTION extvbwlblock, blocksize, data, wordpos, bitpos, gooddata

; local constants

  subschemebits = 3
  databitsbits  = 4
  predictorbits = 16

; local variables

  block = INTARR(blocksize)
  gooddata = INTARR(blocksize)

; get sub-scheme and data bits

  subscheme = extbits ( subschemebits, data, wordpos, bitpos, good)
  if not good then return, block
  databits  = 1+extbits ( databitsbits , data, wordpos, bitpos, good)
  if not good then return, block

  CASE subscheme OF

    0 : BEGIN ; VBWL0
          FOR i = 0, blocksize-1 DO BEGIN
            block(i) = extbits ( databits, data, wordpos, bitpos, good)
	    if not good then return, block
	    gooddata(i) = good
          ENDFOR
        END

    1 : BEGIN ; VBWL1
          predictor = extbits ( predictorbits, data, wordpos, bitpos, good)
	  if not good then return, block
          block(0)  = predictor
	  gooddata(0) = good
          FOR i = 1, blocksize-1 DO BEGIN
            value = extbits ( databits, data, wordpos, bitpos, good)
	    if not good then return, block
            predictor = predictor + unmap(value)
            block(i)  = predictor
	    gooddata(i) = good
          ENDFOR
        END

    2 : BEGIN ; VBWL2
          predictor = extbits ( predictorbits, data, wordpos, bitpos, good)
	  if not good then return, block
          block(0) = predictor
	  gooddata(0) = good
          FOR i = 1, blocksize-1 DO BEGIN
            value = extbits ( databits, data, wordpos, bitpos, good)
	    if not good then return, block
            block(i) = predictor + unmap(value)
	    gooddata(i) = good
          ENDFOR
        END

    3 : BEGIN ; VBWL3
          predictor = extbits ( predictorbits, data, wordpos, bitpos, good)
	  if not good then return, block
          FOR i = 0, blocksize-1 DO BEGIN
            value = extbits ( databits, data, wordpos, bitpos, good)
	    if not good then return, block
            block(i) = predictor + unmap(value)
	    gooddata(i) = good
          ENDFOR
        END

    4 : BEGIN ; VBWL4
          predictor = extbits ( predictorbits, data, wordpos, bitpos, g)
          FOR i = 0, blocksize-1 DO BEGIN
            value = extbits ( databits, data, wordpos, bitpos)      
            block(i) = predictor + value
          ENDFOR
        END

    ELSE : BEGIN
             block(0:blocksize-1) = 0
           END

  ENDCASE

  RETURN, block

END
