/******************************************************************************************************** * Open/Save ********************************************************************************************************/ /// <summary> /// Calls the OpenFileDialog Window to retrive an existing AGEPRO Input file. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OpenExistingAGEPROInputDataFileToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openAgeproInputFile = new OpenFileDialog { InitialDirectory = "~", Filter = "AGEPRO input files (*.inp)|*.inp|All Files (*.*)|*.*", FilterIndex = 1, RestoreDirectory = true }; if (openAgeproInputFile.ShowDialog() == DialogResult.OK) { try { //CoreLib.geproInputFile's ReadInputFile inputData = new AgeproInputFile(); inputData.ReadInputFile(openAgeproInputFile.FileName); LoadAgeproModelFromInputFile(inputData); controlGeneralOptions.GeneralInputFile = openAgeproInputFile.FileName; //Activate Naivagation Panel if in first-run/startup state. EnableNavigationPanel(); } catch (Exception ex) { _ = MessageBox.Show($"Error loading AGEPRO input file:{Environment.NewLine}{ex.Message}", "AGEPRO Input File", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
/// <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> /// 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; }
/// <summary> /// Load AGEPRO InputFile data into AGEPRO Parameter Controls /// </summary> /// <param name="inpFile">AGEPRO CoreLib InputFile</param> protected void LoadAgeproModelFromInputFile(AgeproInputFile inpFile) { //General Options controlGeneralOptions.GeneralModelId = inpFile.CaseID; controlGeneralOptions.GeneralFirstYearProjection = inpFile.General.ProjYearStart.ToString(); controlGeneralOptions.GeneralLastYearProjection = inpFile.General.ProjYearEnd.ToString(); controlGeneralOptions.GeneralFirstAgeClass = inpFile.General.AgeBegin; controlGeneralOptions.GeneralLastAgeClass = inpFile.General.AgeEnd; controlGeneralOptions.GeneralNumberFleets = inpFile.General.NumFleets.ToString(); controlGeneralOptions.GeneralNumberRecruitModels = inpFile.General.NumRecModels.ToString(); controlGeneralOptions.GeneralNumberPopulationSimuations = inpFile.General.NumPopSims.ToString(); controlGeneralOptions.GeneralRandomSeed = inpFile.General.Seed.ToString(); controlGeneralOptions.GeneralDiscardsPresent = inpFile.General.HasDiscards; //JAN-1 controlJan1Weight.LoadStochasticWeightAgeInputData(inpFile.Jan1StockWeight, inpFile.General); //SSB controlSSBWeight.LoadStochasticWeightAgeInputData(inpFile.SSBWeight, inpFile.General); //Mid-Year (Mean) controlMidYearWeight.LoadStochasticWeightAgeInputData(inpFile.MeanWeight, inpFile.General); //Catch Weight controlCatchWeight.LoadStochasticWeightAgeInputData(inpFile.CatchWeight, inpFile.General); //Discard Weight controlDiscardWeight.LoadStochasticWeightAgeInputData(inpFile.DiscardWeight, inpFile.General); //Recruitment controlRecruitment.SetupControlRecruitment(inpFile.General.NumRecModels, inpFile.Recruitment); //Fishery Selectivity controlFisherySelectivity.LoadStochasticAgeInputData(inpFile.Fishery, inpFile.General); //Discard Fraction controlDiscardFraction.LoadStochasticAgeInputData(inpFile.DiscardFraction, inpFile.General); //Natural Mortality controlNaturalMortality.LoadStochasticAgeInputData(inpFile.NaturalMortality, inpFile.General); //Fraction Mortality Prior To Spawning (Biological) controlBiological = new ControlBiological(controlGeneralOptions.SeqYears()); controlBiological.TSpawnPanel.TSpawnTableTimeVarying = inpFile.BiologicalTSpawn.TimeVarying; controlBiological.TSpawnPanel.TSpawnTable = inpFile.BiologicalTSpawn.TSpawn; //Maturity (Biological) controlBiological.maturityAge.LoadStochasticAgeInputData(inpFile.BiologicalMaturity, inpFile.General); //Harvest Scenario if (inpFile.HarvestScenario.AnalysisType == HarvestScenarioAnalysis.Rebuilder) { controlHarvestScenario.Rebuilder = inpFile.Rebuild; } else if (inpFile.HarvestScenario.AnalysisType == HarvestScenarioAnalysis.PStar) { controlHarvestScenario.PStar = inpFile.PStar; } controlHarvestScenario.SeqYears = inpFile.Recruitment.ObservationYears.Select(x => x.ToString()).ToArray(); controlHarvestScenario.SetHarvestScenarioInputDataTable(inpFile.HarvestScenario.HarvestScenarioTable); controlHarvestScenario.SetHarvestScenarioCalcControls(inpFile); //Bootstrapping controlBootstrap.BootstrapFilename = inpFile.Bootstrap.BootstrapFile; controlBootstrap.BootstrapIterations = inpFile.Bootstrap.NumBootstraps.ToString(); controlBootstrap.BootstrapScaleFactors = inpFile.Bootstrap.PopScaleFactor.ToString(); //Misc Options controlMiscOptions.MiscOptionsEnableSummaryReport = inpFile.Options.EnableSummaryReport; controlMiscOptions.MiscOptionsEnableAuxStochasticFiles = inpFile.Options.EnableAuxStochasticFiles; controlMiscOptions.MiscOptionsEnableExportR = inpFile.Options.EnableExportR; controlMiscOptions.MiscOptionsEnablePercentileReport = inpFile.Options.EnablePercentileReport; controlMiscOptions.MiscOptionsReportPercentile = Convert.ToDouble(inpFile.ReportPercentile.Percentile); controlMiscOptions.MiscOptionsEnableRefpointsReport = inpFile.Options.EnableRefpoint; controlMiscOptions.MiscOptionsRefSpawnBiomass = inpFile.Refpoint.RefSpawnBio.ToString(); controlMiscOptions.MiscOptionsRefJan1Biomass = inpFile.Refpoint.RefJan1Bio.ToString(); controlMiscOptions.MiscOptionsRefMeanBiomass = inpFile.Refpoint.RefMeanBio.ToString(); controlMiscOptions.MiscOptionsRefFishingMortality = inpFile.Refpoint.RefFMort.ToString(); controlMiscOptions.MiscOptionsEnableScaleFactors = inpFile.Options.EnableScaleFactors; controlMiscOptions.MiscOptionsScaleFactorBiomass = inpFile.Scale.ScaleBio.ToString(); controlMiscOptions.MiscOptionsScaleFactorRecruits = inpFile.Scale.ScaleRec.ToString(); controlMiscOptions.MiscOptionsScaleFactorStockNumbers = inpFile.Scale.ScaleStockNum.ToString(); controlMiscOptions.MiscOptionsBounds = inpFile.Options.EnableBounds; controlMiscOptions.MiscOptionsBoundsMaxWeight = inpFile.Bounds.MaxWeight.ToString(); controlMiscOptions.MiscOptionsBoundsNaturalMortality = inpFile.Bounds.MaxNatMort.ToString(); controlMiscOptions.MiscOptionsEnableRetroAdjustmentFactors = inpFile.Options.EnableRetroAdjustmentFactors; controlMiscOptions.miscOptionsNAges = inpFile.General.NumAges(); controlMiscOptions.miscOptionsFirstAge = inpFile.General.AgeBegin; controlMiscOptions.LoadRetroAdjustmentsFactorTable(inpFile); if (controlMiscOptions.MiscOptionsEnableRetroAdjustmentFactors) { controlMiscOptions.SetRetroAdjustmentFactorRowHeaders(); } Console.WriteLine("Loaded AGEPRO Parameters .."); }