下面是一个从分子动力学轨迹数据中读取速度值的脚本。我有许多带有名称模式的轨迹文件如下:
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
每个文件都有200帧数据需要分析。所以我计划以这样一种方式,这个代码应该在每个traj文件(如上所示)中一个接一个地读取,并提取速度值并写入特定文件(text_file = open(“Output.traj.dat”, “a”)对应于相应的输入轨迹文件。
所以我定义了一个名为'loops(mmm)'的函数,其中'mmm'是函数'loops'的轨迹文件名解析器。
#!/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 < 10:
for pico in range(100,1100, 100):
if pico >= 1000:
kkk = "waters1445-MD00{0}-run{1}.traj".format(md,pico)
loops(kkk)
elif pico < 1000:
kkk = "waters1445-MD00{0}-run0{1}.traj".format(md,pico)
loops(kkk)
#print kkk
在(#program starts - generation of file name)行,代码应该生成文件名并相应地调用函数并提取速度并转储值(text_file = open(“Output.mmm.dat”) “,”“a”)
执行此代码时,程序正在运行,但遗憾的是无法根据输入轨迹文件名生成输出文件。
我希望输出文件名为:
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
.
.
.
我无法追踪我需要做哪些更改。
答案 0 :(得分:0)
您的代码缩进已损坏:formatxyz
的第一个分配和以下代码未与def grouper
和for FRAMES
对齐。
主要问题可能是(就像Johannes已经评论过的那样)打开文件进行写入以及实际将数据写入文件的时间。
检查:
for FRAMES in range(0,200):
frame = FRAMES
text_file = open("Output.mmm.dat", "a")
输出文件名为(硬编码)Output.mmm.dat
。更改为"Output.{0}.dat".format(mmm)
。但是,变量mmm
永远不会在循环内部发生变化。如果所有帧都应写入同一文件,这可能没问题。
通常,请处理您为变量和函数选择的名称。 loops
非常通用,kkk
和mmm
也是如此。更具体地说,它有助于调试。如果您不知道发生了什么以及您的程序出错了,请插入带有一些描述性文字的print("dbg> do (a)")
语句和/或使用the Python debugger来逐步完成您的程序。尤其是交互式调试对于学习新语言和新概念至关重要。