def component_ops(opline):
    logging.debug("Computing components of {}".format(opline))
    if "iso" not in line:
        logging.debug("line is not a multiop directive")
        return
    types, totalirrep, p1, irrep1, disp1, p2, irrep2, disp2 = line.split()
    _, type1, type2 = types.split("_")
    p1, p2 = p1.strip("[]P="), p2.strip("[]P=")
    disp1, disp2 = disp1.strip("[]\'()"), disp2.strip("[]\"()")
    if type1 == "kbar":
        type1 = "kaon"
    if type2 == "kbar":
        type2 = "kaon"
    comment1 = "# Lowest state in channel not expected to be a single hadron"
    comment2 = comment1
    def print_op(ptype, p, irrep, disp):
        try:
            lowest_single_index = check_lowest(ptype, p, irrep)

            if lowest_single_index:
                if lowest_single_index < 0:
                    comment = " # No expected levels for this mom"
                else:
                    comment = " # There are {} states below the single hadron in this channel".format(lowest_single_index)
            else:
                comment = ""
            if not duplicate(ptype, p, irrep):
                for row in range(1,rows[irrep[0]]+1):
                    print '@oplist.push("{} P={} {}_{} {}"){}'.format(ptype, p, irrep, row, disp, comment)
        except NotImplementedError as e:
            logging.error("{}".format(e))

    psq1 = compute_psq(p1)
    psq2 = compute_psq(p2)
    for m1 in all_permutations(psq1, outputformat="({},{},{})"):
        print_op(type1, m1, irrep1, disp1)
    for m2 in all_permutations(psq2, outputformat="({},{},{})"):
        print_op(type2, m2, irrep2, disp2)
    args = parser.parse_args()

    if args.verbose:
        logging.basicConfig(format='# %(levelname)s: %(message)s', level=logging.DEBUG)
        logging.debug("Verbose debuging mode activated")
    else:
        logging.basicConfig(format='# %(levelname)s: %(message)s', level=logging.WARN)

    args.momray = args.momentum.replace("1","+").replace("2","#")
    print args.momray

    args.hadrontype = None
    get_unspecified_parameters(args)

    psqr = sum(int(i)**2 for i in args.momentum)
    print all_permutations(psqr, nobackies=True)
    for p in all_permutations(psqr, nobackies=True):
        if args.outstub is None:
            args.outfile = sys.stdout
        else:
            args.outfile = open("{}_{}.txt".format(args.outstub, p), 'w')
        args.momray = p.replace("-1", "-").replace("-2","=").replace("1","+").replace("2","#")
        if not args.notallsingles:
            all_single_hadrons(args.momray)

        args.outfile.write("# using mom={}".format(p))

        get_unspecified_parameters(args)
        expected_levels = read_expected_levels(args.strangeness, args.isospin, args.channel, args.thirtytwo, mom=args.momentum)
        logging.info(repr(expected_levels))
        ops = get_ops(args, expected_levels[:args.number])
def rotate(opline):
    print opline
    mom1,mom2 = re.findall("P=\(.*?\)", opline)
    print (mom1, mom2)
    print opline.split()[:8]
    if "CG" in opline:
        base, channel, CG, p1, irrep1, disp1, p2, irrep2, disp2 = opline.split()[:9]
    else:
        base, channel, p1, irrep1, disp1, p2, irrep2, disp2 = opline.split()[:8]
    # print [i for i in mom1.strip("P=()").split(",")]
    # exit()
    m1 = mom1.strip("P=()").split(",")
    m2 = mom2.strip("P=()").split(",")
    psq1 = sum(int(i)**2 for i in m1)
    psq2 = sum(int(i)**2 for i in m2)

    p= "".join([str(int(i)+int(j)) for i,j in zip(m1,m2)])
    print p
    psq_total = sum((int(i)+int(j))**2 for i,j in zip(m1,m2))
    momray = p.replace("-1", "-").replace("-2","=").replace("1","+").replace("2","#")
    print momray
    channel_1 = channel.split("_")[0]+"_1"
    print opline
    coeffs = read_coeffs(momray, channel_1)
    print opline.split()
    expression = ".*{}_.*{}_.*".format(irrep1, irrep2)

    for momentum in all_permutations(psq_total, nobackies=True):
        print momentum
        momray = momentum.replace("-1", "-").replace("-2","=").replace("1","+").replace("2","#")
        print momray
        coeffs = read_coeffs(momray, channel_1)
        print coeffs

        for c in coeffs:
            if re.match(expression, c):
                print c
                S, mom1, i1, mom2, i2, cg = c.split("_")
                coeffpsq1 = sum(mom_map[m]**2 for m in mom1)
                coeffpsq1_2 = sum((mom_map[m]*2)**2 for m in mom1)
                coeffpsq1_3 = sum((mom_map[m]*3)**2 for m in mom1)
                coeffpsq2 = sum(mom_map[m]**2 for m in mom2)
                coeffpsq2_2 = sum((mom_map[m]*2)**2 for m in mom2)
                coeffpsq2_3 = sum((mom_map[m]*3)**2 for m in mom2)
                matches = (coeffpsq1 == int(psq1) and coeffpsq2 == int(psq2))
                scale = 1
                if (coeffpsq1_2 == int(psq1) and coeffpsq2_2 == int(psq2)):
                    matches = True
                    scale = 2
                elif (coeffpsq1_3 == int(psq1) and coeffpsq2_3 == int(psq2)):
                    matches = True
                    scale = 3
                if matches:
                    logging.info("{} matched irreps and moms".format(c))
                    m1 = "({},{},{})".format(*[mom_map[m]*scale for m in mom1])
                    m2 = "({},{},{})".format(*[mom_map[m]*scale for m in mom2])
                    logging.info("Found coeff with psqr{}, psqr{} {}".format(psq1, psq2, c))
                    found = True
                    temp = '{} {} {}[P={} {} {} [P={} {} {}\n'
                    newopline = temp.format(base, channel, cg_map[cg], m1, irrep1, disp1, m2, irrep2, disp2)
                    print newopline
                    print opline
                    with open(args.outputstub+"_{}_{}".format(channel,momray), 'a') as writefile:
                        writefile.write(newopline)