private Dictionary <int, SWATUnit> readUnitBasicInfo(SWATUnitType type) { Dictionary <int, SWATUnit> units = new Dictionary <int, SWATUnit>(); List <int> ids = new List <int>(); DataTable dt = GetDataTable("select * from " + getInfoTableFromType(type)); foreach (DataRow r in dt.Rows) { SWATUnit unit = null; switch (type) { case SWATUnitType.HRU: unit = new HRU(r, this); break; case SWATUnitType.SUB: unit = new Subbasin(r, this); break; case SWATUnitType.RCH: unit = new Reach(r, this); break; case SWATUnitType.RES: unit = new Reservoir(r, this); break; } if (unit != null && unit.ID != ScenarioResultStructure.UNKONWN_ID && !units.ContainsKey(unit.ID)) { units.Add(unit.ID, unit); ids.Add(unit.ID); } } _units.Add(type, units); _unitIds.Add(type, ids); return(units); }
private ArcSWAT.SWATUnitColumnYearResult getResult(ArcSWAT.ScenarioResult result) { if (_unitType == ArcSWAT.SWATUnitType.UNKNOWN) { return(null); } //get the unit ArcSWAT.SWATUnit unit = result.getSWATUnit(_unitType, _id); if (unit == null) { return(null); } //get unit results foreach (ArcSWAT.SWATUnitResult unitResult in unit.Results.Values) { ArcSWAT.SWATUnitColumnYearResult r = unitResult.getResult(_col, -1); if (r != null) { return(r); } } return(null); }
public void onIDChanged(int id) { if (_type != ArcSWAT.SWATUnitType.SUB && _type != ArcSWAT.SWATUnitType.RCH && _type != ArcSWAT.SWATUnitType.HRU && _type != ArcSWAT.SWATUnitType.RES && _unitList != null) { return; } if (id <= 0) { _unit = null; } else { if (_type == ArcSWAT.SWATUnitType.HRU) { _unit = (_scenario.Subbasins[id] as ArcSWAT.Subbasin).HRUs.First().Value; } else { _unit = _unitList[id]; } } //show basic information if (onMapSelectionChanged != null) { onMapSelectionChanged(this, new EventArgs()); } if (_unit != null) { //get hrus if (_type == ArcSWAT.SWATUnitType.SUB) { hruList1.Subbasin = _unit as ArcSWAT.Subbasin; } if (_type == ArcSWAT.SWATUnitType.HRU) { hruList1.Subbasin = (_unit as ArcSWAT.HRU).Subbasin; } } updateTableAndChart(); }
private void TableResultsCtrl_Load(object sender, EventArgs e) { cmbIDs.SelectedIndexChanged += (s, ee) => { _unit = _units[Convert.ToInt32(cmbIDs.SelectedItem.ToString())]; if (_unit == null) return; lblInfo.Text = _unit.ToStringBasicInfo(); if (cmbResultTypes.Items.Count > 0) { _result = _unit.Results[cmbResultTypes.SelectedItem.ToString()]; updateResult(); return; } cmbResultTypes.Items.Clear(); foreach (string type in _unit.Results.Keys) cmbResultTypes.Items.Add(type); if (cmbResultTypes.Items.Count > 0) cmbResultTypes.SelectedIndex = 0; }; cmbResultTypes.SelectedIndexChanged += (s, ee) => { if (_unit == null) return; _result = _unit.Results[cmbResultTypes.SelectedItem.ToString()]; if (_result == null) return; cmbColumns.Items.Clear(); foreach (string col in _result.Columns) cmbColumns.Items.Add(col); if (cmbColumns.Items.Count > 0) cmbColumns.SelectedIndex = 0; updateResult(); }; cmbColumns.SelectedIndexChanged += (s, ee) => { //to-do change the char while columns changes if (_result == null) return; updateResult(); }; }
/// <summary> /// get compare table for given scenario and model type /// </summary> /// <param name="scenario"></param> /// <param name="modelType"></param> /// <returns></returns> private SWATUnitColumnYearResult getCompareResult(Scenario scenario, SWATModelType modelType) { //get scenario result ScenarioResult compareResult = scenario.getModelResult(modelType, _result.Interval); if (compareResult == null) { throw new Exception("Can't find model " + modelType.ToString() + " in scenario " + scenario.Name); } if (compareResult.Status == ScenarioResultStatus.UNKNOWN) { throw new Exception("The status of model " + modelType.ToString() + " in scenario " + scenario.Name + " is unknown."); } if (compareResult.Status == ScenarioResultStatus.UNSUCCESS) { throw new Exception("The simulation of " + modelType.ToString() + " in scenario " + scenario.Name + " is not successful. Please check the model first."); } if (compareResult.Status == ScenarioResultStatus.NO_EXIST) { throw new Exception("The simulation result of " + modelType.ToString() + " in scenario " + scenario.Name + " doesn't exist. Please run the model first."); } //get unit SWATUnit unit = compareResult.getSWATUnit(_result.Unit.Type, _result.Unit.ID); if (unit == null) { throw new Exception("Can't find " + _result.Unit.Type + " " + _result.Unit.ID.ToString() + " in scenario " + scenario.Name + ",model " + modelType.ToString()); } SWATUnitResult unitResult = unit.getResult(_result.Name); if (unitResult == null) { throw new Exception("Can't find result " + _result.Name + " for " + _result.Unit.Type + " " + _result.Unit.ID.ToString() + " in scenario " + scenario.Name + ",model " + modelType.ToString()); } if (unitResult.Interval != _result.Interval) { throw new Exception("The interval for " + _result.Name + " for " + _result.Unit.Type + " " + _result.Unit.ID.ToString() + " in scenario " + scenario.Name + ",model " + modelType.ToString() + " is different from current result."); } return(unitResult.getResult(_col, _year)); }
/// <summary> /// update corresponding layer /// </summary> /// <param name="type"></param> public void drawLayer(string resultType, string col, DateTime date, ArcSWAT.ResultSummaryType summaryType) { if (_type != ArcSWAT.SWATUnitType.SUB && _type != ArcSWAT.SWATUnitType.RCH) { return; } if (_workingLayer == null) { return; } if (_unitList == null && _scenario != null) { if (_type == ArcSWAT.SWATUnitType.SUB) { _unitList = _scenario.Subbasins; } if (_type == ArcSWAT.SWATUnitType.RCH) { _unitList = _scenario.Reaches; } } if (_unitList == null) { return; } _resultColumn = col; _summaryType = summaryType; _resultDate = date; Debug.WriteLine("Draw Layer, " + _workingLayer.LegendText); Debug.WriteLine("Getting results..."); DateTime d = DateTime.Now; DataTable dt = _workingLayer.DataSet.DataTable; int resultIndex = dt.Columns.IndexOf(RESULT_COLUMN); int idIndex = dt.Columns.IndexOf(ID_COLUMN_NAME); if (summaryType == ArcSWAT.ResultSummaryType.AVERAGE_ANNUAL || summaryType == ArcSWAT.ResultSummaryType.ANNUAL) { int year = -1; if (summaryType == ArcSWAT.ResultSummaryType.ANNUAL) { year = date.Year; } Dictionary <int, double> ave_annual = _scenario.getAverageAnnualResults(_type, col, year); foreach (DataRow r in dt.Rows) { r[resultIndex] = ArcSWAT.ScenarioResultStructure.EMPTY_VALUE; int id = int.Parse(r[idIndex].ToString()); r[resultIndex] = ave_annual[id]; } } else { foreach (DataRow r in dt.Rows) { r[resultIndex] = ArcSWAT.ScenarioResultStructure.EMPTY_VALUE; int id = int.Parse(r[idIndex].ToString()); if (!_unitList.ContainsKey(id)) { continue; } ArcSWAT.SWATUnit unit = _unitList[id]; if (!unit.Results.ContainsKey(resultType)) { continue; } ArcSWAT.SWATUnitResult result = unit.Results[resultType]; if (!result.Columns.Contains(col)) { continue; } if (summaryType == ArcSWAT.ResultSummaryType.TIMESTEP) { r[resultIndex] = result.getData(col, date); } else if (summaryType == ArcSWAT.ResultSummaryType.ANNUAL) //annual { r[resultIndex] = result.getData(col, date.Year); } } } Debug.WriteLine(DateTime.Now.Subtract(d).TotalMilliseconds); Debug.WriteLine("setLayerSchema"); d = DateTime.Now; //update symbol setLayerSchema(_workingLayer); Debug.WriteLine(DateTime.Now.Subtract(d).TotalMilliseconds); ////update chart //onLayerSelectionChanged(type); if (onMapUpdated != null) { onMapUpdated(this, new EventArgs()); } }
/// <summary> /// update corresponding layer /// </summary> /// <param name="type"></param> public void drawLayer(string resultType, string col, DateTime date) { if (_type != ArcSWAT.SWATUnitType.SUB && _type != ArcSWAT.SWATUnitType.RCH) { return; } if (_workingLayer == null) { return; } if (_unitList == null && _scenario != null) { if (_type == ArcSWAT.SWATUnitType.SUB) { _unitList = _scenario.Subbasins; } if (_type == ArcSWAT.SWATUnitType.RCH) { _unitList = _scenario.Reaches; } } if (_unitList == null) { return; } Debug.WriteLine("Draw Layer, " + _workingLayer.LegendText); Debug.WriteLine("Getting results..."); DateTime d = DateTime.Now; DataTable dt = _workingLayer.DataSet.DataTable; int resultIndex = dt.Columns.IndexOf(RESULT_COLUMN); int idIndex = dt.Columns.IndexOf(ID_COLUMN_NAME); foreach (DataRow r in dt.Rows) { r[resultIndex] = ArcSWAT.ScenarioResultStructure.EMPTY_VALUE; int id = int.Parse(r[idIndex].ToString()); if (!_unitList.ContainsKey(id)) { continue; } ArcSWAT.SWATUnit unit = _unitList[id]; if (!unit.Results.ContainsKey(resultType)) { continue; } ArcSWAT.SWATUnitResult result = unit.Results[resultType]; if (!result.Columns.Contains(col)) { continue; } r[resultIndex] = result.getData(col, date); } Debug.WriteLine(DateTime.Now.Subtract(d).TotalMilliseconds); Debug.WriteLine("setLayerSchema"); d = DateTime.Now; //update symbol setLayerSchema(_workingLayer); Debug.WriteLine(DateTime.Now.Subtract(d).TotalMilliseconds); ////update chart //onLayerSelectionChanged(type); }
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(); }
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()); } }
public SWATUnitResult(string tableName, SWATUnit parentUnit) { _tableName = tableName; _unit = parentUnit; }
private void TableResultsCtrl_Load(object sender, EventArgs e) { cmbIDs.SelectedIndexChanged += (s, ee) => { _unit = _units[Convert.ToInt32(cmbIDs.SelectedItem.ToString())]; if (_unit == null) { return; } lblInfo.Text = _unit.ToStringBasicInfo(); if (cmbResultTypes.Items.Count > 0) { _result = _unit.Results[cmbResultTypes.SelectedItem.ToString()]; updateResult(); return; } cmbResultTypes.Items.Clear(); foreach (string type in _unit.Results.Keys) { cmbResultTypes.Items.Add(type); } if (cmbResultTypes.Items.Count > 0) { cmbResultTypes.SelectedIndex = 0; } }; cmbResultTypes.SelectedIndexChanged += (s, ee) => { if (_unit == null) { return; } _result = _unit.Results[cmbResultTypes.SelectedItem.ToString()]; if (_result == null) { return; } cmbColumns.Items.Clear(); foreach (string col in _result.Columns) { cmbColumns.Items.Add(col); } if (cmbColumns.Items.Count > 0) { cmbColumns.SelectedIndex = 0; } updateResult(); }; cmbColumns.SelectedIndexChanged += (s, ee) => { //to-do change the char while columns changes if (_result == null) { return; } updateResult(); }; }