#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Built-in imports
from typing import Union

# 3rd party imports
import numpy as np
from xarray.core.dataarray import DataArray
from xarray.core.dataset import Dataset

__author__ = "Louis Richard"
__email__ = "louisr@irfu.se"
__copyright__ = "Copyright 2020-2024"
__license__ = "MIT"
__version__ = "2.4.13"
__status__ = "Prototype"


def calc_fs(inp: Union[Dataset, DataArray]) -> float:
    r"""Computes the sampling frequency of the input time series.

    Parameters
    ----------
    inp : DataArray or Dataset
        Time series of the input variable.

    Returns
    -------
    f_samp : float
        Sampling frequency in Hz.

    """
    # Check input type
    if not isinstance(inp, (Dataset, DataArray)):
        raise TypeError("Input must be a time series")

    # Convert time to nanoseconds
    time_datetime64 = inp.time.data.astype(np.datetime64)
    time_ns = time_datetime64.astype(np.int64)

    # Calculate the sampling frequency
    dt_ns = np.diff(time_ns)
    f_samp = 1e9 / np.median(dt_ns)

    return f_samp
