Exemple #1
0
        public static AQCSVTimeSeries Load(string csv)
        {
            AQCSVTimeSeries ret = new AQCSVTimeSeries();

            if (string.IsNullOrEmpty(csv))
            {
                return(ret);
            }
            ret.metaData = new AQCSVMetaData();
            using (StringReader reader = new StringReader(csv))
            {
                string line      = null;
                int    lineCount = 0;
                //meta header
                line = reader.ReadLine();
                ret.metaData.headers = CSVUtil.splitCCVLine(line);
                ret.metaData.columns = ret.metaData.headers.Length;
                lineCount++;

                //meta data
                line = reader.ReadLine();
                ret.metaData.datas = CSVUtil.splitCCVLine(line);
                int rangeCount = int.Parse(ret.metaData["NumRanges"]);
                ret.ranges = new AQCSVTable[rangeCount];
                lineCount++;

                for (int i = 0; i < rangeCount; i++)
                {
                    ret.ranges[i] = AQCSVTable.Load(csv, null, "NumPoints", ref lineCount);
                }
                reader.Close();
            }
            return(ret);
        }
Exemple #2
0
        /// <summary>
        /// Load csv.
        /// e.g. Load(csv, true, new string[]{"ExpandedPoints", "OriginalPoints"});
        /// </summary>
        /// <param name="csv">csv string used to load into AQCSV instance.</param>
        /// <param name="withMetaHader">If csv with metadata, this parameter should be true, otherwise set to false.</param>
        /// <param name="tablesRecordCount">string array, items used to show the number of records for each table.</param>
        /// <returns></returns>
        public static AQCSV Load(string csv, bool withMetaHader, string[] tablesRecordCount)
        {
            AQCSV ret = new AQCSV();

            if (string.IsNullOrEmpty(csv))
            {
                return(ret);
            }
            if (tablesRecordCount == null || tablesRecordCount.Length < 1)
            {
                return(ret);
            }
            ret.metaData = new AQCSVMetaData();
            ret.dataSet  = new AQCSVDataSet[tablesRecordCount.Length];
            int[] tablePoints     = new int[tablesRecordCount.Length];
            int[] tablePointsRead = new int[tablesRecordCount.Length];
            for (int i = 0; i < tablesRecordCount.Length; i++)
            {
                ret.dataSet[i] = new AQCSVDataSet();
                tablePoints[i] = tablePointsRead[i] = 0;
            }
            using (StringReader reader = new StringReader(csv))
            {
                string          line        = null;
                int             state       = 0; //0, meta header, 1, meta data, 2 data1 header, 3, data 1, 4 data2 header, 5, data 2 ....
                int             dataSection = 0;
                List <string[]> dateItems   = new List <string[]>();
                if (!withMetaHader)
                {
                    state = 2;
                }
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Length > 0)
                    {
                        if (state == 0)//meta header
                        {
                            ret.metaData.headers = CSVUtil.splitCCVLine(line);
                            ret.metaData.columns = ret.metaData.headers.Length;
                            state = 1;
                        }
                        else if (state == 1)//meta data
                        {
                            ret.metaData.datas = CSVUtil.splitCCVLine(line);
                            state = 2;
                            for (int i = 0; i < tablesRecordCount.Length; i++)
                            {
                                tablePoints[i] = int.Parse(ret.metaData[tablesRecordCount[i]]);
                            }
                        }
                        else
                        {
                            if (state % 2 == 0)//data header, oven state
                            {
                                ret.dataSet[dataSection].headers = CSVUtil.splitCCVLine(line);
                                ret.dataSet[dataSection].columns = ret.dataSet[0].headers.Length;
                                state++;
                            }
                            else//data, odd data
                            {
                                string[] dataItem = CSVUtil.splitCCVLine(line);
                                dateItems.Add(dataItem);
                                tablePointsRead[dataSection]++;
                                if (tablePointsRead[dataSection] == tablePoints[dataSection])
                                {
                                    ret.dataSet[dataSection].datas = dateItems.ToArray();
                                    dateItems.Clear();
                                    state++;
                                    dataSection++;
                                }
                            }
                        }
                    }
                }
                reader.Close();
            }
            return(ret);
        }
Exemple #3
0
        public static AQCSVTable Load(string csv, string tblName, string countColumnName, ref int lineCount)
        {
            AQCSVTable ret = new AQCSVTable();

            ret.tblName = tblName;
            if (string.IsNullOrEmpty(csv))
            {
                return(ret);
            }
            ret.metaData = new AQCSVMetaData();
            ret.dataSet  = new AQCSVDataSet();
            using (StringReader reader = new StringReader(csv))
            {
                string          line       = null;
                int             curLineCnt = 0;
                int             dataNumber = 0;
                int             dataCount  = 0;
                int             state      = 0; //0, meta header, 1, meta data, 2 data header, 3, data
                List <string[]> dateItems  = new List <string[]>();
                while ((line = reader.ReadLine()) != null)
                {
                    curLineCnt++;
                    if (curLineCnt <= lineCount)
                    {
                        continue;
                    }
                    lineCount = curLineCnt;
                    if (line.Length > 0)
                    {
                        switch (state)
                        {
                        case 0:    //meta header
                        {
                            ret.metaData.headers = CSVUtil.splitCCVLine(line);
                            ret.metaData.columns = ret.metaData.headers.Length;
                            state = 1;
                            break;
                        }

                        case 1:    //meta data
                        {
                            ret.metaData.datas = CSVUtil.splitCCVLine(line);
                            if (!string.IsNullOrEmpty(countColumnName))
                            {
                                dataNumber = int.Parse(ret.metaData[countColumnName]);
                            }
                            state = 2;
                            break;
                        }

                        case 2:    //data header
                        {
                            ret.dataSet.headers = CSVUtil.splitCCVLine(line);
                            ret.dataSet.columns = ret.dataSet.headers.Length;
                            state = 3;
                            break;
                        }

                        case 3:    //data
                        {
                            string[] dataItem = CSVUtil.splitCCVLine(line);
                            dateItems.Add(dataItem);
                            dataCount++;
                            break;
                        }

                        default:
                            break;
                        }
                        if (dataNumber > 0 && dataCount == dataNumber) //move to next
                        {
                            lineCount++;
                            break;
                        }
                    }
                }
                reader.Close();
                ret.dataSet.datas  = dateItems.ToArray();
                ret.dataSet.length = ret.dataSet.datas.Length;
            }

            return(ret);
        }