#!/usr/bin/env python
#
#  Copyright (C) 2019-2023, 2025  Smithsonian Astrophysical Observatory
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

'DAX wrapper around various image processing tasks'

import sys
import os

import subprocess as sp
from tempfile import NamedTemporaryFile

import ciao_contrib.logger_wrapper as lw
import dax.imgproc_wrapper as dax

lw.initialize_logger("dax")
lgr = lw.get_logger("dax")
lw.set_verbosity(0)


def map_tool_to_object(toolname, xpa, args):
    'Create object based on tool name'

    toolmap = {'dmimgblob': dax.Dmimgblob, 'dmimgadapt': dax.Dmimgadapt,
               'csmooth': dax.Csmooth, 'dmimgfilt': dax.Dmimgfilt,
               'dmimgthresh': dax.Dmimgthresh, 'dmnautilus': dax.Dmnautilus,
               'dmimgdist': dax.Dmimgdist, 'apowerspectrum': dax.Apowerspectrum,
               'acrosscorr_auto': dax.Autocorrelate, 'acrosscorr_x': dax.Crosscorrelate,
               'aconvolve': dax.Aconvolve,
               'dmregrid2': dax.Dmregrid2, 'dmimgcalc': dax.Dmimgcalc,
               'wavdetect': dax.Wavdetect, 'vtpdetect': dax.Vtpdetect,
               'celldetect': dax.Celldetect, 'get_src_region': dax.Getsrcregion,
               'dmimglasso': dax.Dmimglasso, 'dmcontour': dax.Dmcontour,
               'skyfov': dax.Skyfov, 'dmellipse': dax.Dmellipse,
               'dmimghull': dax.Dmimghull, 'dmimgproject': dax.Dmimgproject,
               'dmimghist': dax.Dmimghist, 'imgmoment': dax.Imgmoment,
               'dmstat': dax.Dmstat, 'arestore': dax.Arestore,
               'dmfilth': dax.Dmfilth, 'dmextract': dax.Dmextract,
               'dmimgpick': dax.Dmimgpick, 'ecf_calc': dax.Ecfcalc,
               'dither_region': dax.Ditherregion,
               'reproject_image': dax.Reprojectimage,
               'simulate_psf': dax.Simulatepsf, 'dmcoords': dax.Dmcoords,
               'psfsize_srcs': dax.Psfsizesrcs, 'src_psffrac': dax.Srcpsffrac,
               'glvary': dax.Glvary, 'pfold': dax.Pfold,
               'dme_pha': dax.Dmepha, 'dme_pi': dax.Dmepi, 'dme_time': dax.Dmetime,
               'dme_expno': dax.Dmeexpno, 'dme_generic': dax.Dmegeneric,
               'dmcopy': dax.Dmcopy, 'dmradar': dax.Dmradar,
               'psf_contour': dax.PsfContour, 'bkg_fixed_counts': dax.BkgFixedCounts,
               'functs': dax.Functs, 'srcflux': dax.Srcflux,
               'srcextent': dax.Srcextent}
    ToolClass = toolmap[toolname]
    tool = ToolClass(xpa, args)
    return tool


def doit():
    'Main routine'
    myxpa = sys.argv[1]
    toolname = sys.argv[2]
    tool = map_tool_to_object(toolname, myxpa, sys.argv[3:])
    tool.run()


if __name__ == '__main__':
    doit()
