[gmx-developers] How to get the number of frames contained by an .xtc trajectory file??

Tsjerk Wassenaar tsjerkw at gmail.com
Mon Jun 4 16:11:36 CEST 2012

Hey Paolo,

I think I posted a script for extracting a last frame before, but if I
can't even find it myself... Here it is:

#!/usr/bin/env python

from struct import unpack
import sys

def i(x): return sum([ord(x[j])<<(24-j*8) for j in range(4)])

f = open(sys.argv[1])
tag = f.read(8)                   # Tag: magic number and number of atoms
n = 92 + i(f.read(84)[-4:])       # Size of frame in bytes

f.seek(-5*n/4, 2)                 # This should contain a complete frame
frame = f.read()                  # Read the remaining part in
frame = frame[frame.index(tag):]  # Find the tag

# Open the output file
if len(sys.argv) > 2:
    o = sys.argv[2]
    o = sys.argv[1][:-4]+"-last.xtc"


Hope it helps. Cheers,

On Mon, Jun 4, 2012 at 12:59 PM, Paolo Franz <paolo.franz at gmail.com> wrote:
> Hello everybody!
> I am wondering how I can figure out the number of frames contained in an
> .xtc file. Indeed, I need to read a particular frame of a trajectory and I
> thought that the function
> xtc_seek_frame(FILE * , int *, int *)
> would return 0 if the frame was there and 1 when it was not. Instead, if I
> call it with a frame outside the boundaries it seems to go into an infinite
> loop. What I am doing wrong? Is there a way to read the last frame of an
> .xtc file?
> Sincerely
> Paolo
> --
> gmx-developers mailing list
> gmx-developers at gromacs.org
> http://lists.gromacs.org/mailman/listinfo/gmx-developers
> Please don't post (un)subscribe requests to the list. Use the
> www interface or send it to gmx-developers-request at gromacs.org.

Tsjerk A. Wassenaar, Ph.D.

post-doctoral researcher
Molecular Dynamics Group
* Groningen Institute for Biomolecular Research and Biotechnology
* Zernike Institute for Advanced Materials
University of Groningen
The Netherlands

More information about the gromacs.org_gmx-developers mailing list