#
#
#
 
"""make an AMBER traj file from a sequence of PDB files."""
__author__ = "Randall J. Radmer"
__version__ = "1.0"
  
 
import sys, os
import getopt
import fileinput
import time
#

def doIt(args_proper, scaleCoords, outFilename):
    f=None
    coords=[]
    s=''
    for line in fileinput.input(args_proper):
        if line.find('ATOM')==0:
            x, y, z = float(line[30:38]), float(line[38:46]), float(line[46:54])
            x*=scaleCoords
            y*=scaleCoords
            z*=scaleCoords
            coords.append(x)
            coords.append(y)
            coords.append(z)
        if line.find('END')==0:
            if not f:
                if outFilename:
                    f=open(outFilename, 'w')
                else:
                    f=sys.stdout
                f.write('This file generated by %s on %s\n' 
                        % (os.path.split(sys.argv[0])[1], time.asctime()))
            for coord in coords:
                s+='%8.3f' % coord
                if len(s)==80:
                    f.write('%s\n' % s)
                    s=''
            if len(s)>0:
                f.write('%s\n' % s)
                s=''
            coords=[]
    return True

def parseCommandLine():
    opts, args_proper = getopt.getopt(sys.argv[1:], 'hs:o:')
    sValue = 1
    oValue = ''
    for option, parameter in opts:
        if option=='-h': usageError()
        if option=='-s': sValue = float(parameter)
        if option=='-o': oValue = parameter
    return (args_proper, sValue, oValue)

def main():
    args_proper, scaleCoords, outFilename = parseCommandLine()

    doIt(args_proper, scaleCoords, outFilename)

    return

def usageError():
    print 'usage: %s [-o outFilename] [-s scale] fileName1 [fileName2 [fileName3...]]' \
         % os.path.basename(sys.argv[0])
    sys.exit(1)

if __name__=='__main__':
    main()

