c_ ---------------------------------------------------------------------
c_ RCS lines preceded by "c_ "
c_ ---------------------------------------------------------------------
c_
c_ $Source: /home/orr/WWW/Abiotic/boundcond/RCS/read_co2atm.f,v $ 
c_ $Revision: 1.2 $
c_ $Date: 1999/10/04 10:41:00 $   ;  $State: Exp $
c_ $Author: orr $ ;  $Locker:  $
c_
c_ ---------------------------------------------------------------------
c_ $Log: read_co2atm.f,v $
c_
c_ Revision 1.3 2/14/2003 S. Fletcher modified for use in the 
c_ ocean inversion project
c_
c_ Revision 1.2  1999/10/04 10:41:00  orr
c_ Added 8th Future scenario "CIS92A" for IPCC 2000 SAR
c_
c_ Revision 1.1  1999/04/26 13:52:31  orr
c_ Initial revision
c_
c_ ---------------------------------------------------------------------
c_ 
      SUBROUTINE read_co2atm(futr_scen, nco2rec, yrco2rec, atmco2rec)
C     ------------------------------------------------------------------
C     Reads temporal history of atmospheric CO2 (uatm)
C     ------------------------------------------------------------------
C     ==================================================================
C     Argument list -
C     ===============
C     Note: Variable TYPE is given in square brackets (below)
C     (r-REAL, i-INTEGER, l-LOGICAL, c-CHARACTER; s-scaler, a-array).
C     ===============
C     INPUT:
C     -------
C     [cs]  futr_scen  = IPCC future scenario:IS92a
c
C     OUTPUT: 
C     -------
C     [is]  nco2rec    =  Number of records (years) for atmospheric CO2
C                         from historical (splco2_mod.dat) plus
C                         future (is92a_mod.dat) records
C     [ra]  yrco2rec   =  sequential list of times (in decimal years)
C                         for WHEN atmospheric CO2 data is available
C     [ra]  atmco2rec  =  corresponding sequential list of atmospheric 
C                         co2 (ppm).
C                         This record is from Enting et al. (1994).
C     ==================================================================
C     
C     Reference
C     ---------
C     Enting, I.G., T. M. L. Wigley, M. Heimann, 1994. Future emissions 
C     and concentrations of carbon dioxide: key ocean / atmosphere / 
C     land analyses, CSIRO Aust. Div. Atmos. Res., Tech. Pap. No. 31, 
C     118 pp.
C     ------------------------------------------------------------------
C 

C     James Orr, LSCE/CEA-CNRS, Saclay, France, 17 April 1999

      IMPLICIT NONE 

      INTEGER maxrec, nmxr
      PARAMETER (maxrec=1200, nmxr=700)

      REAL yrco2rec(maxrec), atmco2rec(maxrec)
      INTEGER luco2, irec
      INTEGER nco2rec
      INTEGER is, ifuture, ireadf
      INTEGER nsipl, nstab

      REAL futco2(nmxr,8)
      REAL dummy

      CHARACTER*4 futr_scen
      CHARACTER*4 ipcc_scen(8)

c     Note that the 1st 7 future scenarios are in FILE "stab.dat";
c     the last scenario "CIS9" is short for "CIS92A" (see "cis92a.dat").
c     Note: for the ocean inversion only the last file will be used.  
      DATA ipcc_scen/'S350', 'S450', 'S550', 'S650', 'S750', 
     &               'DS45', 'DS55', 'CIS9'/


c
c     Determine index for future scenario       
      ifuture = 8
      DO is=1,8
        IF (futr_scen(1:4) .EQ. ipcc_scen(is))THEN
            ifuture = is
        ENDIF
      END DO
c
c     Check to be sure that chosen scenario is from allowed IPCC list
      IF (ifuture.eq.0)then
          WRITE(*,*)'Improper 1st argument for read_co2atm.f: '
     &              ,futr_scen(1:4)
          WRITE(*,'(a,7(1x,a))')'You must chose from '
     &                         , (ipcc_scen(is), is=1,8)
          WRITE(*, *)
     &    ' For OCMIP-2, S650 and CIS9 are the ONLY accetable choices'
          STOP
      ELSE
          WRITE(*,'(2a)')'You have chosen IPCC scenario '
     &                   , ipcc_scen(ifuture)
      END IF 

C
C     OPEN FILE
C     ---------
      luco2=50
      OPEN(luco2+1, FILE='splco2_mod.dat',FORM='formatted',STATUS='old')
      OPEN(luco2+3, FILE='cis92a_mod.dat',FORM='formatted',STATUS='old')
C
C     skip over 1st six descriptor lines
C
C-----------------------------------------------------------------------
C       get atmospheric CO2 data
C-----------------------------------------------------------------------
      WRITE(*,*)'  '
      WRITE(*,*)'--------------------------------------------------'
      WRITE(*,*)'Atm. CO2 from spline fit to Siple-Mauna Loa record'
      WRITE(*,*)' And IPCC scenario ', futr_scen(1:4)
      WRITE(*,*)'--------------------------------------------------'
C
C     --------------------------------------------------------------
C     READ either historical or future co2 concentrations, depending
C     upon the value of ireadf, which is enabled (set to 1) during
C     the READ operation, at the END of the historical FILE (IF
C     ifuture > 0)
C     --------------------------------------------------------------

      READ(luco2+1,200)
 200  FORMAT(////)
      ireadf = 0
      nsipl = 0
      nstab = 0
      DO irec=1,maxrec
 210    IF(ireadf .EQ. 0)THEN
C           Read from splco2.dat (historical emissions)
            READ(luco2+1,*,ERR=220,END=220)
     &           yrco2rec(irec),atmco2rec(irec)
            nsipl = nsipl + 1
        ELSE IF(ireadf .EQ. 1)THEN
C           Read from stab.dat (future atm CO2 scenario)
            READ(luco2+2,*,ERR=222,END=222)yrco2rec(irec)
     1          ,(futco2(irec-nsipl,is), is=1,7)
            atmco2rec(irec) = futco2(irec-nsipl,ifuture)
            nstab = nstab + 1
        ELSE IF(ireadf .EQ. 2)THEN
C           Read from stab.dat (future atm CO2 scenario)
            READ(luco2+3,*,ERR=222,END=222)yrco2rec(irec)
     1          ,futco2(irec-nsipl,8), dummy
            atmco2rec(irec) = futco2(irec-nsipl,8)
            nstab = nstab + 1
        ENDIF

        GO TO 221

C       When end of historical co2 reached, turn on read ability
C       (ireadf>0) for future file, go back to read one or the other, 
c       THEN continue reading chosen future file until it ends
 220    IF(ifuture.GT.0 .AND. ifuture.le.7)THEN
            ireadf = 1
C             Read over 1st 5 lines of description + 1st line of data
C             in future file that is repeated from siple record
            READ(luco2+2,201)
 201        FORMAT(/////)
            GO TO 210
        ELSEIF (ifuture .EQ. 8)THEN
            ireadf = 2
C             Read over 1 Header line in future FILE cis92a.dat
C             Note that 
c               - 1st line is 1990 (not identical to historical run)
c               - records are yearly, not every 6 months as for 
c                 1 <= ifuture <=7
            READ(luco2+3,'(1x)')
c           READ first line of DATA (same year as last line in splco2.dat)
c           THEN for CIS92A only, replace that with 
            WRITE (*,*) 'Replace: nsipl = ', nsipl
            READ(luco2+3,*)yrco2rec(nsipl), atmco2rec(nsipl), dummy
            write(*,*)yrco2rec(nsipl), atmco2rec(nsipl), dummy
            GO TO 210
        ELSE
            GO TO 222
        ENDIF

c 221    nco2rec = nsipl + nstab
 221    nco2rec = irec
      END DO

 222  CONTINUE

c      WRITE(0,*) 'Number records in splco2.dat:', nsipl
c      WRITE(0,*) 'Number records in future (stab.dat or cis92a.dat):', nstab
c      WRITE(0,*) 'Sum                          ', nstab + nsipl

       WRITE(*,*) 'Atm. CO2:  No. of entries for 1-box atmosphere =',
     &            nco2rec

      CLOSE(luco2+1)
      CLOSE(luco2+2)
      CLOSE(luco2+3)

      RETURN
      END









