!
! ***********************************************************************
!
!  PARTYUNITY.FOR---READS HDEMREP35_108.DAT/SDEMREP35_108.DAT 
!                   AND COMPUTES PARTY UNITY SCORES FOR 50-50
!                   AND 90-10/10-90 CRITERIA
!                  
!                         17 JANUARY 2006
!                 
!                   FORTRAN 95 VERSION 7 OCTOBER 2010
!                 
! ***********************************************************************
!
      DIMENSION KTOTAL(200),KPARTY50(200),KDEMUNITY(200),&
                KDEMTOTAL(200),KREPUNITY(200),KREPTOTAL(200),&
                KPTY1(200),KPTY2(200)
!
!      OPEN(20,FILE='DWNOM29_HOUSE_FINAL_2014.DAT',STATUS='OLD')
!      OPEN(22,FILE='HPARTYUNITY22_ALL.DAT')
!  *******************************************************************
      OPEN(20,FILE='DWNOM29_SENATE_2014.DAT',STATUS='OLD')
      OPEN(22,FILE='SPARTYUNITY22_ALL.DAT')
!
!
  100 FORMAT(I4,I5,2I3,I5,15I4)
  200 FORMAT(4I5,3F10.5)
  250 FORMAT(I4,I5,15I4)
  300 FORMAT(' NUMBER OF RECORDS READ',I7)
 1000 FORMAT(' START TIME OF JOB    ',3F12.7)
 1001 FORMAT(' END TIME OF JOB      ',3F12.7)
 1002 FORMAT(' ELAPSED TIME OF JOB  ',3F12.7)
!
      call cpu_time(result)
      write(*,1000)result
!
      NTCONG=113
!
!
!  DEFINE TWO-PARTY SYSTEM -- CONGRESSES 18 AND 31-34 HAD NO 
!                             DEFINED TWO-PARTY SYSTEM
!
      DO 8810 I=1,200
      KPTY1(I)=0
      KPTY2(I)=0
      KTOTAL(I)=0
      KPARTY50(I)=0
      KDEMUNITY(I)=0
      KDEMTOTAL(I)=0
      KREPUNITY(I)=0
      KREPTOTAL(I)=0
 8810 CONTINUE
!
!  FEDERALIST-JEFFERSONIAN REPUBLICAN 1789-1812
!
!     (13 - 17 ADDED FOR NICOLE MELLOW)
!
!  4000 = ANTI-ADMINISTRATION (LATER JEFFERSONIAN REPUBLICAN)
!  5000 = PRO-ADMINISTRATION (LATER FEDERALIST)
!
      DO 8811 I=1,3
      KPTY1(I)=4000
      KPTY2(I)=5000
 8811 CONTINUE
!  
!  200 = JEFFERSONIAN REPUBLICAN
!  1   = FEDERALIST
!
      DO 8812 I=4,17
      KPTY1(I)=200
      KPTY2(I)=1
 8812 CONTINUE
!
!  WHIG-DEMOCRAT PARTY SYSTEM 1827-1848
!
!    (19 ADDED FOR NICOLE MELLOW)
!
!  555 = JACKSON
!  22  = ADAMS
!
      KPTY1(19)=555
      KPTY2(19)=22
      KPTY1(20)=555
      KPTY2(20)=22
!
!   555 = JACKSON
!  1275 = ANTI-JACKSON
!
      DO 13 I=21,24
      KPTY1(I)=555
      KPTY2(I)=1275
  13  CONTINUE
!
!  100 = DEMOCRAT
!   29 = WHIG             
!
      DO 14 I=25,30
      KPTY1(I)=100
      KPTY2(I)=29
  14  CONTINUE
!
!  REPUBLICAN-DEMOCRAT PARTY SYSTEM 1857-1998
!
!  100 = DEMOCRAT
!  200 = REPUBLICAN       
!
      DO 15 I=35,NTCONG
      KPTY1(I)=100
      KPTY2(I)=200
  15  CONTINUE
!
!
!  READ HDEMREP35_108.DAT/SDEMREP35_108.DAT
!
!
      I=0
!  500 READ(20,100,END=400)ICONG,ICOUNT,IMONTH,IDAY,IYEAR,IMISS,&
!                     IYEA,INAY,IREPYEA,IREPNAY,IDEMYEA,IDEMNAY,&
!                     INRYEA,INRNAY,ISRYEA,ISRNAY,&
!                     INDYEA,INDNAY,ISDYEA,ISDNAY
  500 READ(20,250,END=400)ICONG,ICOUNT,IMISS,&
                     IYEA,INAY,IREPYEA,IREPNAY,IDEMYEA,IDEMNAY,&
                     INRYEA,INRNAY,ISRYEA,ISRNAY,&
                     INDYEA,INDNAY,ISDYEA,ISDNAY
      I=I+1
      KTOTAL(ICONG)=KTOTAL(ICONG)+1
!
!  CALCULATE PARTY UNITY SCORES -- REPUBLICANS YEA VS. DEMOCRATS NAY
!
      IF(IREPYEA.GE.IREPNAY)THEN
         IF(IDEMYEA.LT.IDEMNAY)THEN
            KPARTY50(ICONG)=KPARTY50(ICONG)+1
            KDEMUNITY(ICONG)=KDEMUNITY(ICONG)+IDEMNAY
            KDEMTOTAL(ICONG)=KDEMTOTAL(ICONG)+IDEMNAY+IDEMYEA
            KREPUNITY(ICONG)=KREPUNITY(ICONG)+IREPYEA
            KREPTOTAL(ICONG)=KREPTOTAL(ICONG)+IREPYEA+IREPNAY
         ENDIF
      ENDIF
!
!  CALCULATE PARTY UNITY SCORES -- REPUBLICANS NAY VS. DEMOCRATS YEA
!
      IF(IREPYEA.LT.IREPNAY)THEN
         IF(IDEMYEA.GE.IDEMNAY)THEN
            KPARTY50(ICONG)=KPARTY50(ICONG)+1
            KDEMUNITY(ICONG)=KDEMUNITY(ICONG)+IDEMYEA
            KDEMTOTAL(ICONG)=KDEMTOTAL(ICONG)+IDEMNAY+IDEMYEA
            KREPUNITY(ICONG)=KREPUNITY(ICONG)+IREPNAY
            KREPTOTAL(ICONG)=KREPTOTAL(ICONG)+IREPYEA+IREPNAY
         ENDIF
      ENDIF
      GO TO 500
  400 WRITE(*,300)I
      NTOTAL=I
!
      DO 1 I=1,NTCONG
      KYEAR=2*I + 1787
      XPERCENT=FLOAT(KPARTY50(I))/FLOAT(KTOTAL(I))
      XREPUNITY=FLOAT(KREPUNITY(I))/FLOAT(KREPTOTAL(I))
      XDEMUNITY=FLOAT(KDEMUNITY(I))/FLOAT(KDEMTOTAL(I))
      WRITE(22,200)I,KYEAR,KTOTAL(I),KPARTY50(I),&
                               XPERCENT,XREPUNITY,XDEMUNITY
  1   CONTINUE
!
!
      call cpu_time(result1)
      write(*,1001)result1
      TOTALTIME=RESULT1-RESULT
      WRITE(*,1002)TOTALTIME
!
      STOP
      END
