;+
; NAME:
;   QUERY_IPLAB
;
; PURPOSE:
;   The purpose of this function is to check the format of a IPLAB image file before trying to 
;		read it.  The header of the file can optionally be returned.  If the file is a IPLab image format
;   3.1 or greater and in data format then a 1 is returned, otherwise a 0 is returned.
;
; CALLING SEQUENCE:
;  result = QUERY_IPLAB([File])
;
;  ARGUMENTS:
;  File    A scalar string containing the name of the file to query. If not provided, a 
;      dialog_pickfile prompt permits the user to select one automatically.
;  
;	KEYWORDS:
;  HDR   Set this keyword equal to a named variable to return the header structure
; 		which contains information about the file.
;  GROUP  Set this keyword equal to the group leader for the widget dialog.  This
;			value is only neccessary if the routine is used in a widget application and the
;			file name is not specified.
;
; EXAMPLE:
;  To interactively locate the file to query and return the file header...
;   GoodFlag = QUERY_IPLAB(HDR = HDR)
;
; COMMENTS:
;  Here's an example of the HDR structure
;  IDL> help, hdr,/str
;** Structure <d183d00>, 13 tags, length=2128, refs=1:
;   VERSION         STRING    '3.1a'
;   IPLABFORMAT     BYTE         0
;   DATATYPE        BYTE         0
;   WIDTH           LONG               640
;   HEIGHT          LONG               480
;   RESERVED1       BYTE         0
;   OVERLAYINFILE   BYTE         0
;   RESERVED2       LONG          16842753
;   NFRAMES         INT              5
;   RESERVED3       BYTE      Array[25]
;   NREGMARKS       BYTE         0
;   RESERVED4       BYTE      Array[24]
;   CLUT            BYTE      Array[2048]
;
; REFERENCE:
;  For more information on the IPLAB format see...
;  'IPLAB Spectrum: Scientific Imaging Software for the Macintosh, User's Guide'
;  Signal Analytics Corporation,  1996.
;
; MODIFICATION HISTORY:
;   Written by Ben Tupper, 27JULY2000
;   Bigelow Laboratory For Ocean Science
;   tupper@bigelow.org
;
; 2AUG2000 Improved file type checking by examining the Version input by byte elements
;-

FUNCTION  query_iplab, File, HDR = HDR, Group = Group

On_Error, 2

   ;if there is an IO error, make sure to return, 0
On_IOError, Trouble

	;if the file is undefined, then prompt with a dialog
If N_elements(File) EQ 0 Then Begin
   File = Dialog_PickFile(Group = Group)
   If File[0] EQ ''  Then Return,0
EndIf

   ;open the file
Openr,u,File[0],/get_lun, /Binary

  ;check if this is the proper format 
Version  = '    '
Format = 0B

ReadU,U, Version

VersionTest = Byte(Version)
If N_elements(VersionTest) LT 3 Then Begin
  If (FSTAT(U)).Open Then Free_LUN, U
	Return, 0
EndIf

If Float(VersionTest[0]) LT 3 Then Begin
	If (FSTAT(U)).Open Then Free_LUN, U
	Return,0
EndIf


ReadU,U, Format
If Format[0] NE 0B Then Begin
	If (FSTAT(U)).Open Then Free_LUN, U
  Return, 0
EndIf

  ;back up the pointer
Point_LUN,U, 0   

  ;prep the header structure
Hdr = {	Version:'    ',$
	IPLabFormat:0B,$
	DataType:0B,$
	Width:1L,$
	Height:1L,$
	Reserved1:0B,$
	OverlayInFile:0B,$
	Reserved2: BytArr(4),$
	nFrames :1,$
	Reserved3: Bytarr(25),$
	nRegMarks:1B,$
	Reserved4: Bytarr(24),$
	CLUT:Bytarr(2048)}

;read the header
ReadU,u,Hdr
FREE_LUN, U

Return, 1

TROUBLE:   
 OK = Error_Message(/TraceBack)
If (FSTAT(U)).Open Then Free_LUN, U
 Return, 0


END


