FUNCTION AxisTransf, axisP, next

  sizeAxis = Size( axisP )
  axis = FltArr( sizeAxis(1), next )
  axis(0) = axisP

  RETURN, axis

END ; axis transformation

FUNCTION ImTransf, imageP, nv, nw , BYTSCL = bytScl

  imSize = Size( imageP )

  IF Keyword_Set( BYTSCL ) THEN BEGIN
    minimum = MIN( imageP )
    maximum = MAX( imageP )
  ENDIF ELSE BEGIN
    minimum = 0
    maximum = 255
  ENDELSE    
  
  IF imSize(0) NE 4 THEN BEGIN
    image  = BytArr( imSize(1), imSize(2), nv, nw )
    image(*,*,0,0) = BytScl( imageP, $
	MIN = minimum, MAX = maximum ) 
  ENDIF ELSE image(0) = BytScl( imageP, $
	MIN = minimum, MAX = maximum )

  RETURN, image

END ; image transformation


PRO SB4_Write, image, xAxisP, yAxis, vAxis, wAxis, $
	TITLE = title, XLAB = xLab, YLAB = yLab, $
	ZLAB = zLab,  VLAB = vLab, WLAB = wLab, $
	STARTTIME = startTime, ENDTIME = endTime, $
	TIMECREATE = timeCreate, FILENAME = fileName, $
	INSTRUMENT = instrument, SPARES = spares, $
	INTERACTIVE = interactive, BYTSCL = bytScl

 On_IOError, ErrLabel

  nbParams = N_Params()
  
  IF NOT Keyword_Set( BYTSCL ) THEN bytScl = 0
  IF NOT Keyword_Set( TITLE ) THEN title = !p.title
  IF NOT Keyword_Set( XLAB ) THEN xLab = !x.title
  IF NOT Keyword_Set( YLAB ) THEN yLab = !y.title
  IF NOT Keyword_Set( ZLAB ) THEN zLab = !z.title
  IF NOT Keyword_Set( VLAB ) THEN vLab = ''
  IF NOT Keyword_Set( WLAB ) THEN wLab = ''
  IF NOT Keyword_Set( STARTTIME ) THEN startTime = ''
  IF NOT Keyword_Set( ENDTIME ) THEN endTime = ''
  IF NOT Keyword_Set( TIMECREATE ) THEN timeCreate = ''
  IF NOT Keyword_Set( INSTRUMENT ) THEN instrument = '' 
  IF NOT Keyword_Set( SPARES ) THEN spares = ''
  IF NOT Keyword_Set( FILENAME ) THEN fileName = 'enrad.sb4'
  IF Keyword_Set(INTERACTIVE) THEN BEGIN
    Print, 'SB4 Write'
    Read,'Please enter the complete filename: ', fileName
  ENDIF

  IF fileName EQ '' THEN RETURN

  sizIm = Size( image )
  CASE 1 OF
    ((sizIm(0) LT  2) AND (sizIm(0) GT 4)): BEGIN
      Print,' Image must be two- three- or four dimensionnal ... '
      RETURN
    END
    (sizIm(0) EQ 4): BEGIN
      nx = sizIm(1) & ny = sizIm(2) & nv = sizIm(3) & nw = sizIm(4)
    END
    (sizIm(0) EQ 3): BEGIN
      nx = sizIm(1) & ny = sizIm(2) & nv = sizIm(3) & nw = 1L
    END
    (sizIm(0) EQ 2): BEGIN
      nx = sizIm(1) & ny = sizIm(2) & nv = 1L & nw = 1L
    END
  ENDCASE

  CASE nbParams OF
    1: BEGIN 
          xAxisP = FIndGen(nx) & yAxis = FIndGen(ny)
          vAxis = FIndGen(nv) & wAxis = FIndGen(nw)
        END
    2: BEGIN 
          yAxis = FIndGen(ny)
          vAxis = FIndGen(nv) & wAxis = FIndGen(nw)
        END
    3: BEGIN 
          vAxis = FIndGen(nv) & wAxis = FIndGen(nw)
        END
    4: wAxis = FIndGen(nw)
    ELSE: 
  ENDCASE
  
  xAxis = xAxisP - xAxisP(0)
  sizeAxis = SIZE( xAxis )
  IF sizeAxis(0) EQ 1 THEN next = 1L ELSE next = sizeAxis(2)

  descriptor = String( Replicate( 32B, 500 ) )

  StrPut, descriptor, title
  StrPut, descriptor, zLab, 25
  StrPut, descriptor, xLab, 50
  StrPut, descriptor, yLab, 75
  StrPut, descriptor, vLab, 100
  StrPut, descriptor, wLab, 125
  StrPut, descriptor, startTime, 150
  StrPut, descriptor, endTime, 175
  StrPut, descriptor, timeCreate, 200
  StrPut, descriptor, instrument, 225
  nbEls = N_Elements( spares )
  FOR i = 0, nbEls DO  StrPut, descriptor, spares, 250*i
  
  OpenW, unit, fileName, /GET_LUN, /F77_UNFORMATTED

  Print, 'Writing SB4 File ' + fileName
  WriteU, unit, descriptor
  WriteU, unit, nx, ny, nv, nw, next
  WriteU, unit, ImTransf(image, nv, nw, BYTSCL = bytScl), $
	AxisTransf(xAxis, next), $
	AxisTransf(yAxis, next), AxisTransf(vAxis, next), $
	AxisTransf(wAxis, next)

  Free_LUn, unit

  RETURN

ErrLabel:
  Print,'I/O error encountered:'
  Print, !err_string
  Free_LUn, unit

END ; SB4 write
