PRO load_lasco,list=list,wbp = wbp, wlp = wlp ,cid = cid, path = path
 
; Author:	Borut Podlipnik, MPAe
;
; Modified:
;  2002-09-25, B. Podlipnik
;
;
COMMON dirs_files, dirs, files, hdrs, sel_method, s_method, a_files, $
                   dirs_f,files_f,dirs_s,files_s, new_dirs, new_hdrs, new_text

@wload.com
@chandle.com

  IF NOT KEYWORD_SET(list) THEN RETURN

  IF N_ELEMENTS(img_resize) EQ 0 THEN img_resize = 1.0
   
  IF N_ELEMENTS(ld) EQ 0 THEN $
   ld = { load_defaults, net:0, dark:0, size:1, scl:0, byt:0 }   
  wdefault
      
   n_files = N_ELEMENTS(list) - 1

  WIDGET_CONTROL,wbp ,HOURGLASS=1
           
;  FOR j=0,n_files DO BEGIN

  process = 0
  j = 0          
  REPEAT BEGIN

   next_valid_image: 

   name = list(j)
   
   ff = FINDFILE(name)
  
   IF ff(0) EQ '' THEN name = path + !delimiter + new_name

   print,"Loading image ",j," : ",name
   
   image = READFITSL(name,head)

   ;h = hdr2struct(head)

   IF N_ELEMENTS(image) EQ 1 THEN BEGIN
     ;dummy = WIDGET_MESSAGE('Error opening file: '+name,/ERROR)
     new_hdrs(j) = 'Error opening file: '+name
     IF (XRegistered("whdrinfo1") NE 0) THEN WIDGET_CONTROL,wlp,SET_VALUE=new_hdrs
     undefine, image
   IF j EQ n_files THEN GOTO,load_image
     j=j+1 & GOTO,next_valid_image
   ENDIF

   IF (XRegistered("whdrinfo1") NE 0) THEN BEGIN

     IF N_ELEMENTS(new_hdrs) GT 0 THEN  $
       new_hdrs(j) = 'Image #:'+STRING(j+1,FORMAT='(I4)')+' of '+ $
                               STRING(n_files+1,FORMAT='(I4)')+' loaded'

     WIDGET_CONTROL,wlp,SET_VALUE=new_hdrs
     ;WIDGET_CONTROL,wlp,SET_TEXT_TOP_LINE=new_hdrs(j+1)
   ENDIF


   ;resize  = FXPAR(head,"RESIZE") > 1
   resize = img_resize

   sumrow  = FXPAR(head,"SUMROW") > 1 
   sumcol  = FXPAR(head,"SUMCOL") > 1
   lebxsum = FXPAR(head,"LEBXSUM") > 1
   lebysum = FXPAR(head,"LEBYSUM") > 1

   IF resize GT 1 THEN BEGIN 
     sumrow=1 & sumcol=1 & lebxsum=1 & lebysum=1   
   ENDIF

   nxsum = sumcol*lebxsum
   nysum = sumrow*lebysum

   naxis1 = FXPAR(head,"NAXIS1")
   naxis2 = FXPAR(head,"NAXIS2")

   resize = img_resize 

   IF img_resize EQ -1 THEN nxsum = sumcol ; for full resolution
   
   IF datatype(image) EQ "INT" and min(image) LT 0 THEN BEGIN
   
    image = FLOAT(image)
    ind = WHERE(image LT 0)
    image(ind) = image(ind) + 65536.0
   
   
   ENDIF
   
   IF nxsum GT 0 THEN BEGIN
      resize = nxsum * ABS(img_resize)      
      image = resize_img(image,resize,head)
   ENDIF ELSE BEGIN
      image = resize_img(image,resize,head)
   ENDELSE

   r1col  = FXPAR(head,"R1COL") - 20
   r1row  = FXPAR(head,"R1ROW") -  1
   r2col  = FXPAR(head,"R2COL") - 20 
   r2row  = FXPAR(head,"R2ROW") -  1

   detector = SXPAR(head,'DETECTOR')
   detector = STRCOMPRESS(detector,/REMOVE_ALL)

   name = path_file(name)
   name = name.files_f
   name = name(0)

   IF ld.dark EQ 1 THEN BEGIN
        image = image - get_bias(head)*lebxsum*lebysum
   ENDIF

   IF ld.net  EQ 1 THEN BEGIN
     exptime = FLOAT(chk_h4s(head,"EXPTIME"))
     IF exptime EQ -1 THEN XVAREDIT,exptime
     image = image / exptime
   ENDIF 

   chandle

;        ENDFOR


  if keyword_set(cid) then begin
   dummy = widget_event( cid, /nowait, /save)
   
   if dummy.id eq cid then begin
     process = 1  
     print,"Delete already loaded images?" 
     WIDGET_CONTROL,wbp ,HOURGLASS=0
     dummy = widget_event( cid, /nowait)
   endif
  endif
  
   j = j+1 

 ;ENDREP UNTIL (j GT n_files) OR (GET_KBRD(0) EQ 'c')
  ENDREP UNTIL (j GT n_files) OR (process eq 1)
      
; files_s = ['']
; dirs_s  = ['']
 new_hdrs= ['']
          
 load_image:

 IF N_ELEMENTS(index) GT 0 THEN  ghandle, index ; get last valid image
help,image
RETURN
END


