;+-------------------------------------------------------------
; NAME:
;       firstSigName
; PURPOSE:
;       Return the first signal name in a TDI expression.
;	Needed when you want the xaxis from a combination of signals.
; CATEGORY:
;       MDSplus
; CALLING SEQUENCE:
;	sig1 = firstSigName( TDIexpr )
; INPUTS:
;   	TDIexpr - TDI expression. Can be just a signal name, or '(\ip1+\ip2)/2'
; RETURNED:
;	sig1 - first signal name in TDI expr, e.g., '\ip1'
; KEYWORDS:
;    Optional Outputs:
;	FOUNDTDI - 1 returned if TDI in node
;	DIMENSION - if root node is 2-D, but expression limits to 1.
;	SECOND - the second signame name in the expression, if any (else '')
;    Optional Inputs:
;	DATA = if set, return everything from DATA to ']', if present, or
;	       to ')'
; COMMON BLOCKS:
;       none
; EXAMPLE:
; 	IDL> print, firstSigName( '\engineering::ip1+\engineering::ip2' )
;	   \engineering::ip1
; LIMITATIONS:
;	probably won't return proper dimension when DATA() is within a TDI function, 
;	like BCSMOOTH( data( \camera:image, 1) )
; MODIFICATION HISTORY:
;	31-Aug-2007 added keyword SECOND
;	19-Jun-2007 ONLY return dimension number from syntax like ",1" if 
;		    MAXVAL, MINVAL or DATA (others can be included here).
;	06-Apr-2007 Added data and dimension keywords 
;  		    to handle style of maxval(\efit01::pisrz,1) and 
;		    data(\efit01::pisrz)[10,*] )
;	01-May-01 Written by Bill Davis
;--------------------------------------------------------------
FUNCTION firstSigName, TDIexpr_in, FOUNDTDI=foundTDI, data=data, second=second, $
                       dimension=dimension, debug=debug

noBlanks = STRTRIM( TDIexpr_in, 2 )

if keyword_set( DATA ) then begin
   iPos = STRPOS(STRUPCASE(TDIexpr_in), 'DATA')
   if iPos GE 0 then  begin   
      nPos = STRPOS(STRUPCASE(TDIexpr_in), ')')
      rest = STRMID( TDIexpr_in, nPos-iPos+1, 1000)
      addPos =  STRPOS(STRUPCASE(rest), ']')
      nChars = nPos-iPos +1 + addPos + 1
      TDIexpr = STRMID( TDIexpr_in, iPos, nChars)
      dimension=0	; probably always 1-dimensional when data function used
      leftBracketPos = STRPOS( TDIexpr_in, '[')
      if leftBracketPos[0] NE -1 then begin
	 rightBracketPos = STRPOS( TDIexpr_in, ']')
	 BracketStr = strmid( TDIexpr_in, leftBracketPos, rightBracketPos-leftBracketPos+1 )
	 commaPos = strpos( BracketStr, ',' )
	 if commaPos gt -1 then begin	; multi-dimenional
            if strpos( strmid(BracketStr, commaPos, 100), '*' )  gt -1 OR  $
	       strpos( strmid(BracketStr, commaPos, 100), ':' )  gt -1 then dimension = 1
	 endif
      endif
      
      return, tdiExpr
   endif 
endif

; don't want to get .5 if signal begins with 2.5 * \blah

   ; so start with first \, if present
slashPos = STRPOS(TDIexpr_in, '\')
if slashPos LT 0 then TDIexpr = TDIexpr_in else begin
   TDIexpr = STRMID( TDIexpr_in, slashPos, 1000)
endelse

firstGoodChar = nonVarPos( TDIexpr, /NONUM, /GOOD )

nextNonPos = nonVarPos( STRMID( TDIexpr, firstGoodChar[0], 1000) )
if nextNonPos[0] EQ -1 then  nextNonPos[0] = STRLEN(TDIexpr) - firstGoodChar[0]

firstTag =STRMID( TDIexpr, firstGoodChar[0], nextNonPos[0] )

if STRLEN( firstTag ) NE STRLEN( noBlanks ) THEN foundTDI=1 else  foundTDI=0

   ; determine dimension number
   ;  ( for style of maxval(\efit01::pisrz,1) and data(\efit01::pisrz)[10,*] )
dimension=0
IF STRPOS( strupcase(tdiexpr), 'MAXVAL') ge 0 OR $
   STRPOS( strupcase(tdiexpr), 'MINVAL') ge 0 OR $
   STRPOS( strupcase(tdiexpr), 'DATA') ge 0 THEN BEGIN
   if strmid( tdiexpr, nextnonpos[0],1) eq ',' then begin
      nextchar = strmid( tdiexpr, nextnonpos[0]+1,1)
      if isnumber( nextchar ) then dimension = LONG(nextChar)
   endif else if strpos( tdiexpr_in,'[') ge 0 then begin
      pos1 = strpos( tdiexpr_in,'[')
      pos2 = strpos( tdiexpr_in,']')
      dimStr = strmid( tdiexpr_in, pos1+1, pos2-pos1-1)
      parts = get_words( dimStr, DELIM=',')
      inds = WHERE( parts eq '*', nfound)
      if nfound gt 0 then begin
	 dimension=inds[0]
      endif else begin
	 pinds = strpos( parts, ':' )
	 inds = where( pinds eq 1, nfound )
	 if nfound gt 0 then dimension=inds[0]
      endelse
   endif
ENDIF

IF Arg_Present( second ) THEN BEGIN
   second =''
   allPos = findallchar(tdiexpr,'\', nfound=nfound)
   if nfound ge 2 then begin
      nextNonPos = nonVarPos( STRMID( TDIexpr, allPos[1], 1000) )
      second = STRMID( TDIexpr, allPos[1], nextNonPos[0] )
   endif
ENDIF

leftBracketPos = STRPOS( TDIexpr_in, '[')
if leftBracketPos[0] NE -1 then begin
   rightBracketPos = STRPOS( TDIexpr_in, ']')
   BracketStr = strmid( TDIexpr_in, leftBracketPos, rightBracketPos-leftBracketPos+1 )
   commaPos = strpos( BracketStr, ',' )
   if commaPos gt -1 then begin	; multi-dimenional
      if strpos( strmid(BracketStr, commaPos, 100), '*' )  gt -1 OR  $
	 strpos( strmid(BracketStr, commaPos, 100), ':' )  gt -1 then dimension = 1
   endif
endif

if keyword_set( debug ) then stop

RETURN, firstTag

END