#!/usr/bin/python
#-------------------------------------------------------------------------------
# Main
import sys
from CSystem import C_System
from CMolType import C_MolType
from CTrajectory import C_Trajectory
from CAtom import CG_AtomType
from CDF import C_DF
import time
import pickle
        
# Reading the command line arguments
input_file_type='old_in'
while len(sys.argv)>1:
    option=sys.argv[1]; del sys.argv[1]
    if option=='-i':
        input_file_name=sys.argv[1]; del sys.argv[1]
    elif option=='-t':
        if sys.argv[1]=='old_in': 
            input_file_type='old_in'; del sys.argv[1]
        elif sys.argv[1]=='xml':
            input_file_type='xml'; del sys.argv[1]
        else :
            print 'unknown type of input file:',sys.argv[1]
            sys.exit(1)        
    else:
        print 'invalid input parameters:',option
        sys.exit(1)
# Checking if all important input parameters are read.
try: 
    print 'Main input file:',input_file_name
except NameError:
     print "No input file type provided \n Try rdf-2.0.py -i input "; sys.exit(1)
try: 
    print 'Main input file type:',input_file_type
except NameError:
     print "No input file type provided \n Try rdf-2.0.py -i input "; sys.exit(1)
# Here we create "the system" object and read it's molecular description and settings
system=C_System(input_file_name,input_file_type)
e0=time.time()
c0=time.clock()
system._RDF_pairs_matrix() #Create matrix of pairs for RDFs
system._ADF_pairs_matrix() #Create matrix of pairs for ADFs
if len(system.RDFs)>0:    
    system.Calculate(system.RDFs,plot=False,smooth=True,trim=True)
    system.RDFs_SaveForMagic()
if len(system.ADFs)>0:    
    system.Calculate(system.ADFs,tolerance=1.0e-6,plot=False,smooth=True,trim=True)
    system.ADFs_SaveForMagic()
system.write_MCMs()
if len(system.box)>0:
    print 'Box size: ',(system.box[0],system.box[1],system.box[2])
#Dump system to file input_file_name.dmp
ofile=open(input_file_name+'.dmp','wb') 
try:
    pickle.dump(system,ofile)
    print "The system was succesfully dumped to file ",input_file_name+'.dmp'
except:
    print "Error happend while dumping the system to file ",input_file_name+'.dmp'
ofile.close()
print "Elasped time:",time.time()-e0, "CPU time:",time.clock()-c0