/// <summary> /// </summary> /// <param name="root">The root JSON token.</param> /// <param name="fileName">The name of the apsimx file.</param> private static void UpgradeToVersion52(JObject root, string fileName) { foreach (var SOM in JsonUtilities.ChildrenOfType(root, "SoilOrganicMatter")) { double rootWt; if (SOM["RootWt"] is JArray) { rootWt = Convert.ToDouble(SOM["RootWt"][0], CultureInfo.InvariantCulture); // This can happen when importing old APSIM file. } else { rootWt = Convert.ToDouble(SOM["RootWt"], CultureInfo.InvariantCulture); } SOM.Remove("RootWt"); double[] thickness = MathUtilities.StringsToDoubles(JsonUtilities.Values(SOM, "Thickness")); double profileDepth = MathUtilities.Sum(thickness); double cumDepth = 0; double[] rootWtFraction = new double[thickness.Length]; for (int layer = 0; layer < thickness.Length; layer++) { double fracLayer = Math.Min(1.0, MathUtilities.Divide(profileDepth - cumDepth, thickness[layer], 0.0)); cumDepth += thickness[layer]; rootWtFraction[layer] = fracLayer * Math.Exp(-3.0 * Math.Min(1.0, MathUtilities.Divide(cumDepth, profileDepth, 0.0))); } // get the actuall FOM distribution through layers (adds up to one) double totFOMfraction = MathUtilities.Sum(rootWtFraction); for (int layer = 0; layer < thickness.Length; layer++) { rootWtFraction[layer] /= totFOMfraction; } double[] rootWtVector = MathUtilities.Multiply_Value(rootWtFraction, rootWt); JsonUtilities.SetValues(SOM, "RootWt", rootWtVector); } }
/// <summary> /// Helper method for renaming variables in report and manager. /// </summary> /// <param name="node">The JSON root node.</param> /// <param name="changes">List of old and new name tuples.</param> public static bool RenameVariables(JObject node, Tuple <string, string>[] changes) { bool replacementMade = false; foreach (var manager in JsonUtilities.ChildManagers(node)) { foreach (var replacement in changes) { if (manager.Replace(replacement.Item1, replacement.Item2)) { replacementMade = true; } } if (replacementMade) { manager.Save(); } } foreach (var report in JsonUtilities.ChildrenOfType(node, "Report")) { foreach (var replacement in changes) { if (JsonUtilities.SearchReplaceReportVariableNames(report, replacement.Item1, replacement.Item2)) { replacementMade = true; } } } foreach (var simpleGrazing in JsonUtilities.ChildrenOfType(node, "SimpleGrazing")) { var expression = simpleGrazing["FlexibleExpressionForTimingOfGrazing"]?.ToString(); if (!string.IsNullOrEmpty(expression)) { foreach (var replacement in changes) { if (expression.Contains(replacement.Item1)) { expression = expression.Replace(replacement.Item1, replacement.Item2); replacementMade = true; } } simpleGrazing["FlexibleExpressionForTimingOfGrazing"] = expression; } } foreach (var compositeFactor in JsonUtilities.ChildrenOfType(node, "CompositeFactor")) { var specifications = compositeFactor["Specifications"] as JArray; if (specifications != null) { bool replacementFound = false; foreach (var replacement in changes) { for (int i = 0; i < specifications.Count; i++) { replacementFound = replacementFound || specifications[i].ToString().Contains(replacement.Item1); specifications[i] = specifications[i].ToString().Replace(replacement.Item1, replacement.Item2); } } if (replacementFound) { replacementMade = true; compositeFactor["Specifications"] = specifications; } } } foreach (JObject variableRef in ChildrenOfType(node, typeof(VariableReference).Name)) { foreach (var replacement in changes) { string variableName = variableRef["VariableName"]?.ToString(); if (variableName.Contains(replacement.Item1)) { replacementMade = true; variableRef["VariableName"] = variableName?.Replace(replacement.Item1, replacement.Item2); } } } foreach (JObject series in ChildrenOfType(node, nameof(Series))) { foreach (var change in changes) { var from = change.Item1.Replace("[", "").Replace("]", ""); var to = change.Item2.Replace("[", "").Replace("]", ""); if (series["XFieldName"]?.ToString() != null && series["XFieldName"].ToString().Contains(from)) { replacementMade = true; series["XFieldName"] = series["XFieldName"].ToString().Replace(from, to); } if (series["YFieldName"]?.ToString() != null && series["YFieldName"].ToString().Contains(from)) { replacementMade = true; series["YFieldName"] = series["YFieldName"].ToString().Replace(from, to); } if (series["X2FieldName"]?.ToString() != null && series["X2FieldName"].ToString().Contains(from)) { replacementMade = true; series["X2FieldName"] = series["X2FieldName"].ToString().Replace(from, to); } if (series["Y2FieldName"]?.ToString() != null && series["Y2FieldName"].ToString().Contains(from)) { replacementMade = true; series["Y2FieldName"] = series["Y2FieldName"].ToString().Replace(from, to); } if (series["Filter"]?.ToString() != null && series["Filter"].ToString().Contains(from)) { replacementMade = true; series["Filter"] = series["Filter"].ToString().Replace(from, to); } } } return(replacementMade); }
private static void WriteProperty(JProperty property, JObject toObject) { string propertyName = property.Name; if (propertyName == "@Version") { propertyName = "Version"; } if (propertyName == "#text" && property.Path.Contains("Memo")) { return; // Old memo have #text, we don't want them. } if (!propertyName.StartsWith("@")) { JToken valueToken = property.Value; if (valueToken.HasValues) { if (property.First.First.First is JValue) { JValue value = property.First.First.First as JValue; string elementType = (value.Parent as JProperty).Name; JArray newArray = new JArray(); newArray.Add(new JValue(value.ToString())); toObject[propertyName] = newArray; } else if (property.First.First.First is JArray) { JArray array = property.First.First.First as JArray; string elementType = (array.Parent as JProperty).Name; JArray newArray = new JArray(); foreach (var value in array.Values()) { if (elementType == "string") { newArray.Add(new JValue(value.ToString())); } else if (elementType == "double") { newArray.Add(new JValue(double.Parse(value.ToString(), CultureInfo.InvariantCulture))); } } toObject[propertyName] = newArray; } } else { string value = valueToken.Value <string>(); int intValue; double doubleValue; bool boolValue; DateTime dateValue; if (property.Name == "Name") { if (JsonUtilities.Type(toObject) == "SoilCrop") { toObject["Name"] = GetSoilCropName(property.Value.ToString()); } else { toObject[propertyName] = value; } } else if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out intValue)) { toObject[propertyName] = intValue; } else if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out doubleValue)) { toObject[propertyName] = doubleValue; } else if (value == "-INF") { toObject[propertyName] = double.NaN; } else if (bool.TryParse(value, out boolValue)) { toObject[propertyName] = boolValue; } else if (DateTime.TryParseExact(value, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue)) { toObject[propertyName] = dateValue.ToString("yyyy-MM-dd"); } else { toObject[propertyName] = value; } } } else if (propertyName == "@name") // Name attribute. { // SoilCrops copied from Apsim classic need to be renamed to CropNameSoil e.g. WheatSoil. if (toObject["$type"]?.ToString() == "Models.Soils.SoilCrop, Models") { toObject["Name"] = property.Value.ToString() + "Soil"; } else if (toObject["Name"] == null) { toObject["Name"] = property.Value; } } }
private static void ProcessObject(string name, JToken obj, JObject newRoot) { // Look for an array of something e.g. variable names in report. if (name == "Code") { JValue childAsValue = obj.First.First as JValue; newRoot["Code"] = childAsValue.Value.ToString(); } else if (name == "MemoText") { JValue childAsValue = obj.First.First as JValue; newRoot["Text"] = childAsValue.Value.ToString(); } else if (name.Equals("Script", StringComparison.CurrentCultureIgnoreCase)) { // manager parameters. JArray parameters = new JArray(); foreach (JProperty parameter in obj.Children()) { JObject newParameter = new JObject(); newParameter["Key"] = parameter.Name; newParameter["Value"] = parameter.Value.ToString(); parameters.Add(newParameter); } newRoot["Parameters"] = parameters; } else if (name.Equals("PaddockList", StringComparison.CurrentCultureIgnoreCase)) { // manager parameters. JArray values = new JArray(); foreach (var child in obj.Children()) { var newObject = CreateObject(child.First); values.Add(newObject); } newRoot[name] = values; } else { if (GetModelFullName(name) == null) { var modelType = GetTypeFromName(JsonUtilities.Type(newRoot)); var property = modelType?.GetProperty(name); var newObject = CreateObject(obj); // If the new obejct is NOT a JArray, and this object is supposed to be an array... if (!(newObject is JArray) && (arrayVariableNames.Contains(name) || (property != null && property.PropertyType.IsArray))) { // Should be an array of objects. if (newObject.First.First is JArray) { newObject = newObject.First.First; } else { JArray array = new JArray(); if (newObject.Count() == 1 && newObject.First is JProperty) { array.Add(newObject.First.First); } else { array.Add(newObject); } newObject = array; } } if (!(newObject is JArray) && newObject["$type"] != null && GetModelFullName(newObject["$type"].ToString()) != null) { AddNewChild(newObject, newRoot); } else if (newObject.Children().Count() == 1 && newObject.First.Path == "#text") { newRoot[name] = newObject.First.First; } else { newRoot[name] = newObject; } } else { AddNewChild(obj, newRoot); } } }
/// <summary> /// Changes initial Root Wt to an array. /// </summary> /// <param name="root">The root JSON token.</param> /// <param name="fileName">The name of the apsimx file.</param> private static void UpgradeToVersion99(JObject root, string fileName) { // Delete all alias children. foreach (var soilNitrogen in JsonUtilities.ChildrenOfType(root, "SoilNitrogen")) { var parent = JsonUtilities.Parent(soilNitrogen); var nutrient = JsonUtilities.CreateNewChildModel(parent, "Nutrient", "Models.Soils.Nutrients.Nutrient"); nutrient["ResourceName"] = "Nutrient"; soilNitrogen.Remove(); } foreach (var manager in JsonUtilities.ChildManagers(root)) { manager.Replace("using Models.Soils;", "using Models.Soils;\r\nusing Models.Soils.Nutrients;"); manager.Replace("SoilNitrogen.FOMN", ".Nutrient.FOMN"); manager.Replace("SoilNitrogen.FOMC", ".Nutrient.FOMC"); if (manager.Replace("Soil.SoilNitrogen.HumicN", "Humic.N")) { manager.AddDeclaration("NutrientPool", "Humic", new string[] { "[ScopedLinkByName]" }); } if (manager.Replace("Soil.SoilNitrogen.HumicC", "Humic.C")) { manager.AddDeclaration("NutrientPool", "Humic", new string[] { "[ScopedLinkByName]" }); } if (manager.Replace("Soil.SoilNitrogen.MicrobialN", "Microbial.N")) { manager.AddDeclaration("NutrientPool", "Microbial", new string[] { "[ScopedLinkByName]" }); } if (manager.Replace("Soil.SoilNitrogen.MicrobialC", "Microbial.C")) { manager.AddDeclaration("NutrientPool", "Microbial", new string[] { "[ScopedLinkByName]" }); } if (manager.Replace("Soil.SoilNitrogen.dlt_n_min_res", "SurfaceResidueDecomposition.MineralisedN")) { manager.AddDeclaration("CarbonFlow", "SurfaceResidueDecomposition", new string[] { "[LinkByPath(Path=\"[Nutrient].SurfaceResidue.Decomposition\")]" }); } manager.Replace("SoilNitrogen.MineralisedN", "Nutrient.MineralisedN"); manager.Replace("SoilNitrogen.TotalN", "Nutrient.TotalN"); if (manager.Replace("SoilNitrogen.TotalN", "Nutrient.TotalN")) { manager.RemoveDeclaration("SoilNitrogen"); manager.AddDeclaration("INutrient", "Nutrient", new string[] { "[ScopedLinkByName]" }); } manager.Replace("SoilNitrogen.TotalC", "Nutrient.TotalC"); if (manager.Replace("SoilNitrogen.TotalC", "Nutrient.TotalC")) { manager.RemoveDeclaration("SoilNitrogen"); manager.AddDeclaration("INutrient", "Nutrient", new string[] { "[ScopedLinkByName]" }); } manager.Replace("SoilNitrogen.mineral_n", "Nutrient.MineralN"); manager.Replace("SoilNitrogen.Denitrification", "Nutrient.Natm"); manager.Replace("SoilNitrogen.n2o_atm", "Nutrient.N2Oatm"); manager.Save(); } foreach (var report in JsonUtilities.ChildrenOfType(root, "Report")) { JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.NO3.kgha", "Nutrient.NO3.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.NH4.kgha", "Nutrient.NH4.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.Urea.kgha", "Nutrient.Urea.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.PlantAvailableNO3.kgha", "Nutrient.PlantAvailableNO3.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.PlantAvailableNH4.kgha", "Nutrient.PlantAvailableNH4.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "[SoilNitrogen].NO3.kgha", "[Nutrient].NO3.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "[SoilNitrogen].NH4.kgha", "[Nutrient].NH4.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "[SoilNitrogen].Urea.kgha", "[Nutrient].Urea.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.FOMN", ".Nutrient.FOMN"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.FOMC", ".Nutrient.FOMC"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.HumicN", ".Nutrient.Humic.N"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.HumicC", ".Nutrient.Humic.C"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.MicrobialN", ".Nutrient.Microbial.N"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.MicrobialC", ".Nutrient.Microbial.C"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.urea", ".Nutrient.Urea.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.dlt_n_min_res", ".Nutrient.SurfaceResidue.Decomposition.MineralisedN"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.MineralisedN", ".Nutrient.MineralisedN"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.Denitrification", ".Nutrient.Natm"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.n2o_atm", ".Nutrient.N2Oatm"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.TotalC", ".Nutrient.TotalC"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.TotalN", ".Nutrient.TotalN"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.mineral_n", ".Nutrient.MineralN"); JsonUtilities.SearchReplaceReportVariableNames(report, ".SoilNitrogen.Nitrification", ".Nutrient.NH4.Nitrification"); } foreach (var series in JsonUtilities.ChildrenOfType(root, "Series")) { if (series["XFieldName"] != null) { series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.NO3.kgha", "Nutrient.NO3.kgha"); series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.NH4.kgha", "Nutrient.NH4.kgha"); series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.Urea.kgha", "Nutrient.Urea.kgha"); series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.PlantAvailableNO3.kgha", "Nutrient.PlantAvailableNO3.kgha"); series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.PlantAvailableNH4.kgha", "Nutrient.PlantAvailableNH4.kgha"); } if (series["YFieldName"] != null) { series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.NO3.kgha", "Nutrient.NO3.kgha"); series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.NH4.kgha", "Nutrient.NH4.kgha"); series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.Urea.kgha", "Nutrient.Urea.kgha"); series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.PlantAvailableNO3.kgha", "Nutrient.PlantAvailableNO3.kgha"); series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.PlantAvailableNH4.kgha", "Nutrient.PlantAvailableNH4.kgha"); } } }
/// <summary> /// Change Factor.Specifications to Factor.Specification. Also FactorValue /// becomes CompositeFactor. /// </summary> /// <param name="root"></param> /// <param name="fileName"></param> private static void UpgradeToVersion56(JToken root, string fileName) { foreach (var factor in JsonUtilities.ChildrenRecursively(root as JObject, "Factor")) { var parent = JsonUtilities.Parent(factor); string parentModelType = JsonUtilities.Type(parent); if (parentModelType == "Factors") { var specifications = factor["Specifications"] as JArray; if (specifications != null) { if (specifications.Count > 1) { // must be a compound factor. // Change our Factor to a CompositeFactor factor["$type"] = "Models.Factorial.CompositeFactor, Models"; // Remove the Factor from it's parent. var parentChildren = parent["Children"] as JArray; parentChildren.Remove(factor); // Create a new site factor and add our CompositeFactor to the children list. var siteFactor = JsonUtilities.ChildWithName(parent as JObject, "Site") as JObject; if (siteFactor == null) { // Create a site factor siteFactor = new JObject(); siteFactor["$type"] = "Models.Factorial.Factor, Models"; siteFactor["Name"] = "Site"; JArray siteFactorChildren = new JArray(); siteFactor["Children"] = siteFactorChildren; // Add our new site factor to our models parent. parentChildren.Add(siteFactor); } (siteFactor["Children"] as JArray).Add(factor); } else { // Convert array to string. if (specifications.Count > 0) { factor["Specification"] = specifications[0].ToString(); } } } } else if (parentModelType == "Factor") { factor["$type"] = "Models.Factorial.CompositeFactor, Models"; } } foreach (var series in JsonUtilities.ChildrenRecursively(root as JObject, "Series")) { var factorToVaryColours = series["FactorToVaryColours"]; if (factorToVaryColours != null && factorToVaryColours.Value <string>() == "Simulation") { series["FactorToVaryColours"] = "SimulationName"; } var factorToVaryMarkers = series["FactorToVaryMarkers"]; if (factorToVaryMarkers != null && factorToVaryMarkers.Value <string>() == "Simulation") { series["FactorToVaryMarkers"] = "SimulationName"; } var factorToVaryLines = series["FactorToVaryLines"]; if (factorToVaryLines != null && factorToVaryLines.Value <string>() == "Simulation") { series["FactorToVaryLines"] = "SimulationName"; } } }
/// <summary> /// Remove SoluteManager. /// </summary> /// <param name="root">The root JSON token.</param> /// <param name="fileName">The name of the apsimx file.</param> private static void UpgradeToVersion54(JObject root, string fileName) { foreach (var soluteManager in JsonUtilities.ChildrenOfType(root, "SoluteManager")) { soluteManager.Remove(); } foreach (var report in JsonUtilities.ChildrenOfType(root, "Report")) { JsonUtilities.SearchReplaceReportVariableNames(report, "[Soil].NO3N", "[Soil].SoilNitrogen.NO3.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "[Soil].NH4N", "[Soil].SoilNitrogen.NH4.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "[Soil].UreaN", "[Soil].SoilNitrogen.Urea.kgha"); } foreach (var manager in JsonUtilities.ChildManagers(root)) { bool managerChanged = false; if (manager.Replace("mySoil.NO3N", "NO3.kgha")) { manager.AddDeclaration("ISolute", "NO3", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("mySoil.NH4N", "NH4.kgha")) { manager.AddDeclaration("ISolute", "NH4", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("mySoil.UreaN", "Urea.kgha")) { manager.AddDeclaration("ISolute", "Urea", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("Soil.NO3N", "NO3.kgha")) { manager.AddDeclaration("ISolute", "NO3", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("Soil.NH4N", "NH4.kgha")) { manager.AddDeclaration("ISolute", "NH4", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("Soil.UreaN", "Urea.kgha")) { manager.AddDeclaration("ISolute", "Urea", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("mySoil.SoilNitrogen.", "SoilNitrogen.")) { manager.AddDeclaration("SoilNitrogen", "SoilNitrogen", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("Soil.SoilNitrogen.", "SoilNitrogen.")) { manager.AddDeclaration("SoilNitrogen", "SoilNitrogen", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("soil.SoilNitrogen.", "SoilNitrogen.")) { manager.AddDeclaration("SoilNitrogen", "SoilNitrogen", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } if (manager.Replace("soil1.SoilNitrogen.", "SoilNitrogen.")) { manager.AddDeclaration("SoilNitrogen", "SoilNitrogen", new string[] { "[ScopedLinkByName]" }); managerChanged = true; } var declarations = manager.GetDeclarations(); if (declarations.RemoveAll(declaration => declaration.TypeName == "SoluteManager") > 0) { manager.SetDeclarations(declarations); managerChanged = true; } if (managerChanged) { var usingLines = manager.GetUsingStatements().ToList(); usingLines.Add("Models.Interfaces"); manager.SetUsingStatements(usingLines); manager.Save(); } } }
/// <summary> /// Adds solutes under SoilNitrogen. /// </summary> /// <param name="root">The root JSON token.</param> /// <param name="fileName">The name of the apsimx file.</param> private static void UpgradeToVersion53(JObject root, string fileName) { foreach (var soilNitrogen in JsonUtilities.ChildrenOfType(root, "SoilNitrogen")) { JsonUtilities.CreateNewChildModel(soilNitrogen, "NO3", "Models.Soils.SoilNitrogenNO3"); JsonUtilities.CreateNewChildModel(soilNitrogen, "NH4", "Models.Soils.SoilNitrogenNH4"); JsonUtilities.CreateNewChildModel(soilNitrogen, "Urea", "Models.Soils.SoilNitrogenUrea"); JsonUtilities.CreateNewChildModel(soilNitrogen, "PlantAvailableNO3", "Models.Soils.SoilNitrogenPlantAvailableNO3"); JsonUtilities.CreateNewChildModel(soilNitrogen, "PlantAvailableNH4", "Models.Soils.SoilNitrogenPlantAvailableNH4"); } foreach (var report in JsonUtilities.ChildrenOfType(root, "Report")) { JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.NO3", "SoilNitrogen.NO3.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.NH4", "SoilNitrogen.NH4.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.urea", "SoilNitrogen.Urea.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.PlantAvailableNO3", "SoilNitrogen.PlantAvailableNO3.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "SoilNitrogen.PlantAvailableNH4", "SoilNitrogen.PlantAvailableNH4.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "[SoilNitrogen].no3", "[SoilNitrogen].NO3.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "[SoilNitrogen].nh4", "[SoilNitrogen].NH4.kgha"); JsonUtilities.SearchReplaceReportVariableNames(report, "[SoilNitrogen].urea", "[SoilNitrogen].Urea.kgha"); } foreach (var manager in JsonUtilities.ChildManagers(root)) { var originalCode = manager.ToString(); if (originalCode != null) { if (originalCode.Contains("SoilNitrogen.NO3")) { manager.Replace("Soil.SoilNitrogen.NO3", "NO3.kgha"); manager.Replace("SoilNitrogen.NO3", "NO3.kgha"); manager.AddDeclaration("ISolute", "NO3", new string[] { "[ScopedLinkByName]" }); } if (originalCode.Contains("SoilNitrogen.NH4")) { manager.Replace("Soil.SoilNitrogen.NH4", "NH4.kgha"); manager.Replace("SoilNitrogen.NH4", "NH4.kgha"); manager.AddDeclaration("ISolute", "NH4", new string[] { "[ScopedLinkByName]" }); } if (originalCode.Contains("SoilNitrogen.urea")) { manager.Replace("Soil.SoilNitrogen.urea", "Urea.kgha"); manager.Replace("SoilNitrogen.urea", "Urea.kgha"); manager.AddDeclaration("ISolute", "Urea", new string[] { "[ScopedLinkByName]" }); } if (originalCode.Contains("SoilNitrogen.PlantAvailableNO3")) { manager.Replace("Soil.SoilNitrogen.PlantAvailableNO3", "PlantAvailableNO3.kgha"); manager.Replace("SoilNitrogen.PlantAvailableNO3", "PlantAvailableNO3.kgha"); manager.AddDeclaration("ISolute", "PlantAvailableNO3", new string[] { "[ScopedLinkByName]" }); } if (originalCode.Contains("SoilNitrogen.PlantAvailableNH4")) { manager.Replace("Soil.SoilNitrogen.PlantAvailableNH4", "PlantAvailableNH4.kgha"); manager.Replace("SoilNitrogen.PlantAvailableNH4", "PlantAvailableNH4.kgha"); manager.AddDeclaration("ISolute", "PlantAvailableNH4", new string[] { "[ScopedLinkByName]" }); } if (originalCode != manager.ToString()) { var usingLines = manager.GetUsingStatements().ToList(); usingLines.Add("Models.Interfaces"); manager.SetUsingStatements(usingLines); manager.Save(); } } } foreach (var series in JsonUtilities.ChildrenOfType(root, "Series")) { if (series["XFieldName"] != null) { series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.NO3", "SoilNitrogen.NO3.kgha"); series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.NH4", "SoilNitrogen.NH4.kgha"); series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.urea", "SoilNitrogen.Urea.kgha"); series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.PlantAvailableNO3", "SoilNitrogen.PlantAvailableNO3.kgha"); series["XFieldName"] = series["XFieldName"].ToString().Replace("SoilNitrogen.PlantAvailableNH4", "SoilNitrogen.PlantAvailableNH4.kgha"); } if (series["YFieldName"] != null) { series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.NO3", "SoilNitrogen.NO3.kgha"); series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.NH4", "SoilNitrogen.NH4.kgha"); series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.urea", "SoilNitrogen.Urea.kgha"); series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.PlantAvailableNO3", "SoilNitrogen.PlantAvailableNO3.kgha"); series["YFieldName"] = series["YFieldName"].ToString().Replace("SoilNitrogen.PlantAvailableNH4", "SoilNitrogen.PlantAvailableNH4.kgha"); } } }
/// <summary> /// Helper method for renaming variables in report and manager. /// </summary> /// <param name="node">The JSON root node.</param> /// <param name="changes">List of old and new name tuples.</param> public static bool RenameVariables(JObject node, Tuple <string, string>[] changes) { bool replacementMade = false; foreach (var manager in JsonUtilities.ChildManagers(node)) { foreach (var replacement in changes) { if (manager.Replace(replacement.Item1, replacement.Item2)) { replacementMade = true; } } if (replacementMade) { manager.Save(); } } foreach (var report in JsonUtilities.ChildrenOfType(node, "Report")) { foreach (var replacement in changes) { if (JsonUtilities.SearchReplaceReportVariableNames(report, replacement.Item1, replacement.Item2)) { replacementMade = true; } } } foreach (var simpleGrazing in JsonUtilities.ChildrenOfType(node, "SimpleGrazing")) { var expression = simpleGrazing["FlexibleExpressionForTimingOfGrazing"]?.ToString(); if (!string.IsNullOrEmpty(expression)) { foreach (var replacement in changes) { if (expression.Contains(replacement.Item1)) { expression = expression.Replace(replacement.Item1, replacement.Item2); replacementMade = true; } } simpleGrazing["FlexibleExpressionForTimingOfGrazing"] = expression; } } foreach (var compositeFactor in JsonUtilities.ChildrenOfType(node, "CompositeFactor")) { var specifications = compositeFactor["Specifications"] as JArray; if (specifications != null) { bool replacementFound = false; foreach (var replacement in changes) { for (int i = 0; i < specifications.Count; i++) { replacementFound = replacementFound || specifications[i].ToString().Contains(replacement.Item1); specifications[i] = specifications[i].ToString().Replace(replacement.Item1, replacement.Item2); } } if (replacementFound) { replacementMade = true; compositeFactor["Specifications"] = specifications; } } } return(replacementMade); }