;+
; NAME:
;	SCA_Read
; PURPOSE:
;	Reads SCA files.
; CATEGORY:
;	Input/Output
; CALLING SEQUENCES:
;	SCA_Read, file, image, [ xAxis, yAxis ]
;	SCA_Read, file, image,  nx, ny, x1, y1, dx, dy
; INPUT ARGUMENTS:
;	file: complete name of the file to be read
; OUTPUT ARGUMENTS:
;	image: the two-dimensionnal array
;	xAxis, yAxis: the axes of "image"
;	nx, ny: the array dimensions
;	dx, dy: the image steps between two pixels
;	x1, y1: the first value of the axes.
; KEYWORDS:
;	TITLE: the title of the image
;	XLAB, YLAB, ZLAB: the titles of the axes involved
;	NAME: the name of the image
;	VMS: if present, the file is assumed to have the
;		"variable record length" from the VMS
;		fortran write. The file will be read, and 
;		directly rewritten in Unix SCA format.
;	TRANSFORM: transforms microseconds in seconds
;		in case of RAG data.
; MODIFICATION HISTORY:
;	Created, 4/91, A.Csillaghy, ETHZ
;	Modified, Jan.93, A.Cs
;-


PRO SCA_Read,file,image,xAxis, yAxis, x1, y1, dx, dy, $
	TITLE = title, NAME = name, VMS = vms, $
	XLAB = xLab, YLAB = yLab, ZLAB = zLab, $
	TRANSFORM = transform


  ON_IOError, ErrLabel

  vmsFile = Keyword_Set(VMS)

  IF Keyword_Set( TRANSFORM ) THEN transform = 1 $
  ELSE transform = 0

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

  fileToR=StrTrim(file,2)
  IF vmsFile THEN Conv_Vax_Block, fileToR
  
  OpenR, unit, fileToR, /GET_LUN, /F77_UNFORMATTED 

  Print, 'Sca File Read'

  Print,' Reading header ... '
  header = STRING(REPLICATE(32B,149))
  ReadU,unit,header

  Print,' Reading constants ... '
  nx=0L 	& ny=0L
  x1=0.   & y1=0.  & dx=0.  & dy=0.
  ReadU,unit,nx,ny,x1,y1,dx,dy

  IF vmsFile THEN BEGIN
    x1 = Conv_Vax_Unix( x1 )
    y1 = Conv_Vax_Unix( y1 )
    dx = Conv_Vax_Unix( dx )
    dy = Conv_Vax_Unix( dy )
  ENDIF

  IF dx EQ 0 THEN dx = 1.
  IF dy EQ 0 THEN dy = 1.

  IF dx GT 1e4 THEN BEGIN
    IF transform EQ 1 THEN dx = dx/1e6 $
    ELSE BEGIN
      rep = ''
      Print, 'The x step is probably in microseconds.'
      Read, 'Do you want to transform it in seconds [y] ? ',rep
      IF StrUpcase(rep) NE 'N' THEN dx = dx / 1e6
    ENDELSE
  ENDIF

  Print,' Reading image array ... '
  image = FltArr(nx,ny)
  ReadU,unit,image

  IF vmsFile THEN image = Conv_Vax_Unix( image )

  Free_LUn,unit

  title = STRTRIM(STRMID(header(0),0,80),2)
  xLab = STRTRIM(STRMID(header(0),80,20),2)
  yLab = STRTRIM(STRMID(header(0),100,20),2) 
  zLab = STRTRIM(STRMID(header(0),120,20),2) 

  Print,' Header: '
  Print,'        title:   '+title
  Print,'        x label: '+xLab
  Print,'        y label: '+yLab
  Print,'        z label: '+zLab
  Print,' Constants:'
  Print,'           x-dimension: '+String(nx)
  Print,'           y-dimension: '+String(ny)
  Print,'           x-step: '+String(dx)
  Print,'           y-step: '+String(dy)
  Print,'           x first value: '+String(x1)
  Print,'           y first value: '+String(y1)

  minZ=StrTrim(MIN(image) ,2) 
  maxZ=StrTrim(MAX(image),2)
  Print,' The image range is: ['+minZ+'..'+maxZ+']'

  IF vmsFile THEN $
	Sca_Write, image, x1, y1, dx, dy, $
		FILENAME = fileToR, TITLE = title, $
		NAME = name, XLAB = xLab, $
		YLAB = yLab, ZLAB = zLab

  IF (nbParams GE 3) AND (nbParams LE 4) THEN BEGIN
    xAxis = FIndGen( nx )* dx + x1
    yAxis = FIndGen( ny )* dy + y1
  ENDIF ELSE IF (nbParams GT 4) THEN BEGIN
    xAxis = nx
    yAxis = ny
  ENDIF

  RETURN

  ErrLabel:
    Print, 'I/O Error in the sca read procedure '
    Print, !err_string
  
END
