      PROGRAM BINTOASC
C  Converts TIGCM ASCII formatted output into binary unformatted form
      INTEGER ZMAX, Z, T, TMAX, VAR, RUN, PARITY,VARMAX,TTRUNC,VARTOP
      PARAMETER (VARMAX=13, ZMAX=7, MMAX=5, NMAX=25, TMAX=7)
      DIMENSION VSH(VARMAX, ZMAX, MMAX, NMAX, TMAX, 2)
      INTEGER ZTRUNC(VARMAX), NTRUNC(VARMAX), ZLOW, RUNLOW, RUNHIGH
      CHARACTER*2 RUNCHAR

      WRITE (*,*) 'THIS PROGRAM WILL CONVERT RUNS FROM BINARY TO ASCII'
      WRITE (*,*) 'ENTER LOWEST RUN # OF FILE TO CONVERT  (INTEGER):'
      READ (*,*) RUNLOW
      WRITE (*,*) 'ENTER HIGHEST RUN #: '
      READ (*,*) RUNHIGH

      DO 9000 RUN= RUNLOW,RUNHIGH
C  Convert run # to a 2-character string
         CALL RUNTOCH( RUN, RUNCHAR )
         OPEN (3,  FILE='run0'//RUNCHAR//'.asc', STATUS='new')
         OPEN (10, FILE='run0'//RUNCHAR//'.new', STATUS='old',
     1                                 FORM='unformatted')
      
         READ (10) ZLOW, VARTOP
         WRITE (3,200) ZLOW, VARTOP
         WRITE (*,*) 'RUN: ',RUN,' OF ',RUNHIGH
         DO 8000 VAR= 1,VARTOP
            WRITE (*,*) 'VARIABLE ',VAR,' OF ',VARMAX
            READ (10) IVAR,MTRUNC,NTRUNC(VAR),
     1                               ZTRUNC(VAR), TTRUNC            
            WRITE (3,300) VAR,MTRUNC,NTRUNC(VAR),
     1                               ZTRUNC(VAR), TTRUNC
            READ (10) (((((VSH(VAR,Z,M,N,T,PARITY),
     1          N=M,NTRUNC(VAR)),  M= PARITY,MTRUNC), PARITY= 1,2), 
     2           T= 1,TTRUNC), Z=ZLOW,ZTRUNC(VAR))
            DO 7000 Z= ZLOW,ZTRUNC(VAR)            
             WRITE (3,400)  ((((VSH(VAR,Z,M,N,T,PARITY),
     1       N=M,NTRUNC(VAR)),  M= PARITY,MTRUNC), PARITY= 1,2), 
     2           T= 1,TTRUNC) 
 7000       CONTINUE 
 8000    CONTINUE
 9000 CONTINUE

 200  FORMAT (I2,1X,I2)
 300  FORMAT (5I3)
 400  FORMAT (6E13.6)
      END
 
      SUBROUTINE RUNTOCH(RUN,CHARRUN)
C   Convert run# to a character string
C   Input:  Run #  (integer)
C   Output: Character string  (CHARACTER*2)
      CHARACTER*2  CHARRUN
      INTEGER CNOUGHT, RUN, TENS, ONES
C   CNOUGHT is the ASCII number that precedes that for the character 1
      PARAMETER (CNOUGHT= 48)
      TENS= RUN/10 
      ONES= RUN - 10*TENS
      CHARRUN= CHAR(TENS+CNOUGHT)//CHAR(ONES+CNOUGHT)
      RETURN
      END


