#!/usr/bin/env python
"""
НАЗВАНИЕ:

 05_srctool.py
 
 
НАЗНАЧЕНИЕ:

   Запускает scrtool для самого широкого канала 0.2-10 кэВ, чтобы спектры имели самое полное покрытие по энергиям. Список источников берется из 0.3-2.3 кэВ.

ВЫЗОВ:

   esass
   ./05_srctool.py


УПРАВЛЕНИЕ:

   Требуется запуск предыдущего скрипта 04_mosaics.py
 
ПАРАМЕТРЫ:

   index=4 : Выбранный энергетический диапазон


ВЫВОД:
 
   Выходные файлы записываются в директорию outfile_dir/srctool_dir
  

ИСТОРИЯ:

   Роман Кривонос, ИКИ РАН, krivonos@cosmos.ru
   Март 2023
   
"""

from astropy.wcs import WCS
from astropy.io import fits
import sys, os, os.path, time, subprocess
from pathlib import Path
import numpy as np
import glob
from os.path import dirname
import inspect
import pickle

import uds

from uds.utils import *
from uds.config import *
from uds.sherpa import *


""" find UDS root dir """
#root_path=dirname(dirname(dirname(inspect.getfile(uds))))
"""
ftools does not like long file path names,
for this reason, we use relative path here
"""
root_path='..'
print("UDS root path: {}".format(root_path))

infile_dir=root_path+'/data/processed'
outfile_dir=root_path+'/products'
create_folder(outfile_dir)

srctool_dir="{}/{}".format(outfile_dir,"srctool-products")
create_folder(srctool_dir)

outkey="tm0"

outfile_srctool="{}_SrcTool_".format(outkey)

do_init          = False
do_merge         = False
do_srctool       = False
do_grppha        = False
do_ecf_calc      = False # for all bands
do_ecf_print     = False # for all bands
do_flux_calc     = False # for all bands

do_catalog       = False
do_extended      = False
do_ds9reg        = False
do_euds_final    = False
do_euds_dr12     = False # crossmatch eUDS with DR12
do_euds_stat     = False
do_euds_cds      = False

do_xmm_catalog   = False
do_xmm_final     = False
do_xmm_xmatch    = False
do_xmm_ds9reg    = False
do_xmm_cds       = False
do_euds_cosmatch = True

do_cross_check   = False # Check whether all E3,E6 sources are detected in E0 


index=0
""" чтобы спектры покрывали все энергии работаем в диапазоне 5 """

vign=True
vignetting = 'vign' if (vign==True) else 'novign'

events=[]
expmaps=[]
bkgmaps=[]
for tmkey in keylist_tm.keys():
    print("TM{} in work... init events".format(tmkey))
    for datakey in keylist_tm[tmkey]:
        print("--> {}".format(datakey))
        """ Подготавливаем списки событий индивидуальных наблюдений """
        outfile_evtool,outfile_expmap=init_events(key=datakey,attcorr=True,
                                                  eband_index=eband[index],
                                                  infile_dir=infile_dir,
                                                  outfile_dir=outfile_dir,
                                                  do_init=do_init,
                                                  do_obsmode=False,
                                                  do_center=False,
                                                  do_evtool=False,
                                                  do_expmap=False,
                                                  vign=vign,
                                                  ra_cen=ra_cen, de_cen=de_cen,
                                                  emin_kev=emin_kev[index],
                                                  emax_kev=emax_kev[index])
        events.append(outfile_evtool)
        expmaps.append(outfile_expmap)

   
""" Собираем общий список событий """    
outfile_evtool="{}_EventList_en{}.fits".format(os.path.join(outfile_dir,outkey),
                                               eband[index])

if(do_merge==True):
    do_evtool_esass(events=events, outfile=outfile_evtool)


suffix_srctool=".fits"
""" Output filename suffix - all output filenames appended with this string.
If suffix contains no filename extension (does not contain a "."), then ".fits"
is also appended to the filename. """

catprep="{}_SourceCatalog_en{}{}".format(os.path.join(outfile_dir,outkey), eband[0], outfile_post)
""" take source catalog from 0.3-2.3 keV band """

if not (os.path.isfile(catprep)==True):
    print("{} not found, run 04_mosaics.py?".format(catprep))
    sys.exit()


if(do_srctool==True):
    test_exe('srctool')
    cmd=['srctool',
         "insts=\'1 5 6 7\'",
         "eventfiles={}".format(outfile_evtool),
         "prefix=\'{}\'".format(os.path.join(srctool_dir,outfile_srctool)),
         "suffix=\'{}\'".format(suffix_srctool),
         "srccoord={}".format("../products/eUDS_for_srctool.fits"),
         # the same as original file eUDS.fits, but changed ML_CTS --> ML_CTS_0, ML_BKG --> ML_BKG_0, ML_EXP --> ML_EXP_0
         #"srcreg=\'fk5;circle * * 60s\'",
         #"backreg=\'fk5;annulus * * 90s 120s\'",
         "srcreg=AUTO",
         "backreg=AUTO",
         "clobber=yes",]
    print((" ").join(cmd))
    #os.system((" ").join(cmd))
    #print((" ").join(cmd))
    
if(do_grppha==True):
    group_spectra("{}/*020_SourceSpec_*.fits".format(srctool_dir))

ecfout="{}_SampleFlux_v1.pickle".format(os.path.join(outfile_dir,outkey))

if(do_ecf_calc==True):
    calc_ecf("{}/tm0_SrcTool_020_ARF_?????.fits".format(srctool_dir),
             catprep=catprep, emin=emin_kev, emax=emax_kev, eband=eband, outfile=ecfout, simnum=10000)

if(do_ecf_print==True):
    print_ecf(infile=ecfout, emin=emin_kev, emax=emax_kev, eband=eband, skipfrac=10.0)

fluxout="{}_SherpaFlux.pickle".format(os.path.join(outfile_dir,outkey))
if(do_flux_calc==True):
    calc_flux("{}/tm0_SrcTool_020_ARF_?????.fits".format(srctool_dir),
             catprep=catprep, emin=emin_kev, emax=emax_kev, eband=eband, outfile=ecfout, simnum=100)


    
#index=0

catprep="{}_SourceCatalog_en{}{}".format(os.path.join(outfile_dir,outkey), eband[index], outfile_post)
rawcat="{}_SourceCatalog_en{}.pickle".format(os.path.join(outfile_dir,outkey), eband[index])
if(do_catalog==True):
    forced_xmm_sources="{}_MaxLikSourceList_en{}.xmm.pickle".format(os.path.join(outfile_dir,outkey), eband[index])
    with open(forced_xmm_sources, 'rb') as f:
        print("Reading forced XMM sources from {}".format(forced_xmm_sources))
        srcs_forced = pickle.load(f)
        print()
        print(srcs_forced)
        print()
    
    make_euds_catalog(infile='../products/tm0_SourceCatalog_en0.forced.fits', rawcat=rawcat, dlmin=10.0, dlmax=100000, ext_like=1000,
                 emin=emin_kev[index], emax=emax_kev[index], eband=eband[index],
                 infile_en00cat=catprep,
                 infile_en01cat='../products/tm0_SourceCatalog_en1.forced.fits',
                 infile_en02cat='../products/tm0_SourceCatalog_en2.forced.fits',
                 infile_en03cat='../products/tm0_SourceCatalog_en3.forced.fits',
                 infile_en06cat='../products/tm0_SourceCatalog_en6.forced.fits',
                 infile_en00sens='../products/tm0_SensitivityMap_dl10_en0.fits',
                 infile_en01sens='../products/tm0_SensitivityMap_dl10_en1.fits',
                 infile_en02sens='../products/tm0_SensitivityMap_dl10_en2.fits',
                 infile_en03sens='../products/tm0_SensitivityMap_dl10_en3.fits',
                 infile_en06sens='../products/tm0_SensitivityMap_dl10_en6.fits',
                 srcs_forced=srcs_forced,
                 )


if(do_extended==True):
    make_extended(infile=rawcat,outreg="{}_ExtendedCat_en{}.reg".format(os.path.join(outfile_dir,outkey), eband[index]))

if(do_ds9reg==True):
    #make_final_ds9reg(infile=rawcat,outreg="{}_FinalCat_dl10.reg".format(os.path.join(outfile_dir,outkey)))
    make_final_ds9reg(infile=rawcat,scale=(60*60)/10,outreg="{}_FinalCat_dl10_talk.reg".format(os.path.join(outfile_dir,outkey)))

if(do_euds_final==True):
    """ make final eUDS catalog """
    final_euds_catalog(infile=rawcat, outfile_fits='../products/eUDS.fits')

if(do_euds_dr12==True):
    crossmatch_dr12('../products/eUDS.fits', catalog=root_path+"/data/4XMM-DR12/4XMM_DR12cat_slim_v1.0_UDS.fits.catalog", devmax=15)


    
if(do_euds_stat==True):
    make_euds_stat(infile="../products/eUDS.fits",fluxlim=5e-14)

if(do_euds_cds==True):
    make_euds_cds(infile="../products/eUDS.fits",outfile='../products/eUDS.cds')

if(do_cross_check==True):
    """ cross check final eUDS catalog """
    cross_check_euds(infile=catprep, euds='../products/eUDS.fits', outkey="../products/en{}_FinalCat_dl10".format(index))

if(do_xmm_catalog==True):
    """ complile raw forced XMM catalog """
    make_xmm_catalog(infile_en00cat='../products/tm0_SourceCatalog_en0.forced-xmm.fits',
                     infile_en01cat='../products/tm0_SourceCatalog_en1.forced-xmm.fits',
                     infile_en02cat='../products/tm0_SourceCatalog_en2.forced-xmm.fits',
                     infile_en03cat='../products/tm0_SourceCatalog_en3.forced-xmm.fits',
                     infile_en06cat='../products/tm0_SourceCatalog_en6.forced-xmm.fits',
                     forced_xmm_sources='../products/tm0_MaxLikSourceList_en0.fixed-xmm.pickle',
                     outfile='../products/tm0_4XMM-DR12.pickle')
    

if(do_xmm_final==True):
    """ make final XMM-forced catalog """
    final_xmm_catalog(infile='../products/tm0_4XMM-DR12.pickle', outfile_fits='../products/eUDS_4XMM-DR12.fits')
   
if(do_xmm_xmatch==True):
    """ cross-match XMM-forced catalog
    outfile_cvs contains 0.3-2.3 keV eUDS flux (col1=flux,col2=err) vs. 4XMM-DR12 flux (col3=flux, col4=err)
    XMM flux was converted from 0.2-2.0 keV to 0.3-2.3 keV using wabs*powerlow with wabs=0.02 gamma=2.0
    """
    final_xmm_xmatch(infile='../products/eUDS_4XMM-DR12.fits',
                     xmmslim='../data/4XMM-DR12/4XMM_DR12cat_slim_v1.0_UDS.fits.catalog',
                     xmmfull='../data/4XMM-DR12/4XMM_DR12cat_v1.0_UDS.fits.catalog',
                     xmmlim=2e-14,
                     outfile_flux="../products/eUDS_4XMM-DR12.flux.csv")
   
if(do_xmm_ds9reg==True):
    """ show XMM-forced catalog """
    make_xmm_ds9reg_confused(infile='../products/eUDS_4XMM-DR12.fits', outfile='../products/eUDS_4XMM-DR12.confused.reg')

""" obsolete """
if(do_xmm_cds==True):
    make_xmm_cds(infile="../products/eUDS_4XMM-DR12.fits", outfile='../products/eUDS_4XMM-DR12.cds')



if(do_euds_cosmatch==True):
    """ prepare eUDS catalog for CosMatch (Mescheryakov) """
    make_euds_cosmatch(infile='../products/eUDS.fits', outfile='../products/eUDS-CosMatch.fits')
