public override void StepRun(bool serialize, int i, string variety = null, string parameterName = null, double parameterValue = 0) { #region get items string miName, piName, riName, roiName, siiName, soiName, viName, exName; if (MultiRuns.Count > 0) { var item = MultiRuns[i]; miName = item.ManagementItemSelected; piName = item.ParameterItemSelected; riName = RunItem.Name; roiName = item.RunOptionItemSelected; siiName = item.SiteItemSelected; soiName = item.SoilItemSelected; viName = item.VarietyItemSelected; exName = item.ExperimentSelected; } else { miName = RunItem.Normal.ManagementItem; piName = RunItem.Normal.ParameterItem; riName = RunItem.Name; roiName = RunItem.Normal.RunOptionItem; siiName = RunItem.Normal.SiteItem; soiName = RunItem.Normal.SoilItem; viName = RunItem.Normal.VarietyItem; exName = RunItem.Normal.ExperimentItem; } ManagementItem mi = null; CropParameterItem pi = null; RunOptionItem roi = null; SiteItem sii = null; SoilItem soi = null; CropParameterItem vi = null; RunCore.GetItems(riName, miName, piName, roiName, siiName, soiName, viName, ref mi, ref pi, ref roi, ref sii, ref soi, ref vi); #endregion #region override some parameters if (variety != null) { if (variety == viName) { vi.ParamValue[parameterName] = parameterValue; } } #endregion if (!multiYear) { switch (roi.OutputPattern) { case "V13": ProjectFile.OutputVersion = OutputVersion.V13; break; case "V15": ProjectFile.OutputVersion = OutputVersion.V15; break; case "Cus": ProjectFile.OutputVersion = OutputVersion.Cus; break; case "Maize": ProjectFile.OutputVersion = OutputVersion.Maize; break; } ///<Behnam> ///<Comment> ///Modifications related to estimating sowing window and forcing the model ///to put daily outputs into one file if the names of the daily output files ///are identical for two successive years. ///</Comment> var yy = sii.MaxSowingDate.Year - sii.MinSowingDate.Year; mi.SowingDate = new DateTime(mi.SowingDate.Year, mi.SowingDate.Month, mi.SowingDate.Day); sii.MinSowingDate = new DateTime(mi.SowingDate.Year, sii.MinSowingDate.Month, sii.MinSowingDate.Day); sii.MaxSowingDate = new DateTime(mi.SowingDate.Year + yy, sii.MaxSowingDate.Month, sii.MaxSowingDate.Day); Run.isFirstYear = true; ///</Behnam> //Debug var excelPage = RunCore.Run(riName, exportNormalRuns, false, mi, pi, roi, sii, soi, vi); ///<Behnam> if (i == 0) { Book.Clear(); var outputFile = OutputFile.ExtractMultiRunHeader(); outputFile.Title = OutputFileName; Book.Add(outputFile); } if (exportNormalRuns) { var dailyOutputFileName = OutputPatternVarDef.OutputPatternValue(dailyOutputPattern, miName, piName, riName, roiName, siiName, soiName, viName, exName) + RunItem.Normal.OutputExtention; dailyOutputFileName = dailyOutputFileName.Replace(VarMultiYearSowingYear, ""); excelPage.Title = dailyOutputFileName; Book.Add(excelPage); if (serialize) { RunCore.Save(excelPage, AbsoluteOutputDirectory + "\\" + dailyOutputFileName); } if (i == 0) { warningFile = OutputFile.ExtractWarningsHeader(); warningFile.Title = "warnings"; } LineData line = OutputFile.ExtractWarningsMultiRun(RunCore.RunInstance); if (line != null) { warningFile.Add(line); } } if (i == 0 && ProjectFile.OutputVersion == OutputVersion.Cus) { Run.SecondLine = false; Book[0].Add(OutputFile.ExtractMultiRunLine(RunCore.RunInstance)); } ///</Behnam> Book[0].Add(OutputFile.ExtractMultiRunLine(RunCore.RunInstance)); /// Behnam (2016.06.01): Added to write the summaty file after each run is finished. /// Other way, if one simulations is not completed, the summary file is not pronted at all. if (serialize) { RunCore.Save(Book[0], AbsoluteOutputPath); } } else { mi = mi.Clone(); ///<Behnam> switch (roi.OutputPattern) { case "V13": ProjectFile.OutputVersion = OutputVersion.V13; break; case "V15": ProjectFile.OutputVersion = OutputVersion.V15; break; case "Cus": ProjectFile.OutputVersion = OutputVersion.Cus; break; case "Maize": ProjectFile.OutputVersion = OutputVersion.Maize; break; } for (var j = firstYear; j <= lastYear; ++j) { foreach (var dateApp in mi.DateApplications) { dateApp.Date = new DateTime(j + (dateApp.Date.Year - mi.SowingDate.Year), dateApp.Date.Month, dateApp.Date.Day); } var yy = sii.MaxSowingDate.Year - sii.MinSowingDate.Year; mi.SowingDate = new DateTime(j, mi.SowingDate.Month, mi.SowingDate.Day); sii.MinSowingDate = new DateTime(j, sii.MinSowingDate.Month, sii.MinSowingDate.Day); sii.MaxSowingDate = new DateTime(j + yy, sii.MaxSowingDate.Month, sii.MaxSowingDate.Day); Run.isFirstYear = (j == firstYear); var excelPage = RunCore.Run(riName, exportNormalRuns, false, mi, pi, roi, sii, soi, vi); if (i == 0 && j == firstYear) { Book.Clear(); var outputFile = OutputFile.ExtractMultiRunHeader(); outputFile.Title = OutputFileName; Book.Add(outputFile); } if (exportNormalRuns) { var dailyOutputFileName = OutputPatternVarDef.OutputPatternValue(dailyOutputPattern, miName, piName, riName, roiName, siiName, soiName, viName, exName) + RunItem.Normal.OutputExtention; dailyOutputFileName = dailyOutputFileName.Replace(VarMultiYearSowingYear, sii.MinSowingDate.Year.ToString()); excelPage.Title = dailyOutputFileName; Book.Add(excelPage); if (j == firstYear || (j != firstYear && oldDailyOutputFileName != dailyOutputFileName)) { if (serialize) { RunCore.Save(excelPage, AbsoluteOutputDirectory + "\\" + dailyOutputFileName, true); } } else { if (serialize) { RunCore.Save(excelPage, AbsoluteOutputDirectory + "\\" + dailyOutputFileName, false); } } oldDailyOutputFileName = dailyOutputFileName; } if (i == 0 && j == firstYear && ProjectFile.OutputVersion == OutputVersion.Cus) { Run.SecondLine = false; Book[0].Add(OutputFile.ExtractMultiRunLine(RunCore.RunInstance)); } Book[0].Add(OutputFile.ExtractMultiRunLine(RunCore.RunInstance)); /// Behnam (2016.06.01): Added to write the summaty file after each run is finished. /// Other way, if one simulations is not completed, the summary file is not pronted at all. if (serialize) { RunCore.Save(Book[0], AbsoluteOutputPath); } ///</Behnam> //Warnings if (i == 0 && j == firstYear) { warningFile = OutputFile.ExtractWarningsHeader(); warningFile.Title = "warnings"; } LineData line = OutputFile.ExtractWarningsMultiRun(RunCore.RunInstance); if (line != null) { warningFile.Add(line); } } } if (i == (MultiRuns.Count - 1)) { Book.Add(warningFile); } }
public override void StepRun(bool serialize, int i, string variety = null, string parameterName = null, double parameterValue = 0) { BindingList <MultiRunItem> multiRuns = RunItem.Multi.MultiRuns; var multiYear = RunItem.Multi.MultiYear; #region get items string miName, piName, riName, roiName, siiName, soiName, viName, exName; if (multiRuns.Count > 0) { var item = multiRuns[i]; miName = item.ManagementItemSelected; piName = item.ParameterItemSelected; riName = RunItem.Name; roiName = item.RunOptionItemSelected; siiName = item.SiteItemSelected; soiName = item.SoilItemSelected; viName = item.VarietyItemSelected; exName = item.ExperimentSelected; } else { miName = RunItem.Normal.ManagementItem; piName = RunItem.Normal.ParameterItem; riName = RunItem.Name; roiName = RunItem.Normal.RunOptionItem; siiName = RunItem.Normal.SiteItem; soiName = RunItem.Normal.SoilItem; viName = RunItem.Normal.VarietyItem; exName = RunItem.Normal.ExperimentItem; } ManagementItem mi = null; CropParameterItem pi = null; RunOptionItem roi = null; SiteItem sii = null; SoilItem soi = null; CropParameterItem vi = null; RunCore.GetItems(riName, miName, piName, roiName, siiName, soiName, viName, ref mi, ref pi, ref roi, ref sii, ref soi, ref vi); mi = mi.Clone(); pi = pi.Clone(); roi = roi.Clone(); sii = sii.Clone(); soi = soi.Clone(); vi = vi.Clone(); #endregion ///<Behnam> var deltaHeader = new string[SensitivityRuns.Count]; var index1 = 0; if (oneByOne) { nbSensitivityStep = 1; SensitivityRuns.Iterate( delegate(SensitivityRunItem item) { nbSensitivityStep += item.NbStep; deltaHeader[index1++] = item.GetHeader(); }); } else { nbSensitivityStep = 1; SensitivityRuns.Iterate( delegate(SensitivityRunItem item) { nbSensitivityStep *= item.NbStep; deltaHeader[index1++] = item.GetHeader(); }); } ///</Behnam> var deltaValue = new double[SensitivityRuns.Count]; int[] index = { 0 }; if (!multiYear) { ///<Behnam> switch (roi.OutputPattern) { case "V13": ProjectFile.OutputVersion = OutputVersion.V13; break; case "V15": ProjectFile.OutputVersion = OutputVersion.V15; break; case "Cus": ProjectFile.OutputVersion = OutputVersion.Cus; break; case "Maize": ProjectFile.OutputVersion = OutputVersion.Maize; break; } ///</Behnam> var yy = sii.MaxSowingDate.Year - sii.MinSowingDate.Year; mi.SowingDate = new DateTime(mi.SowingDate.Year, mi.SowingDate.Month, mi.SowingDate.Day); sii.MinSowingDate = new DateTime(mi.SowingDate.Year, sii.MinSowingDate.Month, sii.MinSowingDate.Day); sii.MaxSowingDate = new DateTime(mi.SowingDate.Year + yy, sii.MaxSowingDate.Month, sii.MaxSowingDate.Day); Run.isFirstYear = true; ///</Behnam> var end = false; InitStepSensitivity(mi, pi, roi, sii, soi, vi, deltaValue, index); var stepIndex = 0; while (!end) { var excelPage = RunCore.Run(riName, exportNormalRuns, false, mi, pi, roi, sii, soi, vi); var deltaSensitivity = ""; index[0] = 0; SensitivityRuns.Iterate( item => { deltaValue[index[0]] = item.GetValue(mi, pi, roi, sii, soi, vi); deltaSensitivity += item.GetHeader() + "_" + Math.Round(deltaValue[index[0]], 4) + "_"; ++index[0]; }); ///<Behnam> if (i == 0 && stepIndex == 0) { Book.Clear(); var outputFile = OutputFile.ExtractSensitivityRunHeader(deltaHeader); outputFile.Title = OutputFileName; Book.Add(outputFile); } if (exportNormalRuns) { var dailyOutputFileName = OutputPatternVarDef.OutputPatternValue(dailyOutputPattern, miName, piName, riName, roiName, siiName, soiName, viName, exName) + RunItem.Normal.OutputExtention; dailyOutputFileName = dailyOutputFileName.Replace(VarDeltaSensitivity, deltaSensitivity); dailyOutputFileName = dailyOutputFileName.Replace(RunItemModeMulti.VarMultiYearSowingYear, ""); excelPage.Title = dailyOutputFileName; Book.Add(excelPage); if (serialize) { RunCore.Save(excelPage, AbsoluteOutputDirectory + "\\" + dailyOutputFileName); } } if (i == 0 && ProjectFile.OutputVersion == OutputVersion.Cus && stepIndex == 0) { Run.SecondLine = false; Book[0].Add(OutputFile.ExtractSensitivityRunLine(RunCore.RunInstance, deltaHeader, deltaValue)); } Book[0].Add(OutputFile.ExtractSensitivityRunLine(RunCore.RunInstance, deltaHeader, deltaValue)); ///</Behnam> end = Increment(stepIndex, mi, pi, roi, sii, soi, vi); ++stepIndex; } } else { ///<Behnam> switch (roi.OutputPattern) { case "V13": ProjectFile.OutputVersion = OutputVersion.V13; break; case "V15": ProjectFile.OutputVersion = OutputVersion.V15; break; case "Cus": ProjectFile.OutputVersion = OutputVersion.Cus; break; case "Maize": ProjectFile.OutputVersion = OutputVersion.Maize; break; } ///</Behnam> var firstYear = RunItem.Multi.FirstYear; var lastYear = RunItem.Multi.LastYear; for (var j = firstYear; j <= lastYear; ++j) { foreach (var dateApp in mi.DateApplications) { dateApp.Date = new DateTime(j + (dateApp.Date.Year - mi.SowingDate.Year), dateApp.Date.Month, dateApp.Date.Day); } ///<Behnam> var yy = sii.MaxSowingDate.Year - sii.MinSowingDate.Year; mi.SowingDate = new DateTime(j, mi.SowingDate.Month, mi.SowingDate.Day); sii.MinSowingDate = new DateTime(j, sii.MinSowingDate.Month, sii.MinSowingDate.Day); sii.MaxSowingDate = new DateTime(j + yy, sii.MaxSowingDate.Month, sii.MaxSowingDate.Day); Run.isFirstYear = (j == firstYear); ///</Behnam> var end = false; InitStepSensitivity(mi, pi, roi, sii, soi, vi, deltaValue, index); var stepIndex = 0; while (!end) { var excelPage = RunCore.Run(riName, exportNormalRuns, false, mi, pi, roi, sii, soi, vi); var deltaSensitivity = ""; index[0] = 0; var index0 = index[0]; SensitivityRuns.Iterate( delegate(SensitivityRunItem item) { deltaValue[index0] = item.GetValue(mi, pi, roi, sii, soi, vi); deltaSensitivity += item.GetHeader() + "_" + Math.Round(deltaValue[index[0]], 4) + "_"; ++index0; }); ///<Behnam (2016.11.12)> if (i == 0 && j == firstYear && stepIndex == 0) { Book.Clear(); var outputFile = OutputFile.ExtractSensitivityRunHeader(deltaHeader); outputFile.Title = OutputFileName; Book.Add(outputFile); } if (exportNormalRuns) { var dailyOutputFileName = OutputPatternVarDef.OutputPatternValue(dailyOutputPattern, miName, piName, riName, roiName, siiName, soiName, viName, exName) + RunItem.Normal.OutputExtention; dailyOutputFileName = dailyOutputFileName.Replace(VarDeltaSensitivity, deltaSensitivity); dailyOutputFileName = dailyOutputFileName.Replace(RunItemModeMulti.VarMultiYearSowingYear, mi.FinalSowingDate.Year.ToString()); excelPage.Title = dailyOutputFileName; Book.Add(excelPage); if ((j == firstYear || (j != firstYear && oldDailyOutputFileName != dailyOutputFileName)) && stepIndex == 0) { if (serialize) { RunCore.Save(excelPage, AbsoluteOutputDirectory + "\\" + dailyOutputFileName, true); } } else { if (serialize) { RunCore.Save(excelPage, AbsoluteOutputDirectory + "\\" + dailyOutputFileName, false); } } oldDailyOutputFileName = dailyOutputFileName; } if (i == 0 && j == firstYear && ProjectFile.OutputVersion == OutputVersion.Cus && stepIndex == 0) { Run.SecondLine = false; Book[0].Add(OutputFile.ExtractSensitivityRunLine(RunCore.RunInstance, deltaHeader, deltaValue)); } Book[0].Add(OutputFile.ExtractSensitivityRunLine(RunCore.RunInstance, deltaHeader, deltaValue)); ///</Behnam> end = Increment(stepIndex, mi, pi, roi, sii, soi, vi); ++stepIndex; } } } }