File layer¶
The file layer python module gsd.fl allows direct low level access to read and write
gsd files of any schema. The hoomd reader (gsd.hoomd) provides higher level access to
hoomd schema files, see HOOMD.
View the page source to find unformatted example code that can be easily copied.
Open a gsd file¶
In [1]: f = gsd.fl.open(name="file.gsd",
...: mode='wb',
...: application="My application",
...: schema="My Schema",
...: schema_version=[1,0])
...:
In [2]: f.close()
Warning
Opening a gsd file with a ‘w’ or ‘x’ mode overwrites any existing file with the given name.
Write data¶
In [3]: f = gsd.fl.open(name="file.gsd",
...: mode='wb',
...: application="My application",
...: schema="My Schema",
...: schema_version=[1,0]);
...:
In [4]: f.write_chunk(name='chunk1', data=numpy.array([1,2,3,4], dtype=numpy.float32))
In [5]: f.write_chunk(name='chunk2', data=numpy.array([[5,6],[7,8]], dtype=numpy.float32))
In [6]: f.end_frame()
In [7]: f.write_chunk(name='chunk1', data=numpy.array([9,10,11,12], dtype=numpy.float32))
In [8]: f.write_chunk(name='chunk2', data=numpy.array([[13,14],[15,16]], dtype=numpy.float32))
In [9]: f.end_frame()
In [10]: f.close()
Call gsd.fl.open() to access gsd files on disk.
Add any number of named data chunks to each frame in the file with
gsd.fl.GSDFile.write_chunk(). The data must be a 1 or 2
dimensional numpy array of a simple numeric type (or a data type that will automatically
convert when passed to numpy.array(data). Call gsd.fl.GSDFile.end_frame()
to end the frame and start the next one.
Note
While supported, implicit conversion to numpy arrays creates a 2nd copy of the data in memory and adds conversion overhead.
Warning
Make sure to call end_frame() before closing the file, or the last frame is lost.
Read data¶
In [11]: f = gsd.fl.open(name="file.gsd",
....: mode='rb',
....: application="My application",
....: schema="My Schema",
....: schema_version=[1,0])
....:
In [12]: f.read_chunk(frame=0, name='chunk1')
Out[12]: array([1., 2., 3., 4.], dtype=float32)
In [13]: f.read_chunk(frame=1, name='chunk2')