;+
; NAME: 
;	SB4_Read
;
; PURPOSE:
;	 Reads sb4 files.  
; CATEGORY:
;	Input/Output
; CALLING SEQUENCE:
;	SB4_Read,file,image,xAxis,yAxis,vAxis,wAxis, desc
; INPUT:
; 	file: the complete name of the file to read.
; OUTPUTS:
;	image: A four dimensional array with
;		dimension <nx,ny,nv,nw> where nx,ny,nv,nw
;		are the number of elements in the x,y,v,w
;		directions, respectively.
; 	xAxis,yAxis,vAxis,wAxis: data array and axis definition
;	 	arrays, xAxis(nx,next), yAxis(ny,next),
;		v(nv,next), w(nw,next), where nx,ny,nv,nw, 
;		and next are read from the second record
;             		of the file. xAxis,yAxis,vAxis,and wAxis
;		are real arrays.
;	desc: Array of 20 character strings containing the
;		following descriptors: title, zlabel, xlabel,
;		ylabel, vlabel, wlabel, timestart,  timeend,
;		 timecreate, instrument, and spares.
; KEYWORDS:
; 	TITLE: The title of the image
;	XLAB, YLAB, ZLAB: The x axis descriptions
;	STARTTIME, ENDTIME: The start and end time 
;		(h:m:s.cc), string
; 	TIMECREATE, INSTRUMENT, SPARES: Additional
;		 information from the header.
;	/VMS: If specified, the file is considered as a VMS file and
;		read in the VMS format. After the read,
;		the file is automatically rewritten in Unix
;		 Format.
;	/SECS: If specified, the x-axis values are in  'Seconds after
;		Midnight': the STARTTIME information is
;		added to the x-axis values.
;
; MODIFICATION HISTORY:  
;	Created: NASA/Goddard Space Flight Center, 
;		SMM Data Analysis Center, 4/89
; 	Modified: 4/91, A. Csillaghy, for WAVE 3
;		Institute for Astronomy, ETH Zurich
;	Use of Conv_Vax_... in Jan 93, A.Cs.
;-



PRO SB4_Read, file, image, xAxis, yAxis, vAxis, wAxis, descriptor, $
	TITLE = title, XLAB = xLab, YLAB = yLab, $
	ZLAB = zLab,  VLAB = vLab, WLAB = wLab, $
	STARTTIME = startTime, ENDTIME = endTime, $
	TIMECREATE = timeCreate, VMS = vms, SECS = secs, $
	INSTRUMENT = instrument, SPARES = spares


  On_IOError, Error 

  vmsFile = Keyword_Set(VMS)
  
  nbParams = N_Params()
  IF nbParams LT 1 THEN BEGIN
    Message,'Usage: SB4_Read, file [, image, xAxis, yAxis ... ]', $
	/INFO, /CONT
    RETURN
  ENDIF

  fileToR = StrTrim(file,2)
  IF vmsFile THEN Conv_Vax_Block, file

  OpenR,unit,fileToR,/GET_LUN, /F77_UNFORMATTED
  
  Print,' Reading SB4 file header ... '
  header = String(Replicate(32B,20*25))
  ReadU,unit,header

  Print, 'Reading constants ... '
  nx = 0L & ny = 0L & nv = 0L & nw = 0L & next = 0L
  ReadU, unit, nx, ny, nv, nw, next

  image = BytArr( nx, ny, nv, nw, /NOZERO )
  xAxis = FltArr( nx, next, /NOZERO )
  yAxis = FltArr( ny, next, /NOZERO )
  vAxis = FltArr( nv, next, /NOZERO )
  wAxis = FltArr( nw, next, /NOZERO )

  Print, 'Reading image and axes ... '
  ReadU, unit, image, xAxis, yAxis, vAxis, wAxis

  Free_LUn,unit

  IF vmsFile THEN BEGIN
    xAxis = Conv_Vax_Unix( xAxis )
    yAxis = Conv_Vax_Unix( yAxis )
    vAxis = Conv_Vax_Unix( vAxis )
    wAxis = Conv_Vax_Unix( wAxis )
  ENDIF

  title = StrTrim( StrMid( header, 0, 25 ), 2 )
  zLab = StrTrim( StrMid( header, 25, 25 ), 2 )
  xLab = StrTrim( StrMid( header, 50, 25 ), 2 )
  yLab = StrTrim( StrMid( header, 75, 25 ), 2 )
  vLab = StrTrim( StrMid( header, 100, 25 ), 2 )
  wLab = StrTrim( StrMid( header, 125, 25 ), 2 )
  startTime = StrTrim( StrMid( header, 150, 25 ), 2 )
  endTime = StrTrim( StrMid( header, 175, 25 ), 2 )
  timeCreate = StrTrim( StrMid( header, 200, 25 ), 2 )
  instrument = StrTrim( StrMid( header, 225, 25 ), 2 )
  spares = StrTrim( StrMid( header, 250, 25 ), 2 )

  IF Keyword_Set(SECS) THEN BEGIN
    hour = Double( StrMid( startTime, 9, 2 ))
    minutes = Double( StrMid( startTime, 11, 2 ))
    seconds = Double( StrMid( startTime, 14, 6 ))
    offset = 3600*hour + 60*minutes + seconds
  ENDIF ELSE offset = 0
  xAxis =xAxis + offset

  Print,' Header: '
  Print,'         Title: ', title
  Print,'         Start/end time: ',startTime,'  ->  ',endTime
  Print,'         Image title: ' , zLab
  Print,'         X axis title: ' , xLab
  Print,'         Y axis title: ' , yLab
  Print,'         V axis title: ' , vLab
  Print,'         W axis title: ' , wLab
  Print,'         Instrument: ' , instrument
  Print,'         Creation time: ' , wLab

  Print, ''
  Print,' Image array dimensions: ', nx, ny, nv, nw
  Print,' X axis dimensions: ', nx, next
  Print,' Y axis dimensions: ', ny, next
  Print,' V axis dimensions: ',nv, next
  Print,' W axis dimensions: ', nw, next
  Print,' '

  Print,' Image array range: [',MIN(image),'..',MAX(image),']'
  Print,' X axis range: [',MIN(xAxis),'..',MAX(xAxis),']'
  Print,' Y axis range: [',MIN(yAxis),'..',MAX(yAxis),']'
  Print,' V axis range: [',MIN(vAxis),'..',MAX(vAxis),']'
  Print,' W axis range: [',MIN(wAxis),'..',MAX(wAxis),']'

  IF vmsFile THEN BEGIN
    Print, 'Rewriting the file in Unix format ... '
    SB4_Write, image, xAxis, yAxis, vAxis, wAxis, $
	TITLE = title, XLAB = xLab, YLAB = yLab, $
	ZLAB = zLab,  VLAB = vLab, WLAB = wLab, $
	STARTTIME = startTime, ENDTIME = endTime, $
	TIMECREATE = timeCreate, FILENAME = fileToR, $
	INSTRUMENT = instrument, SPARES = spares
  ENDIF


  RETURN

Error:
  Print,'I/O error encountered:'
  Print, !err_string
  IF N_Elements( unit ) NE 0 THEN Free_LUn, unit

END

