;+
; Project     : SOHO - CDS     
;                   
; Name        : SHOW_PLAN
;               
; Purpose     : Produce a listing of a CDS observation plan.
;               
; Explanation : For those who like to read paper, this routine displays
;               the salient points in the definition of a CDS entry in 
;               one of the planning databases.
;               
; Use         : IDL> show_plan, str [,/detail, /sci etc.]    
;                             or
;               IDL> show_plan, utc [,/detail etc.]
;
; Inputs      : str          -  a valid CDS details definition structure
;                               as returned by one of the get_xxxx routines.
;                      OR
;               utc          -  a valid CDS time in utc internal format
;
; Opt. Inputs : None
;               
; Outputs     : Can send to file and print, also output to screen if needed.
;               
; Opt. Outputs: None
;               
; Keywords    : KEEP     -  keep temporary output file.  File name is 
;                           detail_yyyymmddhhmmss.dmp  where the yyyy.... is 
;                           date and time of the start of the entry. For other
;                           than the details database the prefic 'detail' is
;                           replaced by something appropriate.
;               QUIET    -  no information to screen
;               HARDCOPY -  send output to printer
;               ERRMSG   -  if defined on entry this will be returned with
;                           any error messages that occur.
;               FILE     -  file name created is returned in this if defined
;                           on entry (and set to '') or any input value is 
;                           used as the output file name.
;               SUMMARY  -  just give a summary of all entries (needs one 
;                           of following keywords to identify the plan 
;                           required.
;     
;               DETAIL   -  use only the CDS details database
;               SCIENCE  -  use only the CDS science plan database
;               SOHO     -  use the Soho plan database
;               FLAG     -  use only the CDS flag database
;               ALT      -  use only the CDS alternative science plan
;               INSTRUMENT - instrument when selecting science plans (default
;                            = all instruments)
;               NOW      -  do a summary from now to infinity
;               FROM     -  do a summary from this time to infinity
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Planning, databases
;               
; Prev. Hist. : Based on original dump_detail.
;
; Written     : C D Pike, RAL, 26-Apr-95
;               
; Modified    : Rework output file name use.  CDP, 2-Jun-95
;               Add time tag info for details time line.  CDP, 8-Jun-95
;               Added message when output KEEPed.  CDP, 27-Jul-95
;               Added NOW keyword.      CDP, 15-Nov-95
;               Added FROM keyword.     CDP, 26-Feb-96
;               Added numberof rasters in /det,/summ mode. CDP, 1-Apr-96
;               Use tai in cal to get_detail instead of string. CDP, 3-May-96
;
; Version     : Version 8, 3-May-96
;-            

pro show_plan, in_str, quiet=quiet, keep=keep, hardcopy=hardcopy,$
                       file=file, errmsg=errmsg, summary=summary,$
                       detail=detail, science=science, soho=soho,$
                       flag=flag, alt=alt, instrument=instrument,$
                       now=now, from=from

;
;  check form of input
;
if n_params() eq 0 and not keyword_set(summary) then begin
   print,' '
   print,'Use: IDL> show_plan, structure + keywords'
   print,'          or'
   print,'     IDL> show_plan, utc + keywords'
   print,' '
   return
endif

;
;  list to terminal?
;
if keyword_set(quiet) then to_screen = 0 else to_screen = 1
if to_screen then openw, screen, filepath(/terminal), /more, /get_lun

;
;  list to home directory unless overridden by user input
;
vms=(!version.os eq 'VMS')
if vms then home='sys$login' else home=getenv('HOME')

;
;  summary listing required
;
if keyword_set(summary) then begin


;
;  want only future events?
;
   if keyword_set(now) then begin
      get_utc,utc1
      utc2 = utc1
      utc2.mjd = utc2.mjd+3L
      startt = strmid(utc2str(utc1),0,16)
      endt   = strmid(utc2str(utc2),0,16)
   endif else begin
      if keyword_set(from) then begin
         startt = anytim2utc(from)
         startt = utc2str(startt)
         endt   = '2005/1/1'
      endif else begin
         startt = '1994/1/1'
         endt   = '2005/1/1'
      endelse
   endelse

;
;  check only one database specified
;
   if total(keyword_set(detail)+$
            keyword_set(science)+$
            keyword_set(soho)+$
            keyword_set(flag)+$
            keyword_set(alt)) ne 1 then begin
      if n_elements(errmsg) gt 0 then begin
         errmsg = 'Must specify one, and one only, planning database via keyword.'
      endif else begin
         print,'Must specify one, and one only, planning database via keyword.'
      endelse
      return
   endif 

;
; possible file names
;
  
   case 1 of
      keyword_set(detail):  hfile = 'detail_summary'
      keyword_set(science): hfile = 'science_summary'
      keyword_set(soho):    hfile = 'soho_detail_summary'
      keyword_set(flag):    hfile = 'cds_flag_summary'
      keyword_set(alt):     hfile = 'cds_alt_sci_summary'
      else: 
   endcase

   if keyword_set(file) then begin
      hfile = file
   endif else begin
      hfile = concat_dir(home,hfile) 
   endelse
   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
 

;
;  get summary details of requested database
;
   case 1 of

;
;  Summary of CDS DETAILS
;  ======================
;
      keyword_set(detail): begin


;
;  try to cater for Euro-date format enthusiasts
;
                              utc = str2utc(startt)
                              if utc.mjd lt 49353 or utc.mjd gt 53371 then begin
                                 t1 = str2utc(startt,/dmy) 
                              endif else begin
                                 t1 = startt 
                              endelse
                              utc = str2utc(endt)
                              if utc.mjd lt 49353 or utc.mjd gt 53371 then begin
                                 t2 = str2utc(endt,/dmy) 
                              endif else begin
                                 t2 = endt 
                              endelse

;
;  anything in details database?
;
                              
                              list_detail,t1,t2,detail,n

;
;  anything found?
;
                              if n gt 0 then begin
;
;  title
;
                                 if to_screen then begin
                                    printf,screen,'Entries in the CDS DETAIL database covering the period '+$
                                                   startt + ' to ' + endt
                                    printf,screen, ' '
                                 endif
                                 printf,lun,'Entries in the CDS DETAIL database covering the period '+$
                                             startt + ' to ' + endt
                                 printf,lun,' '
                                 for i=0,n_elements(detail)-1 do begin
                                    st = anytim2cal(tai2utc(detail(i).date_obs),form=1)
                                    et = strmid(anytim2cal(tai2utc(detail(i).date_end),form=1),10,8)
                                    sv = fmt_vect([detail(i).study_id,detail(i).studyvar],delim='/')
;
;  collect number of rasters
;
                                    totr = 0
                                    get_detail,detail(i).date_obs,obs
                                    n1 = obs.n_rasters1 
                                    n2 = obs.n_pointings > 1
                                    n3 = obs.n_repeat_s  > 1
                                    get_study,obs.study_id, obs.studyvar,study
                                    n4 = n_elements(study.rasters) 
                                    if n4 gt 1 then begin
                                       for j=0,n4-2 do begin
                                          totr = totr + study.rasters(j).n_repeat_r
                                       endfor
                                       totr = totr + n1
                                    endif else begin
                                       totr = n1
                                    endelse
                                    totr = totr*n2*n3
                                    if totr eq 1 then stotr=trim(totr)+' raster - ' else stotr=trim(totr)+' rasters -'
                                    stotr = strpad(stotr,14,/after) 

                                    printf,lun,stotr+'Study/var: '+sv+'   '+ st+' --> '+ et+'  '+ detail(i).sci_obj
                                    if to_screen then begin
                                       printf,screen,stotr+'Study/var: '+sv+'   '+ st+' --> '+ et+'  '+ detail(i).sci_obj
                                    endif
                                    
                                 endfor
                              endif
                           end
;
;  Summary of SOHO SCIENCE
;  =======================
;
      keyword_set(science): begin

;
;  try to cater for Euro-date format enthusiasts
;
                              utc = str2utc(startt)
                              if utc.mjd lt 49353 or utc.mjd gt 53371 then begin
                                 t1 = str2utc(startt,/dmy) 
                              endif else begin
                                 t1 = startt 
                              endelse
                              utc = str2utc(endt)
                              if utc.mjd lt 49353 or utc.mjd gt 53371 then begin
                                 t2 = str2utc(endt,/dmy) 
                              endif else begin
                                 t2 = endt 
                              endelse

;
;  anything in plan database?
;                                     
                              list_plan,t1,t2,plan,n

;
;  anything found?
;
                              if n gt 0 then begin
;
;  title
;
                                 if to_screen then printf,screen,'Entries in the SOHO SCIENCE PLAN database covering the period '+$
                                                        startt + ' to ' + endt
                                 printf,lun,'Entries in the SOHO SCIENCE PLAN database covering the period '+$
                                                   startt + ' to ' + endt
                                 if to_screen then printf,screen,' ' 
                                 printf,lun,' '

;
;  instrument key
;
                                 ins = ['EIT','CDS','SUMER','UVCS','LASCO']
   
;
;  was an instrument specified on the command line?
;
                                 if not keyword_set(instrument) then begin
                                    use_ins = 'A'
                                 endif else begin
                                    use_ins = strmid(strupcase(instrument),0,1)
                                 endelse
;
;  list each entry
;
                                 for i=0,n_elements(plan)-1 do begin
                                    st = anytim2cal(tai2utc(plan(i).start_time),form=1)
                                    et = strmid(anytim2cal(tai2utc(plan(i).end_time),form=1),10,8)
                                    if use_ins eq plan(i).instrume or use_ins eq 'A' then begin
                                       ins_name = where(strmid(ins,0,1) eq plan(i).instrume)
                                       if ins_name(0) ge 0 then begin
                                          det = strpad(ins(ins_name(0)),7,/after)
                                       endif else begin
                                          det = '   ?   '
                                       endelse
                                       printf,lun,' ',+det+'  '+ st+' --> '+ et+'  '+ plan(i).sci_obj
                                       if to_screen then begin
                                          printf,screen,' '+det+'  '+ st+' --> '+ et+'  '+ plan(i).sci_obj
                                       endif
                                    endif
                                 endfor
                              endif else begin
                                 if n_elements(errmsg) eq 0 then begin
                                    print,'No entries found for that period'
                                 endif else begin
                                    errmsg = 'No entries found for that period'
                                 endelse
                              endelse
                           end
      else: begin
               if n_elements(errmsg) gt 0 then begin
                  errmsg = 'Not yet implemented'
               endif else begin
                  print,'Not yet implemented'
               endelse
               return
            end
   endcase

;
;  hard copy print?
;
   if to_screen then free_lun, screen
   free_lun,lun

   if keyword_set(hardcopy) then begin
      if vms then com = 'print '+hfile else com = 'lpr '+hfile
      spawn,com
      print,'Output file sent to printer.'
   endif

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

;
;  pass back file name used
;
   file = hfile


   return
endif

   
;
;  Wanted specific details on a particular entry
;  =============================================



if n_params() eq 1 and (datatype(in_str,1) ne 'Structure') then begin
   if n_elements(errmsg) gt 0 then begin
      errmsg = 'Parameter must be a database structure or a UTC time structure.'
   endif else begin
      print,'Parameter must be a database structure or a UTC time structure.'
   endelse
   return
endif


;
;  check which structure it is.  If a time then get the structure specified by
;  the keyword
;


if n_tags(in_str) eq 2 then begin
   tstr = in_str
   case 1 of
      keyword_set(detail): begin
                             get_detail, tstr, str
                             if str.study_id eq -1 then begin
                                if n_elements(errmsg) eq 0 then begin
                                   print,'No details available for that time.'
                                endif else begin
                                   errmsg = 'No details available for that time.'
                                endelse
                                return
                             endif
                           end
     keyword_set(science): begin
                             if n_elements(errmsg) eq 0 then begin
                                print,'This database not yet implemented.'
                             endif else begin
                                errmsg = 'This database not yet implemented.'
                             endelse
                             return
                           end
        keyword_set(soho): begin
                             if n_elements(errmsg) eq 0 then begin
                                print,'This database not yet implemented.'
                             endif else begin
                                errmsg = 'This database not yet implemented.'
                             endelse
                             return
                           end
        keyword_set(flag): begin
                             if n_elements(errmsg) eq 0 then begin
                                print,'This database not yet implemented.'
                             endif else begin
                                errmsg = 'This database not yet implemented.'
                             endelse
                             return
                           end
         keyword_set(alt): begin
                             if n_elements(errmsg) eq 0 then begin
                                print,'This database not yet implemented.'
                             endif else begin
                                errmsg = 'This database not yet implemented.'
                             endelse
                             return
                           end
                    else:  begin
                             if n_elements(errmsg) eq 0 then begin
                                print,'Must specify a database in this mode.'
                             endif else begin
                                errmsg = 'Must specify a database in this mode.'
                             endelse
                             return
                           end
   endcase
endif else begin
   str = in_str
endelse


;
;  have the plan structure so output as appropriate
;
case str.struct_type of

   'CDS-DETAIL':  begin

;
;  prepare output file
;
             hfile = 'detail_'+strtrim(anytim2cal(tai2utc(str.date_obs),f=8),2)+'.dmp'
             if keyword_set(file) then begin
                hfile = file
             endif else begin
                hfile = concat_dir(home,hfile) 
             endelse
             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
 

   
;
;  Fill out any 'blanks'
;
             if strtrim(str.sci_obj,2)  eq '' then str.sci_obj  = 'Not defined'
             if strtrim(str.sci_spec,2) eq '' then str.sci_spec = 'Not defined'
             if strtrim(str.object,2)   eq '' then str.object   = 'Not defined'
             if strtrim(str.obj_id,2)   eq '' then str.obj_id   = 'Not defined'

;
;  pretty format
;
             printf,lun,' '
             if to_screen then printf,screen,' '

             cst  = anytim2cal(tai2utc(str.date_obs),form=1)
             cend = anytim2cal(tai2utc(str.date_end),form=1)

             printf,lun,'                 CDS Detailed plan entry.'
             if to_screen then printf,screen,'                 CDS Detailed plan entry.'

             printf,lun,'                 ========================'
             if to_screen then printf,screen,'                 ========================'
             
             printf,lun,' '
             if to_screen then printf,screen,' '

             if str.time_tagged then tagged = '  (Time tagged)' else $
                                     tagged = '  (Following previous study)'
             printf,lun,'For the period:  '+cst+tagged
             if to_screen then printf,screen,'For the period:  '+cst+tagged

             printf,lun,'             to  '+cend
             if to_screen then printf,screen,'             to  '+cend

             printf,lun,' '
             if to_screen then printf,screen,' '


;
;  Program ID  and campaign number
;
             txt1 = strtrim(string(str.prog_id,form='(i4)'),2)
             txt2 = strtrim(string(str.cmp_no,form='(i4)'),2)
             printf,lun,'Program ID: '+txt1+'  Campaign number: '+txt2  
             if to_screen then printf,screen,'Program ID: '+txt1+'  Campaign number: '+txt2  

;
;  study and variation IDs
;
             csid = strtrim(string(str.study_id,form='(i3)'),2)
             svid = strtrim(string(str.studyvar,form='(i3)'),2)

             printf,lun,'Study ID #'+csid,'Variation ID #'+svid,$
                        format='(a,5x,a)'
             if to_screen then printf,screen,'Study ID #'+csid,$
                                             'Variation ID #'+svid,$
                                              format='(a,5x,a)'

             printf,lun,' '
             if to_screen then printf,screen,' '
             
             printf,lun,'Science Objective: '+str.sci_obj
             if to_screen then printf,screen,'Science Objective: '+str.sci_obj
             
             printf,lun,'Specific Science Objective: '+str.sci_spec
             if to_screen then printf,screen,'Specific Science Objective: '+str.sci_spec


             printf,lun,' '
             if to_screen then printf,screen,' '

             printf,lun,'Object code: '+str.object
             if to_screen then printf,screen,'Object code: '+str.object

             printf,lun,'Object ID:   '+str.obj_id
             if to_screen then printf,screen,'Object ID:   '+str.obj_id

             
             track = ['OFF','ON']
             printf,lun,'Solar feature tracking '+track(str.tracking)
             if to_screen then printf,screen,'Solar feature tracking '+track(str.tracking)
             printf,lun,' '
             if to_screen then printf,screen,' '


             txt = strtrim(string(str.n_rasters1,form='(i4)'),2)
             printf,lun,'Number of times last raster repeated: '+txt
             if to_screen then printf,screen,'Number of times last raster repeated: '+txt
 
             txt = strtrim(string(str.n_repeat_s,form='(i4)'),2)
             printf,lun,'Number of times this study repeated: '+txt
             if to_screen then printf,screen,'Number of times this study repeated: '+txt


;
;  detail each pointing of the study
;
             np = str.n_pointings
             txt = strtrim(string(np,form='(i4)'),2)
             printf,lun,'Number of pointings of this study: '+txt
             if to_screen then begin
                printf,screen,'Number of pointings of this study: '+txt
             endif
             
             printf,lun,' '
             if to_screen then printf,screen,' '
             
             title = '  Pointing   Xpos    Ypos        Zone '
             printf,lun,title
             if to_screen then printf,screen,title
             printf,lun,' '
             if to_screen then printf,screen,' '
             
             zone_type = ['Not defined','CORONA','LIMB','CORONAL HOLE','QUIET','ACTIVE','FLARE']
             for i=0,np-1 do begin
                printf,lun,i+1,fix(str.pointings(i).ins_x),fix(str.pointings(i).ins_y),$
                               str.pointings(i).zone_id,' (',$
                               zone_type(str.pointings(i).zone_id),')'
                               format='(i2,9x,2i7,3x,i1,a1,a,a1)'
                if to_screen then begin
                   printf,screen,i+1,fix(str.pointings(i).ins_x),$
                                     fix(str.pointings(i).ins_y),$
                                     str.pointings(i).zone_id,' (',$
                                     zone_type(str.pointings(i).zone_id),')'
                                     format='(i2,9x,2i7,3x,i1,a1,a,a1)'
                endif
             endfor
          end
   else: print,'Not yet available.'
endcase

;
;  tidy up, print file
;
if to_screen then free_lun, screen
free_lun, lun

if keyword_set(hardcopy) then begin
   if vms then com = 'print '+hfile else com= 'lpr '+hfile
   spawn,com
   print,'Output file sent to printer.'
endif

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 
;
file = hfile

end
