Example #1
0
        /// <summary>Called after the user has selected a new met file.</summary>
        /// <param name="fileName">Name of the file.</param>
        public void OnBrowse(string fileName)
        {
            bool isCsv = Path.GetExtension(fileName) == ".csv";

            this.weatherDataView.ShowConstantsFile(isCsv);
            if (this.weatherData.FullFileName != PathUtilities.GetAbsolutePath(fileName, this.explorerPresenter.ApsimXFile.FileName))
            {
                if (ExcelUtilities.IsExcelFile(fileName))
                {
                    // Extend height of Browse Panel to show Drop Down for Sheet names
                    this.weatherDataView.ShowExcelSheets(true);
                    this.sheetNames = ExcelUtilities.GetWorkSheetNames(fileName);
                    this.weatherDataView.PopulateDropDownData(this.sheetNames);

                    // We want to attempt to update the table/summary now. This may fail if the
                    // sheet name is incorrect/not set.
                    this.WriteTableAndSummary(fileName);
                }
                else
                {
                    // Shrink Browse Panel so that the sheet name dropdown doesn't show
                    this.weatherDataView.ShowExcelSheets(false);

                    // as a precaution, set this to nothing
                    this.weatherData.ExcelWorkSheetName = string.Empty;
                    this.WriteTableAndSummary(fileName);
                }
            }
        }
Example #2
0
        /// <summary>Called after the user has selected a new met file.</summary>
        /// <param name="fileName">Name of the file.</param>
        public void OnBrowse(string fileName)
        {
            if (this.weatherData.FullFileName != PathUtilities.GetAbsolutePath(fileName, this.explorerPresenter.ApsimXFile.FileName))
            {
                if (ExcelUtilities.IsExcelFile(fileName))
                {
                    //// Extend height of Browse Panel to show Drop Down for Sheet names
                    this.weatherDataView.ShowExcelSheets(true);
                    this.sheetNames = ExcelUtilities.GetWorkSheetNames(fileName);
                    this.weatherDataView.PopulateDropDownData(this.sheetNames);

                    // the following is not required here as it happens when the sheet name is changed
                    // this.WriteTableAndSummary(fileName);
                }
                else
                {
                    //// Shrink Browse Panel so that the sheet name dropdown doesn't show
                    this.weatherDataView.ShowExcelSheets(false);

                    // as a precaution, set this to nothing
                    this.weatherData.ExcelWorkSheetName = string.Empty;
                    this.WriteTableAndSummary(fileName);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Get data from the weather file and present it to the view as both a table and a summary
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="sheetName">The name of the sheet</param>
        private void WriteTableAndSummary(string filename, string sheetName = "")
        {
            // Clear any previos error message
            this.explorerPresenter.MainPresenter.ShowMessage(" ", Simulation.MessageType.Information);
            // Clear any previous summary
            this.weatherDataView.Summarylabel = string.Empty;
            this.weatherDataView.GraphSummary.Clear();
            this.weatherDataView.GraphSummary.Refresh();
            this.weatherDataView.GraphRainfall.Clear();
            this.weatherDataView.GraphRainfall.Refresh();
            this.weatherDataView.GraphMonthlyRainfall.Clear();
            this.weatherDataView.GraphMonthlyRainfall.Refresh();
            this.weatherDataView.GraphTemperature.Clear();
            this.weatherDataView.GraphTemperature.Refresh();
            this.weatherDataView.GraphRadiation.Clear();
            this.weatherDataView.GraphRadiation.Refresh();
            this.graphMetData = new DataTable();
            if (filename != null)
            {
                this.weatherDataView.Filename = PathUtilities.GetAbsolutePath(filename, this.explorerPresenter.ApsimXFile.FileName);
                try
                {
                    if (ExcelUtilities.IsExcelFile(filename))
                    {
                        // Extend height of Browse Panel to show Drop Down for Sheet names
                        this.weatherDataView.ShowExcelSheets(true);
                        if (this.sheetNames == null)
                        {
                            this.sheetNames = ExcelUtilities.GetWorkSheetNames(filename);
                            this.weatherDataView.ExcelSheetChangeClicked -= this.ExcelSheetValueChanged;
                            this.weatherDataView.PopulateDropDownData(this.sheetNames);
                            this.weatherDataView.ExcelSheetChangeClicked += this.ExcelSheetValueChanged;
                        }
                    }
                    else
                    {
                        // Shrink Browse Panel so that the sheet name dropdown doesn't show
                        this.weatherDataView.ShowExcelSheets(false);
                    }

                    ViewBase.MasterView.WaitCursor = true;
                    try
                    {
                        this.weatherData.ExcelWorkSheetName = sheetName;
                        string newFileName = PathUtilities.GetAbsolutePath(filename, this.explorerPresenter.ApsimXFile.FileName);
                        var    changes     = new List <ChangeProperty.Property>();
                        if (weatherData.FullFileName != newFileName)
                        {
                            changes.Add(new ChangeProperty.Property(weatherData, nameof(weatherData.FullFileName), newFileName));
                        }
                        // Set constants file name to null iff the new file name is not a csv file.
                        if (Path.GetExtension(newFileName) != ".csv" && weatherData.ConstantsFile != null)
                        {
                            changes.Add(new ChangeProperty.Property(weatherData, nameof(weatherData.ConstantsFile), null));
                        }
                        if (changes.Count > 0)
                        {
                            ICommand changeFileName = new ChangeProperty(changes);
                            explorerPresenter.CommandHistory.Add(new ChangeProperty(changes));
                        }
                        using (DataTable data = this.weatherData.GetAllData())
                        {
                            this.dataStartDate = this.weatherData.StartDate;
                            this.dataEndDate   = this.weatherData.EndDate;
                            this.WriteTable(data);
                            this.WriteSummary(data);
                            this.DisplayDetailedGraphs(data);
                        }
                    }
                    catch (Exception err)
                    {
                        explorerPresenter.MainPresenter.ShowError(err);
                    }
                    finally
                    {
                        ViewBase.MasterView.WaitCursor = false;
                        this.weatherData.CloseDataFile();
                    }
                }
                catch (Exception err)
                {
                    string message = err.Message;
                    message += "\r\n" + err.StackTrace;
                    this.weatherDataView.Summarylabel = err.Message;
                    this.explorerPresenter.MainPresenter.ShowError(err);
                }
            }

            // this.weatherDataView.Filename = PathUtilities.GetRelativePath(filename, this.explorerPresenter.ApsimXFile.FileName);
            this.weatherDataView.Filename           = PathUtilities.GetAbsolutePath(filename, this.explorerPresenter.ApsimXFile.FileName);
            this.weatherDataView.ConstantsFileName  = weatherData.ConstantsFile;
            this.weatherDataView.ExcelWorkSheetName = sheetName;
        }
Example #4
0
        /// <summary>
        /// Open the weather data file.
        /// </summary>
        /// <returns>True if the file was successfully opened</returns>
        public bool OpenDataFile()
        {
            if (!System.IO.File.Exists(this.FullFileName) &&
                System.IO.Path.GetExtension(FullFileName) == string.Empty)
            {
                FileName += ".met";
            }

            if (System.IO.File.Exists(this.FullFileName))
            {
                if (this.reader == null)
                {
                    if (ExcelUtilities.IsExcelFile(FullFileName) && string.IsNullOrEmpty(ExcelWorkSheetName))
                    {
                        throw new Exception($"Unable to open excel file {FullFileName}: no sheet name is specified");
                    }

                    this.reader = new ApsimTextFile();
                    this.reader.Open(this.FullFileName, this.ExcelWorkSheetName);

                    if (this.reader.Headings == null)
                    {
                        string message = "Cannot find the expected header in ";
                        if (ExcelUtilities.IsExcelFile(FullFileName))
                        {
                            message += $"sheet '{ExcelWorkSheetName}' of ";
                        }
                        message += $"weather file: {FullFileName}";
                        throw new Exception(message);
                    }

                    this.maximumTemperatureIndex = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "Maxt");
                    this.minimumTemperatureIndex = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "Mint");
                    this.radiationIndex          = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "Radn");
                    this.rainIndex            = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "Rain");
                    this.evaporationIndex     = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "Evap");
                    this.rainfallHoursIndex   = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "RainHours");
                    this.vapourPressureIndex  = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "VP");
                    this.windIndex            = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "Wind");
                    this.DiffuseFractionIndex = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "DifFr");
                    this.dayLengthIndex       = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "DayLength");

                    if (!string.IsNullOrEmpty(ConstantsFile))
                    {
                        ApsimTextFile constantsReader = new ApsimTextFile();
                        constantsReader.Open(ConstantsFile);
                        if (constantsReader.Constants != null)
                        {
                            foreach (ApsimConstant constant in constantsReader.Constants)
                            {
                                this.reader.AddConstant(constant.Name, constant.Value, constant.Units, constant.Comment);
                            }
                        }
                    }

                    if (this.maximumTemperatureIndex == -1)
                    {
                        if (this.reader == null || this.reader.Constant("maxt") == null)
                        {
                            throw new Exception("Cannot find MaxT in weather file: " + this.FullFileName);
                        }
                    }

                    if (this.minimumTemperatureIndex == -1)
                    {
                        if (this.reader == null || this.reader.Constant("mint") == null)
                        {
                            throw new Exception("Cannot find MinT in weather file: " + this.FullFileName);
                        }
                    }

                    if (this.radiationIndex == -1)
                    {
                        if (this.reader == null || this.reader.Constant("radn") == null)
                        {
                            throw new Exception("Cannot find Radn in weather file: " + this.FullFileName);
                        }
                    }

                    if (this.rainIndex == -1)
                    {
                        if (this.reader == null || this.reader.Constant("rain") == null)
                        {
                            throw new Exception("Cannot find Rain in weather file: " + this.FullFileName);
                        }
                    }
                }
                else
                {
                    if (this.reader.IsExcelFile != true)
                    {
                        this.reader.SeekToDate(this.reader.FirstDate);
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }