! copyright (c) 2008 Swedish Defence Research Agency (FOI)
! may not be copied or distributed without written permission from FOI



C	to compute certain Airy-function values, using the gil et al. 
C	routine aiz
C       note 1: the SLATEC routines daie,daide would be faster 
C               for real arguments, hence the "ccc commented" statements
C	note 2: the two pertinent functions are computed "separately", 
C	        it would be more efficient to lift out certain "common" 
C	        computations

C	Sven Ivansson  2006



	SUBROUTINE AIRY2E (Z, ALN, AIE,AIDE)
C	upon exit: 
C	  Ai(z)  = dexp(-aln) * aie
C	  Ai'(z) = dexp(-aln) * aide

	IMPLICIT NONE

	COMPLEX*16 Z

	REAL*8 ALN
	COMPLEX*16 AIE,AIDE

	INTEGER IERR
	REAL*8 X, DAIE,DAIDE, REZ,IMZ, REUT,IMUT
	COMPLEX*16 ZNOW, CZET,CSCALE

	ZNOW = Z

ccc	IF (DIMAG(ZNOW).EQ.0.0D0) THEN

ccc	  X = DREAL(ZNOW)
ccc	  IF (X.GT.0.0D0) THEN
ccc	    ALN = 0.6666666666666667D0 * X * DSQRT(X)
ccc	  ELSE
ccc	    ALN = 0.0D0
ccc	  END IF
ccc	  AIE = DAIE(X)
ccc	  AIDE = DAIDE(X)

ccc	ELSE

	  REZ = DREAL(ZNOW)
	  IMZ = DIMAG(ZNOW)

	  CZET = 0.6666666666666667D0 * ZNOW*CDSQRT(ZNOW)
	  CSCALE = CDEXP ( -DCMPLX(0.0D0,DIMAG(CZET)) )
	  ALN = DREAL(CZET)

	  CALL AIZ ( 1,2, REZ,IMZ, REUT,IMUT, IERR )
	  AIE = DCMPLX(REUT,IMUT)
	  IF (IERR.NE.0) THEN
	    WRITE(*,*) '%%% aiz 1, ierr=',IERR
	    WRITE(*,*) 'znow=',ZNOW
	    STOP 567
	  END IF
	  AIE = CSCALE * AIE
	  CALL AIZ ( 2,2, REZ,IMZ, REUT,IMUT, IERR )
	  AIDE = DCMPLX(REUT,IMUT)
	  IF (IERR.NE.0) THEN
	    WRITE(*,*) '%%% aiz 2, ierr=',IERR
	    WRITE(*,*) 'znow=',ZNOW
	    STOP 567
	  END IF
	  AIDE = CSCALE * AIDE

ccc	END IF

	RETURN
	END
