

FUNCTION eis_mapper_aia_map, ttime, wave, local_file=local_file, quiet=quiet, $
                      no_delete=no_delete, outdir=outdir

;+
; NAME:
;      AIA_GET_SYNOPTIC
;
; PURPOSE:
;      For a specified time and wavelength retrieve the AIA synoptic
;      file from the JSOC that is nearest in time to the requested
;      time on the specified day.
;
; CATEGORY:
;      SDO/AIA; file access.
;
; CALLING SEQUENCE:
;      Result = EIS_MAPPER_AIA_MAP( Time, Wave )
;
; INPUTS:
;      Ttime:  Time for which image is required. Can be in any format
;              accepted by the ANYTIM suite of routines.
;      Wave:   AIA wavelength (integer).
;
; 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 AIA image. If a problem
;      is found, then a value of -1 is returned. Note the downloaded
;      FITS file is deleted after it has been read into IDL. To stop
;      this, use /no_delete.
;
; OPTIONAL OUTPUTS:
;     Local_File: The name of the file that is downloaded. Only set if
;     /no_delete given.
;
; RESTRICTIONS:
;      Will only search for files on the specified day. Thus if you
;      search for a file at 01:00 and the nearest files are 23:59 (day
;      before) and 22:00, then it will return the file at 22:00.
;
; EXAMPLES:
;      IDL> aia_get_synoptic, '15-feb-2011', 193
;
; MODIFICATION HISTORY:
;      Ver.1, 1-Apr-2013, Peter Young.
;      Ver.2, 3-Jun-2013, Peter Young
;        corrected problem for 94 images
;      Ver.3, 28-Aug-2013, Peter Young
;        added /DELETE and MAP= keywords
;      Ver.4, 16-Aug-2018, Peter Young
;        added check in case sock_get fails; updated header.
;      Ver.5, 22-Aug-2018, Peter Young
;        now uses /use_shared_lib when calling read_sdo; now downloads
;        file to $IDL_TMPDIR.
;      Ver.6, 17-Mar-2020, Peter Young
;        now checks all the hours in the day to search for the nearest
;        file (sometimes there are hours-long gaps).
;      Ver.7, 24-Mar-2020, Peter Young
;        changed name to eis_mapper_aia_map; updated header; added
;        outdir= optional input; changed /delete to /no_delete.
;-


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

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


;
; delete local_file in case it has previously been defined.
;
junk=temporary(local_file)

webroot='http://jsoc.stanford.edu/data/aia/synoptic'

;
; Create the URL that contains the synoptic file.
;
extime=anytim2utc(ttime,/ex)
yy=trim(extime.year)
mm=strpad(trim(extime.month),2,fill='0')
dd=strpad(trim(extime.day),2,fill='0')

wavestr=strpad(trim(wave),4,fill='0')

;
; The following goes through the hours of the day checking for the
; nearest file. (There must be a better way of doing this!)
;
swtch=0
h=extime.hour
i=0
WHILE swtch EQ 0 AND i LE 48 DO BEGIN
  h=h+(-1)^i*i
  IF h GE 0 AND h LE 23 THEN BEGIN 
    hh='H'+strpad(trim(h),2,fill='0')+'00'
    IF NOT keyword_set(quiet) THEN print,'Checking '+hh+'...'
    date_dir=yy+'/'+mm+'/'+dd+'/'+hh
    search_dir=webroot+'/'+date_dir
    sock_dir,search_dir,files
    IF files[0] EQ '' THEN BEGIN
      swtch=2
    ENDIF ELSE BEGIN 
      wavestr=strpad(trim(wave),4,fill='0')
      chck=strpos(files,'_'+wavestr+'.fits')
      k=where(chck GE 0,nk)
      IF nk GT 0 THEN swtch=1
    ENDELSE 
  ENDIF 
  i=i+1
ENDWHILE 

IF swtch EQ 2 THEN return,-1

files=files[k]


;
; Extract the times for each file and convert to TAI
;
files_base=file_basename(files)
hhmm=strmid(files_base,12,2)+':'+strmid(files_base,14,2)
t_day=anytim2utc(ttime,/ccsds,/date)
file_times=t_day+' '+hhmm
files_tai=anytim2tai(file_times)

;
; Find file nearest in time to the requested time
;
t_tai=anytim2tai(ttime)
getmin=min(abs(t_tai-files_tai),imin)


;
; Retrieve the synoptic file. If there's a problem (status=0)
; then I try the call again.
;
sock_get,files[imin],local_file=local_file,status=status,out_dir=outdir
IF status EQ 0 THEN sock_get,files[imin],local_file=local_file,status=status,out_dir=outdir

;
; status=0 if there's a problem
;
IF status NE 0 THEN BEGIN 
  read_sdo,local_file,index,data,/use_shared_lib
  index2map,index,data,map
 ;
  IF not keyword_set(no_delete) THEN file_delete,local_file
 ;
  IF NOT keyword_set(quiet) AND local_file NE '' AND keyword_set(no_delete) THEN BEGIN 
    print,'The following file has been downloaded:'
    print,'  ',trim(local_file)
  ENDIF
  return,map
ENDIF ELSE BEGIN
  return,-1
ENDELSE 


END
