#!/usr/bin/python

#    fchart draws beautiful deepsky charts in vector formats
#    Copyright (C) 2005  Michiel Brentjens
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
from numpy import *
from string import *
import sys
import os

from fchart.config import *

# slow (first) index: row number
# fast (second) index: (ra, dec, mag)

tycho2_directory = '.'
out_dir          = os.path.join(FCHART_DATA_DIR,'catalogs')

if len(sys.argv) >= 2:
    tycho2_directory = sys.argv[1]
    pass

if len(sys.argv) >= 3:
    out_dir = sys.argv[2]
    pass


tyc2_array = zeros((2539913+17588+1146,3), dtype=float32)

filename_in=tycho2_directory+'/tyc2.dat'
filename_out=out_dir+'/tyc2.bin'

sup1_name = tycho2_directory+'/suppl_1.dat'
sup2_name = tycho2_directory+'/suppl_2.dat'
index_name = tycho2_directory+'/index.dat'

os.system('cp '+index_name+' '+out_dir+'/index.dat')
infile = file(filename_in, 'r')
blocksize_in = 1000

rownumber = 0
while True:
    if rownumber % 5000 == 0:
        print rownumber
        pass
    lines = infile.readlines(blocksize_in)
    if len(lines) == 0:
        break
    for line in lines:
        if line[13] == 'X':
            ra = atof(line[152:164])
            dec= atof(line[165:177])
        else:
            ra = atof(line[15:27])
            dec = atof(line[28:40])
            pass
        magstring = line[123:129].rstrip()
        if len(magstring) == 0:
            magstring = line[110:116].rstrip()
            pass
        mag = atof(magstring)
        tyc2_array[rownumber,0] = ra*pi/180.0
        tyc2_array[rownumber,1] = dec*pi/180.0
        tyc2_array[rownumber,2] = mag
        rownumber += 1
        pass
    pass
infile.close()

infile = file(sup1_name, 'r')
while True:
    if rownumber % 5000 == 0:
        print rownumber
        pass
    lines = infile.readlines(blocksize_in)
    if len(lines) == 0:
        break
    for line in lines:
        ra = atof(line[15:27])
        dec = atof(line[28:40])

        magstring = line[96:102].rstrip()
        if len(magstring) == 0:
            magstring = line[83:89].rstrip()
            pass
        mag = atof(magstring)
        tyc2_array[rownumber,0] = ra*pi/180.0
        tyc2_array[rownumber,1] = dec*pi/180.0
        tyc2_array[rownumber,2] = mag
        rownumber += 1
        pass
    pass
infile.close()

infile = file(sup2_name, 'r')
while True:
    if rownumber % 5000 == 0:
        print rownumber
        pass
    lines = infile.readlines(blocksize_in)
    if len(lines) == 0:
        break
    for line in lines:
        ra = atof(line[15:27])
        dec = atof(line[28:40])

        magstring = line[96:102].rstrip()
        if len(magstring) == 0:
            magstring = line[83:89].rstrip()
            pass
        mag = atof(magstring)
        tyc2_array[rownumber,0] = ra*pi/180.0
        tyc2_array[rownumber,1] = dec*pi/180.0
        tyc2_array[rownumber,2] = mag
        rownumber += 1
        pass
    pass
infile.close()

print rownumber, 'rows total'



outfile= file(filename_out, 'w')
# store in big-endian order
if sys.byteorder == 'little':
    tyc2_array.byteswap()
    pass
tyc2_array.tofile(outfile)
outfile.close()
infile.close()
