;+
; NAME:
;	smei_filepath
; PURPOSE:
;	Construct path to SMEI file (frame or skymap)
; CATEGORY:
;	camera/idl/frm
; CALLING SEQUENCE:
	FUNCTION smei_filepath, tf, $
		base	= base	, $
		source	= source, $
		camera	= camera, $
		mode	= mode	, $
		postfix	= postfix,$
		type	= type	, $
		upto	= upto	, $
		full	= full
; INPUTS:
;	tf				undefined; scalar, or array[1]
;						if defined then either a time (in the form of a standard
;						time structure), or the name of a SMEI frame or skymap
;						(e.g. 'c2m0_2003_123_456789')
;						NOTE: if tf is NOT defined than keyword source MUST be
;						set to an existing directory.
; OPTIONAL INPUT PARAMETERS:
;	source=source	scalar; type: string; default: SMEIDB?
;						directory where to look for SMEI frames.
;						If the directory does not exist then the current SMEI
;						drive is used. The default value SMEIDB? will result
;						in an automatic determination of a source directory
;						using the information in file smei_drives.txt.
;
;	The SMEI frames are stored in directories of the form
;	$SMEIDB#/yyyy_doy/hh. By default the name of the full directory
;	where the frames for time 'tf' are located is returned.
;
;	/base			if set only the part $SMEIDB# is returned			
;	/day			if set only the part $SMEIDB#/yyyy_doy is returned
;
;	mode			scalar; or array[2]
;						mode[0] is used in construction of directory
;						(e.g. $SMEIDB/orb for orbital patterns (mode='orb')
;						mode[1] is used in construction of filename (/full set)
;						(e.g. mode=['orb_003','orb'] results in
;						$SMEIDB/orb_003/c3orb_2000_001_000000.fts.gz)	
; OUTPUTS:
;	ff				scalar; type: string
;						directory where frame(s) for specified time are located
; INCLUDE:
	@compile_opt.pro		; On error, return to caller
; COMMON BLOCKS:
	common smei_filepath_save, checkdrives, sdat
; CALLS:
;	InitVar, IsType, IsTime, TimeUnit, CheckDir, flt_read
;	TimeSet, TimeGet, TimeOp, destroyvar, who_am_i, smei_filename
; PROCEDURE:
;	The SMEI data are distributed across several hard drives.
;	Env variables SMEIDB1, SMEIDB2, etc. exist pointing to the mount
;	points of each drive. Each drive contains data for the time range
;	specified in the file 'smei_drives.txt'.
;
;	SMEI data are organized in separate folders for each day. E.g. the
;	folder $SMEIDB1/2003_100/c2 contains all SMEI frames
;	day of year 100 (in year 2003) for camera 2.
;
;	If tf is NOT defined then the YYYY_DOY part in the returned path
;	will be missing. This is useful to locate skymaps (which usually are
;	split up by camera only, not by time).
; MODIFICATION HISTORY:
;	JUN-2003, Paul Hick (UCSD/CASS)
;		Introduced to deal with multiple hard drives containing
;		SMEI data frames.
;	MAY-2004, Paul Hick (UCSD/CASS)
;		Modified to reflect new directory structure of SMEI data base
;		(frames are now organized in a separate directory for each
;		doy of year, with subdirectories for each of the cameras)
;	AUG-2006, Paul Hick (UCSD/CASS)
;		Modified to do something useful when tf is not defined.
;	FEB-2007, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;		Generalized to work with data other than CCD frames
;		(skymaps, hdr and png files)
;	MAR-2009, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;		Added 'cal', 'orb', 'orb_003', 'sts' to mode options.
;'		'mode' can now be a 2-element array with mode[0] used
;		in the directory name and mode[1] in the filename
;		(if /full is set)
;-

InitVar, full, /key
InitVar, base, /key

uday = TimeUnit(/day)

CASE 1 OF
IsTime(tf		): tt = tf
IsTime(tf, /orbit	): tt = TimeSet(smei=tf)
IsType(tf, /generic_int	): tt = TimeSet(smei=tf)
IsType(tf, /string): BEGIN
	tt = smei_filename(tf,camera=camera_,mode=mode_)
	InitVar, camera, camera_
	InitVar, mode  , mode_
END
ELSE:						; tf and tt undefined
ENDCASE

CASE IsType(mode, /string) OF

0: BEGIN						; SMEI CCD frame

	InitVar, day   , /key
	InitVar, source, 'SMEIDB?'
	InitVar, checkdrives, 1		; Initialize common block


	CASE source EQ 'SMEIDB?' OR source EQ 'SMEIDC?' OF

	0: IF CheckDir(source) THEN srce = source

	1: BEGIN 
		IF checkdrives THEN BEGIN
			CASE flt_read(filepath(root=who_am_i(/dir), 'smei_drives.txt'), sdat, error=error, silent=2) OF
			0: destroyvar, sdat
			1: sdat = round(sdat)
			ENDCASE
			checkdrives = 0		; We check for smei_drives.txt only once
		ENDIF

		IF IsType(sdat, /defined) AND IsType(tt, /defined) THEN BEGIN
			tdir = TimeSet(yr=sdat[[1,3],*],doy=sdat[[2,4],*])
			tdir = (where(0 LE TimeOp(/subtract,tt,tdir[0,*], uday) AND	$
				 			   TimeOp(/subtract,tt,tdir[1,*], uday) LT 0))[0]
			IF tdir NE -1 THEN	$
				srce = getenv(strmid(source,0,strlen(source)-1)+strcompress(sdat[0,tdir],/rem))
				;srce = getenv('SMEIDB'+strcompress(sdat[0,tdir],/rem))
		ENDIF
	END

	ENDCASE

	; If srce is not defined at this point then pick the env var pointing
	; to the current SMEI drive.

	IF IsType(srce, /undefined) THEN BEGIN
		i = 7 
		WHILE getenv('SMEIDB'+strcompress(i,/rem)) NE '' DO i++
		srce = getenv('SMEIDB'+strcompress(i-1,/rem))
	ENDIF

	IF NOT base THEN BEGIN

		IF IsTime(tt) THEN srce = filepath(root=srce, TimeGet( TimeGet(tt,/botime,uday), /_ydoy, upto=uday, /scalar))

		IF IsType(camera, /defined) THEN BEGIN
			srce = filepath(root=srce, 'c'+strcompress(camera,/rem))
			IF IsType(postfix,/defined) THEN srce += postfix
		ENDIF

	ENDIF

END

1: BEGIN

	InitVar, source, 'SMEIDB?', set=srce
	IF srce EQ 'SMEIDB?' THEN BEGIN
		CASE mode[0] OF
		'sid'	 : srce = getenv('SSWDB_SMEI')
		'cal'	 : srce = getenv('SSWDB_SMEI')
		'msk'	 : srce = getenv('SSWDB_SMEI')
		'min'	 : srce = getenv('SSWDB_SMEI')
		'min_003': srce = getenv('SSWDB_SMEI')
		'orb'	 : srce = getenv('SSWDB_SMEI')
		'orb_003': srce = getenv('SSWDB_SMEI')
		'c3mask' : srce = getenv('SSWDB_SMEI')
		'cat'	 : srce = getenv('SSWDB_SMEI')
		'hdr'	 : srce = filepath(root=getenv('SSWDB_SMEI'),'cat')
		'png'	 : srce = filepath(root=getenv('SSWDB_SMEI'),'cat')
		'sts'	 : srce = filepath(root=getenv('SSWDB_SMEI'),'cat')
		'list'	 : srce = filepath(root=getenv('SSWDB_SMEI'),'cat')
		ELSE 	 : srce = getenv('SMEISKY0')
		ENDCASE
	ENDIF

	srce = filepath(root=srce, mode[0])

	IF NOT base AND IsType(camera,/defined) THEN BEGIN
		srce = filepath(root=srce, 'c'+strcompress(camera,/rem))
		IF IsType(postfix,/defined) THEN srce += postfix
	ENDIF

END

ENDCASE

IF full THEN BEGIN
	IF IsTime(tt) THEN BEGIN
		srce = filepath(root=srce, smei_filename(tt, camera=camera, mode=mode[n_elements(mode)-1], type=type, upto=upto))
	ENDIF
ENDIF

RETURN, srce  &  END
