pro ttma2s_mess,outfile,writeit, testing=testing
testing=keyword_set(testing)
fname=strsplit(outfile,'/',/last,/tail)
case 1 of
   writeit: mess='Writing file> ' + fname
   else: mess='File already exists> ' + fname
endcase
if testing then mess=[mess,'TEST MODE']
box_message,mess
return
end

pro trace_tma2secondary, tmafiles, scaleit=scaleit, $
	       force_overwrite=force_overwrite, testing=testing, $
	       showpix_only=showpix_only, noindex=noindex
;+
;   Name: trace_tma2secondary
;
;   Purpose: generate various secondary TRACE Mosaic data bases WWW, etc
;+
showpix_only=keyword_set(showpix_only)
noindex=keyword_set(noindex) or showpix_only
testing=keyword_set(testing)  
nottest=1-testing
force=keyword_set(force_overwrite)

if n_elements(tmafiles) eq 0 then begin 
   tmafiles=reverse(trace_files(/tma))
;   tmafiles=tmafiles(where(strpos(tmafiles,'171_') ne -1))
endif

box_message,tmafiles
break_file,tmafiles,ll,pp,ff,ee,vv

topdir=concat_dir('$tdb','mosaics')
fitsdir=concat_dir(topdir,'fits')
gifdir=concat_dir(topdir,'gif')

for i=0,n_elements(tmafiles)-1 do begin
   box_message,'Working on tmafile: > ' + tmafiles(i)
   read_trace,tmafiles(i),-1,index,data,outsize=1024
   index.naxis1=1024 & index.naxis2=1024
   sdata=trace_scale(index,data,/byte)

;  ------------- SOHO planning (flat fits) ---------------------
   fname=str2arr(trace_struct2filename(index,/soho),'_')
   fname(2)='fd' & fname=arr2str(fname,'_')
   outfile=concat_dir(fitsdir,fname)
   writeit=((1-file_exist(outfile)) or force) and (1-showpix_only)
   ttma2s_mess, outfile, writeit, testing=testing
   if writeit and nottest then write_trace, index, sdata, /flat, outfile=outfile
;  -----------------------------------------------

;  ---------- SOHO/SYNOP gif -----------------
   gdir=concat_dir(gifdir,time2file(index,/year2,/date_only))
   if 1-file_exist(gdir) then begin
      box_message,'Creating: ' + gdir
      spawn,['/bin/mkdir','-p',gdir],/noshell
   endif

   outfile=concat_dir(gdir,str_replace(fname,'fts','gif'))
   trace_colors,index,r,g,b
   writeit=((1-file_exist(outfile)) or force) and (1-showpix_only)
   ttma2s_mess,outfile,writeit, testing=testing
   if writeit and nottest then  write_gif,outfile,byte(sdata),r,g,b
;  ---------------------------------------------

;  ------- TRACE WWW gif archive -------------
   gdir='$path_http/mosaic_archive/gif'
   gwwwname=concat_dir(gdir,ff(i)+'.gif')
   outfile=(str_replace(gwwwname,'5120','1024'))(0)
   writeit=((1-file_exist(outfile)) or force) and (1-showpix_only)
   ttma2s_mess,outfile,writeit, testing=testing
   if writeit and nottest then  write_gif,outfile,byte(sdata),r,g,b     
;  ---------------------------------------------

;  ------------ showpix version ( -> First_LIght ) --------------
   set_logenv,'DIR_GEN_SHOWPIX','/hosts/sxt1/sxt1data3/ydb/showpix'
   outfile=concat_dir(concat_dir('$DIR_GEN_SHOWPIX','trace'), $
		     'last_trace_mosaic_'+gt_tagval(index,/wave_len))
   if file_exist(outfile) then  restgen,file=outfile,text=text  else $
        text=ff(i)
   tss=where(strpos(text,'_'+gt_tagval(index,/wave_len)) ne -1)
   newer=(ff(i) ge text(tss))(0)
   writeit=newer
   ttma2s_mess,outfile, writeit, testing=testing
   if newer then $
      box_message,'Updating; ' + ff(i) + 'is newer than '+ outfile else $
      box_message,'Not updating' +  ff(i) + ' is older than ' + outfile
    if writeit and nottest then savegen, file=outfile,  text=ff(i), byte(sdata), r,g,b
;  ---------------------------------------------
endfor

if not testing and (1-noindex) then begin               ; post processing stuff
   trace_mosaic_index,movie=5           ; make an index for WWW gif site
endif  

return
end

  

  
