def test_sa_h5py_14():
    """
    Test gridding tracks with dx, dy.
    """
    tracks = {
        "x": numpy.array([10, 20, 30]),
        "y": numpy.array([10, 10, 10]),
        "z": numpy.array([-0.2, 0.0, 0.2])
    }

    dx = 1
    dy = 2
    h5_name = storm_analysis.getPathOutputTest("test_sa_hdf5.hdf5")

    # Tracks.
    with saH5Py.SAH5Py(h5_name, is_existing=False, overwrite=True) as h5:
        h5.setMovieInformation(40, 40, 1, "")
        h5.addTracks(tracks)

    with saH5Py.SAH5Grid(filename=h5_name, scale=1, z_bins=3) as h5g:
        im_2d = h5g.gridTracks2D(dx=dx, dy=dy)
        im_3d = h5g.gridTracks3D(-0.201, 0.201, dx=dx, dy=dy)

        for i in range(tracks["x"].size):
            assert (im_2d[int(tracks["x"][i] + dx),
                          int(tracks["y"][i] + dy)] == 1)
            assert (im_3d[int(tracks["x"][i] + dx),
                          int(tracks["y"][i] + dy), i] == 1)
def alignAndMerge(file1,
                  file2,
                  results_file,
                  scale=2,
                  dx=0,
                  dy=0,
                  z_min=-0.5,
                  z_max=0.5):
    """
    Note: This only aligns and merges the tracks not the localizations.
    """
    z_bins = int((z_max - z_min) / 0.05)

    with saH5Py.SAH5Py(results_file, is_existing=False) as h5_out:

        # Process first file, this has no offset.
        with saH5Py.SAH5Grid(filename=file1, scale=scale,
                             z_bins=z_bins) as h5g_in1:
            [mx, my] = h5g_in1.getMovieInformation()[:2]
            h5_out.setMovieInformation(mx, my, 0, "")
            h5_out.setPixelSize(h5g_in1.getPixelSize())
            h5_out.addMetadata(h5g_in1.getMetadata())

            for tracks in h5g_in1.tracksIterator():
                sys.stdout.write(".")
                sys.stdout.flush()
                h5_out.addTracks(tracks)

            sys.stdout.write("\n")

            im1_xy = h5g_in1.gridTracks2D()
            im1_xyz = h5g_in1.gridTracks3D(z_min, z_max)

        # Process second file.
        with saH5Py.SAH5Grid(filename=file2, scale=scale,
                             z_bins=z_bins) as h5g_in2:

            # Determine X/Y offset.
            im2_xy = h5g_in2.gridTracks2D()
            [corr, offx, offy, xy_success
             ] = imagecorrelation.xyOffset(im1_xy,
                                           im2_xy,
                                           scale,
                                           center=[dx * scale, dy * scale])

            if False:
                tifffile.imsave("im1_xy.tif", im1_xy)
                tifffile.imsave("im2_xy.tif", im2_xy)

            assert xy_success, "Could not align images in X/Y."
            offx = offx / float(scale)
            offy = offy / float(scale)

            # Determine Z offset.
            im2_xyz = h5g_in2.gridTracks3D(z_min, z_max, dx=offx, dy=offy)

            [corr, fit, offz,
             z_success] = imagecorrelation.zOffset(im1_xyz, im2_xyz)

            assert z_success, "Could not align images in Z."
            offz = -offz * (z_max - z_min) / float(z_bins)

            for tracks in h5g_in2.tracksIterator():
                sys.stdout.write(".")
                sys.stdout.flush()
                tracks["x"] += offx
                tracks["y"] += offy
                tracks["z"] += offz
                h5_out.addTracks(tracks)

            sys.stdout.write("\n")

    return [offx, offy, offz]