Viewing contents of file '../idllib/ghrs/pro/calhrs_sort.pro'
pro calhrs_sort,udls,ih,first_bin,nbins,nfound,nmerge,log
;
;+
;			calhrs_sort
;
; procedure to sort all bins of data by readout
;
; CALLING SEQUENCE:
;	calhrs_sort,udls,ih,first_bin,nbins,nfound,nmerge,log
;
; INPUTS:
;	udls - array of unique data logs
;	ih - header vectors (128xN)
;
; OUTPUTS:
;	first_bin - vector of length equal to the number of readouts
;		containing the position of the first bin of data
;		for the readout.
;	nbins - scalar gving the number of substeps in the pattern
;	nfound - vector giving the number of bins found for each
;		data readout.
;	nmerge - number of bins which should be merged
;
; OPTIONAL INPUT/OUTPUT:
;	log - processing log
;
; HISTORY:
;	version 1  D. Lindler  Mar 89
;       Apr 17 1991      JKF/ACC    - GHRS DAF (IDL Version 2) - change way
;					to get # of dimensions in IH.
;-
;---------------------------------------------------------------------------
 
	s=size(ih) & n=n_elements(ih)/s(1)	;number of bins of data(IDLV2)
	;	s=size(ih) & n=s(2)			;number of bins of data
;
; If ddlink data then one bin for each readout
;
	if (ih(0) mod 10) eq 0 then begin	;ddlink
		nbins=1
		nmerge=0			;don't merge data
		first_bin=indgen(n)
		nfound=replicate(1,n)
		return
	end
;
; extract pattern information from first UDL
;
	hoff = [0,udls(49:54)]	; horizontal offsets for each bin. first
				; 	bin is always 0
	voff = [0,udls(56:61)]	; vertical offsets
	binids = udls(25:31)	; substep bin ids
	rcodes = [1,udls(63:68)]; repeat codes
;
; determine number of bins
;
	for i=0,6 do begin
		if rcodes(i) eq 0 then goto,stop_counting
		nbins=i+1
	end
stop_counting:
;
; determine number of bins to merge ---------------------------------------
;
	binid0=binids(0)			;first bin id
 
;case 1 first binid not 1 or 2 --->do not merge (neither LSA or SSA data)
 
	if (binid0 ne 1) and (binid0 ne 2) then begin
		nmerge=0				;don't merge
	end else begin $
 
; case 2 both binids 1 and 2 present ---> non-standard, do not merge
 
	if ((binid0 eq 1) and (total(binids eq 2) gt 0)) or $
	   ((binid0 eq 2) and (total(binids eq 1) gt 0)) then begin
		nmerge=0				;both LSA and SSA data
	end else begin
 
; case 3  quarter stepped
 
	gross_bins = where(binids eq binid0)		;data from same aperture
	ngross=!err
	if (ngross eq 4) and 		     $		;four bins for aperture
	   (max(gross_bins) eq 3) and        $		;first four?
	   (max(abs(voff(0:3))) eq 0) and    $		;all same ydef?
	   (total(abs(hoff-[0,2,4,6])) eq 0) then begin ;right x-offsets?
		nmerge=4				;quarter stepped data
	end else begin
 
; case 4 half stepped data
 
	if (ngross eq 2) and 		     $		;two bins for aperture
	   (binids(1) eq binid0) and          $		;first two?
	   (voff(1) eq 0) and		     $		;same ydef?
	   (hoff(1) eq 4) then begin 			;right x-offsets?
		nmerge=2				;half-stepped data
	end else begin
;
; case 5 single stepped data
;
	if ngross eq 1 then nmerge=1 $
			else nmerge=0			;something else
	endelse
	endelse
	endelse
	endelse
;
; sort by readout ------------------------------------------------------------
;
	if !prelaunch then begin		;assume all data is in file
		nreads=n/nbins
		first_bin=indgen(nreads)*nbins
		nfound=replicate(nbins,nreads)
	  end else begin
		obsnum=ih(19,*)
		binnum=ih(0,*) mod 10
		first_bin=intarr(n)
		nfound=intarr(n)
		nreads=0
		nfound(0)=1
		obnum=obsnum(0)
		bin=binnum(0)
		if n gt 1 then begin
			for i=1,n-1 do begin
;
; is it a new readout
;
				if (obsnum(i) ne obnum) or (binnum(i) le bin) $
				  then begin
					nreads=nreads+1
					obnum=obsnum(i)
					first_bin(nreads)=i
				endif
				bin=binnum(i)
				nfound(nreads)=nfound(nreads)+1
			endfor
		endif
		nreads = nreads + 1	;count last one
		nfound=nfound(0:nreads-1)
		first_bin=first_bin(0:nreads-1)
		bad=where(nfound ne nbins) & nbad=!err
		if nbad gt 0 then begin
		   hist=strarr(nbad+1)
		   hist(0)='CALHRS_SORT -  Following readouts have missing bins'
		   for i=0,nbad-1 do hist(i+1)='    Readout '+string(bad(i)+1)
		   if !dump gt 0 then printf,!textunit,hist else print,hist
		   if n_params(0) gt 6 then sxaddhist,hist,log
		endif
	endelse
return
end