/// <summary> /// Run Tract Aggregation tool with user input. Calls TractAggregationTool class for functionality. /// </summary> private async void RunTool() { logger.Info("-->{0}", this.GetType().Name); try { string rootFolder = settingsService.RootPath; if (Model.UseInputParams == true) { Model.CorrelationMatrix = CreateCorrelationMatrix(); //ilimota jos ei oo selectedresulttia Model.ProbDistFile = CombineTractPMFs(); //nyt tosiaan toimii vaan selectedresulteille. is this OK? } else if (Model.CreateInputFile == true) { CombineDistFiles(); } TractAggregationInputParams inputParams = new TractAggregationInputParams { CorrelationMatrix = Model.CorrelationMatrix, ProbDistFile = Model.ProbDistFile, WorkingDir = Model.WorkingDir, TestName = Model.TestName, CreateInputFile = Model.CreateInputFile.ToString(), TractCombinationName = Model.TractCombinationName }; logger.Trace( "GradeTonnageInputParams:\n" + "\tCorrelationMatrix: '{0}'\n" + "\tProbDistFile: '{1}'\n" + "\tWorkingDir: '{2}'\n" + "\tTestName: '{3}'\n", inputParams.CorrelationMatrix, inputParams.ProbDistFile, inputParams.WorkingDir, inputParams.TestName, inputParams.TractCombinationName ); Model.IsBusy = true; var lastResult = Path.Combine(settingsService.RootPath, "TractAggregation", "AggResults", "AGG" + Model.TractCombinationName, "AggEstSummary.csv"); if (File.Exists(lastResult)) { File.Delete(lastResult); } await Task.Run(() => { TractAggregationTool tool = new TractAggregationTool(); TractAggregationResult tractAggregationResult = default(TractAggregationResult); logger.Info("calling TractAggregationTool.Execute(inputParams)"); tractAggregationResult = tool.Execute(inputParams) as TractAggregationResult; Result.TractAggregationSummary = tractAggregationResult.TractAggregationSummary; }); string lastRunFile = Path.Combine(Path.Combine(settingsService.RootPath, "TractAggregation", "tract_aggregation_last_run.lastrun")); File.Create(lastRunFile).Close(); dialogService.ShowNotification("Tract Aggregation tool completed successfully.", "Success"); viewModelLocator.SettingsViewModel.WriteLogText("Tract Aggregation tool completed successfully.", "Success"); Model.RunStatus = 1; Model.LastRunDate = "Last Run: " + DateTime.Now.ToString("g"); //tänne se tractsaggregated.csv:n kirjotus, koska se on kaikille run typeille. Directory.CreateDirectory(Path.Combine(settingsService.RootPath, "TractDelineation", "Tracts", "AGG" + Model.TractCombinationName)); var filePath = Path.Combine(Path.Combine(settingsService.RootPath, "TractDelineation", "Tracts", "AGG" + Model.TractCombinationName, "TractsAggregated.csv")); using (StreamReader sr = new StreamReader(Path.Combine(Path.Combine(settingsService.RootPath, "TractAggregation", "AggResults", "AGG" + Model.TractCombinationName, "TractCorrelations.csv")))) { var headers = sr.ReadLine();//elik tästä pitäs tulla full pathit eikä pelkät otsikot. if (headers[0] == ',') { headers = headers.Substring(1); } var headerArray = headers.Split(','); for (int i = 0; i < headerArray.Length; i++) { headerArray[i] = Path.Combine(settingsService.RootPath, "TractDelineation", "Tracts", headerArray[i]); } var combinedHeaderArray = string.Join(",", headerArray); File.WriteAllText(filePath, combinedHeaderArray); } } catch (Exception ex) { logger.Error(ex, "Failed to run R-script."); dialogService.ShowNotification("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"); viewModelLocator.SettingsViewModel.WriteLogText("Tract Aggregation 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; } }
/// <summary> /// Initializes a new instance of the TractAggregationViewModel 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 TractAggregationViewModel(ILogger logger, IDialogService dialogService, ISettingsService settingsService) { this.logger = logger; this.dialogService = dialogService; this.settingsService = settingsService; viewModelLocator = new ViewModelLocator(); model = new TractAggregationModel(); result = new TractAggregationResultModel(); SelectCorrelationMatrixCommand = new RelayCommand(SelectCorrelationMatrix, CanRunTool); SelectProbDistFileCommand = new RelayCommand(SelectProbDistFile, CanRunTool); FindTractsCommand = new RelayCommand(FindTractIDs, CanRunTool); RunToolCommand = new RelayCommand(RunTool, CanRunTool); CombineDistFilesCommand = new RelayCommand(CombineDistFiles, CanRunTool); AddFilesToCombineListCommand = new RelayCommand(AddFilesToCombineList, CanRunTool); CombineTractsCommand = new RelayCommand(CombineTracts, CanRunTool); TractAggregationInputParams inputParams = new TractAggregationInputParams(); string projectFolder = Path.Combine(settingsService.RootPath, "TractAggregation"); if (!Directory.Exists(projectFolder)) { Directory.CreateDirectory(projectFolder); } string param_json = Path.Combine(projectFolder, "tract_aggregation_input_params.json"); if (File.Exists(param_json)) { try { inputParams.Load(param_json); Model = new TractAggregationModel { CorrelationMatrix = inputParams.CorrelationMatrix, ProbDistFile = inputParams.ProbDistFile, WorkingDir = inputParams.WorkingDir, TestName = inputParams.TestName, CreateInputFile = bool.Parse(inputParams.CreateInputFile), TractCombinationName = inputParams.TractCombinationName }; } catch (Exception ex) { Model = new TractAggregationModel(); logger.Error(ex, "Failed to read json file"); dialogService.ShowNotification("Couldn't load Tract Aggregation tool's inputs correctly. Inputs were initialized to default values.", "Error"); viewModelLocator.SettingsViewModel.WriteLogText("Couldn't load Tract Aggregation tool's inputs correctly. Inputs were initialized to default values.", "Error"); } } else { Model = new TractAggregationModel(); } FindTractIDs(); if (Directory.Exists(Path.Combine(settingsService.RootPath, "TractAggregation", "AggResults"))) { LoadResults(); } var lastRunFile = Path.Combine(settingsService.RootPath, "TractAggregation", "tract_aggregation_last_run.lastrun"); if (File.Exists(lastRunFile)) { Model.LastRunDate = "Last Run: " + (new FileInfo(lastRunFile)).LastWriteTime.ToString(); } }