pro trace_build_mosaic, index, data, mosaic, www=www, csize=csize, fov=fov, $
			title=title, autocolor=autocolor, nogrid=nogrid, $
			_extra=_extra, pixmap=pixmap, zbuffer=zbuffer
;+
;   Name: build_trace_mosaic
;
;   Purpose: generate TRACE mosaic map, optionally -> WWW
;
;   Input Parameters:
;      index, data  - from read_trace, preselected mosic images
;      
;   Output Parameters:
;  
;   Keyword Parameters:
;      www   - if set, make an WWW version
;      csize - composite size (default=1024)  
;      fov   - solar FOV in arcminutes - default=40 = full sun/above limb
;      autocolor - if set, apply wavelenght dependent color table (ssw_ct2rgb)
;      title - optional user defined title
;      nogrid - if set, inhibit solar/limb grid
;      pixmap - if set, build mosaic in off screen PIXMAP
;      zbuffer - if set, build mosaic in Z-buffer  
;  
;   Calling Seqeunce:
;     [select, procss, and scale your 'data' (mosaic cube) as desired, then...]
;     IDL> trace_build_mosiac, index, data [,csize=xxxx, fov=xx, title="title" ,/www
;
;   Method:
;      index2map and then apply DMZarro mapping SW to position/composite
;
;   Restrictions:
;     need index,data with SSW standards (ex: read_trace default output)
;     need at least one SOHO instrument in  SSW path  
;  
;   History:
;      28-April-1998 - S.L.Freeland - apply SSW/Mapping techniques to TRACE
;-
if not data_chk(index,/struct) or n_params() lt 2 then begin
   box_message,['IDL> trace_build_mosiac, index, data [,csize=xxxx, $',$
		'        fov=xx, title="title" ,/www]']
   return
endif    

; ------------------ set up defaults/title -------------------------------
fmt_timer,index,t0,t1                                   ; mosic start/stop times
waves=strtrim(gt_tagval(index,/wave_len,miss='???'),2)  ; uniq wavelens
swave=arr2str(waves(uniq(waves,sort(waves))))            ; scalar version
if not keyword_set(csize) then csize=1024       ; default mosaic size
if not keyword_set(fov) then fov=40             ; arcmin - assume full disk
grid=([1,0])(keyword_set(nogrid))*15.           ; grid=15 is default

if not data_chk(title,/scalar,/string) then title = 'TRACE Mosaic, ' + $
   'Wave: '+swave + ' ' + arr2str(anytim([t0,t1],/ecs,/trunc),' - ')

; --------- load wavelenghth RGB on request -----------
if keyword_set(autocolor) then begin
   ssw_ct2rgb, fix(gt_tagval(index(0),/wave_len)), r,g,b, /load ,hi=!p.color   ; wave->rgb
endif
; -------------------------------------------------------------------------

; set up window
dtemp=!d.name
zbuff=keyword_set(zbuffer) or !d.name eq 'Z'
case 1 of
   !d.name eq 'Z':  
   keyword_set(zbuffer): 
   keyword_set(pixmap):
   csize gt 1024: begin
      box_message,'Too large, forcing use of Z-buffer...
   endcase     
   else:
endcase
if zbuff then begin
  wdef,zwin,/zbuffer,csize,csize
endif

; --------------- the MOSAIC part ---------------------------
index2map,index(0),data(*,*,0),map            ; 1st index/data->map 
plot_map, map, fov=fov, center=[0,0], $       ; plot 1st over fulldisk FOV
   xsize=csize,ysize=csize, title=title, grid=grid, $       ;   
   _extra=_extra

for j=1,n_elements(index)-1 do begin          ; for each additional piece
   index2map,index(j),data(*,*,j), map        ; index,data->map
   plot_map, map, /last_scale, composite=3    ; vignette type composite
endfor      
; -------------------------------------------------------------------------

if n_params() eq 3 then begin       ; read back
  case 1 of 
     !d.name ne 'Z': mosaic=tvrd()          ; X or PIXMAP, 8 bit transfer
     else: begin
       trans16=data_chk(data,/type) ne 1
       mosaic=tvrd(words=trans16, channel=trans16)
     endif       
  endcase
endif  

set_plot,dtemp
return
end
