Landis.Extension.DynamicFire.CSVParser.ParseToDataTable C# (CSharp) Method

ParseToDataTable() public method

public ParseToDataTable ( string path ) : DataTable
path string
return System.Data.DataTable
        public DataTable ParseToDataTable(string path)
        {
            reader = new StreamReader(path);

            DataTable result = new DataTable();
            result.TableName = "CSV";
            int DeduceRowCount = 5;//rows of data to read for deducing data type
            string line = "";
            int read_row_count=0;

            //header
            string header = reader.ReadLine();
            string[] column_names = BreakCSV(header);

            List<string> DeduceRows = new List<string>();
            List<int> ColTypes = new List<int>();
            for (int i = 0; i < column_names.Length; i++)
                ColTypes.Add(0);
            //Deduction codes:
            //Not Set=0
            //String=1
            //Int32=2
            //Double=3

            //need to read the first DeduceRowCount lines and deduce types of each column
            while (!reader.EndOfStream && read_row_count<DeduceRowCount)
            {
                line = reader.ReadLine();
                read_row_count++;
                DeduceRows.Add(line);
                string[] data = BreakCSV(line);
                int col_type = 0;
                int test_int=0;
                double test_double=0.0;
                for (int i = 0; i < data.Length;i++)
                {
                    string entry = data[i];
                    col_type = 1;
                    try
                    {
                        test_int = Convert.ToInt32(entry);
                        col_type = 2;
                    }
                    catch
                    {//Conversion to Int32 failed, could be Double or string
                        try
                        {
                            test_double = Convert.ToDouble(entry);
                            col_type = 3;
                        }
                        catch
                        {//Conversion to Double failed, can only be a string at this point
                        }
                    }
                    ColTypes[i] = Math.Max(col_type, ColTypes[i]);//If we currently have int, but previously had Double, then make sure we remember that
                }
            }

            //Add the column to the DataTable, with its associated type
            for (int i=0;i<ColTypes.Count;i++)
            {
                DataColumn column;
                switch(ColTypes[i])
                {
                    case 2:
                        column = new DataColumn(column_names[i], typeof(Int32));
                        break;
                    case 3:
                        column = new DataColumn(column_names[i], typeof(Double));
                        break;
                    default:
                        column = new DataColumn(column_names[i], typeof(String));
                        break;
                }
                column.ColumnMapping = MappingType.Attribute;
                result.Columns.Add(column);
            }

            //Now add the DeduceRows to DataTable before moving on
            foreach(string deduced_line in DeduceRows)
            {
                string[] data = BreakCSV(deduced_line);
                int i = 0;
                DataRow dr = result.NewRow();
                foreach (string d in data)
                {
                    dr[i++] = d;
                }
                result.Rows.Add(dr);        
            }
            //Now read the remainder of the data           
            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                string[] data = BreakCSV(line);
                int i = 0;
                DataRow dr = result.NewRow();
                foreach (string d in data)
                {
                    dr[i++] = d;
                }
                result.Rows.Add(dr);
            }

            reader.Close();
            return result;
        }
    }

Usage Example

Example #1
0
        //---------------------------------------------------------------------

        //public static int GenerateFMC(ISeasonParameters season, IDynamicInputRecord fire_region)
        //{

        //    int FMC = 0;
        //    if (season.NameOfSeason == SeasonName.Spring)
        //    {
        //        if (PlugIn.ModelCore.GenerateUniform() < fire_region.SpringFMCHiProp)
        //            FMC = fire_region.SpringFMCHi;
        //        else
        //            FMC = fire_region.SpringFMCLo;
        //    }
        //    if (season.NameOfSeason == SeasonName.Summer)
        //    {
        //        if (PlugIn.ModelCore.GenerateUniform() < fire_region.SummerFMCHiProp)
        //            FMC = fire_region.SummerFMCHi;
        //        else
        //            FMC = fire_region.SummerFMCLo;
        //    }
        //    if (season.NameOfSeason == SeasonName.Fall)
        //    {
        //        if (PlugIn.ModelCore.GenerateUniform() < fire_region.FallFMCHiProp)
        //            FMC = fire_region.FallFMCHi;
        //        else
        //            FMC = fire_region.FallFMCLo;
        //    }
        //    return FMC;
        //}
        //---------------------------------------------------------------------

        public static DataTable ReadWindFile(string path)
        {
            PlugIn.ModelCore.UI.WriteLine("   Loading Wind Input Data...");

            CSVParser windParser = new CSVParser();

            DataTable windTable = windParser.ParseToDataTable(path);

            string selectText = ("Day > 0 AND Day < 365");

            DataRow[] foundRows = windTable.Select(selectText);

            if (foundRows.Length > 0)
            {
                //Input validation
                double WSV;
                int    WINDDIR;
                for (int j = 0; j < foundRows.Length; j++)
                {
                    DataRow myDataRow = foundRows[j];

                    WSV = Convert.ToDouble(myDataRow["WindSpeedVelocity"]);
                    if (WSV < 0.0)
                    {
                        throw new System.ApplicationException("Error: Wind Speed < 0:  Day = " + myDataRow["Day"]);
                    }
                    WINDDIR = (int)myDataRow["WindAzimuth"];
                    if (WINDDIR < 0)
                    {
                        throw new System.ApplicationException("Error: WINDDIR < 0:  Day = " + myDataRow["Day"]);
                    }
                    else if (WINDDIR > 360)
                    {
                        throw new System.ApplicationException("Error: WINDDIR > 360:  Day = " + myDataRow["Day"]);
                    }
                }
            }

            return(windTable);
        }
All Usage Examples Of Landis.Extension.DynamicFire.CSVParser::ParseToDataTable