# -*- coding: utf-8 -*-

# 2018-06-13 MW using the fabric tensor of spam

import numpy, math
import spam.label.toolkit as ltk

TEXTOUTPUT = True

folder   = "/home/max/Documents/promotion/dem/image_processing/monotonic/test_2_spam/fabric/"

# input
filenames = ['triax.-1.eps=0.contacts-exclude',
             'triax.0.eps=-0.0025.contacts-exclude',
             'triax.1.eps=-0.005.contacts-exclude',
             'triax.2.eps=-0.01.contacts-exclude',
             'triax.3.eps=-0.02.contacts-exclude',
             'triax.4.eps=-0.03.contacts-exclude',
             'triax.5.eps=-0.04.contacts-exclude',
             'triax.6.eps=-0.05.contacts-exclude',
             'triax.7.eps=-0.06.contacts-exclude',
             'triax.8.eps=-0.07.contacts-exclude',
             'triax.9.eps=-0.08.contacts-exclude',
             'triax.10.eps=-0.09.contacts-exclude',
             'triax.11.eps=-0.1.contacts-exclude',
             'triax.12.eps=-50124.6.contacts-exclude',
                      ]

filenames_output = ["triax_test_2-dem_fabric-tensor.txt"]

if TEXTOUTPUT:
    f = open(folder+filenames_output[0],'w')
    f.write('#state F[1,1] F[1,2]=F[2,1] F[1,3]=F[3,1] F[2,2] F[2,3]=F[3,2] F[3,3] a\n')
    f.close()

counter = 0

for filename in filenames:
    # loading the dem file
    print "\nLoading ", folder + filename + ".txt", "...",
    contacts = numpy.loadtxt( folder + filename+".txt",usecols=(4,3,2) )
    print " done"
    
    N, F, a = ltk.fabricTensor(contacts)
    
    print N
    print ""
    print F
    print ""
    print a
    
    if TEXTOUTPUT:
        f = open(folder+filenames_output[0],'a')
        f.write('%02d %10.8f %10.8f %10.8f %10.8f %10.8f %10.8f %10.8f '%(counter, N[0,0], N[0,1], N[0,2], N[1,1], N[1,2], N[2,2], a))
        f.write('\n')
        f.close() 
        
    counter += 1
