FUNCTION vox_read, InFile   , $
        count       = count     , $
        minf        = minf      , $
        maxf        = maxf      , $
        topb        = topb      , $
        version     = version   , $
        copyright   = copyright , $
        vol_size    = vol_size  , $
        vox_size    = vox_size  , $
        vol_name    = vol_name  , $
        gen_comments= gen_comments, $
        vol_comments= vol_comments, $
        errormessage= errormessage, $
        silent      = silent
    @compile_opt.pro                ; On error, return to caller
InitVar, silent, 0

IF IsType(InFile,/undefined) THEN   $
    message, 'usage: volume = READ_VOX(file)'

early_return_value = -1

fi = (file_search(InFile))[0]

; If file found then unzip if necessary.
; If file not found try adding .gz extension.

gzipped = gunzip_file(InFile+(['','.gz'])[fi EQ ''], rawfile, check=fi EQ '', isgz=isgz)

CASE gzipped OF
0: IF isgz THEN fi = ''
1: fi = rawfile
ENDCASE

IF fi EQ '' THEN BEGIN
    fi = InFile
    errormessage = 'file not found or unzip error'
    goto, EARLY_RETURN
ENDIF

on_ioerror, READ_ERROR

openr, /get_lun, iu, fi
hdr = bytarr((fstat(iu)).size)
readu, iu, hdr
free_lun, iu

on_ioerror, NULL

IF gzipped THEN tmp = do_file(/delete, rawfile)

NL = string('0a'XB)
FF = string('0c'XB)

terminator = '##'+FF+NL

n = strpos(string(hdr),terminator)
IF n EQ -1 THEN BEGIN
    errormessage = 'no header found; not a vox file?'
    goto, EARLY_RETURN
ENDIF

gen_hdr = strtok(string(hdr[0:n-1]),NL,/extract)
hdr = hdr[n+strlen(terminator):*]

IF silent LE 0 THEN BEGIN
    message, /info, 'general header:'
    FOR i=0,n_elements(gen_hdr)-1 DO print, '   '+gen_hdr[i]
    print
ENDIF

; Analyze general header information

version = gen_hdr[0]

n = (where( strpos(gen_hdr,'VolumeCount') EQ 0 ))[0]
IF n EQ -1 THEN BEGIN
    count = 0
    errormessage = 'no volumes'
    goto, EARLY_RETURN
ENDIF

count = round( flt_string( gen_hdr[n] ) )

n = (where( strpos(gen_hdr,'Copyright') EQ 0 ))[0]
IF n NE -1 THEN copyright = strtrim(strmid(gen_hdr[n],strlen('Copyright')),2)

n = where( strpos(gen_hdr,'//') EQ 0 )
IF n[0] NE -1 THEN gen_comments = strmid(gen_hdr[n],strlen('//'))

; Analyze volume headers

n = strpos(string(hdr),terminator)
IF n EQ -1 THEN BEGIN
    errormessage = 'no volume header found'
    goto, EARLY_RETURN
ENDIF

vol_hdr = strtok(string(hdr[0:n-1]),NL,/extract)
hdr = hdr[n+strlen(terminator):*]       ; Data area

IF silent LE 0 THEN BEGIN
    message, /info, 'volume header:'
    FOR i=1,n_elements(vol_hdr)-1 DO print, '   '+vol_hdr[i]
    print
ENDIF

n = (where( strpos(vol_hdr,'VolumeSize') EQ 0 ))[0]
IF n EQ -1 THEN BEGIN
    errormessage = 'no volume size specified'
    goto, EARLY_RETURN
ENDIF

vol_size = round( flt_string( vol_hdr[n] ) )

n = (where( strpos(vol_hdr,'VoxelSize') EQ 0 ))[0]
IF n EQ -1 THEN BEGIN
    errormessage = 'no voxel size specified'
    goto, EARLY_RETURN
ENDIF

vox_size = round( flt_string( vol_hdr[n] ) )

CASE vox_size OF
   8: volume = reform(hdr, vol_size)
ELSE: volume = -1
ENDCASE

n = (where( strpos(vol_hdr,'Field 0') EQ 0 ))[0]
IF n NE -1 THEN BEGIN
    vol_name = vol_hdr[n]
    vol_name = strmid (vol_name, strpos(vol_name,'Name ')+strlen('Name '))
    vol_name = strmid (vol_name, 0, strpos(vol_name,')'))
    vol_name = strtrim(vol_name,2)
ENDIF

n = where( strpos(vol_hdr,'//') EQ 0 )
IF n[0] NE -1 THEN vol_comments = strmid(vol_hdr[n],strlen('//'))

IF silent LE 0 THEN whatis, volume

errormessage = ''

RETURN, volume

READ_ERROR:

on_ioerror, NULL
errormessage = strmessage( !error_state.code )

EARLY_RETURN:

IF IsType(iu,/defined) THEN free_lun, iu

IF gzipped THEN tmp = do_file(/delete, rawfile)

errormessage = errormessage+', '+fi
IF silent LE 1 THEN message, /info, errormessage

RETURN, early_return_value  &  END