function motor_decode,hk,tel,focus=focus,filter=filter,polar=polar, $
	sector=sector, shutter=shutter,door=door,iocsx=iocsx,iocsy=iocsy, $
	leg1=leg1,leg2=leg2,fpll=fpll
;+
; NAME:
;	MOTOR_DECODE
;
; PURPOSE:
;	This function returns a structure array containing the decoded motor 
;	status.
;
; CATEGORY:
;	PACKETS
;
; CALLING SEQUENCE:
;	Result = MOTOR_DECODE (Hk,Tel)
;
; INPUTS:
;	Hk:	A 2D byte array containg the LASCO HK packets (at least HK #2)
;	Tel:	An integer (0..3) giving the telescope number 
;
; KEYWORD PARAMETERS:
;	FOCUS:		Set for the focus motor
;	FILTER:		Set for the filter wheel motor
;	POLAR:		Set for the polarizer wheel motor
;	SECTOR:		Set for the sector wheel motor
;	SHUTTER:	Set for the shutter motor
;	DOOR:		Set for the door motor
;	IOCSX:		Set for the IOCS-X motor
;	IOCSY:		Set for the IOCS-Y motor
;	LEG1:		Set for the leg #2 motor
;	LEG2:		Set for the leg #2 motor
;	FPLL:		Set for the FP launch lock motor
;
; OUTPUTS:
;	This function returns an array of structures
;		pos
;		rate
;		adv
;		status
;		coil
;		mode
;		dir
;		ok
;		channel
;
; MODIFICATION HISTORY:
; 	Written by:	S.E. Paswaters, NRL, 1995
;
;	@(#)motor_decode.pro	1.2 08/17/01 LASCO IDL LIBRARY
;
;	17 Aug 2001	RAH	Corrected some compilation errors
;-
;
ttel=tel
if keyword_set(door)    then motor=0
if keyword_set(filter)  then motor=1
if keyword_set(polar)   then motor=2
if keyword_set(sector)  then motor=2
if keyword_set(shutter) then motor=3
if keyword_set(focus)   then motor=4
if Keyword_set(iocsx)   then motor=5
;
;	motor = 0 for door
;		= 1 for filter wheel
;		= 2 for polarizer/sector wheel
;		= 3 for shutter
;		= 4 for focus
;		= 5 for iocs-x
;		= 6 for iocs-y
;		= 7 for leg 1
;		= 8 for leg 2
;		= 9 for FP Launch Lock
;
if Keyword_set(iocsy)   then motor=6
if keyword_set(leg1)    then begin & ttel=-1 & motor=7 & end
if keyword_set(leg2)    then begin & ttel=-1 & motor=8 & end
if keyword_set(fpll)    then motor=9

hk2=gethkn(hk,2)
s=size(hk2)
IF (s(s(0)+1) EQ 1)  THEN byteflag=0 ELSE byteflag=1
nel=s(2)
motor_struct={motor_struct,   $
		pos	: 0, $
		rate	: 0, $
		adv	: 0, $
		status	: 0, $
		coil	: 0, $
		mode	: 0, $
		dir	: 0, $
		ok	: 0, $
		driven	: 0, $
		channel	: 0 }
a=REPLICATE(motor_struct, nel)
sz = size(hk)
IF (byteflag EQ 0)  THEN BEGIN
   offset=12  
   bytfac = 2
ENDIF ELSE BEGIN
   offset=15
   bytfac = 1
ENDELSE
offmotor=offset+bytfac*(2-1-1)		;offset to beginning of motor status

case ttel of
0: begin				; C1
       case motor of
		0:	subword=1	;door
		1:	subword=5	;filter
		2:	subword=3	;polarizer
		3:	subword=7	;shutter
		4:	subword=43	;Focus
		9:	subword=41	; FP Launch Lock
	endcase
   end
1: begin				; C2
       case motor of
		0:	subword=9	;door
		1:	subword=11	;filter
		2:	subword=13	;polarizer
		3:	subword=15	;shutter
		4:	subword=45	;Focus
		5:	subword=17	;IOCS-X
		6:	subword=19	;IOCS-Y
	endcase
   end
2: begin				; C3
       case motor of
		0:	subword=21	;door
		1:	subword=23	;filter
		2:	subword=25	;polarizer
		3:	subword=27	;shutter
	endcase
   end
3: begin				; EIT
       case motor of
		0:	subword=33	; door
		1:	subword=37	; filter
		2:	subword=35	; sector
		3:	subword=39	; shutter
	endcase
   end
default: begin
	case motor of
		7:	subword=29	; leg 1
		8:	subword=31	; leg 2		
	endcase
   end
endcase
offmotor=offmotor+bytfac*subword
IF (byteflag EQ 0)     THEN BEGIN
   a(*).ok     = fix(hk2(offmotor+1,*)) and 1
   a(*).driven = fix(hk2(offmotor+1,*)) and 2
   a(*).coil   = fix(hk2(offmotor+1,*)) and 'f0'xl
   a(*).mode   = fix(hk2(offmotor  ,*)) and '03'xl
   a(*).dir    = fix(hk2(offmotor  ,*)) and '0c'xl
   a(*).channel= fix(hk2(offmotor  ,*)) and '10'xl
   a(*).rate   = fix(hk2(offmotor+3,*)) and '7f'xl
   a(*).adv    = fix(hk2(offmotor+3,*)) and '80'xl
   a(*).pos    = fix(hk2(offmotor+2,*)) and '0f'xl
   a(*).status = fix(hk2(offmotor+2,*)) and 'f0'xl
ENDIF ELSE BEGIN
   a(*).ok=tmpoint(hk2,offmotor,1)
   a(*).driven=tmpoint(hk2,offmotor,2)
   a(*).coil=tmpoint(hk2,offmotor,'00f0'xl)
   a(*).mode=tmpoint(hk2,offmotor,'0300'xl)
   a(*).dir=tmpoint(hk2,offmotor,'0c00'xl)
   a(*).channel=tmpoint(hk2,offmotor,'1000'xl)
   a(*).rate=tmpoint(hk2,offmotor+1,'007f'xl)
   a(*).adv=tmpoint(hk2,offmotor+1,'0080'xl)
   a(*).pos=tmpoint(hk2,offmotor+1,'0f00'xl)
   a(*).status=tmpoint(hk2,offmotor+1,'f000'xl)
ENDELSE
return,a
end
