#!/usr/bin/python
################################################
#Convert Zubercal parquet files into text files#
#AJD May 16th 2023                             #
################################################

import sys
import os
import numpy as np
import pyarrow.parquet as pq
import pandas as pd
from os.path import exists

verbose=0
fileout="test.parquet"
if(len(sys.argv) < 2):
    print("Inputs: Zubercal parquet input photometry filename, verbose (0/1)")
    print("e.g. ./%s Test.parquet 1" % os.path.basename(__file__))
    sys.exit(0)
else:
    filein = sys.argv[1]
    verbose = int(sys.argv[2])
    fileout = filein.replace('.parquet','.txt')
    print("Writing data to %s " % fileout)

if(os.path.exists(filein)):
    try:
        fout = open(fileout,'w')
    except:
        print("Could not save file %s" % fileout)
        sys.exit(0)
    ds = pq.ParquetDataset(filein)
    df_out = ds.read().to_pandas()

    #To select a single PS1 ID.
    #table = pq.read_table(filename, filters=[('id', '=', PS1ID)])
    #df_out = table.to_pandas().drop_duplicates()

    #To select multiple IDs from a list named tup
    #table = pq.read_table(filein, filters=[('objectid', 'in', tup)])
    #df_out = table.to_pandas().drop_duplicates()

    #print(df_out.dtypes)
    #Header
    fout.write("# PS1_ID           MJD_helio      mag    mag_err     RA        Dec  field rc flag infobits\n")
    for row in df_out.itertuples():
        err = row.magerr /10000
        if(verbose > 0):
            print("%s %f %f %f %f %f %s %s %d %d" % (row.objectid, row.mjd, row.mag, err, row.objra, row.objdec,\
                                                        row.fieldid, row.rcidin, row.flag, row.info))
        fout.write("%s %f %f %f %f %f %s %s %d %d\n" % (row.objectid, row.mjd, row.mag, err, row.objra, row.objdec,\
                                                           row.fieldid, row.rcidin, row.flag, row.info))
    fout.close()
else:
    print("Input file %s does not exist" % filein)
    sys.exit()
