#!/usr/bin/python2
"""
xyz2npy
Author: Patrick A. O'Neil

License:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import numpy as np
import sys

if len(sys.argv) != 3:
    print """
Converts from npy array to xyz format.

Usage:
    ./npy2xyz.py input/path.npy output/path.xyz

    """

# --- Read Inputs --- #
in_file     = sys.argv[1]   # Input  File (Npy)
out_file    = sys.argv[2]   # Output File (xyz)

# --- Load Npy --- #
try:
    D = np.load(in_file)
except IOError:
    print """
    Failed to load Npy file!
    Exiting.
    """

N = len(D)
d = len(D[0])

# --- Write xyz --- #

ofile = open(out_file,'w')

for p_ in D: 
    line = ""
    for p in p_:
        line += str(p) + " "

    # Compensate for no rotation coordinate
    if d == 3:
        # Pad with zeros
        line += "0 0 0"
    else:
        # Remove extra space
        line = line[:-1] + "\n"
    
    ofile.write(line)

ofile.close()

