pro ssw_panorama,fitslist1,fitslist2,fitslist3,fitslist4, $
   datamin=datamin, datamax=datamax,$
   dmin1=dmin1, dmin2=dmin2, dmin3=dmin3, dmin4=dmin4, $
   dmax1=dmax1, dmax2=dmax2, dmax3=dmax3, dmax4=dmax4, $
   _extra=_extra, $ ; Keyword Inherit -> Panorama Global options
   nospawn=nospawn, showcommand=showcommand, debug=debug
;
;+
;   Name: ssw_panorama
;
;   Purpose: form command + launch Panorama by R.Sequin for input FITS list(s)
;
;   Input Parameters:
;      fitslist1 - list of FITS files to include
;      fitsist2,...fitslistN - optional lists for other channels
;
;   Output:
;      None at this time
;
;   Keyword Parameters:
;      datamin - optional data min param(s) - assumed on element per FITSLIST
;      datamax - opttional data max param(s) - assumed on element per FITSLIST
;      dmin1..dminN - quasi synonym for datamin; use if only subset of chans
;      dmax1..dmaxN - quasi synonym for datamax; use if only subset of chans
;      _extra - unspecified keywords assumed to describe Panorama Global
;        options, inc: /activestereo, /passivestereo, plattime=<seconds>
;      pancmd (output) - the fully implied and realized Panorama command
;      nospawn (swithc) - if set, do not actually execute/spawn (return PANCMD only for example)
;      showcommand (switch) - if set, print the implied panorama command
;      background (switch) - if set, spawn panorama as background task (Unix only..)
; 
;   Calling Sequence:
;      IDL> ssw_panorama,fitsl1 [,fitsl2[,fitsl3[fitsl...N]]] , [,OPTIONS]
;
;   Calling Example:
;      IDL> ssw_panorama,euvi_behind_list, euvi_ahead_list  [,OPTIONS]; SECCHI 
;      IDL> ssw_panorama,behind,ahead,/activestereo,dmin1=50,dmax1=5000,playtime=500,/background
;   
;
;   History:
;      7-Feb-2007  - S.L.Freeland - SSW interface to Ralph Seguin Panorama Package
;-

if not data_chk(fitslist1,/string) then begin 
   box_message,'You must provide at least one list of FITS files...'
   return
endif

nchans=4 ; upper limit on #channels
spawnit=1-keyword_set(nospawn)    ; don't actually spawn the Panorama command
	
subdir=arr2str([!version.os,!version.arch],'_')
bindir=concat_dir('$SSW_PANORAMA','binaries')
panexe=concat_dir(concat_dir(bindir,subdir),'panorama')

if not file_exist(panexe) then begin 
   box_message,'Sorry, panorama executable not yet available for your OS/ARCH'
   return
endif


pn=strtrim(indgen(nchans)+1,2)
   
if n_elements(datamin) eq n_params() then begin 
   for i=0,n_params()-1 do begin 
      estat=execute('dmin'+pn(i)+'=strtrim(datamin(i),2)')
   endfor
endif
if n_elements(datamax) eq n_params() then begin 
   for i=0,n_params()-1 do begin 
      estat=execute('dmax'+pn(i)+'=strtrim(datamax(i),2)')
   endfor
endif

dmins=strarr(nchans) 
dmaxs=strarr(nchans)
for i=0,nchans-1 do begin 
   pni=pn(i)
   estat=execute('checkvar,dmin'+pni+',""')
   estat=execute('checkvar,dmax'+pni+',""')
   estat=execute('dmin'+pni+'=strtrim(dmin'+pni+',2)' )
   estat=execute('dmax'+pni+'=strtrim(dmax'+pni+',2)' )
endfor

dmins= [([""," -datamin "+dmin1])(dmin1 ne ''), $
       ([""," -datamin "+dmin2])(dmin2 ne ''), $
       ([""," -datamin "+dmin3])(dmin3 ne ''), $
       ([""," -datamin "+dmin4])(dmin4 ne '')]
dmaxs= [([""," -datamax "+dmax1])(dmax1 ne ''), $
       ([""," -datamax "+dmax2])(dmax2 ne ''), $
       ([""," -datamax "+dmax3])(dmax3 ne ''), $
       ([""," -datamax "+dmax4])(dmax4 ne '')]

; set global parameters (via _extra; keyword_inherit

global=''
; TODO? get these lists from Ralph Seguin README (single point maint...)
; TODO? allow shorter synonyms for all keywords/options
booleans=str2arr('nocursor,fullscreen,activestereo,dualstereo')
params=str2arr('width,height,playtime,cycleviews,cyclecolors')

if data_chk(_extra,/struct) then begin ; process global options (inheritance) 
   tns=tag_names(_extra)
   ltns=strlowcase(tns)
   for i=0,n_tags(_extra)-1 do begin 
      case 1 of 
         is_member(ltns(i),booleans): global=global+' -'+ ltns(i)
         is_member(ltns(i),params): $
             global=global+' -'+ ltns(i) + ' ' + strtrim(_extra.(i),2)
         else: box_message,'Don"t recognize global option>' + ltns(i)
      endcase
   endfor
endif

stereo=strpos(global,'stereo') ne -1

chans=indgen(nchans)
lefts=strarr(nchans)
rights=strarr(nchans)
lefts(where(1-chans mod 2))=([""," -left "])(stereo)
rights(where(chans mod 2))=([""," -right "])(stereo)

lists=strarr(nchans)

pancmd=panexe + ' ' + global + ' ' 

for i=0,n_params()-1 do begin 
   fn='fitslist'+pn(i)
   estat=execute('lists(i)=arr2str('+fn+'," ")')
endfor

vss=where(lists ne '',vcnt) ; valid/defined channels 
chcmds=' -channel ' + dmins(vss) + dmaxs(vss) + lefts(vss) + rights(vss) + lists(vss)

pancmd=pancmd + arr2str(chcmds,' ') ; final=OS exe + global + channel 


if keyword_set(showcommand) then begin 
   prstr,['----------------','','Panorama Command>> ',strrep_logenv(pancmd,'SSW_PANORAMA'),'','---------------']
endif

if spawnit then begin 
   espawn,pancmd,panoutput, background=background
endif

return
end



