private int DoWritePeakList(IRawFile2 rawReader, int scan, string fileName, List <string> returnFiles, string experimental, int lastSpectrumNumber, List <int> ignoreScans, ref bool bReadAgain)
        {
            var result = scan + 1;

            if (ignoreScans.Contains(scan))
            {
                return(result);
            }

            SetPosition(scan);

            int msLevel = rawReader.GetMsLevel(scan);

            if (!DoAcceptMsLevel(msLevel))
            {
                return(result);
            }

            //Console.WriteLine("Reading scan {0}", scan);

            PeakList <Peak> pkl;

            try
            {
                pkl = rawReader.GetPeakList(scan);
            }
            catch (RawReadException ex)
            {
                ignoreScans.Add(ex.Scan);
                File.WriteAllLines(GetIgnoreScanFile(fileName), (from i in ignoreScans
                                                                 let s = i.ToString()
                                                                         select s).ToArray());
                bReadAgain = true;
                return(result);
            }

            pkl.MsLevel      = msLevel;
            pkl.Experimental = experimental;
            pkl.ScanTimes.Add(new ScanTime(scan, rawReader.ScanToRetentionTime(scan)));

            pkl.ScanMode = rawReader.GetScanMode(scan);

            PeakList <Peak> pklProcessed;

            if (msLevel > 1)
            {
                pkl.Precursor = new PrecursorPeak(rawReader.GetPrecursorPeak(scan));

                if (pkl.PrecursorCharge == 0)
                {
                    pkl.PrecursorCharge = PrecursorUtils.GuessPrecursorCharge(pkl, pkl.PrecursorMZ);
                }

                if (options.ExtractRawMS3 && pkl.MsLevel == 3)
                {
                    pklProcessed = pkl;
                }
                else
                {
                    pklProcessed = this.PeakListProcessor.Process(pkl);
                }
            }
            else
            {
                pklProcessed = pkl;
            }

            if (null != pklProcessed && pklProcessed.Count > 0)
            {
                DoWritePeakList(rawReader, pklProcessed, fileName, returnFiles);

                if (!options.MzXmlNestedScan)
                {
                    var intent = GetScanIntent(pkl.MsLevel);
                    sw.WriteLine(intent + "</scan>");
                }

                while (result < lastSpectrumNumber && rawReader.GetMsLevel(result) > msLevel)
                {
                    result = DoWritePeakList(rawReader, result, fileName, returnFiles, experimental, lastSpectrumNumber, ignoreScans, ref bReadAgain);
                    if (bReadAgain)
                    {
                        return(result);
                    }
                }

                if (options.MzXmlNestedScan)
                {
                    var intent = GetScanIntent(pkl.MsLevel);
                    sw.WriteLine(intent + "</scan>");
                }
            }

            return(result);
        }
        protected List <PeakList <Peak> > ReadTandemMassFromRaw(FileInfo rawFilename, List <int> ignoreScans)
        {
            string experimental = FileUtils.ChangeExtension(rawFilename.Name, "");

            var result = new List <PeakList <Peak> >();

            bool bReadAgain = false;

            rawReader.Open(rawFilename.FullName);
            try
            {
                int firstSpectrumNumber = rawReader.GetFirstSpectrumNumber();
                int lastSpectrumNumber  = rawReader.GetLastSpectrumNumber();

                Progress.SetRange(firstSpectrumNumber, lastSpectrumNumber);
                Progress.Begin();
                try
                {
                    for (int scan = firstSpectrumNumber; scan <= lastSpectrumNumber; scan++)
                    {
                        if (Progress.IsCancellationPending())
                        {
                            throw new UserTerminatedException();
                        }

                        if (ignoreScans.Contains(scan))
                        {
                            continue;
                        }

                        Progress.SetPosition(scan);

                        int msLevel = rawReader.GetMsLevel(scan);

                        if (msLevel > 1)
                        {
                            PeakList <Peak> pkl;
                            try
                            {
                                pkl = rawReader.GetPeakList(scan);
                            }
                            catch (RawReadException ex)
                            {
                                ignoreScans.Add(ex.Scan);
                                File.WriteAllLines(GetIgnoreScanFile(rawFilename), (from i in ignoreScans
                                                                                    let s = i.ToString()
                                                                                            select s).ToArray());
                                bReadAgain = true;
                                break;
                            }
                            pkl.Precursor    = rawReader.GetPrecursorPeakWithMasterScan(scan);
                            pkl.MsLevel      = msLevel;
                            pkl.Experimental = experimental;
                            pkl.ScanTimes.Add(new ScanTime(scan, rawReader.ScanToRetentionTime(scan)));
                            pkl.ScanMode = rawReader.GetScanMode(scan);

                            if (pkl.PrecursorCharge == 0)
                            {
                                pkl.PrecursorCharge = PrecursorUtils.GuessPrecursorCharge(pkl, pkl.PrecursorMZ);
                            }

                            PeakList <Peak> pklProcessed = this.pklProcessor.Process(pkl);
                            if (null != pklProcessed && pklProcessed.Count > 0)
                            {
                                result.Add(pklProcessed);
                            }
                        }
                    }
                }
                finally
                {
                    Progress.End();
                }
            }
            finally
            {
                rawReader.Close();
            }

            if (bReadAgain)
            {
                return(ReadTandemMassFromRaw(rawFilename, ignoreScans));
            }
            else
            {
                return(result);
            }
        }
 public string GetScanMode(int scan)
 {
     return(rawFile.GetScanMode(scan));
 }