public bool CalculateWindSpeed(double shearht, out ShearCalculationGridCollection gridcollection) { try { //make sure there are axis vals set if (_xaxis == null || _yaxis == null) { //Console.WriteLine(" shear not calculated because no axes were provided"); gridcollection = null; return(false); } //Console.WriteLine(" x axis " + _xaxis.AxisValues.Length + " y axis " + _yaxis.AxisValues.Length); HourAxis hourAxis = null; MonthAxis monthAxis = null; WindDirectionAxis wdAxis = null; //add axes to a list for easier parsing List <IAxis> AssignedAxis = new List <IAxis>(); AssignedAxis.Add(_xaxis); AssignedAxis.Add(_yaxis); bool useWD = false; foreach (IAxis a in AssignedAxis) { switch (a.AxisType) { case AxisType.Hour: hourAxis = (HourAxis)a; break; case AxisType.Month: monthAxis = (MonthAxis)a; break; case AxisType.WD: wdAxis = (WindDirectionAxis)a; useWD = true; throw new NotImplementedException("shear by WD not yet developed"); default: break; } } //Console.WriteLine("assigned axis types"); _data.Sort = Settings.Default.TimeStampName; DateTime t = DateTime.Now; double missing = Settings.Default.MissingValue; //get a date at the beginning of the dataset provided DateTime start = DateTime.Parse(_data[2][_collection.DateIndex].ToString()); //get a list of the columns for this config sorted ascending by height SortedDictionary <double, ISessionColumn> orderedWsCols = _collection.GetColumnsByType(SessionColumnType.WSAvg, start, true); if (orderedWsCols.Count == 0) { throw new ApplicationException("No WS Composite columns were found in MonthHourShear class. Config boundary dates may have been passed incorrectly"); } //sort columns descending by height orderedWsCols.Reverse(); //put the column indexes into a list for coincident values constructor SortedDictionary <double, int> TopCols = new SortedDictionary <double, int>(); foreach (KeyValuePair <double, ISessionColumn> kv in orderedWsCols) { TopCols.Add(kv.Key, _collection[kv.Value.ColName].ColIndex); } List <double> SensorHts = TopCols.Keys.ToList(); SensorHts.Sort(); //Console.WriteLine("first row after sort " + _data[1][0] + " " + _[SensorHts [1]] + " " + _data[1][SensorHts[0]]); //pass the column list and the dataview to get only coincident rows for the columns passed XbyYShearCoincidentRowCollection coincident = new XbyYShearCoincidentRowCollection(_collection.DateIndex, TopCols, _data); int wdColIndex = _collection["WDAvgComposite"].ColIndex; //Console.WriteLine("Shear.CalculateWS:wd comp index=" + wdColIndex ); List <XbyYCoincidentRow> coincidentValues = coincident.GetRows(wdColIndex); //Console.WriteLine("coincident rows count " + coincidentValues.Count); //collection to hold averages at each x y point gridcollection = new ShearCalculationGridCollection(); //get number of elements in each axis int hour = 0; int month = 0; int x = _xaxis.AxisValues.Length; int y = _yaxis.AxisValues.Length; //Console.WriteLine("assigned axis lengths " + x + ", " + y); if (AxisLengthsDetermined != null) { AxisLengthsDetermined("Assigned Alpha Grid Axis lengths " + x + " by " + y); } //arrays to hold upper and lower avgs at each x,y point double[,] UpperAvgGrid = new double[x, y]; double[,] LowerAvgGrid = new double[x, y]; //list to hold avg values for each sensor grid at each x and y bin List <double> upperWsVals = new List <double>(); List <double> lowerWSVals = new List <double>(); for (int a = 0; a < x; a++) { for (int j = 0; j < y; j++) { int TestCount = 0; if (useWD) { } else { if (_xaxis.AxisType == AxisType.Hour) { hour = a; month = j; } else { hour = j; month = a; } hour = (int)hourAxis.AxisValues[hour]; month = (int)monthAxis.AxisValues[month]; //Console.WriteLine("hour " + hour + ", Month " + month); //find the row collection for this x,y point var r = from s in coincidentValues.AsEnumerable() where s.Date.Month == month && s.Date.Hour == hour && s.WD >= 0 select s; //Console.WriteLine(month + " " + hour + " value: " + r.Count()); double useUpperWSVal; double useLowerWSVal; foreach (var row in r) { //if (month == 11) //{ // Console.WriteLine("date " + row.Date + " upper ws " + (double)row.UpperWS); //} if (Double.TryParse(row.UpperWS.ToString(), out useUpperWSVal) && Double.TryParse(row.LowerWS.ToString(), out useLowerWSVal)) { upperWsVals.Add(useUpperWSVal); lowerWSVals.Add(useLowerWSVal); } TestCount++; } } //Console.WriteLine("upper count at " + month + ", " + hour + ": " + TestCount); //add each collection avg to the avg grids //Console.WriteLine("lower average at " + month + ", " + hour + ": " + lowerWSVals.Average() + "/n"); if (upperWsVals.Count > 0 && lowerWSVals.Count > 0) { UpperAvgGrid[a, j] = upperWsVals.Average(); LowerAvgGrid[a, j] = lowerWSVals.Average(); } else { UpperAvgGrid[a, j] = 0; LowerAvgGrid[a, j] = 0; } upperWsVals.Clear(); lowerWSVals.Clear(); //Console.WriteLine("Upper average at " + a + ", " + hour + " "+ UpperAvgGrid[a, j]); //Console.WriteLine("Lower average at " + month + ", " + hour + " " + LowerAvgGrid[a, j]); } } gridcollection.LowerAvg = LowerAvgGrid; gridcollection.UpperAvg = UpperAvgGrid; //calculate each alpha value and assign to a single grid double[,] alphaGrid = new double[x, y]; //fill alpha table for (int a = 0; a < x; a++) { for (int j = 0; j < y; j++) { //Console.WriteLine(" x=" + a.ToString() + " y=" + j.ToString()); alphaGrid[a, j] = Math.Log(gridcollection.UpperAvg [a, j] / gridcollection.LowerAvg [a, j]) / Math.Log(SensorHts[1] / SensorHts[0]); //Console.WriteLine("Alpha in grid=" + alphaGrid[a, j].ToString() + // " upper ws =" + gridcollection.UpperAvg[a, j].ToString() + // " Lower ws =" + gridcollection.LowerAvg[a, j].ToString() + // " upper ht " + SensorHts[1] + " lower ht " + SensorHts[0] + // " month=" + monthAxis.AxisValues[a] + " hour=" + hourAxis.AxisValues[j]); if (Double.IsNaN(alphaGrid[a, j])) { alphaGrid[a, j] = 0; } } } gridcollection.Alpha = alphaGrid; //create a single multi-d array from the original dictionary of values //values must stay in order List <int> CombineCols = TopCols.Values.ToList(); //height and value of the valid ws comp at each row ordered by datetime double[,] result = CombineSensorVals(CombineCols); //Console.WriteLine("Rows in full data " + _data.Count + " rows in combined dataset " + result.GetLength (1)); //array to store sheared ws List <double> ShearResult = new List <double>(result.GetLength(1)); //add colummn to dataset and capture colindex string localcolname = shearht.ToString(); localcolname += Enum.GetName(typeof(SessionColumnType), SessionColumnType.WSAvgShear); localcolname += x + "by" + y; //Console.WriteLine("X by Y shear column will be named " + localcolname); int shearIndex = AddShearColumnToTable(localcolname, shearht); double thisAlpha; int xVal; int yVal; DateTime thisDate; double thisWD; if (AddingShearValues != null) { AddingShearValues("Calculating Sheared Windspeed"); } //*****derive new windspeed //loop combined values for (int i = 0; i < result.GetLength(1); i++) { thisDate = (DateTime)_data[i][_collection.DateIndex]; if (useWD) { thisWD = (double)_data[i][wdColIndex]; } else { xVal = Convert.ToInt16(monthAxis.GetAxisValue(thisDate)); yVal = Convert.ToInt16(hourAxis.GetAxisValue(thisDate)); thisAlpha = gridcollection.Alpha [xVal, yVal]; if (result[1, i] >= 0) { _data[i][shearIndex] = (result[1, i] * Math.Pow((shearht / result[0, i]), thisAlpha)); } else { _data[i][shearIndex] = missing; } } } _data.Table.AcceptChanges(); DateTime t_end = DateTime.Now; TimeSpan dur = t_end - t; Console.WriteLine("Calculating and adding " + x + " by " + y + " shear ws takes " + dur); if (AddedShearValues != null) { AddedShearValues("Shear Calculation Complete"); } return(true); } catch (Exception e) { throw e; } }
public bool CalculateWindSpeed(double shearht, out ShearCalculationGridCollection gridcollection) { //make sure there are axis vals set if (_xaxis == null) { Console.WriteLine(" shear not calculated because no axis was provided"); gridcollection = null; return(false); } Console.WriteLine(" x axis " + _xaxis.AxisValues.Length); HourAxis hourAxis = (HourAxis)_xaxis; _data.Sort = Settings.Default.TimeStampName; DateTime t = DateTime.Now; double missing = Settings.Default.MissingValue; //get a date at the beginning of the dataset provided DateTime start = DateTime.Parse(_data[2][_collection.DateIndex].ToString()); //get a list of the columns for this config sorted ascending by height SortedDictionary <double, ISessionColumn> orderedWsCols = _collection.GetColumnsByType(SessionColumnType.WSAvg, start, true); //sort columns descending by height orderedWsCols.Reverse(); //put the column indexes into a list for coincident values constructor SortedDictionary <double, int> TopCols = new SortedDictionary <double, int>(); foreach (KeyValuePair <double, ISessionColumn> kv in orderedWsCols) { TopCols.Add(kv.Key, kv.Value.ColIndex); } List <double> SensorHts = TopCols.Keys.ToList(); SensorHts.Sort(); //pass the column list and the dataview to get only coincident rows for the columns passed XbyYShearCoincidentRowCollection coincident = new XbyYShearCoincidentRowCollection(_collection.DateIndex, TopCols, _data); int wdColIndex = _collection["WDAvgComposite"].ColIndex; Console.WriteLine("wd comp index=" + wdColIndex); List <XbyYCoincidentRow> coincidentValues = coincident.GetRows(wdColIndex); Console.WriteLine("coincident rows count " + coincidentValues.Count); //collection to hold averages at each x y point gridcollection = new ShearCalculationGridCollection(); //get number of elements in each axis int hour = 0; int x = _xaxis.AxisValues.Length; Console.WriteLine("assigned axis length 1 by " + x); //arrays to hold upper and lower avgs at each x,y point double[,] UpperAvgGrid = new double[1, x]; double[,] LowerAvgGrid = new double[1, x]; //list to hold avg values for each sensor grid at each x and y bin List <double> upperWsVals = new List <double>(); List <double> lowerWSVals = new List <double>(); //only 1 element because this is a 1 by x shear for (int a = 0; a < 1; a++) { for (int j = 0; j < x; j++) { int TestCount = 0; //Console.WriteLine(" a " + a + " j " + j); hour = (int)hourAxis.AxisValues[j]; //find the row collection for this x,y point var r = from s in coincidentValues.AsEnumerable() where s.Date.Hour == hour && s.WD >= 0 select s; //Console.WriteLine(month + " " + hour + " value: " + r.Count()); double useUpperWSVal; double useLowerWSVal; foreach (var row in r) { if (Double.TryParse(row.UpperWS.ToString(), out useUpperWSVal) && Double.TryParse(row.LowerWS.ToString(), out useLowerWSVal)) { upperWsVals.Add(useUpperWSVal); lowerWSVals.Add(useLowerWSVal); } TestCount++; } if (upperWsVals.Count > 0 && lowerWSVals.Count > 0) { UpperAvgGrid[a, j] = upperWsVals.Average(); LowerAvgGrid[a, j] = lowerWSVals.Average(); } else { UpperAvgGrid[a, j] = 0; LowerAvgGrid[a, j] = 0; } upperWsVals.Clear(); lowerWSVals.Clear(); } } gridcollection.LowerAvg = LowerAvgGrid; gridcollection.UpperAvg = UpperAvgGrid; //calculate each alpha value and assign to a single grid double[,] alphaGrid = new double[1, x]; //fill alpha table for (int a = 0; a < 1; a++) { for (int j = 0; j < x; j++) { //Console.WriteLine(" x=" + a.ToString() + " y=" + j.ToString()); alphaGrid[a, j] = Math.Log(gridcollection.UpperAvg[a, j] / gridcollection.LowerAvg[a, j]) / Math.Log(SensorHts[1] / SensorHts[0]); //Console.WriteLine("Alpha in grid=" + alphaGrid[a, j].ToString() + // " upper ws =" + gridcollection.UpperAvg[a, j].ToString() + // " Lower ws =" + gridcollection.LowerAvg[a, j].ToString() + // " upper ht " + SensorHts[1] + " lower ht " + SensorHts[0] + // " month=" + monthAxis.AxisValues[a] + " hour=" + hourAxis.AxisValues[j]); } } gridcollection.Alpha = alphaGrid; //create a single multi-d array from the original dictionary of values //values must stay in order List <int> CombineCols = TopCols.Values.ToList(); //height and value of the valid ws comp at each row ordered by datetime double[,] result = CombineSensorVals(CombineCols); Console.WriteLine("Rows in full data " + _data.Count + " rows in combined dataset " + result.GetLength(1)); //array to store sheared ws List <double> ShearResult = new List <double>(result.GetLength(1)); //add colummn to dataset and capture colindex string localcolname = shearht.ToString(); localcolname += Enum.GetName(typeof(SessionColumnType), SessionColumnType.WSAvgSingleAxisShear); localcolname += "1" + "by" + x; Console.WriteLine("X by Y shear column will be named " + localcolname); int shearIndex = AddShearColumnToTable(localcolname, shearht); double thisAlpha; int yVal; DateTime thisDate; //loop combined values for (int i = 0; i < result.GetLength(1); i++) { thisDate = (DateTime)_data[i][_collection.DateIndex]; yVal = Convert.ToInt16(hourAxis.GetAxisValue(thisDate)); thisAlpha = gridcollection.Alpha[0, yVal]; if (result[0, i] > 0) { _data[i][shearIndex] = (result[1, i] * Math.Pow((shearht / result[0, i]), thisAlpha)); //Console.WriteLine(_data[i][shearIndex]); } else { _data[i][shearIndex] = missing; } } _data.Table.AcceptChanges(); DateTime t_end = DateTime.Now; TimeSpan dur = t_end - t; Console.WriteLine("Calculating and adding 1 by " + x + " shear ws takes " + dur); return(true); }