@@ -55,11 +55,11 @@ def __init__(
55
55
self ._nc = file
56
56
self ._nc .set_always_mask (False )
57
57
58
- if mode in { "a" , "r+" , "w" } :
58
+ if mode . startswith (( "a" , "r+" , "w" , "x" )) :
59
59
self ._restart = restart
60
60
else :
61
61
self ._restart = self ._nc .Conventions == "AMBERRESTART"
62
- self ._frame = self .get_num_frames ()
62
+ self ._frame = self .get_num_frames () if hasattr ( self . _nc , "Conventions" ) else 0
63
63
64
64
def get_dimensions (
65
65
self , frames : Union [int , list [int ], slice ] = None , units : bool = True
@@ -116,6 +116,11 @@ def get_num_frames(self) -> int:
116
116
Number of frames.
117
117
"""
118
118
119
+ if not hasattr (self ._nc , "Conventions" ):
120
+ raise RuntimeError (
121
+ "The NetCDF file is not a valid AMBER NetCDF "
122
+ "trajectory or does not contain any data."
123
+ )
119
124
return self ._nc .dimensions ["frame" ].size
120
125
121
126
def get_num_atoms (self ) -> int :
@@ -128,7 +133,12 @@ def get_num_atoms(self) -> int:
128
133
Number of atoms.
129
134
"""
130
135
131
- return self ._nc .dimensions ["atom" ].size
136
+ if not hasattr (self ._nc , "Conventions" ):
137
+ raise RuntimeError (
138
+ "The NetCDF file is not a valid AMBER NetCDF "
139
+ "trajectory or does not contain any data."
140
+ )
141
+ return self ._nc .dimensions ["atom" ].size if hasattr (self ._nc , "Conventions" ) else 0
132
142
133
143
def get_times (
134
144
self , frames : Union [int , list [int ], slice ] = None , units : bool = True
@@ -153,6 +163,11 @@ def get_times(
153
163
**Reference unit**: :math:`\\ mathrm{ps}`.
154
164
"""
155
165
166
+ if not hasattr (self ._nc , "Conventions" ):
167
+ raise RuntimeError (
168
+ "The NetCDF file is not a valid AMBER NetCDF "
169
+ "trajectory or does not contain any data."
170
+ )
156
171
times = (
157
172
self ._nc .variables ["time" ][:]
158
173
if frames is None
@@ -185,6 +200,11 @@ def get_positions(
185
200
**Reference unit**: :math:`\\ mathrm{Å}`.
186
201
"""
187
202
203
+ if not hasattr (self ._nc , "Conventions" ):
204
+ raise RuntimeError (
205
+ "The NetCDF file is not a valid AMBER NetCDF "
206
+ "trajectory or does not contain any data."
207
+ )
188
208
positions = (
189
209
self ._nc .variables ["coordinates" ][:]
190
210
if frames is None
@@ -218,6 +238,11 @@ def get_velocities(
218
238
**Reference unit**: :math:`\\ mathrm{Å/ps}`.
219
239
"""
220
240
241
+ if not hasattr (self ._nc , "Conventions" ):
242
+ raise RuntimeError (
243
+ "The NetCDF file is not a valid AMBER NetCDF "
244
+ "trajectory or does not contain any data."
245
+ )
221
246
if "velocities" not in self ._nc .variables :
222
247
wmsg = (
223
248
"The NetCDF file does not contain information about "
@@ -259,6 +284,11 @@ def get_forces(
259
284
**Reference unit**: :math:`\\ mathrm{Å/ps}`.
260
285
"""
261
286
287
+ if not hasattr (self ._nc , "Conventions" ):
288
+ raise RuntimeError (
289
+ "The NetCDF file is not a valid AMBER NetCDF "
290
+ "trajectory or does not contain any data."
291
+ )
262
292
if "forces" not in self ._nc .variables :
263
293
wmsg = (
264
294
"The NetCDF file does not contain information about "
0 commit comments