public void ConvertSoilToDataTable() { Soil Soil = new Soil(); Soil.ApsoilNumber = "100"; Soil.Water.Thickness = new double[] { 150.00, 150.00, 300.00, 300.00, 300.00 }; Soil.Water.BD = new double[] { 1.02, 1.03, 1.02, 1.02, 1.06 }; Soil.Water.BDMetadata = new string[] { "Field measured and checked for sensibility", "Laboratory measured", "", "", "" }; Soil.Water.AirDry = new double[] { 0.15, 0.26, 0.29, 0.29, 0.30 }; Soil.Water.LL15 = new double[] { 0.29, 0.29, 0.29, 0.29, 0.30 }; Soil.Water.DUL = new double[] { 0.54, 0.53, 0.54, 0.54, 0.52 }; Soil.Water.SAT = new double[] { 0.59, 0.58, 0.59, 0.58, 0.57 }; Soil.Water.Crops.Add(new SoilCrop() { Name = "Barley", Thickness = new double[] { 100.00, 100.00 }, LL = new double[] { 0.29, 0.25 }, KL = new double[] { 0.10, 0.08 }, XF = new double[] { 1.00, 1.00 } }); Soil.Water.Crops.Add(new SoilCrop() { Name = "Chickpea", Thickness = new double[] { 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00 }, LL = new double[] { 0.29, 0.29, 0.36, 0.43, 0.51, 0.50, 0.50, 0.48 }, KL = new double[] { 0.10, 0.10, 0.08, 0.06, 0.04, 0.02, 0.01, 0.00 }, XF = new double[] { 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 0.00 } }); Soil.SoilOrganicMatter.Thickness = new double[] { 200.000, 200.00 }; Soil.SoilOrganicMatter.OC = new double[] { 1.040, 0.89 }; Soil.SoilOrganicMatter.FBiom = new double[] { 0.025, 0.02 }; Soil.SoilOrganicMatter.FInert = new double[] { 0.400, 0.89 }; Soil.InitialWater = new InitialWater(); Soil.InitialWater.SetSW(0.5, InitialWater.PercentMethodEnum.FilledFromTop); Soil.SoilWater = new SoilWater(); Soil.SoilWater.SummerCona = 3.5; Soil.SoilWater.WinterCona = 3.5; Soil.Analysis.Thickness = new double[] { 100, 100, 100 }; Soil.Analysis.EC = new double[] { 1.7, double.NaN, double.NaN }; DataTable Table = new DataTable(); SoilDataTable.SoilToTable(Soil, Table); Assert.AreEqual(DataTableUtility.GetColumnAsDoubles(Table, "Thickness (mm)"), Soil.Water.Thickness, 1); Assert.AreEqual(DataTableUtility.GetColumnAsDoubles(Table, "BD (g/cc)"), Soil.Water.BD, 0.01); Assert.AreEqual(DataTableUtility.GetColumnAsStrings(Table, "BDCode")[0], "FM"); Assert.AreEqual(DataTableUtility.GetColumnAsStrings(Table, "BDCode")[1], "LM"); Assert.AreEqual(DataTableUtility.GetColumnAsDoubles(Table, "Chickpea ll (mm/mm)"), new double[] { 0.29, 0.29, 0.36, 0.43, 0.51, 0.50, 0.50, 0.48 }, 0.01); Assert.AreEqual(DataTableUtility.GetColumnAsDoubles(Table, "Chickpea xf (0-1)"), new double[] { 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 0.00 }, 0.01); }
static private double[] GetDoubleValues(DataTable Table, string FieldName, int NumValues, int Row, int DecPlaces) { // Get string value from specified table for specified field. if (Table.Columns.IndexOf(FieldName) != -1) { return(MathUtility.Round(DataTableUtility.GetColumnAsDoubles(Table, FieldName, NumValues, Row), DecPlaces)); } else { return(new double[0]); } }
/// <summary> /// Return an array of values for the specified column. /// </summary> private static double[] GetDoubleValues(DataTable Table, string VariableName, int NumRows, int StartRow) { if (Table.Columns.Contains(VariableName)) { double[] Values = DataTableUtility.GetColumnAsDoubles(Table, VariableName, NumRows, StartRow); if (MathUtility.ValuesInArray(Values)) { // Convert MissingValue for Nan for (int i = 0; i != Values.Length; i++) { if (Values[i] == MathUtility.MissingValue) { Values[i] = double.NaN; } } return(Values); } } return(null); }
/// <summary> /// A helper method to add a series to the chart. /// </summary> public Series AddSeries(DataTable Table, string ColumnName, Series Line) { if (Table.Columns[ColumnName].DataType != typeof(string)) { double[] x = DataTableUtility.GetColumnAsDoubles(Table, ColumnName); // go add series if necessary. if (MathUtility.ValuesInArray(x)) { string Title = ColumnName; Line.DataSource = Table; Line.Active = NoSeriesIsActive; Line.HorizAxis = HorizontalAxis.Top; Line.Title = Title; AddSeries(Line, Table.TableName, ColumnName, "DepthMidPoints (mm)"); return(Line); } } return(null); }
/// <summary> /// Populate all series with data. /// </summary> public void PopulateSeries() { bool LeftAxisUsed = false; bool RightAxisUsed = false; bool BottomAxisUsed = false; bool TopAxisUsed = false; int SeriesIndex = 1; foreach (Series S in Chart.Series) { if (S.HorizAxis == HorizontalAxis.Bottom) { BottomAxisUsed = true; } else { TopAxisUsed = true; } if (S.VertAxis == VerticalAxis.Left) { LeftAxisUsed = true; } else { RightAxisUsed = true; } // Read in XML about this series. XmlDocument Doc = new XmlDocument(); Doc.LoadXml(S.ValuesLists[0].DataMember); string DataSource = XmlHelper.Value(Doc.DocumentElement, "DataSource"); List <string> ColumnNames = new List <string>(); ColumnNames.Add(XmlHelper.Value(Doc.DocumentElement, "X")); ColumnNames.Add(XmlHelper.Value(Doc.DocumentElement, "Y")); ColumnNames.Add(XmlHelper.Value(Doc.DocumentElement, "Y2")); ColumnNames.Add(XmlHelper.Value(Doc.DocumentElement, "Y3")); ColumnNames.Add(XmlHelper.Value(Doc.DocumentElement, "Y4")); DataTable Table = GetDataSourceWithName(DataSource); for (int i = 0; i < S.ValuesLists.Count; i++) { S.ValuesLists[i].Clear(); if (Table != null && i < ColumnNames.Count) { string ColumnName = ColumnNames[i]; if (Table.Columns.Contains(ColumnName)) { if (i == 0 && Table.Columns[ColumnName].DataType == typeof(string)) { S.Labels.Clear(); S.Labels.AddRange(DataTableUtility.GetColumnAsStrings(Table, ColumnName)); } else if (Table.Columns[ColumnName].DataType == typeof(double) || Table.Columns[ColumnName].DataType == typeof(float)) { S.ValuesLists[i].Value = DataTableUtility.GetColumnAsDoubles(Table, ColumnName); S.ValuesLists[i].Count = S.ValuesLists[i].Value.Length; } else if (Table.Columns[ColumnName].DataType == typeof(DateTime)) { DateTime[] Dates = DataTableUtility.GetColumnAsDates(Table, ColumnName); double[] Values = new double[Dates.Length]; for (int j = 0; j < Values.Length; j++) { Values[j] = Dates[j].ToOADate(); } S.ValuesLists[i].Value = Values; S.ValuesLists[i].Count = S.ValuesLists[i].Value.Length; S.ValuesLists[i].DateTime = true; } } else { S.ValuesLists[i].Value = MathUtility.CreateArrayOfValues(SeriesIndex, Table.Rows.Count); S.ValuesLists[i].Count = S.ValuesLists[i].Value.Length; SeriesIndex++; } } } // Strip out missing values. StripOutMissingValues(S); Chart.Axes.Left.Visible = LeftAxisUsed; Chart.Axes.Right.Visible = RightAxisUsed; Chart.Axes.Top.Visible = TopAxisUsed; Chart.Axes.Bottom.Visible = BottomAxisUsed; } }