;+
; NAME
;          EIS_GET_AIA_JPG
;
; PURPOSE
;          This function gets a JPG image from the SDO website
;
; INPUTS
;          NONE
; KEYWORDS
;          date - the date (obviously)
;          wave - the desired AIA wavelength (171, 193, etc)
; CALLS
;          IDLnetUrl
; USE:
;          img = eis_get_aia_jpg([date='1-Jul-2013'])
; WRITTEN
;          John A Rainnie, RAL, 17-Dec-2013
;
; HISTORY
;          v0.1 JAR 17-Dec-2013
;               Blah
;-
;______________________________________________________________________________
PRO eis_get_aia_jpg , date = date , wave = wave

proxy = GETENV("http_proxy")


; Catch any errors generated by the IDLnetURL object
CATCH, errorStatus
IF (errorStatus NE 0) THEN BEGIN
   CATCH, /CANCEL

   ; Print the error msg
   PRINT, !ERROR_STATE.msg

   ; Get the properties that tell us more about
   ; the error and display at the IDL command line.
   ourl->GetProperty, RESPONSE_CODE = rspCode , $
      RESPONSE_HEADER = rspHdr, RESPONSE_FILENAME = rspFn

   print, 'Response Header Start++++++++++++++++++++++++'
   print, rspHdr
   print, 'Response Header End++++++++++++++++++++++++++'
   ;PRINT, 'rspCode = ', rspCode
   ;PRINT, 'rspFn= ', rspFn

   ; Destroy the url object and return.
   OBJ_DESTROY, ourl
   RETURN
ENDIF



; Instantiate an IDLnetUrl object
oUrl = OBJ_NEW('IDLnetUrl' , /VERBOSE)

; Set it's property to your proxy setup
oUrl->SetProperty, proxy_hostname = proxy
oUrl->SetProperty, callback_function = 'eis_get_aia_jpg_callback'

; Get deisred date and create a string of the form;
; yyyy/mm/dd/l0wav.jpg
; where wav = wavelength (171, 193, etc)
; eg: "2013/12/17/l0193.jpg"
IF ~KEYWORD_SET(wave) THEN wave = 193
; Convert wavelength to appropriate string, and suffix with "l0" and append
; with ".jpg"
wave_text = STRCOMPRESS(wave,/REMOVE_ALL)
wave_text = STRJOIN(['l0',wave_text,'.jpg'],/SINGLE)

IF ~KEYWORD_SET(date) THEN date = anytim2utc(systim(),/stime,/date)
; Convert date from dd-Mon-yyyy format to yyyy/mm/dd format
date = anytim2cal(date , form = 11,/date) ;-> '2013/12/17'
;date = 'hellotherehowsitgoing'


; Now concat date and wave_text strings
file_url = STRJOIN([date,wave_text],'/',/SINGLE)
base_url = 'http://sdowww.lmsal.com/sdomedia/SunInTime/'
full_url = STRJOIN([base_url,file_url],/SINGLE)
; eg: url = 'http://sdowww.lmsal.com/sdomedia/SunInTime/2013/12/17/l0193.jpg'

download = oUrl->get(filename = wave_text , url = full_url)

; Cleanup and kill object
OBJ_DESTROY , oUrl

END
