private void bDeleteObservationData_Click(object sender, EventArgs e) { if (_col != null && _id > 0 && tableView1.DataSource != null) { try { if (System.Windows.Forms.MessageBox.Show( string.Format("Do you really want to delete the observed data for {0} {1} {2}?", _unitType, _id, _col), SWAT_SQLite.NAME, MessageBoxButtons.YesNo) == DialogResult.No) { return; } if (_prj.Observation(_interval).delete(_unitType, _id, _col)) { updateTableAndChart(); subbasinMap1.updateObservedStatus(_unitType, _id); yearCtrl1.ObservedData = null; SWAT_SQLite.showInformationWindow( string.Format("Data for {0} {1} is successfully deleted.", _unitType, _id)); } } catch (System.Exception ee) { SWAT_SQLite.showInformationWindow(ee.Message); } } }
private void addCompareResults() { cmbCompareResults.Items.Clear(); _comparableResult.Clear(); if (_scenarioResult == null) { return; } _comparableResult = _scenarioResult.ComparableScenarioResults; foreach (ArcSWAT.ScenarioResult r in _comparableResult) { cmbCompareResults.Items.Add(string.Format("{0}.{1}", r.Scenario.Name, r.ModelType)); } if (cmbCompareResults.Items.Count > 0) { cmbCompareResults.SelectedIndex = 0; } else { SWAT_SQLite.showInformationWindow("No comparable scenarios!"); } }
private void copyModel(string path) { string swat_cup = path; string backup = System.IO.Path.Combine(swat_cup, "backup"); if (!System.IO.Directory.Exists(backup)) { SWAT_SQLite.showInformationWindow(backup + " doesn't exist!"); return; } //start to copy try { updateMessage("Copy all model files from " + _scenario.ModelFolder + " to " + backup); updateMessage(DateTime.Now.ToString()); DirectoryInfo modelInfo = new DirectoryInfo(_scenario.ModelFolder); var modelFiles = modelInfo.EnumerateFiles().Where( f => !(f.Extension.ToLower().Equals(".db3")) && !(f.Name.ToLower().Contains("output"))); //remove db3 files and output files foreach (FileInfo f in modelFiles) { backgroundWorker1.ReportProgress(0, f.Name); File.Copy(f.FullName, f.FullName.Replace(_scenario.ModelFolder, backup), true); //copy and overwrite } updateMessage("Copying finished! " + DateTime.Now.ToString()); SWAT_SQLite.showInformationWindow("DONE!"); } catch (Exception ee) { SWAT_SQLite.showInformationWindow("Failed!" + ee.Message); } }
private void updatePerformanceTable() { if (_result == null) { return; } int year = Convert.ToInt32(cmbSplitYear.SelectedItem.ToString()); DataTable dt = _result.getPerformanceTable(year, _statisticType); if (!_warningHasShown && dt.Rows.Count == 0) { SWAT_SQLite.showInformationWindow("No performance data. Please make sure the observed data has been uploaded and the simulation results exists!"); _warningHasShown = true; } this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = dt; }
private void openModelFile(string fileName) { string filePath = _scenario.ModelFolder + @"\" + fileName; if (!System.IO.File.Exists(filePath)) { SWAT_SQLite.showInformationWindow(filePath + " doesn't exist!"); return; } string notePad = System.Environment.SystemDirectory + @"\notepad.exe"; if (System.IO.File.Exists(notePad)) { System.Diagnostics.Process.Start(notePad, filePath); } }
private void bLoadObservationData_Click(object sender, EventArgs e) { if (_id <= 0 || _col == null) { return; } if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (!System.IO.File.Exists(openFileDialog1.FileName)) { return; } try { if (_prj.Observation(_interval).loadCSV(openFileDialog1.FileName, _unitType, _id, _col)) { _observedData = _prj.Observation(_interval).getObservedData(_unitType, _id, _col); if (_observedData != null) { yearCtrl1.ObservedData = _observedData.getObservedData(-1); //update year control } updateTableAndChart(); subbasinMap1.updateObservedStatus(_unitType, _id); SWAT_SQLite.showInformationWindow( string.Format("Data is successfully loaded to {0} {1}.", _unitType, _id)); } } catch (System.Exception ee) { SWAT_SQLite.showInformationWindow(ee.Message); } } }
private void updateTableAndChart() { _statistics = "No Statistics Data Available"; if (onDataStatisticsChanged != null) { onDataStatisticsChanged(this, new EventArgs()); } if (_resultType == null || _col == null) { return; } if (!this._scenario.Watershed.Results.ContainsKey(_resultType)) { return; } ArcSWAT.SWATUnitResult result = this._scenario.Watershed.Results[_resultType]; if (!result.Columns.Contains(_col)) { return; } int year = -1; if ((result.Interval == ArcSWAT.SWATResultIntervalType.DAILY || result.Interval == ArcSWAT.SWATResultIntervalType.MONTHLY) && yearCtrl1.DisplayByYear) { year = yearCtrl1.Year; } if (_compareResult == null) //don't compare { ArcSWAT.SWATUnitColumnYearResult oneResult = result.getResult(_col, year); this.tableView1.Result = oneResult; this.outputDisplayChart1.Result = oneResult; _statistics = oneResult.Statistics.ToString(); if (onDataStatisticsChanged != null) { onDataStatisticsChanged(this, new EventArgs()); } } else //compare { try { ArcSWAT.SWATUnitColumnYearCompareResult compare = result.getResult(_col, year).Compare(_compareResult); this.tableView1.CompareResult = compare; this.outputDisplayChart1.CompareResult = compare; _statistics = compare.Statistics.ToString(); if (onDataStatisticsChanged != null) { onDataStatisticsChanged(this, new EventArgs()); } } catch (System.Exception e) { SWAT_SQLite.showInformationWindow(e.ToString()); } } }
private void RunSWAT(ArcSWAT.SWATModelType modelType, ArcSWAT.SWATResultIntervalType interval) { if (_scenario == null) { return; } if (modelType == ArcSWAT.SWATModelType.UNKNOWN) { return; } if (interval == ArcSWAT.SWATResultIntervalType.UNKNOWN) { return; } if (_scenario.getModelResult(modelType, interval).Status == ArcSWAT.ScenarioResultStatus.NORMAL) { if (MessageBox.Show("There is a pre-generated model result. Do you want to overwrite?", SWAT_SQLite.NAME, MessageBoxButtons.YesNoCancel) != DialogResult.Yes) { return; } } //find the corresponding executables string swatexe = SWAT_SQLite.InstallationFolder + @"swat_exes\" + ArcSWAT.ScenarioResultStructure.getSWATExecutableName(modelType); if (!System.IO.File.Exists(swatexe)) { SWAT_SQLite.showInformationWindow("Can't find " + swatexe); return; } //change output interval _scenario.modifyOutputInterval(interval); //start to run the model Process myProcess = new Process(); try { myProcess.EnableRaisingEvents = true; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.FileName = swatexe; myProcess.StartInfo.CreateNoWindow = true; myProcess.StartInfo.RedirectStandardError = true; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.WorkingDirectory = _scenario.ModelFolder; myProcess.OutputDataReceived += (sender, agrs) => { if (agrs.Data != null) { updateMessage(agrs.Data); } }; myProcess.ErrorDataReceived += (sender, agrs) => { if (agrs.Data != null) { updateMessage(agrs.Data); } }; myProcess.Exited += (send, agrs) => { //update the results if (onSimulationFinished != null) { onSimulationFinished(modelType, interval); } //update the date time of the result //must be called after onSimulationFinished as the result status is updated in onSimulationFinished updateSimulationTime(); }; updateMessage("Runing " + ModelType.ToString() + " in " + _scenario.ModelFolder); myProcess.Start(); myProcess.BeginOutputReadLine(); //myProcess.WaitForExit(); } catch (Exception e) { Debug.WriteLine(e.Message); } }
private void openProject(string prjPath) { if (_prj != null && prjPath.Equals(_prj.Folder)) { return; } try { _prj = new ArcSWAT.Project(prjPath); } catch (Exception e) { SWAT_SQLite.showInformationWindow(e.Message); return; } if (!_prj.IsValid) { _prj = null; //remove the path if it's not there anymore if (Properties.Settings.Default.Projects.Contains(prjPath)) { Properties.Settings.Default.Projects.Remove(prjPath); Properties.Settings.Default.Save(); cmbProjects.Items.Remove(prjPath); } System.Windows.Forms.MessageBox.Show(prjPath + " is not a valid ArcSWAT project folder."); return; } projectTree1.Project = _prj; //clear views _views.Clear(); _projectView = null; //see what view is currently used if (splitContainer1.Panel2.Controls.Count > 0) { Control currentView = splitContainer1.Panel2.Controls[0]; if (currentView is ProjectView) { updateProjectView(); } else if (currentView is ScenarioView) { if (_prj.Scenarios.Count > 0) { updateScenarioView(_prj.Scenarios.First().Value); } else { updateScenarioView(null); } } else { splitContainer1.Panel2.Controls.Clear(); } } //save current path Properties.Settings.Default.PreviousProjectFolder = prjPath; if (!Properties.Settings.Default.Projects.Contains(prjPath)) { Properties.Settings.Default.Projects.Add(prjPath); cmbProjects.Items.Add(prjPath); cmbProjects.SelectedIndex = cmbProjects.Items.Count - 1; } Properties.Settings.Default.Save(); }
public void setProjectScenario(ArcSWAT.Project project, ArcSWAT.ScenarioResult scenario, ArcSWAT.SWATUnitType type) { _project = project; _scenario = scenario; _type = type; _date = new DateTime(scenario.StartYear, 1, 1); if (onMapTimeChanged != null) { onMapTimeChanged(this, new EventArgs()); } if (type == ArcSWAT.SWATUnitType.SUB) { _unitList = _scenario.Subbasins; } else if (type == ArcSWAT.SWATUnitType.RCH) { _unitList = _scenario.Reaches; } else if (type == ArcSWAT.SWATUnitType.HRU) { _unitList = _scenario.HRUs; } else if (type == ArcSWAT.SWATUnitType.RES) { _unitList = _scenario.Reservoirs; } this.Resize += (ss, ee) => { splitContainer3.SplitterDistance = 72; }; //swat input files extension list swatFileList1.SWATUnitType = _type; swatFileList1.onSWATInputFileExtensionChanged += (s, e) => { if (_unit == null) { return; } string fileName = _unit.getInputFileName(swatFileList1.Extension); if (!System.IO.File.Exists(fileName)) { SWAT_SQLite.showInformationWindow(fileName + " doesn't exist!"); return; } string notePad = System.Environment.SystemDirectory + @"\notepad.exe"; if (System.IO.File.Exists(notePad)) { System.Diagnostics.Process.Start(notePad, fileName); } }; //id list if (type == ArcSWAT.SWATUnitType.HRU) { idList1.IDs = scenario.getSWATUnitIDs(ArcSWAT.SWATUnitType.SUB); } else { idList1.IDs = scenario.getSWATUnitIDs(type); } idList1.onIDChanged += (s, e) => { onIDChanged(idList1.ID); subbasinMap1.ID = idList1.ID; }; //season control seasonCtrl1.onSeasonTypeChanged += (s, e) => { tableView1.Season = seasonCtrl1.Season; outputDisplayChart1.Season = seasonCtrl1.Season; updateTableAndChart(); }; //year control yearCtrl1.Scenario = scenario; yearCtrl1.onYearChanged += (s, e) => { updateTableAndChart(); }; //only for subbasin to show hru list hruList1.Visible = (type == ArcSWAT.SWATUnitType.SUB || type == ArcSWAT.SWATUnitType.HRU); hruList1.IsChangeWhenSelect = (type == ArcSWAT.SWATUnitType.HRU); hruList1.onSwitch2HRU += (hru) => { if (_type == ArcSWAT.SWATUnitType.HRU) { if (_unit != null && _unit.ID == hruList1.HRU.ID) { return; } _unit = hruList1.HRU; //show basic information if (onMapSelectionChanged != null) { onMapSelectionChanged(this, new EventArgs()); } //update table and chart updateTableAndChart(); } if (_type == ArcSWAT.SWATUnitType.SUB) { if (onSwitch2HRU != null) { onSwitch2HRU(hru); } } }; //columns resultColumnTree1.onResultTypeAndColumnChanged += (resultType, col) => { _resultType = resultType; _col = col; //only for daily and monthly this.yearCtrl1.Visible = _scenario.Structure.getInterval(_resultType) == ArcSWAT.SWATResultIntervalType.DAILY || _scenario.Structure.getInterval(_resultType) == ArcSWAT.SWATResultIntervalType.MONTHLY; updateMap(); updateTableAndChart(); }; resultColumnTree1.setScenarioAndUnit(scenario, type); //map subbasinMap1.onLayerSelectionChanged += (unitType, id) => { onIDChanged(id); idList1.ID = id; }; subbasinMap1.setProjectScenario(project, scenario, type); //chart export outputDisplayChart1.onExport += (s, e) => { }; //table view tableView1.onDateChanged += (d) => { if (_type == ArcSWAT.SWATUnitType.HRU) { return; } _date = d; if (onMapTimeChanged != null) { onMapTimeChanged(this, new EventArgs()); } updateMap(); }; //compare control compareCtrl1.ScenarioResult = scenario; compareCtrl1.onCompareResultChanged += (ss, ee) => { updateTableAndChart(); }; //update updateMap(); updateTableAndChart(); }
private void updateTableAndChart() { tableView1.DataTable = null; outputDisplayChart1.clear(); _statistics = "No Statistics Data Available"; if (onDataStatisticsChanged != null) { onDataStatisticsChanged(this, new EventArgs()); } if (_resultType == null || _col == null || _unit == null) { return; } if (!_unit.Results.ContainsKey(_resultType)) { return; } ArcSWAT.SWATUnitResult result = _unit.Results[_resultType]; if (!result.Columns.Contains(_col)) { return; } //consider year selection int year = -1; if ((result.Interval == ArcSWAT.SWATResultIntervalType.DAILY || result.Interval == ArcSWAT.SWATResultIntervalType.MONTHLY) && yearCtrl1.DisplayByYear) { year = yearCtrl1.Year; } //current working result ArcSWAT.SWATUnitColumnYearResult oneResult = result.getResult(_col, year); //set compare control //compareCtrl1.HasObervedData = (oneResult.ObservedData != null); //do the update if (compareCtrl1.CompareResult == null) //don't compare { if (oneResult.Table.Rows.Count == 0 && _type == ArcSWAT.SWATUnitType.HRU) { MessageBox.Show("No results for HRU " + _unit.ID.ToString() + ". For more results, please modify file.cio."); } this.tableView1.Result = oneResult; this.outputDisplayChart1.Result = oneResult; this._statistics = oneResult.SeasonStatistics(seasonCtrl1.Season).ToString(); if (oneResult.ObservedData != null) { this._statistics += " || Compare to Observed: " + oneResult.CompareWithObserved.SeasonStatistics(seasonCtrl1.Season).ToString() + ")"; } if (onDataStatisticsChanged != null) { onDataStatisticsChanged(this, new EventArgs()); } } else //compare { try { ArcSWAT.SWATUnitColumnYearCompareResult compare = null; if (compareCtrl1.CompareResult != null) { compare = oneResult.Compare(compareCtrl1.CompareResult); //compare to scenario this._statistics = string.Format("{0} vs {1}: {2}", result.Unit.Scenario.ModelType, compareCtrl1.CompareResult.ModelType, compare.SeasonStatistics(seasonCtrl1.Season)); if (oneResult.ObservedData != null) { //compare to observed this._statistics += " || "; this._statistics += string.Format("{0} vs Observed: {1}", result.Unit.Scenario.ModelType, oneResult.CompareWithObserved.SeasonStatistics(seasonCtrl1.Season)); ArcSWAT.SWATUnitColumnYearResult comparedData = compare.ComparedData as ArcSWAT.SWATUnitColumnYearResult; this._statistics += " || "; this._statistics += string.Format("{0} vs Observed: {1}", compareCtrl1.CompareResult.ModelType, comparedData.CompareWithObserved.SeasonStatistics(seasonCtrl1.Season)); } } else { compare = oneResult.CompareWithObserved; this._statistics = compare.SeasonStatistics(seasonCtrl1.Season).ToString(); } this.tableView1.CompareResult = compare; this.outputDisplayChart1.CompareResult = compare; if (onDataStatisticsChanged != null) { onDataStatisticsChanged(this, new EventArgs()); } } catch (System.Exception e) { SWAT_SQLite.showInformationWindow(e.ToString()); } } }
public void setProjectScenario(ArcSWAT.Project project, ArcSWAT.ScenarioResult scenario, ArcSWAT.SWATUnitType type) { _project = project; _scenario = scenario; _type = type; _date = new DateTime(scenario.StartYear, 1, 1); if (onMapTimeChanged != null) { onMapTimeChanged(this, new EventArgs()); } if (type == ArcSWAT.SWATUnitType.SUB) { _unitList = _scenario.Subbasins; } else if (type == ArcSWAT.SWATUnitType.RCH) { _unitList = _scenario.Reaches; } else if (type == ArcSWAT.SWATUnitType.HRU) { _unitList = _scenario.HRUs; } else if (type == ArcSWAT.SWATUnitType.RES) { _unitList = _scenario.Reservoirs; } this.Resize += (ss, ee) => { splitContainer3.SplitterDistance = 72; }; //swat input files extension list swatFileList1.SWATUnitType = _type; swatFileList1.onSWATInputFileExtensionChanged += (s, e) => { if (_unit == null) { return; } string fileName = _unit.getInputFileName(swatFileList1.Extension); if (!System.IO.File.Exists(fileName)) { SWAT_SQLite.showInformationWindow(fileName + " doesn't exist!"); return; } string notePad = System.Environment.SystemDirectory + @"\notepad.exe"; if (System.IO.File.Exists(notePad)) { System.Diagnostics.Process.Start(notePad, fileName); } }; //id list if (type == ArcSWAT.SWATUnitType.HRU) { idList1.IDs = scenario.getSWATUnitIDs(ArcSWAT.SWATUnitType.SUB); } else { idList1.IDs = scenario.getSWATUnitIDs(type); } idList1.onIDChanged += (s, e) => { onIDChanged(idList1.ID); subbasinMap1.ID = idList1.ID; setMapTalbeIDSelection(idList1.ID); }; //season control seasonCtrl1.onSeasonTypeChanged += (s, e) => { tableView1.Season = seasonCtrl1.Season; outputDisplayChart1.Season = seasonCtrl1.Season; updateTableAndChart(); }; //year control yearCtrl1.Scenario = scenario; yearCtrl1.onYearChanged += (s, e) => { //update the summary type control summaryTypeCtrl1.CurrentYear = yearCtrl1.Year; //update the time step map view and summary control if (yearCtrl1.Year != -1) { _date = new DateTime(yearCtrl1.Year, 1, 1); summaryTypeCtrl1.TimeForTimeStep = _date; //update the status bar if (onMapTimeChanged != null) { onMapTimeChanged(this, new EventArgs()); } } //update map if (_summaryType != ArcSWAT.ResultSummaryType.AVERAGE_ANNUAL) //only update map when it's not average annual { this.updateMap(); } updateTableAndChart(); }; //summary type control for map summaryTypeCtrl1.ScenarioResult = scenario; summaryTypeCtrl1.onSummaryTypeChanged += (s, e) => { _summaryType = summaryTypeCtrl1.SummaryType; this.updateMap(); //update the status bar if (onMapTimeChanged != null) { onMapTimeChanged(this, new EventArgs()); } }; //only for subbasin to show hru list hruList1.Visible = (type == ArcSWAT.SWATUnitType.SUB || type == ArcSWAT.SWATUnitType.HRU); hruList1.IsChangeWhenSelect = (type == ArcSWAT.SWATUnitType.HRU); hruList1.onSwitch2HRU += (hru) => { if (_type == ArcSWAT.SWATUnitType.HRU) { if (_unit != null && _unit.ID == hruList1.HRU.ID) { return; } _unit = hruList1.HRU; //show basic information if (onMapSelectionChanged != null) { onMapSelectionChanged(this, new EventArgs()); } //update table and chart updateTableAndChart(); } if (_type == ArcSWAT.SWATUnitType.SUB) { if (onSwitch2HRU != null) { onSwitch2HRU(hru); } } }; //columns resultColumnTree1.onResultTypeAndColumnChanged += (resultType, col) => { _resultType = resultType; _col = col; //only for daily and monthly this.yearCtrl1.Visible = _scenario.Structure.getInterval(_resultType) == ArcSWAT.SWATResultIntervalType.DAILY || _scenario.Structure.getInterval(_resultType) == ArcSWAT.SWATResultIntervalType.MONTHLY; updateMap(); updateTableAndChart(); }; resultColumnTree1.setScenarioAndUnit(scenario, type); //the id selection changed tblMapData.RowHeadersVisible = false; tblMapData.ReadOnly = true; tblMapData.Sorted += (s, e) => { //System.Diagnostics.Debug.WriteLine("--------"); //foreach (DataGridViewRow r in tblMapData.Rows) //{ // if (r.Cells[0].Value == null) continue; // System.Diagnostics.Debug.WriteLine(r.Cells[0].Value); //} }; tblMapData.RowEnter += onMapTableIDChanged; //map subbasinMap1.onLayerSelectionChanged += (unitType, id) => { onIDChanged(id); idList1.ID = id; setMapTalbeIDSelection(id); }; subbasinMap1.setProjectScenario(project, scenario, type); subbasinMap1.onMapUpdated += (s, e) => { //get current selected id on map //if none is selected, will be -1 //will keep the current the selection when the datasource of map data table view is changed int id = subbasinMap1.ID; if (id > 0) { this.tblMapData.RowEnter -= onMapTableIDChanged; //remove the handler, don't need to do this when none is selected } this.tblMapData.DataSource = subbasinMap1.DataTable; //set data if (id > 0) { setMapTalbeIDSelection(id); //use current selected id, don't change to a new one this.tblMapData.RowEnter += onMapTableIDChanged; //resume the handler } tblMapData.Columns[SubbasinMap.ID_COLUMN_NAME].HeaderText = _resultType.ToString().ToLower(); tblMapData.Columns[SubbasinMap.RESULT_COLUMN].HeaderText = _col; tblMapData.Columns[SubbasinMap.RESULT_COLUMN].DefaultCellStyle.Format = "F4"; }; //chart export outputDisplayChart1.onExport += (s, e) => { }; //table view tableView1.onDateChanged += (d) => { if (_type == ArcSWAT.SWATUnitType.HRU) { return; } _date = d; summaryTypeCtrl1.TimeForTimeStep = d; if (onMapTimeChanged != null) { onMapTimeChanged(this, new EventArgs()); } if (_summaryType == ArcSWAT.ResultSummaryType.TIMESTEP) { updateMap(); } }; //compare control compareCtrl1.ScenarioResult = scenario; compareCtrl1.onCompareResultChanged += (ss, ee) => { updateTableAndChart(); }; //update updateMap(); updateTableAndChart(); //update the status bar if (onMapTimeChanged != null) { onMapTimeChanged(this, new EventArgs()); } }