/// <summary> /// Upgrades to version 23. Add CarbonConcentration property to all organs. /// </summary> /// <param name="node">The node to upgrade.</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion23(XmlNode node, string fileName) { List <XmlNode> nodeList = new List <XmlNode>(); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "Root")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "Leaf")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "GenericOrgan")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "ReproductiveOrgan")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "LeafCohortParameters")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "SimpleLeaf")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "Nodule")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "PerennialLeaf")); foreach (XmlNode n in nodeList) { XmlNode DMnode; DMnode = XmlUtilities.CreateNode(node.OwnerDocument, "Constant", ""); XmlElement name = node.OwnerDocument.CreateElement("Name"); XmlElement element = node.OwnerDocument.CreateElement("FixedValue"); name.InnerText = "CarbonConcentration"; element.InnerText = "0.4"; DMnode.AppendChild(name); DMnode.AppendChild(element); if (ConverterUtilities.FindModelNode(n, "CarbonConcentration") == null) { n.AppendChild(DMnode); } } }
/// <summary> /// Ensure report variables have a square bracket around the first word. /// </summary> /// <param name="node">The node to upgrade.</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion15(XmlNode node, string fileName) { List <string> modelNames = ConverterUtilities.GetAllModelNames(node); foreach (XmlNode report in XmlUtilities.FindAllRecursivelyByType(node, "report")) { List <string> variables = XmlUtilities.Values(report, "VariableNames/string"); for (int i = 0; i < variables.Count; i++) { // If the first word (delimited by '.') is a model name then make sure it has // square brackets around it. int indexPeriod = variables[i].IndexOf('.'); if (indexPeriod != -1) { string firstWord = variables[i].Substring(0, indexPeriod); if (modelNames.Contains(firstWord) && !firstWord.StartsWith("[")) { variables[i] = "[" + firstWord + "]" + variables[i].Substring(indexPeriod); } } } XmlUtilities.SetValues(report, "VariableNames/string", variables); } }
/// <summary> /// Upgrades to version 26. Add leaf development rate constant to perrenial leaf /// </summary> /// <param name="node">The node to upgrade.</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion26(XmlNode node, string fileName) { foreach (XmlNode perennialLeaf in XmlUtilities.FindAllRecursivelyByType(node, "PerennialLeaf")) { ConverterUtilities.AddConstantFuntionIfNotExists(perennialLeaf, "LeafDevelopmentRate", "1.0"); } }
/// <summary> /// Rename CohortLive. to Live. /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion18(XmlNode node, string fileName) { foreach (XmlNode manager in XmlUtilities.FindAllRecursivelyByType(node, "manager")) { ConverterUtilities.SearchReplaceManagerCode(manager, ".SoilWater.dlayer", ".Thickness"); ConverterUtilities.SearchReplaceManagerCode(manager, ".SoilWater.Thickness", ".Thickness"); ConverterUtilities.SearchReplaceManagerCode(manager, ".SoilWater.LL15", ".LL15"); ConverterUtilities.SearchReplaceManagerCode(manager, ".SoilWater.LL15mm", ".LL15mm"); ConverterUtilities.SearchReplaceManagerCode(manager, ".SoilWater.DUL", ".DUL"); ConverterUtilities.SearchReplaceManagerCode(manager, ".SoilWater.DULmm", ".DULmm"); ConverterUtilities.SearchReplaceManagerCode(manager, ".SoilWater.SAT", ".SAT"); ConverterUtilities.SearchReplaceManagerCode(manager, ".SoilWater.SATmm", ".SATmm"); } foreach (XmlNode report in XmlUtilities.FindAllRecursivelyByType(node, "report")) { ConverterUtilities.SearchReplaceReportCode(report, ".SoilWater.dlayer", ".Thickness"); ConverterUtilities.SearchReplaceReportCode(report, ".SoilWater.Thickness", ".Thickness"); ConverterUtilities.SearchReplaceReportCode(report, ".SoilWater.LL15", ".LL15"); ConverterUtilities.SearchReplaceReportCode(report, ".SoilWater.LL15mm", ".LL15mm"); ConverterUtilities.SearchReplaceReportCode(report, ".SoilWater.DUL", ".DUL"); ConverterUtilities.SearchReplaceReportCode(report, ".SoilWater.DULmm", ".DULmm"); ConverterUtilities.SearchReplaceReportCode(report, ".SoilWater.SAT", ".SAT"); ConverterUtilities.SearchReplaceReportCode(report, ".SoilWater.SATmm", ".SATmm"); } }
/// <summary> /// Rename MainStemNodeAppearanceRate to Phyllochron AND /// MainStemFinalNodeNumber to FinalLeafNumber in Structure /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion12(XmlNode node, string fileName) { ConverterUtilities.RenamePMFFunction(node, "Structure", "MainStemNodeAppearanceRate", "Phyllochron"); ConverterUtilities.RenameVariable(node, ".MainStemNodeAppearanceRate", ".Phyllochron"); ConverterUtilities.RenamePMFFunction(node, "Structure", "MainStemFinalNodeNumber", "FinalLeafNumber"); ConverterUtilities.RenameVariable(node, ".MainStemFinalNodeNumber", ".FinalLeafNumber"); }
/// <summary> /// Upgrades to version 30. Change DisplayAttribute /// </summary> /// <param name="node">The node to upgrade.</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion30(XmlNode node, string fileName) { foreach (XmlNode manager in XmlUtilities.FindAllRecursivelyByType(node, "manager")) { string pattern = @"DisplayType *= *DisplayAttribute\.DisplayTypeEnum\."; ConverterUtilities.SearchReplaceManagerCodeUsingRegEx(manager, pattern, "Type=DisplayType.", null); } }
/// <summary> /// Upgrades to version 28. Change ICrop to IPlant /// </summary> /// <param name="node">The node to upgrade.</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion28(XmlNode node, string fileName) { foreach (XmlNode manager in XmlUtilities.FindAllRecursivelyByType(node, "manager")) { ConverterUtilities.SearchReplaceManagerCode(manager, " ICrop", " IPlant"); ConverterUtilities.SearchReplaceManagerCode(manager, "(ICrop)", "(IPlant)"); } }
/// <summary> /// Upgrades to version 7. Find all occurrences of ESW /// XProperty values. /// </summary> /// <param name="node">The node to upgrade.</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion7(XmlNode node, string fileName) { foreach (XmlNode manager in XmlUtilities.FindAllRecursivelyByType(node, "manager")) { ConverterUtilities.SearchReplaceManagerCodeUsingRegEx(manager, @"([\[\]\.\w]+\.ESW)", "MathUtilities.Sum($1)", "using APSIM.Shared.Utilities;"); } foreach (XmlNode report in XmlUtilities.FindAllRecursivelyByType(node, "report")) { ConverterUtilities.SearchReplaceReportCodeUsingRegEx(report, @"([\[\]\.\w]+\.ESW)", "sum($1)"); } }
/// <summary> /// Upgrades to version 27. Some variables in Leaf became ints rather than doubles. Need to add /// convert.ToDouble(); /// </summary> /// <param name="node">The node to upgrade.</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion27(XmlNode node, string fileName) { foreach (XmlNode manager in XmlUtilities.FindAllRecursivelyByType(node, "manager")) { string replacePattern = @"Convert.ToDouble(zone.Get(${variable}))"; string[] variableNames = new string[] { "ExpandedCohortNo", "AppearedCohortNo", "GreenCohortNo", "SenescingCohortNo" }; foreach (string variableName in variableNames) { string pattern = @"\(double\)zone.Get\((?<variable>\"".+\.Leaf\." + variableName + @"\"")\)"; ConverterUtilities.SearchReplaceManagerCodeUsingRegEx(manager, pattern, replacePattern, null); } } }
/// <summary> /// Add a DMDemandFunction constant function to all Root nodes that don't have one /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion9(XmlNode node, string fileName) { foreach (XmlNode root in XmlUtilities.FindAllRecursivelyByType(node, "Root")) { XmlNode partitionFraction = ConverterUtilities.FindModelNode(root, "PartitionFraction"); if (partitionFraction != null) { root.RemoveChild(partitionFraction); XmlNode demandFunction = root.AppendChild(root.OwnerDocument.CreateElement("PartitionFractionDemandFunction")); XmlUtilities.SetValue(demandFunction, "Name", "DMDemandFunction"); demandFunction.AppendChild(partitionFraction); } } }
/// <summary> /// Upgrades to version 22. Alter MovingAverage Function /// XProperty values. /// </summary> /// <param name="node">The node to upgrade.</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion22(XmlNode node, string fileName) { string StartStage = ""; foreach (XmlNode EmergePhase in XmlUtilities.FindAllRecursivelyByType(node, "EmergingPhase")) { StartStage = XmlUtilities.Value(EmergePhase, "End"); } ConverterUtilities.RenameVariable(node, "InitialValue", "StageToStartMovingAverage"); foreach (XmlNode MovingAverageFunction in XmlUtilities.FindAllRecursivelyByType(node, "MovingAverageFunction")) { XmlUtilities.SetValue(MovingAverageFunction, "StageToStartMovingAverage", StartStage); } }
/// <summary> /// Add default values for generic organ parameters that were previously optional /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion10(XmlNode node, string fileName) { List <XmlNode> organs = XmlUtilities.FindAllRecursivelyByType(node, "GenericOrgan"); organs.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "SimpleLeaf")); organs.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "Nodule")); foreach (XmlNode organ in organs) { ConverterUtilities.AddConstantFuntionIfNotExists(organ, "NRetranslocationFactor", "0.0"); ConverterUtilities.AddConstantFuntionIfNotExists(organ, "NitrogenDemandSwitch", "1.0"); ConverterUtilities.AddConstantFuntionIfNotExists(organ, "DMReallocationFactor", "0.0"); ConverterUtilities.AddConstantFuntionIfNotExists(organ, "DMRetranslocationFactor", "0.0"); ConverterUtilities.AddVariableReferenceFuntionIfNotExists(organ, "CriticalNConc", "[" + organ.FirstChild.InnerText + "].MinimumNConc.Value()"); } }
/// <summary> /// Add DMConversionEfficiency node /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion19(XmlNode node, string fileName) { //Rename existing DMConversionEfficiencyFunction nodes foreach (XmlNode n in XmlUtilities.FindAllRecursivelyByType(node, "Leaf")) { XmlNode dmFunction = ConverterUtilities.FindModelNode(n, "DMConversionEfficiencyFunction"); if (dmFunction != null) { XmlUtilities.SetValue(dmFunction, "Name", "DMConversionEfficiency"); } } List <XmlNode> nodeList = new List <XmlNode>(); XmlUtilities.FindAllRecursively(node, "DMConversionEfficiencyFunction", ref nodeList); foreach (XmlNode n in nodeList) { ConverterUtilities.RenameNode(n, "DMConversionEfficiencyFunction", "DMConversionEfficiency"); } nodeList.Clear(); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "Root")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "Leaf")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "GenericOrgan")); nodeList.AddRange(XmlUtilities.FindAllRecursivelyByType(node, "ReproductiveOrgan")); foreach (XmlNode n in nodeList) { XmlNode DMnode; DMnode = XmlUtilities.CreateNode(node.OwnerDocument, "Constant", ""); XmlElement name = node.OwnerDocument.CreateElement("Name"); XmlElement element = node.OwnerDocument.CreateElement("FixedValue"); name.InnerText = "DMConversionEfficiency"; element.InnerText = "1.0"; DMnode.AppendChild(name); DMnode.AppendChild(element); if (ConverterUtilities.FindModelNode(n, "DMConversionEfficiency") == null) { n.AppendChild(DMnode); } } }
/// <summary> /// Rename CohortLive. to Live. /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion17(XmlNode node, string fileName) { // Rename .CohortLive to .Live in all compositebiomass nodes and report variables. foreach (XmlNode biomass in XmlUtilities.FindAllRecursivelyByType(node, "CompositeBiomass")) { List <string> variables = XmlUtilities.Values(biomass, "Propertys/string"); for (int i = 0; i < variables.Count; i++) { variables[i] = variables[i].Replace(".CohortLive", ".Live"); variables[i] = variables[i].Replace(".CohortDead", ".Dead"); } XmlUtilities.SetValues(biomass, "Propertys/string", variables); } foreach (XmlNode report in XmlUtilities.FindAllRecursivelyByType(node, "report")) { List <string> variables = XmlUtilities.Values(report, "VariableNames/string"); for (int i = 0; i < variables.Count; i++) { variables[i] = variables[i].Replace(".CohortLive", ".Live"); variables[i] = variables[i].Replace(".CohortDead", ".Dead"); } XmlUtilities.SetValues(report, "VariableNames/string", variables); } // remove all live and dead nodes. foreach (XmlNode childToDelete in ConverterUtilities.FindModelNodes(node, "CompositeBiomass", "Live")) { childToDelete.ParentNode.RemoveChild(childToDelete); } foreach (XmlNode childToDelete in ConverterUtilities.FindModelNodes(node, "CompositeBiomass", "Dead")) { childToDelete.ParentNode.RemoveChild(childToDelete); } foreach (XmlNode childToDelete in ConverterUtilities.FindModelNodes(node, "Biomass", "Live")) { childToDelete.ParentNode.RemoveChild(childToDelete); } foreach (XmlNode childToDelete in ConverterUtilities.FindModelNodes(node, "Biomass", "Dead")) { childToDelete.ParentNode.RemoveChild(childToDelete); } }
/// <summary> /// Add nodes for new leaf tiller model /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion16(XmlNode node, string fileName) { foreach (XmlNode n in XmlUtilities.FindAllRecursivelyByType(node, "LeafCohortParameters")) { XmlNode LagDurationAgeMultiplier = XmlUtilities.CreateNode(node.OwnerDocument, "ArrayFunction", ""); XmlNode SenescenceDurationAgeMultiplier = XmlUtilities.CreateNode(node.OwnerDocument, "ArrayFunction", ""); XmlNode LeafSizeAgeMultiplier = XmlUtilities.CreateNode(node.OwnerDocument, "ArrayFunction", ""); XmlElement name = node.OwnerDocument.CreateElement("Name"); XmlElement element = node.OwnerDocument.CreateElement("Values"); name.InnerText = "LagDurationAgeMultiplier"; element.InnerText = "1 1 1"; LagDurationAgeMultiplier.AppendChild(name); LagDurationAgeMultiplier.AppendChild(element); name = node.OwnerDocument.CreateElement("Name"); name.InnerText = "SenescenceDurationAgeMultiplier"; element = node.OwnerDocument.CreateElement("Values"); element.InnerText = "1 1 1"; SenescenceDurationAgeMultiplier.AppendChild(name); SenescenceDurationAgeMultiplier.AppendChild(element); name = node.OwnerDocument.CreateElement("Name"); element = node.OwnerDocument.CreateElement("Values"); name.InnerText = "LeafSizeAgeMultiplier"; element.InnerText = "1 1 1 1 1 1 1 1 1 1 1 1"; LeafSizeAgeMultiplier.AppendChild(name); LeafSizeAgeMultiplier.AppendChild(element); if (ConverterUtilities.FindModelNode(n, "LagDurationAgeMultiplier") == null) { n.AppendChild(LagDurationAgeMultiplier); } if (ConverterUtilities.FindModelNode(n, "SenescenceDurationAgeMultiplier") == null) { n.AppendChild(SenescenceDurationAgeMultiplier); } if (ConverterUtilities.FindModelNode(n, "LeafSizeAgeMultiplier") == null) { n.AppendChild(LeafSizeAgeMultiplier); } } }
private static void UpgradeToVersion20(XmlNode node, string filename) { List <XmlNode> nodeList = new List <XmlNode>(XmlUtilities.FindAllRecursivelyByType(node, "Root")); foreach (XmlNode n in nodeList) { XmlNode MRFnode; MRFnode = XmlUtilities.CreateNode(node.OwnerDocument, "Constant", ""); XmlElement name = node.OwnerDocument.CreateElement("Name"); XmlElement element = node.OwnerDocument.CreateElement("FixedValue"); name.InnerText = "MaintenanceRespirationFunction"; element.InnerText = "1.0"; MRFnode.AppendChild(name); MRFnode.AppendChild(element); if (ConverterUtilities.FindModelNode(n, "MaintenanceRespirationFunction") == null) { n.AppendChild(MRFnode); } } }
/// <summary> /// Rename Plant15 to Plant. /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion13(XmlNode node, string fileName) { ConverterUtilities.RenameNode(node, "Plant15", "Plant"); ConverterUtilities.RenameVariable(node, "using Models.PMF.OldPlant;", "using Models.PMF;"); ConverterUtilities.RenameVariable(node, "Plant15", "Plant"); foreach (XmlNode manager in XmlUtilities.FindAllRecursivelyByType(node, "manager")) { ConverterUtilities.SearchReplaceManagerCodeUsingRegEx(manager, @"(\w+).plant.status *== *\042out\042", @"!$1.IsAlive", null); // /042 is a " } foreach (XmlNode simulationNode in XmlUtilities.FindAllRecursivelyByType(node, "Simulation")) { List <XmlNode> plantModels = XmlUtilities.FindAllRecursivelyByType(simulationNode, "Plant"); plantModels.RemoveAll(p => p.Name == "plant"); // remove lowercase plant nodes - these are in sugarcane if (plantModels.Count > 0) { XmlUtilities.EnsureNodeExists(simulationNode, "SoilArbitrator"); XmlUtilities.EnsureNodeExists(plantModels[0].ParentNode, "MicroClimate"); } } }
/// <summary> /// Rename NonStructural to Storage in Biomass organs /// </summary> /// <param name="node">The node to modifiy</param> /// <param name="fileName">The name of the .apsimx file</param> private static void UpgradeToVersion11(XmlNode node, string fileName) { ConverterUtilities.RenameVariable(node, ".NonStructural", ".Storage"); ConverterUtilities.RenameVariable(node, ".NonStructuralDemand", ".StorageDemand"); ConverterUtilities.RenameVariable(node, ".TotalNonStructuralDemand", ".TotalStorageDemand"); ConverterUtilities.RenameVariable(node, ".NonStructuralAllocation", ".StorageAllocation"); ConverterUtilities.RenameVariable(node, ".NonStructuralFraction", ".StorageFraction"); ConverterUtilities.RenameVariable(node, ".NonStructuralWt", ".StorageWt"); ConverterUtilities.RenameVariable(node, ".NonStructuralN", ".StorageN"); ConverterUtilities.RenameVariable(node, ".NonStructuralNConc", ".StorageNConc"); ConverterUtilities.RenameVariable(node, "NonStructuralFraction", "StorageFraction"); ConverterUtilities.RenameVariable(node, "LeafStartNonStructuralNReallocationSupply", "LeafStartStorageFractionNReallocationSupply"); ConverterUtilities.RenameVariable(node, "LeafStartNonStructuralNRetranslocationSupply", "LeafStartStorageNRetranslocationSupply"); ConverterUtilities.RenameVariable(node, "LeafStartNonStructuralDMReallocationSupply", "LeafStartStorageDMReallocationSupply"); ConverterUtilities.RenameVariable(node, "NonStructuralDMDemand", "StorageDMDemand"); ConverterUtilities.RenameVariable(node, "NonStructuralNDemand", "StorageNDemand"); // renames ConverterUtilities.RenamePMFFunction(node, "LeafCohortParameters", "NonStructuralFraction", "StorageFraction"); ConverterUtilities.RenameNode(node, "NonStructuralNReallocated", "StorageNReallocated"); ConverterUtilities.RenameNode(node, "NonStructuralWtReallocated", "StorageWtReallocated"); ConverterUtilities.RenameNode(node, "NonStructuralNRetrasnlocated", "StorageNRetrasnlocated"); }