# -*- coding: cp932 -*-
import visual as Vp
import re
print "##################################################"
print "#    Start VisBAR_edu.v1.03.py                   #"
print "#    Apr 18, 2012                                #"
print "##################################################"
filename = raw_input("Input:file_name=")
move_type = raw_input("Input:[animation:a, picture:s, frame by fram:c]=")
move_limit = raw_input("input:max_step=")
bond_select = raw_input("[Input bond_list_file:y, no bond list:n]=")
if bond_select == "y":
    bondlist = raw_input("Input:bond_list_name=")
    bond = open(bondlist,"r")
h=open(filename,"r")
ions=[]
allion=[]
line = h.readline()
while line:
    line=h.readline()
    if line.find("mdstep")>=0:
        while 1:
            ion = h.readline()
            ion =ion.strip()
            ion = re.sub("  *"," ",ion)
            ion = re.split(" ",ion)
            if len(ion)<4 or [ion]==['']:
                break
                line =[]
            else:
                for x in range(3):
                    ion[x+1]=float(ion[x+1])
            ions += [ion]
        allion += [ions]
        ions=[]
    if [line]==['']:
        break
h.close()
scene = Vp.display(title="%s"%filename , width=800 , height=800 , background=(0.8,0.8,0.8))
viewobject=Vp.frame()
for i in range(len(allion)):
    if move_type == "s":
        i =int(move_limit)
    Vp.label(frame=viewobject,x=10,y=10, text='step number: %1i' % (i))
    for j in range(len(allion[0])):
        ionball=Vp.sphere(frame=viewobject,pos=(allion[i][j][1],allion[i][j][2],allion[i][j][3]), radius=0.35)

    if bond_select == "y":
        line = bond.readline()
        if line =="":
            for items in sub_items:
                ballposition_ini = [float(allion[i][int(items[0])-1][1]),
                                    float(allion[i][int(items[0])-1][2]),
                                    float(allion[i][int(items[0])-1][3])]
                ballposition_ter = [float(allion[i][int(items[1])-1][1]),
                                    float(allion[i][int(items[1])-1][2]),
                                    float(allion[i][int(items[1])-1][3])]
                ballposition_bin = [ballposition_ter[k]-ballposition_ini[k] for k in range(len(ballposition_ter))]
                rod = Vp.cylinder (frame=viewobject,pos=ballposition_ini, axis=ballposition_bin, radius=0.05, color=(1,0.5,0))                    
        else:
            sub_items = []
            items = line.split()
            for line in range(int(items[1])):
                line = bond.readline()
                items = line.split()
                sub_items +=[items]
                ballposition_ini = [float(allion[i][int(items[0])-1][1]),
                                    float(allion[i][int(items[0])-1][2]),
                                    float(allion[i][int(items[0])-1][3])]
                ballposition_ter = [float(allion[i][int(items[1])-1][1]),
                                    float(allion[i][int(items[1])-1][2]),
                                    float(allion[i][int(items[1])-1][3])]
                ballposition_bin = [ballposition_ter[k]-ballposition_ini[k] for k in range(len(ballposition_ter))]
                rod = Vp.cylinder (frame=viewobject,pos=ballposition_ini, axis=ballposition_bin, radius=0.05, color=(1,0.5,0))
    if j==len(allion[0])-1:
        if i==len(allion)-1 or i==int(move_limit):
            break
        Vp.rate(5)
        if i == 0 or move_type == "c":
            raw_input("[push any key]")
        for obj in viewobject.objects:
            obj.visible=0
    if move_type == "s" or i == int(move_limit):
        break
raw_input("end program [push any key]")
for obj in viewobject.objects:
    obj.visible=0
    scene.visible = 0
bond.close()
