def main():
    
    trig_filename = sys.argv[1]

    if trig_filename[-5:] == '.trig':
        base_filename = trig_filename[:-5]
    else:
        base_filename = trig_filename

    trig = read_triangulation_from_file(trig_filename)

    open(base_filename+'_sl3NeumannZagier.magma','w').write(
        produce_magma_out(trig))
def write_magma_files(options, triangulation_filename):
    # The input file is a triangulation
    # produce the Magma file

    # get N for SL(N,C) representations
    N = options.N

    # parse and orient the triangulation
    t = read_triangulation_from_file(triangulation_filename)
    t.orient()

    # determine the base for the output filenames
    # If this is not overwritten by the command line option,
    # this strips the ".trig" extension of the triangulation file
    # and also tries to write the file into ../magma_slN/
    outfile_base = options.magma_base
    if not outfile_base:
        outfile_base = get_outfile_base(triangulation_filename)
        print "Writing files to %s..." % outfile_base

    # if N is even, compute all cohomology obstruction classes
    # the cohomology obstruction is an element in H^2(M, partial M; Z/2)
    if N % 2 == 0:
        H = manifold.slN.get_all_obstruction_classes(t)
    else:
        H = [None] 
    
    # For each cohomology obstruction class h with index c
    for c, h in basicAlgorithms.indexedIterable(H):
        # List all Ptolemy relations with respect to the cohomology class h
        pt_eqns = manifold.slN.get_Ptolemy_relations(t, N, h)

        # List additional relations for fixing decoration
        #fix_decoration_method = "MANUAL_FIX_DECORATION"
        #addl_eqns = manifold.slN.get_additional_eqns_manual(t, N)

        # List additional relations for fixing decoration
        fix_decoration_method = "AUTOMATIC_FIX_DECORATION"
        addl_eqns = manifold.slN.get_additional_eqns_independent(t, N)

        # Add all equations together
        pre_eqns = pt_eqns + addl_eqns

        # Find which Ptolemy coordinates are getting identified
        # id_c_parms is of type equivalence_with_sign which encapsulates
        # an equivalence relationship with signs
        id_c_parms = manifold.slN.get_identified_c_parameters(t, N)

        # identify the Ptolemy coordinates
        eqns       = manifold.slN.identify_c_parameters(pre_eqns, id_c_parms)

        # Append an equation of the form "x * y * t -1" with dummy variable t
        # This is to prevent Ptolemy coordinates from being zero
        eqns.append( manifold.slN.polynomialNonZeroCondition(eqns,'t') )

        # Make the input for magma
        # pre_vars tells the procedure to list t in the term order used
        # for the computation of the Groebner basis
        term_order = algebra.magma.get_term_order(eqns, pre_vars = ['t'])
        out = algebra.magma.primary_decomposition(eqns, term_order = term_order)

        # Compute the hash of the ideal
        hash_eqns = hash_ideal(eqns, term_order) 

        # Human readable comment at the beginning of the magma file
        comment = generate_magma_comment(t, N, triangulation_filename, h, id_c_parms, pre_eqns)

        # Header for the magma file consists of a bunch of PRINT statements
        # The results are parsed from the magma output file to recover
        # the triangulation, N, the cohomology obstruction...
        # when processing the output magma produced.
        header = generate_magma_header(t, N, triangulation_filename, c, 
                                       outfile_base, fix_decoration_method,
                                       term_order, hash_eqns)

        # name of magma input file contains 
        # N and the index of the cohomology obstruction
        # outfile = outfile_base + "_sl%d_c%d_hash%s.magma" % (N,c,hash_eqns)
        outfile = outfile_base + "_sl%d_c%d.magma" % (N,c)
        print "          ", outfile
        open(outfile,'w').write(comment + header + out)
def process_file(trig_file):

    d,in_filename = tempfile.mkstemp()

    tmp_in_file = open(in_filename, 'wb')
    tmp_in_file.write("r f %s\n" % trig_file)
    tmp_in_file.write("set precision 15\n")
    tmp_in_file.write("set digits_printed 120 f\n")
    tmp_in_file.write("print solution_type\n")
    tmp_in_file.write("print volume\n")
    #tmp_in_file.write("print complex_volume\n")
    tmp_in_file.write("quit\n")
    tmp_in_file.close()

    l = subprocess.Popen("ulimit -t 1; snap <" + in_filename, shell=True, stdout = subprocess.PIPE)
    s = l.stdout.read()

    if not ("solution type: geometric" in s or "solution type: nongeometric" in s):
        return None

    try:
        os.unlink(in_filename)
    except:
        pass

    d,in_filename = tempfile.mkstemp()
    tmp_in_file = open(in_filename, 'wb')
    tmp_in_file.write("r f %s\n" % trig_file)
    tmp_in_file.write("set degree 7\n")
    tmp_in_file.write("set precision 15\n")
    tmp_in_file.write("set digits_printed 120 f\n")
    tmp_in_file.write("compute invariant_trace_field\n")
    tmp_in_file.write("quit\n")
    tmp_in_file.close()

    l = subprocess.Popen("ulimit -t 20; snap <" + in_filename, shell=True, stdout = subprocess.PIPE)
    s2 = l.stdout.read()

    try:
        os.unlink(in_filename)
    except:
        pass

    r = re.search("Complex volume: (.*)",s)

    rcvol = '"-"'
    icvol = '"-"'

    if r:
        cvol = number(r.group(1))
        rcvol = str(cvol.real())
        icvol = str(cvol.imag())

    r = re.search("Volume is: (.*)", s)

    if r:
        rcvol = r.group(1)

    f = re.search("Invariant trace field: (.*) \[",s2)
    fp = '-'
    fdeg = '-'

    if f:
        fp = f.group(1)
        fdeg = str(Polynomial.parseFromMagma(fp).degree())

    
    if re.search(r"\(-?\d+,-?\d+\)", trig_file):
        if "(0,0)" in trig_file: # manifold is cusped if there is an unfilled cusped
            oc = "cusped"
        else:
            oc = "closed"
    else:
        oc = "cusped"

    t = read_triangulation_from_file(trig_file)

    return ('"%s",%s,%d,%s,%s,"%s",%s'
            % (trig_file.split('/')[-1].replace('.trig',''),
               oc,
               t.num_tets,
               rcvol, icvol, fp, fdeg))