function get_tm_img,sc,pktno,hdr,img,npckt
;
;+
;
;	searches sciences packets for the next occurrence of an image
;
;	sc = 2D array containing the science packets
;		the array should contain only data packets when the OBE
;		is running.  ie no packets when the instrument is off,
;		and 'ffff'xl are being transmitted.
;	pktno = packet number to start the search
;		on exit, it will contain the packet number of the header
;	hdr = image header returned
;	img = image returned
;	npckt = number of packets in image
;	the function return is =0 if no image is found
;			       =1 if an image is found
;
; @(#)get_tm_img.pro	1.1 01/23/98 :LASCO IDL LIBRARY
;
;-
w = where((sc(18,pktno:*) and 'ff'xl) ne 6,nw)	; ignore pad data
if nw lt 1 then return,0
pktno=w(0)+pktno
;print,w(0:5)
;print,'pckt # found',pktno,sc(18,pktno) and 'ff'xl
;if ((pktno ne 0) and ((sc(18,pktno) and 'ff'xl) ne 2)) then pktno=pktno-1 
s=size(sc)
pktsize=s(1)
nsc=s(2)
npckt = 0
;
;  find image header
;
for pno=pktno,nsc-1 do begin
    pkidwd = 18
    repeat begin
       nwds = ishft( sc(pkidwd,pno) and 'ff00'xl , -8)
       pkttype = sc(pkidwd,pno) and 'ff'xl
       if pkttype ne 2 then pkidwd=pkidwd+nwds+2 else goto,l1
    endrep until pkidwd ge pktsize
endfor
pktno = nsc+1
return,0	;image header not found
l1:
pktno = pno
;
;  fill up header array, doesn't allow for header crossing packet boundary
;
hdr=sc(pkidwd+2:pkidwd+1+nwds,pno)
hdr=[sc(1:5,pktno),sc(15:17,pktno),hdr]
;
;  now fill up image array
;
for pno=pktno,nsc-1 do begin
pkidwd = pkidwd+2+nwds
while pkidwd lt pktsize do begin
   pkttype = sc(pkidwd,pno) and 'ff'xl
   nwds = ishft(sc(pkidwd,pno) and 'ff00'xl,-8)
;   print,'packet number, type, # words = ',pno,pkttype,nwds
   case pkttype of
      3:  begin
	    img=sc(pkidwd+2:pkidwd+1+nwds,pno)	; start image block
	    print,'Found Start image at time= ',hdr(3),hdr(4),hdr(5)
	  end
      4:  img=[img,sc(pkidwd+2:pkidwd+1+nwds)]	; continue image block
      5:  ; end sub img block
      6:  ; pad data
     23:  begin
	    print,'Found end image:'
	    help,hdr,img
	    return,1
	  end
    else: ;nothing
   endcase
   npckt = npckt+1
   pkidwd = pkidwd+nwds+2
endwhile
pkidwd=16
nwds=0
endfor
return,1
end

