/// <summary>Creates a met factorial.</summary>
        /// <param name="factor">The factor</param>
        public static void ApplyFactor(XmlNode simulationXML, APSIMSpecification.Factor factor)
        {
            // Make sure <factorial> exists.
            XmlNode factorial = XmlUtilities.Find(simulationXML, "Factorials");

            if (factorial == null)
            {
                factorial = simulationXML.AppendChild(simulationXML.OwnerDocument.CreateElement("factorial"));
            }
            XmlUtilities.SetNameAttr(factorial, "Factorials");
            XmlUtilities.SetValue(factorial, "active", "1");

            // Add a <factor>
            XmlNode factorNode = factorial.AppendChild(factorial.OwnerDocument.CreateElement("factor"));

            XmlUtilities.SetNameAttr(factorNode, factor.Name);

            // Set <targets> in factor
            XmlUtilities.SetValue(factorNode, "targets/Target", factor.ComponentPath);

            // Set <vars> in met factor
            string valuesCSV = StringUtilities.BuildString(factor.ComponentVariableValues, ",");

            XmlUtilities.SetValue(factorNode, "vars/" + factor.ComponentVariableName, valuesCSV);

            // Find target component.
            XmlNode target = XmlUtilities.Find(simulationXML.OwnerDocument.DocumentElement, factor.ComponentPath);

            if (target == null)
            {
                throw new Exception("Cannot find target of factor. Target path: " + factor.ComponentPath);
            }

            // Add a component to the factor node.
            factorNode.AppendChild(simulationXML.OwnerDocument.ImportNode(target, true));
        }
Exemple #2
0
        /// <summary>Creates the weather files for all simulations.</summary>
        /// <param name="simulations">The simulations.</param>
        /// <param name="workingFolder">The working folder to create the files in.</param>
        /// <param name="allSimulationsAreSingleSeason">All simulations are short season?</param>
        private static void CreateWeatherFilesForSimulations(APSIMSpecification simulation, string workingFolder, WeatherFileCache weatherCache, bool allSimulationsAreSingleSeason)
        {
            if (simulation.ErrorMessage == null)
            {
                string rainFileName = Path.Combine(workingFolder, simulation.Name + ".met");

                string[] filesCreated = null;

                // Make sure the observed data has a codes column.
                if (simulation.ObservedData != null)
                {
                    Weather.AddCodesColumn(simulation.ObservedData, 'O');
                }

                if (simulation.RunType == APSIMSpecification.RunTypeEnum.LongTermPatched)
                {
                    // long term.
                    DateTime longTermStartDate = new DateTime(simulation.LongtermStartYear, 1, 1);
                    int      numYears          = simulation.StartDate.Year - longTermStartDate.Year + 1;

                    // Check to see if in cache.
                    filesCreated = weatherCache.GetWeatherFiles(simulation.StationNumber,
                                                                simulation.StartDate, simulation.NowDate,
                                                                simulation.ObservedData.TableName, numYears);
                    if (filesCreated == null)
                    {
                        // Create a long term weather file.
                        filesCreated = Weather.CreateLongTerm(rainFileName, simulation.StationNumber,
                                                              simulation.StartDate, simulation.NowDate,
                                                              simulation.ObservedData, simulation.DecileDate, numYears);
                        weatherCache.AddWeatherFiles(simulation.StationNumber,
                                                     simulation.StartDate, simulation.NowDate,
                                                     simulation.ObservedData.TableName, numYears, filesCreated);
                    }
                }
                else
                {
                    // short term.
                    // Create a short term weather file.
                    Weather.Data weatherFile = Weather.ExtractDataFromSILO(simulation.StationNumber, simulation.StartDate, simulation.EndDate);
                    Weather.OverlayData(simulation.ObservedData, weatherFile.Table);
                    Weather.WriteWeatherFile(weatherFile.Table, rainFileName, weatherFile.Latitude, weatherFile.Longitude,
                                             weatherFile.TAV, weatherFile.AMP);
                    filesCreated = new string[] { rainFileName };
                }

                if (filesCreated.Length > 0)
                {
                    simulation.WeatherFileName = Path.GetFileName(filesCreated[0]);

                    // Set the simulation end date to the end date of the weather file. This will avoid
                    // problems where SILO hasn't been updated for a while.
                    ApsimTextFile weatherFile = new ApsimTextFile();
                    try
                    {
                        weatherFile.Open(filesCreated[0]);
                        simulation.EndDate = weatherFile.LastDate;
                    }
                    finally
                    {
                        weatherFile.Close();
                    }
                }

                if (!allSimulationsAreSingleSeason)
                {
                    APSIMSpecification.Factor factor = new APSIMSpecification.Factor();
                    factor.Name                    = "Met";
                    factor.ComponentPath           = "/Simulations/" + simulation.Name + "/Met";
                    factor.ComponentVariableName   = "filename";
                    factor.ComponentVariableValues = filesCreated;
                    if (simulation.Factors == null)
                    {
                        simulation.Factors = new List <APSIMSpecification.Factor>();
                    }
                    simulation.Factors.Add(factor);
                }
            }
        }
 /// <summary>Creates a met factorial.</summary>
 /// <param name="factor">The factor</param>
 public static void ApplyFactor(XmlNode simulationXML, APSIMSpecification.Factor factor)
 {
 }