#!/usr/bin/env python3
# -----------------------------------------------------------------------------
#   Copyright (C): OpenGATE Collaboration
#   This software is distributed under the terms
#   of the GNU Lesser General  Public Licence (LGPL)
#   See LICENSE.md for further details
# -----------------------------------------------------------------------------

import gatetools as gt
import itk
import click
import sys
import logging
logger=logging.getLogger(__name__)

# -----------------------------------------------------------------------------
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS)

@click.argument('dicomfile',
                nargs=1,
                required=True,
                type=click.Path(exists=True, file_okay=True, dir_okay=False,
                                writable=False, readable=True, resolve_path=True,
                                allow_dash=False, path_type=None))

@click.option('--keepzero/--filterzero','-Z/-z',
              help='Keep spots with zero weight (-Z) or filter them out (-z, default)', default=False)

@click.option('--output','-o', help='Output filename', required=True,
              type=click.Path(dir_okay=False,
                              writable=True, readable=False,
                              resolve_path=True, allow_dash=False, path_type=None))
@gt.add_options(gt.common_options)

def gt_pbs_dicom2gate_main(dicomfile, output, keepzero, verbose, **kwargs):
    '''
    Convert DICOM files with treatment plans for pencil beam scanning (PBS)
    radiotherapy (RT) to the text format that can be read by `TPSPencilBeam`
    actor of Gate.
    
    Note that in addition to the plan file, the TPSPencilBeam actor needs a
    "source properties file" that provides the beam model. In case the spot
    weights in the plan file are given in "monitor units" (MU, as opposed to
    "number of ions", NP), the beam model should include the MU calibration
    (polynomial/table in energy that gives the energy dependent conversion
    factor between MU and NP).

    DICOMFILE: the DICOM RT PBS ion beam treatment plan file. Typically a
    treatment plan file is created using one of the major treatment planning
    systems (TPS), such as RayStation or Eclipse, but we also made an effort to
    enable the conversion of artificial plans that are generated by medical
    physicists e.g. for beam delivery verification.  (Such plans may lack some
    attributes, like the isocenter position, gantry angle and patient support
    angle.)

    OUTPUT: File path to store the result. This file should have a ".txt" or a
    ".dat" suffix. The part of the basename without suffix preferrably contains
    the plan name, which is then also stored in the output file (after
    replacing all non-alphanumeric characters with an underscore).
    '''

    # logger
    gt.logging_conf(**kwargs)

    gt.dicom_rt_pbs_plan_to_gate_conversion(dicomfile,output,allow0=keepzero,verbose=verbose)



# -----------------------------------------------------------------------------
if __name__ == '__main__':
    gt_pbs_dicom2gate_main()
