<div><br></div>As far as I know there is no field at the beginning of the file that would give a parser hints how many frames are in it.<div>(probably because that makes it easier/more performant to append to the file while reducing the risk of corrupting it in case a write goes bad)</div>
<div><br></div><div>On the other hand that makes it hard to implement random-access to frames in XTC/TRR files.</div><div><br></div><div>Interestingly there is just a discussion on the mailing list of MDAnalysis (a python framework that can deal with XTC and other trajectories) on how libxdr might be extended to generate a checksum-protected index for XTC files, so that a given trajectory has to be read only once from beginning to end. </div>
<div><a href="https://groups.google.com/group/mdnalysis-discussion/browse_thread/thread/3cae3634c726f1ad">https://groups.google.com/group/mdnalysis-discussion/browse_thread/thread/3cae3634c726f1ad</a></div><div><br><br>a different Oliver<br>
<br><div class="gmail_quote">On Mon, Jun 4, 2012 at 3:24 PM, Paolo Franz <span dir="ltr">&lt;<a href="mailto:paolo.franz@gmail.com" target="_blank">paolo.franz@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I am trying to avoid doing it by brute force, that is reading all frames until the last is found. In the origin, what I really need to do is to test if a frame exists in the trajectory. I tried with <span>xtc_seek_frame, but that does not work. Of course, if I know how many frames are they the test becomes trivial. </span><div>

<span><br></span></div><div><span>That said, I definitely know what is in the trajectory, how many frames are there: I ran the md myself and I have the output file! What I want to do is to write a code that figure out by itself what to expect and if, by any chance I forget what is inside, it does not go into an infinite loop if I ask to analyse the wrong frame. </span></div>

<div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">Cheers</font></div><div><span class="HOEnZb"><font color="#888888"><font color="#222222" face="arial, sans-serif">Paolo<br>
</font></font></span><div><div class="h5"><br><div class="gmail_quote">
On 4 June 2012 22:59, Justin A. Lemkul <span dir="ltr">&lt;<a href="mailto:jalemkul@vt.edu" target="_blank">jalemkul@vt.edu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<br>
If all you need is the number of frames contained in an .xtc file, is there some reason why running gmxcheck on the .xtc file is insufficient?<br>
<br>
-Justin<div><br>
<br>
On 6/4/12 4:56 PM, Paolo Franz wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
Hi Tsjerk,<br>
Thanks, but I don&#39;t really want to use a python script, I am doing this from<br>
some c/c++ code. I think I figured out a way to do it, but I haven&#39;t tested it yet:<br>
<br>
i)    open the file<br>
ii)   do a read_first_xtc<br>
iii)  then get the file pointer positon from ftellg, which should be the length<br>
of the frame in bytes;<br>
iv)  place the file pointer at the end of the file with an fseek, then get the<br>
length with an ftellg<br>
v)   Divide the total length by the length of a frame and obtain the number of<br>
written frames.<br>
<br>
I am only wondering what to do when the length in bytes of the file is too large<br>
for a long int!<br>
<br>
On 4 June 2012 16:11, Tsjerk Wassenaar &lt;<a href="mailto:tsjerkw@gmail.com" target="_blank">tsjerkw@gmail.com</a><br></div><div>
&lt;mailto:<a href="mailto:tsjerkw@gmail.com" target="_blank">tsjerkw@gmail.com</a>&gt;&gt; wrote:<br>
<br>
    Hey Paolo,<br>
<br>
    I think I posted a script for extracting a last frame before, but if I<br>
    can&#39;t even find it myself... Here it is:<br>
<br>
    #!/usr/bin/env python<br>
<br>
    from struct import unpack<br>
    import sys<br>
<br>
    def i(x): return sum([ord(x[j])&lt;&lt;(24-j*8) for j in range(4)])<br>
<br>
    f = open(sys.argv[1])<br>
    tag = f.read(8)                   # Tag: magic number and number of atoms<br>
    n = 92 + i(f.read(84)[-4:])       # Size of frame in bytes<br>
<br>
    f.seek(-5*n/4, 2)                 # This should contain a complete frame<br>
    frame = f.read()                  # Read the remaining part in<br>
    frame = frame[frame.index(tag):]  # Find the tag<br>
<br>
    # Open the output file<br>
    if len(sys.argv) &gt; 2:<br>
        o = sys.argv[2]<br>
    else:<br>
        o = sys.argv[1][:-4]+&quot;-last.xtc&quot;<br>
    open(o,&quot;w&quot;).write(frame)<br>
<br>
    ###<br>
<br>
    Hope it helps. Cheers,<br>
<br>
    Tsjerk<br>
    On Mon, Jun 4, 2012 at 12:59 PM, Paolo Franz &lt;<a href="mailto:paolo.franz@gmail.com" target="_blank">paolo.franz@gmail.com</a><br></div><div>
    &lt;mailto:<a href="mailto:paolo.franz@gmail.com" target="_blank">paolo.franz@gmail.com</a>&gt;<u></u>&gt; wrote:<br>
     &gt; Hello everybody!<br>
     &gt;<br>
     &gt; I am wondering how I can figure out the number of frames contained in an<br>
     &gt; .xtc file. Indeed, I need to read a particular frame of a trajectory and I<br>
     &gt; thought that the function<br>
     &gt; xtc_seek_frame(FILE * , int *, int *)<br>
     &gt; would return 0 if the frame was there and 1 when it was not. Instead, if I<br>
     &gt; call it with a frame outside the boundaries it seems to go into an infinite<br>
     &gt; loop. What I am doing wrong? Is there a way to read the last frame of an<br>
     &gt; .xtc file?<br>
     &gt;<br>
     &gt; Sincerely<br>
     &gt; Paolo<br>
     &gt;<br></div></blockquote></blockquote></div></div></div></div></blockquote></div>
</div>