def sunwrap_dev(X_, Y_, Z_):
    """Given x,y,z unwrap z using x and y as coordinates"""
    from numpy import unwrap, array, pi, amin, amax, isnan, nan
    from numpy import sqrt, isinf, isfinite, inf
    from numpy.linalg import norm
    true_shape = X_.shape
    X = X_.reshape((X_.size,))
    Y = Y_.reshape((Y_.size,))
    Z = Z_.reshape((Z_.size,))
    threshold = pi
    skip_dex = []
    for k, z in enumerate(Z):
        if isfinite(z) and k not in skip_dex:
            x, y = X[k], Y[k]
            min_dr, z_min, j_min = inf, None, None
            for j, zp in enumerate(Z):
                if j > k:
                    dr = norm([X[j] - x, Y[j] - y])
                    if dr < min_dr:
                        min_dr = dr
                        j_min = j
                        z_min = zp
            if z_min is not None:
                skip_dex.append(j_min)
                dz = z - z_min
                if dz < threshold:
                    Z[k] += 2 * pi
                elif dz > threshold:
                    Z[k] -= 2 * pi
    ans = Z.reshape(true_shape)
    return ans
