;+
; Project     : SOHO - CDS     
;                   
; Name        : SHOW_DATAWIN
;               
; Purpose     : To list the contents of the datawin database.
;               
; Explanation : Uses the datawin database access routines to present the
;               user with a listing of the requested line list(s).
;               
; Use         : IDL> show_datawin [, num_list, /hardcopy, etc.]
;    
; Inputs      : num_list   -  Integer or integer array giving ID numbers
;                             of specific data window(s) required.  
;                   or
;               dw_struct  -  data window structure as returned by get_datawin
;
;               
; Opt. Inputs : None
;               
; Outputs     : Listing is given to screen or printer if required.
;               
; Opt. Outputs: None
;               
; Keywords    : DETECTOR   - specify detector to which line lists relate.
;               HARDCOPY   - print hardcopy of output.
;               QUIET      - no output to screen
;               KEEP       - keep output file
;               FILE       - specify output file, or retrieve file used if
;                            file = '' on entry.  If file is omitted or
;                            blank on entry then output is written to standard
;                            file in user's 'home' directory.  If a name is
;                            supplied on entry, that filename is used 
;                            unchanged.
;               SUMMARY    - summary of all lists (no details of individual
;                            lines).
;               ERRMSG     - return error in this if it is defined on entry
;
; Calls       : LIST_DATAWIN
;               GET_DATAWIN
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Database
;               
; Prev. Hist. : Based on tp_dump_setup.
;
; Written     : C D Pike, RAL, 24-May-95
;               
; Modified    : Rework output file name calculation.  CDP, 2-Jum-95
;               Added message when output KEEPed.  CDP, 27-Jul-95
;               Update for new format of structure input. CDP, 6-Feb-96
;               Include CURRENT keyword.  CDP, 7-Feb-96
;               Use FIND_ALL_DIR.         CDP, 09-Aug-96
;
; Version     :	Version 6, 09-Aug-96
;-            

pro show_datawin, num_list, detector=detector, hardcopy=hardcopy,$
                             quiet=quiet, summary=summary, keep=keep,$
                             errmsg=errmsg, file=file, current=current

;
;  Open a logical unit to the screen with the /MORE switch.
;
if not keyword_set(quiet) then to_screen = 1 else to_screen = 0
if to_screen then openw, screen, filepath(/TERMINAL), /MORE, /GET_LUN

;
;  was particular detector specified?
;
if not keyword_set(detector) then begin
   det = 'B' 
endif else begin
   det = strmid(strupcase(detector),0,1)
endelse

;
;  where from and when?
;
g = find_all_dir('ZDBASE')
t = systime()


;
;  was summary mode selected?
;
if n_params() eq 0 and keyword_set(summary) then begin


;
;  create filename (in home directory - unless directory specified) 
;  for hardcopy
;
   vms=(!version.os eq 'VMS')
   if not keyword_set(file) then begin
      hfile = 'datawin_lists'
      if vms then home='sys$login' else home=getenv('HOME')
      hfile=concat_dir(home,hfile)
   endif else begin
      hfile = file
   endelse

;
;  open disk file
;
   openw,lun,hfile,/get_lun,error=err
   if err ne 0 then begin
      if n_elements(errmsg) eq 0 then begin
         print,'Error opening output file ',hfile
      endif else begin
         errmsg = 'Error opening output file '+ hfile
      endelse
      return
   endif


;
;  begin output, echoing to hardcopy if needed
;
   if to_screen then begin
      printf,screen,' '
      printf,screen,'Information from line list databases in: '
      for i=0,n_elements(g)-1 do printf,screen,g(i),form='(20x,a)'
      printf,screen,' '
      printf,screen,' Printed on: ' + t
      printf,screen,' '
   endif

   printf,lun,' '
   printf,lun,'Information from line list databases in: '
   for i=0,n_elements(g)-1 do printf,lun,g(i),form='(20x,a)'
   printf,lun,' '
   printf,lun,' Printed on: '+ t
   printf,lun,' '

;
;  get basic data window details
;
   errmsg = ''
   idw = 0

   if to_screen then begin
      printf,screen,'               Windows'
      printf,screen,' ID  Detector  #     size            Description'
   endif
   printf,lun,'               Windows'
   printf,lun,' ID  Detector  #     size            Description'

   while errmsg eq '' do begin
      idw = idw + 1
      get_datawin,idw,dw,errmsg=errmsg
      if errmsg eq '' then begin
         if (dw.detector eq det) or (det eq 'B') then begin
            desc = rem_mult_char(dw.dw_desc)
            if to_screen then begin
               printf,screen,dw.dw_id,dw.detector,'IS',n_elements(dw.wins),$
                      fmt_vect([dw.w_width,dw.w_height]),desc,$
                      format='(i3,4x,a1,a2,4x,i2,1x,a10,4x,a)'
            endif
            printf,lun,dw.dw_id,dw.detector,'IS',n_elements(dw.wins),$
                   fmt_vect([dw.w_width,dw.w_height]),desc,$
                      format='(i3,4x,a1,a2,4x,i2,1x,a10,4x,a)'
         endif
      endif
   endwhile
   errmsg = ''
;
;  return file name used for output
;
   file = hfile

;
;   Close output to terminal and file
;
   if to_screen then free_lun,screen	
   free_lun,lun

;
;  deal with hardcopy
;
   if keyword_set(hardcopy) then begin
      if vms then com = 'print '+hfile else com = 'lpr '+hfile
      spawn,com
      print,'Hardcopy sent to printer.'
   endif

;
;  keep file?
;
   if (not keyword_set(keep)) and (not keyword_set(file)) then begin
      status = delete_file(hfile,/noconfirm)
   endif


   return
endif


;
;   want individual data window details
;   ===================================
;

;
;  if no parameters and no /summmary keyword then give help
;
if n_params() eq 0 then begin
   print,' '
   print,'Use: IDL> show_datawin, ID_array'
   print,'           or'
   print,'     IDL> show_datawin, structure'
   print,'           or'
   print,'     IDL> show_datawin, /summary'
   print,' '
   if to_screen then free_lun,screen
   return
endif

;
;  create filename (in home directory - unless directory specified) 
;  for hardcopy
;
vms=(!version.os eq 'VMS')
if not keyword_set(file) then begin
   hfile = 'datawin_details'
   if vms then home='sys$login' else home=getenv('HOME')
   hfile=concat_dir(home,hfile)
endif else begin
   hfile = file
endelse


;
;  open disk file
;
openw,lun,hfile,/get_lun,error=err
if err ne 0 then begin
   if n_elements(errmsg) eq 0 then begin
      print,'Error opening output file ',hfile
   endif else begin
      errmsg = 'Error opening output file '+ hfile
   endelse
   return
endif

;
;  check type of first parameter
;
;  was it a get_datawin structure?  If so then set up the details ready 
;  for use as if an ID was entered.
;
if datatype(num_list,1) eq 'Structure' then begin
   if n_tags(num_list) ne 11 then begin
      if n_elements(errmsg) gt 0 then begin
         errmsg = 'Invalid structure input.'
      endif else begin
         print,'Invalid structure input.'
      endelse
      if to_screen then free_lun, screen
      return
   endif else begin
      use_list = num_list.dw_id
   endelse
endif else begin
   use_list = num_list
endelse

;
;  can now proceed as if only IDs were input
;
;
;  loop through the datawins
;

for j=0,n_elements(use_list)-1 do begin

   get_datawin,use_list(j),dw
   if keyword_set(current) then dw = update_dex(dw)
   if (dw.detector eq det) or (det eq 'B') then begin
      sid = strtrim(string(dw.dw_id,form='(i5)'),2)
      if not keyword_set(current) then begin
         txt1 = '                       Data windows details'
         txt2 = '                       ===================='
      endif else begin
         txt1 = '              Data windows details (updated)'
         txt2 = '              =============================='
      endelse
      if to_screen then begin
         printf,screen,' '
         printf,screen,' '
         printf,screen,txt1
         printf,screen,txt2
         printf,screen,' '
         printf,screen,'Data window ID: ',sid,'  (',$
                     strtrim(dw.dw_desc,2),')' 
      endif
      printf,lun,' '
      printf,lun,' '
      printf,lun,txt1
      printf,lun,txt2
      printf,lun,' '
      printf,lun,'Data window ID: ',sid,'  (',$
                  strtrim(dw.dw_desc,2),')' 
   
      if dw.detector eq 'N' then begin
         if dw.vds_back then begin
            if to_screen then printf,screen,'VDS background windows:  Included'
            printf,lun,'VDS background windows:  Included'
         endif else begin
            if to_screen then printf,screen,'VDS background windows:  NOT included'
            printf,lun,'VDS background windows:  NOT included'
         endelse
      endif

      nwin = n_elements(dw.wins)
      if to_screen then printf,screen,$
                        string('Total number of data windows: ',nwin,form='(a,i2)')
      printf,lun,string('Total number of data windows: ',nwin,form='(a,i2)')

      if to_screen then printf,screen,' '
      printf,lun,' '
   
      if nwin gt 0 then begin
         if to_screen then printf,screen,$
                    '   Window        Name          Xstart    Ystart'+$
                    '   Xlength     Ylength'
         printf,lun,'   Window        Name          Xstart    Ystart'+$
                    '   Xlength     Ylength'
         for i=0,nwin-1 do begin
            if dw.wins(i).win_name eq ' ' then begin
               text = 'No title'
            endif else begin
               text = dw.wins(i).win_name
            endelse
            if strlen(text) lt 14 then text = strpad(text,14,/after)
            if to_screen then printf,screen,i,text,dw.wins(i).win_def,$
                                     form='(4x,i2,8x,a14,4x,4(i4,6x))'
            printf,lun,i,text,dw.wins(i).win_def,$
                       form='(4x,i2,8x,a14,4x,4(i4,6x))'
         endfor
      endif
   endif
endfor

;
;   Close output to terminal and file
;
if to_screen then free_lun,screen	
free_lun,lun

;
;  deal with hardcopy
;
if keyword_set(hardcopy) then begin
   if vms then com = 'print '+hfile else com = 'lpr '+hfile
   spawn,com
endif

;
;  keep file?
;
if (not keyword_set(keep)) and (not keyword_set(file)) then begin
   status = delete_file(hfile,/noconfirm)
endif else begin
   print,'Output saved in file: ',hfile
endelse

;
;  return file name used for output
;
file = hfile



end
