Example #1
0
SEXP eCanA(SEXP lai, SEXP Doy, SEXP Hr, SEXP SolarR, SEXP ATemp,
	  SEXP RelH, SEXP WindS, SEXP CA, SEXP OA,
	   SEXP VCMAX, SEXP VPMAX,
	  SEXP VPR, SEXP JMAX, SEXP STOMATAWS)
{
  /* const int NLAYERS = 3;  this should eventually be replaced with
			    an argument coming in from R */
  struct ET_Str tmp5, tmp6;


  int i;
  double Idir, Idiff, cosTh;
  double LAIc;
  double IDir, IDiff, Itot, rh, WS;
  double pLeafsun, pLeafshade;
  double Leafsun, Leafshade;

/*   double tmp5,tmp6; */
  double TempIdir,TempIdiff,AssIdir,AssIdiff;

  double CanopyA;
  double CanHeight;

  const double cf = 3600 * 1e-6 ;

  int DOY , hr;
  double LAI, solarR, Temp, RH, WindSpeed;
  double Ca, Oa, Vcmax, Vpmax, Vpr, Jmax;
  double upperT=27.5, lowerT=3.0;

  double lat = 40;
  int nlayers = 3;
  double kd = 0.1;
  double chil = 1;
  double stomataws;

  SEXP growth;

  PROTECT(growth = allocVector(REALSXP,1));


  LAI = REAL(lai)[0];
  DOY = INTEGER(Doy)[0];
  hr = INTEGER(Hr)[0];
  solarR = REAL(SolarR)[0];
  Temp = REAL(ATemp)[0];
  RH = REAL(RelH)[0];
  WindSpeed = REAL(WindS)[0];
  Ca = REAL(CA)[0];
  Oa = REAL(OA)[0];
  Vcmax = REAL(VCMAX)[0];
  Vpmax = REAL(VPMAX)[0];
  Vpr = REAL(VPR)[0];
  Jmax = REAL(JMAX)[0];
  stomataws = REAL(STOMATAWS)[0];

  lightME(lat,DOY,hr);

  Idir = tmp1[0] * solarR;
  Idiff = tmp1[1] * solarR;
  cosTh = tmp1[2];
    
  sunML(Idir,Idiff,LAI,nlayers,cosTh,kd,chil, 3);

  /* results from multilayer model */
  LAIc = LAI / nlayers;
  /* Next I need the RH and wind profile */
  RHprof(RH,nlayers);
  WINDprof(WindSpeed,LAI,nlayers);

  /* Next use the EvapoTrans function */
  CanopyA=0.0;
  for(i=0;i<nlayers;i++)
    {
      IDir = layIdir[--sp1];
      Itot = layItotal[--sp3];

      rh = tmp4[--tp4];
      WS = tmp3[--tp3];
      pLeafsun = layFsun[--sp4];
      CanHeight = layHeight[--sp6];
      Leafsun = LAIc * pLeafsun;
      tmp5 = EvapoTrans(IDir,Itot,Temp,rh,WS,Leafsun,CanHeight,stomataws,1,39,0.04,0.7,0.83,0.93,0.8,0.01,3,upperT,lowerT);
      /* not the right thing to do here to add these values at the end of the ET function
but just a simple fix for now. The problem is that the eC4photoC function should have its own
EvapoTrans function. */
      TempIdir = Temp + tmp5.Deltat;
      AssIdir = eC4photoC(IDir,TempIdir,rh,Ca,Oa,Vcmax,
			 Vpmax,Vpr,Jmax);

      IDiff = layIdiff[--sp2];
      pLeafshade = layFshade[--sp5];
      Leafshade = LAIc * pLeafshade;
      tmp6 = EvapoTrans(IDiff,Itot,Temp,rh,WS,Leafshade,CanHeight,stomataws,1,39,0.04,0.7,0.83,0.93,0.8,0.01,3,upperT,lowerT);
      /* not the right thing to do here to add these values at the end of the ET function
but just a simple fix for now*/
      TempIdiff = Temp + tmp6.Deltat;
      AssIdiff = eC4photoC(IDiff,TempIdiff,rh,Ca,Oa,Vcmax,
			  Vpmax,Vpr,Jmax);

     CanopyA += Leafsun * AssIdir + Leafshade * AssIdiff;
   }
  /* These are micro mols of CO2 per m2 per sec
    Need to convert to 
    3600 converts seconds to hours
    10^-6 converts micro mols to mols
    */
  REAL(growth)[0] = cf * CanopyA ;
  UNPROTECT(1);  
  return(growth);

   }
Example #2
0
SEXP c3CanA(SEXP Lai,
	    SEXP Doy,
	    SEXP HR,
	    SEXP SOLAR,
	    SEXP TEMP,
	    SEXP ReH,
	    SEXP windspeed,
	    SEXP LAT,
	    SEXP NLAYERS, 
	    SEXP PHOTOPS, 
	    SEXP KD, 
	    SEXP HEIGHTF, 
	    SEXP LNPS)
{
/* Declaring the struct for the Evaop Transpiration */
   struct ET_Str tmp5_ET , tmp6_ET; 
   struct c3_str tmpc3, tmpc32; 

  const double cf = 3600 * 1e-6 * 30 * 1e-6 * 10000;
/* Need a different conversion factor for transpiration */
  const double cf2 = 3600 * 1e-3 * 18 * 1e-6 * 10000; 
  /* 3600 - number of seconds in an hour */
  /* 1e-3 - mili mols to mols */
  /* 18 - grams in one mol of H20 */
  /* 1e-6 - grams to mega grams */
  /* 10000 - meters in one hectare */

  int i;
  double Idir, Idiff, cosTh;
  double LAIc;
  double IDir, IDiff, Itot,rh, WindS;
  double TempIdir,TempIdiff,AssIdir,AssIdiff;
  double  pLeafsun, pLeafshade;
  double Leafsun, Leafshade;

  double CanopyA ;
  double CanopyT , CanopyPe = 0.0, CanopyPr = 0.0;
  double CanopyC = 0.0;
  double CanHeight;
  /* Need to label them something different from the arguments of the c4photoC
     function because these need to be modified by the water stress effect on
     the stomata and assimilation. The Ball-Berry parameters are not implemented
     yet (I need to think about this a bit harder). */
  double vmax1;
  double leafN_lay;


  /* INTEGERS */
  int DOY = INTEGER(Doy)[0];
  int hr = INTEGER(HR)[0];
  int nlayers = INTEGER(NLAYERS)[0];

  /* REALS */
  double LAI = REAL(Lai)[0];
  double solarR = REAL(SOLAR)[0];
  double Temp = REAL(TEMP)[0];
  double RlH = REAL(ReH)[0];
  double WindSpeed = REAL(windspeed)[0];
  double lat = REAL(LAT)[0];
  double kd = REAL(KD)[0];
  double heightf = REAL(HEIGHTF)[0];
  double chil = 1;

  /* Photosynthesis parameters */

  double jmax1 = REAL(PHOTOPS)[1];
  double Rd1 = REAL(PHOTOPS)[2];
  double Catm = REAL(PHOTOPS)[3];
  double O2 = REAL(PHOTOPS)[4];
  double b01 = REAL(PHOTOPS)[5];
  double b11 = REAL(PHOTOPS)[6];
  double theta = REAL(PHOTOPS)[7];

  double  LeafN = REAL(LNPS)[0];
  double  kpLN = REAL(LNPS)[1];
  double lnb0 = REAL(LNPS)[2];
  double lnb1 = REAL(LNPS)[3];
  int lnfun = REAL(LNPS)[4]; /* Coercing a double to integer */

  SEXP lists;
  SEXP names;
  SEXP growth;
  SEXP trans;
  SEXP epen;
  SEXP epries;
  SEXP cond;

  SEXP mat1;

  PROTECT(lists = allocVector(VECSXP,6));
  PROTECT(names = allocVector(STRSXP,6));
  PROTECT(growth = allocVector(REALSXP,1));
  PROTECT(trans = allocVector(REALSXP,1));
  PROTECT(epen = allocVector(REALSXP,1));
  PROTECT(epries = allocVector(REALSXP,1));
  PROTECT(cond = allocVector(REALSXP,1));

  PROTECT(mat1 = allocMatrix(REALSXP,14,nlayers));


  /* Light Macro Environment. As a side effect it populates tmp1. This
   * should eventually be replaced by a structure. */

  lightME(lat,DOY,hr);

  Idir = tmp1[0] * solarR;
  Idiff = tmp1[1] * solarR;
  cosTh = tmp1[2];

/* sun multilayer model. As a side effect it populates the layIdir, layItotal, layFsun, layHeight,
layIdiff, layShade vectors. */
    
    sunML(Idir,Idiff,LAI,nlayers,cosTh,kd,chil,heightf);

    /* results from multilayer model */
    LAIc = LAI / nlayers;
    /* Next I need the RH and wind profile */

    RHprof(RlH,nlayers);
    /* It populates tmp4. */

     WINDprof(WindSpeed,LAI,nlayers);
    /* It populates tmp3. */

     LNprof(LeafN, LAI, nlayers, kpLN);
    /* It populates tmp5 */

    /* Next use the EvapoTrans function */
    CanopyA=0.0;
    CanopyT=0.0;

    for(i=0;i<nlayers;i++)
    {
/* vmax depends on leaf nitrogen and this in turn depends on the layer */
	    leafN_lay = tmp5[--tp5];
	    if(lnfun == 0){
		    vmax1 = REAL(PHOTOPS)[0];
	    }else{
		    vmax1 = leafN_lay * lnb1 + lnb0;
/* For now alpha is not affected by leaf nitrogen */
	    }

	    IDir = layIdir[--sp1];
	    Itot = layItotal[--sp3];
	    
	    rh = tmp4[--tp4];
	    WindS = tmp3[--tp3];

	    pLeafsun = layFsun[--sp4];
	    CanHeight = layHeight[--sp6];
	    Leafsun = LAIc * pLeafsun;
/* Need a new evapo transpiration function specifically for c3*/
	    tmp5_ET = c3EvapoTrans(IDir,Itot,Temp,rh,WindS,Leafsun,CanHeight,
				 vmax1,jmax1,Rd1,b01,b11,Catm,210,theta);
	    TempIdir = Temp + tmp5_ET.Deltat;
	    tmpc3 = c3photoC(IDir,TempIdir,rh,vmax1,jmax1,Rd1,b01,b11,Catm,O2,theta);
	    AssIdir = tmpc3.Assim;

	    IDiff = layIdiff[--sp2];
	    pLeafshade = layFshade[--sp5];
	    Leafshade = LAIc * pLeafshade;
	    tmp6_ET = c3EvapoTrans(IDiff,Itot,Temp,rh,WindS,Leafshade,CanHeight,
				 vmax1,jmax1,Rd1,b01,b11,Catm,210,theta);
	    TempIdiff = Temp + tmp6_ET.Deltat;
	    tmpc32 = c3photoC(IDiff,TempIdiff,rh,vmax1,jmax1,Rd1,b01,b11,Catm,O2,theta);
	    AssIdiff = tmpc32.Assim;

    /* Collect direct radiation assim and trans in a matrix */
	    REAL(mat1)[i*14] = IDir;
	    REAL(mat1)[1 + i*14] = IDiff;
	    REAL(mat1)[2 + i*14] = Leafsun;
	    REAL(mat1)[3 + i*14] = Leafshade;
	    REAL(mat1)[4 + i*14] = tmp5_ET.TransR;
	    REAL(mat1)[5 + i*14] = tmp6_ET.TransR;
	    REAL(mat1)[6 + i*14] = AssIdir;
	    REAL(mat1)[7 + i*14] = AssIdiff;
	    REAL(mat1)[8 + i*14] = tmp5_ET.Deltat;
	    REAL(mat1)[9 + i*14] = tmp6_ET.Deltat;
	    REAL(mat1)[10 + i*14] = tmp5_ET.LayerCond; 
	    REAL(mat1)[11 + i*14] = tmp6_ET.LayerCond; 
	    REAL(mat1)[12 + i*14] = leafN_lay; 
	    REAL(mat1)[13 + i*14] = vmax1; 

/*      REAL(mat1)[11 + i*12] = rh;  */
    /*Layer conductance needs to be transformed back to the correct units here*/

	    CanopyA += Leafsun * AssIdir + Leafshade * AssIdiff;
	    CanopyT += Leafsun * tmp5_ET.TransR + Leafshade * tmp6_ET.TransR; 
	    CanopyPe += Leafsun * tmp5_ET.EPenman + Leafshade * tmp6_ET.EPenman;
	    CanopyPr += Leafsun * tmp5_ET.EPriestly + Leafshade * tmp6_ET.EPriestly;
	    CanopyC += Leafsun * tmp5_ET.LayerCond + Leafshade * tmp6_ET.LayerCond;

    }
 /*## These are micro mols of CO2 per m2 per sec
   ## Need to convert to 
   ## 3600 converts seconds to hours
   ## 10^-6 converts micro mols to mols
   ## 30 converts mols of CO2 to grams
   ## (1/10^6) converts grams to Mg
   ## 10000 scales up to ha */
    REAL(growth)[0] = cf * CanopyA ;
    REAL(trans)[0] = cf2 * CanopyT ;
    REAL(epen)[0] = cf2 * CanopyPe ;
    REAL(epries)[0] = cf2 * CanopyPr ;
    REAL(cond)[0] = CanopyC;

    SET_VECTOR_ELT(lists,0,growth);
    SET_VECTOR_ELT(lists,1,trans);
    SET_VECTOR_ELT(lists,2,cond);
    SET_VECTOR_ELT(lists,3,epen);
    SET_VECTOR_ELT(lists,4,epries);
    SET_VECTOR_ELT(lists,5,mat1);

    SET_STRING_ELT(names,0,mkChar("CanopyAssim"));
    SET_STRING_ELT(names,1,mkChar("CanopyTrans"));
    SET_STRING_ELT(names,2,mkChar("CanopyCond"));
    SET_STRING_ELT(names,3,mkChar("TranEpen"));
    SET_STRING_ELT(names,4,mkChar("TranEpries"));
    SET_STRING_ELT(names,5,mkChar("LayMat"));
    setAttrib(lists,R_NamesSymbol,names);

    UNPROTECT(8);
    return(lists);
   }