;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; UVSPREAD.PRO -- Reads in the selected experiment using UVSPRD
;
; struc		: structure of structure (see UVSPSTRUC.PRO)
; data		: float array with experiment data 
; hd            : integer array of experiment header info. received in read
; status	: when returned indicates 0=unsuccessful read, 1=good read
;
;	Modified: Use lowercase "v" to allow unix/nfs access.
;		  D.M. fecit, 1995 March 15.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


pro uvspread, struc=struc, data=data, hd=hd, status=status
common order1, diskexp, control, generic, comment

status = 0				; assume bad read, quit upon return

 ;
 ; Add the 'v' to the experiment number
 ;

 exp = 'v' + struc.st6.expnu 

 ;
 ; If a disk location is not yet assigned by 'Manual input' then find disk 
 ; contains the experiment requested.
 ;

 if struc.st6.disk eq 'any location:' then begin
	; 
	; Find file on any disk in search path.
        ;

 	opdev = uvspdevck(file=exp+'.*', comment=comment , error=error)  
	if error then return
        struc.st6.disk = opdev
 endif

 ;
 ; Assign a disk to the experiment 
 ;

 exp = struc.st6.disk  + temporary(exp)

 ;
 ; Go ahead and read the file
 ;

 cexp = 0  
 cexp = uvsprd(exp,hd)     		; clear variable then read in full name 
 struc.st6.disk = 'any location:' 	; reset for disk search

 siz = size(cexp)     			; get exp. dimensions
 if siz(0) eq 0 then begin  		; have a problem with the file
    widget_control, comment, set_value='UVSPREAD: Error reading '+exp, /append
    return	
 endif

 if siz(0) eq 1 and siz(1) eq 1 then begin  ; have a single point
    widget_control, comment, set_value='UVSPREAD: Error involving '+exp, /append
    return	
 endif

 ; need full exp. name for later file openings of this file
;95/3/7 if strpos(exp, '.') eq -1 then exp = exp + '.*' ; no extention given by user

 if (strpos(strlowcase(exp),'.pb') eq -1) and $                 ; no extention 
    (strpos(strlowcase(exp),'.fd') eq -1) then exp = exp + '.*' ;  given by user


 a = findfile(exp,count=count)    	; get full filename as known by system
			        	;   FDs listed before PBs (alphabetical)
 if count eq 0 then begin               ; bad file name
	widget_control, comment, set_value='ERROR-UVSPREAD, ' + $
					'file not found, ' + exp, /append
        return
 endif

 ; assign full system name of file
 struc.st6.expno  = a(0)
 widget_control, comment, set_value='Using experiment ' + struc.st6.expno, /append


 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;
 ; Assign dimensions of this new experiment
 ;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 ny = hd(141)>1   &  nx = hd(142)>1   	; switch x and y because of orientation
 wava = 1  &  polo = 1  &  nrpt = 1  &  ndet = 1  ; default sizes

 if siz(0) ge 3 then begin
    wava = siz(3)      			; number of wavelength dim.
    if siz(0) ge 4 then begin
       polo = siz(4)			; number of polarimeter dim.
       if siz(0) ge 5 then begin
          nrpt = siz(5)	          	; number of exp repeats
          if siz(0) ge 6 then begin
             ndet = hd(60)  		; number of detectors
          endif
       endif
    endif
 endif                          

 ; assign these dimensions to the structure system defined in UVSPSTRUC.PRO

 struc.st7.ny = ny      &  struc.st7.nx = nx     
 struc.st7.wava = wava  &  struc.st7.polo = polo  
 struc.st7.nrpt = nrpt  &  struc.st7.ndet = ndet 

 ; create a data array that will be correctly roll for UVSP
 data = fltarr(nx,ny,wava,polo,nrpt,ndet)
    for d = 0,ndet-1 do $
     for r = 0,nrpt-1 do $
      for p = 0,polo-1 do $
       for w = 0,wava-1 do data(0,0,w,p,r,d) = rotate(cexp(*,*,w,p,r,d),1)


 status = 1		       ; indicates don't quit upon return 

return & end
