


function eis_mapper_mdi_map, ttime, quiet=quiet, $
                             outdir=outdir, no_delete=no_delete, $
                             local_file=local_file

;+
; NAME:
;     EIS_MAPPER_MDI_MAP
;
; PURPOSE:
;     Return a full-disk magnetogram from MDI that is closest to the
;     input time. The image is returned as an IDL map.
;
; CATEGORY:
;     SOHO/MDI; file access.
;
; CALLING SEQUENCE:
;     EIS_MAPPER_MDI_MAP, Time
;
; INPUTS:
;     Ttime:  Time for which image is required. Can be in any format
;             accepted by the ANYTIM suite of routines. The routine
;             will search +/- 2 days either side of input time.
;
; OPTIONAL INPUTS:
;     Outdir: Specify a directory where the file will be downloaded
;             to. If not set, then it will go to $IDL_TMPDIR.
;
; 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 MDI magnetogram. If a problem
;     is found, then a value of -1 is returned.
;
; OPTIONAL OUTPUTS:
;     Local_File: The name of the file that is downloaded. Only set if
;     /no_delete given.
;
; EXAMPLE:
;     IDL> map=eis_mapper_mdi_map('15-feb-2011 02:00')
;     IDL> map=eis_mapper_mdi_map('15-feb-2011 02:00',/no_delete,local_file=local_file)
;
; MODIFICATION HISTORY:
;     Ver.1, 23-Mar-2020, Peter Young
;-


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

IF n_elements(outdir) EQ 0 THEN outdir=getenv('IDL_TMPDIR')

junk=temporary(map)

;
; Check for +/- 2 days each side of the requested date.
;
date=anytim2utc(/ccsds,ttime)
t_tai=anytim2tai(ttime)
t_tai0=t_tai-2*86400d
t_tai1=t_tai+2*86400d
ccsds0=anytim2utc(t_tai0,/ccsds)
ccsds1=anytim2utc(t_tai1,/ccsds)


;
; I reduce the list to only include SDAC files and only full-disk
; images. Note that I have to do this after the search as the relevant
; keywords didn't seem to work.
;
list=vso_search(ccsds0,ccsds1,inst='mdi',physobs='los_magnetic_field', $
                count=n,quiet=quiet)
k=where(list.provider EQ 'SDAC' AND list.extent.type EQ 'FULLDISK',nk)
IF nk EQ 0 THEN BEGIN
  print,'% EIS_MAPPER_MDI_MAP: no file found for this date. Returning...'
  return,-1
ENDIF 

list=list[k]

;
; Find the nearest-in-time file and download
;
list_tai=anytim2tai(list.time.start)
getmin=min(abs(list_tai-t_tai),imin)
a=vso_get(list[imin],out_dir=outdir,local_file=local_file,quiet=quiet)

;
; Here I check if the downloaded file is a tar file, or a fits
; file. (It seems files downloaded from Stanford are tar files, but SDAC
; files are fits.)
;
chck=strpos(local_file,'.tar')
IF chck GE 0 THEN BEGIN 
  file_untar,local_file,files=files,/verbose
  n=n_elements(files)
  FOR i=0,n-1 DO BEGIN
    chck=strpos(file_basename(files[i]),'.fits')
    IF chck GT 0 THEN fits2map,files[i],map
  ENDFOR
  IF NOT keyword_set(no_delete) THEN file_delete,local_file,files
ENDIF ELSE BEGIN
  fits2map,local_file,map
  IF NOT keyword_set(no_delete) THEN file_delete,local_file
ENDELSE 

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_MDI_MAP: file downloaded to '+local_file
ENDIF 

return,map

END

