Ejemplo n.º 1
0
        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;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public override int InitRun(bool serialize)
        {
            ///<Behnam 92015.12.08)>
            #region get items
            string miName, piName, riName, roiName, siiName, soiName, viName, exName;
            BindingList <MultiRunItem> multiRuns = RunItem.Multi.MultiRuns;
            var multiYear = RunItem.Multi.MultiYear;

            if (multiRuns.Count > 0)
            {
                var item = multiRuns[0];
                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();

            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;
            }
            #endregion
            ///</Behnam>

            if (RunItem.Multi.MultiYear && RunItem.Multi.FirstYear > RunItem.Multi.LastYear)
            {
                throw new RunException(RunItem.Name, "First year (" + RunItem.Multi.FirstYear + ") must be <= to last year (" + RunItem.Multi.LastYear + ")");
            }

            var deltaHeader = new string[SensitivityRuns.Count];
            var index       = 0;

            if (oneByOne)
            {
                nbSensitivityStep = 1;
                SensitivityRuns.Iterate(
                    delegate(SensitivityRunItem item)
                {
                    nbSensitivityStep   += item.NbStep;
                    deltaHeader[index++] = item.GetHeader();
                });
            }
            else
            {
                nbSensitivityStep = 1;
                SensitivityRuns.Iterate(
                    delegate(SensitivityRunItem item)
                {
                    nbSensitivityStep   *= item.NbStep;
                    deltaHeader[index++] = item.GetHeader();
                });
            }

            if (Book != null)
            {
                Book.Clear();
            }
            else
            {
                Book = new Book();
            }
            var page = OutputFile.ExtractSensitivityRunHeader(deltaHeader);
            page.Title = OutputFileName;
            Book.Add(page);
            return(Math.Max(1, RunItem.Multi.MultiRuns.Count));
        }