Beispiel #1
0
        public void ConvertYPCropReportSpecToAPSIM()
        {
            // NB: This is a real YP crop report XML file from 2014.
            string cropReportFileName = Path.Combine(filesDirectory, "CropReport.xml");

            // Create a YieldProphet object from our file.
            YieldProphet spec = YieldProphetUtility.YieldProphetFromFile(cropReportFileName);

            // Create an APSIM spec
            List <APSIMSpec> simulations = YieldProphetToAPSIM.ToAPSIM(spec);

            Assert.AreEqual(simulations.Count, 5);
            Assert.AreEqual(simulations[0].Name, "ThisYear");
            Assert.AreEqual(simulations[1].Name, "Base");
            Assert.AreEqual(simulations[2].Name, "NUnlimited");
            Assert.AreEqual(simulations[3].Name, "NUnlimitedFromToday");
            Assert.AreEqual(simulations[4].Name, "Next10DaysDry");
            Assert.AreEqual(simulations[0].StartDate, new DateTime(2014, 3, 25));
            Assert.AreEqual(simulations[0].EndDate, new DateTime(2014, 6, 1));
            Assert.AreEqual(simulations[0].Management.Count, 8);
            Assert.IsTrue(simulations[0].Management[0] is Sow);
            Assert.AreEqual((simulations[0].Management[0] as Sow).Date, new DateTime(2014, 4, 1));
            Assert.AreEqual((simulations[0].Management[0] as Sow).Crop, "Wheat");
            Assert.IsTrue(simulations[0].Management[1] is Fertilise);
            Assert.IsTrue(simulations[0].Management[2] is Fertilise);
            Assert.IsTrue(simulations[0].Management[3] is Fertilise);
            Assert.IsTrue(simulations[0].Management[4] is Tillage);
            Assert.IsTrue(simulations[0].Management[5] is ResetWater);
            Assert.IsTrue(simulations[0].Management[6] is ResetSurfaceOrganicMatter);
            Assert.IsTrue(simulations[0].Management[7] is ResetNitrogen);
            Assert.AreEqual(simulations[0].ObservedData, null);
        }
Beispiel #2
0
        /// <summary>Runs APSIM on the given xml file and makes sure no errors.</summary>
        /// <param name="xmlFileName">Name of the XML file.</param>
        private static void RunAPSIM(string xmlFileName)
        {
            // Create a YieldProphet object from our file.
            YieldProphet spec = YieldProphetUtility.YieldProphetFromFile(xmlFileName);

            foreach (Paddock paddock in spec.Paddock)
            {
                paddock.NowDate = new DateTime(2015, 3, 31);
            }

            // Create an APSIM spec
            List <APSIMSpec> simulations = YieldProphetToAPSIM.ToAPSIM(spec);

            // Now create all the files.
            APSIMFiles.Create(simulations, workingDirectory);

            // Make sure we got a .apsim file.
            string      apsimFileName = Path.Combine(workingDirectory, "YieldProphet.apsim");
            XmlDocument doc           = new XmlDocument();

            doc.Load(apsimFileName);

            List <XmlNode> simulationNodes = Utility.Xml.ChildNodes(doc.DocumentElement, "");

            Assert.AreEqual(simulationNodes.Count, 6);
            Assert.AreEqual(Utility.Xml.NameAttr(simulationNodes[0]), "ThisYear");
            Assert.AreEqual(Utility.Xml.NameAttr(simulationNodes[1]), "Base");
            Assert.AreEqual(Utility.Xml.NameAttr(simulationNodes[2]), "NUnlimited");
            Assert.AreEqual(Utility.Xml.NameAttr(simulationNodes[3]), "NUnlimitedFromToday");
            Assert.AreEqual(Utility.Xml.NameAttr(simulationNodes[4]), "Next10DaysDry");
            Assert.AreEqual(Utility.Xml.NameAttr(simulationNodes[5]), "Factorials");

            doc.DocumentElement.RemoveChild(simulationNodes[1]);
            doc.DocumentElement.RemoveChild(simulationNodes[2]);
            doc.DocumentElement.RemoveChild(simulationNodes[3]);
            doc.DocumentElement.RemoveChild(simulationNodes[4]);
            doc.Save(apsimFileName);

            Process p = Utility.Process.RunProcess(@"D:\APSIM\Model\Apsim.exe",
                                                   apsimFileName,
                                                   workingDirectory);
            string errors = Utility.Process.CheckProcessExitedProperly(p);

            Assert.IsFalse(errors.Contains("Fail"));

            StreamReader reader          = new StreamReader(Path.Combine(workingDirectory, "ThisYear_1.sum"));
            string       sumFileContents = reader.ReadToEnd();

            reader.Close();
            Assert.IsFalse(sumFileContents.Contains("APSIM  Fatal  Error"));
        }
Beispiel #3
0
        public void ConvertYPValidationSpecToAPSIM()
        {
            // NB: This is a real YP validation XML file from 2004.
            string validationFileName = Path.Combine(filesDirectory, "TestValidation.xml");

            // Create a YieldProphet object from our file.
            YieldProphet spec = YieldProphetUtility.YieldProphetFromFile(validationFileName);

            // Run the files.
            List <APSIMSpec> simulations = YieldProphetToAPSIM.ToAPSIM(spec);

            Assert.AreEqual(simulations.Count, 2);
            Assert.AreEqual(simulations[0].Name, "2004;A and R Weidemann;Wep 1");
            Assert.AreEqual(simulations[1].Name, "2004;A and R Weidemann;Wep 2");
            Assert.AreEqual(simulations[0].StartDate, new DateTime(2004, 4, 1));
            Assert.AreEqual(simulations[0].EndDate, new DateTime(2005, 1, 26));
            Assert.AreEqual(simulations[0].Management.Count, 6);
            Assert.IsTrue(simulations[0].Management[0] is Sow);
            Assert.AreEqual((simulations[0].Management[0] as Sow).Date, new DateTime(2004, 6, 6));
            Assert.AreEqual((simulations[0].Management[0] as Sow).Crop, "Wheat");
            Assert.IsTrue(simulations[0].Management[1] is Fertilise);
            Assert.IsTrue(simulations[0].Management[2] is Fertilise);
            Assert.IsTrue(simulations[0].Management[3] is ResetWater);
            Assert.IsTrue(simulations[0].Management[4] is ResetSurfaceOrganicMatter);
            Assert.IsTrue(simulations[0].Management[5] is ResetNitrogen);
            Assert.AreEqual(simulations[0].ObservedData.Rows.Count, 275);

            Assert.AreEqual(simulations[0].Samples.Count, 1);
            Assert.AreEqual(simulations[0].Samples[0].SWUnits, Sample.SWUnitsEnum.Gravimetric);
            Assert.AreEqual(simulations[0].Samples[0].NO3Units, Sample.NUnitsEnum.ppm);
            Assert.AreEqual(simulations[0].Samples[0].NH4Units, Sample.NUnitsEnum.ppm);
            Assert.AreEqual(simulations[0].Samples[0].PHUnits, Sample.PHSampleUnitsEnum.CaCl2);
            Assert.AreEqual(simulations[0].Samples[0].OCUnits, Sample.OCSampleUnitsEnum.WalkleyBlack);
            Assert.IsTrue(Utility.Math.AreEqual(simulations[0].Soil.Samples[0].Thickness, new double[] { 100.0, 300.0, 300.0, 300.0 }));
            Assert.IsTrue(Utility.Math.AreEqual(simulations[0].Soil.Samples[0].SW, new double[] { 0.139, 0.247, 0.215, 0.191 }));
            Assert.IsTrue(Utility.Math.AreEqual(simulations[0].Soil.Samples[0].NH4, new double[] { 1.1, 0.5, 0.5, 0.5 }));
            Assert.IsTrue(Utility.Math.AreEqual(simulations[0].Soil.Samples[0].NO3, new double[] { 3, 3, 1.8, 0.5 }));
            Assert.IsTrue(Utility.Math.AreEqual(simulations[0].Soil.Samples[0].OC, new double[] { 1.2, double.NaN, double.NaN, double.NaN }));
            Assert.IsTrue(Utility.Math.AreEqual(simulations[0].Soil.Samples[0].EC, new double[] { 0.09, 0.19, 0.24, 0.3 }));
            Assert.IsTrue(Utility.Math.AreEqual(simulations[0].Soil.Samples[0].PH, new double[] { 6, 7.9, 8.4, 8.5 }));
            Assert.IsTrue(Utility.Math.AreEqual(simulations[0].Soil.Samples[0].CL, new double[] { 29, 11, 10, 10 }));
        }
        public void YPToAPSIMSpecTests_EnsureLongtermWorks()
        {
            YieldProphet yp = new YieldProphet()
            {
                Paddock = new List <Paddock>()
                {
                    new Paddock()
                    {
                        Name                   = "NameOfPaddock",
                        StartSeasonDate        = new DateTime(2016, 4, 1),
                        NowDate                = new DateTime(2016, 7, 1),
                        SoilWaterSampleDate    = new DateTime(2016, 3, 1),
                        SoilNitrogenSampleDate = new DateTime(2016, 6, 1),
                        StationNumber          = 41023,
                        StationName            = "Toowoomba",
                        RunType                = Paddock.RunTypeEnum.LongTerm,
                        StubbleMass            = 100,
                        StubbleType            = "Wheat",
                        Samples                = new List <Sample>()
                        {
                            new Sample()
                            {
                                Thickness = new double[] { 100, 300, 300, 300 },
                                NO3       = new double[] { 34, 6.9, 3.1, 1.8 },
                                NH4       = new double[] { 5.5, 1.8, 1.8, 1.5 },
                                SW        = new double[] { 0.13, 0.18, 0.20, 0.24 },
                                SWUnits   = Sample.SWUnitsEnum.Gravimetric
                            }
                        },
                        SoilPath   = "Soils/Australia/Victoria/Wimmera/Clay (Rupanyup North No742)",
                        Management = new List <Management>()
                        {
                            new Sow()
                            {
                                Crop = "Wheat",
                                Date = new DateTime(2016, 5, 1)
                            }
                        }
                    }
                }
            };

            List <APSIMSpecification> simulations = YieldProphetToAPSIM.ToAPSIM(yp);

            Assert.AreEqual(simulations.Count, 1);
            Assert.AreEqual(simulations[0].Name, "NameOfPaddock");
            Assert.AreEqual(simulations[0].StartDate, new DateTime(1957, 1, 1));
            Assert.AreEqual(simulations[0].EndDate, new DateTime(2016, 6, 30));
            Assert.AreEqual(simulations[0].RunType, APSIMSpecification.RunTypeEnum.Normal);
            Assert.AreEqual(simulations[0].Management.Count, 5);
            Assert.IsTrue(simulations[0].Management[0] is ResetWater);
            Assert.AreEqual(simulations[0].Management[0].Date, new DateTime(2016, 3, 1));
            Assert.IsTrue(simulations[0].Management[0].IsEveryYear);
            Assert.IsTrue(simulations[0].Management[1] is ResetSurfaceOrganicMatter);
            Assert.AreEqual(simulations[0].Management[1].Date, new DateTime(2016, 3, 1));
            Assert.IsTrue(simulations[0].Management[1].IsEveryYear);
            Assert.IsTrue(simulations[0].Management[2] is ResetNitrogen);
            Assert.AreEqual(simulations[0].Management[2].Date, new DateTime(2016, 5, 1));
            Assert.IsTrue(simulations[0].Management[2].IsEveryYear);
            Assert.IsTrue(simulations[0].Management[3] is Sow);
            Assert.AreEqual(simulations[0].Management[3].Date, new DateTime(2016, 5, 1));
            Assert.IsTrue(simulations[0].Management[3].IsEveryYear);
            Assert.IsTrue(simulations[0].Management[4] is ResetNitrogen);
            Assert.AreEqual(simulations[0].Management[4].Date, new DateTime(2016, 6, 1));
            Assert.IsTrue(simulations[0].Management[4].IsEveryYear);

            Assert.IsNotNull(simulations[0].SoilPath);
            Assert.AreEqual(simulations[0].Samples.Count, 1);
            Assert.AreEqual(simulations[0].Samples[0].Thickness, yp.Paddock[0].Samples[0].Thickness);
            Assert.AreEqual(simulations[0].Samples[0].NO3, yp.Paddock[0].Samples[0].NO3);
            Assert.AreEqual(simulations[0].Samples[0].NH4, yp.Paddock[0].Samples[0].NH4);
            Assert.AreEqual(simulations[0].Samples[0].SW, yp.Paddock[0].Samples[0].SW);
            Assert.AreEqual(simulations[0].Samples[0].SWUnits, Sample.SWUnitsEnum.Gravimetric);
        }