
FUNCTION eis_mapper_goes, date, goes_str=goes_str, _extra=extra, $
                          block_times=block_times, verbose=verbose

;+
; NAME:
;     EIS_MAPPER_GOES
;
; PURPOSE:
;     Returns an IDL plot object containing the GOES X-ray 1-8
;     angstrom plot for the specified day.
;
; CATEGORY:
;     EIS mapper; GOES
;
; CALLING SEQUENCE:
;     Result = EIS_MAPPER_GOES ( Date )
;
; INPUTS:
;     Date:  A date for which the GOES plot is required.
;
; OPTIONAL INPUTS:
;     Goes_Str:  An IDL structure containing the GOES data to be
;                plotted. (This was used for bug-testing. Should not
;                be used in normal circumstances.)
;     Block_Times: A structure that identifies time blocks that will
;                  be shaded on the output figure. The structure
;                  needs to have the tags:
;                  .t_start Start time of block (any SSW format).
;                  .t_end   End time of block (any SSW format).
;
;     Accepts various plot object keywords through the "extra" feature.
;
; KEYWORD PARAMETERS:
;     VERBOSE:  If set, then some additional information is printed to
;               the screen.
;
; OUTPUTS:
;     An IDL plot object containing the GOES 1-8 angstrom data. Also
;     shows three horizontal lines giving GOES class. If there is a
;     problem then the value -1 is returned. 
;
; EXAMPLE:
;     IDL> p=eis_mapper_goes('15-feb-2011')
;
; MODIFICATION HISTORY:
;     Ver.1, 24-Mar-2020, Peter Young
;     Ver.2, 01-Mar-2021, Peter Young
;       Switched from using /sdac to /any when setting up the GOES
;       object.
;     Ver.3, 09-Jun-2022, Peter Young
;       Added block_times= optional input; adjusted the y-range for
;       the plot and the labels.
;     Ver.4, 02-Aug-2023, Peter Young
;       I've attempted to fix the gaps in GOES data that are sometimes
;       seen by requesting data from the previous GOES spacecraft.
;     Ver.5, 18-Apr-2024, Peter Young
;       Moved code for "sat" due to crash for 11-Aug-2009.
;-


IF n_params() LT 1 THEN BEGIN
  print,'Use:  IDL> p=eis_mapper_goes( date [, goes_str=, _extra=, block_times=, /verbose ] )'
  return,-1
ENDIF 


date_tai0=anytim2tai(date)
date_tai1=date_tai0+86400.
t0=anytim2utc(date_tai0,/ccsds)
t1=anytim2utc(date_tai1,/ccsds)
tjd0=tim2jd(t0)
tjd1=tim2jd(t1)

IF n_tags(goes_str) EQ 0 THEN BEGIN 
  g=ogoes()
  g->set,tstart=t0,tend=t1,/any,mode=1
  gdata=g->getdata(/struct,/quiet)
  low=g->getdata(/low,/quiet)
ENDIF ELSE BEGIN
  gdata=goes_str
ENDELSE 

IF n_tags(gdata) eq 0 THEN return,-1

sat=gdata.sat
IF keyword_set(verbose) THEN message,/info,/cont,'Downloaded data from '+trim(sat)+' satellite.'


tref=tim2jd(gdata.utbase)
tdays=gdata.tarray/86400d
tt_jd=tref+tdays

;
; Here I retrieve data from a different GOES spacecraft. Sometimes there only
; seems to be one available spacecraft. I also see that requesting GOES-15
; results in GOES-17 data (with the primary dataset being GOES-16). I don't
; know why this happens.
;
k=where(low LE 1e-7,nk)
IF nk GT 0 THEN BEGIN
  goesno=fix(strmid(sat,4,2))
  g->set,sat=goesno-1
  gdata2=g->getdata(/struct,/quiet)
  low2=g->getdata(/low,/quiet)
  sat2=gdata2.sat
 ;
  t_goes_tai2=anytim2tai(gdata2.utbase)+gdata2.tarray
  t_goes_ccsds2=anytim2utc(t_goes_tai2,/ccsds)
  tt_jd2=tim2jd(t_goes_ccsds2)
  IF keyword_set(verbose) THEN message,/info,/cont,'Downloaded data from '+trim(sat2)+' satellite.'
ENDIF
obj_destroy,g

;
; I combine the two GOES plots by taking the maximum. I require
; that the arrays have the same number of elements, but I don't
; require the time arrays to be identical.
; 
IF n_elements(tt_jd2) EQ n_elements(tt_jd) THEN BEGIN
  data=[[low],[low2]]
  lc=max(data,dim=2)
ENDIF ELSE BEGIN
  IF keyword_set(verbose) THEN message,/info,/cont,'Time arrays do not match. Only using data from one spacecraft.'
  lc=low
ENDELSE 

;lc=gdata.ydata[*,0]
p=plot(tt_jd,lc,xtickunits='time',xtickformat='(C(CHI2.2,":",CMI2.2))',xsty=1,/ylog, $
       _extra=extra,yra=[1e-8,1e-3],/ysty,color='blue', $
       xtitle='Time',ytitle='Flux',xrange=[tjd0,tjd1])
pos=p.position
x0=pos[0]
x1=pos[2]
y0=pos[1]
y1=pos[3]
pl1=plot(p.xrange,1e-4*[1,1],_extra=extra,/overplot)  ; X-class
pl2=plot(p.xrange,1e-5*[1,1],_extra=extra,/overplot)  ; M-class
pl3=plot(p.xrange,1e-6*[1,1],_extra=extra,/overplot)  ; C-class
txt1=text(0.98*x1+0.02*x0,(4./5.)*y1+(1./5.)*y0,'X',color='blue',target=p,align=0.5)
txt2=text(0.98*x1+0.02*x0,(3./5.)*y1+(2./5.)*y0,'M',color='blue',target=p,align=0.5)
txt3=text(0.98*x1+0.02*x0,(2./5.)*y1+(3./5.)*y0,'C',color='blue',target=p,align=0.5)

txtg=text(0.03*x1+0.97*x0,.85*y1+.15*y0,'GOES, 1-8 '+string(197b),color='blue',target=p)

IF n_tags(block_times) NE 0 THEN BEGIN
  tjd_start=tim2jd(block_times.t_start)
  tjd_end=tim2jd(block_times.t_end)
  n=n_elements(block_times)
  FOR i=0,n-1 DO BEGIN
    t0=tjd_start[i]
    t1=tjd_end[i]
    xx=[t0,t1,t1,t0,t0]
    yr=p.yrange
    yy=[yr[0],yr[0],yr[1],yr[1],yr[0]]
    b=polygon(xx,yy, $
              /fill_background,/data,fill_color='light steel blue', $
              pattern_orient=45,pattern_spacing=6,target=p)
  ENDFOR 
ENDIF 

return,p

END
