From c8f36bb6f624cafb9738a5fcf01c4b0d7bfe1731 Mon Sep 17 00:00:00 2001
From: James Graham <J.A.Graham@soton.ac.uk>
Date: Wed, 25 May 2016 16:19:53 +0100
Subject: [PATCH] Better error messages when xtc fails

---
 pycgtool/frame.py | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/pycgtool/frame.py b/pycgtool/frame.py
index c0a9ba1..53578c3 100644
--- a/pycgtool/frame.py
+++ b/pycgtool/frame.py
@@ -5,6 +5,8 @@ The Frame class may contain multiple Residues which may each contain multiple At
 Both Frame and Residue are iterable. Residue is indexable with either atom numbers or names.
 """
 
+import os
+
 import numpy as np
 
 from simpletraj import trajectory
@@ -15,6 +17,15 @@ from .parsers.cfg import CFG
 np.seterr(all="raise")
 
 
+try:
+    raise FileNotFoundError
+except FileNotFoundError:
+    pass
+except NameError:
+    class FileNotFoundError(OSError):
+        pass
+
+
 class Atom:
     """
     Hold data for a single atom
@@ -112,9 +123,17 @@ class Frame:
             self.numframes += 1
 
             if xtc is not None:
-                self.xtc = trajectory.XtcTrajectory(xtc)
-                assert self.xtc.numatoms == self.natoms
-                self.numframes += self.xtc.numframes
+                try:
+                    self.xtc = trajectory.XtcTrajectory(xtc)
+                except OSError as e:
+                    if not os.path.isfile(xtc):
+                        raise FileNotFoundError(xtc) from e
+                    e.args = ("Error opening file '{0}'".format(xtc),)
+                    raise
+                else:
+                    if self.xtc.numatoms != self.natoms:
+                        raise AssertionError("Number of atoms does not match between gro and xtc files.")
+                    self.numframes += self.xtc.numframes
 
             if itp is not None:
                 self._parse_itp(itp)
-- 
GitLab