void SW_VES_read(void) {
    /* =================================================== */

    FILE *f;
    IntU i;

    MyFileName = SW_F_name(eVegEstab);
    f = OpenFile(MyFileName, "r");
    SW_VegEstab.use = TRUE;

    /* if data file empty or useflag=0, assume no
    * establishment checks and just continue the model run. */
    if (!GetALine(f, inbuf) || *inbuf == '0' ) {
        SW_VegEstab.use = FALSE;
        if (EchoInits)
            LogError(logfp, LOGNOTE, "Establishment not used.\n");
        return;
    }
    while(GetALine(f, inbuf) ) {
        _read_spp(inbuf);
    }
    CloseFile(&f);

    for (i=0; i < SW_VegEstab.count; i++)
        _spp_init(i);

    if ( SW_VegEstab.count > 0)
        SW_VegEstab.yrsum.days = (TimeInt *)
                                 Mem_Calloc(SW_VegEstab.count,
                                            sizeof(TimeInt),
                                            "SW_VES_read()");
    if (EchoInits) _echo_inits();
}
void SW_VES_read(void) {
	/* =================================================== */
	FILE *f;

	MyFileName = SW_F_name(eVegEstab);
	f = OpenFile(MyFileName, "r");
	SW_VegEstab.use = swTRUE;

	/* if data file empty or useflag=0, assume no
	 * establishment checks and just continue the model run. */
	if (!GetALine(f, inbuf) || *inbuf == '0') {
		SW_VegEstab.use = swFALSE;
		if (EchoInits)
			LogError(logfp, LOGNOTE, "Establishment not used.\n");
		CloseFile(&f);
		return;
	}

	while (GetALine(f, inbuf)) {
		_read_spp(inbuf);
	}

	CloseFile(&f);

	Init_SW_VegEstab();

	if (EchoInits)
		_echo_VegEstab();
}
void onSet_SW_SWC(SEXP SWC) {
	SW_SOILWAT *v = &SW_Soilwat;
	SEXP swcUseData;
	SEXP swcFilePrefix;
	SEXP swcFirstYear;
	SEXP swcMethod;

	MyFileName = SW_F_name(eSoilwat);
	v->surfaceTemp = 0;
	LyrIndex i;
	ForEachSoilLayer(i)
		v->sTemp[i] = SW_Site.lyr[i]->sTemp;

	PROTECT(swcUseData = GET_SLOT(SWC, install("UseSWCHistoricData")));
	PROTECT(swcFilePrefix = GET_SLOT(SWC, install("DataFilePrefix")));
	PROTECT(swcFirstYear = GET_SLOT(SWC, install("FirstYear")));
	PROTECT(swcMethod = GET_SLOT(SWC, install("Method")));

	v->hist_use = LOGICAL(swcUseData)[0];
	//if (!isnull(v->hist.file_prefix)) {//Clear memory before setting it
	//	Mem_Free(v->hist.file_prefix);
	//}
	v->hist.file_prefix = (char *) Str_Dup(CHAR(STRING_ELT(swcFilePrefix,0)));
	v->hist.yr.first = INTEGER(swcFirstYear)[0];
	v->hist.method = INTEGER(swcMethod)[0];

	if (v->hist.method < 1 || v->hist.method > 2) {
		LogError(logfp, LOGFATAL, "swcsetup.in : Invalid swc adjustment method.");
	}
	v->hist.yr.last = SW_Model.endyr;
	v->hist.yr.total = v->hist.yr.last - v->hist.yr.first + 1;
	UNPROTECT(4);
}
void SW_SWC_read(void) {
/* =================================================== */
/* Like all of the other "objects", read() reads in the
* setup parameters.  See _read_hist() for reading
* historical files.
*
*  1/25/02 - cwb - removed unused records of logfile and
*          start and end days.  also removed the SWTIMES dy
*          structure element.
*/

SW_SOILWAT *v = &SW_Soilwat;
FILE *f;
int lineno=0, nitems=4;

// gets the soil temperatures from where they are read in the SW_Site struct for use later
// SW_Site.c must call it's read function before this, or it won't work
LyrIndex i;
ForEachSoilLayer(i)
	v->sTemp[i] = SW_Site.lyr[i]->sTemp;

MyFileName = SW_F_name(eSoilwat);
f = OpenFile(MyFileName, "r");

while( GetALine(f, inbuf) ) {
switch(lineno) {
case 0:  v->hist_use = (atoi(inbuf)) ? TRUE : FALSE; break;
case 1:  v->hist.file_prefix = (char *)Str_Dup(inbuf);                break;
case 2:  v->hist.yr.first = yearto4digit(atoi(inbuf));  break;
case 3:  v->hist.method = atoi(inbuf);               break;
}
if (!v->hist_use)  return;
lineno++;
}
if (lineno < nitems) {
LogError(logfp, LOGFATAL,
"%s : Insufficient parameters specified.",MyFileName);
}
if (v->hist.method < 1 || v->hist.method > 2) {
LogError(logfp, LOGFATAL,
"%s : Invalid swc adjustment method.", MyFileName);
}
v->hist.yr.last = SW_Model.endyr;
v->hist.yr.total = v->hist.yr.last - v->hist.yr.first +1;
}
Example #5
0
void onSet_SW_SKY(SEXP sxp_SW_SKY) {
	int i, k = 6;
	SW_SKY *v = &SW_Sky;
	RealD *p_Cloud;
	PROTECT(sxp_SW_SKY);
	p_Cloud = REAL(GET_SLOT(sxp_SW_SKY, install("Cloud")));

	MyFileName = SW_F_name(eSky);

	for (i = 0; i < 12; i++) { //i=columns
		v->cloudcov[i] = p_Cloud[0 + k * i];
		v->windspeed[i] = p_Cloud[1 + k * i];
		v->r_humidity[i] = p_Cloud[2 + k * i];
		v->transmission[i] = p_Cloud[3 + k * i];
		v->snow_density[i] = p_Cloud[4 + k * i];
		v->n_rain_per_day[i] = p_Cloud[5 + k * i];
	}
	UNPROTECT(1);
}
void SW_SWC_adjust_swc( TimeInt doy) {
/* =================================================== */
/* 01/07/02 (cwb) added final loop to guarantee swc > swc_min
*/

SW_SOILWAT *v = &SW_Soilwat;
RealD lower, upper;
LyrIndex lyr;
TimeInt dy = doy -1;

switch (SW_Soilwat.hist.method) {
case SW_Adjust_Avg:
ForEachSoilLayer(lyr) {
v->swc[Today][lyr] += v->hist.swc[dy][lyr];
v->swc[Today][lyr] /= 2.;
}
break;

case SW_Adjust_StdErr:
ForEachSoilLayer(lyr) {
upper = v->hist.swc[dy][lyr] + v->hist.std_err[dy][lyr];
lower = v->hist.swc[dy][lyr] - v->hist.std_err[dy][lyr];
if ( GT(v->swc[Today][lyr], upper) )
v->swc[Today][lyr] = upper;
else if ( LT(v->swc[Today][lyr], lower) )
v->swc[Today][lyr] = lower;
}
break;

default:
LogError(logfp, LOGFATAL, "%s : Invalid SWC adjustment method.",
SW_F_name(eSoilwat));
}


/* this will guarantee that any method will not lower swc */
/* below the minimum defined for the soil layers          */
ForEachSoilLayer(lyr)
	v->swc[Today][lyr] = fmax(v->swc[Today][lyr], SW_Site.lyr[lyr]->swc_min);

}
void onSet_SW_VPD(SEXP SW_VPD) {
	int i;
	SW_VEGPROD *v = &SW_VegProd;

	SEXP VegComp;
	SEXP Albedo;
	SEXP Canopy;
	RealD *p_Canopy;
	SEXP VegInterception;
	RealD *p_VegInterception;
	SEXP LitterInterception;
	RealD *p_LitterInterception;
	SEXP EsTpartitioning_param;
	SEXP Es_param_limit;
	SEXP Shade;
	RealD *p_Shade;
	SEXP Hydraulic;
	SEXP Hydraulic_flag;
	SEXP CSWP;
	SEXP MonthlyVeg, Grasslands, Shrublands, Forest, Forb;
	SEXP CO2Coefficients;
	RealD *p_Grasslands, *p_Shrublands, *p_Forest, *p_Forb;

	MyFileName = SW_F_name(eVegProd);

	PROTECT(VegComp = GET_SLOT(SW_VPD, install(cVegProd_names[0])));
	v->veg[SW_GRASS].cov.fCover = REAL(VegComp)[0]; //Grass
	v->veg[SW_SHRUB].cov.fCover = REAL(VegComp)[1]; //Shrub
	v->veg[SW_TREES].cov.fCover = REAL(VegComp)[2]; //Tree
	v->veg[SW_FORBS].cov.fCover = REAL(VegComp)[3]; //Forb
	v->bare_cov.fCover = REAL(VegComp)[4]; //Bare Ground

	PROTECT(Albedo = GET_SLOT(SW_VPD, install(cVegProd_names[1])));
	v->veg[SW_GRASS].cov.albedo = REAL(Albedo)[0]; //Grass
	v->veg[SW_SHRUB].cov.albedo = REAL(Albedo)[1]; //Shrub
	v->veg[SW_TREES].cov.albedo = REAL(Albedo)[2]; //Tree
	v->veg[SW_FORBS].cov.albedo = REAL(Albedo)[3]; //Forb
	v->bare_cov.albedo = REAL(Albedo)[4]; //Bare Ground

	PROTECT(Canopy = GET_SLOT(SW_VPD, install(cVegProd_names[2])));
	p_Canopy = REAL(Canopy);
	v->veg[SW_GRASS].cnpy.xinflec = p_Canopy[0];
	v->veg[SW_GRASS].cnpy.yinflec = p_Canopy[1];
	v->veg[SW_GRASS].cnpy.range = p_Canopy[2];
	v->veg[SW_GRASS].cnpy.slope = p_Canopy[3];
	v->veg[SW_GRASS].canopy_height_constant = p_Canopy[4];
	v->veg[SW_SHRUB].cnpy.xinflec = p_Canopy[5];
	v->veg[SW_SHRUB].cnpy.yinflec = p_Canopy[6];
	v->veg[SW_SHRUB].cnpy.range = p_Canopy[7];
	v->veg[SW_SHRUB].cnpy.slope = p_Canopy[8];
	v->veg[SW_SHRUB].canopy_height_constant = p_Canopy[9];
	v->veg[SW_TREES].cnpy.xinflec = p_Canopy[10];
	v->veg[SW_TREES].cnpy.yinflec = p_Canopy[11];
	v->veg[SW_TREES].cnpy.range = p_Canopy[12];
	v->veg[SW_TREES].cnpy.slope = p_Canopy[13];
	v->veg[SW_TREES].canopy_height_constant = p_Canopy[14];
	v->veg[SW_FORBS].cnpy.xinflec = p_Canopy[15];
	v->veg[SW_FORBS].cnpy.yinflec = p_Canopy[16];
	v->veg[SW_FORBS].cnpy.range = p_Canopy[17];
	v->veg[SW_FORBS].cnpy.slope = p_Canopy[18];
	v->veg[SW_FORBS].canopy_height_constant = p_Canopy[19];

	PROTECT(VegInterception = GET_SLOT(SW_VPD, install(cVegProd_names[3])));
	p_VegInterception = REAL(VegInterception);
	v->veg[SW_GRASS].veg_kSmax = p_VegInterception[0];
	v->veg[SW_GRASS].veg_kdead = p_VegInterception[1];
	v->veg[SW_SHRUB].veg_kSmax = p_VegInterception[2];
	v->veg[SW_SHRUB].veg_kdead = p_VegInterception[3];
	v->veg[SW_TREES].veg_kSmax = p_VegInterception[4];
	v->veg[SW_TREES].veg_kdead = p_VegInterception[5];
	v->veg[SW_FORBS].veg_kSmax = p_VegInterception[6];
	v->veg[SW_FORBS].veg_kdead = p_VegInterception[7];

	PROTECT(LitterInterception = GET_SLOT(SW_VPD, install(cVegProd_names[4])));
	p_LitterInterception = REAL(LitterInterception);
	v->veg[SW_GRASS].lit_kSmax = p_LitterInterception[0];
	v->veg[SW_SHRUB].lit_kSmax = p_LitterInterception[1];
	v->veg[SW_TREES].lit_kSmax = p_LitterInterception[2];
	v->veg[SW_FORBS].lit_kSmax = p_LitterInterception[3];

	PROTECT(EsTpartitioning_param = GET_SLOT(SW_VPD, install(cVegProd_names[5])));
	v->veg[SW_GRASS].EsTpartitioning_param = REAL(EsTpartitioning_param)[0]; //Grass
	v->veg[SW_SHRUB].EsTpartitioning_param = REAL(EsTpartitioning_param)[1]; //Shrub
	v->veg[SW_TREES].EsTpartitioning_param = REAL(EsTpartitioning_param)[2]; //Tree
	v->veg[SW_FORBS].EsTpartitioning_param = REAL(EsTpartitioning_param)[3]; //Forb

	PROTECT(Es_param_limit = GET_SLOT(SW_VPD, install(cVegProd_names[6])));
	v->veg[SW_GRASS].Es_param_limit = REAL(Es_param_limit)[0]; //Grass
	v->veg[SW_SHRUB].Es_param_limit = REAL(Es_param_limit)[1]; //Shrub
	v->veg[SW_TREES].Es_param_limit = REAL(Es_param_limit)[2]; //Tree
	v->veg[SW_FORBS].Es_param_limit = REAL(Es_param_limit)[3]; //Forb

	PROTECT(Shade = GET_SLOT(SW_VPD, install(cVegProd_names[7])));
	p_Shade = REAL(Shade);
	v->veg[SW_GRASS].shade_scale = p_Shade[0];
	v->veg[SW_GRASS].shade_deadmax = p_Shade[1];
	v->veg[SW_GRASS].tr_shade_effects.xinflec = p_Shade[2];
	v->veg[SW_GRASS].tr_shade_effects.yinflec = p_Shade[3];
	v->veg[SW_GRASS].tr_shade_effects.range = p_Shade[4];
	v->veg[SW_GRASS].tr_shade_effects.slope = p_Shade[5];
	v->veg[SW_SHRUB].shade_scale = p_Shade[6];
	v->veg[SW_SHRUB].shade_deadmax = p_Shade[7];
	v->veg[SW_SHRUB].tr_shade_effects.xinflec = p_Shade[8];
	v->veg[SW_SHRUB].tr_shade_effects.yinflec = p_Shade[9];
	v->veg[SW_SHRUB].tr_shade_effects.range = p_Shade[10];
	v->veg[SW_SHRUB].tr_shade_effects.slope = p_Shade[11];
	v->veg[SW_TREES].shade_scale = p_Shade[12];
	v->veg[SW_TREES].shade_deadmax = p_Shade[13];
	v->veg[SW_TREES].tr_shade_effects.xinflec = p_Shade[14];
	v->veg[SW_TREES].tr_shade_effects.yinflec = p_Shade[15];
	v->veg[SW_TREES].tr_shade_effects.range = p_Shade[16];
	v->veg[SW_TREES].tr_shade_effects.slope = p_Shade[17];
	v->veg[SW_FORBS].shade_scale = p_Shade[18];
	v->veg[SW_FORBS].shade_deadmax = p_Shade[19];
	v->veg[SW_FORBS].tr_shade_effects.xinflec = p_Shade[20];
	v->veg[SW_FORBS].tr_shade_effects.yinflec = p_Shade[21];
	v->veg[SW_FORBS].tr_shade_effects.range = p_Shade[22];
	v->veg[SW_FORBS].tr_shade_effects.slope = p_Shade[23];

	PROTECT(Hydraulic_flag = GET_SLOT(SW_VPD, install(cVegProd_names[8])));
	PROTECT(Hydraulic = GET_SLOT(SW_VPD, install(cVegProd_names[9])));
	v->veg[SW_GRASS].flagHydraulicRedistribution = LOGICAL_POINTER(Hydraulic_flag)[0]; //Grass
	v->veg[SW_SHRUB].flagHydraulicRedistribution = LOGICAL_POINTER(Hydraulic_flag)[1]; //Shrub
	v->veg[SW_TREES].flagHydraulicRedistribution = LOGICAL_POINTER(Hydraulic_flag)[2]; //Tree
	v->veg[SW_FORBS].flagHydraulicRedistribution = LOGICAL_POINTER(Hydraulic_flag)[3]; //Forb
	v->veg[SW_GRASS].maxCondroot = REAL(Hydraulic)[0]; //Grass
	v->veg[SW_GRASS].swpMatric50 = REAL(Hydraulic)[1]; //Grass
	v->veg[SW_GRASS].shapeCond = REAL(Hydraulic)[2]; //Grass
	v->veg[SW_SHRUB].maxCondroot = REAL(Hydraulic)[3]; //Shrub
	v->veg[SW_SHRUB].swpMatric50 = REAL(Hydraulic)[4]; //Shrub
	v->veg[SW_SHRUB].shapeCond = REAL(Hydraulic)[5]; //Shrub
	v->veg[SW_TREES].maxCondroot = REAL(Hydraulic)[6]; //Tree
	v->veg[SW_TREES].swpMatric50 = REAL(Hydraulic)[7]; //Tree
	v->veg[SW_TREES].shapeCond = REAL(Hydraulic)[8]; //Tree
	v->veg[SW_FORBS].maxCondroot = REAL(Hydraulic)[9]; //Forb
	v->veg[SW_FORBS].swpMatric50 = REAL(Hydraulic)[10]; //Forb
	v->veg[SW_FORBS].shapeCond = REAL(Hydraulic)[11]; //Forb

	PROTECT(CSWP = GET_SLOT(SW_VPD, install(cVegProd_names[10])));
	v->veg[SW_GRASS].SWPcrit = -10 * REAL(CSWP)[0]; //Grass
	v->veg[SW_SHRUB].SWPcrit = -10 * REAL(CSWP)[1]; //Shrub
	v->veg[SW_TREES].SWPcrit = -10 * REAL(CSWP)[2]; //Tree
	v->veg[SW_FORBS].SWPcrit = -10 * REAL(CSWP)[3]; //Forb

	// getting critSoilWater for use with SWA and get_critical_rank()
	// critSoilWater goes tree, shrub, forb, grass
	SW_VegProd.critSoilWater[0] = REAL(CSWP)[2];
	SW_VegProd.critSoilWater[1] = REAL(CSWP)[1];
	SW_VegProd.critSoilWater[2] = REAL(CSWP)[3];
	SW_VegProd.critSoilWater[3] = REAL(CSWP)[0];

	get_critical_rank();

	PROTECT(MonthlyVeg = GET_SLOT(SW_VPD, install(cVegProd_names[11])));
	PROTECT(Grasslands = VECTOR_ELT(MonthlyVeg, SW_GRASS));
	p_Grasslands = REAL(Grasslands);
	for (i = 0; i < 12; i++) {
		v->veg[SW_GRASS].litter[i] = p_Grasslands[i + 12 * 0];
		v->veg[SW_GRASS].biomass[i] = p_Grasslands[i + 12 * 1];
		v->veg[SW_GRASS].pct_live[i] = p_Grasslands[i + 12 * 2];
		v->veg[SW_GRASS].lai_conv[i] = p_Grasslands[i + 12 * 3];
	}
	PROTECT(Shrublands = VECTOR_ELT(MonthlyVeg, SW_SHRUB));
	p_Shrublands = REAL(Shrublands);
	for (i = 0; i < 12; i++) {
		v->veg[SW_SHRUB].litter[i] = p_Shrublands[i + 12 * 0];
		v->veg[SW_SHRUB].biomass[i] = p_Shrublands[i + 12 * 1];
		v->veg[SW_SHRUB].pct_live[i] = p_Shrublands[i + 12 * 2];
		v->veg[SW_SHRUB].lai_conv[i] = p_Shrublands[i + 12 * 3];
	}
	PROTECT(Forest = VECTOR_ELT(MonthlyVeg, SW_TREES));
	p_Forest = REAL(Forest);
	for (i = 0; i < 12; i++) {
		v->veg[SW_TREES].litter[i] = p_Forest[i + 12 * 0];
		v->veg[SW_TREES].biomass[i] = p_Forest[i + 12 * 1];
		v->veg[SW_TREES].pct_live[i] = p_Forest[i + 12 * 2];
		v->veg[SW_TREES].lai_conv[i] = p_Forest[i + 12 * 3];
	}
	PROTECT(Forb = VECTOR_ELT(MonthlyVeg, SW_FORBS));
	p_Forb = REAL(Forb);
	for (i = 0; i < 12; i++) {
		v->veg[SW_FORBS].litter[i] = p_Forb[i + 12 * 0];
		v->veg[SW_FORBS].biomass[i] = p_Forb[i + 12 * 1];
		v->veg[SW_FORBS].pct_live[i] = p_Forb[i + 12 * 2];
		v->veg[SW_FORBS].lai_conv[i] = p_Forb[i + 12 * 3];
	}

	PROTECT(CO2Coefficients = GET_SLOT(SW_VPD, install(cVegProd_names[12])));
	v->veg[SW_GRASS].co2_bio_coeff1 = REAL(CO2Coefficients)[0];
	v->veg[SW_SHRUB].co2_bio_coeff1 = REAL(CO2Coefficients)[1];
	v->veg[SW_TREES].co2_bio_coeff1 = REAL(CO2Coefficients)[2];
	v->veg[SW_FORBS].co2_bio_coeff1 = REAL(CO2Coefficients)[3];
	v->veg[SW_GRASS].co2_bio_coeff2 = REAL(CO2Coefficients)[4];
	v->veg[SW_SHRUB].co2_bio_coeff2 = REAL(CO2Coefficients)[5];
	v->veg[SW_TREES].co2_bio_coeff2 = REAL(CO2Coefficients)[6];
	v->veg[SW_FORBS].co2_bio_coeff2 = REAL(CO2Coefficients)[7];
	v->veg[SW_GRASS].co2_wue_coeff1 = REAL(CO2Coefficients)[8];
	v->veg[SW_SHRUB].co2_wue_coeff1 = REAL(CO2Coefficients)[9];
	v->veg[SW_TREES].co2_wue_coeff1 = REAL(CO2Coefficients)[10];
	v->veg[SW_FORBS].co2_wue_coeff1 = REAL(CO2Coefficients)[11];
	v->veg[SW_GRASS].co2_wue_coeff2 = REAL(CO2Coefficients)[12];
	v->veg[SW_SHRUB].co2_wue_coeff2 = REAL(CO2Coefficients)[13];
	v->veg[SW_TREES].co2_wue_coeff2 = REAL(CO2Coefficients)[14];
	v->veg[SW_FORBS].co2_wue_coeff2 = REAL(CO2Coefficients)[15];


  SW_VPD_fix_cover();

	SW_VPD_init();

	if (EchoInits)
		_echo_VegProd();

	UNPROTECT(17);
}
Example #8
0
void SW_F_read(const char *s) {
	/* =================================================== */
	/* enter with the name of the first file to read for
	 * the filenames, or NULL.  If null, then read files.in
	 * or whichever filename was set previously. see init().
	 *
	 * 1/24/02 - replaced [re]alloc with StrDup()
	 *         - added facility for log-to-file. logfp depends
	 *             on having executed SW_F_read().
	 */

	FILE *f;
	int lineno = 0, fileno = 0;
	char buf[FILENAME_MAX];

	if (!isnull(s))
		init(s); /* init should be run by SW_F_Construct() */

	MyFileName = SW_F_name(eFirst);
	f = OpenFile(MyFileName, "r");

	while (GetALine(f, inbuf)) {

		switch (lineno) {
		case 5:
			strcpy(weather_prefix, inbuf);
			break;
		case 12:
			strcpy(output_prefix, inbuf);
			break;

		default:
			if (++fileno == SW_NFILES)
				break;

			if (!isnull(InFiles[fileno]))
				Mem_Free(InFiles[fileno]);
			strcpy(buf, _ProjDir);
			strcat(buf, inbuf);
			InFiles[fileno] = Str_Dup(buf);
		}

		lineno++;
	}

	if (fileno < eEndFile - 1) {
		CloseFile(&f);
		LogError(stdout, LOGFATAL, "Too few files (%d) in %s", fileno, MyFileName);
	}

	CloseFile(&f);

#if !defined(STEPWAT) && !defined(RSOILWAT)
	if (0 == strcmp(InFiles[eLog], "stdout")) {
		logfp = stdout;
	} else if (0 == strcmp(InFiles[eLog], "stderr")) {
		logfp = stderr;
	} else {
		logfp = OpenFile(SW_F_name(eLog), "w");
	}
#endif

}
void SW_MDL_read(void) {
/* =================================================== */
/*
 * 1/24/02 - added code for partial start and end years
 *
 * 28-Aug-03 (cwb) - N-S hemisphere flag logic changed.
 *    Can now specify first day of first year, last
 *    day of last year or hemisphere.  Code checks
 *    whether the first value is [NnSs] or number to
 *    make the decision.  If the value is numeric,
 *    the hemisphere is assumed to be N.  This method
 *    allows some degree of flexibility on the
 *    starting year
 */
   SW_MODEL *m = &SW_Model;
   FILE *f;
   int y, cnt;
   TimeInt d;
   char *p, enddyval[6];
   Bool fstartdy=FALSE, fenddy=FALSE, fhemi=FALSE;

   MyFileName = SW_F_name(eModel);
   f = OpenFile(MyFileName, "r");

   /* ----- beginning year */
   if (!GetALine(f, inbuf)) {
     LogError(logfp, LOGFATAL, "%s: No input.", MyFileName);
   }
   y = atoi(inbuf);
   if (y < 0) {
     LogError(logfp, LOGFATAL,
              "%s: Negative start year (%d)", MyFileName, y);
   }
   m->startyr = yearto4digit((TimeInt) y);

   /* ----- ending year */
   if (!GetALine(f, inbuf)) {
     LogError(logfp, LOGFATAL, "%s: Ending year not found.", MyFileName);
   }
   y = atoi(inbuf); assert( y > 0);
   if (y < 0) {
     LogError(logfp, LOGFATAL,
              "%s: Negative ending year (%d)", MyFileName, y);
   }
   m->endyr  = yearto4digit((TimeInt) y);
   if (m->endyr < m->startyr) {
     LogError(logfp, LOGFATAL, "%s: Start Year > End Year", MyFileName);
   }

   /* ----- Start checking for model time parameters */
   /*   input should be in order of startdy, enddy, hemisphere,
   		but if hemisphere occurs first, skip checking for the rest
   		and assume they're not there.
    */
   cnt=0;
   while (GetALine(f, inbuf) ) {
     cnt++;
     if (isalpha(*inbuf) && strcmp(inbuf, "end")) { /* get hemisphere */
       m->isnorth = (toupper((int)*inbuf) == 'N' );
       fhemi = TRUE;
       break;
     }
     switch (cnt) {
       case 1:
          m->startstart = atoi(inbuf);
          fstartdy = TRUE;
          break;
       case 2:
          p=inbuf; cnt=0;
          while( *p && cnt < 6) { enddyval[cnt++] = tolower((int)*(p++));}
          enddyval[cnt] = enddyval[5] = '\0';
          fenddy = TRUE;
          break;
       case 3:
          m->isnorth = (toupper((int)*inbuf) == 'N' );
          fhemi = TRUE;
          break;
       default: break;                     /* skip any extra lines */
     }
   }

   if (!(fstartdy && fenddy && fhemi) ) {
     sprintf(errstr, "\nNot found in %s:\n", MyFileName);
     if (!fstartdy) {
       strcat(errstr, "\tStart Day  - using 1\n");
       m->startstart = 1;
     }
     if (!fenddy) {
       strcat(errstr, "\tEnd Day    - using \"end\"\n");
       strcpy(enddyval, "end");
     }
     if (!fhemi) {
       strcat(errstr, "\tHemisphere - using \"N\"\n");
       m->isnorth = TRUE;
     }
     strcat(errstr, "Continuing.\n");
     LogError(logfp, LOGWARN, errstr);
   }

   m->startstart += ((m->isnorth) ? DAYFIRST_NORTH : DAYFIRST_SOUTH) -1;
	if ( strcmp(enddyval, "end")==0 ){
		m->endend = (m->isnorth) ? Time_get_lastdoy_y(m->endyr)  : DAYLAST_SOUTH;
	} else {
		d = atoi(enddyval);
		m->endend = (d < 365) ? d : Time_get_lastdoy_y(m->endyr); 
	}
     
   m->daymid     = (m->isnorth) ? DAYMID_NORTH   : DAYMID_SOUTH;
   CloseFile(&f);

}
Example #10
0
void SW_WTH_read(void) {
/* =================================================== */
SW_WEATHER *w = &SW_Weather;
const int nitems=18;
FILE *f;
int lineno=0, month, x;
RealF sppt,stmax,stmin;

MyFileName = SW_F_name(eWeather);
f = OpenFile(MyFileName, "r");

while( GetALine(f,inbuf) ) {

	switch(lineno) {
		case 0:  w->use_snow = itob(atoi(inbuf));         	break;
		case 1:  w->pct_snowdrift = atoi(inbuf);		  	break;
		case 2:  w->pct_runoff = atoi(inbuf);				break;
		case 3:  w->use_markov = itob(atoi(inbuf));       	break;
		case 4:  w->yr.first = YearTo4Digit(atoi(inbuf));	break;
		case 5:  w->days_in_runavg = atoi(inbuf);
					runavg_list = (RealD *) Mem_Calloc(w->days_in_runavg, sizeof(RealD), "SW_WTH_read()");
															break;
		default:
			if (lineno == 6 + MAX_MONTHS) break;

			x = sscanf(inbuf, "%d %f %f %f", &month, &sppt, &stmax, &stmin);
			if (x < 4) {
				LogError(logfp, LOGFATAL, "%s : Bad record %d.", MyFileName, lineno);
			}

			w->scale_precip[month-1] = sppt;
			w->scale_temp_max[month-1] = stmax;
			w->scale_temp_min[month-1] = stmin;
	}
	
	lineno++;
}

SW_WeatherPrefix(w->name_prefix);

CloseFile(&f);

if (lineno < nitems-1) {
	LogError(logfp, LOGFATAL, "%s : Too few input lines.",MyFileName);
}

w->yr.last = SW_Model.endyr;
w->yr.total = w->yr.last - w->yr.first +1;



if (w->use_markov) {
	SW_MKV_construct();

	if (!SW_MKV_read_prob()) {
		LogError(logfp, LOGFATAL, "%s: Markov weather requested but could not open %s",
		MyFileName, SW_F_name(eMarkovProb));
	}

	if (!SW_MKV_read_cov()) {
		LogError(logfp, LOGFATAL, "%s: Markov weather requested but could not open %s",
		MyFileName, SW_F_name(eMarkovCov));
	}

	} else if (SW_Model.startyr < w->yr.first) {
		LogError(logfp, LOGFATAL,
		"%s : Model year (%d) starts before weather files (%d)"
		" and use_Markov=FALSE.\nPlease synchronize the years"
		" or set up the Markov weather files",
		MyFileName, SW_Model.startyr, w->yr.first);
	}
/* else we assume weather files match model run years */

/* required for PET */
SW_SKY_read();
SW_SKY_init();

}