
FUNCTION eis_mapper_xrt_map, ttime, local_file=local_file, no_delete=no_delete, quiet=quiet, $
                           outdir=outdir

;+
; NAME:
;      EIS_MAPPER_XRT_MAP
;
; PURPOSE:
;      Finds the XRT full-disk Ti-Poly image that is closest to the
;      specified time and loads it into an IDL map.
;
; CATEGORY:
;      Hinode/XRT; search; download.
;
; CALLING SEQUENCE:
;      Result = EIS_MAPPER_XRT_MAP ( Time )
;
; INPUTS:
;      Ttime:  A time for which an image is required. Specified in one
;              of the standard Solarsoft formats.
;
; KEYWORD PARAMETERS:
;     QUIET:  Do not print information to the screen.
;     NO_DELETE: If set, then the downloaded file is
;                not deleted.
;
; OUTPUTS:
;      An IDL map containing the XRT image. If a problem is found then
;      -1 is returned. 
;
; OPTIONAL OUTPUTS:
;      Local_File:  The full path specification of the downloaded
;                   file. Only set if /no_delete given.
;
; EXAMPLE:
;     IDL> map=eis_mapper_xrt_map('15-feb-2011 02:00')
;     IDL> map=eis_mapper_xrt_map('15-feb-2011 02:00',/no_delete,local_file=local_file)
;
; MODIFICATION HISTORY:
;      Ver.1, 28-Sep-2018, Peter Young
;      Ver.2, 23-Mar-2020, Peter Young
;        renamed eis_mapper_xrt_map; improved time search, extending
;        to +/- 1 day.
;      Ver.3, 2-Apr-2020, Peter Young
;        force file to be downloaded to $IDL_TMPDIR by temporarily
;        re-defining $HINODE_DATA; modified search to check on FOV
;        size and solar position, and also avoid dark frames.
;      Ver.4, 13-Jul-2020, Peter Young
;        Require exposure time to be >0.5 seconds.
;-

IF n_params() LT 1 THEN BEGIN
  print,'Use:  IDL> map = eis_mapper_xrt_map( time [, /quiet, map=, /no_delete, local_file=] )'
  return,-1
ENDIF 


date=anytim2utc(/vms,/date,ttime)
t_ccsds=anytim2utc(/ccsds,ttime)
t_tai=anytim2tai(ttime)
tstart_tai=t_tai-86400.
tstart=anytim2utc(/ccsds,tstart_tai)
tend_tai=t_tai+86400.
tend=anytim2utc(/ccsds,tend_tai)

;
; The osdc software will automatically try to download to $HINODE_DATA (if
; defined), even if the directory doesn't exist (thus causing a
; crash). To get around this I temporarily set HINODE_DATA to be the
; IDL temporary directory and then set it back again later. 
;
hinode_data=getenv('HINODE_DATA')
idl_tmpdir=getenv('IDL_TMPDIR')
setenv,'HINODE_DATA='+idl_tmpdir

;
; The following searches for full-disk (fov > 1800"),
; disk-center observations in Ti_poly (EC_FW2=2) that aren't
; dark frames. 
;
o=obj_new('osdc') 
o->limit,0
o->condition,'INSTRUME: ,XRT'
o->condition,'EPOCH_START: '+tstart
o->condition,'EPOCH_END: '+tend
o->condition,'SS__L1LEAD: y'
o->condition,'Gx: NONE'
o->condition,'FOVX: >1800'
o->condition,'FOVY: >1800'
o->condition,'XCEN: <100'
o->condition,'YCEN: <100'
o->condition,'EXPTIME: >0.5'
o->condition,'X__EC_FW2: ,2'
o->condition,'X__EC_IMTY_: normal'

o->search,out

IF n_tags(out) EQ 0 THEN BEGIN
  print,'% EIS_MAPPER_XRT_MAP: no files found for this day. Returning...'
  return,-1
ENDIF 

;
; Now find image closest to requested time.
; 
t=anytim2tai(out.date_obs)
getmin=min(abs(t_tai-t),imin)


;
; The following downloads to my $HINODE directory.
;
local_file =  o->paths(out[imin],/fetch)
obj_destroy,o

;
; Re-set $HINODE_DATA.
;
setenv,'HINODE_DATA='+hinode_data

;
; This is in case my data disk isn't plugged in!
;
dir=file_dirname(local_file)
chck=file_info(dir)
IF chck.exists EQ 0 THEN BEGIN
  dir=getenv('IDL_TMPDIR')
  local_file=concat_dir(dir,file_basename(local_file))
ENDIF


read_xrt,local_file,index,data
xrt_prep,index,data,oindex,odata
index2map,index,data,map

IF NOT keyword_set(no_delete) THEN file_delete,local_file

IF NOT keyword_set(no_delete) THEN junk=temporary(local_file)

IF keyword_set(no_delete) AND NOT keyword_set(quiet) THEN BEGIN
  print,'% EIS_MAPPER_XRT_MAP: file downloaded to '+local_file
ENDIF 

return,map

END
