Esempio n. 1
0
        /// <summary>
        /// This method may be called in another thread.
        /// </summary>
        /// <param name="scenNode"></param>
        /// <param name="scenario"></param>
        /// <param name="modelType"></param>
        private void AddScenarioResult(TreeNode scenNode, ArcSWAT.Scenario scenario, ArcSWAT.SWATModelType modelType)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new addScenarioResultDelegate(AddScenarioResult), scenNode, scenario, modelType);
            }
            else
            {
                foreach (TreeNode n in scenNode.Nodes)
                {
                    if (n.Text.Equals(modelType.ToString()))
                    {
                        scenNode.Nodes.Remove(n); //remove the existing one and then add to update its status.
                        break;
                    }
                }

                TreeNode modelTypeNode = null;
                for (int j = Convert.ToInt32(ArcSWAT.SWATResultIntervalType.MONTHLY); j <= Convert.ToInt32(ArcSWAT.SWATResultIntervalType.YEARLY); j++)
                {
                    ArcSWAT.SWATResultIntervalType interval = (ArcSWAT.SWATResultIntervalType)j;

                    ArcSWAT.ScenarioResult result = scenario.getModelResult(modelType, interval);
                    if (result == null)
                    {
                        continue;
                    }
                    if (result.Status != ArcSWAT.ScenarioResultStatus.NORMAL)
                    {
                        continue;
                    }

                    if (modelTypeNode == null)
                    {
                        modelTypeNode     = scenNode.Nodes.Add(modelType.ToString());
                        modelTypeNode.Tag = modelType;
                    }

                    TreeNode resultNode = modelTypeNode.Nodes.Add(interval.ToString());
                    resultNode.Tag = modelType;
                    resultNode.Nodes.Add("Watershed").Tag = result;
                    resultNode.Nodes.Add("HRU").Tag       = result;
                    resultNode.Nodes.Add("Subbasin").Tag  = result;
                    resultNode.Nodes.Add("Reach").Tag     = result;
                    if (result.Reservoirs.Count > 0)
                    {
                        resultNode.Nodes.Add("Reservoir").Tag = result;
                    }
                    resultNode.Nodes.Add("Difference").Tag  = result;
                    resultNode.Nodes.Add("Performance").Tag = result;
                }

                scenNode.ExpandAll();
            }
        }
Esempio n. 2
0
        private UserControl getView(ArcSWAT.Scenario scenario, ArcSWAT.SWATModelType modelType, ArcSWAT.SWATResultIntervalType interval, ArcSWAT.SWATUnitType unitType)
        {
            string key = getViewName(scenario, modelType, interval, unitType);

            if (!_views.ContainsKey(key))
            {
                if (unitType == ArcSWAT.SWATUnitType.WSHD)
                {
                    WatershedView view = new WatershedView();
                    view.Dock = DockStyle.Fill;
                    view.setProjectScenario(_prj, scenario.getModelResult(modelType, interval));
                    view.onDataStatisticsChanged += (ss, ee) => { onDataStatisticsChanged(view); };

                    _views.Add(key, view);
                }
                else
                {
                    SubbasinView view = new SubbasinView();
                    view.Dock = DockStyle.Fill;

                    view.onMapTimeChanged        += (ss, ee) => { onMapTimeChanged(view); };
                    view.onMapSelectionChanged   += (ss, ee) => { onMapSelectionChanged(view); };
                    view.onDataStatisticsChanged += (ss, ee) => { onDataStatisticsChanged(view); };

                    view.setProjectScenario(_prj, scenario.getModelResult(modelType, interval), unitType);

                    if (unitType == ArcSWAT.SWATUnitType.SUB)
                    {
                        view.onSwitch2HRU += (hru) =>
                        {
                            SubbasinView hruview = switchView(_scenario, _modelType, interval, ArcSWAT.SWATUnitType.HRU) as SubbasinView;
                            hruview.HRU = hru;
                        }
                    }
                    ;
                    _views.Add(key, view);
                }
            }
            return(_views[key]);
        }
Esempio n. 3
0
        private PerformanceView getPerformanceView(ArcSWAT.Scenario scenario, ArcSWAT.SWATModelType modelType, ArcSWAT.SWATResultIntervalType interval)
        {
            string key = string.Format("{0}_{1}_{2}", scenario.Name, modelType, interval);

            if (!_performanceViews.ContainsKey(key))
            {
                PerformanceView performanceView = new PerformanceView();
                performanceView.Result = scenario.getModelResult(modelType, interval);
                performanceView.Dock   = DockStyle.Fill;
                _performanceViews.Add(key, performanceView);
            }
            return(_performanceViews[key]);
        }
Esempio n. 4
0
        /// <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));
        }
Esempio n. 5
0
        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);
            }
        }