#!/usr/bin/env python

'''
Created by Martijn Wieling, University of Groningen, 2010-05-31
   First argument is the file wich should be converted
   Two output files are returned:
       - a text file
       - an SPSS syntax file which is used by SPSS to create the data file
   Note that the path to spssdata.txt on the PC where the file is used
   has to be set manually
'''

import locale, os, sys

def convertDif(filename):
    inputfile = open(filename, 'rt')

    try:
        assert not os.access('readdata.sps', os.F_OK)
        assert not os.access('spssdata.txt', os.F_OK)
    except:
        sys.stderr.write('Error: Output file(s) exist(s)\n')
        return

    outputfile1 = open('readdata.sps', 'wt')
    outputfile1.write('''GET DATA
  /TYPE=TXT
  /FILE='TYPE-COMPLETE-PATH-HERE\spssdata.txt'
  /DELIMITERS="\\t"
  /ARRANGEMENT=DELIMITED
  /FIRSTCASE=2
  /IMPORTCASE=ALL
  /VARIABLES=
  location1 F2.0
  location2 F2.0
  distance F12.10.
CACHE.
EXECUTE.
DATASET NAME DialectDistances WINDOW=FRONT.

value labels location1 location2 ''')

    outputfile2 = open('spssdata.txt', 'wt')
    outputfile2.write('location1\tlocation2\tdistance\n')

    countExpected = True
    placesExpected = False
    distancesExpected = False
    loc1 = 2
    loc2 = 1
    placenr = 1
    dec = locale.localeconv()['decimal_point']
    for line in inputfile:
        line = line.strip()
        if not line or line[0] == '#':
            continue
        if distancesExpected:
            outputfile2.write('%i\t%i\t%s\n' % (loc1, loc2, line.replace('.', dec)))
            loc2 += 1
            if loc2 == loc1:
                loc1 += 1
                loc2 = 1
        elif placesExpected:
            outputfile1.write('%i "%s" ' % (placenr, line.replace('"', "''")))
            placenr += 1
            if placenr > nrplaces:
                placesExpected = False
                distancesExpected = True
        elif countExpected:
            nrplaces = int(line)
            countExpected = False
            placesExpected = True

    inputfile.close()
    outputfile1.close()
    outputfile2.close()


locale.setlocale(locale.LC_ALL, '')
convertDif(sys.argv[1])



