package com.base.orbit.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class LinkCount {

    public static double Re = 6378;

//    public static double elevation = 5.0;//地面天线仰角

    public static double K = -198.6;//玻尔兹曼常数K

    public static double Gcoding = 7;//编码增益

    public static double SNR = 11.5;//解调门限


    /**
     * eirp 卫星发射EIRP dbm
     * f  载波频率 MHz
     * h 卫星轨道高度  Km
     * l1 电离层损耗 dB
     * l2 极化误差损耗 dB
     * l3 雨衰 dB
     * l4 其他损耗 dB
     * GT 地面站GT db/K
     * fb 信息速率 bps
     *
     * @return
     */
    public Double linkMargin(double eirp
            , double f
            , double h
            , double l1
            , double l2
            , double l3
            , double l4
            , double GT
            , double fb
            , double elevation
    ) {

        /*星地距离计算*/
        double elevationrad = Math.toRadians(elevation);
        double d = Math.sqrt(Math.pow(Re, 2) * Math.pow(Math.sin(elevationrad), 2) + 2 * h * Re + Math.pow(h, 2)) - Re * Math.sin(elevationrad);

        /*自由空间损失*/
        double lpath = 20 * Math.log10(d * Math.pow(10, 3)) + 20 * Math.log10(f * Math.pow(10, 6)) + 20 * Math.log10((4 * Math.PI) / (3 * Math.pow(10, 8)));

        /*C/NO  除以*/
        double cno = eirp + GT - lpath - l1 - l2 - l3 - l4 - K;
        /*Eb/N0  除以*/
        double EbNo = cno - 10 * Math.log10(fb);

        double Margin = EbNo + Gcoding - SNR;
        BigDecimal bigDecimal = new BigDecimal(Margin);
        double MarginVal = bigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        return MarginVal;
    }
}
