import tooba_f as tbf
###################################################
#NOTICE:    resids of head in each subdomain may differ in tail case
#           keep all atoms of group in the first occurent subdomain
#           in case of tail is the one closest to the head, hence
#           the code is a good approximation 
###################################################
TRAJ='traj.trr'
NUM_FR=1
GRO='initial.gro'
HEAD=True
HD_GROUP={'MMA':['C1', 'C2']}
TAIL=True
TL_GROUP={'MMA':['C3', 'C4', 'C5']}
###################################################
#Read .trr file
data_all=tbf.fr_export(trajfile=TRAJ,num_frames=NUM_FR)
#Read .gro file
_,data_num,_,res_num,res_type,atom_type,atom_num,_ = tbf.read_gro(GRO)
#Create subdomains coordinates
box_p=tbf.domain_decomposition(data=data_all,dx=2,dy=2,dz=2)

###################################################
if HEAD==True:
    #Find atom type index in lists created above
    group_ndx=tbf.atomid_data(res_num, atom_type, atom_num, group=HD_GROUP)
    #Assign desired atoms (from above function) to subdomains
    ##result1: {step:{res:{atom_type:{atom_num:(subX,subYsubZ)}}}}
    ##result2: {step:{res:{atom_type:{(subX,subYsubZ):[atom_num]}}}}
    _,box_res=tbf.atom2grid(data_all,box_p, group_ndx)
    #Creates dictionary with coordinates per subdomain for each frame
    coord_norm,_=tbf.sub_coord(box=box_res, data=data_all, res_num=res_num)
    #Creates dictionary with c, normal per subdomain for each frame
    surf=tbf.coord2norm(coord_norm,img=True)
###################################################
if TAIL==True:
    #Find atom type index in lists created above
    group_ndx=tbf.atomid_data(res_num, atom_type, atom_num, group=TL_GROUP)
    #Assign desired atoms (from above function) to subdomains
    ##result1: {step:{res:{atom_type:{atom_num:(subX,subYsubZ)}}}}
    ##result2: {step:{res:{atom_type:{(subX,subYsubZ):[atom_num]}}}}
    _,box_res=tbf.atom2grid(data_all,box_p, group_ndx)
    #Creates dictionary with coordinates per subdomain for each frame
    _,coord_vector=tbf.sub_coord(box=box_res, data=data_all, res_num=res_num)
    vector=tbf.coord2vector(coord_vector)
###################################################
tbf.SurfVector_angle(surf,vector)