def _write_surf(surf_filename, maxnode, nodes, tris, quads, node_flags,
    initial_normal_spacing0, pid_to_element_flags, bl_thickness0,
    renumber_pids, tol):
    """writes the actual surf file"""
    ntris = len(tris)
    nquads = len(quads)
    assert ntris + nquads > 0, 'nelements=%s' % (ntris + nquads)
    with open(surf_filename, 'w', encoding='ascii') as surf_file:
        surf_file.write('%i %i %i\n' % (ntris, nquads, maxnode))
        for nid, node in enumerate(nodes):
            x, y, z = node
            if abs(x) < tol:
                x = 0.0
            if abs(y) < tol:
                y = 0.0
            if abs(z) < tol:
                z = 0.0
            try:
                initial_normal_spacing, bl_thickness = node_flags[nid + 1]
            except KeyError:
                initial_normal_spacing = initial_normal_spacing0
                bl_thickness = bl_thickness0
            except ValueError:
                initial_normal_spacing = initial_normal_spacing0
                bl_thickness = bl_thickness0
            surf_file.write('%.10e %.10e %.10e %g %g\n' % (x, y, z,
                initial_normal_spacing, bl_thickness))
        rf = 0
        for unused_eid, (element, pid) in enumerate(tris):
            unused_name, initial_normal_spacing, bl_thickness, grid_bc = (
                pid_to_element_flags[pid])
            if pid in renumber_pids:
                pid = renumber_pids[pid]
            n1, n2, n3 = element
            surf_file.write('%i %i %i %i %s %s\n' % (n1, n2, n3, pid, rf,
                grid_bc))
        rf = 0
        for unused_eid, (element, pid) in enumerate(quads):
            unused_name, initial_normal_spacing, bl_thickness, grid_bc = (
                pid_to_element_flags[pid])
            if pid in renumber_pids:
                pid = renumber_pids[pid]
            n1, n2, n3, n4 = element
            surf_file.write('%i %i %i %i %i %s %s\n' % (n1, n2, n3, n4, pid,
                rf, grid_bc))
