示例#1
0
        /// <summary>
        /// Update the version of the specified file.
        /// </summary>
        /// <param name="fileName"></param>
        public static void UpdateFile(string fileName)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(fileName);
            string version = XmlUtilities.Value(doc.DocumentElement, "Version");

            if (version != new YieldProphet().Version.ToString())
            {
                YieldProphet ypSpec = YieldProphetOld.YieldProphetFromXML(doc.DocumentElement, Path.GetDirectoryName(fileName));
                string       xml    = XmlUtilities.Serialise(ypSpec, false, null);
                File.WriteAllText(fileName, xml);
            }
        }
示例#2
0
        /// <summary>
        /// Adds the job to the APSIM cloud.
        /// </summary>
        /// <param name="yieldProphet">The job specification.</param>
        /// <returns>The unique job name.</returns>
        public string Add(YieldProphet yieldProphet)
        {
            DateTime nowDate = DateTime.Now;
            if (yieldProphet.Paddock.Count > 0 && yieldProphet.Paddock[0].NowDate != DateTime.MinValue)
                nowDate = yieldProphet.Paddock[0].NowDate;

            foreach (Paddock paddock in yieldProphet.Paddock)
                paddock.NowDate = nowDate;
            string newJobName = DateTime.Now.ToString("yyyy-MM-dd (HH-mm-ss) ") + yieldProphet.ReportName;

            string xml = YieldProphetUtility.YieldProphetToXML(yieldProphet);

            AddAsXML(newJobName, xml);
            return newJobName;
        }
示例#3
0
        /// <summary>Converts the old Yield Prophet XML to new XML format capable of deserialisation</summary>
        /// <param name="yieldProphetXML">The old Yield Prophet XML</param>
        /// <returns>The new Yield Prophet XML</returns>
        public static YieldProphet YieldProphetFromXML(XmlNode node, string baseFolder)
        {
            List <Paddock> simulations = new List <Paddock>();

            List <XmlNode> paddocks = XmlUtilities.ChildNodes(node, "Paddock");

            for (int p = 0; p < paddocks.Count; p++)
            {
                try
                {
                    Paddock paddock = CreateSimulationSpec(paddocks[p], baseFolder);
                    simulations.Add(paddock);
                }
                catch (Exception err)
                {
                    string name = XmlUtilities.Value(paddocks[p], "Name");
                    throw new Exception(err.Message + "\r\nPaddock name: " + name);
                }
            }

            YieldProphet simulationsSpec = new YieldProphet
            {
                Paddock = simulations
            };

            // Some top level simulation metadata.
            string reportDescription = XmlUtilities.Value(node, "ReportDescription");

            if (reportDescription != "")
            {
                simulationsSpec.ReportName = reportDescription;
            }
            string reportType = XmlUtilities.Value(node, "ReportType");

            if (reportType != string.Empty)
            {
                throw new Exception("Not sure what to do with a ReportType of " + reportType);
            }

            return(simulationsSpec);
        }
示例#4
0
        /// <summary>Convert the YieldProphet spec to XML.</summary>
        /// <returns>The XML string.</returns>
        public static string YieldProphetToXML(YieldProphet yieldProphet)
        {
            XmlSerializer           serial = new XmlSerializer(typeof(YieldProphet));
            XmlSerializerNamespaces ns     = new XmlSerializerNamespaces();

            ns.Add("", "");
            StringWriter writer = new StringWriter();

            serial.Serialize(writer, yieldProphet, ns);
            string xml = writer.ToString();

            // The code below can cause out of memory error with very large number of simulations
            //if (xml.Length > 5 && xml.Substring(0, 5) == "<?xml")
            //{
            //    // remove the first line: <?xml version="1.0"?>/n
            //    int posEol = xml.IndexOf("\n");
            //    if (posEol != -1)
            //        return xml.Substring(posEol + 1);
            //}
            return(xml);
        }
示例#5
0
        /// <summary>Converts a Yield Prophet specification to an APSIM one.</summary>
        /// <param name="yieldProphet">The yield prophet spec.</param>
        /// <returns>The list of APSIM simulations that will need to be run.</returns>
        public static List <APSIMSpecification> ToAPSIM(YieldProphet yieldProphet)
        {
            List <APSIMSpecification> apsimSpecs = new List <APSIMSpecification>();

            foreach (Paddock paddock in yieldProphet.Paddock)
            {
                APSIMSpecification simulation;
                try
                {
                    simulation = CreateBaseSimulation(paddock);
                }
                catch (Exception err)
                {
                    simulation = new APSIMSpecification();
                    simulation.ErrorMessage = err.Message;
                }

                simulation.Name = paddock.Name;
                apsimSpecs.Add(simulation);
            }
            return(apsimSpecs);
        }
        /// <summary>Converts the old Yield Prophet XML to new XML format capable of deserialisation</summary>
        /// <param name="yieldProphetXML">The old Yield Prophet XML</param>
        /// <returns>The new Yield Prophet XML</returns>
        public static YieldProphet YieldProphetFromXML(XmlNode node, string baseFolder)
        {
            List<Paddock> simulations = new List<Paddock>();

            List<XmlNode> paddocks = XmlUtilities.ChildNodes(node, "Paddock");
            for (int p = 0; p < paddocks.Count; p++)
            {
                try
                {
                    Paddock paddock = CreateSimulationSpec(paddocks[p], baseFolder);
                    simulations.Add(paddock);
                }
                catch (Exception err)
                {
                    string name = XmlUtilities.Value(paddocks[p], "Name");
                    throw new Exception(err.Message + "\r\nPaddock name: " + name);
                }
            }

            YieldProphet simulationsSpec = new YieldProphet();
            simulationsSpec.Paddock = simulations;

            // Some top level simulation metadata.
            string reportDescription = XmlUtilities.Value(node, "ReportDescription");
            if (reportDescription != "")
                simulationsSpec.ReportName = reportDescription;
            string reportType = XmlUtilities.Value(node, "ReportType");
            if (reportType == "Crop Report (Complete)")
                simulationsSpec.ReportType = YieldProphet.ReportTypeEnum.Crop;
            else if (reportType == "Sowing Opportunity Report")
                simulationsSpec.ReportType = YieldProphet.ReportTypeEnum.SowingOpportunity;
            simulationsSpec.ClientName = XmlUtilities.Value(node, "GrowerName");
            simulationsSpec.ReportGeneratedBy = XmlUtilities.Value(node, "LoginName");

            return simulationsSpec;
        }
 /// <summary>Convert the YieldProphet spec to XML.</summary>
 /// <returns>The XML string.</returns>
 public static string YieldProphetToXML(YieldProphet yieldProphet)
 {
     XmlSerializer serial = new XmlSerializer(typeof(YieldProphet));
     XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
     ns.Add("", "");
     StringWriter writer = new StringWriter();
     serial.Serialize(writer, yieldProphet, ns);
     string xml = writer.ToString();
     if (xml.Length > 5 && xml.Substring(0, 5) == "<?xml")
     {
         // remove the first line: <?xml version="1.0"?>/n
         int posEol = xml.IndexOf("\n");
         if (posEol != -1)
             return xml.Substring(posEol + 1);
     }
     return xml;
 }
示例#8
0
        /// <summary>Initialise job manager</summary>
        private void Initialise(bool createSims = true)
        {
            Errors = new List <string>();
            try
            {
                // Create a working directory.
                WorkingDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                Directory.CreateDirectory(WorkingDirectory);

                // Create a YieldProphet object if xml was provided
                string fileBaseToWrite = "YieldProphet";
                if (specXMLToRun != null)
                {
                    if (specXMLToRun.Contains("<YieldProphet>"))
                    {
                        specToRun      = YieldProphetUtility.YieldProphetFromXML(specXMLToRun, WorkingDirectory);
                        allSimulations = YieldProphetToAPSIM.ToAPSIM(specToRun);
                        if (specToRun.ReportName != null)
                        {
                            fileBaseToWrite = specToRun.ReportName;
                        }
                    }
                    else
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(specXMLToRun);
                        allSimulations = XmlUtilities.Deserialise(doc.DocumentElement, typeof(List <APSIMSpecification>)) as List <APSIMSpecification>;
                    }
                }
                else if (specToRun != null)
                {
                    allSimulations = YieldProphetToAPSIM.ToAPSIM(specToRun);
                }

                // Create all the files needed to run APSIM.
                string fileToWrite;
                if (environment.APSIMxBuildNumber > 0)
                {
                    fileToWrite = fileBaseToWrite + ".apsimx";
                }
                else
                {
                    fileToWrite = fileBaseToWrite + ".apsim";
                }
                string apsimFileName = APSIMFiles.Create(allSimulations, WorkingDirectory, fileToWrite);

                // Save YieldProphet.xml to working folder.
                if (specToRun != null)
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(YieldProphetUtility.YieldProphetToXML(specToRun));
                    doc.Save(Path.Combine(WorkingDirectory, fileBaseToWrite + ".xml"));
                }

                if (createSims)
                {
                    // Setup the runtime environment.
                    binFolder = SetupRunTimeEnvironment(environment);

                    // Go find APSIM executable
                    if (environment.APSIMxBuildNumber > 0)
                    {
                        jobsToRun = GetJobToRunAPSIMX(apsimFileName, binFolder);
                    }
                    else
                    {
                        jobsToRun = GetJobToRunAPSIMClassic(apsimFileName, binFolder);
                    }
                }

                // Copy all errors to our errors list.
                foreach (var sim in allSimulations.FindAll(sim => sim.ErrorMessage != null))
                {
                    Errors.Add(sim.ErrorMessage);
                }
            }
            catch (Exception err)
            {
                Errors.Add(err.ToString());
            }
        }
示例#9
0
 /// <summary>Constructor</summary>
 /// <param name="yp">The YieldProphet spec to run</param>
 /// <param name="environment">The runtime environment to use for the run</param>
 public RunYPJob(YieldProphet yp, RuntimeEnvironment environment)
 {
     specToRun        = yp;
     this.environment = environment;
     Initialise();
 }