# Copyright (c) 2020 CNES/JPL
#
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
"""
Copyright (C) 2017-2021 OceanDataLab
This file is part of skimulator.

skimulator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

skimulator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with skimulator.  If not, see <http://www.gnu.org/licenses/>.
"""
"""
Altimeter instrument error
--------------------------
"""
from typing import Dict
import numpy

from . import utils


class Altimeter:
    """Altimeter instrument error.
    
    Args:
        parameters (settings.Parameters): Simulation settings
    """
    def __init__(self, parameters) -> None:
        # Store the generation parameters of the random signal.
        self.delta_al = 2 * parameters.delta_al
        self.nseed = parameters.nseed + 4
        self.len_repeat = parameters.len_repeat
        # Define the sepctrum of the nadir instrument error
        freq = numpy.arange(1 / 3000, 1 / self.delta_al, 1 / 3000)
        psd = 8 + 1.05 * 1e-4 * freq**(-2.2)
        psd[freq < 0.00023627939582672978] = 1e4

        # Convert spectrum in m2/cy
        self.psd = psd * 1e-4
        self.freq = freq

    def generate(self, x_al: numpy.array) -> Dict[str, numpy.ndarray]:
        """Generate altimeter instrument error.

        Args:
            x_al (numpy.ndarray): Along track distance

        Returns:
            dict: variable name and errors simulated.
        """
        # Compute random noise of 10**2 cm**2/(km/cycle)
        # Compute the correspond error on the nadir in m
        error = utils.gen_signal_1d(self.freq, self.psd, x_al,
                                    nseed=self.nseed, fmin=1 / self.len_repeat,
                                    fmax=1 / self.delta_al, alpha=10)
        return {"altimeter": error.reshape(error.shape[0], -1)}
