Exemplo n.º 1
0
        //public methods

        public bool LoadDatafromFile(string filename, DataSourceType datasourcetype)
        {
            try
            {
                DataTable       thisTable  = new DataTable();
                IDataRepository repository = new DataRepository(filename, datasourcetype);
                Console.WriteLine(Path.GetFileName(filename));
                thisTable.TableName = Path.GetFileName(filename);
                thisTable           = repository.GetAllData();


                //create sessioncolumn collection
                ISessionColumnCollection collection = new SessionColumnCollection(thisTable);
                //add table to dataset after column colection set up

                _sessionDataSet.Tables.Add(thisTable);
                //if date found get missing dates



                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 2
0
        private void AssignColIndex(IAxis axis, SessionColumnCollection collection)
        {
            AxisType type = axis.AxisType;

            switch (type)
            {
            case AxisType.Month:
            {
                axis.SessionColIndex = collection.DateIndex;
                break;
            }

            case AxisType.Hour:
            {
                axis.SessionColIndex = collection.DateIndex;
                break;
            }

            case AxisType.WD:
            {
                axis.SessionColIndex = collection.WDComp(collection.DataSetStart);
                break;
            }

            case AxisType.WS:
            {
                axis.SessionColIndex = collection.UpperWSComp(collection.DataSetStart);
                break;
            }

            default:
                axis.SessionColIndex = 0;
                break;
            }
        }
Exemplo n.º 3
0
        public override List <double> DeriveNewWS(ISessionColumnCollection collection, AbstractAlpha alpha,
                                                  double[,] sourceWS, DataTable data, double derivedHt)
        {
            SessionColumnCollection colclxn = (SessionColumnCollection)collection;
            int rowcount      = data.Rows.Count;
            int xAxisColIndex = default(int);
            int yAxisColIndex = default(int);

            List <double> newDerivedWS = new List <double>(rowcount);

            //dynamically create list for xAxis source data

            if (alpha.Xaxis != null && alpha.Yaxis == null)
            {
                AssignColIndex(alpha.Xaxis, (SessionColumnCollection)collection);

                Type t = Type.GetType(collection.Columns[alpha.Xaxis.SessionColIndex].ColumnType.ToString());

                MethodInfo mi          = typeof(WindART.Utils).GetMethod("ExtractDataTableColumn", new Type[] { typeof(int), typeof(DataTable) });
                MethodInfo mi1         = mi.MakeGenericMethod(new Type[] { t });
                dynamic    xAxisSource = mi1.Invoke(null, new object[] { alpha.Xaxis.SessionColIndex, data });

                //only 1 axis
                for (int i = 0; i <= rowcount; i++)
                {
                    double thisAlpha = alpha.Alpha[alpha.Xaxis.GetAxisValue(xAxisSource[i]), 0];
                    newDerivedWS[i] = sourceWS[1, i] * Math.Pow((derivedHt / sourceWS[0, i]), thisAlpha);
                }

                return(newDerivedWS);
            }
            if (alpha.Xaxis != null && alpha.Yaxis != null)
            {
                AssignColIndex(alpha.Xaxis, (SessionColumnCollection)collection);
                AssignColIndex(alpha.Yaxis, (SessionColumnCollection)collection);

                MethodInfo miX = typeof(WindART.Utils).GetMethod("ExtractDataTableColumn", new Type[] { typeof(int), typeof(DataTable) });
                //Console.WriteLine(miX.ToString());
                //x
                //Console.WriteLine(" idx passed in...");
                //Console.WriteLine(alpha.Xaxis.SessionColIndex);

                Type tX = Type.GetType(data.Columns[alpha.Xaxis.SessionColIndex].DataType.ToString());
                //Console.WriteLine(tX.ToString ());

                MethodInfo miX1 = miX.MakeGenericMethod(new Type[] { tX });
                //Console.WriteLine("just before invoke..." + miX1.ToString ());
                dynamic xAxisSource = miX1.Invoke(null, new object[] { alpha.Xaxis.SessionColIndex, data });
                //Console.WriteLine(" xsource " + xAxisSource.Count);
                //y
                Type       tY          = Type.GetType(data.Columns[alpha.Yaxis.SessionColIndex].DataType.ToString());
                MethodInfo miY1        = miX.MakeGenericMethod(new Type[] { tY });
                dynamic    yAxisSource = miY1.Invoke(null, new object[] { alpha.Yaxis.SessionColIndex, data });
                //Console.WriteLine(" ysource " + yAxisSource.Count);

                Console.WriteLine(alpha.Alpha.GetUpperBound(0) + "   " + alpha.Alpha.GetUpperBound(1));
                //2 axes
                for (int i = 0; i <= rowcount - 1; i++)
                {
                    //Console.WriteLine(xAxisSource[i]);
                    // Console.WriteLine(" wd " + alpha.Xaxis.GetAxisValue(xAxisSource[i]).ToString());
                    // Console.WriteLine("                      hour " + alpha.Yaxis.GetAxisValue(yAxisSource[i]).ToString());
                    int x; int y;
                    if (alpha.Xaxis.AxisType != AxisType.WS)
                    {
                        x = (int)alpha.Xaxis.GetAxisValue(xAxisSource[i]);
                    }
                    else
                    {
                        x = (int)alpha.Xaxis.GetAxisValue(sourceWS[1, i]);
                    }

                    if (alpha.Yaxis.AxisType != AxisType.WS)
                    {
                        y = (int)alpha.Yaxis.GetAxisValue(yAxisSource[i]);
                    }
                    else
                    {
                        y = (int)alpha.Yaxis.GetAxisValue(sourceWS[1, i]);
                    }

                    if (x < 0 || y < 0)
                    {
                        newDerivedWS.Add(-9999.99);
                    }
                    else
                    {
                        double thisAlpha = default(double);
                        try
                        {
                            thisAlpha = alpha.Alpha[x, y];
                        }
                        catch
                        {
                            //if there is no bin for the ws encountered use a bulk shear
                            thisAlpha = alpha.AllAlphaAvg;
                        }
                        if (sourceWS[1, i] < 0)
                        {
                            newDerivedWS.Add(-9999.99);
                        }
                        else
                        {
                            newDerivedWS.Add(sourceWS[1, i] * Math.Pow((derivedHt / sourceWS[0, i]), thisAlpha));
                        }
                    }
                }

                return(newDerivedWS);
            }

            return(null);
        }
Exemplo n.º 4
0
        public List <IConfig> GetConfigs()
        {
            //determines configurations in column collection by evaluating changes in sensor HT
            //adds columns with the same ht in the date  of the config to a collection within each config
            //does not provide speicifc configs but guarentess that the columns provided have the hts returned at
            //the time of each config
            try
            {
                List <DateTime> Dates  = new List <DateTime>();
                List <IConfig>  result = new List <IConfig>();

                foreach (ISessionColumn col in _collection.Columns)
                {
                    foreach (ISensorConfig c in col.Configs)//create distinct ht and date listing
                    {
                        //Console.WriteLine(c.StartDate + " " + c.Height);
                        Dates.Add(c.StartDate);
                    }
                }
                List <DateTime> distinctDates = Dates.AsEnumerable().Distinct().ToList();
                distinctDates.Sort();

                //find different configs based on ht
                foreach (DateTime d in distinctDates)
                {
                    //create new config for the distinct date which is based on a differing  ht
                    HeightConfig thisConfig = new HeightConfig();

                    thisConfig.StartDate = d;

                    if (distinctDates.IndexOf(d) + 1 > distinctDates.Count - 1)
                    {
                        //end date is now if end of array
                        SessionColumnCollection sc = (SessionColumnCollection)_collection;
                        if (sc.DataSetEnd != default(DateTime))
                        {
                            thisConfig.EndDate = sc.DataSetEnd;
                        }
                        else
                        {
                            thisConfig.EndDate = DateTime.Now;
                        }
                    }
                    else
                    {
                        //end date is next date -1 second if not at end of array
                        thisConfig.EndDate = distinctDates[distinctDates.IndexOf(d) + 1].AddSeconds(-1);
                    }

                    //find all columns with this ht at this date

                    foreach (ISessionColumn col in _collection.Columns)
                    {
                        //don't process this column if it is a composite
                        if (col.IsComposite)
                        {
                            break;
                        }
                        ISensorConfig workConfig = col.getConfigAtDate(d);

                        if (workConfig != null)
                        {
                            //if ht is already in dictionary add the column to that hts list
                            if (!thisConfig.Columns.ContainsKey(workConfig.Height))
                            {
                                //Console.WriteLine(col.ColName + " is ws " + (col.ColumnType == SessionColumnType.WSAvg));
                                IList <ISessionColumn> newcols = new List <ISessionColumn>();
                                newcols.Add(col);
                                thisConfig.Columns.Add(workConfig.Height, newcols);
                            }
                            else
                            {
                                thisConfig.Columns[workConfig.Height].Add(col);
                            }
                        }
                    }

                    result.Add(thisConfig);
                }
                return(result);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
 public CreateDataRecoveryGrid(ISessionColumnCollection collection, DataView data)
 {
     _data       = data;
     _collection = (SessionColumnCollection)collection;
 }
Exemplo n.º 6
0
 public ShearedWS(SessionColumnCollection columnCollection)
 {
     ColumnCollection = columnCollection;
     DateStart        = columnCollection.DataSetStart;
     DateEnd          = columnCollection.DataSetEnd;
 }
Exemplo n.º 7
0
        //methods
        public bool CalculateComposites()
        {
            //get configs
            //add columns to datatable and column collection
            //check deadranges
            //compare sensor a aand b acorsss all configs
            //add columns and update data through the dataview that is each config


            SelectValue UseColumn = default(SelectValue);
            double      missing   = Settings.Default.MissingValue;

            IWindSpeedConfig A = new WindSpeedConfig();
            int    AIndex;
            double AVal;


            IWindSpeedConfig B = new WindSpeedConfig();
            int    BIndex;
            double BVal;

            ISessionColumn ACol;
            ISessionColumn BCol;
            int            compositeIndex = 0;


            IConfigCollection configCollection = new HeightConfigCollection(_collection);
            string            datecol          = _data.Columns [_collection.DateIndex].ColumnName;

            string wdcolname = Enum.GetName(typeof(SessionColumnType), SessionColumnType.WDAvg)
                               + Settings.Default.CompColName;
            int WDIndex = _collection[wdcolname].ColIndex;

            double WDVal;


            double workVal = default(double);

            //find each config across height changes..
            List <HeightConfig> workConfigs
                = configCollection.GetConfigs().ConvertAll <HeightConfig>(t => (HeightConfig)t).ToList();

            //Console.WriteLine("workconfigs count " + workConfigs.Count);
            //loop through each config
            foreach (HeightConfig htConfig in workConfigs)
            {       //loop each ht in config
                DataView configData = _data.AsDataView();
                configData.Sort = Settings.Default.TimeStampName + " asc ";

                //filter the data based on start and end of the current ht config
                string filter = datecol + ">= '" + htConfig.StartDate.ToString() + "' and " + datecol + " <='"
                                + htConfig.EndDate.ToString() + "'";
                Console.WriteLine(filter);
                configData.RowFilter = filter;

                if (configData.Count > _data.Rows.Count)
                {
                    throw new ApplicationException("Error filtering dataset by configuration dates");
                }

                int RowNum = configData.Count;

                Console.WriteLine("datatable and filtered dataviewcounts: " + _data.Rows.Count + " " + RowNum);

                List <double> resultArray = new List <double>(RowNum);
                Console.WriteLine(" Heights found " + htConfig.Columns.Count);

                //loop each height's columns in config
                //each htconfig stores a collection of collumns with those hts
                foreach (KeyValuePair <double, IList <ISessionColumn> > kv in htConfig.Columns)
                {
                    var a = from f in kv.Value.AsEnumerable()
                            where f.ColumnType == SessionColumnType.WSAvg
                            select f;

                    List <ISessionColumn> thisresult = a.ToList();
                    //get sensor pairs that are ws avg types
                    if (a.Count() == 2)
                    {
                        //Console.WriteLine(thisresult[0].ColumnType);
                        //Console.WriteLine(thisresult[1].ColumnType);

                        ACol = thisresult[0];
                        BCol = thisresult[1];

                        AIndex = ACol.ColIndex;
                        BIndex = BCol.ColIndex;

                        //get the specific config for the date of the ht config
                        A = (IWindSpeedConfig)ACol.getConfigAtDate(htConfig.StartDate);
                        B = (IWindSpeedConfig)BCol.getConfigAtDate(htConfig.StartDate);

                        //change the config in the sensor config data if there are dead zones
                        CheckDeadRange(A, B);


                        //Console.WriteLine("A Good Range: " + A.GoodSector.SectorStart + " to " + A.GoodSector.SectorEnd);
                        //Console.WriteLine("A Bad Range: " + A.ShadowSector.SectorStart + " to " + A.ShadowSector.SectorEnd);
                        //Console.WriteLine("B Good Range: " + B.GoodSector.SectorStart + " to " + B.GoodSector.SectorEnd);
                        //Console.WriteLine("B Bad Range: " + B.ShadowSector.SectorStart + " to " + B.ShadowSector.SectorEnd);
                    }
                    else
                    {
                        continue;
                    }

                    double height = A.Height;

                    IList <ISessionColumn> thisColumnSet = kv.Value;

                    //add column to datatable and column collection if necessary
                    WindSpeedCompColumns wscompcols = new WindSpeedCompColumns(_collection, _data);
                    wscompcols.Add(height, thisColumnSet, htConfig.StartDate, htConfig.EndDate);
                    if (NewCompositeColumnAdded != null)
                    {
                        NewCompositeColumnAdded("Wind Speed Composite Columns Added");
                    }


                    #region decide on value
                    //begin calculating comps for each row
                    if (DeterminingWindSpeedCompositeValues != null)
                    {
                        DeterminingWindSpeedCompositeValues("Assigning Wind Speed Composite Values");
                    }

                    foreach (DataRowView rowview in configData)
                    {
                        if (!Double.TryParse(rowview[AIndex].ToString(), out AVal))
                        {
                            AVal = missing;
                        }
                        else
                        {
                            if (AVal < 0)
                            {
                                AVal = missing;
                            }
                        }
                        if (!Double.TryParse(rowview[BIndex].ToString(), out BVal))
                        {
                            BVal = missing;
                        }
                        else
                        {
                            if (BVal < 0)
                            {
                                BVal = missing;
                            }
                        }
                        if (!double.TryParse(rowview[WDIndex].ToString(), out WDVal) || WDVal < 0)
                        {
                            WDVal     = missing;
                            UseColumn = SelectValue.missing;
                            goto assignvalue;
                        }

                        InSector SensorA = A.BelongsToSector(WDVal);
                        InSector SensorB = B.BelongsToSector(WDVal);

                        //in A not in B
                        if (SensorA.Equals(InSector.Not_Shadowed) &&
                            (SensorB.Equals(InSector.Shadowed) |
                             SensorB.Equals(InSector.Neither)))
                        {
                            workVal = AVal;
                            if (workVal.Equals(missing))
                            {
                                UseColumn = SelectValue.missing;
                            }
                            else
                            {
                                UseColumn = SelectValue.A;
                            }

                            goto assignvalue;
                        }

                        //in B not in A take b
                        if (SensorB.Equals(InSector.Not_Shadowed) &&
                            (SensorA.Equals(InSector.Shadowed) |
                             SensorA.Equals(InSector.Neither)))
                        {
                            workVal = BVal;
                            if (workVal < 0)
                            {
                                UseColumn = SelectValue.missing;
                            }
                            else
                            {
                                UseColumn = SelectValue.B;
                            }

                            goto assignvalue;
                        }

                        //in both A and B avg the two
                        if (SensorB.Equals(InSector.Not_Shadowed) &&
                            SensorA.Equals(InSector.Not_Shadowed))
                        {
                            if (AVal < 0 && BVal >= 0)
                            {
                                UseColumn = SelectValue.B;
                                goto assignvalue;
                            }

                            if (BVal < 0 && AVal >= 0)
                            {
                                UseColumn = SelectValue.A;
                                goto assignvalue;
                            }
                            //if wdval is on a boundary use the other sensor
                            //if (WDVal == A.GoodSector.SectorStart | WDVal == A.GoodSector.SectorEnd)
                            //{
                            //    UseColumn = SelectValue.B;
                            //    goto assignvalue;
                            //}

                            //if (WDVal == B.GoodSector.SectorStart | WDVal == B.GoodSector.SectorEnd)
                            //{
                            //    UseColumn = SelectValue.A;
                            //    goto assignvalue;
                            //}

                            List <double> avg = new List <double>()
                            {
                                BVal, AVal
                            };
                            UseColumn = SelectValue.both;
                            workVal   = avg.Average();
                            goto assignvalue;
                        }

                        //otherwise set to missing
                        workVal = missing;
                        goto assignvalue;

                        #endregion
                        #region assign value
assignvalue:
                        {
                            double        useval       = missing;
                            double        usevalB      = missing;
                            string        heightstring = height.ToString().Replace(".", "_");
                            StringBuilder s            = new StringBuilder();
                            s.Append(heightstring);
                            s.Append(Enum.GetName(typeof(SessionColumnType), SessionColumnType.WSAvg));
                            s.Append(Settings.Default.CompColName);
                            string parentColName = s.ToString();
                            if (_collection[parentColName] != null)
                            {
                                compositeIndex = _collection[parentColName].ColIndex;
                            }
                            else
                            {
                                throw new ApplicationException(" Parent composite column " + parentColName + " not found");
                            }
                            switch (UseColumn)
                            {
                            case SelectValue.A:
                                rowview[compositeIndex] = AVal;
                                foreach (ISessionColumn child in ACol.ChildColumns)
                                {
                                    StringBuilder s1 = new StringBuilder();
                                    s1.Append(heightstring);
                                    s1.Append(Enum.GetName(typeof(SessionColumnType), child.ColumnType));
                                    s1.Append(Settings.Default.CompColName);
                                    string thischildcompname = s1.ToString();

                                    if (_collection[thischildcompname] != null)
                                    {
                                        if (!double.TryParse(rowview[child.ColIndex].ToString(), out useval))
                                        {
                                            rowview[thischildcompname] = missing;
                                        }
                                        else
                                        {
                                            rowview[thischildcompname] = useval;
                                        }
                                    }
                                    else
                                    {
                                        throw new ApplicationException(" Child composite column " + thischildcompname + " not found");
                                    }
                                }
                                break;

                            case SelectValue.B:
                                rowview[compositeIndex] = BVal;
                                foreach (ISessionColumn child in BCol.ChildColumns)
                                {
                                    StringBuilder s1 = new StringBuilder();
                                    s1.Append(heightstring);
                                    s1.Append(Enum.GetName(typeof(SessionColumnType), child.ColumnType));
                                    s1.Append(Settings.Default.CompColName);
                                    string thischildcompname = s1.ToString();

                                    if (_collection[thischildcompname] != null)
                                    {
                                        if (!double.TryParse(rowview[child.ColIndex].ToString(), out useval))
                                        {
                                            rowview[thischildcompname] = missing;
                                        }
                                        else
                                        {
                                            rowview[thischildcompname] = useval;
                                        }
                                    }
                                    else
                                    {
                                        throw new ApplicationException(" Child composite column " + thischildcompname + " not found");
                                    }
                                }
                                break;

                            case SelectValue.missing:
                                rowview[compositeIndex] = missing;
                                //System.Windows.Forms.MessageBox.Show(rowview[0].ToString() + " " + WDVal + " " + AVal + " " + BVal);
                                //loop through comp cols for both a and b in case one column has more children than the other
                                foreach (ISessionColumn child in ACol.ChildColumns)
                                {
                                    StringBuilder s1 = new StringBuilder();
                                    s1.Append(heightstring);
                                    s1.Append(Enum.GetName(typeof(SessionColumnType), child.ColumnType));
                                    s1.Append(Settings.Default.CompColName);
                                    string thischildcompname = s1.ToString();

                                    if (_collection[thischildcompname] != null)
                                    {
                                        rowview[thischildcompname] = missing;
                                    }
                                    else
                                    {
                                        throw new ApplicationException(" Child composite column " + thischildcompname + " not found");
                                    }
                                }
                                foreach (ISessionColumn child in BCol.ChildColumns)
                                {
                                    StringBuilder s1 = new StringBuilder();
                                    s1.Append(heightstring);
                                    s1.Append(Enum.GetName(typeof(SessionColumnType), child.ColumnType));
                                    s1.Append(Settings.Default.CompColName);
                                    string thischildcompname = s1.ToString();

                                    if (_collection[thischildcompname] != null)
                                    {
                                        rowview[thischildcompname] = missing;
                                    }
                                    else
                                    {
                                        throw new ApplicationException(" Child composite column " + thischildcompname + " not found");
                                    }
                                }
                                break;

                            case SelectValue.both:
                                rowview[compositeIndex] = new double[2] {
                                    AVal, BVal
                                }.Average();
                                //loop through comp cols for both a and b in case one column has more children than the other
                                foreach (ISessionColumn child in ACol.ChildColumns)
                                {
                                    StringBuilder s1 = new StringBuilder();
                                    s1.Append(heightstring);
                                    s1.Append(Enum.GetName(typeof(SessionColumnType), child.ColumnType));
                                    s1.Append(Settings.Default.CompColName);
                                    string thischildcompname = s1.ToString();

                                    if (_collection[thischildcompname] != null)
                                    {
                                        var result = from c in BCol.ChildColumns.AsEnumerable()
                                                     where c.ColumnType == child.ColumnType
                                                     select c;

                                        List <ISessionColumn> columnfound = result.ToList();

                                        if (columnfound.Count == 1)
                                        {
                                            //Console.WriteLine(" ****************** average taken");
                                            if (!double.TryParse(rowview[columnfound[0].ColIndex].ToString(), out useval) &
                                                !double.TryParse(rowview[child.ColIndex].ToString(), out usevalB))
                                            {
                                                rowview[thischildcompname] = missing;
                                            }
                                            else
                                            {
                                                rowview[thischildcompname] = new double[2] {
                                                    useval, usevalB
                                                }.Average();
                                            }
                                        }
                                        else
                                        {
                                            Console.WriteLine(" missing taken A value =" + AVal + " B Val " + BVal);
                                            rowview[thischildcompname] = missing;
                                        }
                                    }
                                    else
                                    {
                                        throw new ApplicationException(" Child composite column " + thischildcompname + " not found");
                                    }
                                }
                                break;

                            default:
                                break;
                            }
                        }
                    }

                    //calculate recovery
                    SessionColumnCollection scc = (SessionColumnCollection)_collection;
                    scc.CalculateRecoveryRate(compositeIndex);
                    #endregion
                }
            }
            if (CompletedWindSpeedCompositeValues != null)
            {
                //fire completed event
                CompletedWindSpeedCompositeValues("Completed Generating Wind Speed Composites");
            }
            return(true);
        }