import conf
import numpy
import sys
from Calcn import *
from Casstore import *
def CONTROL(I,NVAC,ICON):
	# IMPLICIT #real*8(A-H,O-Z)
	# IMPLICIT #integer*8(I-N)
	# CHARACTER*6 SCR(17),SCR1(17)
	# c
	def get_globals():
		KGAS=conf.KGAS
		LGAS=conf.LGAS
		DETEFF=conf.DETEFF
		EXCWGHT=conf.EXCWGHT
		LCMP=conf.LCMP
		LCFLG=conf.LCFLG
		LRAY=conf.LRAY
		LRFLG=conf.LRFLG
		LPAP=conf.LPAP
		LPFLG=conf.LPFLG
		LBRM=conf.LBRM
		LBFLG=conf.LBFLG
		LPEFLG=conf.LPEFLG
		ELEV=conf.ELEV
		NSDEG=conf.NSDEG
		AA=conf.AA
		BB=conf.BB
		SCR=conf.SCR
		SCR1=conf.SCR1
		PRSH=conf.PRSH
		ESH=conf.ESH
		AUG=conf.AUG
		RAD=conf.RAD
		PRSHBT=conf.PRSHBT
		IZ=conf.IZ
		INIOCC=conf.INIOCC
		ISHLMX=conf.ISHLMX
		AMZ=conf.AMZ
		NOCC=conf.NOCC
		AUGR=conf.AUGR
		RADR=conf.RADR
		IONSUM=conf.IONSUM
		IFLSUM=conf.IFLSUM
		ESTORE=conf.ESTORE
		EPHOTON=conf.EPHOTON
		DRXE=conf.DRXE
		DRYE=conf.DRYE
		DRZE=conf.DRZE
		DRX=conf.DRX
		DRY=conf.DRY
		DRZ=conf.DRZ
		IONSUM1=conf.IONSUM1
		IFLSUM1=conf.IFLSUM1
		ESTOR1=conf.ESTOR1
		EPHOTG1=conf.EPHOTG1
		DRXE1=conf.DRXE1
		DRYE1=conf.DRYE1
		DRZE1=conf.DRZE1
		DRX1=conf.DRX1
		DRY1=conf.DRY1
		DRZ1=conf.DRZ1
		IONSUM2=conf.IONSUM2
		IFLSUM2=conf.IFLSUM2
		ESTOR2=conf.ESTOR2
		EPHOTG2=conf.EPHOTG2
		DRXE2=conf.DRXE2
		DRYE2=conf.DRYE2
		DRZE2=conf.DRZE2
		DRX2=conf.DRX2
		DRY2=conf.DRY2
		DRZ2=conf.DRZ2
		IONSUM3=conf.IONSUM3
		IFLSUM3=conf.IFLSUM3
		ESTOR3=conf.ESTOR3
		EPHOTG3=conf.EPHOTG3
		DRXE3=conf.DRXE3
		DRYE3=conf.DRYE3
		DRZE3=conf.DRZE3
		DRX3=conf.DRX3
		DRY3=conf.DRY3
		DRZ3=conf.DRZ3
		IONSUM4=conf.IONSUM4
		IFLSUM4=conf.IFLSUM4
		ESTOR4=conf.ESTOR4
		EPHOTG4=conf.EPHOTG4
		DRXE4=conf.DRXE4
		DRYE4=conf.DRYE4
		DRZE4=conf.DRZE4
		DRX4=conf.DRX4
		DRY4=conf.DRY4
		DRZ4=conf.DRZ4
		IONSUM5=conf.IONSUM5
		IFLSUM5=conf.IFLSUM5
		ESTOR5=conf.ESTOR5
		EPHOTG5=conf.EPHOTG5
		DRXE5=conf.DRXE5
		DRYE5=conf.DRYE5
		DRZE5=conf.DRZE5
		DRX5=conf.DRX5
		DRY5=conf.DRY5
		DRZ5=conf.DRZ5
		ECMP=conf.ECMP
		ECDRX=conf.ECDRX
		ECDRY=conf.ECDRY
		ECDRZ=conf.ECDRZ
		XCPOS=conf.XCPOS
		YCPOS=conf.YCPOS
		ZCPOS=conf.ZCPOS
		KCGAS=conf.KCGAS
		LCGAS=conf.LCGAS
		ICSHELL=conf.ICSHELL
		globals().update(locals())
	#
	get_globals()
	def update_globals():
		conf.KGAS=KGAS
		conf.LGAS=LGAS
		conf.DETEFF=DETEFF
		conf.EXCWGHT=EXCWGHT
		conf.LCMP=LCMP
		conf.LCFLG=LCFLG
		conf.LRAY=LRAY
		conf.LRFLG=LRFLG
		conf.LPAP=LPAP
		conf.LPFLG=LPFLG
		conf.LBRM=LBRM
		conf.LBFLG=LBFLG
		conf.LPEFLG=LPEFLG
		conf.ELEV=ELEV
		conf.NSDEG=NSDEG
		conf.AA=AA
		conf.BB=BB
		conf.SCR=SCR
		conf.SCR1=SCR1
		conf.PRSH=PRSH
		conf.ESH=ESH
		conf.AUG=AUG
		conf.RAD=RAD
		conf.PRSHBT=PRSHBT
		conf.IZ=IZ
		conf.INIOCC=INIOCC
		conf.ISHLMX=ISHLMX
		conf.AMZ=AMZ
		conf.NOCC=NOCC
		conf.AUGR=AUGR
		conf.RADR=RADR
		conf.IONSUM=IONSUM
		conf.IFLSUM=IFLSUM
		conf.ESTORE=ESTORE
		conf.EPHOTON=EPHOTON
		conf.DRXE=DRXE
		conf.DRYE=DRYE
		conf.DRZE=DRZE
		conf.DRX=DRX
		conf.DRY=DRY
		conf.DRZ=DRZ
		conf.IONSUM1=IONSUM1
		conf.IFLSUM1=IFLSUM1
		conf.ESTOR1=ESTOR1
		conf.EPHOTG1=EPHOTG1
		conf.DRXE1=DRXE1
		conf.DRYE1=DRYE1
		conf.DRZE1=DRZE1
		conf.DRX1=DRX1
		conf.DRY1=DRY1
		conf.DRZ1=DRZ1
		conf.IONSUM2=IONSUM2
		conf.IFLSUM2=IFLSUM2
		conf.ESTOR2=ESTOR2
		conf.EPHOTG2=EPHOTG2
		conf.DRXE2=DRXE2
		conf.DRYE2=DRYE2
		conf.DRZE2=DRZE2
		conf.DRX2=DRX2
		conf.DRY2=DRY2
		conf.DRZ2=DRZ2
		conf.IONSUM3=IONSUM3
		conf.IFLSUM3=IFLSUM3
		conf.ESTOR3=ESTOR3
		conf.EPHOTG3=EPHOTG3
		conf.DRXE3=DRXE3
		conf.DRYE3=DRYE3
		conf.DRZE3=DRZE3
		conf.DRX3=DRX3
		conf.DRY3=DRY3
		conf.DRZ3=DRZ3
		conf.IONSUM4=IONSUM4
		conf.IFLSUM4=IFLSUM4
		conf.ESTOR4=ESTOR4
		conf.EPHOTG4=EPHOTG4
		conf.DRXE4=DRXE4
		conf.DRYE4=DRYE4
		conf.DRZE4=DRZE4
		conf.DRX4=DRX4
		conf.DRY4=DRY4
		conf.DRZ4=DRZ4
		conf.IONSUM5=IONSUM5
		conf.IFLSUM5=IFLSUM5
		conf.ESTOR5=ESTOR5
		conf.EPHOTG5=EPHOTG5
		conf.DRXE5=DRXE5
		conf.DRYE5=DRYE5
		conf.DRZE5=DRZE5
		conf.DRX5=DRX5
		conf.DRY5=DRY5
		conf.DRZ5=DRZ5
		conf.ECMP=ECMP
		conf.ECDRX=ECDRX
		conf.ECDRY=ECDRY
		conf.ECDRZ=ECDRZ
		conf.XCPOS=XCPOS
		conf.YCPOS=YCPOS
		conf.ZCPOS=ZCPOS
		conf.KCGAS=KCGAS
		conf.LCGAS=LCGAS
		conf.ICSHELL=ICSHELL
		globals().update(locals())
	#  CALCULATES AUGER AND FLUORESCENCE CASCADE FOR VACANCY IN ICSHELL
	#
	IFIRST=1
	ISECOND=2
	ICONPH=1
	X0=XCPOS[NVAC]
	Y0=YCPOS[NVAC]
	Z0=ZCPOS[NVAC]
	DRXE[NVAC][1]=ECDRX[NVAC]
	DRYE[NVAC][1]=ECDRY[NVAC]
	DRZE[NVAC][1]=ECDRZ[NVAC]
	# 
	# CALCULATE CASCADE EVENT
	IDUM1=KCGAS[NVAC]
	IDUM2=LCGAS[NVAC]
	IDUM3=ICSHELL[NVAC]
	ADUM1=ECMP[NVAC]
	CALC(I,NVAC,IDUM1,IDUM2,ADUM1,IDUM3,ICON)
	# STORE EVENT 
	#     Z0=DIST
	CASSTORE(I,NVAC,X0,Y0,Z0)
	# IF INCLUDE PHOTOABSORPTION OF FLUORESENCE : 
	if(ICONPH == 1):
		if(IFLSUM[NVAC]== 0):
			# GO TO 10
			update_globals()
			return 
		# LOOP AROUND FIRST GEN FLUORESCENCE
		IFL1=IFLSUM[NVAC]
		for L1 in range(1,IFL1):
			EPH=EPHOTON[NVAC][L1]
			#
			# CALC ABSORPTION POSITION X1,Y1,Z1 FOR FLUORESENCE PHOTON OF ENERGY EPH
			# WITH ABSORPTION IN SHELL ISHELL1 IN GAS KGAS1 COMPONENT LGAS1 
			ABSO(ISECOND,EPH,ISHELL1,KGAS1,LGAS1,DIST1)
			# LOW ENERGY PHOTON
			if(ISHELL1 == -1):
				# GO TO 9
				continue
			#
			# CALCULATE CASCADE IN GAS 
			CALC1(I,NVAC,KGAS1,LGAS1,EPH,ISHELL1,L1)
			# STORE EVENT
			CSSTF1(I,NVAC,L1,DIST1) 
		# 9  CONTINUE 
		#
		# SECOND GENERATION FLUORESCENCE
		if(IFLSUM1[NVAC]== 0):
			# GO TO 10
			update_globals()
			return 
		# LOOP AROUND SECOND GEN FLUORESCENCE
		IFL2=IFLSUM1[NVAC]
		for L2 in range(1,IFL2):
			EPH=EPHOTG1[NVAC][L2]
			#
			# CALC ABSORPTION POSITION X2,Y2,Z2 FOR FLUORESENCE PHOTON OF ENERGY EPH
			# WITH ABSORPTION IN SHELL ISHELL2 IN GAS KGAS2 COMPONENT LGAS2
			ABSO(ISECOND,EPH,ISHELL2,KGAS2,LGAS2,DIST2)
			# LOW ENERGY PHOTON
			if(ISHELL2 == -1):
				# GO TO 8
				continue
			#
			# CALCULATE CASCADE IN GAS 
			CALC2(I,NVAC,KGAS2,LGAS2,EPH,ISHELL2,L2)
			# STORE EVENT
			CSSTF2(I,NVAC,L2,DIST2)               
		# 8  CONTINUE    
		# 
		# THIRD GENERATION FLUORESCENCE
		if(IFLSUM2[NVAC]== 0):
			# GO TO 10
			update_globals()
			return
		# LOOP AROUND THIRD GEN FLUORESCENCE
		IFL3=IFLSUM2[NVAC]
		for L3 in range(1,IFL3):
			EPH=EPHOTG2[NVAC][L3]
			#
			# CALC ABSORPTION POSITION X3,Y3,Z3 FOR FLUORESENCE PHOTON OF ENERGY EPH
			# WITH ABSORPTION IN SHELL ISHELL3 IN GAS KGAS3 COMPONENT LGAS3
			ABSO(ISECOND,EPH,ISHELL3,KGAS3,LGAS3,DIST3)
			# LOW ENERGY PHOTON
			if(ISHELL3 == -1):
				# GO TO 7
				continue
			#
			# CALCULATE CASCADE IN GAS 
			CALC3(NVAC,KGAS3,LGAS3,EPH,ISHELL3,L3)
			# STORE EVENT
			CSSTF3(I,NVAC,L3,DIST3)               
		# 7  CONTINUE      
		# 
		# FOURTH GENERATION FLUORESCENCE
		if(IFLSUM3[NVAC]== 0):
			# GO TO 10
			update_globals()
			return
		# LOOP AROUND FOURTH GEN FLUORESCENCE
		IFL4=IFLSUM3[NVAC]
		for L4 in range(1,IFL4):
			EPH=EPHOTG3[NVAC][L4]
			#
			# CALC ABSORPTION POSITION X4,Y4,Z4 FOR FLUORESENCE PHOTON OF ENERGY EPH
			# WITH ABSORPTION IN SHELL ISHELL4 IN GAS KGAS4 COMPONENT LGAS4
			ABSO(ISECOND,EPH,ISHELL4,KGAS4,LGAS4,DIST4)
			# LOW ENERGY PHOTON
			if(ISHELL4 == -1):
				# GO TO 6
				continue
			#
			# CALCULATE CASCADE IN GAS 
			CALC4(NVAC,KGAS4,LGAS4,EPH,ISHELL4,L4)
			# STORE EVENT
			CSSTF4(I,NVAC,L4,DIST4)                    
		# 6  CONTINUE       
		# 
		# FIFTH GENERATION FLUORESCENCE
		if(IFLSUM4[NVAC]== 0):
			# GO TO 10
			update_globals()
			return
		# LOOP AROUND FIFTH GEN FLUORESCENCE
		IFL5=IFLSUM5[NVAC]
		for L5 in range (1,IFL5):
			EPH=EPHOTG4[NVAC][L5]
			#
			# CALC ABSORPTION POSITION X5,Y5,Z5 FOR FLUORESENCE PHOTON OF ENERGY EPH
			# WITH ABSORPTION IN SHELL ISHELL5 IN GAS KGAS5 COMPONENT LGAS5
			ABSO(ISECOND,EPH,ISHELL5,KGAS5,LGAS5,DIST5)
			# LOW ENERGY PHOTON
			if(ISHELL5 == -1):
				# GO TO 5
				continue
			#
			# CALCULATE CASCADE IN GAS 
			CALC5(NVAC,KGAS5,LGAS5,EPH,ISHELL5,L5)
			# STORE EVENT
			CSSTF5(I,NVAC,L5,DIST5)               
			if(IFLSUM5[NVAC]>= 1):
				# WRITE(6,89) 
				# 89  
				print(' WARNING SIXTH GENERATION EXISTS IFLSUM5=%d'%IFLSUM5)
		# 5  CONTINUE
	# endif
	# 10 CONTINUE
	update_globals()
	return 
      # end