;+
; Project     : SOHO - CDS     
;                   
; Name        : PLAPCNT
;               
; Purpose     : For use in the calibration. Plots counts versus position on the
;             : aperture from calibration files. May overplot using key-word
;             : OVER when scale will be adjusted to superimpose on the plots
;             : of the apertures.
;               
; Explanation : Specified files will be read using READCALFITS and counts summed
;             : over pixels specified using wrange and srange
;               
; Use         : If called with /OVER, plot will be overplot, counts will be
;             : scaled and added to vertical position before plotting
;    
; Inputs      : det - 'G' or 'N' for grazing or normal incidence detector
;             : gas - 'a' or 'h' or 'n' for Ar, He, Ne
;             : frange - vector dimension 2 containing start and end numbers
;             :          of the files to be read (version no.)
;             : fnos - vector containing list of the file numbers
;             :   (use either frange or fnos)
;             : wrange - vector dimension 2 containing wavelength pixel limits 
;             : within the data window.
;             : srange - vector dimension 2 containing spatial pixel limits 
;             : within the data window
;             : serno - 4-char string containing serial number of sequence e.g.
;             :                  '6001'
;               
; Opt. Inputs : frange/fnos (see above)
;             : window - the number of the window in the data (NI data only)
;             : head - a four-character string. This is the file type.
;             : It may be used if this field in the file name is not the
;             : standard aperture format.
;               
; Outputs     : cnts - array containing the counts
;             : hpos - array containing the horizontal positions
;             : vpos - array containing the vertical positions
;               
; Opt. Outputs: scale - the scale applied to the counts if OVER is used
;               
; Keywords    : DET,GAS,FRANGE,FNOS,WRANGE,SRANGE,CNTS,HPOS,VPOS,OVER,SCALE
;
; Calls       : READCALFITS
;
; Common      :
;               
; Restrictions: 
;               
; Side effects: 
;               
; Category    : 
;               
; Prev. Hist. :
;
; Written     : B.Bromage  25.2.94.
;               
; Modified    : B.Bromage 13.12.94. Correcting minor bug - when file
;               is missing.
;
; Version     : 1.1
;-            
;
pro plapcnt,det=d,gas=g,frange=fr,fnos=fn,wrange=wr,srange=sr,cnts=c,hpos=hp,$
  head=head,vpos=vp,over=over,scale=scal,ver=ver,serno=ser,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 (G NE 'a') AND (G NE 'h') AND (G NE 'n') THEN BEGIN
                                PRINT, " GAS should be 'a','h' or 'n'"
                                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
;
; Form file header:
    IF (N_ELEMENTS(HEAD) EQ 0) THEN HEAD = 'A' + D + G + 'C' ELSE $
         IF(STRLEN(HEAD) NE 4) THEN BEGIN
                             PRINT,'HEAD should be 4-character string'
                             RETURN
                             END
;
; Get number of files to be read and array of file numbers
    NFILS = N_ELEMENTS(FN)
    IF (NFILS EQ 0) THEN BEGIN
                    IF(N_ELEMENTS(FR) EQ 0) THEN BEGIN
                                            PRINT,'Set FRANGE or FNOS'
                                            RETURN
                    ENDIF ELSE BEGIN
                            NFILS = FR(1) - FR(0) + 1
                            IF (NFILS LE 0) THEN BEGIN
                                           PRINT,' Error in FRANGE'
                                           RETURN
                            ENDIF
                            FN = INDGEN(NFILS) + FR(0)
                          ENDELSE
    ENDIF
; Now have array of NFILS file numbers in FN.
; Initialise output arrays and missing file counter.
    C = FLTARR(NFILS)
    HP= FLTARR(NFILS)
    VP= FLTARR(NFILS)
    DF = 0
; Start file loop:
    FOR LF = 0,NFILS-1 DO BEGIN
       JF = LF - DF
; Get file name and read it
       FILENO = STRTRIM(STRING(FN(JF)),2)
       LEN = STRLEN(FILENO)
       IF (LEN EQ 1) THEN FILENO = '0'+FILENO ELSE $
       IF (LEN NE 2) THEN BEGIN
                    PRINT,' INVALID FILENUMBER'
                    RETURN
       ENDIF
       FILE= HEAD + '_' + SER + '_' + '000' + '.' + FILENO
       FILE= CONCAT_DIR('$CDS_CAL_FITS',FILE)
       TEST = FINDFILE(FILE)
       IF (TEST(0) EQ '') THEN BEGIN
                                    NFILS = NFILS-1
                                    FOR I=JF,NFILS-1 DO FN(I)=FN(I+1)
                                    JF = JF-1
                                    DF = DF+1
                                    GOTO,SKIP
       ENDIF
;
       DATA = READCALFITS(FILE)          
; Grazing incidence:
       IF (D EQ 'G') THEN BEGIN
                      ARRAY = DATA.DETDATA  
; (assume this is array containing the GI window)
; Add up the specified pixels
                      SUM = 0.
                      FOR I=WR(0),WR(1) DO SUM = SUM + ARRAY(I)
                      C(JF) = SUM
       ENDIF ELSE BEGIN
; Normal incidence:
; select the window 
                  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)
                  ENDFOR
                  C(JF) = SUM
             ENDELSE
; Read the horizontal and vertical positions on the aperture
       HP(JF) = FLOAT(DATA.HEADER.OPS_L)
       VP(JF) = FLOAT(DATA.HEADER.OPS_R)  
         IF(D EQ 'N') THEN VP(JF) = VP(JF) + 118.25 $
                      ELSE VP(JF) = VP(JF) - 118.25
; now close units opened by readcalfits
       CLOSE,/ALL
SKIP:
    ENDFOR       
; Now have arrays C,HP,VP of dimension NFILS. Correct dimension if
; there were missing files:
    C = C(0:NFILS-1)
    HP= HP(0:NFILS-1)
    VP= VP(0:NFILS-1)
;
;Plotting:
    IF KEYWORD_SET(OVER) THEN BEGIN
; Scale counts to between 0. and 10., then add to VP before plotting
; First ensure C >= 0.
                         C = C*(C GE 0.)
                         SCAL = 10./MAX(C)
                         C = C*SCAL + VP
; and overplot
                         OPLOT,HP,C,PSYM=2
; otherwise just plot
    ENDIF ELSE           PLOT,HP,C,PSYM=2

    RETURN
    END
