From 0b2928279f90387658c8b03f82b32d0619a557aa Mon Sep 17 00:00:00 2001
From: Anton Kudryashov <qubabox@mail.ru>
Date: Thu, 1 Dec 2016 18:11:35 +0300
Subject: [PATCH] fixed check values methods

---
 MES_Wind/frmMain.cs                     | 17 ++++-----
 WindStressPRM/Objects/ClimateCell.cs    |  8 +---
 WindStressPRM/Objects/PowerStation.cs   |  9 +++--
 WindStressPRM/Objects/Powerline.cs      | 50 +++++++++++++------------
 WindStressPRM/Objects/PrognosticCell.cs | 17 +++++----
 WindStressPRM/Utils/CellSize.cs         | 16 +++++---
 WindStressPRM/Utils/Coordinate.cs       | 16 +++++---
 WindStressPRM/Utils/Matrix.cs           |  3 +-
 8 files changed, 72 insertions(+), 64 deletions(-)

diff --git a/MES_Wind/frmMain.cs b/MES_Wind/frmMain.cs
index 6dfa09a..67bafe8 100644
--- a/MES_Wind/frmMain.cs
+++ b/MES_Wind/frmMain.cs
@@ -210,17 +210,16 @@ namespace MES_Wind
             List<WindStressPRM.Powerline> powerlinesToPRM = new List<WindStressPRM.Powerline>();
             foreach (IFeature feature in pwlineSet.Features)
             {
-                WindStressPRM.Powerline dummyline = new WindStressPRM.Powerline();
                 DataRow featureData = feature.DataRow;
-                dummyline.Identifier = feature.Fid;
-                dummyline.Year = int.Parse(featureData["Year"].ToString());
-                dummyline.Height = double.Parse(featureData["HeightOffs"].ToString());
-                dummyline.Voltage = int.Parse(featureData["Voltage"].ToString());
-                dummyline.PointFromID = int.Parse(featureData["PointFrom"].ToString());
-                dummyline.PointToID = int.Parse(featureData["PointTo"].ToString());
+                int Identifier = feature.Fid;
+                int Year = int.Parse(featureData["Year"].ToString());
+                double Height = double.Parse(featureData["HeightOffs"].ToString());
+                int Voltage = int.Parse(featureData["Voltage"].ToString());
+                int PointFromID = int.Parse(featureData["PointFrom"].ToString());
+                int PointToID = int.Parse(featureData["PointTo"].ToString());
                 LineString featureline = feature.BasicGeometry as LineString;
-                dummyline.Coordinates = DotspLinestringToPrm(featureline);
-                powerlinesToPRM.Add(dummyline);
+                WindStressPRM.Powerline powerline = new WindStressPRM.Powerline(DotspLinestringToPrm(featureline), Identifier, Year, Height, Voltage, PointToID, PointFromID);
+                powerlinesToPRM.Add(powerline);
             }
             //create PRM_station list to pass to PRM_wind from loaded point layer
             List<WindStressPRM.PowerStation> powerpointsToPRM = new List<WindStressPRM.PowerStation>();
diff --git a/WindStressPRM/Objects/ClimateCell.cs b/WindStressPRM/Objects/ClimateCell.cs
index 0a8bcec..66d93f6 100644
--- a/WindStressPRM/Objects/ClimateCell.cs
+++ b/WindStressPRM/Objects/ClimateCell.cs
@@ -46,16 +46,13 @@ namespace WindStressPRM
             this.Wind10 = w10;
             this.Wind15 = w15;
             this.Wind25 = w25;
-            if (!(this.CheckValue()))
-            {
-                throw new System.ArgumentException("Climate wind value is not correct");
-            }
+            this.CheckValue();
         }
         /// <summary>
         /// Провекра валидности полей класса
         /// </summary>
         /// <returns></returns>
-        public bool CheckValue()
+        private void CheckValue()
         {
             double w5 = Wind5;
             double w10 = Wind10;
@@ -94,7 +91,6 @@ namespace WindStressPRM
             {
                 throw new System.ArgumentOutOfRangeException("w25", w25, "Expected 0=<w25<70"); 
             }
-            return true;
         }
     }
 }
diff --git a/WindStressPRM/Objects/PowerStation.cs b/WindStressPRM/Objects/PowerStation.cs
index 0d6e8b8..1f96a56 100644
--- a/WindStressPRM/Objects/PowerStation.cs
+++ b/WindStressPRM/Objects/PowerStation.cs
@@ -99,15 +99,16 @@ namespace WindStressPRM
             this.IsON = false;
             CheckValue();
         }
-        public bool CheckValue()
+        private void CheckValue()
         {
-            if (Identifier < 0) {
+            if (Identifier < 0)
+            {
                 throw new System.ArgumentOutOfRangeException("Identifier", Identifier, "Identifer expected to be more than 0"); 
             }
-            if (Voltage < 0 || Voltage > 1500) {
+            if (Voltage < 0 || Voltage > 1500)
+            {
                 throw new System.ArgumentOutOfRangeException("Voltage", Voltage, "Voltage expected to be in range (0,1500)"); 
             }
-            return true;
         }
         /// <summary>
         /// Gets attached lines list
diff --git a/WindStressPRM/Objects/Powerline.cs b/WindStressPRM/Objects/Powerline.cs
index 9fd1ee9..f6f6016 100644
--- a/WindStressPRM/Objects/Powerline.cs
+++ b/WindStressPRM/Objects/Powerline.cs
@@ -56,11 +56,6 @@ namespace WindStressPRM
         /// получает (true) или нет (false) линия питание
         /// </summary>
         public bool IsON { get; set; }
-
-        public Powerline()
-        {
-            //default constructor body
-        }
         /// <summary>
         /// designated constructor
         /// </summary>
@@ -82,31 +77,38 @@ namespace WindStressPRM
             this.IsON = false;
             this.PointFromID = fromID;
             this.PointToID = toID;
-            if (!(this.CheckValue()))
-            {
-                throw new System.ArgumentException("Powerline object wasn't initialized correctly");
-            }
+            this.CheckValue();
         }
         /// <summary>
         /// проверка валидности полей
         /// </summary>
         /// <returns></returns>
-        public bool CheckValue()
+        private void CheckValue()
         {
-            bool checker =
-                Identifier >= 0;
-            if (!checker) { throw new System.ArgumentOutOfRangeException("Identifier", Identifier, "Expected >0"); }
-            checker = checker && Year > 1900 && Year < 2050;
-            if (!checker) { throw new System.ArgumentOutOfRangeException("Year", Year, "Expected 1900<Year<2050"); }
-            checker = checker && Height > 0 && Height < 50;
-            if (!checker) { throw new System.ArgumentOutOfRangeException("Height", Height, "Expected 0<Height<50"); }
-            checker = checker && Voltage > 0 && Voltage < 1500;
-            if (!checker) { throw new System.ArgumentOutOfRangeException("Voltage", Voltage, "Expected 0<Voltage<1500"); }
-            checker = checker && PointFromID >= 0;
-            if (!checker) { throw new System.ArgumentOutOfRangeException("PointFromID", PointFromID, "Expected >0"); }
-            checker = checker && PointToID >= 0;
-            if (!checker) { throw new System.ArgumentOutOfRangeException("PointToID", PointToID, "Expected >0"); }
-            return checker;
+            if (Identifier < 0)
+            { 
+                throw new System.ArgumentOutOfRangeException("Identifier", Identifier, "Expected >0"); 
+            }
+            if (Year > 2050 || Year < 1900)
+            {
+                throw new System.ArgumentOutOfRangeException("Year", Year, "Expected 1900<=Year<=2050");
+            }
+            if (Height <= 0 || Height > 50) 
+            {
+                throw new System.ArgumentOutOfRangeException("Height", Height, "Expected 0<Height<=50"); 
+            }
+            if (Voltage <= 0 || Voltage > 1500)
+            { 
+                throw new System.ArgumentOutOfRangeException("Voltage", Voltage, "Expected 0<Voltage<=1500");
+            }
+            if (PointFromID < 0)
+            {
+                throw new System.ArgumentOutOfRangeException("PointFromID", PointFromID, "Expected >0");
+            }
+            if (PointToID < 0)
+            {
+                throw new System.ArgumentOutOfRangeException("PointToID", PointToID, "Expected >0"); 
+            }
         }
     }
 }
diff --git a/WindStressPRM/Objects/PrognosticCell.cs b/WindStressPRM/Objects/PrognosticCell.cs
index e2e5b0a..4a91543 100644
--- a/WindStressPRM/Objects/PrognosticCell.cs
+++ b/WindStressPRM/Objects/PrognosticCell.cs
@@ -31,30 +31,31 @@ namespace WindStressPRM
         {
             this.VelocityX = vX;
             this.VelocityY = vY;
-            if (!(this.CheckValue()))
-            {
-                throw new System.ArgumentException("Prognostic wind velocities are incorrect");
-            }
+            this.CheckValue();
         }
         /// <summary>
         /// Проверка полей на валидность
         /// </summary>
         /// <returns></returns>
-        public bool CheckValue()
+        private void CheckValue()
         {
             bool res1 = Double.IsNaN(VelocityX);
             bool res2 = Double.IsNaN(VelocityY);
             if (res1 != res2)
             {
-                return false;
+                throw new System.ArgumentException("Prognostic wind velocities are incorrect: one projection is Nan, and other has value.");
             }
             // пустые данные
             if (res1 == res2 == true)
             {
-                return true;
+                return;
             }
             ///Скорость ветра на высоте 10м от поверхности на Земле не может превышать 70м/c
-            return Math.Abs(Math.Pow(VelocityX, 2) + Math.Pow(VelocityY, 2)) <= 4900;
+            double module = Math.Sqrt(Math.Abs(Math.Pow(VelocityX, 2) + Math.Pow(VelocityY, 2)));
+            if (module > 70) 
+            {
+                throw new System.ArgumentOutOfRangeException("Module of wind speed", module, "Expected 0<Module<=70"); 
+            }
         }
     }
 }
diff --git a/WindStressPRM/Utils/CellSize.cs b/WindStressPRM/Utils/CellSize.cs
index 4bb470b..f0e5f19 100644
--- a/WindStressPRM/Utils/CellSize.cs
+++ b/WindStressPRM/Utils/CellSize.cs
@@ -28,18 +28,22 @@ namespace WindStressPRM
         {
             this.Width = width;
             this.Height = height;
-            if (!(this.CheckValue()))
-            {
-                throw new System.ArgumentException("Cell width or height values are incorrect!");
-            }
+            this.CheckValue();
         }
         /// <summary>
         /// Проверка валидности полей
         /// </summary>
         /// <returns></returns>
-        public bool CheckValue()
+        private void CheckValue()
         {
-            return Width > 0 && Height > 0;
+            if (Width <= 0 || Double.IsNaN(Width))
+            {
+                throw new System.ArgumentOutOfRangeException("Cellsize.width is incorrect", Width, "Expected 0<Width");
+            }
+            if (Height <= 0 || Double.IsNaN(Height))
+            {
+                throw new System.ArgumentOutOfRangeException("Cellsize.height is incorrect", Height, "Expected 0<Height");
+            }
         }
     }
 }
diff --git a/WindStressPRM/Utils/Coordinate.cs b/WindStressPRM/Utils/Coordinate.cs
index 6e2f0c8..0e0de41 100644
--- a/WindStressPRM/Utils/Coordinate.cs
+++ b/WindStressPRM/Utils/Coordinate.cs
@@ -29,18 +29,22 @@ namespace WindStressPRM
         {
             this.X = X;
             this.Y = Y;
-            if (!(this.CheckValue()))
-            {
-                throw new System.ArgumentException("Passed coordinates are not valid!");
-            }
+            this.CheckValue();
         }
         /// <summary>
         /// Проверка на валидность значений
         /// </summary>
         /// <returns></returns>
-        public bool CheckValue()
+        private void CheckValue()
         {
-            return !Double.IsNaN(X) && !Double.IsInfinity(X) && !Double.IsNaN(Y) && !Double.IsInfinity(Y);
+            if (Double.IsNaN(X) || Double.IsInfinity(X))
+            {
+                throw new System.ArgumentOutOfRangeException("Coordinate.X is incorrect", X, "Expected X!=Nan, X!=INF");
+            }
+            if (Double.IsNaN(Y) || Double.IsInfinity(Y))
+            {
+                throw new System.ArgumentOutOfRangeException("Coordinate.Y is incorrect", Y, "Expected Y!=Nan, Y!=INF");
+            }
         }
     }
 }
diff --git a/WindStressPRM/Utils/Matrix.cs b/WindStressPRM/Utils/Matrix.cs
index a4677bc..51f81f6 100644
--- a/WindStressPRM/Utils/Matrix.cs
+++ b/WindStressPRM/Utils/Matrix.cs
@@ -18,7 +18,8 @@ namespace WindStressPRM
         /// </summary>
         /// <param name="index"></param>
         /// <returns></returns>
-        public Coordinate CellToProjection(Index index) {
+        public Coordinate CellToProjection(Index index)
+        {
             return new Coordinate(Origin.X + index.Row*Size.Width, Origin.Y - index.Col*Size.Height);
         }
         /// <summary>
-- 
GitLab