#!/usr/bin/env python3
###########################################################################################
#  package:   pNbody
#  file:      splitgmov
#  copyright: GPLv3
#             Copyright (C) 2019 EPFL (Ecole Polytechnique Federale de Lausanne)
#             LASTRO - Laboratory of Astrophysics of EPFL
#  author:    Yves Revaz <yves.revaz@epfl.ch>
#
# This file is part of pNbody.
###########################################################################################

import sys
import os
import getopt
from pNbody import Movie
from numpy import *


##########################################################################
def version():
    ##########################################################################
    print('version 1.0')
    sys.exit(0)


##########################################################################
def help_message():
    ##########################################################################
    print("""Usage : splitgmov -f input --nh=nh --nv=nv

  Options: -h        -- this help message
           -f	     -- input movie
  	   --nh	     -- number of horizontal images
	   --nv	     -- number of vertical images
           --help    -- this help message
           --version -- displays version
  """)
    sys.exit(0)


##########################################################################
def check_arguments(options, xarguments):
    ##########################################################################

    nh = 1
    nv = 1
    output = None

    for a in options[:]:
        if a[0] == '-h':
            help_message()

        if a[0] == '--help':
            help_message()

        if a[0] == '--version':
            version()

        if a[0] == '--info':
            info = 1

        if a[0] == '--nh':
            if a[1] == '':
                help_message()
            else:
                nh = a[1]
                continue

        if a[0] == '--nv':
            if a[1] == '':
                help_message()
            else:
                nv = a[1]
                continue

        if a[0] == '-f':
            if a[1] == '':
                help_message()
            else:
                output = a[1]
                continue

    if output is None:
        help_message()

    return nh, nv, output


##########################################################################
#
#  MAIN
#
##########################################################################


try:
    options, xarguments = getopt.getopt(
        sys.argv[1:], 'f:h', ['info', 'help', 'version', 'nh=', 'nv='])
except getopt.error:
    help_message()
    sys.exit(0)


# check arguments
nh, nv, name = check_arguments(options, xarguments)

nh = int(nh)
nv = int(nv)


bname = os.path.split(os.path.splitext(name)[0])[1]
ext = os.path.splitext(name)[1]


film = Movie.Movie(name)
film.open()


n1 = nh 	# horizontal
n2 = nv		# vertical

nmov = n1 * n2


nh = film.numByte / n1		# sub movie horiz
nv = film.numLine / n2		# sub movie vert

# print film.numByte,n1,nh
# print film.numLine,n2,nv

dd = []

for i in range(1, n1 + 1):
    for j in range(1, n2 + 1):
        dd.append((i, j))


newfilms = []
for n in range(nmov):

    dx, dy = dd[n]
    newfilms.append(Movie.Movie('%s_%d%d%s' % (bname, dx, dy, ext)))
    newfilms[n].new(nh, nv)


n = -1
while True:
    n = n + 1
    mat = film.read_one("array")
    if mat is None:
        break

    print(film.current_time)

    # sous image

    for n in range(nmov):

        dx, dy = dd[n]

        smat = mat[nv * (dy - 1):nv + (nv * (dy - 1)),
                   nh * (dx - 1):nh + (nh * (dx - 1))]
        data = smat.tostring()
        newfilms[n].write_pic(film.current_time, data)
