c_ ---------------------------------------------------------------------
c_ RCS lines preceded by "c_ "
c_ ---------------------------------------------------------------------
c_
c_ $Source: /home/orr/WWW/Abiotic/boundcond/RCS/c_interp.f,v $ 
c_ $Revision: 1.1 $
c_ $Date: 1999/04/26 13:51:41 $   ;  $State: Exp $
c_ $Author: orr $ ;  $Locker:  $
c_
c_ ---------------------------------------------------------------------
c_ $Log: c_interp.f,v $
c_ Revision 1.1  1999/04/26 13:51:41  orr
c_ Initial revision
c_
c_ ---------------------------------------------------------------------
c_ 
      SUBROUTINE c_interp(year_model, futr_scen, atmco2_t, atmc14_t)

c     ------------------------------------------------------------------
c     Interpolates atmospheric CO2 and C-14 to the timestep of the model
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     [rs]  year_model =  decimal year of model (e.g., 1990.67), as
c                         computed from the timestep and the year of
c                         the initialization of the simulation. This
c                         information is necessary to interpolate
c                         atmospheric levels of CO2 (atmco2_t) and 
c                         C-14 (atmc14_t) from the historical records 
c                         chosen for OCMIP-2 (from Enting et al. (1994).
C     [cs]  futr_scen   = Either 'S450' or 'S650' (in upper CASE) 
c                         Choses from the IPCC future scenarios by the 
c                         same names.  These scenarios are standard for
c                         OCMIP-2 future runs.  The choice of "futr_scen"
c                         does NOT affect "atmc14_t". Likewise, it does
c                         NOT affect "atmco2_t" prior to 1990.5.
c
c     OUTPUT: 
c     -------
c     [rs]  atmco2_t   =  Atmospheric CO2 (ppm) at "year_model"
c     [ra]  atmc14_t   =  3-member array of atmospheric C-14 at
c                         year_model".  Sequentially, the 3 values
c                         correspond to forcing in 3 latitudinal bands:
c                         (1) 90S - 20S,
c                         (2) 20S - 20N, and
c                         (3) 20N - 90N.
c     ==================================================================

c     ------------------------------------------------------------------
c     James Orr, LSCE/CEA-CNRS, Saclay, France, 20 April 1999
c     ------------------------------------------------------------------

      IMPLICIT NONE 

      INTEGER maxrec
      PARAMETER (maxrec=1200)
 
      INTEGER maxrec14, nzon14
      PARAMETER (maxrec14=300, nzon14=3)

      INTEGER nco2rec, nc14rec(nzon14)

      REAL yrco2rec(maxrec), atmco2rec(maxrec)
      REAL yrc14rec(maxrec14,nzon14), atmc14rec(maxrec14,nzon14)
      REAL year_model
      REAL atmco2_t, atmc14_t(nzon14)

      REAL yrco2min, yrco2max
      REAL yrc14min, yrc14max
      REAL year

      INTEGER iz
      INTEGER n
      REAL x

      COMMON /saveatm/ nco2rec, yrco2rec, atmco2rec
     &               , nc14rec, yrc14rec, atmc14rec

      CHARACTER*4 futr_scen

      INTEGER ientry
      SAVE ientry
      DATA ientry/0/

      DATA yrco2min, yrco2max/ 1765.0, 2300.5/
      DATA yrc14min, yrc14max/ 1765.0, 1995.5/

c     Counter "ientry" for number of entries in this SUBROUTINE
c     (Read atmospheric records on 1st entry ONLY)
c     ------------------------------------------------------------------
      ientry = ientry + 1
 
      IF (ientry .EQ. 1) THEN
          CALL read_co2atm(futr_scen(1:4), nco2rec, yrco2rec, atmco2rec)
          CALL read_c14atm (nc14rec, yrc14rec, atmc14rec)
      ENDIF

c     ------------------------------------------------------------------
c     Interpolate atmospheric CO2 and C-14 to timestep (decimal years)
c     of the model
c     ------------------------------------------------------------------

c     CO2:
c     ----
      IF (year_model .lt. yrco2min) THEN 
          year = yrco2min
      ELSE IF(year_model .ge. yrco2min  .AND.  year_model .le. yrco2max)
     &    THEN 
          year = year_model
      ELSE IF (year_model .gt. yrco2max) THEN 
          year = yrco2max
      END IF 

c     Find relative POSITION n for year_model in CO2 record
      call locate(yrco2rec, nco2rec, year, n)

c     Determine linear interpolation factor "x"
      x = (year - yrco2rec(n)) / (yrco2rec(n+1) - yrco2rec(n))

c     Perform temporal interpolation for atmospheric CO2
      atmco2_t = atmco2rec(n)   * (1. - x)  
     &         + atmco2rec(n+1) * x

c     C-14:
c     -----
      IF (year_model .lt. yrc14min) THEN 
          year = yrc14min
      ELSE IF(year_model .ge. yrc14min  .AND.  year_model .le. yrc14max) 
     &    THEN 
          year = year_model
      ELSE IF (year_model .gt. yrc14max) THEN 
          year = yrc14max
      END IF         

c     Find relative POSITION n for year_model in C-14 record
      call locate(yrc14rec(1,1), nc14rec(1), year, n)
          
c     Determine linear interpolation factor "x"
      x = (year - yrc14rec(n,1)) / (yrc14rec(n+1,1) - yrc14rec(n,1))
          
c     Perform temporal linear interpolation for atmospheric C-14 
c     (on all 3 latitudinal zones for C-14)
      DO iz=1,3
        atmc14_t(iz) = atmc14rec(n,iz)   * (1. - x)  
     &               + atmc14rec(n+1,iz) * x
      END DO

      RETURN
      END


