#ifndef _Get_figure_FS_valueH
#define _Get_figure_FS_valueH

#include <iostream>
#include <vector>
#include <stdexcept>
#include <fstream>
#include <cmath>
#include "FsValues.h"

/**
 * Compares two double values for equality within a specified tolerance.
 * 
 * @param d1 First double value to compare
 * @param d2 Second double value to compare  
 * @param epsilon Tolerance for comparison (default: 1e-9)
 * @return true if values are equal within tolerance, false otherwise
 */
bool SameDouble(double d1, double d2, double epsilon = 1e-9){
	return (std::abs(d1 - d2) < epsilon);
}
/**
 * Searches for a target value in a fixed-size double array using approximate comparison.
 * 
 * @tparam N Size of the array
 * @param arr Array to search through
 * @param target Value to find
 * @param epsilon Tolerance for comparison (default: 1e-9)
 * @return Index of found element, or -1 if not found
 */
template <size_t N>
int contains(const double (&arr)[N], double target, double epsilon = 1e-9) {
	for (size_t i = 0; i < N; ++i) {
		if (SameDouble(arr[i],target,epsilon)) {
			return i;
			break;
		}
	}
	return -1;
}



/**
 * Performs linear interpolation between two points.
 * 
 * @param x0 First point x-coordinate
 * @param y0 First point y-coordinate
 * @param x1 Second point x-coordinate  
 * @param y1 Second point y-coordinate
 * @param x X-coordinate to interpolate at
 * @return Interpolated y-value at x
 */
double lerp(double x0, double y0, double x1, double y1, double x) {
    if (x0 == x1) return y0;
    return y0 + (y1 - y0) * ((x - x0) / (x1 - x0));
}

/**
 * Performs bilinear interpolation on a 2D grid of values.
 * 
 * @param x Vector of x-coordinates (must be sorted)
 * @param y Vector of y-coordinates (must be sorted)
 * @param z 2D vector of z-values at grid points [y][x]
 * @param xi X-coordinate to interpolate at
 * @param yi Y-coordinate to interpolate at
 * @return Interpolated value at (xi, yi)
 */
double bilinearInterpolate(
    const std::vector<double>& x,
    const std::vector<double>& y,
    const std::vector<std::vector<double>>& z,
    double xi, double yi)
{
    if (xi <= x.front()) xi = x.front();
    else if (xi >= x.back()) xi = x.back();

    if (yi <= y.back()) yi = y.back();
    else if (yi >= y.front()) yi = y.front();

    int x_left = 0;
    while (x_left + 1 < x.size() && x[x_left + 1] < xi)
        ++x_left;

    int x_right = x_left + 1;
    if (x_right >= x.size())
        x_right = x.size() - 1;

    int y_bottom = 0;
    while (y_bottom + 1 < y.size() && y[y_bottom + 1] > yi)
        ++y_bottom;

    int y_top = y_bottom + 1;
    if (y_top >= y.size())
        y_top = y.size() - 1;

    double Q11 = z[y_bottom][x_left];
    double Q21 = z[y_bottom][x_right];
    double Q12 = z[y_top][x_left];
    double Q22 = z[y_top][x_right];

    double x1 = x[x_left], x2 = x[x_right];
    double y1 = y[y_bottom], y2 = y[y_top];

    double R1 = lerp(x1, Q11, x2, Q21, xi);
    double R2 = lerp(x1, Q12, x2, Q22, xi);

    return lerp(y1, R1, y2, R2, yi);
}

/**
 * Calculates field strength (FS) values for land and sea propagation based on ITU-R recommendations.
 * Provides basic field strength prediction without geographical corrections.
 * 
 * @param Figure_frequency Operating frequency in MHz (100, 600, or 2000)
 * @param Figure_Heff Effective antenna height in meters
 * @param Figure_distance Distance in kilometers  
 * @param Figure_Sea_temperature Sea temperature category ('C'=Cold, 'W'=Warm, 'I'=Intermediate)
 * @param Figure_Time_percentage Time percentage (1, 10, or 50)
 * @param Figure_LatM Latitude in degrees for intermediate sea temperature correction
 * @param[out] Land_figure_FS Calculated field strength for land path in dB(μV/m)
 * @param[out] Sea_figure_FS Calculated field strength for sea path in dB(μV/m)
 * @return 0 on success, error code (2000-2005) on invalid input
 */

int Get_figure_FS_value(double Figure_frequency, double Figure_Heff, double Figure_distance,
						char Figure_Sea_temperature, double Figure_Time_percentage,	double Figure_LatM,
						double &Land_figure_FS, double &Sea_figure_FS) {
// Array for effective antenna heights in meters
	const double FS_value_effAntennaHeights[] = {10, 20, 37.5, 75, 150, 300, 600, 1200};

	// Array for distances in kilometers
	const double FS_value_Distances[] = {
			1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
			16, 17, 18, 19, 20, 25, 30, 35, 40, 45, 50, 55, 60,
			65, 70, 75, 80, 85, 90, 95, 100, 110, 120, 130, 140,
			150, 160, 170, 180, 190, 200, 225, 250, 275, 300,
			325, 350, 375, 400, 425, 450, 475, 500, 525, 550,
			575, 600, 625, 650, 675, 700, 725, 750, 775, 800,
			825, 850, 875, 900, 925, 950, 975, 1000
		};

	// Data for 100 MHz, 50%, Land

		// Validate input frequency
	int IndexHeff, IndexDist, IndexTime;

	if (!SameDouble(Figure_frequency,100) && !SameDouble(Figure_frequency,600) && !SameDouble(Figure_frequency,2000)){
		return 2000; // "wrong Figure_frequency (from Get_figure_FS_value)"
	}

	// Validate effective antenna height
	IndexHeff = contains(FS_value_effAntennaHeights, Figure_Heff);
	if (IndexHeff < 0) {
		return 2003; // "wrong Figure_Heff (from Get_figure_FS_value)
	}

	// Validate distance
	IndexDist = contains(FS_value_Distances, Figure_distance);
	if (IndexDist < 0) {
		return 2004; // wrong Figure_distance (from Get_figure_FS_value)
	}
	// Validate Time_percentage
	if (!SameDouble(Figure_Time_percentage,1) && !SameDouble(Figure_Time_percentage,10) && !SameDouble(Figure_Time_percentage,50)){
		return 2001; //"wrong Time_percentage (from Get_figure_FS_value)
	}
	// Validate Sea_temperature
	if (Figure_Sea_temperature != 'C' && Figure_Sea_temperature != 'I' && Figure_Sea_temperature != 'W') {
		return 2002; // "wrong Sea_temperature (from Get_figure_FS_value)
	}

	if (Figure_LatM < -90) {
		Figure_LatM = -90;
	}
	if (Figure_LatM > 90) {
		Figure_LatM = 90;
	}



	if (Figure_frequency == 100 && Figure_Time_percentage == 50) {
		Land_figure_FS = F01[IndexDist][IndexHeff];
	} else if (Figure_frequency == 100 && Figure_Time_percentage == 10) {
		Land_figure_FS = F02[IndexDist][IndexHeff];
	} else if (Figure_frequency == 100 && Figure_Time_percentage == 1) {
		Land_figure_FS = F03[IndexDist][IndexHeff];
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 50) {
		Land_figure_FS = F09[IndexDist][IndexHeff];
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 10) {
		Land_figure_FS = F10[IndexDist][IndexHeff];
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 1) {
		Land_figure_FS = F11[IndexDist][IndexHeff];
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 50) {
		Land_figure_FS = F17[IndexDist][IndexHeff];
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 10) {
		Land_figure_FS = F18[IndexDist][IndexHeff];
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 1) {
		Land_figure_FS = F19[IndexDist][IndexHeff];
	} else {
		return 2005; //"wrong Get_figure_FS_value (error in curves)"}
	}

	// get Sea_figure_FS
	double SF_C = 0.0;
	double SF_W = 0.0;
	if (Figure_frequency == 100 && Figure_Time_percentage == 50) {
		SF_C = F04[IndexDist][IndexHeff];
		SF_W = SF_C;
	} else if (Figure_frequency == 100 && Figure_Time_percentage == 10) {
		SF_C = F05[IndexDist][IndexHeff];
		SF_W = F07[IndexDist][IndexHeff];
	} else if (Figure_frequency == 100 && Figure_Time_percentage == 1) {
		SF_C = F06[IndexDist][IndexHeff];
		SF_W = F08[IndexDist][IndexHeff];
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 50) {
		SF_C = F12[IndexDist][IndexHeff];
		SF_W = SF_C;
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 10) {
		SF_C = F13[IndexDist][IndexHeff];
		SF_W = F15[IndexDist][IndexHeff];
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 1) {
		SF_C = F14[IndexDist][IndexHeff];
		SF_W = F16[IndexDist][IndexHeff];
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 50) {
		SF_C = F20[IndexDist][IndexHeff];
		SF_W = SF_C;
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 10) {
		SF_C = F21[IndexDist][IndexHeff];
		SF_W = F23[IndexDist][IndexHeff];
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 1) {
		SF_C = F22[IndexDist][IndexHeff];
		SF_W = F24[IndexDist][IndexHeff];
	} else {
		return 2005; //"wrong Get_figure_FS_value (error in curves)"}
	}

	if (Figure_Sea_temperature == 'C' ) {
		Sea_figure_FS = SF_C;
	} else if (Figure_Sea_temperature == 'W') {
		Sea_figure_FS = SF_W;
	} else if (Figure_Sea_temperature == 'I') {
		Sea_figure_FS = SF_W - ((SF_W - SF_C) * (Figure_LatM - 35)) / 21.0;
	}

	return 0;
}

/**
 * Enhanced field strength calculation with geographical corrections for refractivity and maximum field strength.
 * Includes bilinear interpolation of refractivity data based on transmitter location.
 * 
 * @param DistKM Distance in kilometers
 * @param TxLongitude Transmitter longitude in degrees
 * @param TxLatitude Transmitter latitude in degrees  
 * @param Figure_frequency Operating frequency in MHz (100, 600, or 2000)
 * @param Figure_Heff Effective antenna height in meters
 * @param Figure_distance Distance in kilometers
 * @param Figure_Sea_temperature Sea temperature category ('C'=Cold, 'W'=Warm, 'I'=Intermediate)
 * @param Figure_Time_percentage Time percentage (1, 10, or 50)
 * @param Figure_LatM Latitude in degrees for intermediate sea temperature correction
 * @param[out] Land_figure_FS Calculated field strength for land path with geographical corrections
 * @param[out] Sea_figure_FS Calculated field strength for sea path with geographical corrections
 * @return 0 on success, error code (2000-2005) on invalid input
 */
int Get_figure_FS_value_plus(double DistKM, double TxLongitude, double TxLatitude, double Figure_frequency, double Figure_Heff, double Figure_distance,
						char Figure_Sea_temperature, double Figure_Time_percentage,	double Figure_LatM,
						double &Land_figure_FS, double &Sea_figure_FS) {
// Array for effective antenna heights in meters
	const double FS_value_effAntennaHeights[] = {10, 20, 37.5, 75, 150, 300, 600, 1200};

	// Array for distances in kilometers
	const double FS_value_Distances[] = {
			1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
			16, 17, 18, 19, 20, 25, 30, 35, 40, 45, 50, 55, 60,
			65, 70, 75, 80, 85, 90, 95, 100, 110, 120, 130, 140,
			150, 160, 170, 180, 190, 200, 225, 250, 275, 300,
			325, 350, 375, 400, 425, 450, 475, 500, 525, 550,
			575, 600, 625, 650, 675, 700, 725, 750, 775, 800,
			825, 850, 875, 900, 925, 950, 975, 1000
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		};

	// Data for 100 MHz, 50%, Land

		// Validate input frequency
	int IndexHeff, IndexDist, IndexTime;

	if (!SameDouble(Figure_frequency,100) && !SameDouble(Figure_frequency,600) && !SameDouble(Figure_frequency,2000)){
		return 2000; // "wrong Figure_frequency (from Get_figure_FS_value)"
	}

	// Validate effective antenna height
	IndexHeff = contains(FS_value_effAntennaHeights, Figure_Heff);
	if (IndexHeff < 0) {
		return 2003; // "wrong Figure_Heff (from Get_figure_FS_value)
	}

	// Validate distance
	IndexDist = contains(FS_value_Distances, Figure_distance);
	if (IndexDist < 0) {
		return 2004; // wrong Figure_distance (from Get_figure_FS_value)
	}
	// Validate Time_percentage
	if (!SameDouble(Figure_Time_percentage,1) && !SameDouble(Figure_Time_percentage,10) && !SameDouble(Figure_Time_percentage,50)){
		return 2001; //"wrong Time_percentage (from Get_figure_FS_value)
	}
	// Validate Sea_temperature
	if (Figure_Sea_temperature != 'C' && Figure_Sea_temperature != 'I' && Figure_Sea_temperature != 'W') {
		return 2002; // "wrong Sea_temperature (from Get_figure_FS_value)
	}

	if (Figure_LatM < -90) {
		Figure_LatM = -90;
	}
	if (Figure_LatM > 90) {
		Figure_LatM = 90;
	}

	double dN0 = 0.0, dN = 0.0, delN = 0.0, dE1 = 0.0,
			dEmax = 0.0, EmaxL_= 0.0, EmaxS_ = 0.0, K = 0.0;
	double EmaxL = 106.9 - 20 * log10(DistKM);
	double EmaxS = EmaxL + 2.38 *(1-exp(-DistKM/8.94)) * log10(50/Figure_Time_percentage);
	

	if (Figure_Time_percentage == 50){
		dN0 = -43.3;
		dN = bilinearInterpolate(LonVector, LatVector, DN50, TxLongitude, TxLatitude);
		delN = dN0 - dN;
	}
	else if (Figure_Time_percentage == 10){
		dN0 = -141.9;
		dN = bilinearInterpolate(LonVector, LatVector, DN10, TxLongitude, TxLatitude);
		delN = dN0 - dN;
	}else if (Figure_Time_percentage == 1){
		dN0 = -301.3;
		dN = bilinearInterpolate(LonVector, LatVector, DN01, TxLongitude, TxLatitude);
		delN = dN0 - dN;
	}
	
	if (dN < -301.3)
		dEmax = 0.007 * (-301.3 - dN) * (1 - exp(-DistKM/50)) * exp(-DistKM/6000); //formula 45 from Annex 7
	else
		dEmax = 0;
	
	EmaxL_ = EmaxL + dEmax;
	EmaxS_ = EmaxS + dEmax;
	if (delN > 0)
		K = 14.94 - 6.693 * (1e-6) * std::pow(1494 - delN, 2);
	else
		K = 0.08 * delN;

	dE1 = K * (1 - exp(-DistKM/50))*exp(-DistKM/6000);


	double E1=0.0, E1_=0.0, En=0.0;
	if (Figure_frequency == 100 && Figure_Time_percentage == 50) {
		Land_figure_FS = F01[IndexDist][IndexHeff];

	    E1 = F01[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
		
	} else if (Figure_frequency == 100 && Figure_Time_percentage == 10) {
		Land_figure_FS = F02[IndexDist][IndexHeff];
		
	    E1 = F02[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	   
	} else if (Figure_frequency == 100 && Figure_Time_percentage == 1) {
		Land_figure_FS = F03[IndexDist][IndexHeff];
	    E1 = F03[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 50) {
		Land_figure_FS = F09[IndexDist][IndexHeff];
		E1 = F09[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 10) {
		Land_figure_FS = F10[IndexDist][IndexHeff];
		E1 = F10[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 1) {
		Land_figure_FS = F11[IndexDist][IndexHeff];
		E1 = F11[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 50) {
		Land_figure_FS = F17[IndexDist][IndexHeff];
		E1 = F17[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 10) {
		Land_figure_FS = F18[IndexDist][IndexHeff];
		E1 = F18[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 1) {
		Land_figure_FS = F19[IndexDist][IndexHeff];
		E1 = F19[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = Land_figure_FS;
	    if (IndexHeff == 0)
	        Land_figure_FS = E1_;
	    else
	        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else {
		return 2005; //"wrong Get_figure_FS_value (error in curves)"}
	}

	// get Sea_figure_FS
	double SF_C = 0.0;
	double SF_W = 0.0;
	if (Figure_frequency == 100 && Figure_Time_percentage == 50) {
		SF_C = F04[IndexDist][IndexHeff];
	    E1 = F04[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
		SF_W = SF_C;
	} else if (Figure_frequency == 100 && Figure_Time_percentage == 10) {
		SF_C = F05[IndexDist][IndexHeff];
		SF_W = F07[IndexDist][IndexHeff];
	    E1 = F05[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	    E1 = F07[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_W;
	    if (IndexHeff == 0)
	        SF_W = E1_;
	    else
	        SF_W = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 100 && Figure_Time_percentage == 1) {
		SF_C = F06[IndexDist][IndexHeff];
		SF_W = F08[IndexDist][IndexHeff];
	    E1 = F06[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	    E1 = F08[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_W;
	    if (IndexHeff == 0)
	        SF_W = E1_;
	    else
	        SF_W = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 50) {
		SF_C = F12[IndexDist][IndexHeff];
		SF_W = SF_C;
	    E1 = F12[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 10) {
		SF_C = F13[IndexDist][IndexHeff];
		SF_W = F15[IndexDist][IndexHeff];
	    E1 = F13[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	    E1 = F15[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_W;
	    if (IndexHeff == 0)
	        SF_W = E1_;
	    else
	        SF_W = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 600 && Figure_Time_percentage == 1) {
		SF_C = F14[IndexDist][IndexHeff];
		SF_W = F16[IndexDist][IndexHeff];
	    E1 = F14[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	    E1 = F16[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_W;
	    if (IndexHeff == 0)
	        SF_W = E1_;
	    else
	        SF_W = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 50) {
		SF_C = F20[IndexDist][IndexHeff];
		SF_W = SF_C;
	    E1 = F20[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 10) {
		SF_C = F21[IndexDist][IndexHeff];
		SF_W = F23[IndexDist][IndexHeff];
	    E1 = F21[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	    E1 = F23[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_W;
	    if (IndexHeff == 0)
	        SF_W = E1_;
	    else
	        SF_W = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else if (Figure_frequency == 2000 && Figure_Time_percentage == 1) {
		SF_C = F22[IndexDist][IndexHeff];
		SF_W = F24[IndexDist][IndexHeff];
	    E1 = F22[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_C;
	    if (IndexHeff == 0)
	        SF_C = E1_;
	    else
	        SF_C = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	    E1 = F24[IndexDist][0];
	    E1_ = E1 + dE1;
	    En = SF_W;
	    if (IndexHeff == 0)
	        SF_W = E1_;
	    else
	        SF_W = E1_ + (En - E1) * (EmaxL_ - E1_)/(EmaxL - E1);
	} else {
		return 2005; //"wrong Get_figure_FS_value (error in curves)"}
	}

	if (Figure_Sea_temperature == 'C' ) {
		Sea_figure_FS = SF_C;
	} else if (Figure_Sea_temperature == 'W') {
		Sea_figure_FS = SF_W;
	} else if (Figure_Sea_temperature == 'I') {
		Sea_figure_FS = SF_W - ((SF_W - SF_C) * (Figure_LatM - 35)) / 21.0;
	}

	return 0;
}
#endif
