;+
; NAME:
;	SELECT_BDA
; PURPOSE:
;	Plots light curve of Ca XIX and allows user to select data set
;	using the cursors. SS vector is created for use with TEST_RD
; CALLING SEQUENCE:  
;	IDL> .run select_bda
; INPUT:
;
; OUTPUT:
;
; OPTIONAL OUTPUT:
;
; HISTORY
;	RDB	Oct 1991	Written, with enhancements
;	RDB	16-Feb-1992	Updated for multiple files
;	rdb	26-Feb-1992	Some tidying; added YOHKOH_EVENTS stuff
;				Reads selected data if required
;	rdb	20-Mar-92	Null out PLOTR array (to stop confusion 
;				if set in TEST_RD)
;	rdb	17-Jul-92	Changed DSET_ARR to DSET_STR
;
;-
;;;         start day of plot
;;int2ex,dp_sync(hr_start_idx).time,dp_sync(hr_start_idx).day,tarr
;;sstm =  fmt_tim(tarr,sday,stime)
;         if day has changed before start of plot, use show
;         hours on the new day.
;;if dp_sync(hr_start_idx).day-dp_sync(0).day eq 1 then begin
;;        ttime = ttime-24
;;        !x.range = [hr_start-24,hr_stop-24] & x_style = 1
;;        endif

;;days = roadmap.day
;;alldays = all_vals(days)
;;if n_elements(alldays gt 1) then qday = where(days ne alldays(0))
;;ttime(qday) = ttime

!p.multi=0

;	  time of data, allowing for day rollover
t0 = fheader.first_time
t1 = fheader.last_time + (fheader.last_day - fheader.first_day)*86400L*1000L
print,' '
print,'Hour range of file is ',t0/3600./1000.,t1/3600./1000.
print,' '

print,'Time is in UT relative to the start of the first day in the file'
;	  determine time range of file
x0 = fheader.first_time/3600./1000.
x1 = fheader.last_time/3600./1000.
x1 = x1 + (fheader.last_day - fheader.first_day)*24.
f_hr_range = [x0,x1]

ans = ''

if n_elements(x_hr_range) ne 2 then begin
   print,'*** A time range is required and none is specified (X_HR_RANGE) ***'
   print,'** X_HR_RANGE set to start and stop time of file **'
   x_hr_range = f_hr_range
   print,x_hr_range
endif else begin
   print,'** Times specified in X_HR_RANGE is: ',x_hr_range
endelse

read,'* Do you want to use these times?: ',ans
ans = strupcase(strmid(ans,0,1))
if ans ne 'Y' then begin
   print,' '
;  print,'Hour range is ',min(ttime),max(ttime)
   read,'* Enter start and stop times: ',hr_start,hr_stop
   x_hr_range = [hr_start,hr_stop]
   plotr = 0		;null array set by PLOT_LCUR
   print,' '
endif

print,' '
print,'Plot will be of the Ca XIX light Curve'
print,' '

if !d.name ne 'PS' then begin
   read,'* Use Cursor? [Def=N]: ',ans
   ans = strupcase(strmid(ans,0,1))
   if ans ne 'Y' then ans = 'N'
endif

ansy = ''
read,'* Plot Yohkoh Event times [Def=N]: ',ansy
ansy = strupcase(strmid(ansy,0,1))
if ansy ne 'Y' then ansy = 'N'

;	  get night/day and saa cycles
if ansy eq 'Y' then  $
 	yohkoh_events,all_vals(roadmap.day),tonit,today,tesaa,txsaa,/hours

ttime = roadmap.time/3600./1000. + (roadmap.day - roadmap(0).day)*24.

!x.range = x_hr_range

qq = where(((roadmap.blockid eq 0) or (roadmap.blockid eq 1)) and 	$
	(ttime ge !x.range(0)) and (ttime le !x.range(1)))
if qq(0) eq -1 then begin
   print,'**** No BDA ROADMAP data in the time range requested ****'
   goto, endit
endif

integ_time = roadmap.dgi*0.125;*(roadmap.controltally+1)		;******

;		work out maximum of the plot
cymax = max(roadmap(qq).total_cnts(2)*10./integ_time(qq))
print,'Maximum value of light curve is:',cymax
if cymax gt 2000 then read,'* Enter cymax: ',cymax
ymax = (cymax +35)/25*25 * 1.2

int2ex,roadmap(qq(0)).time,roadmap(qq(0)).day ,tarr
dstr = strmid(fmt_tim(tarr),0,9)			;????????

;		now plot it
;;utplot,roadmap(qq),roadmap(qq).total_cnts(2)*10./integ_time(qq), $ ;(roadmap(qq).dgi*0.125),	$
plot,ttime(qq),roadmap(qq).total_cnts(2)*10./integ_time(qq),  $;(roadmap(qq).dgi*0.125),	$
	yrange=[0,ymax], ystyle=1, psym=10,  $
	xtitle='UT on '+dstr,title='Ca XIX  Total_counts Lightcurve',		$
	ytitle='Counts/sec'	;DGI'	;,xrange=x_hr_range

;		plot Night and SAA if information available
if ansy eq 'Y' then begin
   if max([tesaa]) gt -1 then for j=0,n_elements(tesaa)-1 do  $
      oplot,[tesaa(j),txsaa(j)],[0.90*ymax,0.90*ymax],thick=3
   xpp = (!x.crange(1)-!x.crange(0))*0.1
   xyouts,!x.crange(0)-xpp,0.9*ymax,'SAA'
   if max([tonit]) gt -1 then for j=0,n_elements(tonit)-1 do  $
      oplot,[tonit(j),today(j)],[0.95*ymax,0.95*ymax],thick=4
   xyouts,!x.crange(0)-xpp,0.95*ymax,'NIGHT'
endif

;		print the filename
;break_file,infil(0),disk_log,dir,filnam,ext,fver
xyouts,0.9,0.05,filnam+ext,/device,siz=1.2

ps_reset
if !d.name ne 'PS' then begin
   if ans eq 'Y' then begin
      print,' '
      print,'** Set Cursor to required region on Light Curve'
      get_limits,xx,yy        ;limits from time plot
      lim_region = where((ttime ge xx(0)) and (ttime le xx(1)))
      lll = where(qq(0) gt lim_region)	;***
      lll = max(lll) & lll = lll(0)+1
      ista = min(lim_region(lll:*)) & ista = ista(0)
      istp = max(lim_region) & istp = istp(0)
      print,'ista, istp =',ista,istp
  
;	  create search array for TEST_RD to use 
;***
;***   Needs better selection criteria!!!!!!!!!!!!!!!!!!!!!!!!

      ss = indgen(istp-ista+1) + ista
;      print,'** Use Test Read to read required data (option = -99) **'
      print,' '
      Print,'** SS vector has been generated **'
      ansr = ''
      read,'* Read the data from the BDA file [Def=N]: ',ansr
      ansr = strupcase(strmid(ansr,0,1))
      if ansr eq 'Y' then begin
         dset_str = ss2dset(ss)
         rd_bda,infil,dset_str,index,data,roadmap,dset_str=dset_str
      endif
   endif
endif

;;jdx = tim2dset(roadmap,dp_sync(iidx_sta:iidx_stp))

endit:
end
