pro loadsci,seq,a,h,nimg=nimg,first=first,last=last,fnam=fnam
;
;  This subroutine reads images from the 160k decom file(s) belonging to a
;  sequence 'yymmdd_hhmmss' and places the images and headers in the caller's 
;  arrays. It is an interface to 'scird' (which can also be called standalone)
;  allowing more options and checking the existence of the files first.
;
;  Call:
;       seq:   name of the decom file sequence (without the '_nn.tfr' extension)
;       a:     array for image data (will be dimensioned [x,y,nimg]) OUTPUT
;       hdr:   array for headers (dimensioned [549,nimg]) OUTPUT
;       nimg:  on INPUT specifies number of images: routine will then return
;                       only the first nimg images (or less if file ends sooner)
;              on OUTPUT has the actual number of images read
;              (optional parameter)
;       first: first file in the sequence series to consider (optional INTPUT)
;       last:  last file in the sequence series to consider (optional INTPUT)
;HISTORY:
;	Written ?? by ??
;	21-Feb-94 (MDM) - Added FNAM optional keyword input
;
;

if (n_elements(fnam) eq 0) then begin
    dir=getenv('MDI_SCI160K_LOG_DIR')
    fnam=dir+'/'+seq+'*.tfr'
end
list=findfile(fnam,count=count)

if count eq 0 then begin
  print,fnam+' not found'
  return
endif

if keyword_set(nimg) then nimg=nimg else nimg=32000

if keyword_set(first) then begin
  first=strtrim(string(first),2)
  exten='_00'
  strput,exten,first,3-strlen(first)
  exists=where(list eq dir+seq+exten+'.tfr')
  if exists(0) lt 0 then begin
    print,dir+seq+exten+'.tfr  not found'
    return;
  endif
  first=seq+exten
  endif $
else first=strmid(list(0),strpos(list(0),'.tfr',15)-16,16)

if keyword_set(last) then begin
  last=strtrim(string(last),2)
  exten='00'
  strput,exten,last,2-strlen(last)
  exists=where(list eq dir+seq+'_'+exten+'.tfr')
  if exists(0) lt 0 then begin
    print,dir+seq+'_'+exten+'.tfr  not found'
    return
  endif
  last='-'+exten
  endif $
else last='-'+strmid(list(count-1),strpos(list(count-1),'.tfr',15)-2,2)

if strmid(first,14,2) eq strmid(last,1,2) $
  then list=first else list=first+last
print,'Reading '+list
scird,list,a,h,nimg=nimg
end
