#!/usr/bin/env python
"""
Reorders the OHSU AIRC ``bolero_mask_conv``
result to conform with the time series x and y order.
"""

import sys
import os
import imp
import argparse
from qipipe.helpers import (roi, command)


def main(argv=sys.argv):
    # Parse the command line arguments.
    in_file, opts = _parse_arguments()
    # The utility function option is out_file, not output.
    out_file = opts.get('output')
    func_opts = dict(out_file=out_file) if out_file else {}
    mask.reorder_bolero_mask(in_file, func_opts)

    return 0


def _parse_arguments():
    """
    Parses the command line arguments.

    :return: the (input, options) tuple, where input is the non-option
        input file argument and options is an {option: value} dictionary
    """
    parser = argparse.ArgumentParser()

    # The general options.
    command.add_options(parser)

    # The output option.
    parser.add_argument('-o', '--output', metavar='FILE',
                        help='the output file name')

    # The input file.
    parser.add_argument('input', metavar='FILE',
                        help='the input Bolero NIfTI mask file')

    args = vars(parser.parse_args())
    nonempty_args = dict((k, v) for k, v in args.iteritems() if v != None)

    return nonempty_args.pop('input'), nonempty_args


if __name__ == '__main__':
    sys.exit(main())
