      SUBROUTINE SEARCH(LU,JN,DX,YCR,STR,IPX,IWDL,IWDR,NWD,IN,ISCH,SENS
     1,NUM,IPF,NT,XL,AVG,M)
C                                                           **********
C                                                           **SEARCH**
C                                                           **********
C MODIFIED BY RAY GOEHNER 7/24/1985
C CALLED NSEARCH MADE TO CONSIDER ENTIRE PEAK
C
C SEARCH DOES A SECOND DIFFERENCE SEARCH FOR PEAKS
C SEARCH IS A SUBROUTINE OF DIFPLOT
C WRITTEN BY RAY GOEHNER (GE) AND MODIFIED BY TERRY QUICK (LLL)
C
C  NT NUMBER OF PTS FOR PEAK TOP SEARCH
C  M NUMBER OF PKS FOUND IN SEARCH
C
      BYTE ND2,ISCH
      DIMENSION AWD(10),A(25),IPX(1),IWDL(10),IWDR(10)
      DATA I3,I5/2*0/
C
      IF(ISCH.EQ.0) RETURN
C
      DR2L=SENS
      ND2=ISCH
      XD2=ISCH     ! NEEDED FOR COMPATIBILITY WITH VAX
      DY=1./YCR
      AM=AVG/YCR  ! CONVERT BACK TO COUNTS FROM CPS
      IF(IN.EQ.'XR')GO TO 200
C      WRITE(6,8)
C    8 FORMAT(/2X,' N 2THETA DSPACE HEIGHT   AREA    W4'/)
      GO TO 205
  200 CONTINUE
C  200 WRITE(6,201)
C  201 FORMAT(/2X,' N 2THETA LAMDA      HEIGHT    W4'/)
  205 XND2=0.0
      DO 75 JJ=1,NWD
   75 AWD(JJ)=0.0
      DO 2 JJ=1,100
    2 IPX(JJ)=-1
      ALI=9999999.
      NN=0
      DRL=0.0
      DRC=0.0
      BK=0.0
      N=0
      IST=1
      IB=0
      M2=ND2/2
      L=-M2
      AREA=0.0
      M=0
      X=0.0
      AL=4100.0
      M3=M2+1
      XM2=M2
      M4=ND2-1
      JAA=0
      D2PK=0.0
      CAREA=0.0
      LSP=0
      PK=0.0
      SDB=SQRT(ABS(AM*DY))
      SA=SDB
      DM=0.0
      SD=SDB/XD2    ! CHANGED FOR VAX COMPATIBILITY
      SDM=DR2L
      DR2L=-DR2L*SD

C SA STANDARD DEVIATION IN BACKGROUND (BKGR)
C AM MEAN OF BKGR
C DM MEAN OF 2ND DIFFERENCE
C SD STANDARD DEVIATION OF 2ND DIFFERENCE OVER BKGR
C SDM REAL DATA SENSITIVITY
C DR2L 2ND DIFF SENSITIVITY
C NUM NUMBER OF DECREASING PTS IN 2ND DIFF SIGNAL

C ****************************************************
C *********** WORKING PORTION ***********************
C DO LOOP IS EXECUTED ONLY 1ST TIME
C I.E. IST=ND2 AFTER 1ST TIME
C THIS IS DONE TO LOAD A FOR THE CALCULATION
C OF D2 AT M3
      JPF=IPF/2
      LPF=JN+JPF
   16 DO 18 I=IST,ND2
      NN=NN+1
      L=L+1
      IF(NN.GT.JN) GO TO 95
      READ(LU'NN) B
      A(I)=B*DY
C ******* XC IS CURRENT X VALUE *****************
      XC=STR+DX*(X-XM2)
      X=X+1.0
   18 CONTINUE
      IST=ND2
C THE REAL DATA COULD BE SMOOTHED HERE
      CALL DER2(A,D2,ND2)
C *******************************************************
C ********************************************************
C FIND THE WINDOWED AREAS
C *************************************************
C ******  H IS HEIGHT ABOVE MEAN OF DEAD VOLUME  ***********
   66 H=A(M3)
      IF(NWD.EQ.0) GO TO 60
      DO 74 JJ=1,NWD
      IF(NN.LT.IWDL(JJ)) GO TO 74
      IF(NN.GT.IWDR(JJ)) GO TO 74
      AWD(JJ)=AWD(JJ)+H*DX*YCR
   74 CONTINUE
   60 DRC=D2
      AL=A(M3)
C ************************************************************
C FIND PEAKS BY THEIR 2ND DERIVATIVES
      IF(AL.GT.ALI) GO TO 31

C DRC  CURRENT 2ND DIFF
C DRL  LAST 2ND DIFF

      IF(DRC.LT.0.0) GO TO 20
      GO TO 30
C   20 IF(DRC.GE.DRL) GO TO 30
20	IF(DRC.LT.D2PK) THEN !ADDED 7/24/1985
	JPK=NN-M2
	D2PK=DRC
	ENDIF
      XX=XC
C IF YOU WANT HEIGHT AT 2ND DERIVATIVE MINIMUM REMOVE
C BOTH (H.GT.PK) PK=H STATEMENTS
      IF(H.GT.PK) PK=H
C PK=H
      PPK=H
C      JPK=NN-M2
      AREA=AREA+DRC
      CAREA=CAREA+H
C      D2PK=DRC
      N=N+1
      GO TO 33
   30 IF(N.LT.NUM) GO TO 31
      IF(D2PK.GT.DR2L) GO TO 31
      IF(H.GT.PK) PK=H
   71 IF(PK.LE.(SDM*SA)) GO TO 31
C  NO PEAK IF LESS THAN ONE STANDARD DEVIATION OF BACKGROUND
      IF(SDM.LT.1.0.AND.PK.LT.SA) GO TO 31
      IF(AREA.GT.-1.0) GO TO 31
      P=-.51*AREA*N
      IF(P.LE.(SDM*SA)) GO TO 31
      P=P*YCR
C THE AREA UNDER THE PEAK HAS TO BE > 1 COUNT
      IF(CAREA.LE.1.0) GO TO 31
C HALF BREADTH BY ZERO OF 2ND DERIVATIVE
      W2A=1.1744*DX
      W2=W2A*N
      CAREA=W2A*YCR*CAREA
      DE=XX
C   FIND TOP OF THE PEAK
      IF(NT.EQ.0) GO TO 79
      ZMAX=-32000.
      IJ1=JPK -NT
      IJ2=JPK + NT
      DO 77 IJP=IJ1,IJ2
      J=IJP
      READ(LU'J) B
      B=B*DY   ! CONVERT TO COUNTS
      IF(B.LT.ZMAX)GO TO 77
      ZMAX=B
      JPK=IJP
   77 CONTINUE
   79 IF(IPF.EQ.0) GO TO 76
C  IPF CENTROID TYPE FIT
  100 IF(JPK.LE.JPF)GO TO 140
      IF(JPK.GE.LPF)GO TO 140
      IJ1=JPK-JPF !JPK PK POSITION JPF=IPF/2
      IJ2=JPK+JPF
      XI=0.0
      XX=0.0
      DO 110 I=IJ1,IJ2
      J=I
      READ(LU'J) B
      B=B*DY   ! CONVERT TO COUNTS
      XI=XI+B
  110 XX=XX+B*((I-1)*DX+STR)
      DE=XX/XI
      GO TO 76
  140 WRITE(6,3) DE,XC
    3 FORMAT(/1X,'PEAK NEAR ',F6.3,
     X' DEG CANNOT BE FIT .'/1X,
     X'IT IS TOO CLOSE TO THE EDGE OF THE PATTERN.'/1X,
     X'A 2THETA VALUE OF ',F6.3,' DEG WILL BE USED.'/)
      DE=XC
   76 I3=0
      I5=0
      PK=PK*YCR
	IF(IN.EQ.'ED') GO TO 211 !  3/1985 RPG
      IF(DE.LE.0.0) GO TO 31
      IF(DE.GE.180.0) GO TO 31
      IF(IN.EQ.'XR') GO TO 210
C  SDE BECOMES LAMDA INSTEAD OD DSPACE FOR XRF SPECTRUM
      SDE=XL/(2.0*SIN(0.008726646*DE))
      GO TO 211
  210 SDE=XL*SIN(0.008726646*DE)
  211 W4=CAREA/PK
      M=M+1
      IPX(M)=JPK
      IF(M.GT.100) GO TO 95
      IF(IN.EQ.'ED') THEN
	Q=12.4/DE
	WRITE(8,80) M,DE,Q,PK,CAREA,P,W2,W4
80	FORMAT(1X,I3,F9.3,F9.4,5F9.2)
	GO TO 31
      ENDIF
C      IF(IN.EQ.'DI') WRITE(6,11) M,DE,SDE,PK,CAREA,W4
C   11 FORMAT(1X,I3,F7.2,F7.3,F7.0,F7.1,F6.2)
C      IF(IN.EQ.'XR') WRITE(6,203) M,DE,SDE,PK,W4
      Q=1.0/(SDE*SDE)
      IF(IN.EQ.'XR') Q=12.4/SDE
C  203 FORMAT(1X,I3,F7.2,F7.3,F11.0,F6.2)
      WRITE(8,5) M,DE,SDE,Q,PK,CAREA,P,W2,W4
    5 FORMAT(1X,I3,F9.3,2F9.4,3F9.1,2F9.3)
   31 N=0
      AREA=0.0
      CAREA=0.0
      PK=0.0
      D2PK=0.0
C ******************************************************
   33 DO 34 I=1,M4
      J=I+1
   34 A(I)=A(J)
      DRL=DRC
      GO TO 16
   95 CONTINUE
      IF(NWD.EQ.0) GO TO 181
      WRITE(8,183)
C      WRITE(6,183)
  183 FORMAT(12X,'WDL',12X,'WDR',11X,'AREA')
      DO 180 I=1,NWD
      SSA=IWDL(I)
      SSA=STR+SSA*DX
      SSD=IWDR(I)
      SSD=STR+SSD*DX
      WRITE(8,182) SSA,SSD,AWD(I)
  180 CONTINUE
C  180 WRITE(6,182) SSA,SSD,AWD(I)
  182 FORMAT(1X,3F15.1)
  181 CONTINUE
C      WRITE(6,78) AM,SA,DM,SD
C   78 FORMAT(1X,'MEAN Y=',F10.3,' +OR- ',E9.3,
C     &'  MEAN D2Y=',F10.4,' +OR- ',E9.3)
      CALL CLOSE(8)
      RETURN
      END
