function gtt_mnem, data0, mnem, data_sou, $
		string=string, raw_value=raw_value, nostring=nostring, $
		qdebug=qdebug, qstop=qstop
;+
;NAME:
;	gtt_mnem
;PURPOSE:
;	TRACE routine to extract mnemonic information from data
;SAMPLE CALLING SEQUENCE:
;	out = gtt_mnem(data, mnem, data_sou)
;	out = gtt_mnem(be, 'iuispzta')
;INPUTS:
;	data	- Raw data.  Form is 2-D where first D is the
;		  packet (or header) and the second D is for 
;		  time (or image #).  It must be BYTE type
;	mnem	- Single mnemonic to extract
;OPTIONAL INPUT:
;	data_sou- Source of data.  It is a string of value
;			'HK': - input data is raw HK packets
;			'DP': - input data is raw data product header
;		  Default is "DP".
;HISTORY:
;	Written 12-Jan-98 by M.Morrison
;	15-Jan-98 (MDM) - Added other machine byte swapping
;			- Added byte swapping for type "U"
;	29-Jan-98 (MDM) - Allowed "data" input to be a structure
;			  and it makes a copy of the data product
;			  header.
;	 5-Mar-98 (MDM) - Corrected to still to analog conversion
;			  even when /NOSTRING is set
;			- Corrected analog conversion problem
;	18-Mar-98 (MDM) - Made "UB" actually come out as byte type
;			- Removed "n = n_elements(data(0,*))" type of calls
;	21-Apr-98 (MDM) - Added data type "I" (signed integer*2)
;       12-May-98 (FZ)  - Changed loop variables to long
;	11-Aug-98 (MDM) - Added "SFP" data type
;-
;
common hk_dbase_blk1, tlm, alg, dsc, cmd
;
if (n_elements(tlm) eq 0) then rd_egse_hk_txt, tlm, alg, dsc, cmd
;
if (n_elements(data_sou) eq 0) then data_sou = 'DP'
;
if (data_type(mnem) ne 7) then begin	;non string type
    print, 'GTT_MNEM: Input mnemonic list must be string
    return, -1
end
;
if (n_elements(mnem) ne 1) then begin	;array of mnemonics
    print, 'GTT_MNEM: Only one mnemonic at a time'
    print, 'MNEM=', mnem
    return, -1
end
if (n_elements(data0) eq 0) then begin
    print, 'GTT_MNEM: No input data passed in'
    return, -1
end
;
if (data_type(data0) eq 8) then begin
    ss = where(tag_names(data0) eq 'DP_HEADER', nss)
    if (nss ne 0) then begin
	n = n_elements(data0)
	len = n_elements(data0(0).dp_header)
	data = byte(data0.dp_header, 0, len*2, n)
    end
end
;
if (n_elements(data) eq 0) then data = data0	;make a copy - wasn't done in struct code
n = data_chk(data, /ny) > 1
len = data_chk(data, /nx)
if (data_type(data) ne 1) then begin
    data = byte(data, 0, len*2, n)
end
;
ss = where(tlm.name eq strlowcase(mnem), nss)
if (nss eq 0) then begin
    print, 'GTT_MNEM: Mnemonic not found in database....................' + mnem
    return, -1
end
;
tlm0 = tlm(ss)
if (keyword_set(qdebug)) then help, /st, tlm0
;
ii = 0
if (strupcase(data_sou) eq 'DP') then ii = (where(tlm0.apid eq 'DPH'))(0)
if (ii eq -1) then begin
    print, 'GTT_MNEM: Mnemonic ' + mnem + ' is not in the Dataproduct header'
    return, -1
end
;
stbyte = tlm0.stbyte(ii)
dtyp = tlm0.type
case dtyp of
    'ULI': out = long( data(stbyte:stbyte+3, *), 0, n)			;32 bits
    'I':   out = fix(  data(stbyte:stbyte+1, *), 0, n)			;16 bits
    'UI':  out = fix(  data(stbyte:stbyte+1, *), 0, n)			;16 bits
    'U':   out = long( data(stbyte:stbyte+3, *), 0, n)			;32 bits
    'UB':  out = reform(data(stbyte, *))				;8 bits
    'TIM': out = long( data(stbyte:stbyte+3, *), 0, n)			;32 bits
    'SFP': out = long( data(stbyte:stbyte+3, *), 0, n)			;32 bits float
    else:  out = long( data(stbyte:stbyte+3, *), 0, n)			;32 bits
endcase
;
;---- Do byte swapping for non-SGI machines...
;
qswap = is_member(/swap_os)
if (qswap) then dec2sun, out
;
;---- Adjust the datatype as necessary (byte swapping, unsigning, masking)
;
case dtyp of
    'UI':  out = unsign(out, 16)	;make unsigned (changes the output type too though)
    'U':   begin			;swap 16bit words '01020304'x becomes '03040102'x
		byteorder, out, /sswap 	
		byteorder, out, /lswap 	
	   end
    'SFP':   begin			;swap 16bit words '01020304'x becomes '03040102'x
		byteorder, out, /sswap 	
		byteorder, out, /lswap 	
		out = float(out, 0, n)
	   end
    else:  				;do nothing
endcase

;
if (keyword_set(qdebug)) then print, n, ss, stbyte, dtyp, tlm0.stbit, tlm0.nbit
;
case dtyp of
    'ULI': nbit4type = 32
    'UI':  nbit4type = 16
    'I':   nbit4type = 16
    'U':   nbit4type = 32
    'UB':  nbit4type =  8
    'TIM': nbit4type = 32
    else:  nbit4type = 32
endcase

if (nbit4type ne tlm0.nbit) then begin
    stbit = nbit4type - tlm0.stbit - tlm0.nbit	;database terminology has bit0=MSB
    if (keyword_set(qdebug)) then print, 'Masking... stbit=', stbit
    out = mask(out, stbit, tlm0.nbit)
end
;
;--------------------------- Conversions
;
if (not keyword_set(raw_value)) then begin
    case tlm0.conv_code of
	'A': begin
		ss = where(alg.name eq tlm0.conv, nss)
		raw = float(out)
		out = 0.
		coeff = alg(ss(0)).coeff
		for i=0L,n_elements(coeff)-1 do out = out + coeff(i)*raw^i
	     end
	else:
    endcase
end
if (not keyword_set(raw_value)) and (not keyword_set(nostring)) then begin
    case tlm0.conv_code of
	'S': begin
		ss = where(dsc.name eq tlm0.conv, nss)
		dsc0 = dsc(ss)
		ntabmax = max([dsc0.low, dsc0.hi])
		table = strarr(ntabmax+2) + '???'
		for i=0L,nss-1 do begin		;build the lookup table
		    ist = dsc0(i).low > 0 < ntabmax
		    ien = dsc0(i).hi  > 0 < ntabmax
		    table(ist:ien) = dsc0(i).str
		end
		nnn = n_elements(table)-1
		out = table(out >0<nnn )
	     end
	else:
    endcase
end
;
if (n_elements(out) eq 1) then out = out(0)	;make it scalar
if (keyword_set(qstop)) then stop
return, out
end
