function gt2exe, input, addind=addind, gtfunctions=gtfunctions, $
   noss=noss, debug=debug
;+
;   Name: gt2exe
; 
;   Purpose: convert gt function shorthand to valid calls (for use w/execute)
;
;   Input Parameters:
;      input - string of form "<item> <relation> <value>"
;              <item>     = {filta, filtb, expmode, dpe, percentd, etc}
;              <relation> = { =,>,<,>=,<=,<>}
;              <value>    = value, list (ex: 1,2,5) or range (ex: 4-10)
;               
;   Examples:
;      IDL> print,gt2exe('dpe=[12-20]',/addind)
;      IDL> gt_dpe(index(ss)) ge 12 AND gt_dpe(index(ss)) le 20
;
;      IDL> print,gt2exe('filta=[2,5]', /addind)
;      IDL> gt_filta(index(ss)) eq 2 OR gt_filta(index(ss)) eq 5
;
;   History:
;      Circa jan-93 (SLF) - for spd files
;      22-mar-1995  (SLF) - add range and list (expanded for obs searches)
;-
gt_functions=   [               $
		'expmode',	$
                'dp_mode',      $
                'filta',        $
                'filtb',        $
                'res',          $
                'dpe',		$
		'percentd',     $
		'comp'		]

if keyword_set(gtfunctions) then return,'gt_' + gt_functions
strings=[0,0,0,0,0,0,0,0] 		 ; for each function, set if /string used

debug=keyword_set(debug)

; define boolean/symbolic translations
log_operators=	['>=','<=','<>' ,'=','<', '>']
bool_equiv   =  ' ' + ['ge','le','ne' ,'eq','lt','gt'] + ' '  ;blank padded

tinput=input				; protect input
tinput=str_replace(tinput,'gt_','')	; remove gt root if it is there
					; already (handle both ways)
cls=[' ',') ']
ind=['','(index(ss)']
str=['',',/string']

index=ind(keyword_set(addind) or keyword_set(noss))
if keyword_set(noss) then index=str_replace(index,'(ss)','')

for i = 0, n_elements(gt_functions)-1 do $
   tinput=str_replace(tinput, gt_functions(i),' gt_'+  gt_functions(i) + $
	index + str(strings(i)) + cls(keyword_set(addind)) )

for i = 0, n_elements(log_operators)-1 do $
   tinput=str_replace(tinput,log_operators(i), bool_equiv(i))

tinput=strcompress(strtrim(tinput,2))	; elminate extraneous blanks

; brackets are optional for list and range values - eliminate them
tinput=str_replace(tinput,'[','')
tinput=str_replace(tinput,']','')

; break the input (<item><relationship><value>)
tinput=str2arr(tinput,' ')

; check for list syntax (ex: [1,4,6]) 
list=where(strpos(tinput,',') ne -1,lcnt)	; comma delimited
if lcnt gt 0 then tinput= $
   arr2str(tinput(0) + ' ' + tinput(1) + ' ' + str2arr(tinput(2)),' OR ')

; expand ranges ex: [1-5] -> ge low AND le hi
range=where(strpos(tinput,'-') ne -1,rcnt)	; dash delimited
if rcnt gt 0 then begin
   rng=str2arr(tinput(range),'-')   
   tinput= gt2exe(tinput(0) + ' >= ' + rng(0)) + ' AND ' + $
           gt2exe(tinput(0) + ' <= ' + rng(1)) 
endif

tinput=str_replace(arr2str(tinput,' '),' (','(')

if debug then stop

return,tinput

end      



