#! /usr/bin/env python

"""\
%(prog)s <infile> <outfile>

Convert between natively YODA-supported data formats (.yoda, ...)

TODO:
 * Support reading/writing from ROOT... or wait for native ROOT I/O via Reader/Writer classes?
"""

import yoda, os, sys, argparse, re
# TODO: Support an extra arg or option to choose the target format and hence convert many files at once with auto-naming
# from yoda.script_helpers import parse_x2y_args

parser = argparse.ArgumentParser(usage=__doc__)
parser.add_argument("ARGS", nargs=2, help="<infile> <outfile>")
parser.add_argument("-m", "--match", dest="MATCH", metavar="PATT", default=None,
                    help="only write out histograms whose path matches this regex")
parser.add_argument("-M", "--unmatch", dest="UNMATCH", metavar="PATT", default=None,
                    help="exclude histograms whose path matches this regex")

args = parser.parse_args()
# if not len(args.ARGS) == 2:
#     sys.stderr.write("You must specify the in and out file names\n")
#     sys.exit(1)
i, o = args.ARGS

## Read, filter, and write out histos
from yoda.script_helpers import filter_aos
aos = yoda.read(i)
filter_aos(aos, args.MATCH, args.UNMATCH)
yoda.write(aos, o)
