#!/usr/bin/env python

import sys
import argparse
import numpy as np
from paw import pawpotcar


def parse_cml_args(cml):
    '''
    CML parser.
    '''
    arg = argparse.ArgumentParser(add_help=True)

    arg.add_argument('-p', '--potcar', dest='potcar', action='store', type=str,
                     default='POTCAR',
                     help='location of VASP POTCAR')

    arg.add_argument('-n', dest='npotcar', action='store', type=int,
                     default=0,
                     help='which element to show when there are more than one element in POTCAR')

    return arg.parse_args(cml)


def main(cml):
    p = parse_cml_args(cml)

    pawpp = [pawpotcar(potstr) for potstr in
             open(p.potcar).read().split('End of Dataset')[:-1]]
    assert p.npotcar < len(pawpp)

    print("POTCAR Found: [{}]".format(', '.join([pp.element for pp in pawpp])))
    print("Plotting the POTCAR of {}".format(pawpp[p.npotcar]))
    pawpp[p.npotcar].plot()


if __name__ == "__main__":
    main(sys.argv[1:])
