pro sxt2mpeg, sxtfile, r,g,b, movie_name=movie_name, outsize=outsize,  $
   nodelete=nodelete, cycle=cycle, goes=goes, 			$
   thumbnail=thumbnail, thumbfile=thumbfile, tframes=tframes,   $
   table=table, gamma=gamma, reverse=reverse, currentcolor=currentcolor, $
   debug=debug
;+
;   Name: sxt2mpeg
;
;   Purpose: convert an SXT SDA file to mpeg movie
;
;   Input Parameters:
;      sxtfile - sxt file name (default is most recent spd file)
;   
;   Keyword Paramters:
;      movie_name - mpg file name (default is derived)
;
;   History:
;      6-nov-1995 (SLF) 
;      8-nov-1995 (SLF) - make a thumbnail and html for mpg interface.
;-

reverse=keyword_set(reverse)
path_http=get_logenv('path_http')
top_http=get_logenv('top_http')

encode=concat_dir(concat_dir(path_http,'bin'),'mpeg_encode')
gif2pnm=concat_dir(concat_dir(path_http,'bin'),'giftopnm'	)

if not file_exist(encode) or not file_exist(gif2pnm) then begin
   message,/info,"Dont have required executables for mpeg production..."
   return
endif
 
scratchdir=get_logenv('DIR_MPEG_SC')
if scratchdir eq '' then scratchdir=concat_dir(path_http,'mpeg_scratch')

if not file_exist(scratchdir,/dir) then $
   spawn,['mkdir','-p',scratchdir],/noshell

curfid=str_replace(ex2fid(anytim2ex(ut_time())),'.','_')

if n_elements(sxtfile) eq 0 then sxtfile=newfiles(/spd,/last)
 
; generate temporary gif files
id='sxt_' + curfid

; if goes, load a combined table (image + bright plot colors)
goes=keyword_set(goes)
if goes then line_table, table, gamma=gamma, reverse=reverse 

sxt2file, infil=sxtfile, /gif, outdir=scratchdir, prefix=id, outsize=outsize, $
	newfiles=newfiles, $
        goes=goes, currentcolor=goes or keyword_set(currentcolor)

tvlct,r,g,b,/get

break_file,newfiles,nlogs,npaths,nfnames,next,nver
mfile_list=nfnames + next + nver

param_file=concat_dir(path_http,'mpeg_encode.param')

if not file_exist(param_file) then begin
   message,/info,"No mpeg parameter file <" + param_file + "> found..."
   return
endif

params=rd_tfile(param_file)
outparam=concat_dir(scratchdir,id + '_mpeg.param')
  
; now substitute mpeg parameters
movie_dir=concat_dir(path_http,'movies')

break_file,sxtfile,log,path,fname,fext,fver
if not keyword_set(movie_name) then movie_name= $
    concat_dir(movie_dir,fname + fext +  '.mpg')

if keyword_set(thumbnail) or keyword_set(thumbfile) then begin
   if not keyword_set(thumbfile) then $
      thumbfile=str_replace(movie_name,'.mpg','_mthumb.gif')
   iconfile=str_replace(thumbfile,'mthumb','micon')  
   rd_roadmap,sxtfile,rmap, nds
   if n_elements(tframes) eq 0 then tframes=8.
   secs=int2secarr(rmap)
   deltas=secs(nds-1)/tframes
   ss=grid_data(rmap,sec=deltas)
   ss=ss(0:tframes<(n_elements(ss)-1))

   if secs(nds-1) - secs(ss(n_elements(ss)-1)) gt (.3)*deltas then $
      ss=[ss,nds-1]

   rd_sda,sxtfile,ss,index,tdat
   if goes then tdat=bytscl(tdat,top=!d.table_size-14)+17 > 16 <!d.table_size
   fmt_timer,index,t0,t1
   func=(['gt_time','gt_day'])(ssw_deltat(t0,t1,/day) gt 1)   
   thumb=mkthumb(tdat,r,g,b,outsize=64,outfile=thumbfile, /frame, $
      labels=call_function(func,index,/string))
   icon=mkthumb(tdat,r,g,b,outsize=40,outfile=iconfile,/frame,/nofilm)
endif

params=str_replace(params,'!MOVIE_NAME!',movie_name)
params=str_replace(params,'!INPUT_PATH!',scratchdir)

ifss=(where(strpos(params,'!INPUT_FILTER!') gt 0,ifcnt))(0)
;params=str_replace(params,'!INPUT_FILTER!','sxt_' + curfid + '*.gif')

if ifcnt eq 0 then begin
   message,/info,"Problem with input parameter file...
   return
endif

; concatentate image list with mpeg paramters 
nimg=n_elements(mfile_list)
imgseq=lindgen(nimg)				; default image pointers

if keyword_set(cycle) then begin		; cycle = forward/time reversal
   imgseq=[imgseq,rotate(imgseq,2)]
   imgseq=reform(rebin(imgseq,nimg*2,cycle,/samp),cycle*nimg*2)   
endif 

params=[params(0:ifss-1),mfile_list(imgseq),params(ifss+1:*)]

file_append,outparam,params,/new		; write new param file

; now spawn mpeg

mpegcmd = encode + ' ' + outparam
start=ut_time()
if keyword_set(debug) then stop, 'before mpeg spawn...'

spawn,mpegcmd,status

stopt=ut_time()


if not keyword_set(nodelete) then file_delete,concat_dir(scratchdir,newfiles)

return
end
