;+
; Project     : SOHO - CDS     
;                   
; Name        : CDS_TEMP_ATLAS
;               
; Purpose     : Plots standard set of temperatures from temperature log.
;               
; Explanation : Retrieves CDS temperature data from the stored log and plots
;               9 groups defined by S Peskett.
;               
; Use         : IDL> cds_temp_atlas [, start, + keywords for duration etc]
;    
;               eg IDL> cds_temp_atlas,'1995-12-1',dur=15
;
;               would plot 15 days of the logged data from  Dec 1  1995
;
; Inputs      : start = if specified data are only plotted from this date/time
;                       Format is CDS string or internal.  Default is to
;                       start 10 days back from current time.
;               
; Opt. Inputs : None
;               
; Outputs     : None
;               
; Opt. Outputs: None
;               
; Keywords    : SMOOTH - if present smoothing over this number of data points
;                        is performed prior to plotting.  Remember the log
;                        stores samples taken 30 minutes apart.
;
;               DURATION - specify plot duration in days (default=10)
;
;               TMIN, TMAX - if present change the plotting temperature scale
;                            from the defaults.
;
;               ALL  - unless this is given, only the last 10 days (or that
;                      specified by 'duration' are plotted
;
; Calls       : CDS_UTPLOT, CDS_OUTPLOT
;
; Common      : None
;               
; Restrictions: Needs CDS_ENG_DATA to be defined and temperature log file
;               to exist
;               
; Side effects: None
;               
; Category    : Engineering, temperature
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 8-Dec-95
;               
; Modified    : Handle no data case.  CDP, 13-Dec-95
;               Changed plotting symbols.  CDP, 18-Dec-95
;               Separate temperature scale for Thermal Stops group. 
;                                          CDP, 18-Dec-95
;               Make 10 day display the default.  CDP, 10-May-96
;               Rework handling of input parameters/keywords.  CDP, 31-May-96
;               Change name of UTPLOT.  CDP, 2-Oct-96
;
; Version     : Version 7, 2-Oct-96
;-            

pro cds_temp_atlas, start, duration=duration, smooth=smooth,$
                           tmin=tmin, tmax=tmax, all=all

;
;  check evar is defined
;
if getenv('CDS_ENG_DATA') eq '' then begin
   print,'$CDS_ENG_DATA is not defined.'
   return
endif

;
;  check log file exists and is accessible
;
logfile = concat_dir('$CDS_ENG_DATA','temperature_log')
if not file_exist(logfile) then begin
   bell
   print,'CDS temperature log file does not exist. <'+logfile+'>'
   return
endif


;
;  restore temperature log data
;
cds_temp_log = 0
restore,logfile

;
;  trim if necessary
;

if not keyword_set(all) then begin

   if not keyword_set(duration) then duration = 10
;
;  was a start time given?  It could be a DOY or string or structure.
;
   if n_elements(start) eq 0 then begin
      get_utc,utc
      utc.mjd = utc.mjd - long(duration)
      start = utc
   endif
   case datatype(start,1) of
   
      'Structure': begin
                      if tag_exist(start,'MJD') then begin
                         t1 = start
                      endif else begin
                         print,'Invalid start time strucutre'
                         return
                      endelse
                   end 
         'Double': t1 = tai2utc(start)
   
         'String': t1 = anytim2utc(start)
             else: begin
                      bell
                      print,'Start time format not recognised.'
                      return
                   end
   endcase
;
;  was a duration given?
;

;
;  set stop time
;
   t2 = t1
   t2.mjd = t2.mjd + long(duration)

;
; select the data required
;
   tai1 = utc2tai(t1)
   tai2 = utc2tai(t2)
   nst = min(where(cds_temp_log(0).time gt tai1))
   nend = max(where(cds_temp_log(0).time le tai2))

   nval = nend-nst+1
   temp = replicate({name:'',time:dblarr(nval),value:intarr(nval)},48)
   for i=0,47 do begin
      temp(i).name = cds_temp_log(i).name
      temp(i).time  = cds_temp_log(i).time(nst:nend)
      temp(i).value = cds_temp_log(i).value(nst:nend)
   endfor
   cds_temp_log = temp
   temp = 0
endif else begin
   t1 = tai2utc(cds_temp_log(0).time(0))
   t2 = tai2utc(cds_temp_log(0).time(n_elements(cds_temp_log(0).time)-1))
endelse
   
 
;
;  pick out only the 32 BTMxxx temperatures
;     
n = where(strmid(cds_temp_log.name,0,3) eq 'BTM')
temp = cds_temp_log(n)
temp = temp(0:31)

print,'Plotting from '+anytim2cal(t1)+' to '+ anytim2cal(t2)

;
;  set up groups defined by S Peskett
;   

gtitle = strarr(9)
gtitle(0) = 'Front Bulkhead Group'
gtitle(1) = 'Optical bench Group'
gtitle(2) = 'Bulkhead 3 Group'
gtitle(3) = 'Bulkhead 4 Group'
gtitle(4) = 'Optical Bench Mechanisms Group'
gtitle(5) = 'Actuators Group'
gtitle(6) = 'Electronics Platform Group'
gtitle(7) = 'Thermal Stops Group'
gtitle(8) = 'Assorted Group'

group = intarr(9,6) 
group(0,*) = [0,1,2,3,31,-1]
group(1,*) = [12,13,14,15,-1,-1]
group(2,*) = [4,5,6,7,23,-1]
group(3,*) = [8,9,10,11,-1,-1]
group(4,*) = [18,19,30,-1,-1,-1]
group(5,*) = [20,21,-1,-1,-1,-1]
group(6,*) = [22,24,25,26,27,17]
group(7,*) = [28,29,16,-1,-1,-1]
group(8,*) = [0,12,6,26,-1,-1]

;
;  set up 3 per page (then have 3 pages)
;
!p.multi=[0,1,3]

;
;  setup plotting selections
;
if n_elements(smooth) eq 0 then smooth = 1
circle_sym,/fill

;
;  loop through the groups
;
for i=0,8 do begin
;
;  temp? fix
;
   if n_elements(tmin) eq 0 then begin
      case i of
         0: begin & pmin = 10 & pmax = 30 & end 
         1: begin & pmin = 10 & pmax = 30 & end 
         2: begin & pmin = 10 & pmax = 30 & end 
         3: begin & pmin = 10 & pmax = 30 & end 
         4: begin & pmin = 10 & pmax = 50 & end 
         5: begin & pmin =  0 & pmax = 20 & end 
         6: begin & pmin = 10 & pmax = 50 & end 
         7: begin & pmin = -20 & pmax = 100 & end 
         8: begin & pmin = 10 & pmax = 30 & end 
      endcase
   endif else begin
      pmax = tmax
      pmin = tmin
   endelse
   if (i eq 2) or (i eq 5) or (i eq 8) then begin
      xtit = 'Time (UT) - Start date: '+anytim2cal(t1,/date)
   endif else begin
      xtit = ' '
   endelse
   for j=0,5 do begin
      n = group(i,j)  
      if n ge 0 then begin
         case j of
               0: begin & circle_sym & psym=8 & end
               1: begin & circle_sym,/fill & psym=8 & end
               2: psym=1
               3: psym=5
               4: psym=3
               5: psym=6
            else: psym=3
         endcase
         ystr = pmax - ((pmax-pmin)/13)*(j+1)
         data = tm_calib(temp(n).name, temp(n).value,words,/quiet)
         if smooth gt 1 then data = smooth(data,smooth)
         if j eq 0 then begin
            cds_utplot,[t1,t2], [pmin,pmax],/nodata, xtit = xtit,ystyle=1,$
                   yr=[pmin,pmax],psym=psym,timerange=[t1,t2],$
                   ytit='Temperature (degrees)'
            xstr1 = !x.crange(0) +  0.87*(!x.crange(1)-!x.crange(0))
            xstr2 = !x.crange(0) +  0.9*(!x.crange(1)-!x.crange(0))
            xstr3 = !x.crange(0) +  0.2*(!x.crange(1)-!x.crange(0))
            xyouts,xstr3,pmax - ((pmax-pmin)/8),gtitle(i)
         endif
         cds_outplot,tai2utc(cds_temp_log(n).time),data,psym=psym,symsize=0.3

;;;;;;;;;;
;         lims = find_data_runs(temp(n).time,temp(n).value,0,1,3600.)
;         if not same_data(reform(lims),[-1,-1]) then begin
;            for l = 0,n_elements(lims)/2-1 do begin
;               l1 = lims(l,0)
;               l2 = lims(l,1)
;               cds_outplot,tai2utc(temp(n).time(l1:l2)),data(l1:l2),psym=psym,$
;                        symsize=0.3
;            endfor
;         endif
;;;;;;;;;;;;;

         oplot,[xstr1],[ystr],psym=psym,symsize=0.5
         xyouts,xstr2,0.995*ystr,strtrim(string(n),2),/data,chars=0.6
      endif
   endfor
   if (i eq 2) or (i eq 5) and (!d.name ne 'PS') then begin
      yes_no,'OK to continue?',ok,'Y'
      if not ok then return
   endif
endfor

;
; tidy up
;
!p.multi=0


end
