/// <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>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> /// 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; }
/// <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(); } }