/// <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; } }
/// <summary> /// Saves data that is currently on the GUI to file. /// </summary> protected void SaveAgeproInputDataFileDialog() { SaveFileDialog saveAgeproInputFile = new SaveFileDialog { InitialDirectory = "~", Filter = "AGEPRO input files (*.inp)|*.inp|All Files (*.*)|*.*", FilterIndex = 1, RestoreDirectory = true }; if (saveAgeproInputFile.ShowDialog() == DialogResult.OK) { try { //Validate if (GetValidateControlInputs() == false) { throw new InvalidAgeproParameterException("Unable to save AGEPRO Input Data due to invalid input."); } //Case Id inputData.CaseID = controlGeneralOptions.GeneralModelId; //Weights of Age controlJan1Weight.BindStochasticAgeData(inputData.Jan1StockWeight); controlSSBWeight.BindStochasticAgeData(inputData.SSBWeight); controlMidYearWeight.BindStochasticAgeData(inputData.MeanWeight); controlCatchWeight.BindStochasticAgeData(inputData.CatchWeight); //Fishery Selectivity controlFisherySelectivity.BindStochasticAgeData(inputData.Fishery); //Natural Mortality controlBiological.maturityAge.BindStochasticAgeData(inputData.BiologicalMaturity); controlNaturalMortality.BindStochasticAgeData(inputData.NaturalMortality); inputData.BiologicalTSpawn.TimeVarying = controlBiological.TSpawnPanel.TSpawnTableTimeVarying; if (inputData.General.HasDiscards) { controlDiscardWeight.BindStochasticAgeData(inputData.DiscardWeight); controlDiscardFraction.BindStochasticAgeData(inputData.DiscardFraction); } //Harvest Scenario: Rebuilder/PStar if (controlHarvestScenario.CalcType == HarvestScenarioAnalysis.PStar) { inputData.HarvestScenario.AnalysisType = controlHarvestScenario.CalcType; inputData.PStar.AnalysisType = controlHarvestScenario.CalcType; inputData.PStar.PStarLevels = controlHarvestScenario.PStar.PStarLevels; inputData.PStar.PStarF = controlHarvestScenario.PStar.PStarF; inputData.PStar.TargetYear = controlHarvestScenario.PStar.TargetYear; inputData.PStar.PStarTable = controlHarvestScenario.PStar.PStarTable; } else if (controlHarvestScenario.CalcType == HarvestScenarioAnalysis.Rebuilder) { inputData.HarvestScenario.AnalysisType = controlHarvestScenario.CalcType; inputData.Rebuild.AnalysisType = controlHarvestScenario.CalcType; inputData.Rebuild.TargetYear = controlHarvestScenario.Rebuilder.TargetYear; inputData.Rebuild.TargetPercent = controlHarvestScenario.Rebuilder.TargetPercent; inputData.Rebuild.TargetType = controlHarvestScenario.Rebuilder.TargetType; } //Misc options inputData.Options.EnableSummaryReport = controlMiscOptions.MiscOptionsEnableSummaryReport; inputData.Options.EnableExportR = controlMiscOptions.MiscOptionsEnableExportR; inputData.Options.EnableAuxStochasticFiles = controlMiscOptions.MiscOptionsEnableAuxStochasticFiles; inputData.Options.EnablePercentileReport = controlMiscOptions.MiscOptionsEnablePercentileReport; inputData.Options.EnableRefpoint = controlMiscOptions.MiscOptionsEnableRefpointsReport; inputData.Options.EnableScaleFactors = controlMiscOptions.MiscOptionsEnableScaleFactors; inputData.Options.EnableBounds = controlMiscOptions.MiscOptionsBounds; inputData.Options.EnableRetroAdjustmentFactors = controlMiscOptions.MiscOptionsEnableRetroAdjustmentFactors; //Misc Options: Refpoint inputData.Refpoint.RefSpawnBio = double.Parse(controlMiscOptions.MiscOptionsRefSpawnBiomass); inputData.Refpoint.RefJan1Bio = double.Parse(controlMiscOptions.MiscOptionsRefJan1Biomass); inputData.Refpoint.RefMeanBio = double.Parse(controlMiscOptions.MiscOptionsRefMeanBiomass); inputData.Refpoint.RefFMort = double.Parse(controlMiscOptions.MiscOptionsRefFishingMortality); //Misc Options: Report Percentile inputData.ReportPercentile.Percentile = controlMiscOptions.MiscOptionsReportPercentile; //Misc Options: Scale Factors inputData.Scale.ScaleBio = double.Parse(controlMiscOptions.MiscOptionsScaleFactorBiomass); inputData.Scale.ScaleRec = double.Parse(controlMiscOptions.MiscOptionsScaleFactorRecruits); inputData.Scale.ScaleStockNum = double.Parse(controlMiscOptions.MiscOptionsScaleFactorStockNumbers); //Misc Options: Retro Adjustment Factors inputData.RetroAdjustments.RetroAdjust = controlMiscOptions.MiscOptionsRetroAdjustmentFactorTable; inputData.WriteInputFile(saveAgeproInputFile.FileName); //Set filename to generalOptions Input File textbox controlGeneralOptions.GeneralInputFile = saveAgeproInputFile.FileName; _ = MessageBox.Show($"AGEPRO Input Data was saved at{Environment.NewLine}{saveAgeproInputFile.FileName}", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { _ = MessageBox.Show($"AGEPRO input file was not saved.{Environment.NewLine}{ex.Message}", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }