!
! ***********************************************************************
!
!  PARTYUNITY2.FOR---READS HDEMREP35_108.DAT/SDEMREP35_108.DAT 
!                   AND H01108_PRES.VOT/S01108.VOT 
!                   AND COMPUTES PARTY UNITY SCORES FOR 50-50
!                   AND 90-10/10-90 CRITERIA FOR EVERY MEMBER
!                   OF THE DEMOCRATIC AND REPUBLICAN PARTIES
!                  
!                             8 MAY 2006
!
!                   FORTRAN 95 VERSION, 11 JANUARY 2011
!                 
! ***********************************************************************
!
      DIMENSION KTOTAL(200),KPARTY50(200,3700),JVOTE(3600),&
                KPTY1(200),KPTY2(200),LONESIX(99999),LNONZERO(99999)
      CHARACTER*1 JJSTATE(25),JNAME(25)
!
!      OPEN(1,FILE='C:\ATIME2013\RCDATA\H01113_FINAL_2014.VOT',&
!                        STATUS='OLD')
!      OPEN(20,FILE='DWNOM29_HOUSE_FINAL_2014.DAT',STATUS='OLD')
!      OPEN(22,FILE='HPARTYUNITY22.DAT')
!      OPEN(23,FILE='HPARTYVOTES23.DAT')
!  *******************************************************************
      OPEN(1,FILE='C:\ATIME2013\RCDATA\S01113_FINAL_2014.VOT',&
                       STATUS='OLD')
      OPEN(20,FILE='DWNOM29_SENATE_2014.DAT',STATUS='OLD')
      OPEN(22,FILE='SPARTYUNITY22.DAT')
      OPEN(23,FILE='SPARTYVOTES23.DAT')
!
!
  100 FORMAT(I4,I5,15I4)
  150 FORMAT(I4,I6,I3,I2,1X,7A1,1X,I4,1X,11A1,2600I1)
  160 FORMAT(I4,I6,I3,I2,1X,7A1,1X,I4,1X,11A1,F8.3,2I7,F8.3,2I7)
  200 FORMAT(4I5,3F10.5)
  300 FORMAT(' NUMBER OF RECORDS READ    ',I7)
  301 FORMAT(' NUMBER OF LEGISLATORS READ',3I7)
 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
!
      DO 8809 I=1,99999
      LONESIX(I)=0
      LNONZERO(I)=0
 8809 CONTINUE
!
!  DEFINE TWO-PARTY SYSTEM -- CONGRESSES 18 AND 31-34 HAD NO 
!                             DEFINED TWO-PARTY SYSTEM
!
      DO 8810 I=1,200
      KTOTAL(I)=0
      KPTY1(I)=0
      KPTY2(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,IMISS,&
                     IYEA,INAY,IREPYEA,IREPNAY,IDEMYEA,IDEMNAY,&
                     INREPYEA,INREPNAY,ISREPYEA,ISREPNAY,&
                     INDYEA,INDNAY,ISDYEA,ISDNAY
      I=I+1
      KTOTAL(ICONG)=KTOTAL(ICONG)+1
      KPARTY50(ICONG,ICOUNT)=0
!
!  CALCULATE PARTY UNITY SCORES -- REPUBLICANS YEA VS. DEMOCRATS NAY
!
      IF(IREPYEA.GE.IREPNAY)THEN
         IF(IDEMYEA.LT.IDEMNAY)THEN
            KPARTY50(ICONG,ICOUNT)=1
         ENDIF
      ENDIF
!
!  CALCULATE PARTY UNITY SCORES -- REPUBLICANS NAY VS. DEMOCRATS YEA
!
      IF(IREPYEA.LT.IREPNAY)THEN
         IF(IDEMYEA.GE.IDEMNAY)THEN
            KPARTY50(ICONG,ICOUNT)=2
         ENDIF
      ENDIF
      GO TO 500
  400 WRITE(*,300)I
      NTOTAL=I
!
!  READ H01108_PRES.VOT/S01108.VOT
!
      I=0
      II=0
      III=0
  550 READ(1,150,END=450)JCONG,JID,JSTATE,JDIST,(JJSTATE(J),J=1,7),&
                          JPARTY,(JNAME(J),J=1,11),&
                          (JVOTE(J),J=1,2600)
      I=I+1
!      IF(JCONG.LT.35)GO TO 550
      II=II+1
!      WRITE(*,1800)I,II,JCONG,JID,JSTATE,KTOTAL(JCONG)
! 1800 FORMAT(2I7,I4,I6,I3,I5)
!
!  COMPUTE THE PERCENTAGE OF ROLL CALLS THE MEMBER VOTES ON -- (1+6)/(~0)
!
       NONZERO=0
       KONESIX=0
       DO 61 JJ=1,KTOTAL(JCONG)
       IF(JVOTE(JJ).NE.0)NONZERO=NONZERO+1
       IF(JVOTE(JJ).EQ.1.OR.JVOTE(JJ).EQ.6)KONESIX=KONESIX+1
  61   CONTINUE
       LONESIX(JID)=LONESIX(JID)+KONESIX
       LNONZERO(JID)=LNONZERO(JID)+NONZERO
       XVOTES=0.0
       IF(NONZERO.GT.0)XVOTES=(FLOAT(KONESIX)/FLOAT(NONZERO))*100.0
       XVOTES2=0.0
       IF(LNONZERO(JID).GT.0)XVOTES2=&
                   (FLOAT(LONESIX(JID))/FLOAT(LNONZERO(JID)))*100.0
       WRITE(23,160)JCONG,JID,JSTATE,JDIST,(JJSTATE(J),J=1,7),&
                          JPARTY,(JNAME(J),J=1,11),&
                          XVOTES,KONESIX,NONZERO,&
                          XVOTES2,LONESIX(JID),LNONZERO(JID)
!
!  SELECT ON DEMOCRATS AND REPUBLICANS
!
      IF(JPARTY.EQ.KPTY1(JCONG).OR.JPARTY.EQ.KPTY2(JCONG))THEN
         ISUPPORT=0
         IUNITY=0
         DO 1 JJ=1,KTOTAL(JCONG)
         IVOTE=0
         IF(JVOTE(JJ).EQ.1.OR.JVOTE(JJ).EQ.2.OR.JVOTE(JJ).EQ.3)IVOTE=1
         IF(JVOTE(JJ).EQ.4.OR.JVOTE(JJ).EQ.5.OR.JVOTE(JJ).EQ.6)IVOTE=6
         IF(KPARTY50(JCONG,JJ).GT.0.AND.IVOTE.GT.0)IUNITY=IUNITY+1
!
!  REPUBLICAN YEA AND PARTY SUPPORT YEA
!
         IF(IVOTE.EQ.1.AND.KPARTY50(JCONG,JJ).EQ.1.AND.&
                                          JPARTY.EQ.KPTY2(JCONG))THEN
            ISUPPORT=ISUPPORT+1
         ENDIF
!
!  REPUBLICAN NAY AND PARTY SUPPORT NAY
!
         IF(IVOTE.EQ.6.AND.KPARTY50(JCONG,JJ).EQ.2.AND.&
                                          JPARTY.EQ.KPTY2(JCONG))THEN
            ISUPPORT=ISUPPORT+1
         ENDIF
!
!  DEMOCRAT YEA AND PARTY SUPPORT YEA
!
         IF(IVOTE.EQ.1.AND.KPARTY50(JCONG,JJ).EQ.2.AND.&
                                          JPARTY.EQ.KPTY1(JCONG))THEN
            ISUPPORT=ISUPPORT+1
         ENDIF
!
!  DEMOCRAT NAY AND PARTY SUPPORT NAY
!
         IF(IVOTE.EQ.6.AND.KPARTY50(JCONG,JJ).EQ.1.AND.&
                                          JPARTY.EQ.KPTY1(JCONG))THEN
            ISUPPORT=ISUPPORT+1
         ENDIF
  1      CONTINUE
      ENDIF
!
      XUNITY=0.0
      IF(IUNITY.GE.10)THEN
         III=III+1
         XUNITY=(FLOAT(ISUPPORT)/FLOAT(IUNITY))*100.0
         WRITE(22,160)JCONG,JID,JSTATE,JDIST,(JJSTATE(J),J=1,7),&
                          JPARTY,(JNAME(J),J=1,11),&
                          XUNITY,ISUPPORT,IUNITY
      ENDIF
      GO TO 550
  450 WRITE(*,301)I,II,III
!
      call cpu_time(result1)
      write(*,1001)result1
      TOTALTIME=RESULT1-RESULT
      WRITE(*,1002)TOTALTIME
!
      STOP
      END
