function tfr_decom, tf_blk, close=close, test_case=test_case
;+
;NAME:
;	tfr_decom
;PURPOSE:
;	To decompress a single telemetry frame
;SAMPLE CALLING SEQUENCE:
;	decom_rec = tfr_decom(rec)
;COMMENTS:
;	idl_speed & read_mdi,'/md54/mdisw_idl_guide/940422_035054',1,img,h  & idl_speed,1
;HISTORY:
;	Written Apr-94 by M.Morrison & R.Chevalier
;	31-Mar-95 (MDM) - Added a new option to use CALL_EXTERNAL
;			  for MDI SGI machines.  The old method is
;			  still available which uses a pipe with STDIO
;			  which is much slower
;	 3-Apr-95 (MDM) - Added umbra to the machines that can use
;			  CALL_EXTERNAL
;	 6-Apr-95 (MDM) - Expanded to work with Ultrix machines
;	19-Jun-95 (MDM) - Removed using CALL_EXTERNAL for mdisas
;			- Added ON_IOERROR logic to trap on problems
;	28-Feb-96 (MDM) - Removed using CALL_EXTERNAL for umbra (now nobody
;			  uses it)
;	 3-Sep-96 (MDM) - Added protection againgst TFR_DECOM crashing
;			  and causing IDL to exit.
;-
;
common tfr_decom_blk, lun, quse_external
;
swap_os=['vms','ultrix','OSF']                  ; list of swap machines
chk=where(!version.os eq swap_os,nscount)       ; current OS in noswap list?
qswap = nscount ne 0                            ; assign boolean
;
start_time = systime(1)
;
if (n_elements(lun) eq 0) then lun = -1
if (n_elements(quse_external) eq 0) then begin
    ;quse_external = strupcase(strmid(get_host(),0,3)) eq 'MDI'
    case strlowcase(get_host(/short)) of
	; 'mdisas': quse_external = 1
	; 'umbra': quse_external = 1
	else:     quse_external = 0
    endcase
    print, 'TFR_DECOM:  quse_external = ', quse_external
end
if (getenv('TFR_DECOM_FORCE') ne '') then quse_external = fix(getenv('TFR_DECOM_FORCE'))
;
b=0b
if (quse_external) then begin
    if (keyword_set(close)) then return, 0b
    exe_fil = concat_dir(getenv('MDI_EXE_DIR'), 'tfr_decom2')
    raw_pkt = tf_blk(4:*)
    b = intarr(4000)
    n_words = call_external(exe_fil, 'decom_hr',raw_pkt,b)
    b = b(0:n_words-1)
end else begin
    if (keyword_set(close)) then begin
	if (lun ne -1) then free_lun, lun
	lun = -1
	return, 0
    end
    ;
    if (keyword_set(test_case)) then restore, file='tfr_raw.idl'
    ;
    if (lun eq -1) then begin
        ;;print, 'Starting the SPAWN process to TFR_DECOM'
        cmd = concat_dir(getenv('MDI_EXE_DIR'), 'tfr_decom')
        spawn, [cmd, '-p', '-f'], unit=lun, /noshell
    end
    ;
    avail = {block_addr: 0L, n_words: 0L }
    raw_pkt = tf_blk(4:*)
    ;
    if (qswap) then begin
	junk = raw_pkt
	raw_pkt(indgen(5)*2) = junk(indgen(5)*2+1)
	raw_pkt(indgen(5)*2+1) = junk(indgen(5)*2)
	;
    end
    on_ioerror, error
    writeu, lun, raw_pkt
    readu, lun, avail
    writeu, lun, avail.block_addr
    b = intarr(avail.n_words)
    readu, lun, b
    goto, skip
	error:
	print, 'IO Error within TFR_DECOM
	on_ioerror, NULL
	free_lun, lun		;MDM added 3-Sep-96
	lun = -1
    skip:
end
;
end_time = systime(1)
run_time = (end_time - start_time)
if (getenv('QDEBUG_TFR_DECOM') ne '') then print, 'lun= ',lun,'  avail = ', avail, ' one call took: ', run_time
;
return, b
end
