private int loadTrajectoryAtomCount(string trajectoryFile)
        {
            int count = 0;

            try {
                if (trajectoryFile.EndsWith(".xtc"))
                {
                    count = XTCTrajectoryParser.GetAtomCount(trajectoryFile);
                }
                else if (trajectoryFile.EndsWith(".dcd"))
                {
                    count = DCDTrajectoryParser.GetAtomCount(trajectoryFile);
                }
                else if (trajectoryFile.EndsWith(".gro"))
                {
                    count = GROTrajectoryParser.GetAtomCount(trajectoryFile);
                }
            }
            catch (FileParseException ex) {
                MoleculeEvents.RaiseShowMessage("Error Loading Trajectory File: " + ex.Message, true);
            }

            return(count);
        }
        // Loads a molecule trajectory. Will only work if molecule is already loaded
        public IEnumerator LoadMoleculeTrajectory(int moleculeID, string filePath)
        {
            if (!molecules.ContainsKey(moleculeID))
            {
                MoleculeEvents.RaiseShowMessage("Can't load molecule trajectory. No molecule found.", true);
                yield break;
            }

            if (loadingTrajectory)
            {
                MoleculeEvents.RaiseShowMessage("Can't Load Trajectory: another trajectory currently loading", true);
                yield break;
            }

            PrimaryStructure primaryStructure = molecules[moleculeID].PrimaryStructure;
            int atomCount = loadTrajectoryAtomCount(filePath);

            if (atomCount != primaryStructure.AtomCount())
            {
                MoleculeEvents.RaiseShowMessage("Trajectory atom count [" + atomCount + " doesn't match loaded structure atom count [" + primaryStructure.AtomCount() + "]", true);
                yield break;
            }

            MoleculeEvents.RaiseShowMessage("Loading trajectory. Please wait", false);

            loadingTrajectory = true;

            PrimaryStructureTrajectory trajectory = null;
            string loadException = null;

            Thread thread = new Thread(() => {
                try {
                    int startFrame     = 0;
                    int frameFrequency = 1;
                    int frameCount     = Settings.MaxTrajectoryFrames;

                    if (filePath.EndsWith(".xtc"))
                    {
                        trajectory = XTCTrajectoryParser.GetTrajectory(filePath, startFrame, frameCount, frameFrequency);
                    }
                    else if (filePath.EndsWith(".dcd"))
                    {
                        trajectory = DCDTrajectoryParser.GetTrajectory(filePath, startFrame, frameCount, frameFrequency);
                    }
                    else if (filePath.EndsWith(".gro"))
                    {
                        trajectory = GROTrajectoryParser.GetTrajectory(filePath, startFrame, frameCount, frameFrequency);
                    }
                }
                catch (FileParseException ex) {
                    loadException = ex.Message;
                }
            });

            thread.Start();

            while (thread.IsAlive)
            {
                yield return(null);
            }

            if (loadException != null)
            {
                MoleculeEvents.RaiseShowMessage("Error Loading Trajectory File: " + loadException, true);
                loadingTrajectory = false;
                yield break;
            }

            if (trajectory != null)
            {
                molecules[moleculeID].SetTrajectory(trajectory);
                MoleculeEvents.RaiseTrajectoryLoaded(moleculeID, filePath, trajectory.FrameCount());
            }

            loadingTrajectory = false;
        }