	program cei 

c	This program creates composite CEI results from the 5 or 6
c	separate indicators.  
************************************************************ 

	parameter ( iBegYear = 1895 )
	parameter ( iEndYear = 2050 )

c	Note: Change dimensions of iYear and rValues arrays
c	in ReadIn subroutine

************************************************************
c	Declarations

        integer iMo(1:5),iMoYr(1:5)
	real rStep1(iBegYear:iEndYear)
	real rStep2(iBegYear:iEndYear)
	real rStep3(iBegYear:iEndYear)
	real rStep4(iBegYear:iEndYear)
	real rStep5(iBegYear:iEndYear)
	real rStep6(iBegYear:iEndYear)
	real rCEI(iBegYear:iEndYear)
	real rCEItc(iBegYear:iEndYear)
	character *4, argv
	character *4, cBegYr,cEndYr
	character*2, cBegMo,cEndMo
	character*90, cOutFile,cOutFile2
	character*16, cPath(5)
	character*14, cPath2(1)
	character*4, cType1(2),cType2(2)
	character*8, cPeriod(1:5)
	character*22, cNewPath1
	character*19, cNewPath2
	data cPath / '../data/dk-step1','../data/dk-step2', 
     +               '../data/dk-step3','../data/dk-step4',
     +		     '../data/dk-step5' /
        data cPath2 / '../data/step6.' /
	data cType1 /'-hi.','-wi.' /
	data cType2 /'-lo.','-wo.' /
        data cPeriod/'Current ','3-Month ','6-Month ',
     +               '12-Month','YTD     '/
************************************************************

         call GETARG (1,argv)
         read(argv,'(i4.4)') iBegYr
         call GETARG (2,argv)
         read(argv,'(i4.4)') iCurYr
         call GETARG (3,argv)
         read(argv,'(i2.2)') iCurMo

************************************************************
c       Determine all month and year variables for production run:
c       current month, 3-mo, 6-mo, 12-mo and YTD

        iMo(1) = iCurMo
        iMoYr(1) = iBegYr

        if(iCurMo.ge.3) then
          iMo(2) = iCurMo-2
          iMoYr(2) = iBegYr
        else
          iMo(2) = iCurMo + 10
          iMoYr(2) = iBegYr + 1
        endif
        if(iCurMo.ge.6) then
          iMo(3) = iCurMo-5
          iMoYr(3) = iBegYr
        else
          iMo(3) = iCurMo + 7
          iMoYr(3) = iBegYr + 1
        endif
        if(icurMo.eq.12) then
          iMo(4) = 1
          iMoYr(4) = iBegYr
        else
          iMo(4) = iCurMo + 1
          iMoYr(4) = iBegYr + 1
        endif

************************************************************
************************************************************
      Do iLoop = 1,5

        write(*,*) 'Working on: ', cPeriod(iLoop)

        if(iLoop.ne.5) then
          iBegMo = iMo(iLoop)
          iBegYr = iMoYr(iLoop)
        else
          iBegMo = 1
          iBegYr = iMoYr(1)
        endif
          iEndMo = iCurMo
          iEndYr = iCurYr

	 write(cBegMo,'(i2.2)') iBegMo
	 write(cEndMo,'(i2.2)') iEndMo

c  Determine if Hurr season calculation is applicable:
c(Must have span of months include at least one month from June to November)

	if((iLoop.eq.1).and.(iBegMo.ge.6.or.iBegMo.le.11)) then
	  rNum = 6.0
	elseif((iLoop.eq.2).and.(iEndMo.ge.6.or.iBegMo.eq.11)) then
	  rNum = 6.0
	elseif((iLoop.eq.3).and.(iEndMo.ge.6.or.iEndMo.le.4)) then
	  rNum = 6.0
	elseif(iLoop.eq.4) then
	  rNum = 6.0
	elseif((iLoop.eq.5).and.(iEndMo.ge.6)) then
	  rNum = 6.0
	else
	  rNum = 5.0
	endif

************************************************************
c	Read in results files:

c	Step 1

	cNewPath1 = cPath(1)//'.'//cBegMo//'-'//cEndMo
	open(20,file=cNewPath1//'.results')
 5	read(20,*,end=10) iYear, rValue
        rStep1(iYear) = rValue
        goto 5

 10     close(20)

c	Step 2 

	cNewPath1 = cPath(2)//'.'//cBegMo//'-'//cEndMo
	open(20,file=cNewPath1//'.results')
 15	read(20,*,end=20) iYear, rValue
        rStep2(iYear) = rValue
        goto 15

 20     close(20)

c	Step 3 

	cNewPath1 = cPath(3)//'.'//cBegMo//'-'//cEndMo
	open(20,file=cNewPath1//'.results')
 25	read(20,*,end=30) iYear, rValue
        rStep3(iYear) = rValue
        goto 25

 30     close(20)

c	Step 4 

	cNewPath1 = cPath(4)//'.'//cBegMo//'-'//cEndMo
	open(20,file=cNewPath1//'.results')
 35	read(20,*,end=40) iYear, rValue
        rStep4(iYear) = rValue
        goto 35

 40     close(20)

c	Step 5 

	cNewPath1 = cPath(5)//'.'//cBegMo//'-'//cEndMo
	open(20,file=cNewPath1//'.results')
 45	read(20,*,end=50) iYear, rValue
        rStep5(iYear) = rValue
        goto 45

 50     close(20)

	if(rNum.eq.6.0) then
c	  Step 6 

	  cNewPath2 = cPath2(1)//cBegMo//'-'//cEndMo
	  open(20,file=cNewPath2//'.results')
 55	  read(20,*,end=60) iYear, rValue
          rStep6(iYear) = rValue
          goto 55

 60       close(20)
	endif

************************************************************
************************************************************
c	Calculate Climate Extremes Index

	if(rNum.eq.6.0) then
	  do i=iBegYr,iEndYr
	    rCEItc(i) = (rStep1(i)+rStep2(i)+rStep3(i)+rStep4(i)*2.+
     + 	  	    rStep5(i)+rStep6(i))/rNum
	    rCEI(i) = (rStep1(i)+rStep2(i)+rStep3(i)+rStep4(i)*2.+
     + 	  	    rStep5(i))/(rNum-1.0)
	  enddo
	else
	  do i=iBegYr,iEndYr
	    rCEI(i) = (rStep1(i)+rStep2(i)+rStep3(i)+rStep4(i)*2.+
     + 	  	    rStep5(i))/rNum
	  enddo
	endif

***************************************************************

c	Write out results
c	Mutliply by 100 to get ratio to percent

	cOutFile = '../data/dk-cei.'//cBegMo//'-'//cEndMo//'.results'
	open (unit=13,file=cOutFile)

 	do i=iBegYr,iEndYr
	  write (13,13) i,rCEI(i)*100.0
13	  format (i4,2x,f8.3)
	enddo

	close(13)
	if(iLoop.eq.5) then
	  cOutFile = '../data/dk-cei.ytd.results'
	  open (unit=13,file=cOutFile)

 	  do i=iBegYr,iEndYr
	    write (13,13) i,rCEI(i)*100.0
	  enddo

	  close(13)

	endif

	if(rNum.eq.6.0) then
c	  Create output file with the tropical cyclone indicator results:
	  cOutFile2 = '../data/dk-cei.tc.'//cBegMo//'-'//cEndMo//
     +                '.results'
	  open (unit=13,file=cOutFile2)

 	  do i=iBegYr,iEndYr
	    write (13,13) i,rCEItc(i)*100.0
	  enddo

	  close(13)
	  if(iLoop.eq.5) then
	    cOutFile2 = '../data/dk-cei.tc.ytd.results'
	    open (unit=13,file=cOutFile2)

 	    do i=iBegYr,iEndYr
	      write (13,13) i,rCEItc(i)*100.0
	    enddo

	    close(13)
	  endif
	endif

      Enddo

	end
