#!/usr/bin/env python

# Partition an exodus file, writing the results to a parallel Triangle file

import sys
import posix
from sets import Set

args = Set()
argmap = {}

for x in sys.argv :
    args.add(x)
    z = x.split('=')
    if len(z)==2 : argmap[z[0]]=z[1]
  

if (len(args) < 2) | ('-h' in args) | ('--h' in args) | ('--help' in args) \
       | ('--i' not in argmap) | ('--np' not in argmap) :
    print 'usage: partition --np=<num processors> --i=<filename>'
    print 'do not include .ele or .node suffices on filename'
    sys.exit(0)

filename = argmap['--i']
np = argmap['--np']


# Run ncdump to convert the exodus file to a NCDF file

posix.system('/usr/local/bin/ncdump %s.exo > %s.ncdf' % (filename,filename))
print('wrote output to %s.ncdf' % filename)



# Run ncdf2tri to convert the NCDF file to a triangle file

posix.system('./ncdf2tri.py %s' % filename)
print('wrote Triangle file')



# Partition the triangle file

posix.system('./PartitionTri.py --i=%s --np=%s' % (filename, np))


