/// <summary> /// Select certain result. /// </summary> private void SelectResult() { if (Model.ModelNames.Count <= 0) { dialogService.ShowNotification("There are no results to select.", "Error"); viewModelLocator.SettingsViewModel.WriteLogText("There are no results to select.", "Error"); return; } try { var modelDirPath = Model.ModelNames[Model.SelectedModelIndex]; var modelDirInfo = new DirectoryInfo(Model.ModelNames[Model.SelectedModelIndex]); var selectedProjectFolder = Path.Combine(settingsService.RootPath, "MCSim", Model.SelectedTract, "SelectedResult"); if (modelDirPath == selectedProjectFolder) { dialogService.ShowNotification("SelectedResult folder cannot be selected. ", "Error"); return; } if (!Directory.Exists(selectedProjectFolder)) { Directory.CreateDirectory(selectedProjectFolder); } DirectoryInfo di = new DirectoryInfo(selectedProjectFolder); foreach (FileInfo file in di.GetFiles()) { file.Delete(); } foreach (DirectoryInfo dir in di.GetDirectories()) { dir.Delete(true); } foreach (FileInfo file2 in modelDirInfo.GetFiles()) { var destPath = Path.Combine(selectedProjectFolder, file2.Name); var sourcePath = Path.Combine(modelDirPath, file2.Name); File.Copy(sourcePath, destPath, true); } MonteCarloSimulationInputParams inputParams = new MonteCarloSimulationInputParams(); string param_json = Path.Combine(selectedProjectFolder, "monte_carlo_simulation_input_params.json"); if (File.Exists(param_json)) { inputParams.Load(param_json); Model.GradePlot = inputParams.GradePlot; Model.TonnagePlot = inputParams.TonnagePlot; Model.NDepositsPmf = inputParams.NDepositsPmf; Model.ExtensionFolder = inputParams.ExtensionFolder; Model.SelectedTract = inputParams.TractID; File.Copy(param_json, Path.Combine(settingsService.RootPath, "MCSim", "monte_carlo_simulation_input_params.json"), true); } dialogService.ShowNotification("Monte Carlo Simulation result selected successfully.", "Success"); viewModelLocator.SettingsViewModel.WriteLogText("Monte Carlo Simulation result selected successfully.", "Success"); } catch (Exception ex) { logger.Trace(ex, "Error in result selection"); dialogService.ShowNotification("Failed to select result.", "Error"); viewModelLocator.SettingsViewModel.WriteLogText("Failed to select result in Monte Carlo Simulation tool.", "Error"); } var metroWindow = (Application.Current.MainWindow as MetroWindow); var dialog = metroWindow.GetCurrentDialogAsync<BaseMetroDialog>(); metroWindow.HideMetroDialogAsync(dialog.Result); LoadResults(); }
/// <summary> /// Initializes an instance of MonteCarloSimulationViewModel Class. /// </summary> /// <param name="logger">Logging for the MapWizard.</param> /// <param name="dialogService">Service for using dialogs and notifications.</param> /// <param name="settingsService">Service for using and editing settings.</param> public MonteCarloSimulationViewModel(ILogger logger, IDialogService dialogService, ISettingsService settingsService) { this.logger = logger; this.dialogService = dialogService; this.settingsService = settingsService; viewModelLocator = new ViewModelLocator(); result = new MonteCarloSimulationResultModel(); RunToolCommand = new RelayCommand(RunTool, CanRunTool); FindTractsCmd = new RelayCommand(FindTracts, CanRunTool); TractChangedCommand = new RelayCommand(TractChanged, CanRunTool); ShowModelDialog = new RelayCommand(OpenModelDialog, CanRunTool); SelectModelCommand = new RelayCommand(SelectResult, CanRunTool); SelectGradeObjectCommand = new RelayCommand(SelectGradeObject, CanRunTool); SelectTonnageObjectCommand = new RelayCommand(SelectTonnageObject, CanRunTool); SelectGradeTonnageObjectCommand = new RelayCommand(SelectGradeTonnageObject, CanRunTool); SelectNDepositsPmfObjectCommand = new RelayCommand(SelectNDepositsPmfObject, CanRunTool); OpenResultExcelObjectCommand = new RelayCommand(OpenResultExcelObject, CanRunTool); OpenTotalTonnagePlotCommand = new RelayCommand(OpenTotalTonnagePlot, CanRunTool); OpenMarginalPlotCommand = new RelayCommand(OpenMarginalPlot, CanRunTool); MonteCarloSimulationInputParams inputParams = new MonteCarloSimulationInputParams(); string projectFolder = Path.Combine(settingsService.RootPath, "MCSim"); if (!Directory.Exists(projectFolder)) { Directory.CreateDirectory(projectFolder); } string param_json = Path.Combine(projectFolder, "monte_carlo_simulation_input_params.json"); if (File.Exists(param_json)) { try { inputParams.Load(param_json); Model = new MonteCarloSimulationModel { GradePlot = inputParams.GradePlot, TonnagePlot = inputParams.TonnagePlot, NDepositsPmf = inputParams.NDepositsPmf, SelectedTract = inputParams.TractID, LastRunTract = "Tract: " + inputParams.LastRunTract }; FindTractIDs(); // Gets the tractID names from PermissiveTractTool's Delineation folder. //FindMCSimTractIDs(); // Gets the tractID names from MCSim folder } catch (Exception ex) { Model = new MonteCarloSimulationModel(); logger.Error(ex, "Failed to read json file"); dialogService.ShowNotification("Couldn't load Monte Carlo Simulation tool's inputs correctly. Inputs were initialized to default values.", "Error"); viewModelLocator.SettingsViewModel.WriteLogText("Couldn't load Monte Carlo Simulation tool's inputs correctly. Inputs were initialized to default values.", "Error"); } } else { Model = new MonteCarloSimulationModel(); FindTractIDs(); // Gets the tractID names from PermissiveTractTool's Delineation folder. //FindMCSimTractIDs(); // Gets the tractID names from MCSim folder } if (Model.SelectedTract != null) { LoadResults(); } if (Model.SelectedTract != null) { projectFolder = Path.Combine(projectFolder, Model.SelectedTract); if (Directory.Exists(projectFolder)) { Model.ModelNames.Clear(); FindModelnames(projectFolder); // Find saved results. } } var lastRunFile = Path.Combine(settingsService.RootPath, "MCSim", "monte_carlo_simulation_last_run.lastrun"); if (File.Exists(lastRunFile)) { Model.LastRunDate = "Last Run: " + (new FileInfo(lastRunFile)).LastWriteTime.ToString(); } }
/// <summary> /// Run tool with user input. /// </summary> private async void RunTool() { if(!Model.GradePlot.Contains("Please select") && Model.TonnagePlot.Contains("Please select")) { dialogService.ShowNotification("Cannot run when only Grade object is defined!", "Error"); return; } if (Model.TonnagePlot.Contains("Please select") && Model.GradeTonnagePlot.Contains("Please select")) { dialogService.ShowNotification("Please select Grade and Tonnage objects OR Tonnage object OR GradeTonnage object!", "Error"); return; } Result.TotalTonPlot = null; Result.MarginalPlot = null; logger.Info("-->{0}", this.GetType().Name); if (Model.UseModelName == false) { Model.ExtensionFolder = ""; } var rootPath = Path.Combine(settingsService.RootPath, "MCSim"); try { DirectoryInfo rootDirectory = new DirectoryInfo(rootPath); //Clear the folder from all the old files. foreach (FileInfo file in rootDirectory.GetFiles()) { if (file.Name != "monte_carlo_simulation_last_run.lastrun") { file.Delete(); } } } catch (Exception ex) { logger.Error(ex, "Failed to clear output folder"); dialogService.ShowNotification("Failed to clear output folder.", "Error"); viewModelLocator.SettingsViewModel.WriteLogText("Failed to clear output folder in Monte Carlo Simulation tool.", "Error"); Model.RunStatus = 0; return; } // 1. Collect input parameters MonteCarloSimulationInputParams input = new MonteCarloSimulationInputParams { GradePlot = Model.GradePlot, TonnagePlot = Model.TonnagePlot, GradeTonnagePlot = Model.GradeTonnagePlot, NDepositsPmf = Model.NDepositsPmf, ExtensionFolder = Model.ExtensionFolder, TractID = model.SelectedTract, LastRunTract = model.SelectedTract }; // 2. Execute tool MonteCarloSimulationResult ddResult = default(MonteCarloSimulationResult); Model.IsBusy = true; try { await Task.Run(() => { MonteCarloSimulationTool tool = new MonteCarloSimulationTool(); ddResult = tool.Execute(input) as MonteCarloSimulationResult; Result.SummaryTotalTonnage = ddResult.SummaryTotalTonnage; Result.TotalTonPlot = ddResult.TotalTonPlot; Result.MarginalPlot = ddResult.MarginalPlot; Result.SimulatedDepositsCSV = ddResult.SimulatedDepositsCSV; Result.TotalTonPlotBitMap = BitmapFromUri(Result.TotalTonPlot); Result.MarginalPlotBitMap = BitmapFromUri(Result.MarginalPlot); }); var modelFolder = Path.Combine(rootPath, Model.SelectedTract, Model.ExtensionFolder); Model.ModelNames.Clear(); FindModelnames(Path.Combine(rootPath, Model.SelectedTract)); // Find saved results. string lastRunFile = Path.Combine(Path.Combine(settingsService.RootPath, "MCSim", "monte_carlo_simulation_last_run.lastrun")); File.Create(lastRunFile).Close(); dialogService.ShowNotification("Monte Carlo simulation tool completed successfully.", "Success"); viewModelLocator.SettingsViewModel.WriteLogText("Monte Carlo simulation tool completed successfully.", "Success"); Model.LastRunDate = "Last Run: " + DateTime.Now.ToString("g"); Model.LastRunTract = "Tract: " + model.SelectedTract; Model.RunStatus = 1; } catch (Exception ex) { logger.Error(ex, "Failed to execute REngine() script"); dialogService.ShowNotification("Run failed. Check output for details. Check output for details\r\n- Are all input parameters correct?\r\n- Are all input files valid? \r\n- Are all input and output files closed?", "Error"); viewModelLocator.SettingsViewModel.WriteLogText("Monte Carlo Simulation tool run failed. Check output for details\r\n- Are all input parameters correct?\r\n- Are all input files valid? \r\n- Are all input and output files closed?", "Error"); Model.RunStatus = 0; } finally { Model.IsBusy = false; } logger.Info("<--{0} completed", this.GetType().Name); }