if dataset.meshfileAt(iteration=ii, superiteration=options.superiteration
                          ) is None and dataset.testSet().numIterations() != 0:
        print("Couldn't find ", options.superiteration, "-", i)
        break

    if dataset.testSet().numIterations() == 0:
        meshfile = dataset.testSet().initialMeshPath()
    else:
        meshfile = dataset.meshfileAt(iteration=ii,
                                      superiteration=options.superiteration)

    paramlist = testset.parameterList()
    bsdf = toMap(
        paramlist,
        dataset.BSDFAt(iteration=bi, superiteration=options.superiteration))

    for k in range(dataset.testSet().numLights()):
        print "Iteration ", i, "/", testset.numIterations(
        ), ", Light ", k, "/", dataset.testSet().numLights(), "\r",
        sys.stdout.flush()

        testset.renderables[k].setEmbeddedParameter("sampleCount",
                                                    options.directSamples)
        testset.renderables[k].setParameter("blockSize", 8)
        copyfile(meshfile,
                 "/tmp/mts_mesh_intensity_slot_" + format(k) + ".ply")
        merl_io.merl_write(
            "/tmp/tabular-bsdf-" + format(k) + ".binary",
            dataset.tabularBSDFAt(iteration=bi,
                                  superiteration=options.superiteration))
                  "--distribution",
                  dest="distribution",
                  default=None,
                  type="str")
parser.add_option("-o", "--output", dest="output", type="str")
parser.add_option("-d", "--dry-run", action="store_true", dest="dryRun")

(options, args) = parser.parse_args()

directory = args[0]

dataset = Dataset(directory)
if options.superIndex == -1 and options.index == -1:
    targetBSDF = dataset.lastAvailableBSDF()
else:
    targetBSDF = dataset.BSDFAt(iteration=options.index,
                                superiteration=options.superIndex)
print targetBSDF

if options.dryRun:
    sys.exit(0)

testset = dataset.testSet()

sphereXML = os.path.dirname(__file__) + "/data/sphere-embeddable.xml"
sphereXMLP = os.path.dirname(__file__) + "/data/sphere-postprocessed.xml"
testset.embedOnto(sphereXML, sphereXMLP)

renderable = testset.renderables[0]
renderable.setFile(sphereXMLP)
renderable.setEmbeddedParameter("envmap", "doge.exr")
        bi = -1
    else:
        ii = 0
        bi = i - dataset.testSet().numIterations()

    if dataset.meshfileAt(iteration=ii, superiteration=options.superiteration) is None and ii != 0:
        print("Couldn't find ", options.superiteration, "-", ii)
        break

    if ii != 0:
        meshfile = dataset.meshfileAt(iteration=ii, superiteration=options.superiteration)
    else:
        meshfile = dataset.testSet().initialMeshPath()

    paramlist = testset.parameterList()
    bsdf = toMap(paramlist, dataset.BSDFAt(iteration=bi, superiteration=options.superiteration))

    for k in range(dataset.testSet().numLights()):
        print "Iteration ", i, "/", testset.numIterations(), ", Light ", k, "/", dataset.testSet().numLights(), "",
        sys.stdout.flush()

        testset.gradientRenderables[k].setEmbeddedParameter("sampleCount", options.samples)
        testset.gradientRenderables[k].setParameter("blockSize", 8)
        testset.renderables[k].setEmbeddedParameter("sampleCount", options.samples)
        testset.renderables[k].setParameter("blockSize", 8)
        copyfile(meshfile,
                            "/tmp/mts_mesh_gradient_slot_" + format(k) + ".ply")
        copyfile(meshfile,
                            "/tmp/mts_mesh_intensity_slot_" + format(k) + ".ply")
        testset.gradientRenderables[k].setEmbeddedParameter("depth", 2)
        testset.gradientRenderables[k].setEmbeddedParameter("meshSlot", k)