def id_format(filename): """ Identifies the file type as an Amber NetCDF trajectory file Parameters ---------- filename : str Name of the file to check format for Returns ------- is_fmt : bool True if it is an Amber NetCDF trajectory file. False otherwise Notes ----- Remote NetCDF files cannot be loaded """ if filename.startswith('http://') or filename.startswith('https://')\ or filename.startswith('ftp://'): return False try: f = NetCDFFile(filename, 'r', mmap=False) except (TypeError, OSError): return False try: try: if f.Conventions.decode() != 'AMBER': return False except AttributeError: return False # Passed all our tests return True finally: f.close()
def __init__(self, fname, mode='r'): """ Opens a NetCDF File. The main constructor should never be called directly. The alternative "open_old" and "open_new" constructors should be called instead for reading existing or writing new NetCDF files, respectively. """ self.closed = False self._ncfile = NetCDFFile(fname, mode, mmap=False)
def __init__(self, fname, mode='r'): """ Opens a NetCDF File """ self.closed = False if mode.startswith('w') and nc is not None: self._ncfile = nc.Dataset(fname, mode, format='NETCDF3_64BIT') else: if mode.startswith('w'): warnings.warn('Could not find netCDF4 module. Falling back on ' 'scipy implementation, which can significantly ' 'slow down simulations if used as a reporter') self._ncfile = NetCDFFile(fname, mode, mmap=False)
def __init__(self, fname, mode='r'): """ Opens a NetCDF File. The main constructor should never be called directly. The alternative "open_old" and "open_new" constructors should be called instead for reading existing or writing new NetCDF files, respectively. """ self.closed = False if mode.startswith('w') and nc is not None: self._ncfile = nc.Dataset(fname, mode, format='NETCDF3_64BIT') else: if mode.startswith('w'): warnings.warn('Could not find netCDF4 module. Falling back on ' 'scipy implementation, which can significantly ' 'slow down simulations if used as a reporter') self._ncfile = NetCDFFile(fname, mode, mmap=False)
def testNetCDF(self): """ Test scipy NetCDF parsing """ traj = NetCDFTraj.open_old(get_fn('tz2.truncoct.nc')) self._check_traj(traj) rst = NetCDFRestart.open_old(get_fn('ncinpcrd.rst7')) self._check_rst(rst) # Now try writing files ntraj = NetCDFTraj.open_new(get_fn('test.nc', written=True), traj.atom, box=True) for crd in traj.coordinates: ntraj.add_coordinates(crd) for box in traj.box: ntraj.add_box(box) ntraj.close() traj = NetCDFTraj.open_old(get_fn('test.nc', written=True)) self._check_traj(traj, written=True) nrst = NetCDFRestart.open_new(get_fn('test.ncrst', written=True), rst.atom, rst.hasbox, rst.hasvels, title=rst.title) nrst.coordinates = rst.coordinates nrst.velocities = rst.velocities nrst.time = rst.time nrst.box = rst.box nrst.close() rst = NetCDFRestart.open_old(get_fn('test.ncrst', written=True)) self._check_rst(rst, written=True) # Now write NetCDF trajectory without any optional attributes/variables # and check proper reading natom = randint(100, 1000) nframe = randint(5, 20) coords = np.random.rand(nframe, natom, 3) * 20 - 10 coords = np.array(coords, dtype='f') nctraj = NetCDFFile(get_fn('test2.nc', written=True), 'w', mmap=False) nctraj.Conventions = 'AMBER' nctraj.ConventionVersion = '1.0' nctraj.program = 'ParmEd' nctraj.programVersion = __version__ nctraj.createDimension('frame', None) nctraj.createDimension('spatial', 3) nctraj.createDimension('atom', natom) v = nctraj.createVariable('spatial', 'c', ('spatial', )) v[:] = np.asarray(list('xyz')) v = nctraj.createVariable('coordinates', 'f', ('frame', 'atom', 'spatial')) v[:] = coords del v nctraj.close() traj2 = NetCDFTraj.open_old(get_fn('test2.nc', written=True)) np.testing.assert_allclose(traj2.coordinates, coords) # Now write NetCDF restart without any optional attributes/variables and # check proper reading natom = randint(100, 1000) nframe = randint(5, 20) coords = np.random.rand(natom, 3) * 20 - 10 nctraj = NetCDFFile(get_fn('test2.ncrst', written=True), 'w', mmap=False) nctraj.Conventions = 'AMBERRESTART' nctraj.ConventionVersion = '1.0' nctraj.program = 'ParmEd' nctraj.programVersion = __version__ nctraj.createDimension('frame', None) nctraj.createDimension('spatial', 3) nctraj.createDimension('atom', natom) v = nctraj.createVariable('spatial', 'c', ('spatial', )) v[:] = np.asarray(list('xyz')) v = nctraj.createVariable('coordinates', 'd', ('atom', 'spatial')) v[:] = coords del v nctraj.close() traj2 = NetCDFRestart.open_old(get_fn('test2.ncrst', written=True)) np.testing.assert_allclose(traj2.coordinates[0], coords)
def testNetCDF(self): """ Test scipy NetCDF parsing """ traj = NetCDFTraj.open_old(get_fn('tz2.truncoct.nc')) self._check_traj(traj) rst = NetCDFRestart.open_old(get_fn('ncinpcrd.rst7')) self._check_rst(rst) # Now try writing files ntraj = NetCDFTraj.open_new(get_fn('test.nc', written=True), traj.atom, box=True) for crd in traj.coordinates: ntraj.add_coordinates(crd) for box in traj.box: ntraj.add_box(box) ntraj.close() traj = NetCDFTraj.open_old(get_fn('test.nc', written=True)) self._check_traj(traj, written=True) nrst = NetCDFRestart.open_new(get_fn('test.ncrst', written=True), rst.atom, rst.hasbox, rst.hasvels, title=rst.title) nrst.coordinates = rst.coordinates nrst.velocities = rst.velocities nrst.time = rst.time nrst.box = rst.box nrst.close() rst = NetCDFRestart.open_old(get_fn('test.ncrst', written=True)) self._check_rst(rst, written=True) # Now write NetCDF trajectory without any optional attributes/variables # and check proper reading natom = randint(100, 1000) nframe = randint(5, 20) coords = np.random.rand(nframe, natom, 3) * 20 - 10 coords = np.array(coords, dtype='f') nctraj = NetCDFFile(get_fn('test2.nc', written=True), 'w', mmap=False) nctraj.Conventions = 'AMBER' nctraj.ConventionVersion = '1.0' nctraj.program = 'ParmEd' nctraj.programVersion = __version__ nctraj.createDimension('frame', None) nctraj.createDimension('spatial', 3) nctraj.createDimension('atom', natom) v = nctraj.createVariable('spatial', 'c', ('spatial',)) v[:] = np.asarray(list('xyz')) v = nctraj.createVariable('coordinates', 'f', ('frame', 'atom', 'spatial')) v[:] = coords del v nctraj.close() traj2 = NetCDFTraj.open_old(get_fn('test2.nc', written=True)) np.testing.assert_allclose(traj2.coordinates, coords) # Now write NetCDF restart without any optional attributes/variables and # check proper reading natom = randint(100, 1000) nframe = randint(5, 20) coords = np.random.rand(natom, 3) * 20 - 10 nctraj = NetCDFFile(get_fn('test2.ncrst', written=True), 'w', mmap=False) nctraj.Conventions = 'AMBERRESTART' nctraj.ConventionVersion = '1.0' nctraj.program = 'ParmEd' nctraj.programVersion = __version__ nctraj.createDimension('frame', None) nctraj.createDimension('spatial', 3) nctraj.createDimension('atom', natom) v = nctraj.createVariable('spatial', 'c', ('spatial',)) v[:] = np.asarray(list('xyz')) v = nctraj.createVariable('coordinates', 'd', ('atom', 'spatial')) v[:] = coords del v nctraj.close() traj2 = NetCDFRestart.open_old(get_fn('test2.ncrst', written=True)) np.testing.assert_allclose(traj2.coordinates[0], coords)
def __init__(self, fname, mode='r'): """ Opens a NetCDF File """ self.closed = False self._ncfile = NetCDFFile(fname, mode, mmap=False)