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 style>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 style><br></span></div><div><span style>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><font color="#222222" face="arial, sans-serif">Paolo<br></font><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 class="im"><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 class="im">
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 class="im">
&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 class="im">
    &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>
     &gt;<br>
     &gt;<br>
     &gt;<br>
     &gt; --<br>
     &gt; gmx-developers mailing list<br></div>
     &gt; <a href="mailto:gmx-developers@gromacs.org" target="_blank">gmx-developers@gromacs.org</a> &lt;mailto:<a href="mailto:gmx-developers@gromacs.org" target="_blank">gmx-developers@<u></u>gromacs.org</a>&gt;<div class="im">
<br>
     &gt; <a href="http://lists.gromacs.org/mailman/listinfo/gmx-developers" target="_blank">http://lists.gromacs.org/<u></u>mailman/listinfo/gmx-<u></u>developers</a><br>
     &gt; Please don&#39;t post (un)subscribe requests to the list. Use the<br>
     &gt; www interface or send it to <a href="mailto:gmx-developers-request@gromacs.org" target="_blank">gmx-developers-request@<u></u>gromacs.org</a><br></div>
    &lt;mailto:<a href="mailto:gmx-developers-request@gromacs.org" target="_blank">gmx-developers-<u></u>request@gromacs.org</a>&gt;.<div class="im"><br>
<br>
<br>
<br>
    --<br>
    Tsjerk A. Wassenaar, Ph.D.<br>
<br>
    post-doctoral researcher<br>
    Molecular Dynamics Group<br>
    * Groningen Institute for Biomolecular Research and Biotechnology<br>
    * Zernike Institute for Advanced Materials<br>
    University of Groningen<br>
    The Netherlands<br>
    --<br>
    gmx-developers mailing list<br></div>
    <a href="mailto:gmx-developers@gromacs.org" target="_blank">gmx-developers@gromacs.org</a> &lt;mailto:<a href="mailto:gmx-developers@gromacs.org" target="_blank">gmx-developers@<u></u>gromacs.org</a>&gt;<div class="im">
<br>
    <a href="http://lists.gromacs.org/mailman/listinfo/gmx-developers" target="_blank">http://lists.gromacs.org/<u></u>mailman/listinfo/gmx-<u></u>developers</a><br>
    Please don&#39;t post (un)subscribe requests to the list. Use the<br>
    www interface or send it to <a href="mailto:gmx-developers-request@gromacs.org" target="_blank">gmx-developers-request@<u></u>gromacs.org</a><br></div>
    &lt;mailto:<a href="mailto:gmx-developers-request@gromacs.org" target="_blank">gmx-developers-<u></u>request@gromacs.org</a>&gt;.<br>
<br>
<br>
<br>
<br><span class="HOEnZb"><font color="#888888">
</font></span></blockquote><span class="HOEnZb"><font color="#888888">
<br>
-- <br>
==============================<u></u>==========<br>
<br>
Justin A. Lemkul, Ph.D.<br>
Research Scientist<br>
Department of Biochemistry<br>
Virginia Tech<br>
Blacksburg, VA<br>
jalemkul[at]<a href="http://vt.edu" target="_blank">vt.edu</a> | <a href="tel:%28540%29%20231-9080" value="+15402319080" target="_blank">(540) 231-9080</a><br>
<a href="http://www.bevanlab.biochem.vt.edu/Pages/Personal/justin" target="_blank">http://www.bevanlab.biochem.<u></u>vt.edu/Pages/Personal/justin</a><br>
<br>
==============================<u></u>==========</font></span><div class="HOEnZb"><div class="h5"><br>
-- <br>
gmx-developers mailing list<br>
<a href="mailto:gmx-developers@gromacs.org" target="_blank">gmx-developers@gromacs.org</a><br>
<a href="http://lists.gromacs.org/mailman/listinfo/gmx-developers" target="_blank">http://lists.gromacs.org/<u></u>mailman/listinfo/gmx-<u></u>developers</a><br>
Please don&#39;t post (un)subscribe requests to the list. Use the www interface or send it to <a href="mailto:gmx-developers-request@gromacs.org" target="_blank">gmx-developers-request@<u></u>gromacs.org</a>.<br>
</div></div></blockquote></div><br></div>