/// <summary> /// A helper to cleanly get a DataTable from the contents of a file. /// </summary> /// <param name="fileName">Name of the file.</param> /// <returns>The data table.</returns> public static DataTable ToTable(string fileName) { ApsimTextFile file = new ApsimTextFile(); try { file.Open(fileName); DataTable data = file.ToTable(); data.TableName = Path.GetFileNameWithoutExtension(fileName); return(data); } finally { file.Close(); } }
/// <summary> /// Return a datatable for this input file. Returns null if no data. /// </summary> /// <returns></returns> public DataTable GetTable() { DataTable returnDataTable = null; string fullFileName = FullFileName; if (fullFileName != null) { if (File.Exists(fullFileName)) { ApsimTextFile textFile = new ApsimTextFile(); try { textFile.Open(fullFileName); } catch (Exception err) { ErrorMessage = err.Message; return null; } DataTable table = textFile.ToTable(); textFile.Close(); if (returnDataTable == null) returnDataTable = table; else returnDataTable.Merge(table); } else { ErrorMessage = "The specified file does not exist."; } } else { ErrorMessage = "Please select a file to use."; } return returnDataTable; }
private void OnSimulationCompleted(object sender, EventArgs e) { if (this.reader != null) { this.reader.Close(); this.reader = null; } }
/// <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)) { if (this.reader == null) { this.reader = new ApsimTextFile(); this.reader.Open(this.FullFileName, this.ExcelWorkSheetName); 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.vapourPressureIndex = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "VP"); this.windIndex = StringUtilities.IndexOfCaseInsensitive(this.reader.Headings, "Wind"); 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; } }
/// <summary> /// Get the DataTable view of this data /// </summary> /// <returns>The DataTable</returns> public DataTable GetAllData() { this.reader = null; if (this.OpenDataFile()) { List<string> metProps = new List<string>(); metProps.Add("mint"); metProps.Add("maxt"); metProps.Add("radn"); metProps.Add("rain"); metProps.Add("wind"); return this.reader.ToTable(metProps); } else { return null; } }
/// <summary>Close the datafile.</summary> public void CloseDataFile() { if (reader != null) { reader.Close(); reader = null; } }
/// <summary>Extracts weather data from calling a url and returns an ApsimTextFile.</summary> /// <param name="url">The url to call</param> private static ApsimTextFile ExtractDataFromURL(string url) { MemoryStream stream = WebUtilities.ExtractDataFromURL(url); if (stream != null) { // Convert the memory stream to a data table. stream.Seek(0, SeekOrigin.Begin); ApsimTextFile inputFile = new ApsimTextFile(); inputFile.Open(stream); return inputFile; } return null; }
/// <summary>Fills the auto-calculated fields.</summary> /// <param name="paddock">The paddock.</param> /// <param name="observedData">The observed data.</param> /// <param name="weatherData">The weather data.</param> public static void FillInCalculatedFields(Paddock paddock, DataTable observedData, string workingFolder) { IEnumerable<Tillage> tillages = paddock.Management.OfType<Tillage>(); if (tillages.Count() > 0) paddock.StubbleIncorporatedPercent = YieldProphetUtility.CalculateAverageTillagePercent(tillages); DateTime lastRainfallDate = GetLastRainfallDate(observedData); if (lastRainfallDate != DateTime.MinValue) paddock.DateOfLastRainfallEntry = lastRainfallDate.ToString("dd/MM/yyyy"); string[] metFiles = Directory.GetFiles(workingFolder, "*.met"); if (metFiles.Length > 0) { string firstMetFile = Path.Combine(workingFolder, metFiles[0]); ApsimTextFile textFile = new ApsimTextFile(); textFile.Open(firstMetFile); DataTable data = textFile.ToTable(); textFile.Close(); paddock.RainfallSinceSoilWaterSampleDate = SumTableAfterDate(data, "Rain", paddock.SoilWaterSampleDate); if (data.Rows.Count > 0) { DataRow lastweatherRow = data.Rows[data.Rows.Count - 1]; paddock.LastClimateDate = DataTableUtilities.GetDateFromRow(lastweatherRow); } } }
/// <summary>Get a rainfall forecast for the specified station number.</summary> /// <param name="stationNumber">The SILO station number.</param> /// <returns>A datatable with date and rain as columns.</returns> public DataTable GetDataTable(int stationNumber, DateTime nowDate, bool rainOnly) { Stream inStream = Get(stationNumber, nowDate, rainOnly); if (inStream != null) { // Read in the forecast data. ApsimTextFile f = new ApsimTextFile(); f.Open(inStream); return f.ToTable(); } else return null; }
public void WeatherTests_EnsureFullPOAMAWorks() { DataTable observedData = new DataTable(); observedData.Columns.Add("Date", typeof(DateTime)); observedData.Columns.Add("Rain", typeof(double)); observedData.Columns.Add("Codes", typeof(string)); observedData.Rows.Add(new object[] { new DateTime(2016, 5, 3), 20.0, "O" }); observedData.Rows.Add(new object[] { new DateTime(2016, 5, 4), 30.0, "O" }); observedData.Rows.Add(new object[] { new DateTime(2016, 5, 5), 40.0, "O" }); observedData.Rows.Add(new object[] { new DateTime(2016, 5, 6), 0.0, "O" }); string tempPOAMAFolder = Path.Combine(Path.GetTempPath(), "POAMA"); Weather.CreatePOAMA(Path.Combine(tempPOAMAFolder, "poama.met"), 77008, new DateTime(2016,4,1), new DateTime(2016, 5, 10), observedData); // **** Check 2016 file. ApsimTextFile metFile1 = new ApsimTextFile(); metFile1.Open(Path.Combine(tempPOAMAFolder, "poama2016.met")); DataTable data1 = metFile1.ToTable(); // Make sure observed data was patched and code is correct. Assert.AreEqual(data1.Rows[32][0], new DateTime(2016, 5, 3)); Assert.AreEqual(data1.Rows[32][4], 20.0f); Assert.AreEqual(data1.Rows[32][5], "ssso"); // Make sure POAMA data was added. Assert.AreEqual(data1.Rows[40][0], new DateTime(2016, 5, 11)); Assert.AreEqual(data1.Rows[40][4], 0.0f); Assert.AreEqual(data1.Rows[40][5], "PPPP"); metFile1.Close(); // **** Check 2017 file. ApsimTextFile metFile2 = new ApsimTextFile(); metFile2.Open(Path.Combine(tempPOAMAFolder, "poama2017.met")); DataTable data2 = metFile2.ToTable(); // Make sure observed data was patched and code is correct. Assert.AreEqual(data2.Rows[32][0], new DateTime(2016, 5, 3)); Assert.AreEqual(data2.Rows[32][4], 20.0f); Assert.AreEqual(data2.Rows[32][5], "ssso"); // Make sure POAMA data was added. Assert.AreEqual(data2.Rows[40][0], new DateTime(2016, 5, 11)); Assert.AreEqual(data2.Rows[40][4], 0.0f); Assert.AreEqual(data2.Rows[40][5], "PPPP"); metFile2.Close(); Directory.Delete(tempPOAMAFolder, true); }
public void WeatherTests_EnsureLongtermWorks() { DataTable observedData = new DataTable(); observedData.Columns.Add("Date", typeof(DateTime)); observedData.Columns.Add("Rain", typeof(double)); observedData.Columns.Add("Codes", typeof(string)); observedData.Rows.Add(new object[] { new DateTime(2016, 5, 3), 20.0, "O" }); observedData.Rows.Add(new object[] { new DateTime(2016, 5, 4), 30.0, "O" }); observedData.Rows.Add(new object[] { new DateTime(2016, 5, 5), 40.0, "O" }); observedData.Rows.Add(new object[] { new DateTime(2016, 5, 6), 0.0, "O" }); string tempFolder = Path.Combine(Path.GetTempPath(), "Longterm"); Weather.CreateLongTerm(Path.Combine(tempFolder, "longterm.met"), 77008, new DateTime(2016, 4, 1), new DateTime(2016, 5, 10), observedData, DateTime.MinValue, 30); // **** Check 2016 file. ApsimTextFile metFile1 = new ApsimTextFile(); metFile1.Open(Path.Combine(tempFolder, "longterm1986.met")); DataTable data1 = metFile1.ToTable(); // Make sure observed data was patched and code is correct. Assert.AreEqual(data1.Rows[32][0], new DateTime(2016, 5, 3)); Assert.AreEqual(data1.Rows[32][4], 20.0f); Assert.AreEqual(data1.Rows[32][7], "ssso--"); Assert.AreEqual(data1.Rows[40][0], new DateTime(2016, 5, 11)); Assert.AreEqual(data1.Rows[40][4], 0.0f); Assert.AreEqual(data1.Rows[40][7], "SSSS--"); metFile1.Close(); // **** Check 2017 file. ApsimTextFile metFile2 = new ApsimTextFile(); metFile2.Open(Path.Combine(tempFolder, "longterm1987.met")); DataTable data2 = metFile2.ToTable(); // Make sure observed data was patched and code is correct. Assert.AreEqual(data2.Rows[32][0], new DateTime(2016, 5, 3)); Assert.AreEqual(data2.Rows[32][4], 20.0f); Assert.AreEqual(data2.Rows[32][7], "ssso--"); Assert.AreEqual(data2.Rows[40][0], new DateTime(2016, 5, 11)); Assert.AreEqual(data2.Rows[40][4], 0.0f); Assert.AreEqual(data2.Rows[40][7], "SSSS--"); metFile2.Close(); Directory.Delete(tempFolder, true); }
/// <summary>Concatenates the specified output files into one file.</summary> /// <param name="outFiles">The out files.</param> private static void ConcatenateOutputFiles(string[] outFiles, string fileName, string outputFileType) { if (outFiles.Length > 0) { // Assume they are all structured the same i.e. same headings and units. // Read in data from all files. DataTable allData = null; foreach (string outputFileName in outFiles) { ApsimTextFile reader = new ApsimTextFile(); reader.Open(outputFileName); List<string> constantsToAdd = new List<string>(); constantsToAdd.Add("Title"); DataTable data = reader.ToTable(constantsToAdd); reader.Close(); if (data.Columns.Count > 0 && data.Rows.Count > 0) { if (allData == null) allData = data; else DataTableUtilities.CopyRows(data, allData); } } if (allData != null) { // Move the title column to be first. allData.Columns["Title"].SetOrdinal(0); // Strip off the outputFileType (e.g. Yearly) from the titles. foreach (DataRow row in allData.Rows) row["Title"] = row["Title"].ToString().Replace(outputFileType, ""); // Write data. string workingFolder = Path.GetDirectoryName(outFiles[0]); string singleOutputFileName = Path.Combine(workingFolder, fileName); StreamWriter outWriter = new StreamWriter(singleOutputFileName); DataTableUtilities.DataTableToText(allData, 0, ", ", true, outWriter); outWriter.Close(); } // Delete the .out files. foreach (string outputFileName in outFiles) File.Delete(outputFileName); } }
/// <summary> /// A helper to cleanly get a DataTable from the contents of a file. /// </summary> /// <param name="fileName">Name of the file.</param> /// <returns>The data table.</returns> public static DataTable ToTable(string fileName) { ApsimTextFile file = new ApsimTextFile(); try { file.Open(fileName); DataTable data = file.ToTable(); data.TableName = Path.GetFileNameWithoutExtension(fileName); return data; } finally { file.Close(); } }