c $Log$
c =====================================================================
c
c     SUBROUTINE read_co2atm
c
c     PURPOSE: Reads temporal history of atmospheric CO2 (uatm)
c               based on OCMIP2 code
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: either S350, S450, S550,
c                        S650, S750, DS450, or DS550 from Enting et al.
c                        (1994), or CIS9 signifying c-IS92A for
c                        IPCC (2000) run.  From 1765-1990.5, it 
c                        doesn't matter which scenario you use, i.e., 
c                        atmospheric CO2 will be the same (from a
c                        spline fit to Siple Ice core and Mauna Loa
c                        data.  Subsequently, atmospheric CO2 is 
c                        different, according to the choice given above.
c
c     OUTPUT: 
c     -------
c     [is]  nco2rec    =  Number of records (years) for atmospheric CO2
c                         from historical (splco2.dat) plus
c                         future (stab.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     AUTHOR:  Nicolas Gruber (gruber@splash.princeton.edu)  
c                 based on OCMIP2 code supplied by Jim Orr
c
c     VERSION: MOM3
c
c     REVISIONS:
c
c     date     author   remarks
c     
c     08.01.00  ng      started coding
c      
c =====================================================================
c
 
      subroutine read_co2atm(futr_scen, nco2rec, yrco2rec, atmco2rec)
c
      implicit none
c
c-----------------------------------------------------------------------
c     local variables
c-----------------------------------------------------------------------
c
      integer maxrec, nmxr
      parameter (maxrec=1200, nmxr=700)
      
      character(len=4), intent(in)	:: futr_scen
      integer , intent(out)		:: nco2rec
      real, intent(out)	:: yrco2rec(maxrec), atmco2rec(maxrec)
      
      integer	:: lun(3), irec
      integer is, ifuture, ireadf
      integer nsipl, nstab
      
      real futco2(nmxr,8)
      real dummy
      
      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").
      
      data ipcc_scen/'S350', 'S450', 'S550', 'S650', 'S750',		
     $               'DS45', 'DS55', 'CIS9'/
      
c
c =====================================================================
c     begin of executable code
c =====================================================================
c
c
c-----------------------------------------------------------------------
c     determine index for future scenario
c-----------------------------------------------------------------------
c
      ifuture = 0
      do is=1,8
         if (futr_scen(1:4) .eq. ipcc_scen(is))then
            ifuture = is
         endif
      end do
c
c-----------------------------------------------------------------------
c     check to be sure that chosen scenario is from allowed ipcc list
c-----------------------------------------------------------------------
c
      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-----------------------------------------------------------------------
c     open file
c-----------------------------------------------------------------------
c
      call getunit (lun(1), 'splco2_mod.dat', 'formatted sequential rewind')
      call getunit (lun(2), 'stab.dat',   'formatted sequential rewind')
      call getunit (lun(3), 'cis92a_mod.dat', 'formatted sequential rewind')
c
c-----------------------------------------------------------------------
c     give info
c-----------------------------------------------------------------------
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     skip first lines in historical data
c-----------------------------------------------------------------------
c      
      read(lun(1),'(////)')
      ireadf = 0
      nsipl = 0
      nstab = 0
      do irec=1,maxrec
 210     continue
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 --------------------------------------------------------------
c
c
c-----------------------------------------------------------------------
c     read from splco2.dat (historical emissions)
c-----------------------------------------------------------------------
c
         if (ireadf .eq. 0) then
            read(lun(1),*,err=220,end=220) yrco2rec(irec), atmco2rec(irec)
*            print *, '**************************************************' 
*            print*, 'Reading Atmospheric CO2', yrco2rec(irec), 
*     .               atmco2rec(irec)
*            print *, '**************************************************' 
            nsipl = nsipl + 1
c
c-----------------------------------------------------------------------
c     read from stab.dat (future atm co2 scenario)
c-----------------------------------------------------------------------
c
         else if (ireadf .eq. 1) then
            
            read(lun(2),*,err=222,end=222) yrco2rec(irec)		
     $           , (futco2(irec-nsipl,is), is=1,7)
            atmco2rec(irec) = futco2(irec-nsipl,ifuture)
            nstab = nstab + 1
c
c-----------------------------------------------------------------------
c     read from cis92a
c-----------------------------------------------------------------------
c
         else if (ireadf .eq. 2) then            
            read(lun(3),*,err=222,end=222) yrco2rec(irec)		
     $           , futco2(irec-nsipl,8), dummy
            atmco2rec(irec) = futco2(irec-nsipl,8)
*            print *, '**************************************************' 
*            print*, 'Reading FUTURE Atmospheric CO2', yrco2rec(irec), 
*     .               futco2(irec-nsipl,8)
*            print *, '**************************************************' 
            nstab = nstab + 1
         endif
         
         go to 221
c
c-----------------------------------------------------------------------
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 endsc     
c-----------------------------------------------------------------------
c         
 220     continue
         if (ifuture .gt. 0 .and. ifuture .le. 7) then
            ireadf = 1
c
c-----------------------------------------------------------------------
c     read over 1st 5 lines of description + 1st line of data
c     in future file that is repeated from siple recorddetermine index 
c     for future scenario
c-----------------------------------------------------------------------
c           
            read (lun(2),'(/////)')
            go to 210
         elseif (ifuture .eq. 8) then
            ireadf = 2
c
c-----------------------------------------------------------------------
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      
c       read first line of data (same year as last line in splco2.dat)
c     then for cis92a only, replace that with 
c-----------------------------------------------------------------------
c                                
            read(lun(3),'(1x)')
c
c-----------------------------------------------------------------------
c     then for cis92a only, replace that with 
c-----------------------------------------------------------------------
c                                
            write (*,*) 'replace: nsipl = ', nsipl
            read(lun(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     continue
         nco2rec = irec
      end do
      
 222  continue
c
c-----------------------------------------------------------------------
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 + nsiplc     
c-----------------------------------------------------------------------
c                 
      write(*,*) 'atm. co2:  no. of entries for 1-box atmosphere =',	
     $     nco2rec
c
c-----------------------------------------------------------------------
c     release units
c-----------------------------------------------------------------------
c
      call relunit(lun(1))
      call relunit(lun(2))
      call relunit(lun(3))
c
c-----------------------------------------------------------------------
c     end of subroutine
c-----------------------------------------------------------------------
c
      return
      end subroutine  read_co2atm 
