#!/usr/bin/env python3
#
# (c) 2017 Fetal-Neonatal Neuroimaging & Developmental Science Center
#                   Boston Children's Hospital
#
#              http://childrenshospital.org/FNNDSC/
#                        dev@babyMRI.org
#

import sys, os
sys.path.insert(1, os.path.join(os.path.dirname(__file__), '../pfdicom'))

import  pfdicom
from    argparse            import RawTextHelpFormatter
from    argparse            import ArgumentParser
import  pudb

import  pfmisc
from    pfmisc._colors      import Colors
from    pfmisc              import other

str_version = "1.1.2"
str_desc = Colors.CYAN + """

        __    _ _                     
       / _|  | (_)                    
 _ __ | |_ __| |_  ___ ___  _ __ ___  
| '_ \|  _/ _` | |/ __/ _ \| '_ ` _ \ 
| |_) | || (_| | | (_| (_) | | | | | |
| .__/|_| \__,_|_|\___\___/|_| |_| |_|
| |                                   
|_|                                   

  

                        Path-File DICOM Base Processor

        A common module/class for various downstream processing on DICOM
        files. This module reads a DICOM file, parses tags, and provides
        the data for additional processing.

                             -- version """ + \
             Colors.YELLOW + str_version + Colors.CYAN + """ --


""" + Colors.NO_COLOUR

def synopsis(ab_shortOnly = False):
    scriptName = os.path.basename(sys.argv[0])
    shortSynopsis =  '''
    NAME

	    %s - read a DICOM file and provide methods for further analysis.

    SYNOPSIS

            %s                                       \\
                    -I|--inputDir <inputDir>                \\
                    [-i|--inputFile <inputFile>]            \\
                    [-e|--extension <DICOMextension>]       \\
                    [-O|--outputDir <outputDir>]            \\
                    [-x|--man]                              \\
                    [-y|--synopsis]

    BRIEF EXAMPLE

	    %s -l tagList.txt -i slice.dcm

    ''' % (scriptName, scriptName, scriptName)

    description =  '''
    DESCRIPTION

        `%s` in and of itself provides minimal end value. This module/class
        is intended to be a building block for deeper functionality. Its
        purpose is to probe a given directory filesystem for DICOM files and 
        construct a tree representation (using pftree), and then, for each
        directory provide the means to read in a given DICOM file (using
        pydicom) to provide some minimal tag extraction and output file
        templating.

        Most importantly, derived classes of this parent class can provide
        detailed and powerful methods to process the directories containing
        DICOM files, saving results to an output file tree.

    ARGS

        -I|--inputDir <inputDir>
        Input DICOM directory to examine. By default, the first file in this
        directory is examined for its tag information. There is an implicit
        assumption that each <inputDir> contains a single DICOM series.

        -i|--inputFile <inputFile>
        An optional <inputFile> specified relative to the <inputDir>. If 
        specified, then do not perform a directory walk, but convert only 
        this file.

        [-O|--outputDir <outputDir>]
        The directory to contain all output files.

        -e|--extension <DICOMextension>
        An optional extension to filter the DICOM files of interest from the 
        <inputDir>.

        [-x|--man]
        Show full help.

        [-y|--synopsis]
        Show brief help.

        -v|--verbosity <level>
        Set the app verbosity level. 

             -1: No internal output.
              0: All internal output.

    EXAMPLES


    ''' % (scriptName)
    if ab_shortOnly:
        return shortSynopsis
    else:
        return shortSynopsis + description



parser  = ArgumentParser(description = str_desc, formatter_class = RawTextHelpFormatter)


parser.add_argument("-I", "--inputDir",
                    help    = "input dir",
                    dest    = 'inputDir',
                    default = '')
parser.add_argument("-i", "--inputFile",
                    help    = "input file",
                    dest    = 'inputFile',
                    default = '')
parser.add_argument("-O", "--outputDir",
                    help    = "output image directory",
                    dest    = 'outputDir',
                    default = '')
parser.add_argument("-o", "--outputFileStem",
                    help    = "output file",
                    default = "",
                    dest    = 'outputFileStem')
parser.add_argument("-x", "--man",
                    help    = "man",
                    dest    = 'man',
                    action  = 'store_true',
                    default = False)
parser.add_argument("-e", "--extension",
                    help    = "DICOM file extension",
                    dest    = 'extension',
                    default = '')
parser.add_argument("-y", "--synopsis",
                    help    = "short synopsis",
                    dest    = 'synopsis',
                    action  = 'store_true',
                    default = False)
parser.add_argument("-v", "--verbosity",
                    help    = "verbosity level for app",
                    dest    = 'verbosity',
                    default = "0")
parser.add_argument("--printElapsedTime",
                    help    = "print program run time",
                    dest    = 'printElapsedTime',
                    action  = 'store_true',
                    default = False)
parser.add_argument('--version',
                    help    = 'if specified, print version number',
                    dest    = 'b_version',
                    action  = 'store_true',
                    default = False)

args = parser.parse_args()

if args.man or args.synopsis:
    print(str_desc)
    if args.man:
        str_help     = synopsis(False)
    else:
        str_help     = synopsis(True)
    print(str_help)
    sys.exit(1)

if args.b_version:
    print("Version: %s" % str_version)
    sys.exit(1)

# pudb.set_trace()

pf_dicom             = pfdicom.pfdicom(
                        inputDir            = args.inputDir,
                        inputFile           = args.inputFile,
                        outputDir           = args.outputDir,
                        outputFileStem      = args.outputFileStem,
                        verbosity           = args.verbosity
                    )

# And now run it!
other.tic()
pf_dicom.run()
if args.printElapsedTime: pf_dicom.dp.qprint("Elapsed time = %f seconds" % other.toc())
sys.exit(0)
