Ejemplo n.º 1
0
        //Daily will not come to here. the average in daily is calculated in the AnnualClimate_Daily
        private ClimateRecord[] AnnualClimate_AvgMonth(IEcoregion ecoregion, double latitude)
        {
            Dictionary <int, ClimateRecord[][]> timestepData;

            if (this.climatePhase == Climate.Phase.Future_Climate)
            {
                timestepData = Climate.Future_AllData;
            }
            else
            {
                timestepData = Climate.Spinup_AllData;
            }

            var monthlyData = new ClimateRecord[12];      // for returning summary data in ClimateRecord form.

            var yearCount = timestepData.Count;

            for (int mo = 0; mo < 12; mo++)
            {
                var monthlyMinTemp       = 0.0;
                var monthlyMaxTemp       = 0.0;
                var monthlyVarTemp       = 0.0;
                var monthlyVarPpt        = 0.0;
                var monthlyPrecip        = 0.0;
                var monthlyPAR           = 0.0;
                var monthlyWindDirection = 0.0;
                var monthlyWindSpeed     = 0.0;
                var monthlyNDeposition   = 0.0;

                foreach (var yearMonthlyRecords in timestepData.Values)
                {
                    monthlyMinTemp       += yearMonthlyRecords[ecoregion.Index][mo].AvgMinTemp;
                    monthlyMaxTemp       += yearMonthlyRecords[ecoregion.Index][mo].AvgMaxTemp;
                    monthlyVarTemp       += yearMonthlyRecords[ecoregion.Index][mo].AvgVarTemp;
                    monthlyVarPpt        += yearMonthlyRecords[ecoregion.Index][mo].AvgVarPpt;
                    monthlyPrecip        += yearMonthlyRecords[ecoregion.Index][mo].AvgPpt;
                    monthlyPAR           += yearMonthlyRecords[ecoregion.Index][mo].AvgPAR;
                    monthlyWindDirection += yearMonthlyRecords[ecoregion.Index][mo].AvgWindDirection;
                    monthlyWindSpeed     += yearMonthlyRecords[ecoregion.Index][mo].AvgWindSpeed;
                    monthlyNDeposition   += yearMonthlyRecords[ecoregion.Index][mo].AvgNDeposition;
                }

                monthlyData[mo] = new ClimateRecord();
                if (yearCount > 0)
                {
                    monthlyData[mo].AvgMinTemp       = monthlyMinTemp / yearCount;
                    monthlyData[mo].AvgMaxTemp       = monthlyMaxTemp / yearCount;
                    monthlyData[mo].AvgVarTemp       = monthlyVarTemp / yearCount;
                    monthlyData[mo].StdDevTemp       = Math.Sqrt(monthlyVarTemp / yearCount);
                    monthlyData[mo].AvgVarPpt        = monthlyVarPpt / yearCount;
                    monthlyData[mo].AvgPpt           = monthlyPrecip / yearCount;
                    monthlyData[mo].StdDevPpt        = Math.Sqrt(monthlyPrecip / yearCount);
                    monthlyData[mo].AvgPAR           = monthlyPAR / yearCount;
                    monthlyData[mo].AvgWindDirection = monthlyWindDirection / yearCount;
                    monthlyData[mo].AvgWindSpeed     = monthlyWindSpeed / yearCount;
                    monthlyData[mo].AvgNDeposition   = monthlyNDeposition / yearCount;
                }
            }
            return(monthlyData);
        }
        // Not used?
        private void CalculateMonthlyData_NoVariance(IEcoregion ecoregion, ClimateRecord[][] timestepData, int actualYear, double latitude)
        {
            ClimateRecord[] ecoClimate = new ClimateRecord[12];

            this.Year = actualYear;
            this.TotalAnnualPrecip = 0.0;

            for (int mo = 0; mo < 12; mo++)
            {
                //Climate.TextLog.WriteLine("  Calculating Monthly Climate (No Variance):  Yr={0}, month={1}, Eco={2}, Phase={3}.", actualYear, mo, ecoregion.Name, this.climatePhase);
                ecoClimate[mo] = timestepData[ecoregion.Index][mo];

                double MonthlyAvgTemp = (ecoClimate[mo].AvgMinTemp + ecoClimate[mo].AvgMaxTemp) / 2.0;
                double MonthlyAvgRH   = (ecoClimate[mo].AvgMinRH + ecoClimate[mo].AvgMaxRH) / 2.0;

                this.MonthlyTemp[mo]               = MonthlyAvgTemp;
                this.MonthlyMinTemp[mo]            = ecoClimate[mo].AvgMinTemp;
                this.MonthlyMaxTemp[mo]            = ecoClimate[mo].AvgMaxTemp;
                this.MonthlyPrecip[mo]             = Math.Max(0.0, ecoClimate[mo].AvgPpt);
                this.MonthlyWindDirection[mo]      = ecoClimate[mo].AvgWindDirection;
                this.MonthlyWindSpeed[mo]          = ecoClimate[mo].AvgWindSpeed;
                this.MonthlyNDeposition[mo]        = ecoClimate[mo].AvgNDeposition;
                this.MonthlyRH[mo]                 = MonthlyAvgRH;
                this.MonthlyMinRH[mo]              = ecoClimate[mo].AvgMinRH;
                this.MonthlyMaxRH[mo]              = ecoClimate[mo].AvgMaxRH;
                this.MonthlySpecificHumidity[mo]   = ecoClimate[mo].AvgSpecificHumidity;
                this.MonthlyPAR[mo]                = ecoClimate[mo].AvgPAR;
                this.MonthlyOzone[mo]              = ecoClimate[mo].AvgOzone;
                this.MonthlyCO2[mo]                = ecoClimate[mo].AvgOzone;
                this.MonthlyShortWaveRadiation[mo] = ecoClimate[mo].AvgShortWaveRadiation;
                this.MonthlyTemperature[mo]        = ecoClimate[mo].Temp;
                this.MonthlyFWI[mo]                = ecoClimate[mo].AvgFWI;

                this.TotalAnnualPrecip += this.MonthlyPrecip[mo];

                if (this.MonthlyPrecip[mo] < 0)
                {
                    this.MonthlyPrecip[mo] = 0;
                }

                double hr = CalculateDayLength(mo, latitude);
                this.MonthlyDayLength[mo]   = (60.0 * 60.0 * hr);                // seconds of daylight/day
                this.MonthlyNightLength[mo] = (60.0 * 60.0 * (24 - hr));         // seconds of nighttime/day
            }

            this.MeanAnnualTemperature = CalculateMeanAnnualTemp(actualYear);
        }
Ejemplo n.º 3
0
        public static void Convert_USGS_to_ClimateData_FillAlldata(TemporalGranularity timeStep, string climateFile, string climateFileFormat, Climate.Phase climatePhase)
        {
            // **
            // John McNabb:  new parsing code
            List <int> yearKeys;
            List <List <ClimateRecord>[]> climateRecords;

            Convert_USGS_to_ClimateData2(timeStep, climateFile, climateFileFormat, out yearKeys, out climateRecords);

            Dictionary <int, ClimateRecord[][]> allDataRef = null; //this dictionary is filled out either by Daily data or Monthly

            if (climatePhase == Climate.Phase.Future_Climate)
            {
                allDataRef = Climate.Future_AllData;
            }

            if (climatePhase == Climate.Phase.SpinUp_Climate)
            {
                allDataRef = Climate.Spinup_AllData;
            }

            if (allDataRef == null)
            {
                allDataRef = new Dictionary <int, ClimateRecord[][]>();
            }
            else
            {
                allDataRef.Clear();
            }

            for (var i = 0; i < yearKeys.Count; ++i)
            {
                var ecoRecords = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];
                allDataRef[yearKeys[i]] = ecoRecords;

                for (var j = 0; j < Climate.ModelCore.Ecoregions.Count; ++j)
                {
                    // convert the parsed climateRecords for this year from List<ClimateRecord>[] to ClimateRecord[][]
                    ecoRecords[j] = climateRecords[i][j].ToArray();
                }
            }
        }
        //This method is not currently being called.  If it is later used, it will need to be checked to make sure it's working properly.
        private void CalculateMonthlyData_AddVariance(IEcoregion ecoregion, ClimateRecord[][] timestepData, int actualYear, double latitude)
        {
            ClimateRecord[] ecoClimate = new ClimateRecord[12];

            this.Year = actualYear;
            this.TotalAnnualPrecip = 0.0;

            //if(timestepData[ecoregion.Index].  ADD MONTH CHECK HERE.

            for (int mo = 0; mo < 12; mo++)
            {
                ecoClimate[mo] = timestepData[ecoregion.Index][mo];

                double MonthlyAvgTemp = (ecoClimate[mo].AvgMinTemp + ecoClimate[mo].AvgMaxTemp) / 2.0;

                double standardDeviation = ecoClimate[mo].StdDevTemp * (Climate.ModelCore.GenerateUniform() * 2.0 - 1.0);

                this.MonthlyTemp[mo]          = MonthlyAvgTemp + standardDeviation;
                this.MonthlyMinTemp[mo]       = ecoClimate[mo].AvgMinTemp + standardDeviation;
                this.MonthlyMaxTemp[mo]       = ecoClimate[mo].AvgMaxTemp + standardDeviation;
                this.MonthlyPrecip[mo]        = Math.Max(0.0, ecoClimate[mo].AvgPpt + (ecoClimate[mo].StdDevPpt * (Climate.ModelCore.GenerateUniform() * 2.0 - 1.0)));
                this.MonthlyPAR[mo]           = ecoClimate[mo].AvgPAR;
                this.MonthlyWindDirection[mo] = ecoClimate[mo].AvgWindDirection;
                this.MonthlyWindSpeed[mo]     = ecoClimate[mo].AvgWindSpeed;
                this.MonthlyNDeposition[mo]   = ecoClimate[mo].AvgNDeposition;
                this.MonthlyFWI[mo]           = ecoClimate[mo].AvgFWI;

                this.TotalAnnualPrecip += this.MonthlyPrecip[mo];

                if (this.MonthlyPrecip[mo] < 0)
                {
                    this.MonthlyPrecip[mo] = 0;
                }

                double hr = CalculateDayLength(mo, latitude);
                this.MonthlyDayLength[mo]   = (60.0 * 60.0 * hr);                // seconds of daylight/day
                this.MonthlyNightLength[mo] = (60.0 * 60.0 * (24 - hr));         // seconds of nighttime/day
            }

            this.MeanAnnualTemperature = CalculateMeanAnnualTemp(actualYear);
        }
Ejemplo n.º 5
0
        private ClimateRecord[] AnnualClimate_AvgDaily(IEcoregion ecoregion, double latitude)
        {
            var dailyData = new ClimateRecord[365];     // year-averaged data are always of length 365, even if averaging includes leapyears.

            Dictionary <int, ClimateRecord[][]> timestepData;

            if (this.climatePhase == Climate.Phase.Future_Climate)
            {
                timestepData = Climate.Future_AllData;
            }
            else
            {
                timestepData = Climate.Spinup_AllData;
            }

            var yearCount = timestepData.Count;

            var feb28DayIndex = 31 + 28 - 1;  // zero-based so subtract 1

            for (var d = 0; d < 365; ++d)
            {
                var dailyMinTemp            = 0.0;
                var dailyMaxTemp            = 0.0;
                var dailyVarTemp            = 0.0;
                var dailyVarPpt             = 0.0;
                var dailyPrecip             = 0.0;
                var dailyWindDirection      = 0.0;
                var dailyWindSpeed          = 0.0;
                var dailyNDeposition        = 0.0;
                var dailyCO2                = 0.0;
                var dailyMinRH              = 0.0;
                var dailyMaxRH              = 0.0;
                var dailySpecificHumidity   = 0.0;
                var dailyPET                = 0.0;
                var dailyPAR                = 0.0;
                var dailyOzone              = 0.0;
                var dailyShortWaveRadiation = 0.0;
                var dailyFWI                = 0.0;

                // loop over years
                int dIndex;

                foreach (var yearDailyRecords in timestepData.Values)
                {
                    var yearRecords = yearDailyRecords[ecoregion.Index];

                    if (yearRecords.Length == 366 && d == feb28DayIndex)
                    {
                        // average data for both Feb28 and Feb29
                        dailyMinTemp            += (yearRecords[d].AvgMinTemp + yearRecords[d + 1].AvgMinTemp) / 2.0;
                        dailyMaxTemp            += (yearRecords[d].AvgMaxTemp + yearRecords[d + 1].AvgMaxTemp) / 2.0;
                        dailyVarTemp            += (yearRecords[d].VarTemp + yearRecords[d + 1].VarTemp) / 2.0;
                        dailyVarPpt             += (yearRecords[d].VarPpt + yearRecords[d + 1].VarPpt) / 2.0;
                        dailyPrecip             += (yearRecords[d].AvgPpt + yearRecords[d + 1].AvgPpt) / 2.0;
                        dailyWindDirection      += (yearRecords[d].AvgWindDirection + yearRecords[d + 1].AvgWindDirection) / 2.0;
                        dailyWindSpeed          += (yearRecords[d].AvgWindSpeed + yearRecords[d + 1].AvgWindSpeed) / 2.0;
                        dailyNDeposition        += (yearRecords[d].AvgNDeposition + yearRecords[d + 1].AvgNDeposition) / 2.0;
                        dailyCO2                += (yearRecords[d].AvgCO2 + yearRecords[d + 1].AvgCO2) / 2.0;
                        dailyMinRH              += (yearRecords[d].AvgMinRH + yearRecords[d + 1].AvgMinRH) / 2.0;
                        dailyMaxRH              += (yearRecords[d].AvgMaxRH + yearRecords[d + 1].AvgMaxRH) / 2.0;
                        dailySpecificHumidity   += (yearRecords[d].AvgSpecificHumidity + yearRecords[d + 1].AvgSpecificHumidity) / 2.0;
                        dailyPET                += (yearRecords[d].AvgPET + yearRecords[d + 1].AvgPET) / 2.0;
                        dailyPAR                += (yearRecords[d].AvgPAR + yearRecords[d + 1].AvgPAR) / 2.0;
                        dailyOzone              += (yearRecords[d].AvgOzone + yearRecords[d + 1].AvgOzone) / 2.0;
                        dailyShortWaveRadiation += (yearRecords[d].AvgShortWaveRadiation + yearRecords[d + 1].AvgShortWaveRadiation) / 2.0;
                        dailyFWI                += (yearRecords[d].AvgFWI + yearRecords[d + 1].AvgFWI) / 2.0;
                    }
                    else
                    {
                        // if it is a leapyear and the day is after Feb28, add one to the day index
                        dIndex = (yearRecords.Length == 366 && d > feb28DayIndex) ? d + 1 : d;

                        dailyMinTemp          += yearRecords[dIndex].AvgMinTemp;
                        dailyMaxTemp          += yearRecords[dIndex].AvgMaxTemp;
                        dailyVarTemp          += yearRecords[dIndex].VarTemp;
                        dailyVarPpt           += yearRecords[dIndex].VarPpt;
                        dailyPrecip           += yearRecords[dIndex].AvgPpt;
                        dailyWindDirection    += yearRecords[dIndex].AvgWindDirection;
                        dailyWindSpeed        += yearRecords[dIndex].AvgWindSpeed;
                        dailyNDeposition      += yearRecords[dIndex].AvgNDeposition;
                        dailyMinRH            += yearRecords[dIndex].AvgMinRH;
                        dailyMaxRH            += yearRecords[dIndex].AvgMaxRH;
                        dailySpecificHumidity += yearRecords[dIndex].AvgSpecificHumidity;
                        dailyPET   += yearRecords[dIndex].AvgPET;
                        dailyPAR   += yearRecords[dIndex].AvgPAR;
                        dailyCO2   += yearRecords[dIndex].AvgCO2;
                        dailyOzone += yearRecords[dIndex].AvgOzone;
                        dailyShortWaveRadiation += yearRecords[dIndex].AvgShortWaveRadiation;
                        dailyFWI += yearRecords[dIndex].AvgFWI;
                    }
                }

                dailyData[d] = new ClimateRecord();
                if (yearCount > 0)
                {
                    dailyData[d].AvgMinTemp            = dailyMinTemp / yearCount;
                    dailyData[d].AvgMaxTemp            = dailyMaxTemp / yearCount;
                    dailyData[d].VarTemp               = dailyVarTemp / yearCount;
                    dailyData[d].StdDevTemp            = Math.Sqrt(dailyVarTemp / yearCount);
                    dailyData[d].VarPpt                = dailyVarPpt / yearCount;
                    dailyData[d].AvgPpt                = dailyPrecip / yearCount;
                    dailyData[d].StdDevPpt             = Math.Sqrt(dailyPrecip / yearCount);
                    dailyData[d].AvgWindDirection      = dailyWindDirection / yearCount;
                    dailyData[d].AvgWindSpeed          = dailyWindSpeed / yearCount;
                    dailyData[d].AvgNDeposition        = dailyNDeposition / yearCount;
                    dailyData[d].AvgCO2                = dailyCO2 / yearCount;
                    dailyData[d].AvgMinRH              = dailyMinRH / yearCount;
                    dailyData[d].AvgMaxRH              = dailyMaxRH / yearCount;
                    dailyData[d].AvgSpecificHumidity   = dailySpecificHumidity / yearCount;
                    dailyData[d].AvgPET                = dailyPET / yearCount;
                    dailyData[d].AvgPAR                = dailyPAR / yearCount;
                    dailyData[d].AvgOzone              = dailyOzone / yearCount;
                    dailyData[d].AvgShortWaveRadiation = dailyShortWaveRadiation / yearCount;
                    dailyData[d].AvgFWI                = dailyFWI / yearCount;
                }
            }

            return(dailyData);
        }
        private void CalculateMonthlyData_NoVariance(IEcoregion ecoregion, ClimateRecord[][] timestepData, int actualYear, double latitude)
        {
            ClimateRecord[] ecoClimate = new ClimateRecord[12];

            this.Year = actualYear;
            this.TotalAnnualPrecip = 0.0;

            for (int mo = 0; mo < 12; mo++)
            {
                //Climate.ModelCore.UI.WriteLine("  Calculating Monthly Climate (No Variance):  Yr={0}, month={1}, Eco={2}, Phase={3}.", actualYear, mo, ecoregion.Name, this.climatePhase);
                ecoClimate[mo] = timestepData[ecoregion.Index][mo];

                double MonthlyAvgTemp = (ecoClimate[mo].AvgMinTemp + ecoClimate[mo].AvgMaxTemp) / 2.0;

                this.MonthlyTemp[mo] = MonthlyAvgTemp;
                this.MonthlyMinTemp[mo] = ecoClimate[mo].AvgMinTemp;
                this.MonthlyMaxTemp[mo] = ecoClimate[mo].AvgMaxTemp;
                this.MonthlyPrecip[mo] = Math.Max(0.0, ecoClimate[mo].AvgPpt);
                this.MonthlyPAR[mo] = ecoClimate[mo].AvgPAR;
                this.MonthlyWindDirection[mo] = ecoClimate[mo].AvgWindDirection;
                this.MonthlyWindSpeed[mo] = ecoClimate[mo].AvgWindSpeed;
                this.MonthlyNDeposition[mo] = ecoClimate[mo].AvgNDeposition;

                this.TotalAnnualPrecip += this.MonthlyPrecip[mo];

                if (this.MonthlyPrecip[mo] < 0)
                    this.MonthlyPrecip[mo] = 0;

                double hr = CalculateDayLength(mo, latitude);
                this.MonthlyDayLength[mo] = (60.0 * 60.0 * hr);                  // seconds of daylight/day
                this.MonthlyNightLength[mo] = (60.0 * 60.0 * (24 - hr));         // seconds of nighttime/day

            }

            this.MeanAnnualTemperature = CalculateMeanAnnualTemp(actualYear);
        }
        //public double[] DailyRH = new double[366];
        //public double[] DailyWindSpeed = new double[366];
        //public double[] DailyNdeposition = new double[366];
        private ClimateRecord[] AnnualClimate_AvgDaily(IEcoregion ecoregion, double latitude)
        {
            var dailyData = new ClimateRecord[365];     // year-averaged data are always of length 365, even if averaging includes leapyears.

            Dictionary<int, ClimateRecord[][]> timestepData;

            if (this.climatePhase == Climate.Phase.Future_Climate)
                timestepData = Climate.Future_AllData;
            else
                timestepData = Climate.Spinup_AllData;

            var yearCount = timestepData.Count;

            var feb28DayIndex = 31 + 28 - 1;  // zero-based so subtract 1

            for (var d = 0; d < 365; ++d)
            {
                var dailyMinTemp = 0.0;
                var dailyMaxTemp = 0.0;
                var dailyVarTemp = 0.0;
                var dailyVarPpt = 0.0;
                var dailyPrecip = 0.0;
                var dailyPAR = 0.0;
                var dailyWindDirection = 0.0;
                var dailyWindSpeed = 0.0;
                var dailyNDeposition = 0.0;
                var dailyCO2 = 0.0;
                var dailyRH = 0.0;

                // loop over years
                int dIndex;

                foreach (var yearDailyRecords in timestepData.Values)
                {
                    var yearRecords = yearDailyRecords[ecoregion.Index];

                    if (yearRecords.Length == 366 && d == feb28DayIndex)
                    {
                        // average data for both Feb28 and Feb29
                        dailyMinTemp += (yearRecords[d].AvgMinTemp + yearRecords[d + 1].AvgMinTemp) / 2.0;
                        dailyMaxTemp += (yearRecords[d].AvgMaxTemp + yearRecords[d + 1].AvgMaxTemp) / 2.0;
                        dailyVarTemp += (yearRecords[d].AvgVarTemp + yearRecords[d + 1].AvgVarTemp) / 2.0;
                        dailyVarPpt += (yearRecords[d].AvgVarPpt + yearRecords[d + 1].AvgVarPpt) / 2.0;
                        dailyPrecip += (yearRecords[d].AvgPpt + yearRecords[d + 1].AvgPpt) / 2.0;
                        dailyPAR += (yearRecords[d].AvgPAR + yearRecords[d + 1].AvgPAR) / 2.0;
                        dailyWindDirection += (yearRecords[d].AvgWindDirection + yearRecords[d + 1].AvgWindDirection) / 2.0;
                        dailyWindSpeed += (yearRecords[d].AvgWindSpeed + yearRecords[d + 1].AvgWindSpeed) / 2.0;
                        dailyNDeposition += (yearRecords[d].AvgNDeposition + yearRecords[d + 1].AvgNDeposition) / 2.0;
                        dailyCO2 += (yearRecords[d].AvgCO2 + yearRecords[d + 1].AvgCO2) / 2.0;
                        dailyRH += (yearRecords[d].AvgRH + yearRecords[d + 1].AvgRH) / 2.0;
                    }
                    else
                    {
                        // if it is a leapyear and the day is after Feb28, add one to the day index
                        dIndex = (yearRecords.Length == 366 && d > feb28DayIndex) ? d + 1 : d;

                        dailyMinTemp += yearRecords[dIndex].AvgMinTemp;
                        dailyMaxTemp += yearRecords[dIndex].AvgMaxTemp;
                        dailyVarTemp += yearRecords[dIndex].AvgVarTemp;
                        dailyVarPpt += yearRecords[dIndex].AvgVarPpt;
                        dailyPrecip += yearRecords[dIndex].AvgPpt;
                        dailyPAR += yearRecords[dIndex].AvgPAR;
                        dailyWindDirection += yearRecords[dIndex].AvgWindDirection;
                        dailyWindSpeed += yearRecords[dIndex].AvgWindSpeed;
                        dailyNDeposition += yearRecords[dIndex].AvgNDeposition;
                        dailyRH += yearRecords[dIndex].AvgRH;

                    }
                }

                dailyData[d] = new ClimateRecord();
                if (yearCount > 0)
                {
                    dailyData[d].AvgMinTemp = dailyMinTemp / yearCount;
                    dailyData[d].AvgMaxTemp = dailyMaxTemp / yearCount;
                    dailyData[d].AvgVarTemp = dailyVarTemp / yearCount;
                    dailyData[d].StdDevTemp = Math.Sqrt(dailyVarTemp / yearCount);
                    dailyData[d].AvgVarPpt = dailyVarPpt / yearCount;
                    dailyData[d].AvgPpt = dailyPrecip / yearCount;
                    dailyData[d].StdDevPpt = Math.Sqrt(dailyPrecip / yearCount);
                    dailyData[d].AvgPAR = dailyPAR / yearCount;
                    dailyData[d].AvgWindDirection = dailyWindDirection / yearCount;
                    dailyData[d].AvgWindSpeed = dailyWindSpeed / yearCount;
                    dailyData[d].AvgNDeposition = dailyNDeposition / yearCount;
                    dailyData[d].AvgCO2 = dailyCO2 / yearCount;
                    dailyData[d].AvgRH = dailyRH / yearCount;
                }
            }

            return dailyData;
        }
        private ClimateRecord[] AnnualClimate_From_AnnualClimate_Daily(IEcoregion ecoregion, double latitude, Climate.Phase spinupOrfuture, int timeStep, int timeStepIndex)
        {
            var monthlyData = new ClimateRecord[12];

            int nDays;
            int dayOfYear = 0;
            AnnualClimate_Daily annDaily = new AnnualClimate_Daily(ecoregion, latitude, spinupOrfuture, timeStep, timeStepIndex); //for the same timeStep

            // if annDaily data come from averaging over years, it will always have 365 days, so I can't use the DaysInMonth() method based on the actualYear
            var daysInMonth = annDaily.DailyDataIsLeapYear ? new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } : new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

            if (spinupOrfuture == Climate.Phase.Future_Climate)
            {
                Climate.Future_DailyData[timeStep][ecoregion.Index] = annDaily;
            }
            else
            {
                Climate.Spinup_DailyData[timeStep][ecoregion.Index] = annDaily;
            }

            for (int mo = 0; mo < 12; mo++)
            {
                var monthlyMinTemp       = 0.0;
                var monthlyMaxTemp       = 0.0;
                var monthlyVarTemp       = 0.0;
                var monthlyPptVarTemp    = 0.0;
                var monthlyPrecip        = 0.0;
                var monthlyPAR           = 0.0;
                var monthlyWindDirection = 0.0;
                var monthlyWindSpeed     = 0.0;
                var monthlyNDeposition   = 0.0;
                var monthlyCO2           = 0.0;
                //var monthlyMinRH = 0.0;
                //var monthlyMaxRH = 0.0;
                var monthlyFWI = 0.0;

                nDays = daysInMonth[mo];
                for (int d = 0; d < nDays; d++)
                {
                    monthlyMinTemp       += annDaily.DailyMinTemp[dayOfYear];
                    monthlyMaxTemp       += annDaily.DailyMaxTemp[dayOfYear];
                    monthlyVarTemp       += annDaily.DailyVarTemp[dayOfYear];
                    monthlyPptVarTemp    += annDaily.DailyVarPpt[dayOfYear];
                    monthlyPrecip        += annDaily.DailyPrecip[dayOfYear];
                    monthlyPAR           += annDaily.DailyPAR[dayOfYear];
                    monthlyWindDirection += annDaily.DailyWindDirection[dayOfYear];
                    monthlyWindSpeed     += annDaily.DailyWindSpeed[dayOfYear];
                    monthlyNDeposition   += annDaily.DailyNDeposition[dayOfYear];
                    monthlyCO2           += annDaily.DailyCO2[dayOfYear];
                    //monthlyMaxRH += annDaily.DailyMaxRH[dayOfYear];
                    //monthlyMinRH += annDaily.DailyMinRH[dayOfYear];
                    monthlyFWI += annDaily.DailyFireWeatherIndex[dayOfYear];

                    dayOfYear++;
                }

                monthlyData[mo] = new ClimateRecord();

                monthlyData[mo].AvgMinTemp       = monthlyMinTemp / nDays;
                monthlyData[mo].AvgMaxTemp       = monthlyMaxTemp / nDays;
                monthlyData[mo].AvgVarTemp       = monthlyVarTemp / nDays;
                monthlyData[mo].StdDevTemp       = Math.Sqrt(monthlyVarTemp / nDays);
                monthlyData[mo].AvgVarPpt        = monthlyPptVarTemp / nDays;
                monthlyData[mo].AvgPpt           = monthlyPrecip;
                monthlyData[mo].StdDevPpt        = Math.Sqrt(monthlyPrecip / nDays);
                monthlyData[mo].AvgPAR           = monthlyPAR / nDays;
                monthlyData[mo].AvgWindDirection = monthlyWindDirection / nDays;
                monthlyData[mo].AvgWindSpeed     = monthlyWindSpeed / nDays;
                monthlyData[mo].AvgNDeposition   = monthlyNDeposition / nDays;
                monthlyData[mo].AvgCO2           = monthlyCO2 / nDays;
                //monthlyData[mo].AvgMinRH = monthlyMinRH / nDays;
                //monthlyData[mo].AvgMaxRH = monthlyMaxRH / nDays;
                monthlyData[mo].AvgFWI = monthlyFWI / nDays;
            }

            return(monthlyData);
        }
        //Daily will not come to here. the average in daily is calculated in the AnnualClimate_Daily
        private ClimateRecord[] AnnualClimate_AvgMonth(IEcoregion ecoregion, double latitude)
        {
            Dictionary<int, ClimateRecord[][]> timestepData;

            if (this.climatePhase == Climate.Phase.Future_Climate)
                timestepData = Climate.Future_AllData;
            else
                timestepData = Climate.Spinup_AllData;

            var monthlyData = new ClimateRecord[12];      // for returning summary data in ClimateRecord form.

            var yearCount = timestepData.Count;

            for (int mo = 0; mo < 12; mo++)
            {
                var monthlyMinTemp = 0.0;
                var monthlyMaxTemp = 0.0;
                var monthlyVarTemp = 0.0;
                var monthlyVarPpt = 0.0;
                var monthlyPrecip = 0.0;
                var monthlyPAR = 0.0;
                var monthlyWindDirection = 0.0;
                var monthlyWindSpeed = 0.0;
                var monthlyNDeposition = 0.0;

                foreach (var yearMonthlyRecords in timestepData.Values)
                {
                    monthlyMinTemp += yearMonthlyRecords[ecoregion.Index][mo].AvgMinTemp;
                    monthlyMaxTemp += yearMonthlyRecords[ecoregion.Index][mo].AvgMaxTemp;
                    monthlyVarTemp += yearMonthlyRecords[ecoregion.Index][mo].AvgVarTemp;
                    monthlyVarPpt += yearMonthlyRecords[ecoregion.Index][mo].AvgVarPpt;
                    monthlyPrecip += yearMonthlyRecords[ecoregion.Index][mo].AvgPpt;
                    monthlyPAR += yearMonthlyRecords[ecoregion.Index][mo].AvgPAR;
                    monthlyWindDirection += yearMonthlyRecords[ecoregion.Index][mo].AvgWindDirection;
                    monthlyWindSpeed += yearMonthlyRecords[ecoregion.Index][mo].AvgWindSpeed;
                    monthlyNDeposition += yearMonthlyRecords[ecoregion.Index][mo].AvgNDeposition;

                }

                monthlyData[mo] = new ClimateRecord();
                if (yearCount > 0)
                {
                    monthlyData[mo].AvgMinTemp = monthlyMinTemp / yearCount;
                    monthlyData[mo].AvgMaxTemp = monthlyMaxTemp / yearCount;
                    monthlyData[mo].AvgVarTemp = monthlyVarTemp / yearCount;
                    monthlyData[mo].StdDevTemp = Math.Sqrt(monthlyVarTemp / yearCount);
                    monthlyData[mo].AvgVarPpt = monthlyVarPpt / yearCount;
                    monthlyData[mo].AvgPpt = monthlyPrecip / yearCount;
                    monthlyData[mo].StdDevPpt = Math.Sqrt(monthlyPrecip / yearCount);
                    monthlyData[mo].AvgPAR = monthlyPAR / yearCount;
                    monthlyData[mo].AvgWindDirection = monthlyWindDirection / yearCount;
                    monthlyData[mo].AvgWindSpeed = monthlyWindSpeed / yearCount;
                    monthlyData[mo].AvgNDeposition = monthlyNDeposition / yearCount;
                }
            }
            return monthlyData;
        }
Ejemplo n.º 10
0
        public static void Convert_USGS_to_ClimateData_FillAlldata(TemporalGranularity timeStep, string climateFile, string climateFileFormat, Climate.Phase climatePhase)
        {
            //Dictionary<int, ClimateRecord[][]> allDataRef = null; //this dictionary is filled out either by Daily data or Monthly
            //if (climatePhase == Climate.Phase.Future_Climate)
            //    allDataRef = Climate.Future_AllData;

            //if (climatePhase == Climate.Phase.SpinUp_Climate)
            //    allDataRef = Climate.Spinup_AllData;

            //// parse the input file into lists of timestamps and corresponding climate records arrays
            //List<string> timeStamps;
            //List<ClimateRecord>[] climateRecords;       // indexing: [ecoregion.Count][i]
            //Convert_USGS_to_ClimateData(timeStep, climateFile, climateFileFormat, out timeStamps, out climateRecords);

            //// break up the ecoregion lists into a dictionary by year based on timeStamp keys
            //var yearData = new Dictionary<int, List<ClimateRecord>[]>();    // indexing: [year][ecoregion][i]

            //var currentYear = -999;

            //List<ClimateRecord>[] yearRecords = null;

            //for (var j = 0; j < timeStamps.Count; ++j)
            //{
            //    var year = int.Parse(timeStamps[j].Substring(0, 4));

            //    // timestamps are grouped by year in the input files
            //    if (year != currentYear)
            //    {
            //        // make yearRecords instance for the new year
            //        yearRecords = new List<ClimateRecord>[Climate.ModelCore.Ecoregions.Count];
            //        for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
            //            yearRecords[i] = new List<ClimateRecord>();

            //        yearData[year] = yearRecords;
            //        currentYear = year;
            //    }

            //    // add the climate records onto the year records
            //    for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
            //        yearRecords[i].Add(climateRecords[i][j]);
            //}

            //// transfer the data to allDataRef and
            //// do some basic error checking

            //if (allDataRef == null)
            //    allDataRef = new Dictionary<int, ClimateRecord[][]>();
            //else
            //    allDataRef.Clear();

            //foreach (var key in yearData.Keys)
            //{
            //    allDataRef[key] = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];

            //    for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
            //    {
            //        if (timeStep == TemporalGranularity.Monthly && yearData[key][i].Count != 12)
            //            throw new ApplicationException(string.Format("Error in ClimateDataConvertor: Monthly data for year {0} in climate file '{1}' does not have 12 records.  It has {2} records.", key, climateFile, yearData[key][i].Count));

            //        if (timeStep == TemporalGranularity.Daily && yearData[key][i].Count != 365)
            //            throw new ApplicationException(string.Format("Error in ClimateDataConvertor: Daily data for year {0} in climate file '{1}' does not have 365 records.  It has {2} records.", key, climateFile, yearData[key][i].Count));

            //        // convert the yearRecords from List<ClimateRecord>[] to ClimateRecord[][]
            //        allDataRef[key][i] = yearData[key][i].ToArray();
            //    }
            //}

            // **
            // John McNabb:  new parsing code
            List <int> yearKeys;
            List <List <ClimateRecord>[]> climateRecords;

            Convert_USGS_to_ClimateData2(timeStep, climateFile, climateFileFormat, out yearKeys, out climateRecords);

            Dictionary <int, ClimateRecord[][]> allDataRef = null; //this dictionary is filled out either by Daily data or Monthly

            if (climatePhase == Climate.Phase.Future_Climate)
            {
                allDataRef = Climate.Future_AllData;
            }

            if (climatePhase == Climate.Phase.SpinUp_Climate)
            {
                allDataRef = Climate.Spinup_AllData;
            }

            if (allDataRef == null)
            {
                allDataRef = new Dictionary <int, ClimateRecord[][]>();
            }
            else
            {
                allDataRef.Clear();
            }

            for (var i = 0; i < yearKeys.Count; ++i)
            {
                var ecoRecords = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];
                allDataRef[yearKeys[i]] = ecoRecords;

                for (var j = 0; j < Climate.ModelCore.Ecoregions.Count; ++j)
                {
                    // convert the parsed climateRecords for this year from List<ClimateRecord>[] to ClimateRecord[][]
                    ecoRecords[j] = climateRecords[i][j].ToArray();
                }
            }
        }
        //--------------------------------------------------------------------
        /// <summary>
        /// This function converts Monthly to Monthly and Daily to Monthly
        /// </summary>
        /// <returns>string: file name of the converted monthly file </returns>
        public static void Convert_USGS_to_ClimateData_FillAlldata(TemporalGranularity timeStep, string climateFile, string climateFileFormat, Climate.Phase climatePhase)
        {
            Dictionary<int, IClimateRecord[,]> allDataRef = null; //this dictionary is filled out either by Daily data or Monthly
            if (climatePhase == Climate.Phase.Future_Climate)
                allDataRef = Climate.Future_AllData;
            
            if (climatePhase == Climate.Phase.SpinUp_Climate)
                allDataRef = Climate.Spinup_AllData;

            //The Convert conversts the csv file data to a dictionary (of either daily or monthly data)
            //--------------------------------------------------------------------
            //string resultingFileName = 
                Convert_USGS_to_ClimateData(timeStep, climateFile, climateFileFormat);
            //-------------------------------------------------------------------- 
            //Then read from the dictionary and convert it to IClimate records 

            //if (timeStep == TimeStep.Daily)
            //{
            //setIndexed(checkRHWindSpeed);
            IndexMaxT_Mean = 0;
            IndexMaxT_Var = 1;
            IndexMaxT_STD = 2;
            IndexMinT_Mean = 3;
            IndexMinT_Var = 4;
            IndexMinT_STD = 5;
            IndexPrcp_Mean = 6;
            IndexPrcp_Var = 7;
            IndexPrcp_STD = 8;

            IndexRH_Mean = 9;
            IndexRH_Var = 10;
            IndexRH_STD = 11;

            IndexwindSpeed_Mean = 12;
            IndexwindSpeed_Var = 13;
            IndexwindSpeed_STD = 14;


            for (int j = firstYear; j <= lastYear; j++)//for each year
            {
                currentYear = j.ToString();

                //Dictionary<string, double[]> climate_Dic_currentYear = (Dictionary<string, double[]>)climate_Dic.Where(r => r.Key.Substring(0, 4).ToString() == currentYear);
                IEnumerable<KeyValuePair<string, double[]>> climate_Dic_currentYear = climate_Dic.Where(r => r.Key.Substring(0, 4).ToString() == currentYear);

                IClimateRecord[,] icrs = new IClimateRecord[Climate.ModelCore.Ecoregions.Count, climate_Dic_currentYear.Count()]; // climate_Dic_currentYear.Count: number of days/months in a year
                
                for (int i = 0; i < Climate.ModelCore.Ecoregions.Count; i++) //for each ecoregion either active or inactive
                {
                    //IClimateRecord icr;
                    List<IClimateRecord> icrList = new List<IClimateRecord>();
                    int icrCount = 0;
                    foreach (KeyValuePair<string, double[]> row in climate_Dic_currentYear) // foreach day/month in a certain year-ecoregion
                    {

                        IClimateRecord icr = new ClimateRecord(row.Value[IndexMinT_Mean], row.Value[IndexMaxT_Mean], (row.Value[IndexMinT_STD] + row.Value[IndexMaxT_STD]) / 2, row.Value[IndexPrcp_Mean], row.Value[IndexPrcp_STD], 0, (row.Value[IndexMinT_Var] + row.Value[IndexMaxT_Var]) / 2, 0, row.Value[IndexRH_Mean], row.Value[IndexRH_Var], row.Value[IndexRH_STD], row.Value[IndexwindSpeed_Mean], row.Value[IndexwindSpeed_Var], row.Value[IndexwindSpeed_STD]);
                        if (Climate.ModelCore.Ecoregions[i].Active)
                            icrs[i, icrCount++] = icr;//new KeyValuePair<int, IClimateRecord>(i, icr);

                    }

                }

                allDataRef.Add(j, icrs);
            }

            return;// resultingFileName;
        }
        public static void Convert_USGS_to_ClimateData_FillAlldata(TemporalGranularity timeStep, string climateFile, string climateFileFormat, Climate.Phase climatePhase)
        {
            //Dictionary<int, ClimateRecord[][]> allDataRef = null; //this dictionary is filled out either by Daily data or Monthly
            //if (climatePhase == Climate.Phase.Future_Climate)
            //    allDataRef = Climate.Future_AllData;

            //if (climatePhase == Climate.Phase.SpinUp_Climate)
            //    allDataRef = Climate.Spinup_AllData;

            //// parse the input file into lists of timestamps and corresponding climate records arrays
            //List<string> timeStamps;
            //List<ClimateRecord>[] climateRecords;       // indexing: [ecoregion.Count][i]
            //Convert_USGS_to_ClimateData(timeStep, climateFile, climateFileFormat, out timeStamps, out climateRecords);

            //// break up the ecoregion lists into a dictionary by year based on timeStamp keys
            //var yearData = new Dictionary<int, List<ClimateRecord>[]>();    // indexing: [year][ecoregion][i]

            //var currentYear = -999;

            //List<ClimateRecord>[] yearRecords = null;

            //for (var j = 0; j < timeStamps.Count; ++j)
            //{
            //    var year = int.Parse(timeStamps[j].Substring(0, 4));

            //    // timestamps are grouped by year in the input files
            //    if (year != currentYear)
            //    {
            //        // make yearRecords instance for the new year
            //        yearRecords = new List<ClimateRecord>[Climate.ModelCore.Ecoregions.Count];
            //        for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
            //            yearRecords[i] = new List<ClimateRecord>();

            //        yearData[year] = yearRecords;
            //        currentYear = year;
            //    }

            //    // add the climate records onto the year records
            //    for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
            //        yearRecords[i].Add(climateRecords[i][j]);
            //}

            //// transfer the data to allDataRef and
            //// do some basic error checking

            //if (allDataRef == null)
            //    allDataRef = new Dictionary<int, ClimateRecord[][]>();
            //else
            //    allDataRef.Clear();

            //foreach (var key in yearData.Keys)
            //{
            //    allDataRef[key] = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];

            //    for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
            //    {
            //        if (timeStep == TemporalGranularity.Monthly && yearData[key][i].Count != 12)
            //            throw new ApplicationException(string.Format("Error in ClimateDataConvertor: Monthly data for year {0} in climate file '{1}' does not have 12 records.  It has {2} records.", key, climateFile, yearData[key][i].Count));

            //        if (timeStep == TemporalGranularity.Daily && yearData[key][i].Count != 365)
            //            throw new ApplicationException(string.Format("Error in ClimateDataConvertor: Daily data for year {0} in climate file '{1}' does not have 365 records.  It has {2} records.", key, climateFile, yearData[key][i].Count));

            //        // convert the yearRecords from List<ClimateRecord>[] to ClimateRecord[][]
            //        allDataRef[key][i] = yearData[key][i].ToArray();
            //    }
            //}

            // **
            // John McNabb:  new parsing code
            List<int> yearKeys;
            List<List<ClimateRecord>[]> climateRecords;

            Convert_USGS_to_ClimateData2(timeStep, climateFile, climateFileFormat, out yearKeys, out climateRecords);

            Dictionary<int, ClimateRecord[][]> allDataRef = null; //this dictionary is filled out either by Daily data or Monthly
            if (climatePhase == Climate.Phase.Future_Climate)
                allDataRef = Climate.Future_AllData;

            if (climatePhase == Climate.Phase.SpinUp_Climate)
                allDataRef = Climate.Spinup_AllData;

            if (allDataRef == null)
                allDataRef = new Dictionary<int, ClimateRecord[][]>();
            else
                allDataRef.Clear();

            for (var i = 0; i < yearKeys.Count; ++i)
            {
                var ecoRecords = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];
                allDataRef[yearKeys[i]] = ecoRecords;

                for (var j = 0; j < Climate.ModelCore.Ecoregions.Count; ++j)
                {
                    // convert the parsed climateRecords for this year from List<ClimateRecord>[] to ClimateRecord[][]
                    ecoRecords[j] = climateRecords[i][j].ToArray();
                }
            }
        }
        //public int tempEcoIndex = -1;
        //public double[] DailyPET = new double[366];  // Potential Evapotranspiration
        //public double[] DailyVPD = new double[366];  // Vapor Pressure Deficit
        //public double[] DailyDayLength = new double[366];
        //public double[] DailyNightLength = new double[366];
        //public int[] DailyGDD = new int[366];
        //For Sequenced and Random timeStep arg should be passed
        public AnnualClimate_Daily(IEcoregion ecoregion, double latitude, Climate.Phase spinupOrfuture, int timeStep, int timeStepIndex)
        {
            this.climatePhase = spinupOrfuture;
            ClimateRecord[][] timestepData = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];
            for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
                timestepData[i] = new ClimateRecord[366];

            string climateOption = Climate.ConfigParameters.ClimateTimeSeries;
            if (this.climatePhase == Climate.Phase.SpinUp_Climate)
                climateOption = Climate.ConfigParameters.SpinUpClimateTimeSeries;

            ClimateRecord[] dailyData = null;

            int actualTimeStep;

            //Climate.TextLog.WriteLine("  Calculating daily data ...  Ecoregion = {0}, Year = {1}, timestep = {2}.", ecoregion.Name, actualYear, timeStep);
            switch (climateOption)
            {
                case "Daily_RandomYears":
                    {
                        // JM: this code assumes that the constructor for AnnualClimate_Daily is ONLY called from within
                        //  AnnualClimate_Monthly.AnnualClimate_From_AnnualClimate_Daily(), and, for Daily_RandomYear, the
                        //  actualYear contains the randomly-selected year.

                        TimeStep = timeStep;
                        Dictionary<int, ClimateRecord[][]> allData;
                        List<int> randomKeyList;

                        if (this.climatePhase == Climate.Phase.Future_Climate)
                        {
                            allData = Climate.Future_AllData;
                            randomKeyList = Climate.RandSelectedTimeKeys_future;
                        }
                        else
                        {
                            allData = Climate.Spinup_AllData;
                            randomKeyList = Climate.RandSelectedTimeKeys_spinup;
                        }

                        if (timeStepIndex >= randomKeyList.Count())
                        {
                            throw new ApplicationException(string.Format("Exception: the requested Time-step {0} is out-of-range for the {1} input file.", timeStep, this.climatePhase));
                        }
                        else
                            actualTimeStep = randomKeyList[timeStepIndex];

                        Climate.TextLog.WriteLine("  AnnualClimate_Daily: Daily_RandomYear: timeStep = {0}, actualYear = {1}, phase = {2}.", timeStep, actualTimeStep, this.climatePhase);

                        dailyData = allData[actualTimeStep][ecoregion.Index];
                        CalculateDailyData(ecoregion, dailyData, actualTimeStep, latitude);
                        break;

                    }
                case "Daily_AverageAllYears":
                    {
                        TimeStep = timeStep;
                        actualTimeStep = 0;

                        dailyData = AnnualClimate_AvgDaily(ecoregion, latitude);
                        CalculateDailyData(ecoregion, dailyData, actualTimeStep, latitude);
                        break;
                    }
                case "Daily_SequencedYears":
                    {
                        TimeStep = timeStep;
                        actualTimeStep = timeStep;
                        Dictionary<int, ClimateRecord[][]> allData;

                        if (this.climatePhase == Climate.Phase.Future_Climate)
                            allData = Climate.Future_AllData;
                        else
                            allData = Climate.Spinup_AllData;

                        ClimateRecord[][] yearRecords;

                        // get the climate records for the requested year, or if the year is not found, get the records for the last year
                        if (!allData.TryGetValue(timeStep, out yearRecords))
                        {
                            actualTimeStep = allData.Keys.Max();
                            yearRecords = allData[actualTimeStep];
                        }

                        dailyData = yearRecords[ecoregion.Index];
                        CalculateDailyData(ecoregion, dailyData, actualTimeStep, latitude);
                        break;
                    }
                default:
                    throw new ApplicationException(String.Format("Unknown Climate Time Series: {0}", climateOption));

            }

            this.beginGrowing = CalculateBeginGrowingDay_Daily(); //ecoClimate);
            this.endGrowing = CalculateEndGrowingDay_Daily(dailyData);
            this.growingDegreeDays = GrowSeasonDegreeDays();

            this.DailyDataIsLeapYear = dailyData.Length == 366;
        }
        //, Random autoRand)  //Actually only using monthly data for establishment.
        //Calculate End Growing Degree Day (First frost; Minimum = 0 degrees C):
        //---------------------------------------------------------------------------
        private int CalculateEndGrowingDay_Daily(ClimateRecord[] annualClimate)
        {
            double nightTemp = 0.0;
            //int beginGrowingDay = CalculateBeginGrowingDay_Daily(annualClimate);
            int endGrowingDay = MaxDayInYear;
            //int i = beginGrowingDay;
            for (int day = MaxDayInYear; day > this.BeginGrowing; day--)  //Loop through all the days of the year from day 1 to day 162
            {
                nightTemp = this.DailyMinTemp[day];
                if (nightTemp > 0)
                {
                    //this.endGrowing = i;
                    //endGrowingDay = i;
                    return day;
                }
                //Climate.TextLog.WriteLine("  Calculating end begin growing season day...{0}", endGrowingDay);
            }

            return 0;
        }
        private void CalculateDailyData(IEcoregion ecoregion, ClimateRecord[] dailyClimateRecords, int actualYear, double latitude)
        {
            this.Year = actualYear;

            this.TotalAnnualPrecip = 0.0;
            for (int d = 0; d < dailyClimateRecords.Length; d++)
            {
                this.DailyMinTemp[d] = dailyClimateRecords[d].AvgMinTemp;
                this.DailyMaxTemp[d] = dailyClimateRecords[d].AvgMaxTemp;
                this.DailyVarTemp[d] = dailyClimateRecords[d].AvgVarTemp;
                this.DailyVarPpt[d] = dailyClimateRecords[d].AvgVarPpt;
                this.DailyPrecip[d] = dailyClimateRecords[d].AvgPpt;
                this.DailyPAR[d] = dailyClimateRecords[d].AvgPAR;
                this.DailyWindDirection[d] = dailyClimateRecords[d].AvgWindDirection;
                this.DailyWindSpeed[d] = dailyClimateRecords[d].AvgWindSpeed;
                this.DailyNDeposition[d] = dailyClimateRecords[d].AvgNDeposition;
                this.DailyCO2[d] = dailyClimateRecords[d].AvgCO2;

                this.DailyTemp[d] = (this.DailyMinTemp[d] + this.DailyMaxTemp[d]) / 2.0;

                this.TotalAnnualPrecip += this.DailyPrecip[d];

                //var hr = CalculateDayLength(d, latitude);
                //this.DailyDayLength[d] = (3600.0 * hr);                  // seconds of daylight/day
                //this.DailyNightLength[d] = (3600.0 * (24.0 - hr));         // seconds of nighttime/day
                var avgTemp = (this.DailyMinTemp[d] + this.DailyMaxTemp[d])/2;
                this.DailyRH[d] = 100 * Math.Exp((17.269 * this.DailyMinTemp[d]) / (273.15 + this.DailyMinTemp[d]) - (17.269 * avgTemp) / (273.15 + avgTemp));
            }
        }
Ejemplo n.º 16
0
        //For Sequenced and Random timeStep arg should be passed
        public AnnualClimate_Daily(IEcoregion ecoregion, double latitude, Climate.Phase spinupOrfuture, int timeStep, int timeStepIndex)
        {
            this.climatePhase = spinupOrfuture;
            ClimateRecord[][] timestepData = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];
            for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
            {
                timestepData[i] = new ClimateRecord[366];
            }

            string climateOption = Climate.ConfigParameters.ClimateTimeSeries;

            if (this.climatePhase == Climate.Phase.SpinUp_Climate)
            {
                climateOption = Climate.ConfigParameters.SpinUpClimateTimeSeries;
            }

            ClimateRecord[] dailyData = null;

            int actualTimeStep;

            switch (climateOption)
            {
            case "Daily_RandomYears":
            {
                // JM: this code assumes that the constructor for AnnualClimate_Daily is ONLY called from within
                //  AnnualClimate_Monthly.AnnualClimate_From_AnnualClimate_Daily(), and, for Daily_RandomYear, the
                //  actualYear contains the randomly-selected year.

                TimeStep = timeStep;
                Dictionary <int, ClimateRecord[][]> allData;
                List <int> randomKeyList;

                if (this.climatePhase == Climate.Phase.Future_Climate)
                {
                    allData       = Climate.Future_AllData;
                    randomKeyList = Climate.RandSelectedTimeKeys_future;
                }
                else
                {
                    allData       = Climate.Spinup_AllData;
                    randomKeyList = Climate.RandSelectedTimeKeys_spinup;
                }

                if (timeStepIndex >= randomKeyList.Count())
                {
                    throw new ApplicationException(string.Format("Exception: the requested Time-step {0} is out-of-range for the {1} input file.", timeStep, this.climatePhase));
                }
                else
                {
                    actualTimeStep = randomKeyList[timeStepIndex];
                }

                Climate.TextLog.WriteLine("  AnnualClimate_Daily: Daily_RandomYear: timeStep = {0}, actualYear = {1}, phase = {2}.", timeStep, actualTimeStep, this.climatePhase);

                dailyData = allData[actualTimeStep][ecoregion.Index];
                CalculateDailyData(ecoregion, dailyData, actualTimeStep, latitude);
                break;
            }

            case "Daily_AverageAllYears":
            {
                TimeStep       = timeStep;
                actualTimeStep = 0;

                dailyData = AnnualClimate_AvgDaily(ecoregion, latitude);
                CalculateDailyData(ecoregion, dailyData, actualTimeStep, latitude);
                break;
            }

            case "Daily_SequencedYears":
            {
                TimeStep       = timeStep;
                actualTimeStep = timeStep;
                Dictionary <int, ClimateRecord[][]> allData;

                if (this.climatePhase == Climate.Phase.Future_Climate)
                {
                    allData = Climate.Future_AllData;
                }
                else
                {
                    allData = Climate.Spinup_AllData;
                }

                ClimateRecord[][] yearRecords;

                // get the climate records for the requested year, or if the year is not found, get the records for the last year
                if (!allData.TryGetValue(timeStep, out yearRecords))
                {
                    actualTimeStep = allData.Keys.Max();
                    yearRecords    = allData[actualTimeStep];
                }

                dailyData = yearRecords[ecoregion.Index];
                CalculateDailyData(ecoregion, dailyData, actualTimeStep, latitude);
                break;
            }

            default:
                throw new ApplicationException(String.Format("Unknown Climate Time Series: {0}", climateOption));
            }

            this.beginGrowing        = CalculateBeginGrowingDay_Daily(dailyData); //ecoClimate);
            this.endGrowing          = CalculateEndGrowingDay_Daily(dailyData);
            this.growingDegreeDays   = GrowSeasonDegreeDays();
            this.DailyDataIsLeapYear = dailyData.Length == 366;
        }
Ejemplo n.º 17
0
        //---------------------------------------------------------------------
        private static void WriteSpinupInputLog(ClimateRecord[][] TimestepData, int year)
        {
            int maxtimestep = 12;
            if (spinup_allData_granularity == TemporalGranularity.Daily)
                maxtimestep = 365;

            //spinup_allData.
            foreach (IEcoregion ecoregion in Climate.ModelCore.Ecoregions)
            {
                if (ecoregion.Active)
                {
                    //for (int month = 0; month < 12; month++)
                        for (int timestep = 0; timestep < maxtimestep; timestep++)
                        {
                        SpinupInputLog.Clear();
                        InputLog sil = new InputLog();

                        //sil.SimulationPeriod = period;
                        sil.Year = year;
                        sil.Timestep = timestep + 1;
                        sil.EcoregionName = ecoregion.Name;
                        sil.EcoregionIndex = ecoregion.Index;
                        sil.min_airtemp = TimestepData[ecoregion.Index][timestep].AvgMinTemp;
                        sil.max_airtemp = TimestepData[ecoregion.Index][timestep].AvgMaxTemp;
                        sil.std_temp = TimestepData[ecoregion.Index][timestep].StdDevTemp;
                        sil.ppt = TimestepData[ecoregion.Index][timestep].AvgPpt;
                        sil.std_ppt = TimestepData[ecoregion.Index][timestep].StdDevPpt;
                        sil.ndeposition = TimestepData[ecoregion.Index][timestep].AvgNDeposition;
                        //sil.co2 = TimestepData[ecoregion.Index][timestep].AvgCO2;

                        SpinupInputLog.AddObject(sil);
                        SpinupInputLog.WriteToFile();

                    }
                }
            }
        }
        public AnnualClimate_Monthly(IEcoregion ecoregion, double latitude, Climate.Phase spinupOrfuture, int timeStep, int timeStepIndex)
        {
            this.climatePhase = spinupOrfuture;
            this.Latitude = latitude;

            // ------------------------------------------------------------------------------------------------------
            // Case:  Daily data used for future climate.  Note: No need to ever use daily data with spinup climate
            //if (Climate.AllData_granularity == TemporalGranularity.Daily && spinupOrfuture == Climate.Phase.Future_Climate)
            //{
            //    //Climate.ModelCore.UI.WriteLine("  Processing Daily data into Monthly data.  Ecoregion = {0}, Year = {1}, timestep = {2}.", ecoregion.Name, monthlyDataKey, timeStep);
            //    this.AnnualClimate_From_AnnualClimate_Daily(ecoregion, monthlyDataKey, latitude, spinupOrfuture, timeStep);
            //    return;
            //}

            ClimateRecord[][] timestepData = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];
            for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
                timestepData[i]= new ClimateRecord[12];

            // ------------------------------------------------------------------------------------------------------

            string climateOption = Climate.ConfigParameters.ClimateTimeSeries;
            if (this.climatePhase == Climate.Phase.SpinUp_Climate)
                climateOption = Climate.ConfigParameters.SpinUpClimateTimeSeries;

            ClimateRecord[] monthlyData;

            int actualTimeStep;

            switch (climateOption)
            {
                case "Monthly_AverageAllYears":
                    {
                        TimeStep = timeStep;
                        actualTimeStep = 0;
                        monthlyData = AnnualClimate_AvgMonth(ecoregion, latitude);
                        CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                        //Climate.ModelCore.UI.WriteLine("  Completed calculations for {0} using AVERAGE MONTHLY data. Ecoregion = {1}, SimulatedYear = AVERAGED.", this.climatePhase, ecoregion.Name, actualTimeStep);
                        break;
                    }

                case "Monthly_AverageWithVariation": //this case is not working as of 5/15/14
                    {
                        TimeStep = timeStep;
                        actualTimeStep = 0;
                        monthlyData = AnnualClimate_AvgMonth(ecoregion, latitude);
                        //Climate.ModelCore.UI.WriteLine("  Completed calculations for {0} from AVERAGE MONTHLY data. Ecoregion = {1}, SimulatedYear = AVERAGED.", this.climatePhase, ecoregion.Name, actualTimeStep);
                        //timestepData = AnnualClimate_AvgMonth(ecoregion, monthlyDataKey, latitude);
                        //CalculateMonthlyData_AddVariance(ecoregion, monthlyData, actualTimeStep, latitude);
                        break;
                    }
                case "Monthly_RandomYears":
                    {
                        TimeStep = timeStep;
                        Dictionary<int, ClimateRecord[][]> allData;
                        List<int> randomKeyList;

                        if (this.climatePhase == Climate.Phase.Future_Climate)
                        {
                            allData = Climate.Future_AllData;
                            randomKeyList = Climate.RandSelectedTimeKeys_future;
                        }
                        else
                        {
                            allData = Climate.Spinup_AllData;
                            randomKeyList = Climate.RandSelectedTimeKeys_spinup;
                        }

                        if (timeStepIndex >= randomKeyList.Count())
                        {
                            throw new ApplicationException(string.Format("Exception: the requested Time-step {0} is out-of-range for the {1} input file.", timeStep, this.climatePhase));
                        }
                        else
                            actualTimeStep = randomKeyList[timeStepIndex];

                        //Climate.ModelCore.UI.WriteLine("  AnnualClimate_Monthly: Monthly_RandomYear: timeStep = {0}, actualYear = {1}, phase = {2}.", timeStep, actualTimeStep, this.climatePhase);
                        //Climate.ModelCore.UI.WriteLine("  Completed calculations for FutureData using AnnualClimate_Monthly: SimulatedYear = {0}, actualYearSelected = {1}.", timeStep, actualTimeStep);
                        //Climate.ModelCore.UI.WriteLine("  Completed calculations for {0} using RandomYear_Monthly. Ecoregion = {1}, SimulatedYear = {2}, actualYearUsed={3}.", this.climatePhase, ecoregion.Name, timeStep, actualTimeStep);

                        monthlyData = allData[actualTimeStep][ecoregion.Index];
                        CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                        break;

                    }
                case "Monthly_SequencedYears":
                    {
                        TimeStep = timeStep;
                        actualTimeStep = timeStep;
                        Dictionary<int, ClimateRecord[][]> allData;

                        if (this.climatePhase == Climate.Phase.Future_Climate)
                            allData = Climate.Future_AllData;
                        else
                            allData = Climate.Spinup_AllData;

                        ClimateRecord[][] yearRecords;

                        // get the climate records for the requested year, or if the year is not found, get the records for the last year
                        if (!allData.TryGetValue(timeStep, out yearRecords))
                        {
                            actualTimeStep = allData.Keys.Max();
                            yearRecords = allData[actualTimeStep];
                        }

                        monthlyData = yearRecords[ecoregion.Index];
                        CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);

                        //Climate.ModelCore.UI.WriteLine("  Completed calculations for {0} using Monthly_SequencedYears. Ecoregion = {1}, SimulatedYear = {2}, actualYearUsed={3}.", this.climatePhase, ecoregion.Name, timeStep, actualTimeStep);
                        break;
                    }
                case "Daily_RandomYears":
                    {
                        TimeStep = timeStep;
                        Dictionary<int, ClimateRecord[][]> allData;
                        List<int> randomKeyList;

                        if (this.climatePhase == Climate.Phase.Future_Climate)
                        {
                            allData = Climate.Future_AllData;
                            randomKeyList = Climate.RandSelectedTimeKeys_future;
                        }
                        else
                        {
                            allData = Climate.Spinup_AllData;
                            randomKeyList = Climate.RandSelectedTimeKeys_spinup;
                        }

                        if (timeStepIndex >= randomKeyList.Count())
                        {
                            throw new ApplicationException(string.Format("Exception: the requested Time-step {0} is out-of-range for the {1} input file.", timeStep, this.climatePhase));
                        }
                        else
                            actualTimeStep = randomKeyList[timeStepIndex];

                        //Climate.ModelCore.UI.WriteLine("  Completed calculations for {0} using Daily_RandomYear. Ecoregion = {1}, SimulatedYear = {2}, actualYearUsed={3}.", this.climatePhase, ecoregion.Name, timeStep, actualTimeStep);

                        monthlyData = AnnualClimate_From_AnnualClimate_Daily(ecoregion, latitude, spinupOrfuture, timeStep, timeStepIndex);
                        CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                        break;

                    }
                case "Daily_AverageAllYears":
                    {
                        TimeStep = timeStep;
                        actualTimeStep = 0;
                        monthlyData = AnnualClimate_From_AnnualClimate_Daily(ecoregion, latitude, spinupOrfuture, timeStep, timeStepIndex);
                        CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                        //Climate.ModelCore.UI.WriteLine("  Completed calculations for {0} using Daily_AverageAllYears. Ecoregion = {1}, SimulatedYear = {2}.", this.climatePhase, ecoregion.Name, timeStep);
                        break;
                    }
                case "Daily_SequencedYears":
                    {
                        TimeStep = timeStep;
                        actualTimeStep = timeStep;
                        Dictionary<int, ClimateRecord[][]> allData;

                        if (this.climatePhase == Climate.Phase.Future_Climate)
                            allData = Climate.Future_AllData;
                        else
                            allData = Climate.Spinup_AllData;

                        if (!allData.ContainsKey(timeStep))
                            actualTimeStep = allData.Keys.Max();

                        monthlyData = AnnualClimate_From_AnnualClimate_Daily(ecoregion, latitude, spinupOrfuture, timeStep, timeStepIndex);
                        CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                        //Climate.ModelCore.UI.WriteLine("  Completed calculations for {0} using Daily_SequencedYears. Ecoregion = {1}, SimulatedYear = {2}, actualYearUsed={3}.", this.climatePhase, ecoregion.Name, timeStep, actualTimeStep);
                        break;
                    }

                default:
                    throw new ApplicationException(String.Format("Unknown Climate Time Series: {0}", climateOption));

            }

            //this.MonthlyPET = CalculatePotentialEvapotranspiration();
            this.MonthlyPET = CalculatePotentialEvapotranspirationThornwaite();
            this.MonthlyVPD = CalculateVaporPressureDeficit();
            this.MonthlyGDD = CalculatePnETGDD();

            this.beginGrowing = CalculateBeginGrowingSeason();
            this.endGrowing = CalculateEndGrowingSeason();
            this.growingDegreeDays = GrowSeasonDegreeDays();

            this.JJAtemperature = 0.0;
            for (int mo = 5; mo < 8; mo++)
                this.JJAtemperature += this.MonthlyTemp[mo];
            this.JJAtemperature /= 3.0;
        }
Ejemplo n.º 19
0
        //---------------------------------------------------------------------

        protected override Dictionary <int, ClimateRecord[, ]> Parse()
        {
            InputVar <string> landisData = new InputVar <string>("LandisData");

            ReadVar(landisData);
            if (landisData.Value.Actual != LandisDataValue)
            {
                throw new InputValueException(landisData.Value.String, "The value is not \"{0}\"", LandisDataValue);
            }

            Dictionary <int, ClimateRecord[, ]> allData = new Dictionary <int, ClimateRecord[, ]>();

            const string nextTableName = "ClimateTable";


            //---------------------------------------------------------------------
            //Read in climate data:

            ReadName(nextTableName);

            InputVar <string> ecoregionName = new InputVar <string>("Ecoregion");
            //InputVar<int> ecoregionIndex = new InputVar<int>("Ecoregion Index");
            InputVar <int>    year       = new InputVar <int>("Time step for updating the climate");
            InputVar <int>    month      = new InputVar <int>("The Month");
            InputVar <double> avgMinTemp = new InputVar <double>("Monthly Minimum Temperature Value");
            InputVar <double> avgMaxTemp = new InputVar <double>("Monthly Maximum Temperature Value");
            InputVar <double> stdDevTemp = new InputVar <double>("Monthly Std Deviation Temperature Value");
            InputVar <double> avgPpt     = new InputVar <double>("Monthly Precipitation Value");
            InputVar <double> stdDevPpt  = new InputVar <double>("Monthly Std Deviation Precipitation Value");
            InputVar <double> avgPAR     = new InputVar <double>("Monthly Photosynthetically Active Radiation Value");
            InputVar <double> avgVarTemp = new InputVar <double>("Monthly Variance Temperature Value");
            InputVar <double> avgVarPpt  = new InputVar <double>("Monthly Precipitation Variance Temperature Value");

            while (!AtEndOfInput)
            {
                StringReader currentLine = new StringReader(CurrentLine);

                ReadValue(ecoregionName, currentLine);
                //ReadValue(ecoregionIndex, currentLine);

                IEcoregion ecoregion = GetEcoregion(ecoregionName.Value);

                ReadValue(year, currentLine);
                int yr = year.Value.Actual;

                if (!allData.ContainsKey(yr))
                {
                    ClimateRecord[,] climateTable = new ClimateRecord[Climate.ModelCore.Ecoregions.Count, 12];
                    allData.Add(yr, climateTable);
                    //UI.WriteLine("  Climate Parser:  Add new year = {0}.", yr);
                }

                ReadValue(month, currentLine);
                int mo = month.Value.Actual;

                ClimateRecord climateRecord = new ClimateRecord();

                ReadValue(avgMinTemp, currentLine);
                climateRecord.AvgMinTemp = avgMinTemp.Value;

                ReadValue(avgMaxTemp, currentLine);
                climateRecord.AvgMaxTemp = avgMaxTemp.Value;

                ReadValue(stdDevTemp, currentLine);
                climateRecord.StdDevTemp = stdDevTemp.Value;

                ReadValue(avgPpt, currentLine);
                climateRecord.AvgPpt = avgPpt.Value;

                ReadValue(stdDevPpt, currentLine);
                climateRecord.StdDevPpt = stdDevPpt.Value;

                ReadValue(avgPAR, currentLine);
                climateRecord.AvgPAR = avgPAR.Value;

                try
                {
                    ReadValue(avgVarTemp, currentLine);
                    climateRecord.AvgVarTemp = avgVarTemp.Value;

                    ReadValue(avgVarPpt, currentLine);
                    climateRecord.AvgVarPpt = avgVarPpt.Value;

                    allData[yr][ecoregion.Index, mo - 1] = climateRecord;

                    CheckNoDataAfter("the " + avgVarPpt.Name + " column",
                                     currentLine);
                }
                catch (InputVariableException ex)
                {
                    if (ex is InputVariableException) // This we know how to handle.
                    {
                        allData[yr][ecoregion.Index, mo - 1] = climateRecord;

                        CheckNoDataAfter("the " + avgPAR.Name + " column",
                                         currentLine);
                    }
                }

                GetNextLine();
            }

            return(allData);
        }
        private ClimateRecord[] AnnualClimate_From_AnnualClimate_Daily(IEcoregion ecoregion, double latitude, Climate.Phase spinupOrfuture, int timeStep, int timeStepIndex)
        {
            var monthlyData = new ClimateRecord[12];

            int nDays;
            int dayOfYear = 0;
            AnnualClimate_Daily annDaily = new AnnualClimate_Daily(ecoregion, latitude, spinupOrfuture, timeStep, timeStepIndex); //for the same timeStep

            // if annDaily data come from averaging over years, it will always have 365 days, so I can't use the DaysInMonth() method based on the actualYear
            var daysInMonth = annDaily.DailyDataIsLeapYear ? new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } : new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

            if (spinupOrfuture == Climate.Phase.Future_Climate)
                Climate.Future_DailyData[timeStep][ecoregion.Index] = annDaily;
            else
                Climate.Spinup_DailyData[timeStep][ecoregion.Index] = annDaily;

            for (int mo = 0; mo < 12; mo++)
            {
                var monthlyMinTemp = 0.0;
                var monthlyMaxTemp = 0.0;
                var monthlyVarTemp = 0.0;
                var monthlyPptVarTemp = 0.0;
                var monthlyPrecip = 0.0;
                var monthlyPAR = 0.0;
                var monthlyWindDirection = 0.0;
                var monthlyWindSpeed = 0.0;
                var monthlyNDeposition = 0.0;
                var monthlyCO2 = 0.0;
                var monthlyRH = 0.0;

                nDays = daysInMonth[mo];
                for (int d = 0; d < nDays; d++)
                {
                    monthlyMinTemp += annDaily.DailyMinTemp[dayOfYear];
                    monthlyMaxTemp += annDaily.DailyMaxTemp[dayOfYear];
                    monthlyVarTemp += annDaily.DailyVarTemp[dayOfYear];
                    monthlyPptVarTemp += annDaily.DailyVarPpt[dayOfYear];
                    monthlyPrecip += annDaily.DailyPrecip[dayOfYear];
                    monthlyPAR += annDaily.DailyPAR[dayOfYear];
                    monthlyWindDirection += annDaily.DailyWindDirection[dayOfYear];
                    monthlyWindSpeed += annDaily.DailyWindSpeed[dayOfYear];
                    monthlyNDeposition += annDaily.DailyNDeposition[dayOfYear];
                    monthlyCO2 += annDaily.DailyCO2[dayOfYear];
                    monthlyRH += annDaily.DailyRH[dayOfYear];

                    dayOfYear++;
                }

                monthlyData[mo] = new ClimateRecord();

                monthlyData[mo].AvgMinTemp = monthlyMinTemp / nDays;
                monthlyData[mo].AvgMaxTemp = monthlyMaxTemp / nDays;
                monthlyData[mo].AvgVarTemp = monthlyVarTemp / nDays;
                monthlyData[mo].StdDevTemp = Math.Sqrt(monthlyVarTemp / nDays);
                monthlyData[mo].AvgVarPpt = monthlyPptVarTemp / nDays;
                monthlyData[mo].AvgPpt = monthlyPrecip;
                monthlyData[mo].StdDevPpt = Math.Sqrt(monthlyPrecip / nDays);
                monthlyData[mo].AvgPAR = monthlyPAR / nDays;
                monthlyData[mo].AvgWindDirection = monthlyWindDirection / nDays;
                monthlyData[mo].AvgWindSpeed = monthlyWindSpeed / nDays;
                monthlyData[mo].AvgNDeposition = monthlyNDeposition / nDays;
                monthlyData[mo].AvgCO2 = monthlyCO2 / nDays;
                monthlyData[mo].AvgRH = monthlyRH / nDays;
            }

            return monthlyData;
        }
        public AnnualClimate_Monthly(IEcoregion ecoregion, double latitude, Climate.Phase spinupOrfuture, int timeStep, int timeStepIndex)
        {
            this.climatePhase = spinupOrfuture;
            this.Latitude     = latitude;

            // ------------------------------------------------------------------------------------------------------
            // Case:  Daily data used for future climate.  Note: No need to ever use daily data with spinup climate
            //if (Climate.AllData_granularity == TemporalGranularity.Daily && spinupOrfuture == Climate.Phase.Future_Climate)
            //{
            //    //Climate.TextLog.WriteLine("  Processing Daily data into Monthly data.  Ecoregion = {0}, Year = {1}, timestep = {2}.", ecoregion.Name, monthlyDataKey, timeStep);
            //    this.AnnualClimate_From_AnnualClimate_Daily(ecoregion, monthlyDataKey, latitude, spinupOrfuture, timeStep);
            //    return;
            //}

            ClimateRecord[][] timestepData = new ClimateRecord[Climate.ModelCore.Ecoregions.Count][];
            for (var i = 0; i < Climate.ModelCore.Ecoregions.Count; ++i)
            {
                timestepData[i] = new ClimateRecord[12];
            }

            // ------------------------------------------------------------------------------------------------------

            string climateOption = Climate.ConfigParameters.ClimateTimeSeries;

            if (this.climatePhase == Climate.Phase.SpinUp_Climate)
            {
                climateOption = Climate.ConfigParameters.SpinUpClimateTimeSeries;
            }

            ClimateRecord[] monthlyData;

            int actualTimeStep;

            switch (climateOption)
            {
            case "Monthly_AverageAllYears":
            {
                TimeStep       = timeStep;
                actualTimeStep = 0;
                monthlyData    = AnnualClimate_AvgMonth(ecoregion, latitude);
                CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                Climate.TextLog.WriteLine("  Completed calculations for {0} using AVERAGE MONTHLY data. Ecoregion = {1}, SimulatedYear = AVERAGED.", this.climatePhase, ecoregion.Name, actualTimeStep);
                break;
            }

            case "Monthly_AverageWithVariation":     //this case is not working as of 5/15/14
            {
                TimeStep       = timeStep;
                actualTimeStep = 0;
                monthlyData    = AnnualClimate_AvgMonth(ecoregion, latitude);
                Climate.TextLog.WriteLine("  Completed calculations for {0} from AVERAGE MONTHLY data. Ecoregion = {1}, SimulatedYear = AVERAGED.", this.climatePhase, ecoregion.Name, actualTimeStep);
                //timestepData = AnnualClimate_AvgMonth(ecoregion, monthlyDataKey, latitude);
                //CalculateMonthlyData_AddVariance(ecoregion, monthlyData, actualTimeStep, latitude);
                break;
            }

            case "Monthly_RandomYears":
            {
                TimeStep = timeStep;
                Dictionary <int, ClimateRecord[][]> allData;
                List <int> randomKeyList;

                if (this.climatePhase == Climate.Phase.Future_Climate)
                {
                    allData       = Climate.Future_AllData;
                    randomKeyList = Climate.RandSelectedTimeKeys_future;
                }
                else
                {
                    allData       = Climate.Spinup_AllData;
                    randomKeyList = Climate.RandSelectedTimeKeys_spinup;
                }

                if (timeStepIndex >= randomKeyList.Count())
                {
                    throw new ApplicationException(string.Format("Exception: the requested Time-step {0} is out-of-range for the {1} input file.", timeStep, this.climatePhase));
                }
                else
                {
                    actualTimeStep = randomKeyList[timeStepIndex];
                }

                //Climate.TextLog.WriteLine("  AnnualClimate_Monthly: Monthly_RandomYear: timeStep = {0}, actualYear = {1}, phase = {2}.", timeStep, actualTimeStep, this.climatePhase);
                Climate.TextLog.WriteLine("  Completed calculations for {0} using RandomYear_Monthly. Ecoregion = {1}, SimulatedYear = {2}, actualYearUsed={3}.", this.climatePhase, ecoregion.Name, timeStep, actualTimeStep);

                monthlyData = allData[actualTimeStep][ecoregion.Index];
                CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                break;
            }

            case "Monthly_SequencedYears":
            {
                TimeStep       = timeStep;
                actualTimeStep = timeStep;
                Dictionary <int, ClimateRecord[][]> allData;

                if (this.climatePhase == Climate.Phase.Future_Climate)
                {
                    allData = Climate.Future_AllData;
                }
                else
                {
                    allData = Climate.Spinup_AllData;
                }

                ClimateRecord[][] yearRecords;

                // get the climate records for the requested year, or if the year is not found, get the records for the last year
                if (!allData.TryGetValue(timeStep, out yearRecords))
                {
                    actualTimeStep = allData.Keys.Max();
                    yearRecords    = allData[actualTimeStep];
                }

                monthlyData = yearRecords[ecoregion.Index];
                CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);

                Climate.TextLog.WriteLine("  Completed calculations for {0} using Monthly_SequencedYears. Ecoregion = {1}, SimulatedYear = {2}, actualYearUsed={3}.", this.climatePhase, ecoregion.Name, timeStep, actualTimeStep);
                break;
            }

            case "Daily_RandomYears":
            {
                TimeStep = timeStep;
                Dictionary <int, ClimateRecord[][]> allData;
                List <int> randomKeyList;

                if (this.climatePhase == Climate.Phase.Future_Climate)
                {
                    allData       = Climate.Future_AllData;
                    randomKeyList = Climate.RandSelectedTimeKeys_future;
                }
                else
                {
                    allData       = Climate.Spinup_AllData;
                    randomKeyList = Climate.RandSelectedTimeKeys_spinup;
                }

                if (timeStepIndex >= randomKeyList.Count())
                {
                    throw new ApplicationException(string.Format("Exception: the requested Time-step {0} is out-of-range for the {1} input file.", timeStep, this.climatePhase));
                }
                else
                {
                    actualTimeStep = randomKeyList[timeStepIndex];
                }

                Climate.TextLog.WriteLine("  Completed calculations for {0} using Daily_RandomYear. Ecoregion = {1}, SimulatedYear = {2}, actualYearUsed={3}.", this.climatePhase, ecoregion.Name, timeStep, actualTimeStep);


                monthlyData = AnnualClimate_From_AnnualClimate_Daily(ecoregion, latitude, spinupOrfuture, timeStep, timeStepIndex);
                CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                break;
            }

            case "Daily_AverageAllYears":
            {
                TimeStep       = timeStep;
                actualTimeStep = 0;
                monthlyData    = AnnualClimate_From_AnnualClimate_Daily(ecoregion, latitude, spinupOrfuture, timeStep, timeStepIndex);
                CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                //Climate.TextLog.WriteLine("  Completed calculations for {0} using Daily_AverageAllYears. Ecoregion = {1}, SimulatedYear = {2}.", this.climatePhase, ecoregion.Name, timeStep);
                break;
            }

            case "Daily_SequencedYears":
            {
                TimeStep       = timeStep;
                actualTimeStep = timeStep;
                Dictionary <int, ClimateRecord[][]> allData;

                if (this.climatePhase == Climate.Phase.Future_Climate)
                {
                    allData = Climate.Future_AllData;
                }
                else
                {
                    allData = Climate.Spinup_AllData;
                }

                if (!allData.ContainsKey(timeStep))
                {
                    actualTimeStep = allData.Keys.Max();
                }

                monthlyData = AnnualClimate_From_AnnualClimate_Daily(ecoregion, latitude, spinupOrfuture, timeStep, timeStepIndex);
                CalculateMonthlyData(ecoregion, monthlyData, actualTimeStep, latitude);
                //Climate.TextLog.WriteLine("  Completed calculations for {0} using Daily_SequencedYears. Ecoregion = {1}, SimulatedYear = {2}, actualYearUsed={3}.", this.climatePhase, ecoregion.Name, timeStep, actualTimeStep);
                break;
            }

            default:
                throw new ApplicationException(String.Format("Unknown Climate Time Series: {0}", climateOption));
            }

            //this.MonthlyPET = CalculatePotentialEvapotranspiration();
            this.MonthlyPET = CalculatePotentialEvapotranspirationThornwaite();
            this.MonthlyVPD = CalculateVaporPressureDeficit();
            this.MonthlyGDD = CalculatePnETGDD();

            this.beginGrowing      = CalculateBeginGrowingSeason();
            this.endGrowing        = CalculateEndGrowingSeason();
            this.growingDegreeDays = GrowSeasonDegreeDays();

            this.JJAtemperature = 0.0;
            for (int mo = 5; mo < 8; mo++)
            {
                this.JJAtemperature += this.MonthlyTemp[mo];
            }
            this.JJAtemperature /= 3.0;
        }
        // ------------------------------------------------------------------------------------------------------
        private void CalculateMonthlyData(IEcoregion ecoregion, ClimateRecord[] monthlyClimateRecords, int actualYear, double latitude)
        {
            this.Year = actualYear;

            this.TotalAnnualPrecip = 0.0;
            for (int mo = 0; mo < 12; mo++)
            {
                this.MonthlyMinTemp[mo] = monthlyClimateRecords[mo].AvgMinTemp;
                this.MonthlyMaxTemp[mo] = monthlyClimateRecords[mo].AvgMaxTemp;
                this.MonthlyVarTemp[mo] = monthlyClimateRecords[mo].AvgVarTemp;
                this.MonthlyVarPpt[mo] = monthlyClimateRecords[mo].AvgVarPpt;
                this.MonthlyPrecip[mo] = monthlyClimateRecords[mo].AvgPpt;
                this.MonthlyPAR[mo] = monthlyClimateRecords[mo].AvgPAR;

                this.MonthlyTemp[mo] = (this.MonthlyMinTemp[mo] + this.MonthlyMaxTemp[mo]) / 2.0;

                this.TotalAnnualPrecip += this.MonthlyPrecip[mo];

                this.MonthlyWindDirection[mo] = monthlyClimateRecords[mo].AvgWindDirection;
                this.MonthlyWindSpeed[mo] = monthlyClimateRecords[mo].AvgWindSpeed;
                this.MonthlyNDeposition[mo] = monthlyClimateRecords[mo].AvgNDeposition;
                var hr = CalculateDayLength(mo, latitude);
                this.MonthlyDayLength[mo] = (3600.0 * hr);                  // seconds of daylight/day
                this.MonthlyNightLength[mo] = (3600.0 * (24.0 - hr));         // seconds of nighttime/day
            }

            this.MeanAnnualTemperature = CalculateMeanAnnualTemp(actualYear);
        }
        //This method is not currently being called.  If it is later used, it will need to be checked to make sure it's working properly.
        private void CalculateMonthlyData_AddVariance(IEcoregion ecoregion, ClimateRecord[][] timestepData, int actualYear, double latitude)
        {
            ClimateRecord[] ecoClimate = new ClimateRecord[12];

            this.Year = actualYear;
            this.TotalAnnualPrecip = 0.0;

            //if(timestepData[ecoregion.Index].  ADD MONTH CHECK HERE.

            for (int mo = 0; mo < 12; mo++)
            {
                ecoClimate[mo] = timestepData[ecoregion.Index][mo];

                double MonthlyAvgTemp = (ecoClimate[mo].AvgMinTemp + ecoClimate[mo].AvgMaxTemp) / 2.0;

                double standardDeviation = ecoClimate[mo].StdDevTemp * (Climate.ModelCore.GenerateUniform() * 2.0 - 1.0);

                this.MonthlyTemp[mo] = MonthlyAvgTemp + standardDeviation;
                this.MonthlyMinTemp[mo] = ecoClimate[mo].AvgMinTemp + standardDeviation;
                this.MonthlyMaxTemp[mo] = ecoClimate[mo].AvgMaxTemp + standardDeviation;
                this.MonthlyPrecip[mo] = Math.Max(0.0, ecoClimate[mo].AvgPpt + (ecoClimate[mo].StdDevPpt * (Climate.ModelCore.GenerateUniform() * 2.0 - 1.0)));
                this.MonthlyPAR[mo] = ecoClimate[mo].AvgPAR;
                this.MonthlyWindDirection[mo] = ecoClimate[mo].AvgWindDirection;
                this.MonthlyWindSpeed[mo] = ecoClimate[mo].AvgWindSpeed;
                this.MonthlyNDeposition[mo] = ecoClimate[mo].AvgNDeposition;

                this.TotalAnnualPrecip += this.MonthlyPrecip[mo];

                if (this.MonthlyPrecip[mo] < 0)
                    this.MonthlyPrecip[mo] = 0;

                double hr = CalculateDayLength(mo, latitude);
                this.MonthlyDayLength[mo] = (60.0 * 60.0 * hr);                  // seconds of daylight/day
                this.MonthlyNightLength[mo] = (60.0 * 60.0 * (24 - hr));         // seconds of nighttime/day

            }

            this.MeanAnnualTemperature = CalculateMeanAnnualTemp(actualYear);
        }
Ejemplo n.º 24
0
        //---------------------------------------------------------------------

        protected override Dictionary <int, IClimateRecord[, ]> Parse()
        {
            ReadLandisDataVar();

            Dictionary <int, IClimateRecord[, ]> allData = new Dictionary <int, IClimateRecord[, ]>();

            const string nextTableName = "ClimateTable";


            //---------------------------------------------------------------------
            //Read in climate data:

            ReadName(nextTableName);

            //InputVar<string> ecoregionName = new InputVar<string>("Ecoregion");
            InputVar <int>    ecoregionIndex = new InputVar <int>("Ecoregion Index");
            InputVar <int>    year           = new InputVar <int>("Time step for updating the climate");
            InputVar <int>    month          = new InputVar <int>("The Month");
            InputVar <double> avgMinTemp     = new InputVar <double>("Monthly Minimum Temperature Value");
            InputVar <double> avgMaxTemp     = new InputVar <double>("Monthly Maximum Temperature Value");
            InputVar <double> stdDevTemp     = new InputVar <double>("Monthly Std Deviation Temperature Value");
            InputVar <double> avgPpt         = new InputVar <double>("Monthly Precipitation Value");
            InputVar <double> stdDevPpt      = new InputVar <double>("Monthly Std Deviation Precipitation Value");
            InputVar <double> par            = new InputVar <double>("Monthly Photosynthetically Active Radiation Value");

            while (!AtEndOfInput)
            {
                StringReader currentLine = new StringReader(CurrentLine);

                //ReadValue(ecoregionName, currentLine);
                ReadValue(ecoregionIndex, currentLine);

                //IEcoregion ecoregion = GetEcoregion(ecoregionName.Value);

                ReadValue(year, currentLine);
                int yr = year.Value.Actual;

                if (!allData.ContainsKey(yr))
                {
                    IClimateRecord[,] climateTable = new IClimateRecord[ecoregionDataset.Count, 12];
                    allData.Add(yr, climateTable);
                    //UI.WriteLine("  Climate Parser:  Add new year = {0}.", yr);
                }

                ReadValue(month, currentLine);
                int mo = month.Value.Actual;

                IClimateRecord climateRecord = new ClimateRecord();

                ReadValue(avgMinTemp, currentLine);
                climateRecord.AvgMinTemp = avgMinTemp.Value;

                ReadValue(avgMaxTemp, currentLine);
                climateRecord.AvgMaxTemp = avgMaxTemp.Value;

                ReadValue(stdDevTemp, currentLine);
                climateRecord.StdDevTemp = stdDevTemp.Value;

                ReadValue(avgPpt, currentLine);
                climateRecord.AvgPpt = avgPpt.Value;

                ReadValue(stdDevPpt, currentLine);
                climateRecord.StdDevPpt = stdDevPpt.Value;

                ReadValue(par, currentLine);
                climateRecord.PAR = par.Value;

                allData[yr][ecoregionIndex.Value, mo - 1] = climateRecord;

                //UI.WriteLine(" climateTable avgPpt={0:0.0}.", climateTable[ecoregion.Index, mo-1].AvgPpt);
                //UI.WriteLine(" allData yr={0}, mo={1}, avgPpt={2:0.0}.", yr, mo, allData[yr][ecoregion.Index, mo-1].AvgPpt);

                CheckNoDataAfter("the " + par.Name + " column",
                                 currentLine);

                GetNextLine();
            }

            return(allData);
        }