/// <summary>
        /// This gathers the bootstrap file, and stores it with the the GUI input under the "AGEPRO" subdirectory
        /// in the desginagted user document directory. After the calcuation engine is done, the function will
        /// attempt to display the AGEPRO calcuation engine output file (if requested) and the directory the
        /// outputs were written to.
        /// </summary>
        public void LaunchAgeproModel(AgeproInputFile ageproData, string inputFile = "")
        {
            string ageproModelJobName;
            string jobDT;

            //Set the user data work directory
            if (string.IsNullOrWhiteSpace(inputFile))
            {
                ageproModelJobName = "untitled_";
                jobDT = string.Format(ageproModelJobName + "_{0:yyyy-MM-dd_HH-mm-ss}", DateTime.Now);
            }
            else
            {
                ageproModelJobName = Path.GetFileNameWithoutExtension(inputFile);
                //Remove potential invalid filename characters
                foreach (char c in Path.GetInvalidFileNameChars())
                {
                    ageproModelJobName = ageproModelJobName.Replace(c.ToString(), "");
                }
                jobDT = string.Format(ageproModelJobName + "_{0:yyyy-MM-dd_HH-mm-ss}", DateTime.Now);
            }
            string ageproWorkPath = Path.Combine(Util.GetAgeproUserDataPath(), jobDT);
            string inpFile        = Path.Combine(ageproWorkPath, ageproModelJobName + ".INP");
            string bsnFile        = Path.Combine(ageproWorkPath, ageproModelJobName + ".BSN");

            if (!Directory.Exists(ageproWorkPath))
            {
                _ = Directory.CreateDirectory(ageproWorkPath);
            }

            //check for bootstrap file
            //1. File Exists from the bootstrap parameter
            if (File.Exists(ageproData.Bootstrap.BootstrapFile))
            {
                File.Copy(ageproData.Bootstrap.BootstrapFile, bsnFile, true);
            }
            //2. If not, in the same directory as the AGEPRO Input File
            else if (File.Exists($"{Path.GetDirectoryName(ageproData.General.InputFile)}\\{Path.GetFileName(ageproData.Bootstrap.BootstrapFile)}"))
            {
                File.Copy($"{Path.GetDirectoryName(ageproData.General.InputFile)}\\{Path.GetFileName(ageproData.Bootstrap.BootstrapFile)}",
                          bsnFile, true);
            }
            //3. Else, Explictly locate the bootstrap file (via OpenFileDialog).
            else
            {
                OpenFileDialog openBootstrapFileDialog = ControlBootstrap.SetBootstrapOpenFileDialog();

                if (openBootstrapFileDialog.ShowDialog() == DialogResult.OK)
                {
                    File.Copy(openBootstrapFileDialog.FileName, bsnFile, true);
                }
                else
                {
                    Console.WriteLine("Cancel Launch AGEPRO Model");
                    //If user declines (Cancel), do not Launch AGEPRO Calc Engine
                    return;
                }
            }
            //Store original bootstrap filename in case of error
            string originalBSNFile = ageproData.Bootstrap.BootstrapFile;

            try
            {
                //Set bootstrap filename to copied workDir version
                ageproData.Bootstrap.BootstrapFile = bsnFile;

                //Write Interface Inputs to file
                ageproData.WriteInputFile(inpFile);

                //use command line to open AGEPRO40.exe
                LaunchAgeproCalcEngineProgram(inpFile);

                //crude method to search for AGEPRO output file
                string ageproOutfile = Directory.GetFiles(Path.GetDirectoryName(inpFile), "*.out").First();

                LaunchOutputViewerProgram(ageproOutfile, AgeproUserOptions);

                //Open WorkPath directory for the user
                _ = Process.Start(ageproWorkPath);
            }
            catch (Exception ex)
            {
                _ = MessageBox.Show("An error occured when Launching the AGEPRO Model." + Environment.NewLine + ex, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //reset original bootstrap filename
                ageproData.Bootstrap.BootstrapFile = originalBSNFile;
            }
        }
Пример #2
0
        /// <summary>
        /// Initiates the Control's "Startup State" or the "Uninitialized Model" phase.
        /// </summary>
        protected void SetupStartupState()
        {
            //AGEPRO Input Data, If any
            inputData = new AgeproInputFile();

            //Load User Controls
            controlGeneralOptions     = new ControlGeneral();
            controlMiscOptions        = new ControlMiscOptions();
            controlBootstrap          = new ControlBootstrap();
            controlFisherySelectivity = new ControlStochasticAge();
            controlDiscardFraction    = new ControlStochasticAge();
            controlNaturalMortality   = new ControlStochasticAge();
            controlBiological         = new ControlBiological(new string[] { string.Empty });
            controlJan1Weight         = new ControlStochasticWeightAge(new int[] { 0, 1 });
            controlSSBWeight          = new ControlStochasticWeightAge(new int[] { 0, 1, -1 });
            controlMidYearWeight      = new ControlStochasticWeightAge(new int[] { 0, 1, -1, -2 });
            controlCatchWeight        = new ControlStochasticWeightAge(new int[] { 0, 1, -1, -2, -3 });
            controlDiscardWeight      = new ControlStochasticWeightAge(new int[] { 0, 1, -1, -2, -3, -4 });
            controlRecruitment        = new ControlRecruitment();
            controlHarvestScenario    = new ControlHarvestScenario();

            //Set General Options Controls (to handle "New Cases")
            controlGeneralOptions.GeneralInputFile = "";
            controlGeneralOptions.GeneralModelId   = "untitled";
            inputData.General.InputFile            = "";
            inputData.CaseID = controlGeneralOptions.GeneralModelId;

            //initially set Number of Ages
            _ = controlGeneralOptions.GeneralFirstAgeClass; //Spinbox Value

            //Biological Stochastic Options
            controlFisherySelectivity.StochasticParameterLabel = "Fishery Selectivity";
            controlFisherySelectivity.IsMultiFleet             = true;
            controlFisherySelectivity.FleetDependency          = StochasticAgeFleetDependency.dependent;
            controlDiscardFraction.StochasticParameterLabel    = "Discard Fraction";
            controlDiscardFraction.IsMultiFleet              = true;
            controlDiscardFraction.FleetDependency           = StochasticAgeFleetDependency.dependent;
            controlNaturalMortality.StochasticParameterLabel = "Natural Mortality";
            controlNaturalMortality.IsMultiFleet             = false;
            controlNaturalMortality.FleetDependency          = StochasticAgeFleetDependency.independent;

            //Weight Age Options
            controlJan1Weight.IsMultiFleet       = false;
            controlJan1Weight.FleetDependency    = StochasticAgeFleetDependency.independent;
            controlSSBWeight.IsMultiFleet        = false;
            controlSSBWeight.FleetDependency     = StochasticAgeFleetDependency.independent;
            controlMidYearWeight.IsMultiFleet    = false;
            controlMidYearWeight.FleetDependency = StochasticAgeFleetDependency.independent;
            controlCatchWeight.IsMultiFleet      = true;
            controlCatchWeight.FleetDependency   = StochasticAgeFleetDependency.dependent;
            controlDiscardWeight.IsMultiFleet    = true;
            controlDiscardWeight.FleetDependency = StochasticAgeFleetDependency.dependent;

            //Set WeightAgeType
            controlJan1Weight.WeightAgeType    = StochasticWeightOfAge.Jan1Weight;
            controlSSBWeight.WeightAgeType     = StochasticWeightOfAge.SSBWeight;
            controlMidYearWeight.WeightAgeType = StochasticWeightOfAge.MidYearWeight;
            controlCatchWeight.WeightAgeType   = StochasticWeightOfAge.CatchWeight;
            controlDiscardWeight.WeightAgeType = StochasticWeightOfAge.DiscardWeight;

            //Weights Option
            controlJan1Weight.ShowJan1WeightsOption    = false;
            controlJan1Weight.ShowSSBWeightsOption     = false;
            controlJan1Weight.showMidYearWeightsOption = false;
            controlJan1Weight.ShowCatchWeightsOption   = false;
            //SSB
            controlSSBWeight.ShowSSBWeightsOption     = false;
            controlSSBWeight.showMidYearWeightsOption = false;
            controlSSBWeight.ShowCatchWeightsOption   = false;
            //Mid-Year
            controlMidYearWeight.showMidYearWeightsOption = false;
            controlMidYearWeight.ShowCatchWeightsOption   = false;
            //Catch-weight
            controlCatchWeight.ShowCatchWeightsOption = false;
        }