;+
; Given a time range, presumably for 1 orbit, returns background
; times, for 4 or 5 minutes before daytine, 4 or 5 minutes after
; daytime, also returns background rates and ephemeris in spherical
; coordinates fro each 20 second interval within the 2 4 minute
; periods. if the count rate is zero inside a 20 second interval or if
; there is a particle or non-solar event, the interval is not counted.
pro hsi_night_bck, time_range, otp_struct
;-
  otp_struct = -1
  ooo = {hsi_night_bck_struct, bck_rate:fltarr(9), bck_time:0.0d0, $
         ephemeris:fltarr(3), hlat_flag:0b, front_dec:0b, rear_dec:0b, $
         part_flag:0b, saa_flag:0b, atten_state:0b}
  oti = anytim(time_range)
  message, /info, 'Time_range:'
  print, anytim(/yoh, oti[0]), ' -- ',  anytim(/yoh, oti[1])
  robj = obj_new('hsi_obs_summ_rate', obs_time_i = oti)
  ddr = robj -> getdata()
  if(is_struct(ddr) eq 0) then begin
    message, /info, 'No Data'
    return
  endif
  fobj = obj_new('hsi_obs_summ_flag', obs_time_i = oti)
  ddf = fobj -> getdata()
  if(is_struct(ddf) eq 0) then begin
    message, /info, 'No Obs Summ Flag Data'
    return
  endif
  eobj = obj_new('hsi_ephemeris', obs_time_i = oti)
  dde = eobj -> getdata()
  if(is_struct(dde) eq 0) then begin
    message, /info, 'No Ephemeris Data'
    return
  endif
  etimes = eobj -> get(/time_array)
  neph = n_elements(etimes)
  tim_arr = fobj -> get(/time_array)
  ndset = n_elements(tim_arr)
;Countrate to use?
  xrate = robj -> corrected_countrate(obs_summ_flag_obj = fobj)
  xrate_flag = xrate[0, *] gt 0 and xrate[1, *] gt 0 and xrate[2, *] gt 0
  xrate_flag = reform(xrate_flag)
  xrate_ch0 = reform(xrate[0, *]+xrate[1, *]+xrate[2, *])*5.0 ;n_det=5
  ok = where(xrate_ch0 gt 0)
  if(ok[0] eq -1) then begin
    message, /info, 'No Data with nonzero rates'
    return
  endif
;Find the first minute of sunlight, sc_in_sunlight = 100
  sc_in_sunlight = fobj -> get(flag_name = 'sc_in_sunlight')
  s1 = where(sc_in_sunlight eq 100, ns1)
  if(ns1 eq 0) then begin
    message, /info, 'No Sunlight?'
    return
  endif
;need flags:
  fchan = fobj -> get(flag_name = 'decimation_energy')
  rchan = fobj -> get(flag_name = 'rear_dec_chan/128')
  pflag = fobj -> get(flag_name = 'Particle_flag')
  hflag = fobj -> get(flag_name = 'nmz_flag') + $
          fobj -> get(flag_name = 'smz_flag')
  sflag = fobj -> get(flag_name = 'saa_flag')
  attst = fobj -> get(flag_name = 'attenuator_state')
;find the time 5 minutes before sunlight
  npt5 = 75
  s10_b = s1[0]-2-lindgen(npt5) ;last 5 minutes before
  if(s10_b[npt5-1] lt 0) then begin ;not likely...
    message, /info, 'Not enough time before sunlight?'
    return
  endif
;find the time 5 minutes after sunlight
  s11_b = s1[ns1-1]+1+lindgen(npt5) ;first 5 minutes after
  if(s11_b[npt5-1] gt ndset-1) then begin  ;not likely...
    message, /info, 'Not enough time after sunlight?'
    return
  endif
;Ok, now get all of the ephemeris points in the intervals
  eph0 = where(etimes ge tim_arr[s10_b[npt5-1]]  and $
               etimes lt tim_arr[s10_b[0]], neph0)
  eph1 = where(etimes ge tim_arr[s11_b[0]]  and $
               etimes lt tim_arr[s11_b[npt5-1]], neph1)
  if(neph0 gt 0 and neph1 gt 0) then begin
    ephh = [eph0, eph1]
  endif else begin
    if(neph0 gt 0) then ephh = eph0 else ephh = eph1
  endelse
  if(ephh[0] eq -1) then begin
    message,  /info, 'No Good Ephemeris points'
    return
  endif
  nephh = n_elements(ephh)
;this is short, so just do it in a loop
  point_count = 0
  for j = 0, nephh-1 do begin
    xtimes = where(abs(tim_arr-etimes[ephh[j]]) lt 0.1) ;has to be true
    if(xtimes[0] ne -1) then begin
      ssx = xtimes[0]+lindgen(5) ;there are 5 rate points per eph point
      interval_notok = where(xrate_ch0[ssx] eq 0)
      if(interval_notok[0] eq -1) then begin 
;this is ok, check the flags
        if(total(fchan[ssx]) gt 0) then front_decj = 1 else front_decj = 0
        if(total(rchan[ssx]) gt 0) then rear_decj = 1 else rear_decj = 0
        if(total(pflag[ssx]) gt 0) then part_flagj = 1 else part_flagj = 0
        if(total(hflag[ssx]) gt 0) then hlat_flagj = 1 else hlat_flagj = 0
        if(total(sflag[ssx]) gt 0) then saa_flagj = 1 else saa_flagj = 0
        atten_statej = max(attst[ssx])
;Get the count rate
        bck_ratej = total(xrate[*, ssx], 2)/5.0 ;for concatenation
        bck_tim_arrj = etimes[ephh[j]]
;And the ephemeris, shperical q's please
        ephemerisj = hsi_eci2radec(dde[ephh[j]].xyz_eci[0:2])
        otp_structj = {hsi_night_bck_struct}
        otp_structj.front_dec = front_decj
        otp_structj.rear_dec = rear_decj
        otp_structj.part_flag = part_flagj
        otp_structj.hlat_flag = hlat_flagj
        otp_structj.saa_flag = saa_flagj
        otp_structj.bck_rate = bck_ratej
        otp_structj.bck_time = bck_tim_arrj
        otp_structj.ephemeris = ephemerisj
        otp_structj.atten_state = atten_statej
        if(point_count eq 0) then otp_struct = otp_structj else begin
          otp_struct = [otp_struct, otp_structj]
        endelse
        point_count = point_count+1
      endif
    endif
  endfor

  return
end