import numpy as np
import mikeio


def uz2u10(Uz, z, mode, z0=0.001, power=0.1):
    if mode == "log":
        kappa = 0.4  # von Karman constant
        Uf = Uz * kappa / np.log(z / z0)
        U10 = Uf * (1 / kappa) * np.log(10 / z0)
    elif mode == "power":
        U10 = Uz * (10 / z) ** power
    else:
        raise ValueError("Mode must be 'log' or 'power'")
    return U10


def find_dfs2_index(ds, x, y):
    X = ds.geometry.x
    Y = ds.geometry.y
    dx = ds.geometry.dx
    dy = ds.geometry.dy
    j = np.argmin(np.abs(X - x))
    i = np.argmin(np.abs(Y - y))
    # Check if the point is within the grid cell
    if (X[j] - dx / 2 <= x <= X[j] + dx / 2) and (Y[i] - dy / 2 <= y <= Y[i] + dy / 2):
        return i, j
    else:
        raise ValueError("Point is outside the grid cell")
    




