Exemple #1
0
        protected override void OnLoad()
        {
            base.OnLoad();

            // We need not just the XML for this profile node but the whole soil XML.
            OurComponent = Controller.ApsimData.Find(NodePath);
            ApsimFile.Component SoilComponent;
            if (OurComponent.Parent.Type == "factor")
            {
                XmlNode factorNode    = OurComponent.Parent.ContentsAsXML;
                string  initWaterPath = XmlHelper.Value(factorNode, "targets/target");
                int     posLastSlash  = initWaterPath.LastIndexOf('/');
                if (posLastSlash != -1)
                {
                    string soilPath = initWaterPath.Remove(posLastSlash);
                    SoilComponent = Controller.ApsimData.Find(soilPath);
                }
                else
                {
                    throw new Exception("Cannot find soil node under: " + OurComponent.FullPath);
                }
            }
            else
            {
                SoilComponent = OurComponent.Parent;
            }

            if (SoilComponent.Type.ToLower() != "soil")
            {
                SoilComponent = SoilComponent.Parent;
            }
            if (SoilComponent.Type.ToLower() != "soil")
            {
                throw new Exception("Cannot find soil node under: " + OurComponent.FullPath);
            }

            XmlDocument soilDoc = new XmlDocument();

            soilDoc.LoadXml(SoilComponent.FullXMLNoShortCuts());

            if (OurComponent.Parent.Type == "factor")
            {
                // Install this InitWater under the Soil, replacing the existing one.
                XmlNode existingInitWater = XmlHelper.FindByType(soilDoc.DocumentElement, "InitialWater");
                if (existingInitWater == null)
                {
                    throw new Exception("Cannot find InitWater under soil");
                }
                soilDoc.DocumentElement.RemoveChild(existingInitWater);
                soilDoc.DocumentElement.AppendChild(soilDoc.ImportNode(OurComponent.ContentsAsXML, true));
            }



            Soil = Soil.Create(soilDoc.OuterXml);

            RelativeToCombo.Items.Clear();
            RelativeToCombo.Items.Add("ll15");
            RelativeToCombo.Items.AddRange(Soil.Water.CropNames);
        }
    private static string ReplaceSoilMacros(string ApsimToSimContents, Component ApsimComponent)
    {
        // Replace the [soil.] macros with values.
        if (ApsimToSimContents.Contains("[soil."))
        {
            // Firstly we need to locate the nearest soil.
            Component SoilComponent = null;
            if (ApsimComponent.Type.ToLower() == "soil")
            {
                SoilComponent = ApsimComponent;
            }
            else
            {
                foreach (Component Sibling in ApsimComponent.Parent.ChildNodes)
                {
                    if (Sibling.Type.ToLower() == "soil")
                    {
                        SoilComponent = Sibling;
                    }
                }
            }

            if (SoilComponent != null)
            {
                // Create a soil XML node.
                XmlDocument Doc = new XmlDocument();
                Doc.LoadXml(SoilComponent.FullXMLNoShortCuts());
                XmlNode SoilNode = Doc.DocumentElement;



                // Now do all soil macro replacements.
                ApsimToSimContents = ReplaceSoilMacros(SoilNode, ApsimToSimContents);
            }
        }
        return(ApsimToSimContents);
    }