#!/usr/remote/python/2.7.8/bin/python -W ignore::FutureWarning
'''
                 *** The command line interface for ExAMS ***                                                                                                                                  
'''

# General python libraries import
import argparse
import sys
import logging as log

from exams import rmsd
from exams._version import __version__

def main(args):
      
      if args.verbosity:
            log.basicConfig(format="%(levelname)s: %(message)s", level=log.INFO)
            log.info("Verbose output.")
      else:
            log.basicConfig(format="%(levelname)s: %(message)s")

      print '''\nPlease select one of the following options:\n'''
      print '''1 - If you want to calculate the average RMSD and the variance of the RMSDs for each snapshot in a trajectory with respect to a reference structure. \n'''
      print '''2 - If you want to calculate the RMSD of the time-averaged structure of a data set containing trajectories of replicated simulations, with respect to a reference structure. \n'''
      print '''3 - If you want to produce a chart, once you have got the output files of cases 1 and 2. \n'''
      print '''q - If you want to quit the program now. \n'''
      try:
            choice = raw_input("Now select an option from the list: \n")
            if choice == 'q':
                  sys.exit(0)
            choice = int(choice)
      except ValueError:
            log.error( '''Error: Please enter a value according to the listed options!''')
            sys.exit(-1)
      if choice == 1:
            log.info(''' You have selected case 1.''')
            if (args.reference is None) or (args.base_trajectory is None) or (args.name_trajectory is None) or (args.set_size is None) or (args.max_snapshots is None) or (args.output is None):
                  log.info(''' Example: ''')
                  log.info(''' ExAMS -r /users/username/MUP/1qy1/1qy1.gro -b /users/username/MUP/1qy1/rep -n md5_whole.xtc -s 100 -m 10000''')
                  log.info(''' In this case the trajectories to be analysed will be (specified in their absolute path):\n
                  /users/username/MUP/1qy1/rep0/md5_whole.xtc,\n
                  /users/username/MUP/1qy1/rep1/md5_whole.xtc,\n
                   ... ,\nrmsd.rmsd_avg_str(args)
                 /users/username/MUP/1qy1/rep99/md5_whole.xtc)''')
                  log.info('''\n According to the just shown example, specify the following inputs in order: \n''')
            if args.reference is None:
                  print '''      - The name of the reference structure to be taken as a reference for the calculation of the RMSDs!'''
                  args.reference = raw_input('''In the example above "/users/username/MUP/1qy1/1qy1.gro" corresponds to the name of the file of the reference structure. Provide the absolute path of your reference structure:\n ''')
            if args.base_trajectory is None:
                  log.info( '''      - Assuming that you have got many trajectories to analyse, and that they are stored in the same similar absoulte paths\n
                  (ex.\n
                  <Base_path>0/<traject_name>,\n
                  <Base_path>1/<traject_name>,\n
                   ... ,\n
                  <Base_path><numreplicas>-1/<traject_name>)\n
            you will need to specify the <Base_path> and the <traject name>''')
                  args.base_trajectory = raw_input('''In the example above "/users/username/MUP/1qy1/rep" corresponds to <Base_path>. Provide your <Base_path>: ''')
            if args.name_trajectory is None:
                  args.name_trajectory = raw_input('''In the example above "md5_whole.xtc" corresponds to <traject_name>. Provide your <traject_name>: ''')
            if args.set_size is None:   
                  print '''      - The number of the replicas (size of the data set) that you have got)'''
                  args.set_size = int(raw_input('''Provide the number of replicas/trajectory file to be analysed: '''))
            if args.max_snapshots is None:
                  args.max_snapshots = int(raw_input(''' Provide the maximum number of snapshots a trajectory among the considered ones might have: '''))
            if args.output is None:            
                  out = raw_input(''' Do you want to specify a name for the output file? y/n: ''')
                  if (out == "y") or (out == 'Y'):
                        args.output=raw_input(''' Provide the name for the output file: ''')
                  else:
                        print ''' You did not select to provide a name for the output file. The default name for the output file provided will be: 1_rmsd_var.dat'''
            print '''\n You have selected the following: \n'''
            print "ExAMS -r %s -b %s -n %s -s %d -m %d -o %s" % (str(args.reference), str(args.base_trajectory), str(args.name_trajectory), int(args.set_size), int(args.max_snapshots), str(args.output))
            rmsd.rmsd_var_avg(args)
      elif choice == 2:
            print ''' You have selected case 2.''' 
            if (args.reference is None) or (args.base_trajectory is None) or (args.name_trajectory is None) or (args.set_size is None) or (args.max_snapshots is None) or (args.output is None):
                  log.info(''' Example: ''')
                  log.info(''' ExAMS -r /users/username/MUP/1qy1/1qy1.gro -b /users/username/MUP/1qy1/rep -n md5_whole.xtc -s 100 -m 10000''')
                  log.info(''' In this case the trajectories to be analysed will be (specified in their absolute path):\n
                  /users/username/MUP/1qy1/rep0/md5_whole.xtc,\n
                  /users/username/MUP/1qy1/rep1/md5_whole.xtc,\n
                   ... ,\nrmsd.rmsd_avg_str(args)
                 /users/username/MUP/1qy1/rep99/md5_whole.xtc)''')
                  log.info('''\n According to the just shown example, specify the following inputs in order: \n''')
            if args.reference is None:
                  print '''      - The name of the reference structure to be taken as a reference for the calculation of the RMSDs!'''
                  args.reference = raw_input('''In the example above "/users/username/MUP/1qy1/1qy1.gro" corresponds to the name of the file of the reference structure. Provide the absolute path of your reference structure:\n ''')
            if args.base_trajectory is None:
                  log.info( '''      - Assuming that you have got many trajectories to analyse, and that they are stored in the same similar absoulte paths\n
                  (ex.\n
                  <Base_path>0/<traject_name>,\n
                  <Base_path>1/<traject_name>,\n
                   ... ,\n
                  <Base_path><numreplicas>-1/<traject_name>)\n
            you will need to specify the <Base_path> and the <traject name>''')
                  args.base_trajectory = raw_input('''In the example above "/users/username/MUP/1qy1/rep" corresponds to <Base_path>. Provide your <Base_path>: ''')
            if args.name_trajectory is None:
                  args.name_trajectory = raw_input('''In the example above "md5_whole.xtc" corresponds to <traject_name>. Provide your <traject_name>: ''')
            if args.set_size is None:   
                  print '''      - The number of the replicas (size of the data set) that you have got)'''
                  args.set_size = int(raw_input('''Provide the number of replicas/trajectory file to be analysed: '''))
            if args.max_snapshots is None:
                  args.max_snapshots = int(raw_input(''' Provide the maximum number of snapshots a trajectory among the considered ones might have: '''))
            if args.output is None:            
                  out = raw_input(''' Do you want to specify a name for the output file? y/n: ''')
                  if (out == "y") or (out == 'Y'):
                        args.output=raw_input(''' Provide the name for the output file: ''')
                  else:
                        print ''' You did not select to provide a name for the output file. The default name for the output file provided will be: 2_rmsd_avgstr.dat'''
            print '''\n You have selected the following: \n'''
            print "ExAMS -r %s -b %s -n %s -s %d -m %d -o %s" % (str(args.reference), str(args.base_trajectory), str(args.name_trajectory), int(args.set_size), int(args.max_snapshots), str(args.output))
            rmsd.rmsd_avg_str(args)
      elif choice == 3:
            if (args.data1 is None) or (args.data2 is None):
                  log.error('''Try again providing the names of the two files while running the program!''')
                  log.error(''' Example: ''')
                  log.error(''' ExAMS --data1 1_rmsd_var.dat --data2 2_rmsd_avgstr.dat''')
                  sys.exit(-1)

            rmsd.rmsd_rmsd_avg_str_plot(args)
      else:
            print '''Error: Please enter a value according to the listed options!'''
     
if __name__ == '__main__':
      parser=argparse.ArgumentParser()
      parser.add_argument('-b','--base_trajectory', type=str, help=''' Specify the absoulte path of the trajectory without the trajectory name. ''')
      parser.add_argument('-n','--name_trajectory', type=str, help=''' Specify the name of the trajectory file that you need to analyse. ''')
      parser.add_argument('-r','--reference', type=str, help='''The name of the reference structure file to be taken as a reference for the comparison of rmsd-s etc.''')
      parser.add_argument('-o','--output', type=str, help='''The name of the output file for the storage of the calculations results.''')
      parser.add_argument('-s','--set_size', type=int, default=100, help='''Specify the number of total replicas considered for the analysis of the specified set.''')
      parser.add_argument('-m','--max_snapshots', type=int, default=10000, help='''Specify the maximum number of snapshots that might be included in a trajectory of the replicas considered for analysis.''')
      parser.add_argument('-v','--verbosity', nargs='?',help='''Increase output verbosity.''')
      parser.add_argument('--data1', type=str, help=''' Specify the absoulte path of the name of a data file containing information on the average RMSD and the variance of the RMSDs for each snapshot in a trajectory with respect to a reference structure.''')
      parser.add_argument('--data2', type=str, help=''' Specify the absoulte path of the name of a data file containing information on the RMSD of the time-averaged structure of a data set containing trajectories of replicated simulations, with respect to a reference structure.''')
      parser.add_argument('-V','--version', action='version', version=__version__)
      
      args=parser.parse_args()
      main(args)
