import sys

versionString = '%s: version 1.2' % sys.argv[0]
print versionString

try:
    import psyco
    psyco.full()
except:
    print 'psyco not running'

from som import SOM
from mapGraphics import saveMapPNG, coordTranslate

if len(sys.argv) < 6:
    print "usage: python %s somfile scorefile inmap1 inmap2 outmap [-png pngfile] [-title string] [-vmax value] " % sys.argv[0]
    sys.exit(0)

somfile = sys.argv[1]
scorefile = sys.argv[2]
inmapfile1 = sys.argv[3]
inmapfile2 = sys.argv[4]
outmapfile = sys.argv[5]

outfiletitle = ''
doCoords = False

savePNG = False
pngfile = ''
if '-png' in sys.argv:
    pngfile = sys.argv[sys.argv.index('-png') + 1]
    savePNG = True

if '-title' in sys.argv:
    outfiletitle = sys.argv[sys.argv.index('-title') + 1]

maxValue = 0
if '-vmax' in sys.argv:
    maxValue = float(sys.argv[sys.argv.index('-vmax') + 1])

mysom = SOM(initialFile=somfile)

xmax = mysom.outCols
ymax = mysom.outRows
winnerDict = mysom.readScoreFile(scorefile)

inmap1 = mysom.readMap(inmapfile1)
inmap2 = mysom.readMap(inmapfile2)
outmap = mysom.newMap()

for (row, col) in mysom.units:
    outmap[row][col] = inmap1[row][col] - inmap2[row][col]

mysom.saveMap(outmap, outmapfile)

if savePNG:
    saveMapPNG(outmap, pngfile, outfiletitle, maxVal=maxValue, grid=mysom.gridType)

