Пример #1
0
        public ReturnCode ParseFirstEntry(string newFileName)
        {
            fileName = newFileName;
            string[] lines;
            try
            {
                lines = IOUtility.PermissiveReadLines(fileName, nHeaders + 1);
            }
            catch (Exception ex)
            {
                return(ReturnCode.COULD_NOT_OPEN_FILE);
            }

            int nDataLines = lines.Length - nHeaders;

            if (nDataLines < 1)
            {
                return(ReturnCode.CORRUPTED_FILE);
            }
            nDataLines = 1;

            if (GetEndTimes)
            {
                data = new double[nDataLines, nColumns - 2];
            }
            else
            {
                data = new double[nDataLines, nColumns - 1];
            }
            timeStamps = new DateTime[nDataLines];
            if (GetEndTimes)
            {
                endTimes = new DateTime[nDataLines];
            }

            nRecords = nDataLines;
            int entry;

            try
            {
                string[] tokens;
                int      i = nHeaders;
                entry = i - nHeaders;
                char[] delimiters = new char[] { ',' };
                switch (Delimiter)
                {
                case DelimiterType.Comma:
                    delimiters = new char[] { ',' };
                    break;

                case DelimiterType.CommaOrWhitespace:
                    delimiters = new char[] { ',', ' ', '\t' };
                    break;
                }
                tokens = lines[i].Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                if (tokens.Length < nColumns)
                {
                    return(ReturnCode.CORRUPTED_FILE);
                }
                if (TimeStampFormat == "")
                {
                    timeStamps[entry] = DateTime.Parse(tokens[0].Trim(TRIM_CHARS));
                    if (GetEndTimes)
                    {
                        endTimes[entry] = DateTime.Parse(tokens[1].Trim(TRIM_CHARS));
                    }
                }
                else
                {
                    timeStamps[entry] = DateTime.ParseExact(tokens[0].Trim(TRIM_CHARS), TimeStampFormat, CULTURE_INFO);
                    if (GetEndTimes)
                    {
                        endTimes[entry] = DateTime.ParseExact(tokens[0].Trim(TRIM_CHARS), TimeStampFormat, CULTURE_INFO);
                    }
                }
                int dataColStart = 1;
                if (GetEndTimes)
                {
                    dataColStart = 2;
                }
                for (int j = dataColStart; j < nColumns; j++)
                {
                    data[entry, j - dataColStart] = double.Parse(tokens[j]);
                }
            }
            catch
            {
                return(ReturnCode.CORRUPTED_FILE);
            }

            return(ReturnCode.SUCCESS);
        }
Пример #2
0
        public ReturnCode ParseFile(string fileName)
        {
            // Try reading the file into lines
            string[] lines;
            try
            {
                lines = IOUtility.PermissiveReadAllLines(fileName);
            }
            catch (Exception ex)
            {
                return(ReturnCode.COULD_NOT_OPEN_FILE);
            }
            if (lines.Length < 2)
            {
                return(ReturnCode.CORRUPTED_FILE);
            }

            // Read the first line
            string[] tokens;
            tokens = lines[0].Split(',');
            if (tokens[0].ToLower() != "event list")
            {
                return(ReturnCode.CORRUPTED_FILE);
            }
            if (tokens[1].ToLower() != "version")
            {
                return(ReturnCode.CORRUPTED_FILE);
            }
            int version;

            if (!int.TryParse(tokens[2], out version))
            {
                return(ReturnCode.CORRUPTED_FILE);
            }
            Version = version;

            // Read the column headers
            int eventStartCol = 0;
            int eventEndCol   = 1;
            int durationCol   = 2;
            int maxValCol     = 3;
            int maxTimeCol    = 4;
            int commentsCol   = 5;
            int nColumns      = 5;

            tokens = lines[1].Split(',');
            for (int col = 0; col < tokens.Length; col++)
            {
                switch (tokens[col].ToLower())
                {
                case "event start":
                    eventStartCol = col;
                    if (col > nColumns)
                    {
                        nColumns = col;
                    }
                    break;

                case "event end":
                    eventEndCol = col;
                    if (col > nColumns)
                    {
                        nColumns = col;
                    }
                    break;

                case "duration":
                    durationCol = col;
                    if (col > nColumns)
                    {
                        nColumns = col;
                    }
                    break;

                case "max value":
                    maxValCol = col;
                    if (col > nColumns)
                    {
                        nColumns = col;
                    }
                    break;

                case "max time":
                    maxTimeCol = col;
                    if (col > nColumns)
                    {
                        nColumns = col;
                    }
                    break;

                case "comments":
                    commentsCol = col;
                    if (col > nColumns)
                    {
                        nColumns = col;
                    }
                    break;
                }
            }
            nColumns++;

            // Read event content
            DateTime start;
            DateTime end;

            for (int l = 2; l < lines.Length; ++l)
            {
                tokens = lines[l].Split(',');
                if (tokens.Length < nColumns)
                {
                    return(ReturnCode.CORRUPTED_FILE);
                }
                start = DateTime.Parse(tokens[eventStartCol]);
                end   = DateTime.Parse(tokens[eventEndCol]);
                StartTime.Add(start);
                EndTime.Add(end);
                MaxValue.Add(double.Parse(tokens[maxValCol]));
                MaxTime.Add(DateTime.Parse(tokens[maxTimeCol]));
                Comments.Add(tokens[commentsCol]);
            }

            return(ReturnCode.SUCCESS);
        }
Пример #3
0
        public override ReturnCode ParseSpectrumFile(string newFileName)
        {
            string[] lines;
            try
            {
                lines = IOUtility.PermissiveReadAllLines(newFileName);
            }
            catch
            {
                return(ReturnCode.COULD_NOT_OPEN_FILE);
            }

            // Grab the counts
            int dataLine = -1;

            for (int l = 0; l < lines.Length; l++)
            {
                if (lines[l].ToUpper().StartsWith("$DATA"))
                {
                    dataLine = l;
                    break;
                }
            }
            if (dataLine < 0)
            {
                return(ReturnCode.CORRUPTED_FILE);
            }
            try
            {
                string[] tokens     = lines[dataLine + 1].Split(' ');
                int      first_chan = int.Parse(tokens[0]);
                int      last_chan  = int.Parse(tokens[1]);
                int      nChannels  = last_chan - first_chan + 1;
                counts = new int[nChannels];
                for (int chan = 0; chan < nChannels; chan++)
                {
                    counts[chan] = int.Parse(lines[dataLine + chan + 2]);
                }
            }
            catch
            {
                return(ReturnCode.CORRUPTED_FILE);
            }

            // Grab measurement time
            int measTimLine = -1;

            for (int l = 0; l < lines.Length; l++)
            {
                if (lines[l].ToUpper().StartsWith("$MEAS_TIM"))
                {
                    measTimLine = l;
                    break;
                }
            }
            if (measTimLine > 0)
            {
                try
                {
                    string[] tokens = lines[measTimLine + 1].Split(' ');
                    liveTime = double.Parse(tokens[0]);
                    realTime = double.Parse(tokens[1]);
                }
                catch
                {
                    return(ReturnCode.CORRUPTED_FILE);
                }
            }

            // Grab start DateTime
            int dateMeaLine = -1;

            for (int l = 0; l < lines.Length; l++)
            {
                if (lines[l].ToUpper().StartsWith("$DATE_MEA"))
                {
                    dateMeaLine = l;
                    break;
                }
            }
            if (dateMeaLine > 0)
            {
                try
                {
                    string[] tokens = lines[dateMeaLine + 1].Replace('-', ' ').Replace(':', ' ').Split(' ');
                    int      month  = int.Parse(tokens[0]);
                    int      day    = int.Parse(tokens[1]);
                    int      year   = int.Parse(tokens[2]);
                    int      hour   = int.Parse(tokens[3]);
                    int      min    = int.Parse(tokens[4]);
                    int      sec    = int.Parse(tokens[5]);
                    startDateTime = new DateTime(year, month, day, hour, min, sec);
                }
                catch
                {
                    return(ReturnCode.CORRUPTED_FILE);
                }
            }

            // Grab calibration
            int enerFitLine = -1;

            for (int l = 0; l < lines.Length; l++)
            {
                if (lines[l].ToUpper().StartsWith("$ENER_FIT"))
                {
                    enerFitLine = l;
                    break;
                }
            }
            if (enerFitLine > 0)
            {
                try
                {
                    string[] tokens = lines[enerFitLine + 1].Split(' ');
                    zero          = float.Parse(tokens[0]);
                    keVPerChannel = float.Parse(tokens[1]);
                }
                catch
                {
                    return(ReturnCode.CORRUPTED_FILE);
                }
            }
            return(ReturnCode.SUCCESS);
        }