/**
         * Copies the most recent data aquisition
         * */
        public MHMDataSet stopAddingData()
        {
            if (index > 1)
            {
                MHMDataSet d = new MHMDataSet(index - 1, startTime);

                for (int i = 0; i < index - 1; i++)
                {
                    d.addDataPoint(Bx[i], By[i], Bz[i], Bnorm[i], timestamp[i], T[i]);
                }

                return(d);
            }
            else
            {
                return(null);
            }
        }
        public MHMDataSet updateReading()
        {
            //get the newest file
            string[]   files      = Directory.GetFiles(filePath, "*.csv");
            DateTime[] fileWrites = new DateTime[files.Length];
            for (int i = 0; i < fileWrites.Length; i++)
            {
                fileWrites[i] = File.GetLastWriteTime(files[i]);
            }
            Array.Sort(fileWrites, files);

            //check if the file is new or whether we are already loading from it
            if (!currentFilename.Equals(files[files.Length - 1]))
            {
                //restart the data aquisition, remember the youngest file is the last one
                currentFilename = files[files.Length - 1];
                prevLineCount   = 0;
                data            = new MHMDataSet();
            }


            //check if the no. of lines has increased since the last time
            int lineCount = 0;

            using (FileStream stream = File.Open(currentFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string line = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        lineCount++;
                    }
                }
            }

            if (lineCount > prevLineCount)
            {
                data.startAddingData();
                int curLine = 0;
                try
                {
                    using (FileStream stream = File.Open(currentFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                //check of the header has been found (the file is not new if the header is already here)
                                if (prevLineCount == 0)
                                {
                                    //find the header
                                    if (String.Equals("DATA", line))
                                    {
                                        line = reader.ReadLine();
                                        curLine++;
                                        string[] pieces = line.Split(';');
                                        header        = pieces;
                                        prevLineCount = curLine;
                                    }
                                }
                                else
                                {
                                    //add new data lines to the buffer
                                    if (curLine > prevLineCount)
                                    {
                                        string[] pieces = line.Split(';');
                                        data.addDataPoint(Convert.ToDouble(pieces[4]), Convert.ToDouble(pieces[5]), Convert.ToDouble(pieces[6]), Convert.ToInt32(pieces[0]), Convert.ToInt32(pieces[1]), Convert.ToInt32(pieces[2]), Convert.ToInt32(pieces[3]), Convert.ToDouble(pieces[7]));
                                    }
                                }
                                curLine++;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine(curLine - prevLineCount);
                prevLineCount = curLine;

                return(data.stopAddingData());
            }
            else
            {
                return(null);
            }
        }