#!/usr/bin/env python

from pysas.wrapper import Wrapper as w
import os, sys
from os.path import dirname
import inspect
import glob

import os.path
from os import path
import subprocess
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.table import Table
from matplotlib.colors import LogNorm

import subprocess

import pyds9

import arches
from arches.utils import *
from arches.config import *

root_path=dirname(dirname(dirname(inspect.getfile(arches))))
print("Arches root path: {}".format(root_path))

archive_dir=root_path+'/data/archive'
events_dir=root_path+'/data/processed'
events_oot_dir=root_path+'/data/processed-oot'
products_dir=root_path+'/products/esas'
ds9reg_dir=root_path+'/data/ds9reg'

fast_track=True

create_folder(products_dir)

inargs = ['--version']
t = w('sasver', inargs)
t.run()

files = glob.glob(archive_dir+'/086*')

for obsid in files:
    obsid = os.path.basename(obsid)

    if(obsid in skip):
        continue

    work_dir = init_work_dir(obsid, products_dir=products_dir)
    print(f"\n*** jump to {work_dir} ***\n")
    os.chdir(work_dir)
    

    """ energy limits for all cameras """
    pi_min=1000
    pi_max=10000

    """ EPN """
    search_str = f'{events_dir}/{obsid}/????_{obsid}_EPN_S???_ImagingEvts.ds'
    epfiles = glob.glob(search_str)
    if not (epfiles):
        print("*** run 01_init_events.py ***")
        sys.exit()    
    eventfile, = epfiles
    unfilt_image = eventfile.replace("ImagingEvts.ds","diag-det-unfilt.fits")
    expression = f'#XMMEA_EP&&(PATTERN<=4)&&(PI in [{pi_min}:{pi_max}])'  # event filter expression
    inargs = [f'table={eventfile}',
           'withimageset=yes', f'imageset={unfilt_image}',
           'filtertype=expression', f'expression={expression}',
           'ignorelegallimits=yes', 'imagebinning=imageSize',
           'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
           'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
    w('evselect', inargs).run()
    
    d = pyds9.DS9()
    d.set("file "+unfilt_image)
    d.set('cmap bb')
    d.set('scale log')

    if (fast_track):
        pass
    else:
        print(f"\nEPN, ObsID: {obsid}")
        user_input = input()

    
    """
    try:
        print(f"\n### espfilt EPN {obsid} ###\n")    
        w('espfilt', [f'eventfile={eventfile}','elow=2500','ehigh=3500','withsmoothing=yes', 'smooth=51',
                      'rangescale=25', 'allowsigma=2.0', 'method=histogram', 'keepinterfiles=true',
                      'withoot=no',]).run()
        
    except Exception as e:
        print(f"An error occurred: {e}")
        pass
    """
    #sys.exit()

    
    """ EPN OOT """
    search_str = f'{events_oot_dir}/{obsid}/????_{obsid}_EPN_S???_ImagingEvts.ds'
    epfiles = glob.glob(search_str)
    if not (epfiles):
        print("*** run 01_init_events-oot.py ***")
        sys.exit()    
    eventfile_oot, = epfiles
    unfilt_image = eventfile_oot.replace("ImagingEvts.ds","diag-det-unfilt.fits")
    expression = f'#XMMEA_EP&&(PATTERN<=4)&&(PI in [{pi_min}:{pi_max}])'  # event filter expression
    inargs = [f'table={eventfile_oot}',
           'withimageset=yes', f'imageset={unfilt_image}',
           'filtertype=expression', f'expression={expression}',
           'ignorelegallimits=yes', 'imagebinning=imageSize',
           'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
           'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
    w('evselect', inargs).run()

    d = pyds9.DS9()
    d.set("file "+unfilt_image)
    d.set('cmap bb')
    d.set('scale log')
    
    if (fast_track):
        pass
    else:
        print(f"\nEPN-OOT, ObsID: {obsid}")
        user_input = input()

    try:
        print(f"\n### espfilt EPN-OOT {obsid} ###\n")
        
        w('espfilt', [f'eventfile={eventfile}','elow=2500','ehigh=3500','withsmoothing=yes', 'smooth=51',
                      'rangescale=25', 'allowsigma=2.0', 'method=histogram', 'keepinterfiles=true',
                      'withoot=yes', f'ootfile={eventfile_oot}',]).run()
        
    except Exception as e:
        print(f"An error occurred: {e}")
        pass

    #sys.exit()
    
    """ MOS1 """
    imos=1
    search_str=f'{events_dir}/{obsid}/????_{obsid}_EMOS{imos}_S???_ImagingEvts.ds'
    emfiles = glob.glob(search_str)
    print(emfiles)
    if not (emfiles):
        print("*** run 01_init_events.py ***")
        sys.exit()    
    eventfile, = emfiles
    unfilt_image = eventfile.replace("ImagingEvts.ds","diag-det-unfilt.fits")
    expression = f'(PATTERN<=12)&&(PI in [{pi_min}:{pi_max}])&&((FLAG & 0x766aa000)==0)'
    inargs = [f'table={eventfile}',
           'withimageset=yes', f'imageset={unfilt_image}',
           'filtertype=expression', f'expression={expression}',
           'ignorelegallimits=yes', 'imagebinning=imageSize',
           'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
           'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
    w('evselect', inargs).run()

    try:
        w('emanom', [f'eventfile={eventfile}','keepcorner=no',]).run()
    except:
        pass
    
    d = pyds9.DS9()
    d.set("file "+unfilt_image)
    d.set('cmap bb')
    d.set('scale log')
    
    if (fast_track):
        pass
    else:
        print(f"\nMOS{imos}, ObsID: {obsid}")
        user_input = input()
    
    try:
        print(f"\n### espfilt MOS{imos} {obsid} ###\n")
        w('espfilt', [f'eventfile={eventfile}','elow=2500','ehigh=3500','withsmoothing=yes', 'smooth=51',
                     'rangescale=6.0', 'allowsigma=3.0', 'method=histogram', 'keepinterfiles=false']).run()
    except Exception as e:
        print(f"An error occurred: {e}")
        pass
    
    

    """ MOS2 """
    imos=2
    search_str=f'{events_dir}/{obsid}/????_{obsid}_EMOS{imos}_S???_ImagingEvts.ds'
    emfiles = glob.glob(search_str)
    print(emfiles)
    if not (emfiles):
        print("*** run 01_init_events.py ***")
        sys.exit()    
    eventfile, = emfiles
    unfilt_image = eventfile.replace("ImagingEvts.ds","diag-det-unfilt.fits")
    expression = f'(PATTERN<=12)&&(PI in [{pi_min}:{pi_max}])&&((FLAG & 0x766aa000)==0)'
    inargs = [f'table={eventfile}',
           'withimageset=yes', f'imageset={unfilt_image}',
           'filtertype=expression', f'expression={expression}',
           'ignorelegallimits=yes', 'imagebinning=imageSize',
           'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
           'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
    w('evselect', inargs).run()

    try:
        w('emanom', [f'eventfile={eventfile}','keepcorner=no',]).run()
    except:
        pass
    
    d = pyds9.DS9()
    d.set("file "+unfilt_image)
    d.set('cmap bb')
    d.set('scale log')

    if (fast_track):
        pass
    else:
        print(f"\nMOS{imos}, ObsID: {obsid}")
        user_input = input()

    try:
        print(f"\n### espfilt MOS{imos} {obsid} ###\n")
        w('espfilt', [f'eventfile={eventfile}','elow=2500','ehigh=3500','withsmoothing=yes', 'smooth=51',
                      'rangescale=6.0', 'allowsigma=3.0', 'method=histogram','keepinterfiles=false']).run()
    except Exception as e:
        print(f"An error occurred: {e}")
        pass

    masks = [
        "*-fovlc.fits",
        #"*-corev.fits",
        "*-allimc.fits",
        "*-allim.fits",
        "*-anom.log",
        "*-corlc.fits",
        "*-corim.fits",
        "*-corimc.fits",
        "*-corevc.fits",
    ]
    rmfiles = [file for mask in masks for file in glob.glob(mask)]
    """
    for fn in rmfiles:
        print("rm ",fn)
        try:
            os.remove(fn)
        except:
            pass
    """
    #sys.exit()




    
