Example #1
0
        private void OnDoWeather(object sender, EventArgs e)
        {
            if (this.doSeek)
            {
                if (!this.OpenDataFile())
                {
                    throw new ApsimXException(this, "Cannot find weather file '" + this.FileName + "'");
                }

                this.doSeek = false;
                this.reader.SeekToDate(this.clock.Today);
            }

            object[] values = this.reader.GetNextLineOfData();

            if (this.clock.Today != this.reader.GetDateFromValues(values))
            {
                throw new Exception("Non consecutive dates found in file: " + this.FileName + ".  Another posibility is that you have two clock objects in your simulation, there should only be one");
            }

            if (this.radiationIndex != -1)
            {
                this.Radn = Convert.ToSingle(values[this.radiationIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                this.Radn = this.reader.ConstantAsDouble("radn");
            }

            if (this.maximumTemperatureIndex != -1)
            {
                this.MaxT = Convert.ToSingle(values[this.maximumTemperatureIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                this.MaxT = this.reader.ConstantAsDouble("maxt");
            }

            if (this.minimumTemperatureIndex != -1)
            {
                this.MinT = Convert.ToSingle(values[this.minimumTemperatureIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                this.MinT = this.reader.ConstantAsDouble("mint");
            }

            if (this.rainIndex != -1)
            {
                this.Rain = Convert.ToSingle(values[this.rainIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                this.Rain = this.reader.ConstantAsDouble("rain");
            }

            if (this.evaporationIndex == -1)
            {
                this.PanEvap = double.NaN;
            }
            else
            {
                this.PanEvap = Convert.ToSingle(values[this.evaporationIndex], CultureInfo.InvariantCulture);
            }

            if (this.rainfallHoursIndex == -1)
            {
                this.RainfallHours = double.NaN;
            }
            else
            {
                this.RainfallHours = Convert.ToSingle(values[this.rainfallHoursIndex], CultureInfo.InvariantCulture);
            }

            if (this.vapourPressureIndex == -1)
            {
                this.VP = Math.Max(0, MetUtilities.svp(this.MinT));
            }
            else
            {
                this.VP = Convert.ToSingle(values[this.vapourPressureIndex], CultureInfo.InvariantCulture);
            }

            if (this.windIndex == -1)
            {
                this.Wind = 3.0;
            }
            else
            {
                this.Wind = Convert.ToSingle(values[this.windIndex], CultureInfo.InvariantCulture);
            }

            if (this.DiffuseFractionIndex == -1)
            {
                this.DiffuseFraction = -1;
            }
            else
            {
                this.DiffuseFraction = Convert.ToSingle(values[this.DiffuseFractionIndex], CultureInfo.InvariantCulture);
            }

            if (this.dayLengthIndex == -1)  // Daylength is not a column - check for a constant
            {
                if (this.reader.Constant("daylength") != null)
                {
                    this.DayLength = this.reader.ConstantAsDouble("daylength");
                }
                else
                {
                    this.DayLength = -1;
                }
            }
            else
            {
                this.DayLength = Convert.ToSingle(values[this.dayLengthIndex], CultureInfo.InvariantCulture);
            }


            if (this.PreparingNewWeatherData != null)
            {
                this.PreparingNewWeatherData.Invoke(this, new EventArgs());
            }

            if (clock.Today.DayOfYear == WinterSolsticeDOY)
            {
                DaysSinceWinterSolstice = 0;
            }
            else
            {
                DaysSinceWinterSolstice += 1;
            }

            Qmax = MetUtilities.QMax(clock.Today.DayOfYear + 1, Latitude, MetUtilities.Taz, MetUtilities.Alpha, VP);
        }
Example #2
0
        /// <summary>Method to read one days met data in from file</summary>
        /// <param name="date">the date to read met data</param>
        private DailyMetDataFromFile GetMetData(DateTime date)
        {
            if (this.doSeek)
            {
                if (!this.OpenDataFile())
                {
                    throw new ApsimXException(this, "Cannot find weather file '" + this.FileName + "'");
                }

                this.doSeek = false;
                this.reader.SeekToDate(date);
            }

            object[] values;

            DailyMetDataFromFile readMetData = new DailyMetDataFromFile();

            try
            {
                values = this.reader.GetNextLineOfData();
            }
            catch (IndexOutOfRangeException err)
            {
                throw new Exception($"Unable to retrieve weather data on {date.ToString("yyy-MM-dd")} in file {FileName}", err);
            }

            if (date != this.reader.GetDateFromValues(values))
            {
                throw new Exception("Non consecutive dates found in file: " + this.FileName + ".  Another posibility is that you have two clock objects in your simulation, there should only be one");
            }

            if (this.radiationIndex != -1)
            {
                readMetData.Radn = Convert.ToSingle(values[this.radiationIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                readMetData.Radn = this.reader.ConstantAsDouble("radn");
            }

            if (this.maximumTemperatureIndex != -1)
            {
                readMetData.MaxT = Convert.ToSingle(values[this.maximumTemperatureIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                readMetData.MaxT = this.reader.ConstantAsDouble("maxt");
            }

            if (this.minimumTemperatureIndex != -1)
            {
                readMetData.MinT = Convert.ToSingle(values[this.minimumTemperatureIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                readMetData.MinT = this.reader.ConstantAsDouble("mint");
            }

            if (this.rainIndex != -1)
            {
                readMetData.Rain = Convert.ToSingle(values[this.rainIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                readMetData.Rain = this.reader.ConstantAsDouble("rain");
            }

            if (this.evaporationIndex == -1)
            {
                readMetData.PanEvap = double.NaN;
            }
            else
            {
                readMetData.PanEvap = Convert.ToSingle(values[this.evaporationIndex], CultureInfo.InvariantCulture);
            }

            if (this.rainfallHoursIndex == -1)
            {
                readMetData.RainfallHours = double.NaN;
            }
            else
            {
                readMetData.RainfallHours = Convert.ToSingle(values[this.rainfallHoursIndex], CultureInfo.InvariantCulture);
            }

            if (this.vapourPressureIndex == -1)
            {
                readMetData.VP = Math.Max(0, MetUtilities.svp(readMetData.MinT));
            }
            else
            {
                readMetData.VP = Convert.ToSingle(values[this.vapourPressureIndex], CultureInfo.InvariantCulture);
            }

            if (this.windIndex == -1)
            {
                readMetData.Wind = 3.0;
            }
            else
            {
                readMetData.Wind = Convert.ToSingle(values[this.windIndex], CultureInfo.InvariantCulture);
            }

            if (this.DiffuseFractionIndex == -1)
            {
                // Estimate Diffuse Fraction using the Approach of Bristow and Campbell
                double Qmax = MetUtilities.QMax(clock.Today.DayOfYear + 1, Latitude, MetUtilities.Taz, MetUtilities.Alpha, 0.0); // Radiation for clear and dry sky (ie low humidity)
                double Q0   = MetUtilities.Q0(clock.Today.DayOfYear + 1, Latitude);
                double B    = Qmax / Q0;
                double Tt   = MathUtilities.Bound(readMetData.Radn / Q0, 0, 1);
                if (Tt > B)
                {
                    Tt = B;
                }
                readMetData.DiffuseFraction = (1 - Math.Exp(0.6 * (1 - B / Tt) / (B - 0.4)));
                if (Tt > 0.5 && readMetData.DiffuseFraction < 0.1)
                {
                    readMetData.DiffuseFraction = 0.1;
                }
            }
            else
            {
                readMetData.DiffuseFraction = Convert.ToSingle(values[this.DiffuseFractionIndex], CultureInfo.InvariantCulture);
            }

            if (this.dayLengthIndex == -1)  // Daylength is not a column - check for a constant
            {
                if (this.reader.Constant("daylength") != null)
                {
                    readMetData.DayLength = this.reader.ConstantAsDouble("daylength");
                }
                else
                {
                    readMetData.DayLength = -1;
                }
            }
            else
            {
                readMetData.DayLength = Convert.ToSingle(values[this.dayLengthIndex], CultureInfo.InvariantCulture);
            }

            return(readMetData);
        }
Example #3
0
        private void OnDoWeather(object sender, EventArgs e)
        {
            if (First)
            {
                TodaysMetData     = GetMetData(this.clock.Today);            //Read first date to get todays data
                YesterdaysMetData = TodaysMetData;                           //Use todays Data to represent yesterday on the first day
                TomorrowsMetData  = GetMetData(this.clock.Today.AddDays(1)); // Read another line ahead to get tomorrows data
            }
            else
            { // Move everything forward a day
                YesterdaysMetData = TodaysMetData;
                TodaysMetData     = TomorrowsMetData;

                if (clock.Today == clock.EndDate && clock.EndDate == reader.LastDate)
                {
                    TomorrowsMetData = TodaysMetData;
                }
                else
                {
                    TomorrowsMetData = GetMetData(this.clock.Today.AddDays(1));
                }
            }

            this.Radn            = TodaysMetData.Radn;
            this.MaxT            = TodaysMetData.MaxT;
            this.MinT            = TodaysMetData.MinT;
            this.Rain            = TodaysMetData.Rain;
            this.PanEvap         = TodaysMetData.PanEvap;
            this.RainfallHours   = TodaysMetData.RainfallHours;
            this.VP              = TodaysMetData.VP;
            this.Wind            = TodaysMetData.Wind;
            this.DiffuseFraction = TodaysMetData.DiffuseFraction;
            this.DayLength       = TodaysMetData.DayLength;

            if (this.PreparingNewWeatherData != null)
            {
                this.PreparingNewWeatherData.Invoke(this, new EventArgs());
            }

            if (First)
            {
                //StartDAWS = met.DaysSinceWinterSolstice;
                if (clock.Today.DayOfYear < WinterSolsticeDOY)
                {
                    if (DateTime.IsLeapYear(clock.Today.Year - 1))
                    {
                        DaysSinceWinterSolstice = 366 - WinterSolsticeDOY + clock.Today.DayOfYear;
                    }
                    else
                    {
                        DaysSinceWinterSolstice = 365 - WinterSolsticeDOY + clock.Today.DayOfYear;
                    }
                }
                else
                {
                    DaysSinceWinterSolstice = clock.Today.DayOfYear - WinterSolsticeDOY;
                }

                First = false;
            }

            if (clock.Today.DayOfYear == WinterSolsticeDOY & First == false)
            {
                DaysSinceWinterSolstice = 0;
            }
            else
            {
                DaysSinceWinterSolstice += 1;
            }

            Qmax = MetUtilities.QMax(clock.Today.DayOfYear + 1, Latitude, MetUtilities.Taz, MetUtilities.Alpha, VP);
        }
Example #4
0
        private void OnDoWeather(object sender, EventArgs e)
        {
            if (this.doSeek)
            {
                if (!this.OpenDataFile())
                {
                    throw new ApsimXException(this, "Cannot find weather file '" + this.FileName + "'");
                }

                this.doSeek = false;
                this.reader.SeekToDate(this.clock.Today);
            }

            object[] values;

            try
            {
                values = this.reader.GetNextLineOfData();
            }
            catch (IndexOutOfRangeException err)
            {
                throw new Exception($"Unable to retrieve weather data on {clock.Today.ToString("yyy-MM-dd")} in file {FileName}", err);
            }

            if (this.clock.Today != this.reader.GetDateFromValues(values))
            {
                throw new Exception("Non consecutive dates found in file: " + this.FileName + ".  Another posibility is that you have two clock objects in your simulation, there should only be one");
            }

            if (this.radiationIndex != -1)
            {
                this.Radn = Convert.ToSingle(values[this.radiationIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                this.Radn = this.reader.ConstantAsDouble("radn");
            }

            if (this.maximumTemperatureIndex != -1)
            {
                this.MaxT = Convert.ToSingle(values[this.maximumTemperatureIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                this.MaxT = this.reader.ConstantAsDouble("maxt");
            }

            if (this.minimumTemperatureIndex != -1)
            {
                this.MinT = Convert.ToSingle(values[this.minimumTemperatureIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                this.MinT = this.reader.ConstantAsDouble("mint");
            }

            if (this.rainIndex != -1)
            {
                this.Rain = Convert.ToSingle(values[this.rainIndex], CultureInfo.InvariantCulture);
            }
            else
            {
                this.Rain = this.reader.ConstantAsDouble("rain");
            }

            if (this.evaporationIndex == -1)
            {
                this.PanEvap = double.NaN;
            }
            else
            {
                this.PanEvap = Convert.ToSingle(values[this.evaporationIndex], CultureInfo.InvariantCulture);
            }

            if (this.rainfallHoursIndex == -1)
            {
                this.RainfallHours = double.NaN;
            }
            else
            {
                this.RainfallHours = Convert.ToSingle(values[this.rainfallHoursIndex], CultureInfo.InvariantCulture);
            }

            if (this.vapourPressureIndex == -1)
            {
                this.VP = Math.Max(0, MetUtilities.svp(this.MinT));
            }
            else
            {
                this.VP = Convert.ToSingle(values[this.vapourPressureIndex], CultureInfo.InvariantCulture);
            }

            if (this.windIndex == -1)
            {
                this.Wind = 3.0;
            }
            else
            {
                this.Wind = Convert.ToSingle(values[this.windIndex], CultureInfo.InvariantCulture);
            }

            if (this.DiffuseFractionIndex == -1)
            {
                this.DiffuseFraction = -1;
            }
            else
            {
                this.DiffuseFraction = Convert.ToSingle(values[this.DiffuseFractionIndex], CultureInfo.InvariantCulture);
            }

            if (this.dayLengthIndex == -1)  // Daylength is not a column - check for a constant
            {
                if (this.reader.Constant("daylength") != null)
                {
                    this.DayLength = this.reader.ConstantAsDouble("daylength");
                }
                else
                {
                    this.DayLength = -1;
                }
            }
            else
            {
                this.DayLength = Convert.ToSingle(values[this.dayLengthIndex], CultureInfo.InvariantCulture);
            }


            if (this.PreparingNewWeatherData != null)
            {
                this.PreparingNewWeatherData.Invoke(this, new EventArgs());
            }

            if (First)
            {
                //StartDAWS = met.DaysSinceWinterSolstice;
                if (clock.Today.DayOfYear < WinterSolsticeDOY)
                {
                    if (DateTime.IsLeapYear(clock.Today.Year))
                    {
                        DaysSinceWinterSolstice = 366 - WinterSolsticeDOY + clock.Today.DayOfYear - 1;  //minus 1 as we set the first day as zero
                    }
                    else
                    {
                        DaysSinceWinterSolstice = 365 - WinterSolsticeDOY + clock.Today.DayOfYear - 1;
                    }
                }
                else
                {
                    DaysSinceWinterSolstice = clock.Today.DayOfYear - WinterSolsticeDOY;
                }

                First = false;
            }

            if (clock.Today.DayOfYear == WinterSolsticeDOY & First == false)
            {
                DaysSinceWinterSolstice = 0;
            }
            else
            {
                DaysSinceWinterSolstice += 1;
            }

            Qmax = MetUtilities.QMax(clock.Today.DayOfYear + 1, Latitude, MetUtilities.Taz, MetUtilities.Alpha, VP);
        }