// ---------------------------------------------------------- // Get string value from specified table for specified field. // ---------------------------------------------------------- static private string GetStringValue(string FileName, string SectionName, string Key) { string Value = ApsimFile.IniFile.INIRead(FileName, SectionName, Key); StringManip.SplitOffAfterDelimiter(ref Value, "!"); StringManip.SplitOffAfterDelimiter(ref Value, "("); Value = Value.Replace("\t", " "); if (Value.StartsWith("$")) { Value = ResolveVariableMacro(FileName, Value); } return(Value); }
static private string GetStringValue(string FileName, string SectionName, string Key) { // ---------------------------------------------------------- // Get string value from specified table for specified field. // ---------------------------------------------------------- string Value = IniFile.INIRead(FileName, SectionName, Key); StringManip.SplitOffAfterDelimiter(ref Value, "!"); StringManip.SplitOffAfterDelimiter(ref Value, "("); Value = Value.Replace("\t", " "); if (Value.StartsWith("$")) { Value = ResolveVariableMacro(FileName, Value); } return(Value); }
// ------------------------------------------------------------ // Resolve the specified macro to a name and return its value. // NB It looks in a '*variables' section. // ------------------------------------------------------------ static private string ResolveVariableMacro(string FileName, string MacroName) { string MacroLine = ApsimFile.IniFile.INIRead(FileName, "*variables", MacroName); StringManip.SplitOffAfterDelimiter(ref MacroLine, "!"); if (MacroLine == "") { throw new Exception("Cannot resolve macro: " + MacroName + " in file:" + FileName); } StringCollection MacroBits = StringManip.SplitStringHonouringQuotes(MacroLine, " "); if (MacroBits.Count != 3) { throw new Exception("Invalid variables line: " + MacroLine); } return(MacroBits[1]); }
static private double[] GetDoubleValues(string FileName, string SectionName, string Key) { // ---------------------------------------------------------- // Get string value from specified table for specified field. // ---------------------------------------------------------- string Value = IniFile.INIRead(FileName, SectionName, Key); StringManip.SplitOffAfterDelimiter(ref Value, "!"); StringManip.SplitOffAfterDelimiter(ref Value, "("); Value = Value.Replace("\t", " "); if (Value == "") { return(new double[0]); } else { StringCollection Values = StringManip.SplitStringHonouringQuotes(Value, " "); double[] ReturnValues = new double[Values.Count]; for (int i = 0; i != Values.Count; i++) { if (Values[i].StartsWith("$")) { Values[i] = ResolveVariableMacro(FileName, Values[i]); } try { ReturnValues[i] = Convert.ToDouble(Values[i]); } catch (Exception) { throw new Exception("Cannot convert value to a floating point number" + ". Filename: " + FileName + ". Section: " + SectionName + ". Key: " + Key); } } return(ReturnValues); } }
static private string ResolveVariableMacro(string FileName, string MacroName) { // ------------------------------------------------------------ // Resolve the specified macro to a name and return its value. // NB It looks in a '*variables' section. // ------------------------------------------------------------ string MacroLine = IniFile.INIRead(FileName, "*variables", MacroName); StringManip.SplitOffAfterDelimiter(ref MacroLine, "!"); if (MacroLine == "") { throw new Exception("Cannot resolve macro: " + MacroName + " in file:" + FileName); } StringCollection MacroBits = StringManip.SplitStringHonouringQuotes(MacroLine, " "); if (MacroBits.Count != 3) { throw new Exception("Invalid variables line: " + MacroLine); } return(MacroBits[1]); }
/// <summary> /// Read in the APSIM header - headings/units and constants. /// </summary> private void ReadApsimHeader(StreamReaderRandomAccess In) { StringCollection ConstantLines = new StringCollection(); StringCollection HeadingLines = new StringCollection(); ReadApsimHeaderLines(In, ref ConstantLines, ref HeadingLines); bool TitleFound = false; foreach (string ConstantLine in ConstantLines) { string Line = ConstantLine; string Comment = StringManip.SplitOffAfterDelimiter(ref Line, "!"); Comment.Trim(); int PosEquals = Line.IndexOf('='); if (PosEquals != -1) { string Name = Line.Substring(0, PosEquals).Trim(); if (Name.ToLower() == "title") { TitleFound = true; Name = "Title"; } string Value = Line.Substring(PosEquals + 1).Trim(); string Unit = StringManip.SplitOffBracketedValue(ref Value, '(', ')'); _Constants.Add(new APSIMConstant(Name, Value, Unit, Comment)); } } if (HeadingLines.Count >= 2) { if (CSV) { HeadingLines[0] = HeadingLines[0].TrimEnd(','); HeadingLines[1] = HeadingLines[1].TrimEnd(','); Headings = new StringCollection(); Units = new StringCollection(); Headings.AddRange(HeadingLines[0].Split(",".ToCharArray())); Units.AddRange(HeadingLines[1].Split(",".ToCharArray())); for (int i = 0; i < Headings.Count; i++) { Headings[i] = Headings[i].Trim(); } for (int i = 0; i < Units.Count; i++) { Units[i] = Units[i].Trim(); } } else { Headings = StringManip.SplitStringHonouringQuotes(HeadingLines[0], " \t"); Units = StringManip.SplitStringHonouringQuotes(HeadingLines[1], " \t"); } TitleFound = TitleFound || StringManip.IndexOfCaseInsensitive(Headings, "title") != -1; if (Headings.Count != Units.Count) { throw new Exception("The number of headings and units doesn't match in file: " + _FileName); } } if (!TitleFound) { _Constants.Add(new APSIMConstant("Title", Path.GetFileNameWithoutExtension(_FileName), "", "")); } }
// ------------------------------ // Import from specified w2 file. // ------------------------------ static public void ImportW2N2P2(string[] FileNames, BaseController Apsoil) { Cursor.Current = Cursors.WaitCursor; string NewXml = ""; foreach (string FileName in FileNames) { string W2FileName = Path.GetDirectoryName(FileName) + "\\" + Path.GetFileNameWithoutExtension(FileName) + ".w2"; if (!File.Exists(W2FileName)) { throw new Exception("Cannot import file " + W2FileName + ". File doesn't exist"); } string[] Sections = ApsimFile.IniFile.INIReadAllSections(W2FileName); //Look for a title on the first few line. string Title = ""; StreamReader sr = new StreamReader(W2FileName); for (int i = 0; i != 5; i++) { string TitleLine = sr.ReadLine(); if (TitleLine.Length > 5 && TitleLine.ToLower().Substring(0, 6) == "!title") { Title = StringManip.SplitOffAfterDelimiter(ref TitleLine, "="); } } if (Title.Length == 0) { throw new Exception("Cannot find title line in file " + W2FileName); } // create a new soil. XmlDocument Doc = new XmlDocument(); Doc.AppendChild(XmlHelper.CreateNode(Doc, "soil", Title)); Soil NewSoil = new Soil(Doc.DocumentElement); // Read in all water parameters ReadWaterSection("run%", W2FileName, NewSoil); // Read in all crop sections. ReadCropSections("run%", W2FileName, NewSoil); // Read in all nitrogen parameters. string N2FileName = Path.GetDirectoryName(FileName) + "\\" + Path.GetFileNameWithoutExtension(FileName) + ".n2"; if (File.Exists(N2FileName)) { ReadNitrogenSection("run%", N2FileName, NewSoil); } // Read in all phosphorus parameters. string P2FileName = Path.GetDirectoryName(FileName) + "\\" + Path.GetFileNameWithoutExtension(FileName) + ".p2"; if (File.Exists(P2FileName)) { ReadPhosphorusSection("run%", P2FileName, NewSoil); } NewXml += NewSoil.Data.OuterXml; } // Add new soil to our soils. Apsoil.Selection.Add(NewXml); Cursor.Current = Cursors.Default; }