generate output file name accordance to input file names
<p>Below is a script to read velocity values from molecular dynamics trajectory data. I have many trajectory files with the name pattern as below:</p>

<pre><code>waters1445-MD001-run0100.traj
waters1445-MD001-run0200.traj
waters1445-MD001-run0300.traj
waters1445-MD001-run0400.traj
waters1445-MD001-run0500.traj
waters1445-MD001-run0600.traj
waters1445-MD001-run0700.traj
waters1445-MD001-run0800.traj
waters1445-MD001-run0900.traj
waters1445-MD001-run1000.traj
waters1445-MD002-run0100.traj
waters1445-MD002-run0200.traj
waters1445-MD002-run0300.traj
waters1445-MD002-run0400.traj
waters1445-MD002-run0500.traj
waters1445-MD002-run0600.traj
waters1445-MD002-run0700.traj
waters1445-MD002-run0800.traj
waters1445-MD002-run0900.traj
waters1445-MD002-run1000.traj
</code></pre>

<p>Each file has 200 frames of data to analyse. So I planned in such a way where this code supposed to read in each traj file (shown above) one after one and extract the velocity values and write in a specific file (text_file = open("Output.traj.dat", "a") corresponding to the respective input trajectory file.</p>

<p>So I defined a function called 'loops(mmm)', where 'mmm' is a trajectory file name parser to the function 'loops'.</p>

<pre><code>#!/usr/bin/env python
'''
always put #!/usr/bin/env python at the shebang
'''
#from __future__ import print_function
from Scientific.IO.NetCDF import NetCDFFile as Dataset
import itertools as itx
import sys
#####################


def loops(mmm): 
  inputfile = mmm

  for FRAMES in range(0,200):
      frame = FRAMES
      text_file = open("Output.mmm.dat", "a")

      def grouper(n, iterable, fillvalue=None):
        args = [iter(iterable)] * n
        return itx.izip_longest(fillvalue=fillvalue, *args)

    formatxyz = "%12.7f%12.7f%12.7f%12.7f%12.7f%12.7f"
    formatxyz_size = 6
    formatxyzshort = "%12.7f%12.7f%12.7f"
    formatxyzshort_size = 3

    #ncfile = Dataset(inputfile, 'r')
    ncfile = Dataset(ppp, 'r')

    variableNames = ncfile.variables.keys()
    #print variableNames

    shape = ncfile.variables['coordinates'].shape
    '''
    do the header
    '''

    print 'title ' + str(frame)
    text_file.write('title ' + str(frame) + '\n')
    print "%5i%15.7e" % (shape[1],ncfile.variables['time'][frame])
    text_file.write("%5i%15.7e" % (shape[1],ncfile.variables['time']\
    [frame]) + '\n')

    '''
    do the velocities
    '''
    try:
        xyz = ncfile.variables['velocities'][frame]
        temp = grouper(2, xyz, "")

        for i in temp:
            z = tuple(itx.chain(*i))
            if (len(z) == formatxyz_size): 
                print formatxyz % z
                text_file.write(formatxyz % z + '\n')
            elif (len(z) == formatxyzshort_size): 
                print formatxyzshort % z
                text_file.write(formatxyzshort % z + '\n' )

    except(KeyError):
        xyz = [0] * shape[2] 
        xyz = [xyz] * shape[1]
        temp = grouper(2, xyz, "")

        for i in temp:
            z = tuple(itx.chain(*i))
            if (len(z) == formatxyz_size): 
                print formatxyz % z
            elif (len(z) == formatxyzshort_size): 
                print formatxyzshort % z
            x = ncfile.variables['cell_angles'][frame]
            y = ncfile.variables['cell_lengths'][frame]

 #text_file.close()


# program starts - generation of file name
for md in range(1,3):
   if md &lt; 10:
      for pico in range(100,1100, 100):
        if  pico &gt;= 1000:
            kkk = "waters1445-MD00{0}-run{1}.traj".format(md,pico)
            loops(kkk)
        elif pico &lt; 1000:
            kkk = "waters1445-MD00{0}-run0{1}.traj".format(md,pico)
            loops(kkk)

        #print kkk
</code></pre>

<p>At the (# program starts - generation of file name) line, the code supposed to generate the file name and accordingly call the function and extract the velocity and dump the values in (text_file = open("Output.mmm.dat", "a")</p>

<p>When execute this code, the program is running, but unfortunately could not produce output files according the input trajectory file names.</p>

<p>I want the output file names to be:</p>

<pre><code>velo-waters1445-MD001-run0100.dat
velo-waters1445-MD001-run0200.dat
velo-waters1445-MD001-run0300.dat
velo-waters1445-MD001-run0400.dat
velo-waters1445-MD001-run0500.dat
.
.
.
</code></pre>

<p>I could not trace where I need to do changes. </p>

<p>Could anyone help ?</p>

<p>Many thanks in advance.
Regards  </p>
