;+
;NAME:
; phoenix_filedb_get
;
;PROJECT:
;  PHOENIX-2, ETHZ
;
;CATEGORY:
; Phoenix-2 filedb access
;
;PURPOSE:
; This routine , when given a time interval, will look into the proper Phoenix filedb fits files, and retrieve the information stored there.
; The default is to return the whole db structure, unless some keywords are set (see below).
;
;CALLING SEQUENCE:
;	dblist=phoenix_filedb_get(time_intv, POL=POL, QUIET=QUIET, START_TIME=START_TIME, ...)
;
;INPUT:
; 	time_intv: a 1-element or 2-element anytim-compatible time.
;
;OUTPUT:
; 	A structure with start_time, end_time, filename and type (I, L1, L2, or P) of each Phoenix fits file
;	If some keywords are set, only those elements are returned.
;
;KEYWORDS:
;	/START_TIME: if set, routine will return start_time of each file, instead of default output.
;	/END_TIME: if set, routine will return end_time of each file, instead of default output.
;	/FILENAME: if set, routine will return filename of each file, instead of default output.
;	/TYPE: if set, routine will return stype of each file, instead of default output.
;
;HISTORY:
; 	2003/10/07: Pascal Saint-Hilaire, shilaire@astro.phys.ethz.ch
;	
;-



; CHECKS in filedb.fits whether there are files corresponding to the input time_intv, and returns them. Otherwise, returns -1.
;
;
; EXAMPLE:
;	PRINT,phoenix_filedb_get('2002/02/26 10:26',/FILENAME)
;
;

FUNCTION phoenix_filedb_get, time_intv, POL=POL, QUIET=QUIET, 	$
		START_TIME=START_TIME		,$
		END_TIME=END_TIME		,$
		FILENAME=FILENAME 		,$
		TYPE=TYPE

	IF KEYWORD_SET(QUIET) THEN LOUD=0 ELSE LOUD=1
	IF KEYWORD_SET(POL) THEN addletter='p' ELSE addletter='i'
	any_intv=anytim(time_intv)
	IF N_ELEMENTS(any_intv) EQ 1 THEN any_intv=[any_intv,any_intv+0.001]
	IF any_intv[1] LT any_intv[0] THEN BEGIN
		PRINT,'......time intervals not in increasing order!'
		RETURN,-1
	ENDIF

	;filedb_dir='/ftp/pub/hedc/fs/data1/rapp_idl/TEMP/dbase'
	filedb_dir=GETENV('SSW')+'/radio/ethz/dbase/'

	;now, make a list of filedb_files, according to time_intv
	start_timex=anytim(any_intv[0],/EX)
	cur_ym=[start_timex[6],start_timex[5]]
	filedb_files=-1
	WHILE anytim([0,0,0,0,1,cur_ym[1],cur_ym[0]]) LE any_intv[1] DO BEGIN	
		IF datatype(filedb_files) EQ 'INT' THEN filedb_files=addletter+'_'+int2str(cur_ym[0],4)+int2str(cur_ym[1],2) ELSE filedb_files=[filedb_files,addletter+'_'+int2str(cur_ym[0],4)+int2str(cur_ym[1],2)]		
		cur_ym=cur_ym+[0,1]
		IF cur_ym[1] EQ 13 THEN cur_ym=[cur_ym[0]+1,1]
	ENDWHILE
		
	;IF datatype(filedb_files) EQ 'INT' THEN RETURN,-1 ; should not happen!
	;concatenate the stuff...
	filedb_files=filedb_dir+'/phoenix_filedb_'+filedb_files+'.fits'

	;now that I have a complete list of dbfiles to check for, read'em (at least, those that exist and are accessible !)!
	
	db=-1
	FOR i=0L, N_ELEMENTS(filedb_files)-1 DO BEGIN
		db1=mrdfits(filedb_files[i],1,status=status,/SILENT)
		IF status LT 0 THEN BEGIN
			IF LOUD THEN PRINT,' ..........problem opening/reading the file '+filedb_files[i]
			RETURN,-1
		ENDIF ELSE BEGIN
			IF datatype(db1) EQ 'STC' THEN BEGIN
				 IF datatype(db) EQ 'INT' THEN db=db1 ELSE db=[db,db1]
			ENDIF
		ENDELSE
	ENDFOR

	db=db[UNIQ(db.filename, SORT(db.start_time))]	; just in case it is not already properly sorted (it should be, I think)...
	ss=WHERE( (db.end_time GE any_intv[0]) AND (db.start_time LE any_intv[1]))
	IF ss[0] EQ -1 THEN BEGIN
		IF LOUD THEN PRINT,'........ no Phoenix fits files within time range!'
		RETURN,-1
	ENDIF

	res=db[ss]
	IF KEYWORD_SET(START_TIME) THEN res=db[ss].start_time
	IF KEYWORD_SET(END_TIME) THEN res=db[ss].end_time
	IF KEYWORD_SET(FILENAME) THEN res=STRTRIM(db[ss].filename,2)		;2005/08/16: Had to add strn() because of mrdfits/mwrfits screwup in writing arrays of structures containing strings...
	IF KEYWORD_SET(TYPE) THEN res=db[ss].type

	ss_warning=WHERE( (db[ss].type EQ 'L1') OR (db[ss].type EQ 'L2'))
	IF ss_warning[0] NE -1 THEN BEGIN
		IF LOUD THEN PRINT,'WARNING!: Using L1/L2 data !'
	ENDIF

	RETURN,res
END
