Ejemplo n.º 1
0
void FlowManager::output(const string &fileName) const
{
    NcFile *file;

    struct stat statInfo;
    int ret = stat(fileName.c_str(), &statInfo);

    NcError ncError(NcError::silent_nonfatal);

    if (ret != 0 || TimeManager::isFirstStep()) {
        file = new NcFile(fileName.c_str(), NcFile::Replace);
    } else {
        file = new NcFile(fileName.c_str(), NcFile::Write);
    }

    if (!file->is_valid()) {
        char message[100];
        sprintf(message, "Failed to open file %s.", fileName.c_str());
        REPORT_ERROR(message)
    }
Ejemplo n.º 2
0
//=========================================================================
//
// WriteNetCDF(DICE *dice, BORG_Archive result, string ncfile)
//
// Writes the full model output for a set of solutions and (if applicable)
// climate sentitivities.
//
//=========================================================================
void WriteNetCDF(DICE *dice, BORG_Archive result, const char *ncfile)
{
  // Loop control variables
  int iInd, jInd, tInd;
  
  // Was doeclim used?
  int use_doeclim = dice->ctrl.use_doeclim;  
  
  // Define the dimensions for the vectors
  int nPeriods = dice->config.nPeriods;
  int nSamples = dice->ctrl.csSamples;
  int nDoeclimts = dice->clim.ns;
  int nSolutions = BORG_Archive_get_size(result);
  
  int ndims = 3;

  // Dimension ids
  int nPeriodsid, nSamplesid, nSolutionsid, nDoeclimtsid;
  int fulldimids[ndims];
  int extfulldimids[ndims];
  int per_sol_dimids[2];
  int sol_sam_dimids[2];
  int sam_dimids[1];
  int per_dimids[1];
  
  // netcdf file id
  int ncid;
  
  // Variable ids
  int tatmid, matid, miuid, sid, utilityid, csid, oceandiffid, alphaid;
  int yearid, forcid, toceanid, muid, mlid, ccaid, kid, cid;
  int cpcid, iid, riid, yid, ygrossid, ynetid, damagesid, damfracid;
  int abatecostid, perioduid, sccid, lid, gaid, alid, gsigid;
  int sigmaid, cost1id, etreeid, eindid, rrid, forcothid, partfractid;
  int pv_abatecostid, pv_damagesid, totalcostid, pv_totalcostid;
  int pbacktimeid, eid, mcabateid, cemutotperid;
  
  int tempid, doeclim_forcid, heat_mixedid;
  
  // Initialize the variable vectors
  float tatm[nPeriods][nSolutions][nSamples]; 
  float mat[nPeriods][nSolutions][nSamples];
  float forc[nPeriods][nSolutions][nSamples];  
  float tocean[nPeriods][nSolutions][nSamples]; 
  float mu[nPeriods][nSolutions][nSamples];
  float ml[nPeriods][nSolutions][nSamples];
  float cca[nPeriods][nSolutions][nSamples];
  float k[nPeriods][nSolutions][nSamples];
  float c[nPeriods][nSolutions][nSamples];
	float cpc[nPeriods][nSolutions][nSamples];
  float i[nPeriods][nSolutions][nSamples];
  float ri[nPeriods][nSolutions][nSamples];
  float y[nPeriods][nSolutions][nSamples];
  float ygross[nPeriods][nSolutions][nSamples];
	float ynet[nPeriods][nSolutions][nSamples];
  float damages[nPeriods][nSolutions][nSamples];
  float damfrac[nPeriods][nSolutions][nSamples];
  float abatecost[nPeriods][nSolutions][nSamples];
  float periodu[nPeriods][nSolutions][nSamples];
  float scc[nPeriods][nSolutions][nSamples];
  float eind[nPeriods][nSolutions][nSamples];
  float pv_abatecost[nPeriods][nSolutions][nSamples];
  float pv_damages[nPeriods][nSolutions][nSamples];
  float totalcost[nPeriods][nSolutions][nSamples];
  float pv_totalcost[nPeriods][nSolutions][nSamples];
  float pbacktime[nPeriods][nSolutions][nSamples];
  float e[nPeriods][nSolutions][nSamples];
  float mcabate[nPeriods][nSolutions][nSamples]; 
  float cemutotper[nPeriods][nSolutions][nSamples];
  
  float temp[nDoeclimts][nSolutions][nSamples];
	float doeclim_forc[nDoeclimts][nSolutions][nSamples];
  float heat_mixed[nDoeclimts][nSolutions][nSamples];

  float miu[nPeriods][nSolutions];
	float utility[nSolutions][nSamples];
     
  float cs[nSamples];
  float oceandiff[nSamples];
  float alpha[nSamples];

  short year[nPeriods];
  float l[nPeriods];
  float ga[nPeriods];
  float al[nPeriods];
  float gsig[nPeriods];
  float sigma[nPeriods];
  float cost1[nPeriods];
  float etree[nPeriods];
  float rr[nPeriods];
  float forcoth[nPeriods];
  float partfract[nPeriods];
  
  // If the savings rate is set from the control file,
  // we will only need to provide a single dimension
  // for the savings rate output
  float s1[nPeriods];				// Savings rate if set in control file
  float s2[nPeriods][nSolutions];	// Savings rate if used as decision variable
  
  // Setup the netcdf file
  int retval;
  if((retval = nc_create(ncfile, NC_64BIT_OFFSET, &ncid))) { ncError(retval); }
  
  // Define the dimensions in the netcdf file
  if((retval = nc_def_dim(ncid, "nPeriods", nPeriods, &nPeriodsid))) { ncError(retval); }
  if((retval = nc_def_dim(ncid, "nSolutions", nSolutions, &nSolutionsid))) { ncError(retval); }
  if((retval = nc_def_dim(ncid, "nSamples", nSamples, &nSamplesid))) { ncError(retval); }
  
  if(use_doeclim == 1) {
    if((retval = nc_def_dim(ncid, "nDoeclimts", nDoeclimts, &nDoeclimtsid))) { ncError(retval); }
  }

  // Gather the dimension IDs into a vector
  fulldimids[0] = nPeriodsid;
  fulldimids[1] = nSolutionsid;
  fulldimids[2] = nSamplesid;
  
  extfulldimids[0] = nDoeclimtsid;
  extfulldimids[1] = nSolutionsid;
  extfulldimids[2] = nSamplesid;
  
  per_sol_dimids[0] = nPeriodsid;
  per_sol_dimids[1] = nSolutionsid;
  
  sol_sam_dimids[0] = nSolutionsid;
  sol_sam_dimids[1] = nSamplesid;
  
  sam_dimids[0] = nSamplesid;
  
  per_dimids[0] = nPeriodsid;
  
  // Create the netcdf variables
  if((retval = nc_def_var(ncid, "mat", NC_FLOAT, 3, fulldimids, &matid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "forc", NC_FLOAT, 3, fulldimids, &forcid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "mu", NC_FLOAT, 3, fulldimids, &muid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "ml", NC_FLOAT, 3, fulldimids, &mlid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "cca", NC_FLOAT, 3, fulldimids, &ccaid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "k", NC_FLOAT, 3, fulldimids, &kid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "c", NC_FLOAT, 3, fulldimids, &cid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "cpc", NC_FLOAT, 3, fulldimids, &cpcid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "i", NC_FLOAT, 3, fulldimids, &iid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "ri", NC_FLOAT, 3, fulldimids, &riid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "y", NC_FLOAT, 3, fulldimids, &yid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "ygross", NC_FLOAT, 3, fulldimids, &ygrossid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "ynet", NC_FLOAT, 3, fulldimids, &ynetid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "damages", NC_FLOAT, 3, fulldimids, &damagesid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "damfrac", NC_FLOAT, 3, fulldimids, &damfracid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "abatecost", NC_FLOAT, 3, fulldimids, &abatecostid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "periodu", NC_FLOAT, 3, fulldimids, &perioduid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "eind", NC_FLOAT, 3, fulldimids, &eindid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "pv_abatecost", NC_FLOAT, 3, fulldimids, &pv_abatecostid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "pv_damages", NC_FLOAT, 3, fulldimids, &pv_damagesid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "totalcost", NC_FLOAT, 3, fulldimids, &totalcostid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "pv_totalcost", NC_FLOAT, 3, fulldimids, &pv_totalcostid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "e", NC_FLOAT, 3, fulldimids, &eid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "mcabate", NC_FLOAT, 3, fulldimids, &mcabateid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "cemutotper", NC_FLOAT, 3, fulldimids, &cemutotperid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "scc", NC_FLOAT, 3, fulldimids, &sccid))) { ncError(retval); }

  if(use_doeclim == 1) {
    if((retval = nc_def_var(ncid, "temp", NC_FLOAT, 3, extfulldimids, &tempid))) { ncError(retval); }
    if((retval = nc_def_var(ncid, "doeclim_forc", NC_FLOAT, 3, extfulldimids, &doeclim_forcid))) { ncError(retval); }
    if((retval = nc_def_var(ncid, "heat_mixed", NC_FLOAT, 3, extfulldimids, &heat_mixedid))) { ncError(retval); }
  }
  else {
    if((retval = nc_def_var(ncid, "tatm", NC_FLOAT, 3, fulldimids, &tatmid))) { ncError(retval); }
    if((retval = nc_def_var(ncid, "tocean", NC_FLOAT, 3, fulldimids, &toceanid))) { ncError(retval); }
  }
  
  
  if((retval = nc_def_var(ncid, "miu", NC_FLOAT, 2, per_sol_dimids, &miuid))) { ncError(retval); }
  
  if((retval = nc_def_var(ncid, "utility", NC_FLOAT, 2, sol_sam_dimids, &utilityid))) { ncError(retval); }
  
  if((retval = nc_def_var(ncid, "cs", NC_FLOAT, 1, sam_dimids, &csid))) { ncError(retval); }  
  if(use_doeclim == 1) {
  	if((retval = nc_def_var(ncid, "oceandiff", NC_FLOAT, 1, sam_dimids, &oceandiffid))) { ncError(retval); }
  	if((retval = nc_def_var(ncid, "alpha", NC_FLOAT, 1, sam_dimids, &alphaid))) { ncError(retval); }
  }
  
  if((retval = nc_def_var(ncid, "Year", NC_SHORT, 1, per_dimids, &yearid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "l", NC_FLOAT, 1, per_dimids, &lid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "ga", NC_FLOAT, 1, per_dimids, &gaid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "al", NC_FLOAT, 1, per_dimids, &alid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "gsig", NC_FLOAT, 1, per_dimids, &gsigid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "sigma", NC_FLOAT, 1, per_dimids, &sigmaid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "cost1", NC_FLOAT, 1, per_dimids, &cost1id))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "etree", NC_FLOAT, 1, per_dimids, &etreeid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "rr", NC_FLOAT, 1, per_dimids, &rrid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "forcoth", NC_FLOAT, 1, per_dimids, &forcothid))) { ncError(retval); }
  if((retval = nc_def_var(ncid, "partfract", NC_FLOAT, 1, per_dimids, &partfractid))) { ncError(retval); }
  
  // If the savings rate is set in the control file,
  // the savings rate will only need one dimension
  if (dice->ctrl.setSavings < 0) {
    if((retval = nc_def_var(ncid, "s", NC_FLOAT, 2, per_sol_dimids, &sid))) { ncError(retval); }
  }
  else {
    if((retval = nc_def_var(ncid, "s", NC_FLOAT, 1, per_dimids, &sid))) { ncError(retval); }
  }
  
  // End "Metadata" mode
  if((retval = nc_enddef(ncid))) { ncError(retval); }
  
  // Populate the variable vectors with the appropriate data
  // First, loop over the BORG solutions
  for(iInd=0; iInd<nSolutions; iInd++) {

    // Get this BORG solution
    BORG_Solution this_solution = BORG_Archive_get(result, iInd);
    
    // Apply the decision vector to the dice object
    for(tInd=0; tInd<dice->ctrl.nvars; tInd++) {
      if(tInd < 59) {
      	if(dice->ctrl.policy_inertia <= 0.0) {
					dice->dvars.miu[tInd+1] = BORG_Solution_get_variable(this_solution, tInd);
				}
				else {
					dice->dvars.miu[tInd+1] = dice->dvars.miu[tInd] + (BORG_Solution_get_variable(this_solution, tInd) * dice->ctrl.policy_inertia);
				}
      }
      else {
	dice->dvars.s[tInd-59] = BORG_Solution_get_variable(this_solution, tInd);
      }
    }
    
    // Store the decision variables from this solution
    for(tInd=0; tInd<nPeriods; tInd++) {
      miu[tInd][iInd] = (float) dice->dvars.miu[tInd];
      if (dice->ctrl.setSavings < 0) {
	s2[tInd][iInd] = (float) dice->dvars.s[tInd];
      }
    }			
    
    // Loop over the climate sensitivity samples
    for(jInd=0; jInd<nSamples; jInd++) {
      
      // If this is the first BORG solution, put
      // the climate sensitivity in its vector
      if(iInd==0) {
				cs[jInd] = (float) dice->clim.cs_sample_vec[jInd];
				if(use_doeclim == 1) {
					oceandiff[jInd] = (float) dice->clim.ocean_diff_vec[jInd];
					alpha[jInd] = (float) dice->clim.alpha_vec[jInd];
				}
      }
      
      // Apply this climate sensitivity to the dice object
      if(use_doeclim == 1) {
	dice->clim.t2co = dice->clim.cs_sample_vec[jInd];
	dice->clim.kappa = dice->clim.ocean_diff_vec[jInd];
	dice->clim.alpha = dice->clim.alpha_vec[jInd];
	doeclim_DICE_init(dice);
      }
      else {
	dice->clim.t2xco2 = dice->clim.cs_sample_vec[jInd];
      }
            
      // Run CDICE with the SCC calculation
      calcSCC(dice, this_solution);
      //calc_CDICE(dice);
      
      // Capture the full time series of the model variables
      // in the appropriate vector
      utility[iInd][jInd] = (float) dice->econ.utility;
      for(tInd=0; tInd<nPeriods; tInd++) {
	
	if(use_doeclim == 0) {
	  tatm[tInd][iInd][jInd] = (float) dice->clim.tatm[tInd];
	  tocean[tInd][iInd][jInd] = (float) dice->clim.tocean[tInd];
	}
	mat[tInd][iInd][jInd] = (float) dice->carb.mat[tInd];
	forc[tInd][iInd][jInd] = (float) dice->carb.forc[tInd];
	mu[tInd][iInd][jInd] = (float) dice->carb.mu[tInd];
	ml[tInd][iInd][jInd] = (float) dice->carb.ml[tInd];
	cca[tInd][iInd][jInd] = (float) dice->econ.cca[tInd];
	k[tInd][iInd][jInd] = (float) dice->econ.k[tInd];
	c[tInd][iInd][jInd] = (float) dice->econ.c[tInd];
	cpc[tInd][iInd][jInd] = (float) dice->econ.cpc[tInd];
	i[tInd][iInd][jInd] = (float) dice->econ.i[tInd];
	ri[tInd][iInd][jInd] = (float) dice->econ.ri[tInd];
	y[tInd][iInd][jInd] = (float) dice->econ.y[tInd];
	ygross[tInd][iInd][jInd] = (float) dice->econ.ygross[tInd];
	ynet[tInd][iInd][jInd] = (float) dice->econ.ynet[tInd];
	damages[tInd][iInd][jInd] = (float) dice->econ.damages[tInd];
	damfrac[tInd][iInd][jInd] = (float) dice->econ.damfrac[tInd];
	abatecost[tInd][iInd][jInd] = (float) dice->econ.abatecost[tInd];
	periodu[tInd][iInd][jInd] = (float) dice->econ.periodu[tInd];
	eind[tInd][iInd][jInd] = (float) dice->econ.eind[tInd];
	pv_abatecost[tInd][iInd][jInd] = (float) dice->econ.pv_abatecost[tInd];
	pv_damages[tInd][iInd][jInd] = (float) dice->econ.pv_damages[tInd];
	totalcost[tInd][iInd][jInd] = (float) dice->econ.totalcost[tInd];
	pv_totalcost[tInd][iInd][jInd] = (float) dice->econ.pv_totalcost[tInd];
	e[tInd][iInd][jInd] = (float) dice->econ.e[tInd];
	mcabate[tInd][iInd][jInd] = (float) dice->econ.mcabate[tInd];
	cemutotper[tInd][iInd][jInd] = (float) dice->econ.cemutotper[tInd];
	scc[tInd][iInd][jInd] = (float) dice->econ.scc[tInd];
	
	
	if((iInd == 0) && (jInd == 0)) {
	  year[tInd] = (short) dice->config.dateSeries[tInd];
	  l[tInd] = (float) dice->econ.l[tInd];
	  ga[tInd] = (float) dice->econ.ga[tInd];
	  al[tInd] = (float) dice->econ.al[tInd];
	  gsig[tInd] = (float) dice->econ.gsig[tInd];
	  sigma[tInd] = (float) dice->econ.sigma[tInd];
	  cost1[tInd] = (float) dice->econ.cost1[tInd];
	  etree[tInd] = (float) dice->econ.etree[tInd];
	  rr[tInd] = (float) dice->econ.rr[tInd];
	  forcoth[tInd] = (float) dice->carb.forcoth[tInd];
	  partfract[tInd] = (float) dice->econ.partfract[tInd];
	  if (dice->ctrl.setSavings >= 0) {
	    s1[tInd] = (float) dice->dvars.s[tInd];
	  }
	}
	
	
      } // end loop over nPeriods
      
      // If DOEclim was used, store the variables with appropriate
      // dimensions.
      if(use_doeclim == 1) {
	for(tInd=0; tInd<nDoeclimts; tInd++) {
	  temp[tInd][iInd][jInd] = (float) dice->clim.temp[tInd];
	  doeclim_forc[tInd][iInd][jInd] = (float) dice->clim.forc[tInd];
	  heat_mixed[tInd][iInd][jInd] = (float) dice->clim.heat_mixed[tInd];
	}
      }
      
    } // end loop over nSamples
    
  } // end loop over nSolutions

  // Write these variable vectors to the netcdf file
  if(use_doeclim == 0) {
    if((retval = nc_put_var(ncid, tatmid, &tatm[0][0][0]))) { ncError(retval); }
    if((retval = nc_put_var(ncid, toceanid, &tocean[0][0][0]))) { ncError(retval); }
  }
  if((retval = nc_put_var(ncid, matid, &mat[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, forcid, &forc[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, muid, &mu[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, mlid, &ml[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, ccaid, &cca[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, kid, &k[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, cid, &c[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, cpcid, &cpc[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, iid, &i[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, riid, &ri[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, yid, &y[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, ygrossid, &ygross[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, ynetid, &ynet[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, damagesid, &damages[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, damfracid, &damfrac[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, abatecostid, &abatecost[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, perioduid, &periodu[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, eindid, &eind[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, pv_abatecostid, &pv_abatecost[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, pv_damagesid, &pv_damages[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, totalcostid, &totalcost[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, pv_totalcostid, &pv_totalcost[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, eid, &e[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, mcabateid, &mcabate[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, cemutotperid, &cemutotper[0][0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, sccid, &scc[0][0][0]))) { ncError(retval); }

  if(use_doeclim == 1) {
    if((retval = nc_put_var(ncid, tempid, &temp[0][0][0]))) { ncError(retval); }
    if((retval = nc_put_var(ncid, doeclim_forcid, &doeclim_forc[0][0][0]))) { ncError(retval); }
    if((retval = nc_put_var(ncid, heat_mixedid, &heat_mixed[0][0][0]))) { ncError(retval); }
    if((retval = nc_put_var(ncid, oceandiffid, &oceandiff[0]))) { ncError(retval); }
	  if((retval = nc_put_var(ncid, alphaid, &alpha[0]))) { ncError(retval); }
  }
  
  
  if((retval = nc_put_var(ncid, miuid, &miu[0][0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, utilityid, &utility[0][0]))) { ncError(retval); }
  
  if((retval = nc_put_var(ncid, csid, &cs[0]))) { ncError(retval); }
  
  if((retval = nc_put_var(ncid, yearid, &year[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, lid, &l[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, gaid, &ga[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, alid, &al[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, gsigid, &gsig[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, sigmaid, &sigma[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, cost1id, &cost1[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, etreeid, &etree[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, rrid, &rr[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, forcothid, &forcoth[0]))) { ncError(retval); }
  if((retval = nc_put_var(ncid, partfractid, &partfract[0]))) { ncError(retval); }
  
  if (dice->ctrl.setSavings < 0) {
    if((retval = nc_put_var(ncid, sid, &s2[0][0]))) { ncError(retval); }
  }
  else {
    if((retval = nc_put_var(ncid, sid, &s1[0]))) { ncError(retval); }
  }
  
  
  // Close the netcdf file
  if((retval = nc_close(ncid))) { ncError(retval); }
  
  return;
}
Ejemplo n.º 3
0
void PolygonManager::init()
{
    string fileName;
    ConfigTools::read("parcel_polygon_file", fileName);
    NOTICE("PolygonManager::init", "Reading polygons from \""+fileName+"\" ...");
    NcFile file(fileName.c_str(), NcFile::ReadOnly);
    if (!file.is_valid()) {
        REPORT_ERROR(string("Failed to open file "+fileName+"."))
    }

    NcError ncError(NcError::silent_nonfatal);

    if (TimeManager::onLine()) {
        NcAtt *timeAtt = file.get_att("time");
        NcAtt *timeStepAtt = file.get_att("time_step");
        NcAtt *stepsAtt = file.get_att("steps");
        if (timeAtt != NULL && timeStepAtt != NULL && stepsAtt != NULL) {
            TimeManager::reset();
            double dt = timeStepAtt->as_double(0);
            double second = timeAtt->as_double(0);
            double steps = stepsAtt->as_int(0);
            TimeManager::setClock(dt, second, steps);
        }
    }
    NcDim *numVertexDim = file.get_dim("num_total_vertex");
    if (numVertexDim == NULL) {
        Message message;
        message << "Failed to find \"num_total_vertex\" dimension in file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    NcDim *numEdgeDim = file.get_dim("num_total_edge");
    if (numEdgeDim == NULL) {
        Message message;
        message << "Failed to find \"num_total_edge\" dimension in file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    NcDim *numPolygonDim = file.get_dim("num_total_polygon");
    if (numPolygonDim == NULL) {
        Message message;
        message << "Failed to find \"num_total_polygon\" dimension in file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    int numVertex = static_cast<int>(numVertexDim->size());
    int numEdge = static_cast<int>(numEdgeDim->size());
    int numPolygon = static_cast<int>(numPolygonDim->size());
    // -------------------------------------------------------------------------
    // vertices part
    vertices.create(numVertex);
    double *oldVtxLon = new double[numVertex];
    double *oldVtxLat = new double[numVertex];
    double *oldVtxLev = new double[numVertex];
    double *newVtxLon = new double[numVertex];
    double *newVtxLat = new double[numVertex];
    double *newVtxLev = new double[numVertex];
    file.get_var("old_vertex_lon")->get(oldVtxLon, numVertex);
    file.get_var("old_vertex_lat")->get(oldVtxLat, numVertex);
    file.get_var("new_vertex_lon")->get(newVtxLon, numVertex);
    file.get_var("new_vertex_lat")->get(newVtxLat, numVertex);
    if (file.get_var("old_vertex_lev") != NULL) {
        file.get_var("old_vertex_lev")->get(oldVtxLev, numVertex);
        file.get_var("new_vertex_lev")->get(newVtxLev, numVertex);
    } else {
        for (int i = 0; i < vertices.size(); ++i) {
            oldVtxLev[i] = 0.0;
            newVtxLev[i] = 0.0;
        }
    }
    // change the units from degree to rad
    for (int i = 0; i < numVertex; ++i) {
        oldVtxLon[i] /= Rad2Deg;
        oldVtxLat[i] /= Rad2Deg;
        newVtxLon[i] /= Rad2Deg;
        newVtxLat[i] /= Rad2Deg;
    }
    Vertex *vertexMap[vertices.size()];
    Vertex *vertex = vertices.front();
    for (int i = 0; i < vertices.size(); ++i) {
        vertexMap[i] = vertex;
        vertex->setCoordinate(newVtxLon[i], newVtxLat[i], newVtxLev[i], NewTimeLevel);
        vertex->setCoordinate(oldVtxLon[i], oldVtxLat[i], oldVtxLev[i], OldTimeLevel);
        vertex = vertex->next;
    }
    delete [] newVtxLon;
    delete [] newVtxLat;
    delete [] newVtxLev;
    delete [] oldVtxLon;
    delete [] oldVtxLat;
    delete [] oldVtxLev;
    // -------------------------------------------------------------------------
    // edges part
    edges.create(numEdge);
    int *firstPoint = new int[numEdge];
    int *secondPoint = new int[numEdge];
    file.get_var("first_point_idx")->get(firstPoint, numEdge);
    file.get_var("second_point_idx")->get(secondPoint, numEdge);
    Edge *edgeMap[edges.size()];
    Edge *edge = edges.front();
    for (int i = 0; i < edges.size(); ++i) {
        edgeMap[i] = edge;
        edge->linkEndPoint(FirstPoint, vertexMap[firstPoint[i]-1]);
        edge->linkEndPoint(SecondPoint, vertexMap[secondPoint[i]-1]);
        edge->calcNormVector();
        edge = edge->next;
    }
    delete [] firstPoint;
    delete [] secondPoint;
    // test point
    double *oldTestLon = new double[numEdge];
    double *oldTestLat = new double[numEdge];
    double *newTestLon = new double[numEdge];
    double *newTestLat = new double[numEdge];
    file.get_var("old_testpoint_lon")->get(oldTestLon, numEdge);
    file.get_var("old_testpoint_lat")->get(oldTestLat, numEdge);
    file.get_var("new_testpoint_lon")->get(newTestLon, numEdge);
    file.get_var("new_testpoint_lat")->get(newTestLat, numEdge);
    for (int i = 0; i < numEdge; ++i) {
        oldTestLon[i] /= Rad2Deg;
        oldTestLat[i] /= Rad2Deg;
        newTestLon[i] /= Rad2Deg;
        newTestLat[i] /= Rad2Deg;
    }
    edge = edges.front();
    for (int i = 0; i < numEdge; ++i) {
        Vertex *testPoint = edge->getTestPoint();
        testPoint->setCoordinate(oldTestLon[i], oldTestLat[i], OldTimeLevel);
        testPoint->setCoordinate(newTestLon[i], newTestLat[i], NewTimeLevel);
        edge = edge->next;
    }
    delete [] oldTestLon;
    delete [] oldTestLat;
    delete [] newTestLon;
    delete [] newTestLat;
    // -------------------------------------------------------------------------
    // polygons part
    polygons.create(numPolygon);
    int edgeNum[numPolygon];
    file.get_var("edge_num")->get(edgeNum, numPolygon);
    int numEdgeIdx = static_cast<int>(file.get_dim("num_edge_idx")->size());
    int *edgeIdx = new int[numEdgeIdx];
    int *edgeOnt = new int[numEdgeIdx];
    file.get_var("edge_idx")->get(edgeIdx, numEdgeIdx);
    file.get_var("edge_ont")->get(edgeOnt, numEdgeIdx);
    Polygon *polygon = polygons.front();
    int counter = 0;
    for (int i = 0; i < polygons.size(); ++i) {
        for (int j = 0; j < edgeNum[i]; ++j) {
            OrientStatus orient;
            if (edgeOnt[counter] == 0) {
                orient = OrientLeft;
            } else if (edgeOnt[counter] == 1) {
                orient = OrientRight;
            } else {
                string message = "Invalid edge_ont in file "+fileName+".";
                REPORT_ERROR(message.c_str());
            }
            edgeMap[edgeIdx[counter]-1]->linkPolygon(orient, polygon);
            counter++;
        }
        polygon->edgePointers.ring();
        // calculate the angles
        EdgePointer *edgePointer = polygon->edgePointers.front();
        for (int j = 0; j < polygon->edgePointers.size(); ++j) {
            edgePointer->calcAngle();
            edgePointer = edgePointer->next;
        }
        polygon->calcArea();
        polygon = polygon->next;
    }
    delete [] edgeIdx;
    delete [] edgeOnt;
    // -------------------------------------------------------------------------
    file.close();
}
Ejemplo n.º 4
0
void TracerManager::registerTracer(const string &fileName,
                                   const MeshManager &meshManager)
{
    // -------------------------------------------------------------------------
    // parse and read file
    NcError ncError(NcError::silent_nonfatal);

    NcFile file(fileName.c_str(), NcFile::ReadOnly);
    if (!file.is_valid()) {
        Message message;
        message << "Failed to open tracer file \"" << fileName << "\"!";
        REPORT_ERROR(message.str());
    }

    if (file.get_att("name") == NULL) {
        Message message;
        message << "There is no \"name\" attribute in tracer file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    tracerNames.push_back(file.get_att("name")->as_string(0));

    if (file.get_att("name") == NULL) {
        Message message;
        message << "There is no \"name\" attribute in tracer file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    tracerNames.push_back(file.get_att("name")->as_string(0));

    int numPolygon = static_cast<int>(file.get_dim("num_total_polygon")->size());
    if (numPolygon != polygonManager.polygons.size()) {
        Message message;
        message << "Polygon numbers (" << numPolygon << " != ";
        message << polygonManager.polygons.size();
        message << ") are not consistent in tracer file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    double mass[numPolygon];
    if (file.get_var("mass") == NULL) {
        Message message;
        message << "There is no \"mass\" variable in tracer file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    file.get_var("mass")->get(mass);
    // -------------------------------------------------------------------------
    // initialize tracer density on the mesh
    tracerDensities.push_back(Field());
    if (meshManager.hasLayers())
        tracerDensities.back().init(meshManager.getMesh(PointCounter::Center),
                                    meshManager.getMesh(PointCounter::Bound),
                                    meshManager.getLayers(Layers::Full));
    else
        tracerDensities.back().init(meshManager.getMesh(PointCounter::Center),
                                    meshManager.getMesh(PointCounter::Bound));
    // -------------------------------------------------------------------------
    // set up tracer variables in polygons
    int tracerId = static_cast<int>(tracerNames.size()-1);
    Polygon *polygon = polygonManager.polygons.front();
    for (int i = 0; i < polygonManager.polygons.size(); ++i) {
        polygon->tracers.push_back(Tracer());
        polygon->tracers[tracerId].setMass(mass[i]);
        polygon = polygon->next;
    }
}