PRO SCA_Write, z, x, y, dx, dy, $
	FILENAME = fileName, NAME = name, $
	TITLE=title, XLAB=xLab,YLAB=yLab ,ZLAB=zLab, $
	INTERACTIVE = interactive
;+
; NAME:
; 	Sca_Write
; PURPOSE:
;	Writes SCA files
; CATEGORY:
;	I/O
; CALLING SEQUENCE:
;	Sca_Write, array [, xAxis, yAxis ]
;	Sca_Write, array [, x1, y1, dx, dy ]
; INPUTS:
;	array: the data. If it is not in floating type it will be 
;		transformed.
; 	xAxis,yAxis: The axes, which the same number of elements
;		as their corresponding direction in the array.
;		The axes must be homogen.
;	x1, y1: the start values of the axes.
;	dx, dy: the step values of the axes.
; KEYWORDS:
;	FILENAME: the complete name of the file to produce. If
;		not present, it will have the name stored in
;		!p.title, without blank chars.
;	TITLE: the title of the array.  If not present, it will have the 
;		name stored in !p.title (max 80 chars)
;	NAME: the name of the array  (max 9 chars) If not
;		present, it will be blank.
;	XLAB, YLAB, ZLAB: titles for the three axes. If not 
;		present, the names will be taken from the 
;		variables !x.title, !y.title and !z.title,
;		respectively.
;	INTERACTIVE: If set, the file name is asked on the
;		terminal window. (when FILENAME is
;		not given).
; SIDE EFFECT:
;	A file is produced. If it already exists, it will be deleted 
;	and replaced by the new one.
; PROCEDURE:
;	The fortran implementation of the sca file write is
;	
;	CHARACTER NAME*9, TITLE*80, XLAB*20,
;		YLAB*20, ZLAB*20
;	INTEGER*4  NX, NY
;	REAL*4  Z(NX,NY), X1, Y1, DX, DY
;	
;	OPEN( UNIT=4, FILE=NAME//'.SCA', STATUS='OLD'
;		FORM='UNFORMATTED',IOSTAT=IO)
;	
;	WRITE(4) TITLE, XLAB, YLAB, ZLAB, NAME
;	WRITE(4) NX, NY, X1, Y1, DX, DY
;	WRITE(4) Z
;-
; MODIFICATION HISTORY:
;	Created in August 1991 by A.Csillaghy, from the
;		model of M.Aschwanden (IDL version)
;		and M.Guedel (specification).
;		Institute of Astronomy, ETH Zurich

nbParams = N_Params()
nx = N_Elements(z(*,0)) & ny = N_Elements(z(0,*))

CASE nbParams OF
  0: BEGIN
        Print,'You must specify an array to write ...'
        Print, ''
        RETURN
      END
  1: BEGIN
        x1 = 1. & y1 = 1.
        dx = 1. & dy = 1.
      END
  3: BEGIN
         x1 = FLOAT(x(0)) & y1 = FLOAT(y(0))
         IF nx GT 1 THEN  dx = FLOAT(x(1)) - FLOAT(x(0)) $
         ELSE dx = 0.
         IF ny GT 1 THEN dy = FLOAT(y(1)) - FLOAT(y(0)) $
         ELSE dy = 0.
      END
  5: BEGIN
          x1 = FLOAT(x) & y1 = FLOAT(y)
          dx = FLOAT(dx) & dy = FLOAT(dy)
      END
  ELSE: BEGIN
     Print,' Wrong number of parameters ... '
     Print,' '
     RETURN
  END
ENDCASE

z = FLOAT(z)

IF NOT Keyword_Set(FILENAME) THEN BEGIN
  IF Keyword_Set(INTERACTIVE) THEN BEGIN
    Clear_Screen
    filename = ''
    Print,'SCA file write'
    Print, ' '
    Read,'Please give the complete name of the file ', filename
  ENDIF ELSE $
    filename = StrCompress(!p.title,/REMOVE_ALL)+'.sca'
ENDIF

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(TITLE) THEN title = !p.title
IF NOT Keyword_Set(NAME) THEN name = ''

OpenW,unitw,filename,/GET_LUN, /F77_UNFORMATTED

Print, 'Writing sca file with name '+ filename

desc = String(Replicate(32B,149))
StrPut,desc,StrTrim(title,2),0
StrPut,desc,StrTrim(xLab,2),80
StrPut,desc,StrTrim(yLab,2),100
StrPut,desc,StrTrim(zLab,2),120
StrPut, desc, StrTrim(name,2),140

WriteU,unitw,desc
WriteU, unitw, nx, ny, x1, y1, dx, dy
WriteU, unitw, z

Free_LUn,unitw

END
