/// <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); }
/// <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); } } }