pro annotate_flareloc, gev, tref

message,'Plotting flare location',/cont
newxy = fltarr(2,n_elements(gev))

for j=0,n_elements(gev)-1 do begin
   tt = gev(j).time_start
   print, tt, gev(j).nar, gev(j).longitude,gev(j).latitude, format='(a,i8,2f9.3)'
   newxy(*,j) = conv_h2a([gev(j).longitude,gev(j).latitude],tt)
   newxy(*,j) = rot_xy(newxy(0,j), newxy(1,j), tstart=tt, tend=tref)
endfor
  
circle_sym,symsize=0.8,/fill ;,thick=1       ;make psym=8 a filled circle
linecolor,1,'turquoise'
oplot, newxy(0,*),newxy(1,*), psym=8,color=1		;was psym=4

end


pro annotate_bkgimg, info, nar=nar

map = *info.map
if datatype(map) eq 'STC' then rtime = map.time $
  else rtime = info.sel_trange(0)

;       label NOAA active regions and show flare locations (on some plots)
print,'* Annotating plot'
;do_nar=keyword_set(nar)
;if not limb_only and do_nar then begin

if not info.network then begin

;	use NAR and GEV data stored in the $SSWDB tree

  if have_proc('get_nar') and keyword_set(nar) then begin
;    help,/st,map
;;    rtime = map.time
    nar=call_function('get_nar',rtime,quiet=quiet,count=count,/nearest)

;	if interval spans day boundary, need to select only one...
    if count gt 0 then begin
      nartimes = fmt_tim(nar)  &  vnartimes = all_vals(nartimes)
      if n_elements(vnartimes) gt 1 then begin
        qv = where(nartimes eq vnartimes(0))
        nar = nar(qv)
      endif
      nar=call_function('drot_nar',nar,rtime,count=count)
    endif
    if count gt 0 then oplot_nar,nar,charsize=1.0,off=[100,100]
  endif

  if have_proc('get_gev') then begin
    gev=get_gev(info.sel_trange(0),info.sel_trange(1),count=count)
    if count gt 0 then begin
      help,gev
;     help,/st,gev
      ss = where(gev.location(0) gt -900 and gev.location(1) gt -900)
      print,'Valid coords.:',ss
      if ss(0) ge 0 then begin
        new = gev(ss)
        tt = fmt_tim(anytim(new,/ints))

  temp = {time_start:'', latitude:0.0, longitude:0.0, nar:0L}
  flares = replicate(temp, n_elements(new))
  flares.time_start = tt
  flares.longitude = new.location(0)
  flares.latitude = new.location(1)
  flares.nar = long(new.noaa)
;  help,flares & help,/st,flares
  annotate_flareloc, flares, rtime

      endif
    endif
  endif

endif else begin

;	get flare data from the SEC Server

  query = form_sec_query(timerange=info.trange) 	; sel_trange?
  if query ne info.sec_query then begin
    message, '>>>> Get data from SEC',/cont
    flares = decode_votable(egso_query_sec(query),/quiet) ;, verb=verb), quiet=quiet)
    help,flares & help,/st,flares
;	save flare array and *** timer range used for request ***?
    *info.sec_records = flares
    info.sec_query = query
  endif else message, '>>>> Data from SEC already copied',/cont


;	??? check if events have a location; class greater than C1.0 !!!
  if have_tag(*info.sec_records, 'nar') then begin
    flares = *info.sec_records
    qv = where(flares.latitude ne 0.0 and flares.longitude ne 0.0) 
    if qv(0) ge 0 then annotate_flareloc, flares(qv), rtime      ;map.time
  endif

endelse

;endif

end


;***************************************************************
;	Plot image, all/selected FOV and time series
;***************************************************************

;---------------------------------------------------------------
pro plot_sswfov_bgimage,info,limb_only=limb_only,nar=nar,xsize=xsize,ysize=ysize

;print,datatype(*info.map)
;help,info.bkgimg_type,info.bkgimg_used

newimg = 0
if info.bkgimg_type ne info.bkgimg_used then newimg=1
;;;if datatype(*info.map) ne 'STC' then newimg=1
if not valid_map(*info.map) then newimg=1

;help,newimg
if newimg then begin
  print,'* Reading new image: ',info.bkgimg_type
  map = get_bgimage(info.trange(0), context=info.bkgimg_type, remote=info.network)	;do_log=do_log,
  *info.map = map 		;& info.do_log = do_log
  info.bkgimg_used = info.bkgimg_type
endif else map = *info.map

limb_only=0b
if not valid_map(map) then limb_only=1b

;;xsize=[-22.,22.] & ysize=[-22.,22.]
xsize=[-20.,20.]*60  &  ysize=[-20.,20.]*60

if not limb_only then begin

;	plot the image

  title=map.id + '!C' + strmid(map.time,0,20)
  title = str_replace(title,'Rocket Science','SOHO')

  loadct,0 & do_log=0
  if strpos(title,'SOHO EIT') ge 0 then begin
    eit_wave = fix(strmid(title, strpos(title,'SOHO EIT ')+9,3))
    help, eit_wave
    eit_colors, eit_wave
    do_log=1
  endif

  pmm, map.data
  plot_map,map, title=title, $
      xtitle='Solar EW (arc sec)', ytitle='Solar NS (arc sec)',       $
      xrange=xsize, yrange=ysize, /square, xstyle=1, ystyle=1,        $
      log=do_log, rotate=0, grid=grid, margin=0.1,               $
      _extra=extra, dmin=5

endif else begin

;       outline solar limb -  Stonyhurst Grid with appropriate B angle?

  PlotPosition=get_aspect(margin=0.1)     ;+.02           ;shifted square
  if info.debug then print,'Aspect Pos.:',PlotPosition

  plot,[0,1],[0,1],psym=1,/nodata,                                    $
      title='No Image Selected/Available',                            $
      xtitle='Solar EW (arc sec)', ytitle='Solar NS (arc sec)',       $
      xrange=xsize, yrange=ysize, xstyle=1, ystyle=1,                 $
      position=PlotPosition
  rr = get_rb0p(info.trange)
  rad = total(rr(0,*)) / n_elements(info.trange)        ;average radius
  theta = 2.*!pi*findgen(400)/399.
  oplot, rad*cos(theta), rad*sin(theta)

endelse

;       label NOAA active regions and show flare locations (on some plots)
do_nar=keyword_set(nar)
;;if not limb_only and do_nar then begin

annotate_bkgimg, info, nar=nar

;;endif

if info.debug then begin                ;test cross on "image"
   oplot, [-1000,1000], [-1000,1000]
   oplot, [-1000,1000], [1000,-1000]
endif

end
