* Fri Jan 11 15:21:53 EST 1991 /herschel/h1/rlm/src/test calanl.f
c
c rlm 2/19/88
c
      subroutine calanl(ANS,JTB,JDATA,JNE,LSTAT,JTLMOD,JCLK,TEMP,
     *IEOD,IL,ITYPE,ipls,ipltdc,RMARK,thresh)
      COMMON/ANSWER/aANS(150),CURRNT(512)
      INTEGER*2 JTB(6),JDATA(512),JNE,JTLMOD,JCLK
      DIMENSION TEMP(3)
      dimension curint(513),rmark(3)
      dimension residual(513)
      dimension calcur(12)
      dimension fact(128,2)
      data calcur/3.61 e1, 1.64 e2, 1.35 e3, 1.81 e3, 3.54 e3,
     + 1.61 e4, 1.33 e5, 1.77 e5, 1.78 e5, 8.07 e5, 6.65 e6,
     + 8.88 e6/
      data idsrn/6/
c
c Calabration analysis not yet implemented
c 
      if (ipls .ne. 7) go to 10 
c
c Modulator Voltage Monitor
c
      write (6,210)
  210 format (1h ,'Modulator Voltage Monitor Mode')
      write (6,211)
  211 format (1h ,'Analysis not yet implemented.')
      go to 1000
   10 if (ipls .ne. 10) go to 20
c
c Current calibration - Modulator Off
c
      write (6,220)
  220 format (1h ,'Current Calibration - Modulator Off')
      write (6,211)
c
c L and E2 modes inject calibration currents I0,I1,I2,I3
c in channels 1-4,5-8,9-12,13-16 to check noise at low end.
c E1 injects I0-I11 in channels 1-12, and I0-I3 in channels 13-16;
c M injects I0-I11 in 12 channels 10 times (channels 1-120) and
c I0-I7 in 8 channels 121-128.
c
c --> Offset calibration in L and E2
c --> Gain calibration in M and E1
c Data numbers telemetered are for these injected currents and the 
c current amplifier gain, capacitor, and integration time of the 
c instrument.
c
c Calibration currents are as follows (from Plasma Science 
c Experiment Preliminary Design Review, January 15, 1975)
c
c
c I0 = 3.61 e -14 = 3.61 e1 fA
c I1 = 1.64 e -13 = 1.64 e2 fA
c I2 = 1.35 e -12 = 1.35 e3 fA
c I3 = 1.81 e -12 = 1.81 e3 fA
c I4 = 3.54 e -12 = 3.54 e3 fA
c I5 = 1.61 e -11 = 1.61 e4 fA
c I6 = 1.33 e -10 = 1.33 e5 fA
c I7 = 1.77 e -10 = 1.77 e5 fA
c I8 = 1.78 e -10 = 1.78 e5 fA
c I9 = 8.07 e -10 = 8.07 e5 fA
c I10 = 6.65 e -9 = 6.65 e6 fA
c I11 = 8.88 e -9 = 8.88 e6 fA
c
      write(6,500)
  500 format (1h ,'CALIBRATION CURRENTS (fA):'/
     +,5x,'I0',5x,'I1',5x,'I2',5x,'I3',5x,'I4',5x,'I5',5x,'I6',5x,
     +'I7',5x,'I8',5x,'I9',5x,'I10',5x,'I11')
      if (jtlmod .eq. 2 .or. jtlmod .eq. 3)
     +write(6,501) (calcur(i),i=1,12)
  501 format (12e12.3/)
      if (jtlmod .eq. 1 .or. jtlmod .eq. 4)
     +write(6,502) (calcur(i),i=1,4)
  502 format (4e12.3/)
      go to 1000
   20 if (ipls .ne. 11) go to 30
c
c Current calibration - Modulator On
c
      write (6,230) 
  230 format (1h ,'Current Calibration - Modulator On') 
      write (6,231)
  231 format(3h   ,'Plasma + Cal. Currents for background',
     +' correction.')
      write (6,211)
c
c See description under current calibration - modulator off;
c same injected currents are used plus any modulated current at
c the collector plate (plus modulator induced noise currents).
c
      go to 1000
   30 if ((ipls .ne. 14) .and. (ipls .ne. 2)) go to 40
c
c Plasma - Modulator Off
c
      write (6,240)
  240 format (1h ,'Plasma - Mod. Off')
      write (6,211)
      go to 1000
   40 continue
      if (((ipls .eq. 15) .or. (ipls .eq. 3)) .and. 
     +(ipltdc .eq. 1)) go to 41
      if ((ipls .ne. 0) .and. (ipls .ne. 1) .and. 
     +(ipls .ne. 12) .and. (ipls .ne. 13)) go to 50
c
c DC Return Current
c
c ipltdc is set to: 1 for plasma mode for which DC return
c   spectra are to be simulated and 2 for DC return mode in which 
c   A and D cups are to be integrated for direct comparison with 
c   DC spectra to obtain correct count to current conversion 
c   factor.
c
      if ((ipltdc .ne. 1) .and. (ipltdc .ne. 2)) go to 300
c
c Electron data in plasma mode for comparison with 
c DC return currents; change sign on currents to 
c make them negative.
c
c Take plasma mode current and synthesize DC return 
c currents by summing all current acquired above a 
c given (by the channel number) energy threshold.
c
c  March 25, 1988 
c The best determination from the electrical schematics (and 
c talking with A. J. Lazarus) indicates that the telemetered 
c DC return current is an average DC current during the 
c measurement time (integration time) as there is no 
c apparent synchronous detection of theses currents.  The code 
c is modified to take account of this.  Suppose the current 
c present above voltage level V,k is I,k then
c
c IPLS,k=I,k - I,k+1
c IDC,k =1/2 (I,k + I,k+1)
c
c          inf
c ==> I,k= SUM IPLS,n
c          n=k
c
c where inf --> jne (i.e.,there is no information in the plasma 
c                    data of the direct contribution from higher
c                    energies)
c
c                           inf
c ==> IDC,k = 1/2 IPLS,k +  SUM IPLS,n
c                           n=k+1
c          
c
c First initialize array curint
c
   41 continue
      do 304 i=1,jne*4
      curint(i)=0.0
  304 continue
c
c Integrate current; N.B. THRESHOLD CURRENTS 
c ARE NOT INCLUDED IN SUM
c
      sign = 1.0
      if ((jtlmod .eq. 3) .or. (jtlmod .eq. 4)) sign = -1.0
c
c Option to integrate in DC return only for positive ion 
c spectra
c
      if (ipltdc .eq. 2 .and. sign .lt. 0.0) go to 300
c
c Synthesize currents for E1, E2, and A cup ion modes
c
      do 306 k=1,jne
      indx=K
      do 3060 indx2=indx,jne
      if (indx2 .eq. indx) factor=0.5 
      if (indx2 .ne. indx) factor=1.0
      if (currnt(indx2) .gt. rmark(1)) 
     +curint(indx)=curint(indx)+currnt(indx2)*sign*factor
 3060 continue
  306 continue
      if (jtlmod .ge. 3) go to 305
c
c Synthesize currents for D cup ion modes
c
      jne2=jne*4
      do 307 k=1,jne 
      indx=K+JNE*3
      do 3070 indx2=indx,jne2
      if (indx2 .eq. indx) factor=0.5
      if (indx2 .ne. indx) factor=1.0
      if (currnt(indx2) .gt. rmark(1)) 
     +curint(indx)=curint(indx)+currnt(indx2)*factor 
 3070 continue
  307 continue
  305 continue
      if (jtlmod .lt. 3) go to 308
c
c Reload currnt for E1 and E2 data
c
      do 309 i=1,16
      currnt(i)=curint(i)
  309 continue
      go to 300
  308 continue
c
c currnt loaded with integrated current if plasma data
c
      do 310 i=1,jne
c
c Load B cup in currnt with synthesized A cup DC Returns
c if plasma mode; if already DC return overwrite plasma 
c data in A cup if this option is selected.
c
      if (ipltdc .eq. 1) currnt(i+jne)=curint(i)
      if (ipltdc .eq. 2) currnt(i)=curint(i)
c 
c Load C cup in currnt with synthesized D cup DC Returns 
c if plasma mode; if already DC return overwrite plasma 
c data in D cup if this option is selected. 
c 
      if (ipltdc .eq. 1) currnt(i+jne*2)=curint(i+jne*3)
      if (ipltdc .eq. 2) currnt(i+jne*3)=curint(i+jne*3)
  310 continue    
c
c Option for comparison of plasma and DC return currents for
c calibration purposes
c
      if (ipltdc .ne. 2) go to 300
c
c First compare DC and integrated plasma current in A cup;
c subtract off photoelectrons due to suppressor inefficiency.
c
c A cup DC returns are in B cup, so adjust these
c by subtracting off current in highest B cup channel
c
c D cup DC returns are in C cup, so adjust these
c by subtracting off current in highest C cup channel
c
      do 400 i=1,4*jne
      residual(i)=currnt(i)
  400 continue
      do 401 j=1,jne
      i=j+jne
      k=j+2*jne
      ii=2*jne
      kk=3*jne
      if (currnt(ii) .gt. 7.5e4) residual(i)=residual(i)-residual(ii)
      if (residual(i) .eq. 0.0) residual(i)=7.5e4
      if (currnt(kk) .gt. 7.5e4) residual(k)=residual(k)-residual(kk)
      if (residual(k) .eq. 0.0) residual(k)=7.5e4
  401 continue
c
c Integrated plasma data is in A cup
c Find normalization by dividing DC return spectra by integrated spectra
c
c Integrated plasma data is in D cup
c Find normalization by dividing DC return spectra by integrated spectra
c
      do 405 j=1,jne
      if (residual(j) .lt. 7.5e4) residual(j)=7.5e4
      l=j+3*jne
      if (residual(l) .lt. 7.5e4) residual(l)=7.5e4
      i=j+jne
      k=j+2*jne
      if (residual(j) .ge. 7.5e4) fact(j,1)=residual(i)/residual(j)
      if (residual(l) .ge. 7.5e4) fact(j,2)=residual(k)/residual(l)
  405 continue
c
c Write out results
c
      write (6,408)
  408 format (1h ,'A cup: DC return / integrated plasma')
      write (6,406) (fact(j,1), j=1,jne)
      write (6,407)
  407 format (1h ,'D cup: DC return / integrated plasma')
      write (6,406) (fact(j,2), j=1,jne)
  406 format (1p,8e12.4)
  300 continue
c     call idcsp(ANS,JTB,JDATA,JNE,LSTAT,JTLMOD,JCLK,
c    +TEMP,ipls)
      go to 1000
   50 continue
c
c Illegal Status
c
      write (6,100) ipls
  100 format (1h ,'Illegal Status; IPLS = ',i5)
c     call voyprt (ANS,JTB,JDATA,JNE,LSTAT,JTLMOD,JCLK,TEMP,
c    +IDSRN)
c
c Return to MJSANL
c
 1000 continue
      return
      end 
