import cdspyreadme
from astropy.io import fits
from astropy.table import Table

def make_euds_cds(infile1=None,outfile1=None,infile2=None,outfile2=None,fluxlim=None):

    print("Reading {}".format(infile1))

    #########################
    """ Read eUDS catalog """
    #########################
    
    tablemaker = cdspyreadme.CDSTablesMaker()

    r = Table.read(infile1)
    tab=Table([
        r['ID_SRC'],           # 0
        r['NAME'],             # 1
        r['RA'],               # 2
        r['DEC'],              # 3
        r['RADEC_ERR'],        # 4 Positional error (68% confidence)
        r['EXT'],              # 5 Source extent 
        r['EXT_ERR'],          # 6 Extent uncertainty
        r['EXT_LIKE'],         # 7
        r['DET_LIKE'],         # 8 
        r['ML_RATE'],          # 9
        r['ML_RATE_ERR'],      # 10 
        r['ML_CTS'],           # 11
        r['ML_CTS_ERR'],       # 12 
        r['ML_FLUX'],          # 13
        r['ML_FLUX_ERR'],      # 14 
        r['ML_EXP'],           # 15
        r['ML_BKG'],           # 16
        r['DR12_IAU_NAME'] ,   # 17 4XMM J021738.8-051257
        r['DET_LIKE_1']    ,   # 18
        r['ML_RATE_1']     ,   # 19
        r['ML_RATE_ERR_1'] ,   # 20
        r['ML_CTS_1']      ,   # 21
        r['ML_CTS_ERR_1']  ,   # 22
        r['ML_FLUX_1']     ,   # 23
        r['ML_FLUX_ERR_1'] ,   # 24
        r['ML_EXP_1']      ,   # 25
        r['ML_BKG_1']      ,   # 26
        r['DET_LIKE_2']    ,   # 27
        r['ML_RATE_2']     ,   # 28
        r['ML_RATE_ERR_2'] ,   # 29
        r['ML_CTS_2']      ,   # 30
        r['ML_CTS_ERR_2']  ,   # 31
        r['ML_FLUX_2']     ,   # 32
        r['ML_FLUX_ERR_2'] ,   # 33
        r['ML_EXP_2']      ,   # 34
        r['ML_BKG_2']      ,   # 35
        r['DET_LIKE_3']    ,   # 36
        r['ML_RATE_3']     ,   # 37
        r['ML_RATE_ERR_3'] ,   # 38
        r['ML_CTS_3']      ,   # 39
        r['ML_CTS_ERR_3']  ,   # 40
        r['ML_FLUX_3']     ,   # 41
        r['ML_FLUX_ERR_3'] ,   # 42
        r['ML_EXP_3']      ,   # 43
        r['ML_BKG_3']      ,   # 44
        r['DET_LIKE_4']    ,   # 45
        r['ML_RATE_4']     ,   # 46
        r['ML_RATE_ERR_4'] ,   # 47
        r['ML_CTS_4']      ,   # 48
        r['ML_CTS_ERR_4']  ,   # 49
        r['ML_FLUX_4']     ,   # 50
        r['ML_FLUX_ERR_4'] ,   # 51
        r['ML_EXP_4']      ,   # 52
        r['ML_BKG_4']      ,   # 53
    ])

    table = tablemaker.addTable(tab, name=outfile1, description="eUDS X-ray catalog, converted from eUDS.fits")

    col = table.get_column('ID_SRC')           # 0
    col.name="SrcID"
    col.set_format("I3")
    col.description="Source ID (ID_SRC)"

    col = table.get_column('NAME')             # 1
    col.name="Name"
    col.set_format("A21")
    col.description="Source name, eUDS JHHMMSS.s+DDMMSS"

    col = table.get_column('RA')               # 2
    col.set_format("F10.6")
    col.name="RAdeg"
    col.description="RA (J2000) (RA)"

    col = table.get_column('DEC')              # 3
    col.name="DEdeg"
    col.set_format("F10.6")
    col.description="Dec (J2000) (RA)"

    col = table.get_column('RADEC_ERR')        # 4 Positional error (68% confidence)
    col.set_format("F6.3")
    col.name="ePos"
    col.description="Positional uncertainty (RADEC_ERR)"

    col = table.get_column('EXT')              # 5 Source extent 
    col.set_format("F5.2")
    col.name="Ext"
    col.description="Source extent (EXT)"

    col = table.get_column('EXT_ERR')          # 6 Extent uncertainty
    col.set_format("F5.2")
    col.name="e_Ext"
    col.description="Extent error (EXT_ERR)"

    col = table.get_column('EXT_LIKE')         # 7
    col.set_format("F10.6")
    col.name="ExtLike"
    col.description="Extent likelihood (EXT_LIKE)"

    col = table.get_column('DET_LIKE')         # 8 
    col.set_format("F12.6")
    col.name="DetLike"
    col.description="Detection likelihood measured by PSF-fitting; 0.3-2.3 keV (DET_LIKE)"

    col = table.get_column('ML_RATE')          # 9
    col.name="MLRate"
    col.set_format("F8.6")
    col.description="Source count rate measured by PSF-fitting; 0.3-2.3 keV (ML_RATE)"

    col = table.get_column('ML_RATE_ERR')      # 10 
    col.name="e_MLRate"
    col.set_format("F8.6")
    col.description="1 sigma count rate error; 0.3-2.3 keV (ML_RATE_ERR)"

    col = table.get_column('ML_CTS')           # 11
    col.name="MLCts"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 0.3-2.3 keV (ML_CTS)"

    col = table.get_column('ML_CTS_ERR')       # 12 
    col.name="e_MLCts"
    col.set_format("F11.6")
    col.description="1 sigma counts error; 0.3-2.3 keV (ML_CTS_ERR)"

    col = table.get_column('ML_FLUX')          # 13
    col.name="MLFlux"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 0.3-2.3 keV (ML_FLUX)"

    col = table.get_column('ML_FLUX_ERR')      # 14 
    col.name="e_MLFlux"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 0.3-2.3 keV (ML_FLUX_ERR)"

    col = table.get_column('ML_EXP')           # 15
    col.name="MLExp"
    col.set_format("E12.6")
    col.description="Vignetted exposure value; 0.3-2.3 keV (ML_EXP)"

    col = table.get_column('ML_BKG')           # 16
    col.name="MLBkg"
    col.set_format("F12.6")
    col.description="Background at the source position; 0.3-2.3 keV (ML_BKG)"

    col = table.get_column('DR12_IAU_NAME')    # 17 4XMM J021738.8-051257
    col.name="DR12Name"
    col.set_format("A21")
    col.description="4XMM-DR12 source name used for forced photometry (DR12_IAU_NAME)"

    col = table.get_column('DET_LIKE_1')       # 18
    col.name="DetLike1"
    col.set_format("F12.6")
    col.description="Detection likelihood measured by PSF-fitting; 0.3-0.6 keV (DET_LIKE_1)"

    col = table.get_column('ML_RATE_1')        # 19
    col.name="MLRate1"
    col.set_format("F8.6")
    col.description="Source count rate measured by PSF-fitting; 0.3-0.6 keV (ML_RATE_1)"

    col = table.get_column('ML_RATE_ERR_1')    # 20
    col.name="e_MLRate1"
    col.set_format("F8.6")
    col.description="1 sigma count rate error; 0.3-0.6 keV (ML_RATE_ERR_1)"

    col = table.get_column('ML_CTS_1')         # 21
    col.name="MLCts1"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 0.3-0.6 keV (ML_CTS_1)"

    col = table.get_column('ML_CTS_ERR_1')     # 22
    col.name="e_MLCts1"
    col.set_format("F11.6")
    col.description="1 sigma counts error; 0.3-0.6 keV (ML_CTS_ERR_1)"

    col = table.get_column('ML_FLUX_1')        # 23
    col.name="MLFlux1"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 0.3-0.6 keV (ML_FLUX_1)"

    col = table.get_column('ML_FLUX_ERR_1')    # 24
    col.name="e_MLFlux1"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 0.3-0.6 keV (ML_FLUX_ERR_1)"

    col = table.get_column('ML_EXP_1')         # 25
    col.name="MLExp1"
    col.set_format("E12.6")
    col.description="Vignetted exposure value; 0.3-0.6 keV (ML_EXP_1)"

    col = table.get_column('ML_BKG_1')         # 26
    col.name="MLBkg1"
    col.set_format("F12.6")
    col.description="Background at the source position; 0.3-0.6 keV (ML_BKG_1)"

    col = table.get_column('DET_LIKE_2')       # 27
    col.name="DetLike2"
    col.set_format("F12.6")
    col.description="Detection likelihood measured by PSF-fitting; 0.6-2.3 keV (DET_LIKE_2)"

    col = table.get_column('ML_RATE_2')        # 28
    col.name="MLRate2"
    col.set_format("F8.6")
    col.description="Source count rate measured by PSF-fitting; 0.6-2.3 keV (ML_RATE_2)"

    col = table.get_column('ML_RATE_ERR_2')    # 29
    col.name="e_MLRate2"
    col.set_format("F8.6")
    col.description="1 sigma count rate error; 0.6-2.3 keV (ML_RATE_ERR_2)"

    col = table.get_column('ML_CTS_2')         # 30
    col.name="MLCts2"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 0.6-2.3 keV (ML_CTS_2)"

    col = table.get_column('ML_CTS_ERR_2')     # 31
    col.name="e_MLCts2"
    col.set_format("F11.6")
    col.description="1 sigma counts error; 0.6-2.3 keV (ML_CTS_ERR_2)"

    col = table.get_column('ML_FLUX_2')        # 32
    col.name="MLFlux2"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 0.6-2.3 keV (ML_FLUX_2)"

    col = table.get_column('ML_FLUX_ERR_2')    # 33
    col.name="e_MLFlux2"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 0.6-2.3 keV (ML_FLUX_ERR_2)"

    col = table.get_column('ML_EXP_2')         # 34
    col.name="MLExp2"
    col.set_format("E12.6")
    col.description="Vignetted exposure value; 0.6-2.3 keV (ML_EXP_2)"

    col = table.get_column('ML_BKG_2')         # 35
    col.name="MLBkg2"
    col.set_format("F12.6")
    col.description="Background at the source position; 0.6-2.3 keV (ML_BKG_2)"

    col = table.get_column('DET_LIKE_3')       # 36
    col.name="DetLike3"
    col.set_format("F12.6")
    col.description="Detection likelihood measured by PSF-fitting; 2.3-5 keV (DET_LIKE_3)"

    col = table.get_column('ML_RATE_3')        # 37
    col.name="MLRate3"
    col.set_format("F8.6")
    col.description="Source count rate measured by PSF-fitting; 2.3-5 keV (ML_RATE_3)"

    col = table.get_column('ML_RATE_ERR_3')    # 38
    col.name="e_MLRate3"
    col.set_format("F8.6")
    col.description="1 sigma count rate error; 2.3-5 keV (ML_RATE_ERR_3)"

    col = table.get_column('ML_CTS_3')         # 39
    col.name="MLCts3"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 2.3-5 keV (ML_CTS_3)"

    col = table.get_column('ML_CTS_ERR_3')     # 40
    col.name="e_MLCts3"
    col.set_format("F11.6")
    col.description="1 sigma counts error; 2.3-5 keV (ML_CTS_ERR_3)"

    col = table.get_column('ML_FLUX_3')        # 41
    col.name="MLFlux3"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 2.3-5 keV (ML_FLUX_3)"

    col = table.get_column('ML_FLUX_ERR_3')    # 42
    col.name="e_MLFlux3"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 2.3-5 keV (ML_FLUX_ERR_3)"

    col = table.get_column('ML_EXP_3')         # 43
    col.name="MLExp3"
    col.set_format("E12.6")
    col.description="Vignetted exposure value; 2.3-5 keV (ML_EXP_3)"

    col = table.get_column('ML_BKG_3')         # 44
    col.name="MLBkg3"
    col.set_format("F12.6")
    col.description="Background at the source position; 2.3-5 keV (ML_BKG_3)"

    col = table.get_column('DET_LIKE_4')       # 45
    col.name="DetLike4"
    col.set_format("F12.6")
    col.description="Detection likelihood measured by PSF-fitting; 5-8 keV (DET_LIKE_4)"

    col = table.get_column('ML_RATE_4')        # 46
    col.name="MLRate4"
    col.set_format("F8.6")
    col.description="Source count rate measured by PSF-fitting; 5-8 keV (ML_RATE_4)"

    col = table.get_column('ML_RATE_ERR_4')    # 47
    col.name="e_MLRate4"
    col.set_format("F8.6")
    col.description="1 sigma count rate error; 5-8 keV (ML_RATE_ERR_4)"

    col = table.get_column('ML_CTS_4')         # 48
    col.name="MLCts4"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 5-8 keV (ML_CTS_4)"

    col = table.get_column('ML_CTS_ERR_4')     # 49
    col.name="e_MLCts4"
    col.set_format("F11.6")
    col.description="1 sigma counts error; 5-8 keV (ML_CTS_ERR_4)"

    col = table.get_column('ML_FLUX_4')        # 50
    col.name="MLFLux4"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 5-8 keV (ML_FLUX_4)"

    col = table.get_column('ML_FLUX_ERR_4')    # 51
    col.name="e_MLFlux4"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 5-8 keV (ML_FLUX_ERR_4)"

    col = table.get_column('ML_EXP_4')         # 52
    col.name="MLExp4"
    col.set_format("E12.6")
    col.description="Vignetted exposure value; 5-8 keV (ML_EXP_4)"

    col = table.get_column('ML_BKG_4')         # 53
    col.name="MLBkg4"
    col.set_format("F12.6")
    col.description="Background at the source position; 5-8 keV (ML_BKG_4)"

    #####################################
    """ Read 4XMM-DR12 forced catalog """
    #####################################
    print("Reading {}".format(infile2))

    r = Table.read(infile2)
    tab2=Table([
            r['DR12_IAU_NAME'],                                         # 0
            r['DR12_SRCID'],                                            # 1
            r['DR12_RA'],                                               # 2
            r['DR12_DEC'],                                              # 3
            r['DR12_RADEC_ERR'],                                        # 4
            r['DET_LIKE_0']   ,   # 5
            r['DET_LIKE_1']    ,   # 6
            r['DET_LIKE_2']    ,   # 7
            r['DET_LIKE_3']    ,   # 8
            r['DET_LIKE_4']    ,   # 9
            r['ML_RATE_0']     ,   # 10
            r['ML_RATE_1']     ,   # 11
            r['ML_RATE_2']     ,   # 12
            r['ML_RATE_3']     ,   # 13
            r['ML_RATE_4']     ,   # 14
            r['ML_RATE_ERR_0']     ,   # 15
            r['ML_RATE_ERR_1']     ,   # 16
            r['ML_RATE_ERR_2']     ,   # 17
            r['ML_RATE_ERR_3']     ,   # 18
            r['ML_RATE_ERR_4']     ,   # 19
            r['ML_CTS_0']     ,   # 20 
            r['ML_CTS_1']     ,   # 21
            r['ML_CTS_2']     ,   # 22
            r['ML_CTS_3']     ,   # 23
            r['ML_CTS_4']     ,   # 24
            r['ML_CTS_ERR_0']     ,   # 25 
            r['ML_CTS_ERR_1']     ,   # 26 
            r['ML_CTS_ERR_2']     ,   # 27 
            r['ML_CTS_ERR_3']     ,   # 28 
            r['ML_CTS_ERR_4']     ,   # 29 
            r['ML_FLUX_0']     ,   # 30
            r['ML_FLUX_1']     ,   # 31
            r['ML_FLUX_2']     ,   # 32
            r['ML_FLUX_3']     ,   # 33
            r['ML_FLUX_4']     ,   # 34
            r['ML_FLUX_ERR_0']     ,   # 35
            r['ML_FLUX_ERR_1']     ,   # 36
            r['ML_FLUX_ERR_2']     ,   # 37
            r['ML_FLUX_ERR_3']     ,   # 38
            r['ML_FLUX_ERR_4']     ,   # 39
            r['ML_EXP_0']     ,   # 40
            r['ML_EXP_1']     ,   # 41
            r['ML_EXP_2']     ,   # 42
            r['ML_EXP_3']     ,   # 43
            r['ML_EXP_4']     ,   # 44
            r['ML_BKG_0']     ,   # 45
            r['ML_BKG_1']     ,   # 46
            r['ML_BKG_2']     ,   # 47
            r['ML_BKG_3']     ,   # 48
            r['ML_BKG_4']     ,   # 49
            r['CONF'],   # 50
    ])

    table2 = tablemaker.addTable(tab2, name=outfile2, description="eUDS forced photometry of 4XMM-DR12 sources, converted from eUDS_4XMM-DR12.fits")

    col = table2.get_column('DR12_IAU_NAME')                                         # 0
    col.name="DR12Name"
    col.set_format("A21")
    col.description="4XMM-DR12 source IAU name (DR12_IAU_NAME)"

    col = table2.get_column('DR12_SRCID')                                            # 1
    col.name="DR12SrcID"
    col.set_format("I15")
    col.description="Source ID (64-bit integer, DR12_SRCID)"

    col = table2.get_column('DR12_RA')                                               # 2
    col.name="RAdeg"
    col.set_format("F10.6")
    col.description="Right ascension (J2000, DR12_RA)"

    col = table2.get_column('DR12_DEC')                                              # 3
    col.name="DEdeg"
    col.set_format("F10.6")
    col.description="Declination (J2000, DR12_DEC)"

    col = table2.get_column('DR12_RADEC_ERR')                                        # 4
    col.name="ePos"
    col.set_format("F6.3")
    col.description="Positional error (DR12_RADEC_ERR)"

    col = table2.get_column('DET_LIKE_0')      # 5
    col.name="DetLike0"
    col.set_format("F12.6")
    col.description="Detection likelihood; 0.3-2.3 keV (DET_LIKE_0)"

    col = table2.get_column('DET_LIKE_1')       # 6
    col.name="DetLike1"
    col.set_format("F12.6")
    col.description="Detection likelihood; 0.3-0.6 keV (DET_LIKE_1)"

    col = table2.get_column('DET_LIKE_2')       # 7
    col.name="DetLike2"
    col.set_format("F12.6")
    col.description="Detection likelihood; 0.6-2.3 keV (DET_LIKE_2)"

    col = table2.get_column('DET_LIKE_3')       # 8
    col.name="DetLike3"
    col.set_format("F12.6")
    col.description="Detection likelihood; 2.3-5 keV (DET_LIKE_3)"

    col = table2.get_column('DET_LIKE_4')       # 9
    col.name="DetLike4"
    col.set_format("F12.6")
    col.description="Detection likelihood; 5-8 keV (DET_LIKE_4)"

    col = table2.get_column('ML_RATE_0')        # 10
    col.name="MLRate0"
    col.set_format("F10.6")
    col.description="Source count rate measured by PSF-fitting; 0.3-2.3 keV (ML_RATE_0)"

    col = table2.get_column('ML_RATE_1')        # 11
    col.name="MLRate1"
    col.set_format("F10.6")
    col.description="Source count rate measured by PSF-fitting; 0.3-0.6 keV (ML_RATE_1)"

    col = table2.get_column('ML_RATE_2')        # 12
    col.name="MLRate2"
    col.set_format("F10.6")
    col.description="Source count rate measured by PSF-fitting; 0.6-2.3 keV (ML_RATE_2)"

    col = table2.get_column('ML_RATE_3')        # 13
    col.name="MLRate3"
    col.set_format("F10.6")
    col.description="Source count rate measured by PSF-fitting; 2.3-5 keV (ML_RATE_3)"

    col = table2.get_column('ML_RATE_4')        # 14
    col.name="MLRate4"
    col.set_format("F10.6")
    col.description="Source count rate measured by PSF-fitting; 5-8 keV (ML_RATE_4)"

    col = table2.get_column('ML_RATE_ERR_0')        # 15
    col.name="e_MLRate0"
    col.set_format("F10.6")
    col.description="1 sigma count rate error; 0.3-2.3 keV (ML_RATE_ERR_0)"

    col = table2.get_column('ML_RATE_ERR_1')        # 16
    col.name="e_MLRate1"
    col.set_format("F10.6")
    col.description="1 sigma count rate error; 0.3-0.6 keV (ML_RATE_ERR_1)"

    col = table2.get_column('ML_RATE_ERR_2')        # 17
    col.name="e_MLRate2"
    col.set_format("F10.6")
    col.description="1 sigma count rate error; 0.6-2.3 keV (ML_RATE_ERR_2)"

    col = table2.get_column('ML_RATE_ERR_3')        # 18
    col.name="e_MLRate3"
    col.set_format("F10.6")
    col.description="1 sigma count rate error; 2.3-5 keV (ML_RATE_ERR_3)"

    col = table2.get_column('ML_RATE_ERR_4')        # 19
    col.name="e_MLRate4"
    col.set_format("F10.6")
    col.description="1 sigma count rate error; 5-8 keV (ML_RATE_ERR_4)"

    col = table2.get_column('ML_CTS_0')        # 20 
    col.name="MLCts0"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 0.3-2.3 keV (ML_CTS_0)"

    col = table2.get_column('ML_CTS_1')        # 21
    col.name="MLCts1"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 0.3-0.6 keV (ML_CTS_1)"

    col = table2.get_column('ML_CTS_2')        # 22
    col.name="MLCts2"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 0.6-2.3 keV (ML_CTS_2)"

    col = table2.get_column('ML_CTS_3')        # 23
    col.name="MLCts3"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 2.3-5 keV (ML_CTS_3)"

    col = table2.get_column('ML_CTS_4')        # 24
    col.name="MLCts4"
    col.set_format("F11.6")
    col.description="Source net counts measured from count rate; 5-8 keV (ML_CTS_4)"

    col = table2.get_column('ML_CTS_ERR_0')        # 25 
    col.name="e_MLCts0"
    col.set_format("F12.6")
    col.description="1 sigma counts error; 0.3-2.3 keV (ML_CTS_ERR_0)"

    col = table2.get_column('ML_CTS_ERR_1')        # 26 
    col.name="e_MLCts1"
    col.set_format("F12.6")
    col.description="1 sigma counts error; 0.3-0.6 keV (ML_CTS_ERR_1)"

    col = table2.get_column('ML_CTS_ERR_2')        # 27 
    col.name="e_MLCts2"
    col.set_format("F12.6")
    col.description="1 sigma counts error; 0.6-2.3 keV (ML_CTS_ERR_2)"

    col = table2.get_column('ML_CTS_ERR_3')        # 28 
    col.name="e_MLCts3"
    col.set_format("F12.6")
    col.description="1 sigma counts error; 2.3-5 keV (ML_CTS_ERR_3)"

    col = table2.get_column('ML_CTS_ERR_4')        # 29 
    col.name="e_MLCts4"
    col.set_format("F12.6")
    col.description="1 sigma counts error; 5-8 keV (ML_CTS_ERR_4)"

    col = table2.get_column('ML_FLUX_0')        # 30
    col.name="MLFlux0"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 0.3-2.3 keV (ML_FLUX_0)"

    col = table2.get_column('ML_FLUX_1')        # 31
    col.name="MLFlux1"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 0.3-0.6 keV (ML_FLUX_1)"

    col = table2.get_column('ML_FLUX_2')        # 32
    col.name="MLFlux2"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 0.6-2.3 keV (ML_FLUX_2)"

    col = table2.get_column('ML_FLUX_3')        # 33
    col.name="MLFlux3"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 2.3-5 keV (ML_FLUX_3)"

    col = table2.get_column('ML_FLUX_4')        # 34
    col.name="MLFlux4"
    col.set_format("E12.6")
    col.description="Source flux converted from count rate; 5-8 keV (ML_FLUX_4)"

    col = table2.get_column('ML_FLUX_ERR_0')        # 35
    col.name="e_MLFlux0"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 0.3-2.3 keV (ML_FLUX_ERR_0)"

    col = table2.get_column('ML_FLUX_ERR_1')        # 36
    col.name="e_MLFlux1"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 0.3-0.6 keV (ML_FLUX_ERR_1)"

    col = table2.get_column('ML_FLUX_ERR_2')        # 37
    col.name="e_MLFlux2"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 0.6-2.3 keV (ML_FLUX_ERR_2)"

    col = table2.get_column('ML_FLUX_ERR_3')        # 38
    col.name="e_MLFlux3"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 2.3-5 keV (ML_FLUX_ERR_3)"

    col = table2.get_column('ML_FLUX_ERR_4')        # 39
    col.name="e_MLFlux4"
    col.set_format("E12.6")
    col.description="1 sigma flux error; 5-8 keV (ML_FLUX_ERR_4)"

    col = table2.get_column('ML_EXP_0')        # 40
    col.name="MLExp0"
    col.set_format("E10.6")
    col.description="Vignetted exposure value; 0.3-2.3 keV (ML_EXP_0)"

    col = table2.get_column('ML_EXP_1')        # 41
    col.name="MLExp1"
    col.set_format("E10.6")
    col.description="Vignetted exposure value; 0.3-0.6 keV (ML_EXP_1)"

    col = table2.get_column('ML_EXP_2')        # 42
    col.name="MLExp2"
    col.set_format("E10.6")
    col.description="Vignetted exposure value; 0.6-2.3 keV (ML_EXP_2)"

    col = table2.get_column('ML_EXP_3')        # 43
    col.name="MLExp3"
    col.set_format("E10.6")
    col.description="Vignetted exposure value; 2.3-5 keV (ML_EXP_3)"

    col = table2.get_column('ML_EXP_4')        # 44
    col.name="MLExp4"
    col.set_format("E10.6")
    col.description="Vignetted exposure value; 5-8 keV (ML_EXP_4)"

    col = table2.get_column('ML_BKG_0')        # 45
    col.name="MLBkg0"
    col.set_format("F12.6")
    col.description="Background at the source position; 0.3-2.3 keV (ML_BKG_0)"

    col = table2.get_column('ML_BKG_1')        # 46
    col.name="MLBkg1"
    col.set_format("F12.6")
    col.description="Background at the source position; 0.3-0.6 keV (ML_BKG_1)"

    col = table2.get_column('ML_BKG_2')        # 47
    col.name="MLBkg2"
    col.set_format("F12.6")
    col.description="Background at the source position; 0.6-2.3 keV (ML_BKG_2)"

    col = table2.get_column('ML_BKG_3')        # 48
    col.name="MLBkg3"
    col.set_format("F12.6")
    col.description="Background at the source position; 2.3-5 keV (ML_BKG_3)"

    col = table2.get_column('ML_BKG_4')        # 49
    col.name="MLBkg4"
    col.set_format("F12.6")
    col.description="Background at the source position; 5-8 keV (ML_BKG_4)"

    col = table2.get_column('CONF')   # 50
    col.name="Conf"
    col.set_format("I1")
    col.description="True if source is located within 60 arcsec from another source with 0.3-2.3 keV flux > 1E−14 erg/s/cm**2 (CONF)"

    tablemaker.writeCDSTables()

    # Customize ReadMe output
    
    tablemaker.title = "eUDS: The SRG/eROSITA X-ray Survey of the UKIDSS Ultra Deep Survey Field. Catalogue of Sources."
    tablemaker.author = 'Krivonos R.'
    tablemaker.authors="Gilfanov M., Medvedev P., Sazonov S., Sunyaev R."
    tablemaker.date = 2024
    tablemaker.bibcode = "2024MNRAS.528.1264K"
    tablemaker.keywords = "catalogues - surveys - X-ray: general"
    tablemaker.abstract = "The eROSITA X-ray telescope on board the Spectrum-Roentgen-Gamma (SRG) spacecraft observed the field of the UKIDSS Ultra-Deep Survey (UDS) in August-September 2019, during its flight to Sun-Earth L2 point. The resulting eROSITA UDS (or eUDS) survey was thus the first eROSITA X-ray imaging survey, which demonstrated the capability of the telescope to perform uniform observations of large sky areas. With a moderate single-camera exposure of 150 ks, eUDS covered ∼5 deg^2^ with the limiting flux ranging between 4*10^-15^ and 5*10^-14^erg/cm^2^/s in the 0.3-2.3 keV band. We present a catalogue of 647 sources detected at likelihood >10 (∼4sigma) during the eUDS. The catalogue provides information on the source fluxes in the main energy band 0.3-2.3 keV and forced photometry in a number of bands between 0.3 and 8 keV. Using the deeper 4XMM-DR12 catalogue, we have identified 22 strongly variable objects that have brightened or faded by at least a factor of ten during the eROSITA observations compared to previous observations by XMM-Newton. We also provide a catalogue of 22 sources detected by eROSITA in the hard energy band of 2.3-5 keV."
    tablemaker.more_description = "The catalogue (fits/eUDS.fits) contains 647 sources detected in the 0.3-2.3 keV band using eSASS adopting a detection likelihood threshold of 10. The catalogue provides information on the source fluxes in the main energy band 0: 0.3-2.3 keV and forced photometry in 1: 0.3-0.6, 2: 0.6-2.3, 3: 2.3-5.0 and 4: 5.0-8.0 keV bands. The catalogue of forced PSF-fitting fluxes (fits/eUDS_4XMM-DR12.fits) contains 3603 XMM-Newton sources from 4XMM-DR12 catalogue with the eUDS exposure of more than 100 s. The catalogue provides information on the source forced photometry in the energy bands 0: 0.3-2.3, 1: 0.3-0.6, 2: 0.6-2.3, 3: 2.3-5.0 and 4: 5.0-8.0 keV. To warn about the potential impact of spatial confusion with a nearby bright source, we added a confusion flag (see original paper for details)."
    
    tablemaker.putRef("J/A+A/661/A1","The eFEDS X-ray catalogs (Brunner+, 2022)")
    tablemaker.putRef("J/A+A/661/A38","SRG/ART-XC 1st year all-sky X-ray survey (Pavlinsky+, 2022)")
    tablemaker.putRef("https://www.srg.cosmos.ru/eUDS","eUDS catalog")
    #tablemaker.makeReadMe()
    with open("ReadMe.tmp","w") as fd:
        tablemaker.makeReadMe(out=fd)

make_euds_cds(infile1="../uds/products/eUDS.fits",outfile1='table1.dat',
              infile2="../uds/products/eUDS_4XMM-DR12.fits",outfile2='table2.dat')
