static Library.Biomass.Species.AuxParm <int> GetFirstActiveMonth(DateTime date, int timespan) { Library.Biomass.Species.AuxParm <int> FirstActiveMonth = new Library.Biomass.Species.AuxParm <int>(PlugIn.ModelCore.Species); foreach (ISpecies spc in PlugIn.ModelCore.Species) { DateTime DateCounter = date.AddYears(-5); bool FoundFirstActiveMonth = false; int SumFirstActiveMonth = 0; int c = 0; while (DateCounter.CompareTo(date.AddYears(5)) < 1) { if (DateCounter.Month == 1) { FoundFirstActiveMonth = false; } if (DateCounter.Year >= FirstYearOnFile && DateCounter.Year < LastYearOnFile) { if (isactive[DateCounter][spc] && FoundFirstActiveMonth == false) { FoundFirstActiveMonth = true; SumFirstActiveMonth += DateCounter.Month; c++; } } DateCounter = DateCounter.AddMonths(1); } FirstActiveMonth[spc] = (int)SumFirstActiveMonth / c; } return(FirstActiveMonth); }
public VarEcoregionSpecies(string label) { values = new Library.Biomass.Ecoregions.AuxParm <Library.Biomass.Species.AuxParm <T> >(PlugIn.modelCore.Ecoregions); //= new VarEcoregion<Library.Biomass.Species.AuxParm<T>>(label); this.label = label; foreach (IEcoregion eco in PlugIn.ModelCore.Ecoregions) { values[eco] = new Library.Biomass.Species.AuxParm <T>(PlugIn.modelCore.Species); } }
private static void SetEstablishmentMonths(IInputParameters Parameters, IEcoregion ecoregion) { DateTime date = DateRange[0]; int wettestmonth = -1; Library.Biomass.Species.AuxParm <int> firstactivemonthspc = null; Library.Biomass.Species.AuxParm <int> Firstpossibleestmonth = new Library.Biomass.Species.AuxParm <int>(PlugIn.modelCore.Species); Library.Biomass.Species.AuxParm <int> Lastpossibleestmonth = new Library.Biomass.Species.AuxParm <int>(PlugIn.modelCore.Species); while (date.CompareTo(DateRange[1]) < 0) { if (date.Month == 1 || wettestmonth < 0) { wettestmonth = GetWettestMonth(ecoregion, date.Year, 10); } if (date.Month == 1 || firstactivemonthspc == null) { firstactivemonthspc = new Library.Biomass.Species.AuxParm <int>(PlugIn.modelCore.Species); foreach (ISpecies spc in PlugIn.ModelCore.Species) { firstactivemonthspc[spc] = GetFirstActiveMonth(spc, Parameters, date, ecoregion, 10); Firstpossibleestmonth[spc] = Math.Min(wettestmonth - 1, firstactivemonthspc[spc]); Lastpossibleestmonth[spc] = Math.Max(wettestmonth + 1, firstactivemonthspc[spc] + 3); } } foreach (ISpecies spc in PlugIn.ModelCore.Species) { if (date.Month >= Firstpossibleestmonth[spc] && date.Month <= Lastpossibleestmonth[spc]) { possibleestmonth[ecoregion, spc, date] = true; } } date = date.AddMonths(1); } }
protected override IInsect Parse() { InputVar <string> landisData = new InputVar <string>("LandisData"); ReadVar(landisData); if (landisData.Value.Actual != "InsectDefoliator") { throw new InputValueException(landisData.Value.String, "The value is not \"{0}\"", "InsectDefoliator"); } Insect insect = new Insect(PlugIn.ModelCore.Species.Count); InputVar <string> insectName = new InputVar <string>("InsectName"); ReadVar(insectName); insect.Name = insectName.Value; InputVar <double> mD = new InputVar <double>("MeanDuration"); ReadVar(mD); insect.MeanDuration = mD.Value; InputVar <int> mTBO = new InputVar <int>("MeanTimeBetweenOutbreaks"); ReadVar(mTBO); insect.MeanTimeBetweenOutbreaks = mTBO.Value; InputVar <int> sdTBO = new InputVar <int>("StdDevTimeBetweenOutbreaks"); ReadVar(sdTBO); insect.StdDevTimeBetweenOutbreaks = sdTBO.Value; InputVar <int> nhs = new InputVar <int>("NeighborhoodSize"); ReadVar(nhs); insect.NeighborhoodDistance = nhs.Value; InputVar <double> ipsc = new InputVar <double>("InitialPatchShapeCalibrator"); ReadVar(ipsc); insect.InitialPatchShapeCalibrator = ipsc.Value; InputVar <double> ipnc = new InputVar <double>("InitialPatchOutbreakSensitivity"); ReadVar(ipnc); insect.InitialPatchOutbreakSensitivity = ipnc.Value; InputVar <DistributionType> ipdt = new InputVar <DistributionType>("InitialPatchDistribution"); ReadVar(ipdt); insect.InitialPatchDistr = ipdt.Value; InputVar <double> ipv1 = new InputVar <double>("InitialPatchValue1"); ReadVar(ipv1); insect.InitialPatchValue1 = ipv1.Value; InputVar <double> ipv2 = new InputVar <double>("InitialPatchValue2"); ReadVar(ipv2); insect.InitialPatchValue2 = ipv2.Value; //--------- Read In Species Table --------------------------------------- PlugIn.ModelCore.UI.WriteLine(" Begin parsing SPECIES table."); ReadName("SpeciesParameters"); InputVar <string> annMort = new InputVar <string>("MortalityEstimate"); ReadVar(annMort); insect.AnnMort = annMort.Value; InputVar <string> sppName = new InputVar <string>("Species"); InputVar <int> susc = new InputVar <int>("Species Susceptibility"); InputVar <double> grs = new InputVar <double>("Growth Reduction Slope"); InputVar <double> gri = new InputVar <double>("Growth Reduction Intercept"); InputVar <double> msl = new InputVar <double>("Mortality Slope"); InputVar <double> min = new InputVar <double>("Mortality Intercept"); Dictionary <string, int> lineNumbers = new Dictionary <string, int>(); const string Susceptiblities = "Susceptibilities"; Landis.Library.Biomass.Species.AuxParm <bool> AccountedFor = new Library.Biomass.Species.AuxParm <bool>(PlugIn.ModelCore.Species); while (!AtEndOfInput && CurrentName != Susceptiblities) { StringReader currentLine = new StringReader(CurrentLine); ReadValue(sppName, currentLine); ISpecies species = PlugIn.ModelCore.Species[sppName.Value.Actual]; if (species == null) { throw new InputValueException(sppName.Value.String, "{0} is not a species name.", sppName.Value.String); } int lineNumber; if (lineNumbers.TryGetValue(species.Name, out lineNumber)) { throw new InputValueException(sppName.Value.String, "The species {0} was previously used on line {1} for insect " + insect.Name, sppName.Value.String, lineNumber); } else { lineNumbers[species.Name] = LineNumber; } ReadValue(susc, currentLine); insect.Susceptibility[species] = Landis.Library.Biomass.CheckParms.CheckBiomassParm(susc.Value, 1, 4); ReadValue(grs, currentLine); insect.GrowthReduceSlope[species] = Landis.Library.Biomass.CheckParms.CheckBiomassParm(grs.Value, double.MinValue, 0); ReadValue(gri, currentLine); insect.GrowthReduceIntercept[species] = Landis.Library.Biomass.CheckParms.CheckBiomassParm(gri.Value, 0, double.MaxValue); ReadValue(msl, currentLine); insect.MortalitySlope[species] = Landis.Library.Biomass.CheckParms.CheckBiomassParm(msl.Value, 0, double.MaxValue); ReadValue(min, currentLine); insect.MortalityIntercept[species] = Landis.Library.Biomass.CheckParms.CheckBiomassParm(min.Value, 0, double.MaxValue); AccountedFor[species] = true; CheckNoDataAfter("the " + min.Name + " column", currentLine); GetNextLine(); } foreach (ISpecies species in PlugIn.ModelCore.Species) { if (AccountedFor[species] == false) { throw new System.Exception(species.Name + " is not parameterized for " + insect.Name); } } // Read table of Susceptibilities. // Susceptibilities are in decreasing order. ReadName(Susceptiblities); const string MapNames = "MapNames"; InputVar <byte> number = new InputVar <byte>("Susceptibility Number"); InputVar <DistributionType> dt = new InputVar <DistributionType>("Distribution"); InputVar <double> v1 = new InputVar <double>("Distribution Value 1"); InputVar <double> v2 = new InputVar <double>("Distribution Value 2"); byte previousNumber = 0; while (!AtEndOfInput && CurrentName != MapNames) { StringReader currentLine = new StringReader(CurrentLine); ISusceptible susceptible = new Susceptible(); insect.SusceptibleTable.Add(susceptible); ReadValue(number, currentLine); susceptible.Number = number.Value; // Check that the current severity's number is 1 less than // the previous number (numbers are must be in decreasing // order). if (number.Value.Actual != previousNumber + 1) { throw new InputValueException(number.Value.String, "Expected the severity number {0}", previousNumber + 1); } previousNumber = number.Value.Actual; IDistribution distribution = new Distribution(); ReadValue(dt, currentLine); distribution.Name = dt.Value; ReadValue(v1, currentLine); distribution.Value1 = v1.Value; ReadValue(v2, currentLine); distribution.Value2 = v2.Value; susceptible.Distribution_80 = distribution; //.GetComplete(); distribution = new Distribution(); ReadValue(dt, currentLine); distribution.Name = dt.Value; ReadValue(v1, currentLine); distribution.Value1 = v1.Value; ReadValue(v2, currentLine); distribution.Value2 = v2.Value; susceptible.Distribution_60 = distribution; //.GetComplete(); distribution = new Distribution(); ReadValue(dt, currentLine); distribution.Name = dt.Value; ReadValue(v1, currentLine); distribution.Value1 = v1.Value; ReadValue(v2, currentLine); distribution.Value2 = v2.Value; susceptible.Distribution_40 = distribution; //.GetComplete(); distribution = new Distribution(); ReadValue(dt, currentLine); distribution.Name = dt.Value; ReadValue(v1, currentLine); distribution.Value1 = v1.Value; ReadValue(v2, currentLine); distribution.Value2 = v2.Value; susceptible.Distribution_20 = distribution; //.GetComplete(); distribution = new Distribution(); ReadValue(dt, currentLine); distribution.Name = dt.Value; ReadValue(v1, currentLine); distribution.Value1 = v1.Value; ReadValue(v2, currentLine); distribution.Value2 = v2.Value; susceptible.Distribution_0 = distribution; //.GetComplete(); CheckNoDataAfter("the " + v2.Name + " column", currentLine); GetNextLine(); } if (insect.SusceptibleTable.Count == 0) { throw NewParseException("No susceptibilities defined."); } return(insect);//.GetComplete(); }
public static void Initialize(IInputParameters Parameters) { PlugIn.ModelCore.UI.WriteLine(" Reading climate date from " + Parameters.climateFileName); climateFileName = Parameters.climateFileName; SetDelimitor(); string[] Content = ReadContent(); SetFirstAndLastYearOnFile(Content); firstpossibleestmonth = new DateVar <int>("firstpossibleestmonth", FirstYearOnFile, LastYearOnFile); lastpossibleestmonth = new DateVar <int>("lastpossibleestmonth", FirstYearOnFile, LastYearOnFile); possibleestmonth = new DateVar <bool>("PossibleEstmonth", FirstYearOnFile, LastYearOnFile); tmax = new DateVar <float>("tmax", FirstYearOnFile, LastYearOnFile); tmin = new DateVar <float>("tmin", FirstYearOnFile, LastYearOnFile); co2 = new DateVar <float>("co2", FirstYearOnFile, LastYearOnFile); tday = new DateVar <float>("tday", FirstYearOnFile, LastYearOnFile); par0 = new DateVar <float>("par0", FirstYearOnFile, LastYearOnFile); tave = new DateVar <float>("tave", FirstYearOnFile, LastYearOnFile); prec = new DateVar <float>("prec", FirstYearOnFile, LastYearOnFile); vpd = new DateVar <float>("vpd", FirstYearOnFile, LastYearOnFile); dayspan = new DateVar <int>("dayspan", FirstYearOnFile, LastYearOnFile); treesareactive = new DateVar <bool>("treesareactive", FirstYearOnFile, LastYearOnFile); SetClimateFileHeaders(Content[0]); FolNCon = Parameters.FolNCon; BaseFolRespFrac = Parameters.BaseFolRespFrac; DVPD1 = Parameters.DVPD1; DVPD2 = Parameters.DVPD2; AmaxA = Parameters.AmaxA; AmaxB = Parameters.AmaxB; WUEConst = Parameters.WUEConst; RespQ10 = Parameters.RespQ10; PsnTOpt = Parameters.PsnTOpt; PsnTMin = Parameters.PsnTMin; firstactivemonthspc = new DateVar <Landis.Library.Biomass.Species.AuxParm <int> >("wettestmonth", FirstYearOnFile, LastYearOnFile); wettestmonth = new DateVar <int>("wettestmonth", FirstYearOnFile, LastYearOnFile); dtemppsn = new DateVar <Library.Biomass.Species.AuxParm <float> >("dtemppsn", FirstYearOnFile, LastYearOnFile); wue = new DateVar <Library.Biomass.Species.AuxParm <float> >("wue", FirstYearOnFile, LastYearOnFile); delamax = new DateVar <Library.Biomass.Species.AuxParm <float> >("delamax", FirstYearOnFile, LastYearOnFile); refnetpsn = new DateVar <Library.Biomass.Species.AuxParm <float> >("refnetpsn", FirstYearOnFile, LastYearOnFile); refresp = new DateVar <Library.Biomass.Species.AuxParm <float> >("refresp", FirstYearOnFile, LastYearOnFile); dTempResp = new DateVar <Library.Biomass.Species.AuxParm <float> >("dTempResp", FirstYearOnFile, LastYearOnFile); dTempRespDay = new DateVar <Library.Biomass.Species.AuxParm <float> >("dTempRespDay", FirstYearOnFile, LastYearOnFile); dTempRespNight = new DateVar <Library.Biomass.Species.AuxParm <float> >("dTempRespNight", FirstYearOnFile, LastYearOnFile); wue_co2_corr = new DateVar <Library.Biomass.Species.AuxParm <float> >("wue_co2_corr", FirstYearOnFile, LastYearOnFile); dvpd = new DateVar <Library.Biomass.Species.AuxParm <float> >("dvpd", FirstYearOnFile, LastYearOnFile); resp_folbase = new DateVar <Library.Biomass.Species.AuxParm <float> >("resp_folbase", FirstYearOnFile, LastYearOnFile); isactive = new DateVar <Library.Biomass.Species.AuxParm <bool> >("IsActive", FirstYearOnFile, LastYearOnFile); gdd = new DateVar <Library.Biomass.Species.AuxParm <float> >("gdd", FirstYearOnFile, LastYearOnFile); DateTime date = new DateTime(FirstYearOnFile, 1, 15); while (date.CompareTo(new DateTime(LastYearOnFile, 12, 15)) <= 0) { // GetLine checks if year/month according to the loop is equal to the date read in string[] terms = GetLine(date.Year, date.Month, Content).Split(delim); if (terms.Count() != ColumnCount) { throw new System.Exception("Unexpected number of columns in " + Parameters.climateFileName + " date = " + date); } if (int.Parse(terms[0]) != date.Year || int.Parse(terms[1]) != date.Month) { throw new System.Exception("Missing month " + date + " in climate file"); } try { tmax[date] = CheckInputValue(float.Parse(terms[TMaxCol]), -80, 80, "date = " + date); tmin[date] = CheckInputValue(float.Parse(terms[TMinCol]), -80, tmax[date], "date = " + date); co2[date] = CheckInputValue(float.Parse(terms[CO2Col]), 0, float.MaxValue, "date = " + date); par0[date] = CheckInputValue(float.Parse(terms[PAR0col]), 0, float.MaxValue, "date = " + date); prec[date] = CheckInputValue(float.Parse(terms[Preccol]), 0, float.MaxValue, "date = " + date); if (prec[date] < 0) { throw new System.Exception("Precipitation = " + prec + "\t" + date); } float hr = Calculate_hr(date.DayOfYear, Parameters.Latitude); float daylength = Calculate_DayLength(hr); float nightlength = Calculate_NightLength(hr); dayspan[date] = Calculate_DaySpan(date.Month); tave[date] = (float)0.5 * (tmin[date] + tmax[date]); tday[date] = (float)0.5 * (tmax[date] + tave[date]); vpd[date] = Calculate_VPD(Tday[date], TMin[date]); gdd[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); isactive[date] = new Library.Biomass.Species.AuxParm <bool>(PlugIn.ModelCore.Species); dtemppsn [date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); wue[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); delamax[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); refnetpsn[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); refresp[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); dTempResp [date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); dTempRespDay[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); dTempRespNight[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); wue_co2_corr[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); resp_folbase[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); gdd[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); dvpd[date] = new Library.Biomass.Species.AuxParm <float>(PlugIn.ModelCore.Species); foreach (ISpecies spc in PlugIn.ModelCore.Species) { float dGDD = Math.Max(0, (Tave[date] - PsnTMin[spc]) * DaySpan[date]); if (date.Month == 1) { gdd[date][spc] = dGDD; } else { DateTime last_month = new DateTime(date.Year, date.Month - 1, 15); gdd[date][spc] = gdd[last_month][spc] + dGDD; } if (tday[date] < PsnTMin[spc]) { IsActive[date][spc] = false; } else { IsActive[date][spc] = true; treesareactive[date] = true; } //dtemppsn[date][spc] = ParabolicPsnTempResponse(tday[date], PsnTOpt[spc], PsnTMin[spc]); dtemppsn[date][spc] = LinearPsnTempResponse(tday[date], PsnTOpt[spc], PsnTMin[spc]); dvpd[date][spc] = Math.Max(0, 1 - DVPD1[spc] * (float)Math.Pow(vpd[date], DVPD2[spc])); float cicaRatio = (-0.075f * FolNCon[spc]) + 0.875f; float ci350 = 350 * cicaRatio; float Arel350 = 1.22f * ((ci350 - 68) / (ci350 + 136)); float ciElev = co2[date] * cicaRatio; float ArelElev = 1.22f * ((ciElev - 68) / (ciElev + 136)); DelAmax[date][spc] = 1 + ((ArelElev - Arel350) / Arel350); // CO2 effect on photosynthesis // Calculate CO2 effect on conductance and set slope and intercept for A-gs relationship float Delgs = DelAmax[date][spc] / ((co2[date] - co2[date] * cicaRatio) / (350.0f - ci350)); wue[date][spc] = (WUEConst[spc] / vpd[date]) * (1 + 1 - Delgs); //DWUE determined from CO2 effects on conductance wue_co2_corr[date][spc] = wue[date][spc] / DelAmax[date][spc]; /* * wue_co2_corr[spc] = WUEConst[spc]; * Delgs = DelAmax / ((site.CaMo[mo] - CiElev) / (350.0 - Ci350)); * DWUE = 1.0 + (1 - Delgs); * gsSlope = (-1.1309 * DelAmax) + 1.9762; // used to determine ozone uptake * gsInt = (0.4656 * DelAmax) - 0.9701; */ // NETPSN float amax = DelAmax[date][spc] * (AmaxA[spc] + AmaxB[spc] * FolNCon[spc]); //Reference net Psn (lab conditions) RefNetPsn[date][spc] = Static.DaySpan[date] * (amax * dvpd[date][spc] * daylength * Constants.MC) / Constants.billion; dTempRespDay[date][spc] = ((float)Math.Pow(RespQ10[spc], (Tday[date] - PsnTOpt[spc]) / 10)); dTempRespNight[date][spc] = ((float)Math.Pow(RespQ10[spc], (TMin[date] - PsnTOpt[spc]) / 10)); dTempResp[date][spc] = (float)Math.Min(1.0, (dTempRespDay[date][spc] * daylength + dTempRespNight[date][spc] * nightlength) / ((float)daylength + (float)nightlength)); // gC/day RefResp[date][spc] = BaseFolRespFrac[spc] * Static.DaySpan[date] * (amax * daylength * Constants.MC) / Constants.billion; } date = date.AddMonths(1); } catch { throw new System.Exception("Cannot find climate date for (year,month) " + date.Year.ToString() + " " + date.Month.ToString()); } } while (date.CompareTo(new DateTime(LastYearOnFile, 12, 15)) < 0) { if (Parameters.EstMoistureSensitivity != null) { wettestmonth[date] = GetWettestMonth(date, 10); } if (Parameters.EstRadSensitivity != null) { firstactivemonthspc[date] = GetFirstActiveMonth(date, 10); } foreach (ISpecies spc in PlugIn.ModelCore.Species) { int Firstpossibleestmonth = Math.Min(wettestmonth[date] - 1, firstactivemonthspc[date][spc]); int Lastpossibleestmonth = Math.Max(wettestmonth[date] + 1, firstactivemonthspc[date][spc] + 3); if (date.Month >= Firstpossibleestmonth && date.Month <= Lastpossibleestmonth) { possibleestmonth[date] = true; } } date = date.AddMonths(1); } }