PRO WRITE_DISK_MOVIE_TEST, Fname, Img, Hdr, new=new, maxnum=maxnum, $
                      SUNXCEN=sunxcen, SUNYCEN=sunycen, SEC_PIX=sec_pix, $
                      REPLACE=replace, INDEX=replace_index, $
                      LCT=LCT,GCT=GCT,Z=Z
;+
; NAME:
;	WRITE_DISK_MOVIE
;
; PURPOSE:
;	This procedure adds an image to a file in the disk movie format.
;
; CATEGORY:
;	LASCO MOVIE
;
; CALLING SEQUENCE:
;	WRITE_DISK_MOVIE, Fname, Img, Hdr
;
; INPUTS:
;	Fname:	File name of the movie disk file, with full path
;
;	Img:	2D image to be added to the file
;
;	Hdr:	FITS header
;
; KEYWORD PARAMETERS:
;	NEW:	If set this keyword indicates that a new file is to be created.
;		The default is to write to an existing file
;
;	MAXNUM:	If set, this keyword specifies the maximum number of files to
;		be loaded into the file.  The default value is 128
;
;	/REPLACE:  Flag to replace an image rather than append
;	REPLACE_INDEX:Set to index of frame to replace in .mvi file
;
; PROCEDURE:
;	The disk movie format is:
;
;	file header:	# images in the file
;			# columns in each image
;			# rows in each image
;			# maximum number of images in file
;			# bytes in image header
;			# version number
;
;	for version 1:	# bytes in file header
;	for version 1:	# sunxcen * 10
;	for version 1:	# sunycen * 10
;	for version 1:	# arc sec per pixel * 100
;	for version 1:	# red color vector BYTARR(256)
;	for version 1:	# green color vector BYTARR(256)
;	for version 1:	# blue color vector BYTARR(256)
;
;	img hdr #1:	date of image, string (15)
;			time of image, string (15)
;			file name, string (15)
;			filter wheel, string (10)
;			polarizer wheel, string (10)
;			detector, string (10)
;	img hdr #2
;		...
;	img #1
;	img #2
;		...
;
; EXAMPLE:
;
;	Create a new movie file with img being the first image in the file:
;
;		DISK_MOVIE_FILE, '~/mymovie.mvi', img , hdr, /new, maxnum=20
;
;	Add to an existing movie file:
;
;		DISK_MOVIE_FILE, '~/mymovie.mvi', img , hdr
;
; MODIFICATION HISTORY:
; 	Written by:	RA Howard, NRL, 16 Mar 1996
;
; Modified    : SEP  05 Feb 1997 - Mods for mvi version 1 format.
;               SEP  08 Sep 1997 - Added REPLACE option.
;
;	@(#)write_disk_movie.pro	1.1 10/12/96 LASCO IDL LIBRARY
;-
;
sz = size(img)

nb = 128	; number of bytes reserved for image header
f_nb = 20			;** bytes in file_hdr before color table
file_hdr_nb = f_nb+256*3 	;** bytes in file_hdr with color table

IF (KEYWORD_SET(new)) THEN BEGIN
    OPENW,lu,Fname,/GET_LUN
    nf = 0
    nx = sz(1)
    ny = sz(2)
    fhdr = INTARR(f_nb/2)
    IF (KEYWORD_SET(maxnum)) THEN maxn = maxnum ELSE maxn=128
    fhdr(0) = 0
    fhdr(1) = nx
    fhdr(2) = ny
    fhdr(3) = maxn
    fhdr(4) = nb
    fhdr(5) = 2		;** version 2
    fhdr(6) = file_hdr_nb
    IF (KEYWORD_SET(SUNXCEN)) THEN xcen = sunxcen ELSE xcen=0
    IF (KEYWORD_SET(SUNYCEN)) THEN ycen = sunycen ELSE ycen=0
    fhdr(7) = FIX(xcen * 10)
    fhdr(8) = FIX(ycen * 10)
    IF (KEYWORD_SET(sec_pix)) THEN arcs = sec_pix ELSE arcs=0
    fhdr(9) = FIX(arcs * 100)
    file_hdr = ASSOC(lu,INTARR(f_nb/2))
ENDIF ELSE BEGIN    
    OPENU,lu,Fname,/GET_LUN
    file_hdr = ASSOC(lu,INTARR(file_hdr_nb/2))
    fhdr = file_hdr(0)
    nf = fhdr(0)
    nx = fhdr(1)
    ny = fhdr(2)
    maxn = fhdr(3)
    nb = fhdr(4)
ENDELSE                      
IF NOT(KEYWORD_SET(REPLACE)) THEN nf = nf+1	;** adding to end
IF (nf GT maxn) THEN BEGIN
   PRINT,'ERROR, number of images greater than maximum allowed, exiting'
   FREE_LUN,lu
   RETURN
ENDIF  
fhdr(0) = nf
file_hdr(0) = fhdr

IF (KEYWORD_SET(new)) THEN BEGIN	;** save colortable
   IF KEYWORD_SET(Z) THEN BEGIN
    dname=!d.name
    set_plot,'Z'
    IF KEYWORD_SET(lct) THEN loadct,lct
    IF KEYWORD_SET(gct) THEN gamma_ct,gct
    TVLCT, r, g, b, /GET
    device,/close
    set_plot,dname
   ENDIF ELSE BEGIN
    IF KEYWORD_SET(lct) THEN loadct,lct
    IF KEYWORD_SET(gct) THEN gamma_ct,gct
    TVLCT, r, g, b, /GET
   ENDELSE
   red = BYTARR(256) & red(0:N_ELEMENTS(r)-1) = r & red(N_ELEMENTS(r)-1:255) = r(N_ELEMENTS(r)-1)
   green = BYTARR(256) & green(0:N_ELEMENTS(g)-1) = g  & green(N_ELEMENTS(g)-1:255) = g(N_ELEMENTS(g)-1)
   blue = BYTARR(256) & blue(0:N_ELEMENTS(b)-1) = b & blue(N_ELEMENTS(b)-1:255) = b(N_ELEMENTS(b)-1)
   file_hdr_r = ASSOC(lu,BYTARR(256), f_nb) 
   file_hdr_g = ASSOC(lu,BYTARR(256), f_nb+256) 
   file_hdr_b = ASSOC(lu,BYTARR(256), f_nb+256+256) 
   file_hdr_r(0) = red
   file_hdr_g(0) = green
   file_hdr_b(0) = blue
ENDIF

img_hdrs = ASSOC(lu,BYTARR(nb),file_hdr_nb)
minhdr = BYTARR(nb)
minhdr(0)  = BYTE(fxpar(hdr,'DATE-OBS'))
minhdr(15) = BYTE(fxpar(hdr,'TIME-OBS'))
minhdr(30) = BYTE(fxpar(hdr,'FILENAME'))
minhdr(45) = BYTE(fxpar(hdr,'FILTER'))
det = fxpar(hdr,'DETECTOR')
IF (det EQ 'EIT')   THEN minhdr(55) = BYTE(fxpar(hdr,'SECTOR'))  $
                    ELSE minhdr(55) = BYTE(fxpar(hdr,'POLAR'))
minhdr(65) = BYTE(det)
IF KEYWORD_SET(REPLACE) THEN index = FIX(replace_index) ELSE index = nf-1
img_hdrs(index) = minhdr
imgs = ASSOC(lu,BYTARR(nx,ny),LONG(file_hdr_nb)+LONG(nb)*(LONG(maxn)))
imgs(index) = BYTE(img)
CLOSE,lu
FREE_LUN,lu
RETURN
END
