
pro read_lyra,yyyymmdd,lyjd,ly1,ly2,ly3,ly4, GOES_jd,GOES_flux, level1 = level1, level2 = level2, level3 = level3, 	$
	filename = filename, lyradir = lyradir, Get_data = get_data, OutputDirectory = OutputDirectory, $
	get_Goes=get_Goes
;
; PURPOSE: Read observed LYRA time series. Download the data if requested.
; 
; USE (example):
;
;	To read a level3 (or 2 or 1) fitsfile for 1st May 2010 that has already been downloaded: 
;
;		read_lyra,20100501,lyjd,ly1,ly2,ly3,ly4,/LEVEL3,filename='~/DATA/LYRA/lyra_20100501-000000_lev3_std.fits'
;		read_lyra,'20100501',lyjd,ly1,ly2,ly3,ly4,/LEVEL3,lyradir='~/DATA/LYRA/' (the program will find for the corrsponding file)
;	
;	  To download the data and then read them:
;
;		read_lyra,20100501,lyjd,ly1,ly2,ly3,ly4,/LEVEL3,/GET_DATA ; by default will copy the data in your directory
;		read_lyra,20100501,lyjd,ly1,ly2,ly3,ly4,/LEVEL3,/GET_DATA,OUTPUTDIRECTORY='/home/user/LYRA/' ; will copy the data in '/home/user/LYRA/' 
;					;;;;; NB: OUTPUTDIRECTORY must be a full explicit path (e.g. does not accept alias like '~' or '$HOME' for example)
;
;
; INPUT: 
;	YYYYMMDD: day for which you want the data.
;
; OUTPUT: 
;	LYJD: time in julian day
;	LY1: LYRA channel1 (Ly-alpha), in Count/s (frequency) if level1 is selectec, in W/m^2 for level2 and level3
;	LY2: LYRA channel2 (200-220nm), in Count/s (frequency) if level1 is selectec, in W/m^2 for level2 and level3
;	LY3: LYRA channel3 (17-70nm+SXR), in Count/s (frequency) if level1 is selectec, in W/m^2 for level2 and level3
;	LY4: LYRA channel4 (6-20nm + SXR), in Count/s (frequency) if level1 is selectec, in W/m^2 for level2 and level3
;   GOES_jd: Goes time. Only if keyword GET_GOES is set
;   GOES_flux: Goes flux. Only if keyword GET_GOES is set
;
; KEYWORDS:
;	LEVEL1: request for level1 data measurements for that day. Exclusive with LEVEL2, LEVEL3, and META
;	LEVEL2: request for level1 data measurements for that day. Exclusive with LEVEL1, LEVEL3, and META
;	LEVEL3: request for level1 data measurements for that day. Exclusive with LEVEL2, LEVEL1, and META (DEFAULT)
;	GET_DATA: dowload the associated fitsfile
;	OUTPUTDIRECTORY: Where to write the file is GET_DATA is set. NB: REQUIRE FULL EXPLICIT PATH (e.g. does not accept alias like '~' or '$HOME' for example). Default is current directory
;	FILENAME: path+filename all together of the fitsfile to read if already downloaded. IF GET_DATA is set, it returns the path+filename of the downloaded file.
;	LYRADIR: directory where to search for the file if already downloaded. Default is current directory
;	GET_GOES: if set, then return a structure 		
;	
; DEPENDANCE:
;	dl_lyradata.pro
;
; HISTORY:
;	V1. M. Kretzschmar & I.E. Dammasch  (SIDC/ROB) - Dec 2011
;	V1.1 M. Kretzschmar (SIDC/ROB) Add possibility to add goes data.
;
;			Please report bugs and send comment and suggestions to <swap_lyra@oma.be>

on_error,2

;
; -- Keywords checking
;
IF keyword_set(level1) + keyword_set(level2) + keyword_set(level3) gt 1 then begin
	Print,'%READ_LYRA: please specify only one kind of data (level1,2,3, or meta). Level3 is default. Returning'
	Return
ENDIF
IF keyword_set(level1) + keyword_set(level2) + keyword_set(level3) eq 0 then begin
	Print,'%DL_LYRADATA: no data level specified. Will retrieve level3 data.'
ENDIF
;
level='3'
IF keyword_set(level1) then level='1'
IF keyword_set(level2) then level='2'
;
;
IF KEYWORD_SET(GET_DATA) THEN BEGIN		;;;; download the data is asked
	dl_lyradata,yyyymmdd,level1=level1,level2=level2,level3=level3,meta=meta,OutputDirectory=OutputDirectory,filename=filename
	filename=OutputDirectory+'/'+filename
	;
	; - save for double '/'
	;
	oo=strpos(filename,'//')
	if oo[0] ge 0 then begin
	  	strspl=strsplit(filename,'//',/extract)
	  	nn=n_elements(strspl)    	
	  	if strmid(filename,0,1) eq '/' then filename='/'+strspl[0] else filename=strspl[0]
	  	for k=1,nn-1 do filename=filename+'/'+strspl[k]
	endif
	filen=filename
ENDIF ELSE BEGIN
	;
	yyyymmdd=string(yyyymmdd,format='(i8)')
	;
	;
	; -- Adjust filename
	;
	if NOT KEYWORD_SET(LYRADIR) THEN cd,current=lyradir
	;
	IF NOT KEYWORD_SET(FILENAME) THEN BEGIN
		year  = STRMID(yyyymmdd, 0, 4)
		month = STRMID(yyyymmdd, 4, 2)
		day   = STRMID(yyyymmdd, 6, 2)
		;
		case level of
			'1': filePattern = 'lyra_'+year+month+day+'-??????_lev1_std.fits'
			'2': filePattern = 'lyra_'+year+month+day+'-??????_lev2_std.fits'
			'3': filePattern = 'lyra_'+year+month+day+'-??????_lev3_std.fits'
			;'meta': filePattern = 'lyra_'+year+month+day+'-??????_lev1_met.fits'
		endcase
		;
		len=strlen(lyradir)
		if len ge 1 then begin
			if strmid(lyradir,len-1,1) eq '/' then  filen=lyradir+filepattern else filen=lyradir+'/'+filepattern
		endif else filen=filepattern
	;
	ENDIF ELSE FILEN=FILENAME
	;
ENDELSE
;
; -- Now read
; 
year  = STRMID(yyyymmdd, 0, 4)
month = STRMID(yyyymmdd, 4, 2)
day   = STRMID(yyyymmdd, 6, 2)
;
test=file_exist(filen) 
IF TEST eq 1 then begin
	info=file_info(filen)
	truefilen=info.name
	;
	;
	; --- now read the file
	;
				;fxread,fitsfilename,data,headerp
				;;result=fxpar(headerp,'DATE-OBS')
				;;fileday=strmid(result,0,10)
				;;day=strmid(result,8,2)
				;;mon=strmid(result,5,2)
				;
				ext1=readfits(truefilen,headerb,exten_no=1)
				;print,headerb
				length=fxpar(headerb,'NAXIS2')
				if (length eq 0) then begin
					print,'empty binary table in '+fitsfilename
					lyjd=-1 & ly1=lyjd & ly2=ly1 & ly3=ly1 & ly4=ly1
				endif
				TIME=tbget(headerb,ext1,'TIME')
				if LEVEL eq '3' THEN $
					LYJD=julday(long(month),long(day),long(year),0,0,0)+TIME/1440D ELSE $
							 LYJD=julday(long(month),long(day),long(year),0,0,0)+TIME/86400D
				LY1=tbget(headerb,ext1,'CHANNEL1')
				LY2=tbget(headerb,ext1,'CHANNEL2')
				LY3=tbget(headerb,ext1,'CHANNEL3')
				LY4=tbget(headerb,ext1,'CHANNEL4')
				;FREQ_QFACTOR=tbget(headerb,ext1,'QFACTOR') ;level1
				; IRRAD_WARNING=tbget(headerb,ext1,'WARNING') ;level2 & level3
	
ENDIF ELSE BEGIN ; no file exist
	print,'%READ_LYRADATA: cannot find the file '+filen+'. Returning'
	return
ENDELSE
;
; -- get goes data if requested
;
IF KEYWORD_SET(GET_GOES) THEN BEGIN
	;trange= jd2str(julday(month,day,year,0,0,0)+[0,86399]/86400d,/ccsds)
	mjd=julday(month,day,year,0,0,0)+[0,0] -2400000.5d
	time=[0,86399.9d3] ; sod in ms
	;
	 temp = {mjd: 0L, time: 0L}
 	temp = replicate(temp, n_elements(mjd))
 	temp.mjd = mjd
 	temp.time = time
 	utc = utc2int(temp)
 	utc = int2utc(utc,/ccsds)
	;
	rd_goes,tgoes,goes_flux,trange=utc
	;
	goes_jd=julday(1,1,1979,0,0,0)+tgoes/86400d
	;
ENDIF

;
;
;
END  
	




