      SUBROUTINE QSIMP2(ainteg2,as,be,S)
	implicit real*8(a-h,o-z)
	common		elong,ass,bee, 		! extra parameters used by make_model
     &                  zss,zee,xs,xe,ccc,PI,au,au2,anu,alamb,freq,twopi,qxf,
     &			L1,L2,L3,L4,L5,L6,L7,L8,L9,A1,A2,A3,A4,A5,A6,A7,A8,A9,
     &			z,z0,R,p,Vx,Xnorm,XMnorm,ahpow,anoise,
     &			er1,er2,er3,er4,er5,er6,er7,er8,er9,er10,
     &			ijsing,iFs(500),iFt(500),iFd(500),aispecq(500),ispecq1,iq1(500),ispecq2,iq2(500)
        PARAMETER (EPS=1.E-6, JMAX=20)
C       PARAMETER (EPS=1.0E-3, JMAX=15)
C
	external ainteg
C
        save OST, OS
C
C	print *, 'inside qsimp2'

      OST=-1.E30
      OS= -1.E30
      DO 11 J=1,JMAX
        CALL TRAPZD2(ainteg2,as,be,ST,J)
        S=(4.*ST-OST)/3.
        IF (ABS(S-OS).LT.EPS*ABS(OS)) RETURN
        OS=S
        OST=ST
C	print *, 'In qsimp2',	J,ST,S
11    CONTINUE
          write(*,'(A,I3,3E15.7)') 'Error - at end of qsimp2',J,as,be,S
          iq2(ispecq2) = iq2(ispecq2) + 1
C      PAUSE 'QSIMP2 Too many steps.'
      RETURN
      END
