#/usr/bin/env python

'''
	Software to cross correlate two files. To use it you had to file a params file
	who contains the information of the file you want to correlate.
	The information must have the format:
	   namefile = list of column ; delimiter
	   
	example:
	   file1 = 1,2,3 ;
	   file2 = 20,19,21 ; ,
	   
	no delimiter = blanck
'''

# there are a big problem of efficiency. The software is far to long with big file like SDSS. 
# I had to find where is the problem

import sys
import numarray
import string

#read the params file
params = {}
for line in file(sys.argv[1],'rU'):
    line = line.strip()         # delete the end of line (\n on unix)
    if not len(line): continue  # is line empty do nothing and pass to the next line
    if line.startswith('#'): continue # test if the line is a comments (# is the character to signal it)
    tup = line.split('=',1)     # split the line, the delimiter is the sign =
    columns = [int(i) for i in tup[1].strip().split(';')[0].strip().split(',')] # creat a list who contains 
    										# the columns we want to use
    delimiter = tup[1].strip().split(';')[1].strip()	# check the delimiter of the data file (generally space or coma)
    if not len(delimiter): delimiter = None
    params[tup[0].strip()] = { 'columns' : columns, 'delimiter' : delimiter}

# Read the data files (only the columns ask in the params file)
debut_data = 1
data = []
for namefile in params.iterkeys():
    import readcol2  #import the function to read the files
    #data.append(readcol2.readcol(namefile,columns=params[namefile]['columns'],comments='#',delimiter=params[namefile]['delimiter'],dep=1,arraytype='character'))
    params[namefile]['data'] = readcol2.readcol(namefile,columns=params[namefile]['columns'],comments='#',delimiter=params[namefile]['delimiter'],dep=debut_data,arraytype='character')


# Read another times the data files to have all the lines! 
# Question: like it's a dictionnary are we sure that the file are in the same order... Check it!!!!!!!!!
if len(params.keys()) == 2:
    namefile,data,delimiter = [],[],[]
    for keys in params.iterkeys():
        namefile.append(keys)
        data.append(params[keys]['data'])
        delim = params[keys]['delimiter']
        if delim != None:
            delimiter.append(params[keys]['delimiter'])
        else:
            delimiter.append('   ')
    #res_a = [] 
    #res_b = []
    
    f1_ini = file(namefile[0]).readlines()[debut_data:]
    f2_ini = file(namefile[1]).readlines()[debut_data:]

    #f1_ini = [line for line in file(namefile[0])][debut_data:]
    #f2_ini = [line for line in file(namefile[1])][debut_data:]
    
    f1=open('cross'+namefile[0],'w')
    f2=open('cross'+namefile[1],'w')
    f3=open('pastecross'+namefile[0]+namefile[1],'w')

    for a_i in range(data[0].shape[0]):
	for b_i in range(data[1].shape[0]):
            if numarray.all(data[0][a_i,:] == data[1][b_i,:]):
                f1.write(f1_ini[a_i])
                f2.write(f2_ini[b_i])
                f3.write(f1_ini[a_i].strip()+delimiter[0]+'   '+string.replace(f2_ini[b_i],delimiter[1],delimiter[1]))
                del f2_ini[b_i]
                break
                #res_a.append(a_i)
                #res_b.append(b_i)
    f1.close()
    f2.close()
    f3.close()
else:
    print "too much file: only two allowed for the moment"



#save the results in 3 files: 2 with the common objects from each file.
# one with a paste of the lines of the 2 initial files.
