Beispiel #1
0
        /// <summary>Create all necessary YP files (.apsim and .met) from a YieldProphet spec.</summary>
        /// <param name="simulations">The simulations to write.</param>
        /// <param name="workingFolder">The folder where files shoud be created.</param>
        /// <param name="fileNameToWrite">The name of a file to write.</param>
        /// <returns>The name of the created .apsim file.</returns>
        public static string Create(List <APSIMSpecification> simulations, string workingFolder, string fileNameToWrite)
        {
            bool usingAPSIMx = Path.GetExtension(fileNameToWrite) == ".apsimx";

            // Create the .apsim XML
            XmlDocument doc = new XmlDocument();

            if (usingAPSIMx)
            {
                doc.AppendChild(doc.CreateElement("Simulations"));
                XmlUtilities.SetValue(doc.DocumentElement, "Name", "Simulations");
                XmlUtilities.SetValue(doc.DocumentElement, "DataStore/Name", "DataStore");
            }
            else
            {
                doc.AppendChild(doc.CreateElement("folder"));
                XmlUtilities.SetNameAttr(doc.DocumentElement, "Simulations");
                XmlUtilities.SetAttribute(doc.DocumentElement, "version", APSIMVerionNumber.ToString());
            }

            // Determine whether all simulations are single season.
            bool allSimulationsAreSingleSeason = true;

            foreach (APSIMSpecification simulation in simulations)
            {
                if (simulation.RunType != APSIMSpecification.RunTypeEnum.Normal)
                {
                    allSimulationsAreSingleSeason = false;
                }
            }

            WeatherFileCache weatherCache = new WeatherFileCache();

            foreach (APSIMSpecification simulation in simulations.FindAll(sim => sim.ErrorMessage == null))
            {
                try
                {
                    CreateWeatherFilesForSimulations(simulation, workingFolder, weatherCache, allSimulationsAreSingleSeason);
                    CreateApsimFile(simulation, workingFolder, doc.DocumentElement, usingAPSIMx);
                }
                catch (Exception err)
                {
                    simulation.ErrorMessage = simulation.Name + ": " + err.Message;
                }
            }

            // Apply factors.
            if (!usingAPSIMx)
            {
                foreach (APSIMSpecification simulation in simulations)
                {
                    if (simulation.ErrorMessage == null && simulation.Factors != null)
                    {
                        foreach (APSIMSpecification.Factor factor in simulation.Factors)
                        {
                            APSIMFileWriter.ApplyFactor(doc.DocumentElement, factor);
                        }
                    }
                }
            }

            // Write the .apsim file.
            string apsimFileName = Path.Combine(workingFolder, fileNameToWrite);

            File.WriteAllText(apsimFileName, XmlUtilities.FormattedXML(doc.DocumentElement.OuterXml));

            // Write a .spec file.
            string apsimRunFileName = Path.Combine(workingFolder, Path.ChangeExtension(fileNameToWrite, ".spec"));
            string xml = XmlUtilities.Serialise(simulations, false);

            File.WriteAllText(apsimRunFileName, xml);

            return(apsimFileName);
        }
Beispiel #2
0
        /// <summary>
        /// Create a one year APSIM simulation for the specified yield prophet specification
        /// and paddock
        /// </summary>
        /// <param name="simulation">The specification to use</param>
        /// <param name="workingFolder">The folder where files shoud be created.</param>
        /// <param name="usingAPSIMx">Write APSIMx files?</param>
        /// <returns>The XML node of the APSIM simulation.</returns>
        private static XmlNode CreateSimulationXML(APSIMSpecification simulation, string workingFolder, bool usingAPSIMx)
        {
            IAPSIMFileWriter apsimWriter;

            if (usingAPSIMx)
            {
                apsimWriter = new APSIMxFileWriter();
            }
            else
            {
                apsimWriter = new APSIMFileWriter();
            }

            // Name the paddock.
            apsimWriter.NameSimulation(simulation.Name);

            // Set the clock start and end dates.
            apsimWriter.SetStartEndDate(simulation.StartDate, simulation.EndDate);

            // Set the report date.
            apsimWriter.SetReportDate(simulation.NowDate);

            // Set the weather file
            apsimWriter.SetWeatherFile(simulation.WeatherFileName);

            // Set the stubble
            apsimWriter.SetStubble(simulation.StubbleType, simulation.StubbleMass, YieldProphetUtility.GetStubbleCNRatio(simulation.StubbleType));

            // Set NUnlimited
            if (simulation.NUnlimited)
            {
                apsimWriter.SetNUnlimited();
            }

            // Set NUnlimited from today
            if (simulation.NUnlimitedFromToday)
            {
                apsimWriter.SetNUnlimitedFromToday();
            }

            if (simulation.WriteDepthFile)
            {
                apsimWriter.WriteDepthFile();
            }

            if (simulation.Next10DaysDry)
            {
                apsimWriter.Next10DaysDry();
            }

            apsimWriter.SetErosion(simulation.Slope, simulation.SlopeLength);

            // Do soil stuff.
            DoSoil(simulation, workingFolder);
            apsimWriter.SetSoil(simulation.Soil);

            // Loop through all management actions and create an operations list
            foreach (Management management in simulation.Management)
            {
                if (management is Sow)
                {
                    apsimWriter.AddSowingOperation(management as Sow, simulation.UseEC);
                }
                else if (management is Fertilise)
                {
                    apsimWriter.AddFertilseOperation(management as Fertilise);
                }
                else if (management is Irrigate)
                {
                    apsimWriter.AddIrrigateOperation(management as Irrigate);
                }
                else if (management is Tillage)
                {
                    apsimWriter.AddTillageOperation(management as Tillage);
                }
                else if (management is StubbleRemoved)
                {
                    apsimWriter.AddStubbleRemovedOperation(management as StubbleRemoved);
                }
                else if (management is ResetWater)
                {
                    apsimWriter.AddResetWaterOperation(management as ResetWater);
                }
                else if (management is ResetNitrogen)
                {
                    apsimWriter.AddResetNitrogenOperation(management as ResetNitrogen);
                }
                else if (management is ResetSurfaceOrganicMatter)
                {
                    apsimWriter.AddSurfaceOrganicMatterOperation(management as ResetSurfaceOrganicMatter);
                }
            }

            // Set Daily output
            if (simulation.DailyOutput)
            {
                apsimWriter.SetDailyOutput();
            }

            // Set Monthly output
            if (simulation.MonthlyOutput)
            {
                apsimWriter.SetMonthlyOutput();
            }

            // Set Yearly output
            if (simulation.YearlyOutput)
            {
                apsimWriter.SetYearlyOutput();
            }

            return(apsimWriter.ToXML());
        }