#!/usr/bin/env python
"""Создает начальные списки событий и помещает их в coma/data/processed

Оригинальные файлы со списками событий задаются в файлах coma/data/evtlists/*.txt
"""

import os
import inspect

from os.path import dirname

import coma
from coma.config import ra_cen, de_cen
from coma.utils import *

""" find root dir """
root_path=dirname(dirname(dirname(inspect.getfile(coma))))
print("root path: {}".format(root_path))

el=root_path+'/data/evtlists/'
pr=root_path+'/data/processed/'
create_folder(pr)

region="box({},{},12d,12d,0)".format(ra_cen,de_cen) # Selection region



tm_list=[1,2,3,4,5,6,7]
#tm_list=[1,]

do_partI        = False
do_partI_scans  = False
do_partII       = False
do_partII_scans = False
do_survey       = True

if(do_partI):
    for m in tm_list:
        print("tm{}_".format(m))
        do_evtool_esass(evlist=el+'tm{}_partI.txt'.format(m), outfile=pr+'tm{}_partI.fits'.format(m), gti='628781744.631552 628981200.', emin=0.2, emax=10.0, region=region, rmlock=True)

### Part I ###
partI=[628781744.631552,
       628819857.664192,
       628860287.683584,
       628900714.688512,
       628941140.612288,
       628981202.60192,]
scan=1
for i in range(len(partI)-1):
    print("partI scan={}".format(scan))
    tstart=partI[i]
    tstop=partI[i+1]
    if(do_partI_scans):
        for m in tm_list:
            print("*** tm{}_partI_scan{}".format(m,scan))
            do_evtool_esass(evlist=el+'tm{}_partI.txt'.format(m), outfile=pr+'tm{}_scan{}.fits'.format(m,scan), gti='{} {}'.format(tstart,tstop), emin=0.2, emax=10.0, region=region, rmlock=True)
    scan=scan+1
    
### Part II ###
if(do_partII):
    for m in tm_list:
        print("tm{}_".format(m))
        do_evtool_esass(evlist=el+'tm{}_partII.txt'.format(m), outfile=pr+'tm{}_partII.fits'.format(m), gti='645681013.670848 645832002.60928', emin=0.2, emax=10.0, region=region, rmlock=True)
    
partII=[645681013.670848,
        645746786.665408,
        645832002.60928]
for i in range(len(partII)-1):
    print("partII scan={}".format(scan))
    tstart=partII[i]
    tstop=partII[i+1]
    if(do_partII_scans):
        for m in tm_list:
            print("*** tm{}_partII_scan{}".format(m,scan))
            do_evtool_esass(evlist=el+'tm{}_partII.txt'.format(m), outfile=pr+'tm{}_scan{}.fits'.format(m,scan), gti='{} {}'.format(tstart,tstop), emin=0.2, emax=10.0, region=region, rmlock=True)
    scan=scan+1



### Survey ###
if(do_survey):
    for m in tm_list:
        do_evtool_esass(evlist=el+'tm{}_survey.txt'.format(m), outfile=pr+'tm{}_survey.fits'.format(m), emin=0.2, emax=10.0, rmlock=True)
        



