File: libdcd.pyx

package info (click to toggle)
mdanalysis 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,696 kB
  • sloc: python: 92,135; ansic: 8,156; makefile: 215; sh: 138
file content (748 lines) | stat: -rw-r--r-- 26,660 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the Lesser GNU Public Licence, v2.1 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
# doi: 10.25080/majora-629e541a-00e
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#

#cython: boundscheck=False, wraparound=False,
"""\
Low level DCD  trajectory reading - :mod:`MDAnalysis.lib.formats.libdcd`
------------------------------------------------------------------------

:mod:`libdcd` contains the class :class:`DCDFile` to read and write frames of a
DCD file. The class tries to behave similar to a normal file object.

:mod:`libdcd` contains the classes :class:`DCDFile`, which can be used to read
and write frames from and to DCD files. These classes are used internally by
MDAnalysis in :mod:`MDAnalysis.coordinates.DCD`. They behave similar to normal
file objects.

For example, one can use a :class:`DCDFile` to directly calculate mean
coordinates (where the coordinates are stored in `x` attribute of the
:class:`namedtuple` `frame`):

.. code-block:: python
   :emphasize-lines: 1,2,5

   with DCDFile("trajectory.dcd") as dcd:
       header = dcd.header
       mean = np.zeros((header['natoms'], 3))
       # iterate over trajectory
       for frame in dcd:
           mean += frame.x
   mean /= header['natoms']


Besides iteration one can also seek to arbitrary frames using the
:meth:`~DCDFile.seek` method. Note that instead of seeking to a byte-offset as
for normal Python streams, the seek and tell method of DCDFile operate on
complete trajectory frames.

.. rubric:: Acknowledgements

:mod:`libdcd` contains and is originally based on DCD reading and writing code
from VMD's `molfile`_ plugin and `catdcd`_.

.. _molfile: http://www.ks.uiuc.edu/Research/vmd/plugins/molfile/
.. _catdcd: http://www.ks.uiuc.edu/Development/MDTools/catdcd/

"""
from os import path
import numpy as np
from collections import namedtuple
import string
import sys
import cython 

cimport numpy as cnp
from libc.stdio cimport SEEK_SET, SEEK_CUR, SEEK_END
from libc.stdint cimport uintptr_t
from libc.stdlib cimport free

cnp.import_array()

_whence_vals = {"FIO_SEEK_SET": SEEK_SET,
                "FIO_SEEK_CUR": SEEK_CUR,
                "FIO_SEEK_END": SEEK_END}


# module level C decls
cdef  int DCD_IS_CHARMM_ = 0x01
cdef  int DCD_HAS_4DIMS_ = 0x02
cdef  int DCD_HAS_EXTRA_BLOCK_ = 0x04

# exportable versions in python
DCD_IS_CHARMM       = DCD_IS_CHARMM_
DCD_HAS_4DIMS       = DCD_HAS_4DIMS_
DCD_HAS_EXTRA_BLOCK = DCD_HAS_EXTRA_BLOCK_

cdef enum:
    FIO_READ = 0x01
    FIO_WRITE = 0x02

DCD_ERRORS = {
    0: 'Success',
    -1: 'Normal EOF',
    -2: 'DCD file does not exist',
    -3: 'Open of DCD file failed',
    -4: 'read call on DCD file failed',
    -5: 'premature EOF found in DCD file',
    -6: 'format of DCD file is wrong',
    -7: 'output file already exists',
    -8: 'malloc failed'
}

FLOAT = np.float32
DOUBLE = np.float64

DCDFrame = namedtuple('DCDFrame', 'xyz unitcell')

cdef class DCDFile:
    """DCDFile(fname, mode='r')

    File like wrapper for DCD files

    This class can be similar to the normal file objects in python. The read()
    function will return a frame and all information in it instead of a single
    line. Additionally the context-manager protocol is supported as well.

    DCDFile can read typical DCD files created by e.g., CHARMM, NAMD, or LAMMPS. It
    reads raw data from the trajectory and hence interpretation of, for instance,
    different unitcell conventions or time and length units, has to be handled in
    higher level code. Reading and writing does not support fixed atoms or 4D
    coordinates.

    Parameters
    ----------
    fname : str
        The filename to open.
    mode : ('r', 'w')
        The mode in which to open the file, either 'r' read or 'w' write

    Examples
    --------
    >>> from MDAnalysis.lib.formats.libdcd import DCDFile
    >>> with DCDFile('foo.dcd') as f:
    >>>     for frame in f:
    >>>         print(frame.x)


    Notes
    -----
    DCD is not a well defined format. One consequence of this is that different
    programs like CHARMM and NAMD are using different convention to store the
    unitcell information. The :class:`DCDFile` will read the unitcell
    information as is when available. Post processing depending on the program
    this DCD file was written with is necessary. Have a look at the MDAnalysis
    DCD reader for possible post processing into a common unitcell data
    structure. You can also find more information how different programs store
    unitcell information in DCD on the `mdawiki`_ . This class can be pickled.
    The pickle will store filename, mode, current frame

    .. _mdawiki: https://github.com/MDAnalysis/mdanalysis/wiki/FileFormats#dcd
    """

    def __cinit__(self, fname, mode='r'):
        self.fname = fname.encode('utf-8')
        self.natoms = 0
        self.is_open = False
        self.wrote_header = False
        self._buffers_setup = False
        self.open(mode)

    def __dealloc__(self):
        self.close()

    def __enter__(self):
        """Support context manager"""
        if not self.is_open:
            self.open(self.mode)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Support context manager"""
        self.close()
        # always propagate exceptions forward
        return False

    def __iter__(self):
        self.close()
        self.open(self.mode)
        return self

    def __next__(self):
        if self.reached_eof:
            raise StopIteration
        return self.read()

    def __len__(self):
        if not self.is_open:
            raise IOError('No file currently opened')
        return self.n_frames

    def __reduce__(self):
        return (self.__class__, (self.fname.decode(), self.mode),
                self.__getstate__())

    def __getstate__(self):
        return self.is_open, self.current_frame, self.n_frames

    def __setstate__(self, state):
        is_open = state[0]
        if not is_open:
            self.close()
            return

        current_frame = state[1]
        if current_frame < self.n_frames:
            self.seek(current_frame)
        elif current_frame == self.n_frames:
            #  cannot seek to self.n_frames (a.k.a. len(DCDFile));
            #  instead, we seek to the previous frame and read next. 
            #  which is the state of the file when we need to serialize
            #  at the end of the trajectory.
            self.seek(current_frame - 1)
            _ = self.read()
        else:             # pragma: no cover
            raise RuntimeError("Invalid frame number {} > {} -- this should"
                               "not happen.".format(current_frame,
                                                    self.n_frames)
                              )

    def tell(self):
        """
        Returns
        -------
        current frame (0-based)
        """
        return self.current_frame

    def open(self, mode='r'):
        """open(mode='r')

        Open a DCD file

        If another DCD file is currently opened it will be closed

        Parameters
        ----------
        mode : ('r', 'w')
            The mode in which to open the file, either 'r' read or 'w' write

        """
        cdef int ok

        if self.is_open:
            self.close()

        if mode == 'r':
            if not path.isfile(self.fname):
                raise IOError("DCD file does not exist")
            fio_mode = FIO_READ
        elif mode == 'w':
            fio_mode = FIO_WRITE
        else:
            raise IOError("unkown mode '{}', use either r or w".format(mode))
        self.mode = str(mode)

        ok = fio_open(self.fname, fio_mode, <fio_fd*> &self.fp)
        if ok != 0:
            raise IOError("couldn't open file: {}\n"
                          "ErrorCode: {}".format(self.fname, DCD_ERRORS[ok]))
        self.is_open = True
        self.current_frame = 0
        self.reached_eof = False
        self.wrote_header = False
        # Has to come last since it checks the reached_eof flag
        if self.mode == 'r':
            self._read_header()
            self._setup_buffers()

    def close(self):
        """Close the open DCD file

        """
        cdef int ok
        if self.is_open:
            # In case there are fixed atoms we should free the memory again.
            # Both pointers are guaranted to be non NULL if either one is.
            if self.freeind != NULL:
                close_dcd_read(self.freeind, self.fixedcoords);

            ok = fio_fclose(self.fp)

            self.is_open = False
            if ok != 0:
                raise IOError("couldn't close file: {}\n"
                              "ErrorCode: {}".format(self.fname, DCD_ERRORS[ok]))


    cdef void _read_header(self):
        """read header and populate internal fields"""
        if not self.is_open:
            raise IOError("No file open")

        cdef char* c_remarks
        cdef int len_remarks = 0
        cdef int nsets
        cdef int ok

        ok = read_dcdheader(self.fp, &self.natoms, &nsets, &self.istart,
                            &self.nsavc, &self.delta, &self.nfixed, &self.freeind,
                            &self.fixedcoords, &self.reverse_endian,
                            &self.charmm, &c_remarks, &len_remarks)
        if ok != 0:
            raise IOError("Reading DCD header failed: {}".format(DCD_ERRORS[ok]))

        self.is_periodic = int(bool((self.charmm & DCD_IS_CHARMM) and
                                (self.charmm & DCD_HAS_EXTRA_BLOCK)))

        if c_remarks != NULL:
            py_remarks = <bytes> c_remarks[:len_remarks]
            free(c_remarks)
        else:
            py_remarks = ""
        self.ndims = 3 if not self.charmm & DCD_HAS_4DIMS_ else 4
        # This function assumes that the dcd header was already read and
        # self.ndims is set. It will only work when called here !!!
        self.n_frames = self._estimate_n_frames()
        self.b_read_header = True

        # make sure fixed atoms have been read
        try:
            self.read()
            self.seek(0)
        except IOError:
            if self.n_frames != 0:
                raise IOError("DCD is corrupted")


        if isinstance(py_remarks, bytes):
            py_remarks = py_remarks.decode('ascii', 'ignore')

        py_remarks = "".join(s for s in py_remarks if s in string.printable)

        self.remarks = py_remarks

    cdef void _setup_buffers(self):
        cdef cnp.npy_intp[2] dims 
        dims[0] = self.natoms
        dims[1] = self.ndims
        # note use of fortran flag (1)
        self._coordinate_buffer = cnp.PyArray_EMPTY(2, dims, cnp.NPY_FLOAT32, 1)
        cdef cnp.npy_intp[1] unitcell_dims
        unitcell_dims[0] = 6
        self._unitcell_buffer = cnp.PyArray_EMPTY(1, unitcell_dims, cnp.NPY_FLOAT64, 0)

        # fortran contiguity
        self.xview = self._coordinate_buffer[:, 0]
        self.yview = self._coordinate_buffer[:, 1]
        self.zview = self._coordinate_buffer[:, 2]
        self.unitcellview = self._unitcell_buffer[:]
        self._buffers_setup = True

    cdef int _estimate_n_frames(self):
        """ Only call this function in _read_header!!!
        """
        extrablocksize = 48 + 8 if self.charmm & DCD_HAS_EXTRA_BLOCK else 0
        self._firstframesize = (self.natoms + 2) * self.ndims * sizeof(float) + extrablocksize
        self._framesize = ((self.natoms - self.nfixed + 2) * self.ndims * sizeof(float) +
                          extrablocksize)
        filesize = path.getsize(self.fname)
        # It's safe to use ftell, even though ftell returns a long, because the
        # header size is < 4GB.
        self._header_size = fio_ftell(self.fp)
        nframessize = filesize - self._header_size - self._firstframesize
        return nframessize / self._framesize + 1

    def seek(self, int frame):
        """seek(frame)

        Seek to Frame.

        Parameters
        ----------
        frame : int
            seek the file to given frame (0-based)

        """
        if frame >= self.n_frames:
            raise EOFError('Trying to seek over max number of frames')
        self.reached_eof = False

        cdef long long offset
        if frame == 0:
            offset = self._header_size
        else:
            offset = self._header_size
            offset += self._firstframesize
            offset += self._framesize * (<long long>frame - 1)

        cdef int ok = fio_fseek(self.fp, offset, _whence_vals['FIO_SEEK_SET'])
        if ok != 0:
            raise IOError("DCD seek failed with DCD error={}".format(DCD_ERRORS[ok]))
        self.current_frame = frame

    @property
    def header(self):
        """
        Returns
        -------
        dict of header values needed to write new dcd.
        natoms: number of atoms
        istart: starting frame number
        nsavc: number of frames between saves
        delta: integrator time step.
        charm: bitfield integer if file contains special CHARMM information
        remarks: remark string, max 240 bytes.


        """
        return {'natoms': self.natoms,
                'istart': self.istart,
                'nsavc': self.nsavc,
                'delta': self.delta,
                'is_periodic': self.is_periodic,
                'remarks': self.remarks}

    @property
    def charmm_bitfield(self):
        """This DCDFile reader can process files written by different MD simulation
        programs. For files produced by CHARMM or other programs that follow
        the same convention we are reading a special CHARMM bitfield that
        stores different flags about additional information that is stored in
        the dcd. The bit flags are:

        .. code::

            DCD_IS_CHARMM       = 0x01
            DCD_HAS_4DIMS       = 0x02
            DCD_HAS_EXTRA_BLOCK = 0x04

        Here `DCD_HAS_EXTRA_BLOCK` means that unitcell information is stored.

        """
        return self.charmm

    def write_header(self,  remarks,  int natoms, int istart, int nsavc, float delta, int is_periodic):
        """write_header(remarks, natoms, istart, nsavc, delta, is_periodic)
        Write DCD header

        This function needs to be called before the first frame can be written.

        Parameters
        ----------
        remarks : str
            remarks of DCD file. Writes up to 239 characters (ASCII). The
            character 240 will be the null terminator
        natoms : int
            number of atoms to write
        istart : int
            starting frame number
        nsavc : int
            number of frames between saves
        delta : float
            integrator time step. The time for 1 frame is nsavc * delta
        is_periodic : bool
            write unitcell information. Also pretends that file was written by CHARMM 24

        """
        if not self.is_open:
            raise IOError("No file open")
        if not self.mode=='w':
            raise IOError("Incorrect file mode for writing.")
        if self.wrote_header:
            raise IOError("Header already written")

        cdef int with_unitcell = is_periodic
        if is_periodic:
            self.charmm = DCD_HAS_EXTRA_BLOCK_ | DCD_IS_CHARMM_
        self.natoms = natoms

        if isinstance(remarks, str):
            try:
                remarks = bytearray(remarks, 'ascii')
            except UnicodeDecodeError:
                remarks = bytearray(remarks)

        cdef int ok = write_dcdheader(self.fp, remarks, self.natoms, istart,
                             nsavc, delta, with_unitcell,
                             self.charmm)
        if ok != 0:
            raise IOError("Writing DCD header failed: {}".format(DCD_ERRORS[ok]))
        self.wrote_header = True

    def write(self, xyz,  box=None):
        """write(xyz, box=None)
        write one frame into DCD file.

        Parameters
        ----------
        xyz : array_like, shape=(natoms, 3)
            cartesian coordinates
        box : array_like, shape=(6) (optional)
            Box vectors for this frame. Can be left to skip writing a unitcell

        """
        if not self.is_open:
            raise IOError("No file open")
        if self.mode != 'w':
            raise IOError('File opened in mode: {}. Writing only allowed '
                          'in mode "w"'.format('self.mode'))
        if (self.charmm & DCD_HAS_EXTRA_BLOCK_):
            if len(box) != 6:
                raise ValueError("box size is wrong should be 6, got: {}".format(box.size))
        else:
            # use a dummy box. It won't be written anyway in readdcd.
            box = np.zeros(6)

        if not self.wrote_header:
            raise IOError("write header first before frames can be written")
        cdef cnp.ndarray xyz_ = np.asarray(xyz, order='F', dtype=FLOAT)
        cdef cnp.npy_intp[2] shape = cnp.PyArray_DIMS(xyz_)
        if (shape[0] != self.natoms) or (shape[1] != 3):
            raise ValueError("xyz shape is wrong should be (natoms, 3), got:".format(xyz.shape))
        cdef double[::1] c_box = np.asarray(box, order='C', dtype=DOUBLE)
        # fortran contiguity
        cdef float[::1] x = xyz_[:, 0]
        cdef float[::1] y = xyz_[:, 1]
        cdef float[::1] z = xyz_[:, 2]

        step = self.istart + self.current_frame * self.nsavc
        ok = write_dcdstep(self.fp, self.current_frame + 1, step,
                           self.natoms, <float*> &x[0],
                           <float*> &y[0], <float*> &z[0],
                           <double*> &c_box[0], self.charmm)
        if ok != 0:
            raise IOError("Couldn't write DCD frame: reason {}".format(DCD_ERRORS[ok]))

        self.current_frame += 1

    def read(self):
        """
        Read next dcd frame

        Returns
        -------
        DCDFrame : namedtuple
            positions are in ``x`` and unitcell in ``unitcell`` attribute of DCDFrame

        Notes
        -----
        unitcell is read as is from DCD. Post processing depending on the program this
        DCD file was written with is necessary. Have a look at the MDAnalysis DCD reader
        for possible post processing into a common unitcell data structure.

        """
        if self.reached_eof:
            raise EOFError('Reached last frame in DCD, seek to 0')
        if not self.is_open:
            raise IOError("No file open")
        if self.mode != 'r':
            raise IOError('File opened in mode: {}. Reading only allow '
                               'in mode "r"'.format('self.mode'))
        if self.n_frames == 0:
            raise IOError("opened empty file. No frames are saved")

        if not self._buffers_setup:
            self._setup_buffers()

        self.unitcellview[0] = self.unitcellview[2] = self.unitcellview[5] = 0.0;
        self.unitcellview[4] = self.unitcellview[3] = self.unitcellview[1] = 90.0;

        cdef int first_frame = self.current_frame == 0
        cdef int ok = self.c_readframes_helper(self.xview, self.yview, self.zview, self.unitcellview, first_frame)
        if ok != 0 and ok != -4:
            raise IOError("Reading DCD header failed: {}".format(DCD_ERRORS[ok]))

        # we couldn't read any more frames.
        if ok == -4:
            self.reached_eof = True
            raise StopIteration

        self.current_frame += 1
        return DCDFrame(self._coordinate_buffer, self._unitcell_buffer)

    def readframes(self, start=None, stop=None, step=None, order='fac', indices=None):
        """readframes(start=None, stop=None, step=None, order='fac', indices=None)
        read multiple frames at once

        Parameters
        ----------
        start : int (optional)
            starting frame, default to 0
        stop : int (optional)
            stop frame, default to ``n_frames``
        step : int (optional)
            step between frames read, defaults to 1
        order : str (optional)
            give order of returned array with `f`:frames, `a`:atoms, `c`:coordinates
        indices : array_like (optional)
            only read selected atoms. In ``None`` read all.

        Returns
        -------
        DCDFrame : namedtuple
            positions are in ``x`` and unitcell in ``unitcell`` attribute of DCDFrame.
            Here the attributes contain the positions for all frames in the given order

        Notes
        -----
        unitcell is read as it from DCD. Post processing depending the program
        this DCD file was written with is necessary. Have a look at the
        MDAnalysis DCD reader for possible post processing into a common
        unitcell data structure.

        """
        if self.reached_eof:
            raise EOFError('Reached last frame in DCD, seek to 0')
        if not self.is_open:
            raise IOError("No file open")
        if self.mode != 'r':
            raise IOError('File opened in mode: {}. Reading only allow '
                               'in mode "r"'.format('self.mode'))
        if self.n_frames == 0:
            raise IOError("opened empty file. No frames are saved")

        self.seek(0)
        # if we only want to iterate backwards flip start and end
        if start is None and stop is None and step is not None and step < 0:
            stop = -1
            start = self.n_frames - 1
        stop = stop if not stop is None else self.n_frames
        start = start if not start is None else 0
        step = step if not step is None else 1

        # c delcs
        cdef int stop_ = stop
        cdef int start_ = start
        cdef int step_ = step 

        cdef int n
        n = len(range(start, stop, step))
        cdef int natoms
        cdef cnp.ndarray[cnp.int64_t, ndim=1] c_indices
        if indices is None:
            c_indices = cnp.PyArray_Arange(0, self.natoms, 1, cnp.NPY_INT64)
            natoms = self.natoms
        else:
            natoms = len(indices)
            c_indices = np.asarray(indices, dtype=np.int64)
        
        cdef cnp.npy_intp[3] dims
        cdef int hash_order = -1
        if order == 'fac':
            dims[0] = n
            dims[1] = natoms
            dims[2] = self.ndims
            hash_order = 1
        elif order == 'fca':
            dims[0] = n
            dims[1] = self.ndims
            dims[2] = natoms
            hash_order = 2
        elif order == 'afc':
            dims[0] = natoms
            dims[1] = n
            dims[2] = self.ndims
            hash_order = 3
        elif order == 'acf':
            dims[0] = natoms
            dims[1] = self.ndims
            dims[2] = n
            hash_order = 4
        elif order == 'caf':
            dims[0] = self.ndims
            dims[1] = natoms
            dims[2] = n
            hash_order = 5
        elif order == 'cfa':
            dims[0] = self.ndims
            dims[1] = n
            dims[2] = natoms
            hash_order = 6
        else:
            raise ValueError("unkown order '{}'".format(order))

        cdef cnp.ndarray[float, ndim=3] xyz = cnp.PyArray_EMPTY(3, dims, cnp.NPY_FLOAT32, 0)
        cdef cnp.npy_intp[2] unitcell_dims
        unitcell_dims[0] = n
        unitcell_dims[1] = 6
        cdef cnp.ndarray[double, ndim=2] box = cnp.PyArray_EMPTY(2, unitcell_dims, cnp.NPY_FLOAT64, 0)

        cdef cnp.npy_intp[2] xyz_tmp_dims
        xyz_tmp_dims[0] = self.natoms
        xyz_tmp_dims[1] = self.ndims
        # note fortran flag (1)
        cdef cnp.ndarray[float, ndim=2] xyz_tmp = cnp.PyArray_EMPTY(2, xyz_tmp_dims, cnp.NPY_FLOAT32, 1)

        # memoryviews for slices into xyz_temp, note fortran contiguous
        cdef float[::1] x = xyz_tmp[:, 0]
        cdef float[::1] y = xyz_tmp[:, 1]
        cdef float[::1] z = xyz_tmp[:, 2]

        cdef int ok, i, counter 
        
        if (start_ == 0) and (step_ == 1) and (stop_ == self.n_frames):
            for i in range(n):
                ok = self.c_readframes_helper(x, y, z, box[i], i==0)
                if ok != 0 and ok != -4:
                    raise IOError("Reading DCD frames failed: {}".format(DCD_ERRORS[ok]))
                copy_in_order(xyz_tmp[c_indices], xyz, hash_order, i)
        else:
            counter = 0
            for i in range(start, stop, step):
                self.seek(i)
                ok = self.c_readframes_helper(x, y, z, box[counter], i==0)
                if ok != 0 and ok != -4:
                    raise IOError("Reading DCD frames failed: {}".format(DCD_ERRORS[ok]))
                copy_in_order(xyz_tmp[c_indices], xyz, hash_order, counter)
                counter += 1

        return DCDFrame(xyz, box)

    # Helper to read current DCD frame
    cdef int c_readframes_helper(self, float[::1] x,
                                 float[::1] y, float[::1] z,
                                 double[::1] unitcell, int first_frame):
        cdef int ok
        ok = read_dcdstep(self.fp, self.natoms,
                          <float*> &x[0],
                          <float*> &y[0], <float*> &z[0],
                          <double*> &unitcell[0], self.nfixed, first_frame,
                          self.freeind, self.fixedcoords,
                          self.reverse_endian, self.charmm)
        return ok


# Helper in readframes to copy given a specific memory layout
cdef void copy_in_order(float[:, :] source, float[:, :, :] target, int order, int index):
    if order == 1:  #  'fac':
        target[index] = source
    elif order == 2:  #  'fca':
        target[index] = source.T
    elif order == 3:  # 'afc':
        target[:, index] = source
    elif order == 4:  # 'acf':
        target[:, :, index] = source
    elif order == 5:  # 'caf':
        target[:, :, index] = source.T
    elif order == 6:  # 'cfa':
        target[:, index] = source.T