;+
; NAME:  reduce_pha.pro
;
; PURPOSE:  To reduce the data from readout .pro into a series of numbers.
;		Weighted and normal means are applied, with the weight being
;		given by the inverse of the chi squared.
;
; CALLING SEQUENCE: .run reduce_pha.pro 
;
; INPUTS:  Data arrays from readout .pro
;
; OUTPUTS:  A table of values for the centre and fwhm of each channel.
;
; OPTIONAL OUTPUTS: none
;
; RESTRICTIONS: requires wstdev.pro - modified library procedure.
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;      written atp - jan 1992.
;	       
;-
;
;reduce pha - turn pha from readout.pro into a mean, stdev
;

;
; 1st divide channels
;
print,'What is the number of data sets?'
read,ndset
phachn = phachan(0:ndset-1)
phacen0 = phacen(where(phachn eq 0))
phacen1 = phacen(where(phachn eq 1))
phacen2 = phacen(where(phachn eq 2))
phacen3 = phacen(where(phachn eq 3))
;
phachi0 = phachi(where(phachn eq 0))
phachi1 = phachi(where(phachn eq 1))
phachi2 = phachi(where(phachn eq 2))
phachi3 = phachi(where(phachn eq 3))
;
phafwhm0 = phafwhm(where(phachn eq 0))
phafwhm1 = phafwhm(where(phachn eq 1))
phafwhm2 = phafwhm(where(phachn eq 2))
phafwhm3 = phafwhm(where(phachn eq 3))
;
; work out weights
;
weight0 = 1.0/phachi0
weight1 = 1.0/phachi1
weight2 = 1.0/phachi2
weight3 = 1.0/phachi3
;
n0fs = stdev(phafwhm0,n0fm)
n1fs = stdev(phafwhm1,n1fm)
n2fs = stdev(phafwhm2,n2fm)
n3fs = stdev(phafwhm3,n3fm)
;
n0cs = stdev(phacen0,n0cm)
n1cs = stdev(phacen1,n1cm)
n2cs = stdev(phacen2,n2cm)
n3cs = stdev(phacen3,n3cm)
;
w0fs = wstdev(phafwhm0,weight0,w0fm)
w1fs = wstdev(phafwhm1,weight1,w1fm)
w2fs = wstdev(phafwhm2,weight2,w2fm)
w3fs = wstdev(phafwhm3,weight3,w3fm)
;
w0cs = wstdev(phafwhm0,weight0,w0cm)
w1cs = wstdev(phafwhm1,weight1,w1cm)
w2cs = wstdev(phafwhm2,weight2,w2cm)
w3cs = wstdev(phafwhm3,weight3,w3cm)
;
nelem0 = n_elements(phacen0)
nelem1 = n_elements(phacen1)
nelem2 = n_elements(phacen2)
nelem3 = n_elements(phacen3)
;
print,'channel 0 nsamp',nelem0,' fwhm',n0fm,' stdev',n0fs
print,'channel 1 nsamp',nelem1,' fwhm',n1fm,' stdev',n1fs
print,'channel 2 nsamp',nelem2,' fwhm',n2fm,' stdev',n2fs
print,'channel 3 nsamp',nelem3,' fwhm',n3fm,' stdev',n3fs
;
print,'channel 0 Weighted (1/chi) fwhm',w0fm,' stdev',w0fs
print,'channel 1 Weighted (1/chi) fwhm',w1fm,' stdev',w1fs
print,'channel 2 Weighted (1/chi) fwhm',w2fm,' stdev',w2fs
print,'channel 3 Weighted (1/chi) fwhm',w3fm,' stdev',w3fs
;
print,'channel 0 nsamp',nelem0,' centre',n0cm,' stdev',n0cs
print,'channel 1 nsamp',nelem1,' centre',n1cm,' stdev',n1cs
print,'channel 2 nsamp',nelem2,' centre',n2cm,' stdev',n2cs
print,'channel 3 nsamp',nelem3,' centre',n3cm,' stdev',n3cs
;
print,'channel 0 Weighted (1/chi) centre',w0cm,' stdev',w0cs
print,'channel 1 Weighted (1/chi) centre',w1cm,' stdev',w1cs
print,'channel 2 Weighted (1/chi) centre',w2cm,' stdev',w2cs
print,'channel 3 Weighted (1/chi) centre',w3cm,' stdev',w3cs
;
end

