;+
; Project     : SOHO - CDS     
;                   
; Name        : PLCNTS
;               
; Purpose     : For use in the calibration. Plots counts versus scan mirror
;             : or slit position
;               
; Explanation : A specified file will be read using READCALFITS and counts 
;             : summed over pixels specified using wrange and srange for each
;             : position of the scan mirror or the slit, whichever is specified.
;               
; Use         : 
;             : 
;    
; Inputs      : det - 'G' or 'N' for grazing or normal incidence detector
;             : file - the name of the file containing the data to be read
;             : wrange - vector dimension 2 containing wavelength pixel limits 
;             : srange - vector dimension 2 containing spatial pixel limits
;               
; Opt. Inputs : /slit - if present, the dependent variable is slit position
;             : /mirror - if present, the dependent variable is mirror position
;             : Either /slit or /mirror should be present
;             : window - the number of the window in the data (NI only)
;               
; Outputs     : cnts - array containing the counts
;             : spos - array containing the slit positions
;             : mpos - array containing the mirror positions
;               
; Opt. Outputs: scale - the scale applied to the counts if OVER is used
;               
; Keywords    : DET,FILE,WRANGE,SRANGE,CNTS,SPOS,MPOS,SLIT,MIRROR
;
; Calls       : READCALFITS
;
; Common      :
;               
; Restrictions: 
;               
; Side effects: 
;               
; Category    : 
;               
; Prev. Hist. :
;
; Written     : B.Bromage  27.2.94.
;               
; Modified    : 
;
; Version     : 1.0
;-            
;
pro plcnts,det=d,file=file,wrange=wr,srange=sr,cnts=c,spos=sp,mpos=mp,$
            slit=slit,mirror=mirror,window=nw
;
; Check validity of parameters:
    IF (D NE "N") AND (D NE 'G') THEN BEGIN
                                PRINT, "DET should be 'N' or 'G'"
                                RETURN
                                END
    IF (N_ELEMENTS(WR) NE 2) THEN BEGIN
                                PRINT,' WR should be 2-vector'
                                RETURN
                                END
    IF (D EQ 'N') THEN BEGIN
                  IF (N_ELEMENTS(SR) NE 2) THEN BEGIN
                                PRINT,' SR should be 2-vector'
                                RETURN
                                END
    ENDIF
;
; Read data
    DATA = READCALFITS(FILE)
    DEPVAR=''
    IF KEYWORD_SET(SLIT) THEN  DEPVAR = 'SLI' $
    ELSE IF KEYWORD_SET(MIRROR) THEN  DEPVAR = 'MIR' $
    ELSE PRINT,' Specify either /SLIT or /MIRROR'
;
; Read number of exposures
   NPOS = N_ELEMENTS(DATA.MIRPDATA)
   C=FLTARR(NPOS)
   SP=FLTARR(NPOS)
   MP=FLTARR(NPOS)
;
; Loop through exposures, filling CNTS and the position arrays
   FOR IEXP=0,NPOS-1 DO BEGIN
; Grazing incidence:
       IF (D EQ 'G') THEN BEGIN
                     ARRAY = DATA.DETDATA(*,IEXP)
; (assume this is 8192 array)
; Add up the specified pixels
                      SUM = 0.
                      FOR I=WR(0),WR(1) DO SUM = SUM + ARRAY(I)
                      C(IEXP) = SUM
       ENDIF ELSE BEGIN
; Normal incidence:
                  ARRAY = DETDATA(DATA,NW)
; Add up the specified pixels
                  SUM = 0.
                  FOR I=WR(0),WR(1) DO BEGIN
                       FOR J=SR(0),SR(1) DO SUM = SUM + ARRAY(I,J,IEXP)
                  ENDFOR
                  C(IEXP) = SUM
             ENDELSE
; Read the mirror or slit position and plot
       IF (DEPVAR EQ 'SLI') THEN SP(IEXP)=DATA.SLIPDATA(IEXP)  ELSE $
       IF (DEPVAR EQ 'MIR') THEN MP(IEXP)=DATA.MIRPDATA(IEXP)    
    ENDFOR       
; Now have arrays C and MP or SP of dimension NPOS
;
; Now plot them
    IF (DEPVAR EQ 'SLI') THEN PLOT,SP,C,XTITLE= ' Slit Position'  ELSE $
    IF (DEPVAR EQ 'MIR') THEN PLOT,MP,C,XTITLE= ' Mirror Position'
;
    RETURN
    END
