FieldNeQuickParameters.java

  1. /* Copyright 2002-2024 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.models.earth.ionosphere;

  18. import org.hipparchus.Field;
  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.util.FastMath;
  21. import org.hipparchus.util.FieldSinCos;
  22. import org.hipparchus.util.MathArrays;
  23. import org.hipparchus.util.SinCos;
  24. import org.orekit.time.DateComponents;
  25. import org.orekit.time.DateTimeComponents;
  26. import org.orekit.time.TimeComponents;

  27. /**
  28.  * This class perfoms the computation of the parameters used by the NeQuick model.
  29.  *
  30.  * @author Bryan Cazabonne
  31.  *
  32.  * @see "European Union (2016). European GNSS (Galileo) Open Service-Ionospheric Correction
  33.  *       Algorithm for Galileo Single Frequency Users. 1.2."
  34.  *
  35.  * @since 10.1
  36.  */
  37. class FieldNeQuickParameters <T extends CalculusFieldElement<T>> {

  38.     /** Solar zenith angle at day night transition, degrees. */
  39.     private static final double X0 = 86.23292796211615;

  40.     /** F2 layer maximum density. */
  41.     private final T nmF2;

  42.     /** F2 layer maximum density height [km]. */
  43.     private final T hmF2;

  44.     /** F1 layer maximum density height [km]. */
  45.     private final T hmF1;

  46.     /** E layer maximum density height [km]. */
  47.     private final T hmE;

  48.     /** F2 layer bottom thickness parameter [km]. */
  49.     private final T b2Bot;

  50.     /** F1 layer top thickness parameter [km]. */
  51.     private final T b1Top;

  52.     /** F1 layer bottom thickness parameter [km]. */
  53.     private final T b1Bot;

  54.     /** E layer top thickness parameter [km]. */
  55.     private final T beTop;

  56.     /** E layer bottom thickness parameter [km]. */
  57.     private final T beBot;

  58.     /** topside thickness parameter [km]. */
  59.     private final T h0;

  60.     /** Layer amplitudes. */
  61.     private final T[] amplitudes;

  62.     /**
  63.      * Build a new instance.
  64.      * @param field field of the elements
  65.      * @param dateTime current date time components
  66.      * @param f2 F2 coefficients used by the F2 layer
  67.      * @param fm3 Fm3 coefficients used by the F2 layer
  68.      * @param latitude latitude of a point along the integration path, in radians
  69.      * @param longitude longitude of a point along the integration path, in radians
  70.      * @param alpha effective ionisation level coefficients
  71.      * @param modipGrip modip grid
  72.      */
  73.     FieldNeQuickParameters(final Field<T> field, final DateTimeComponents dateTime, final double[][][] f2,
  74.                            final double[][][] fm3, final T latitude, final T longitude,
  75.                            final double[] alpha, final double[][] modipGrip) {

  76.         // Zero
  77.         final T zero = field.getZero();

  78.         // MODIP in degrees
  79.         final T modip = computeMODIP(latitude, longitude, modipGrip);
  80.         // Effective ionisation level Az
  81.         final T az = computeAz(modip, alpha);
  82.         // Effective sunspot number (Eq. 19)
  83.         final T azr = FastMath.sqrt(az.subtract(63.7).multiply(1123.6).add(167273.0)).subtract(408.99);
  84.         // Date and Time components
  85.         final DateComponents date = dateTime.getDate();
  86.         final TimeComponents time = dateTime.getTime();
  87.         // Hours
  88.         final double hours  = time.getSecondsInUTCDay() / 3600.0;
  89.         // Effective solar zenith angle in radians
  90.         final T xeff = computeEffectiveSolarAngle(date.getMonth(), hours, latitude, longitude);

  91.         // Coefficients for F2 layer parameters
  92.         // Compute the array of interpolated coefficients for foF2 (Eq. 44)
  93.         final T[][] af2 = MathArrays.buildArray(field, 76, 13);
  94.         for (int j = 0; j < 76; j++) {
  95.             for (int k = 0; k < 13; k++ ) {
  96.                 af2[j][k] = azr.multiply(0.01).negate().add(1.0).multiply(f2[0][j][k]).add(azr.multiply(0.01).multiply(f2[1][j][k]));
  97.             }
  98.         }

  99.         // Compute the array of interpolated coefficients for M(3000)F2 (Eq. 46)
  100.         final T[][] am3 = MathArrays.buildArray(field, 49, 9);
  101.         for (int j = 0; j < 49; j++) {
  102.             for (int k = 0; k < 9; k++ ) {
  103.                 am3[j][k] = azr.multiply(0.01).negate().add(1.0).multiply(fm3[0][j][k]).add(azr.multiply(0.01).multiply(fm3[1][j][k]));
  104.             }
  105.         }

  106.         // E layer maximum density height in km (Eq. 78)
  107.         this.hmE = field.getZero().newInstance(120.0);
  108.         // E layer critical frequency in MHz
  109.         final T foE = computefoE(date.getMonth(), az, xeff, latitude);
  110.         // E layer maximum density in 10^11 m-3 (Eq. 36)
  111.         final T nmE = foE.multiply(foE).multiply(0.124);

  112.         // Time argument (Eq. 49)
  113.         final double t = FastMath.toRadians(15 * hours) - FastMath.PI;
  114.         // Compute Fourier time series for foF2 and M(3000)F2
  115.         final T[] cf2 = computeCF2(field, af2, t);
  116.         final T[] cm3 = computeCm3(field, am3, t);
  117.         // F2 layer critical frequency in MHz
  118.         final T foF2 = computefoF2(field, modip, cf2, latitude, longitude);
  119.         // Maximum Usable Frequency factor
  120.         final T mF2  = computeMF2(field, modip, cm3, latitude, longitude);
  121.         // F2 layer maximum density in 10^11 m-3
  122.         this.nmF2 = foF2.multiply(foF2).multiply(0.124);
  123.         // F2 layer maximum density height in km
  124.         this.hmF2 = computehmF2(field, foE, foF2, mF2);

  125.         // F1 layer critical frequency in MHz
  126.         final T foF1 = computefoF1(field, foE, foF2);
  127.         // F1 layer maximum density in 10^11 m-3
  128.         final T nmF1;
  129.         if (foF1.getReal() <= 0.0 && foE.getReal() > 2.0) {
  130.             final T foEpopf = foE.add(0.5);
  131.             nmF1 = foEpopf.multiply(foEpopf).multiply(0.124);
  132.         } else {
  133.             nmF1 = foF1.multiply(foF1).multiply(0.124);
  134.         }
  135.         // F1 layer maximum density height in km
  136.         this.hmF1 = hmF2.add(hmE).multiply(0.5);

  137.         // Thickness parameters (Eq. 85 to 89)
  138.         final T a = clipExp(FastMath.log(foF2.multiply(foF2)).multiply(0.857).add(FastMath.log(mF2).multiply(2.02)).add(-3.467)).multiply(0.01);
  139.         this.b2Bot = nmF2.divide(a).multiply(0.385);
  140.         this.b1Top = hmF2.subtract(hmF1).multiply(0.3);
  141.         this.b1Bot = hmF1.subtract(hmE).multiply(0.5);
  142.         this.beTop = FastMath.max(b1Bot, zero.newInstance(7.0));
  143.         this.beBot = zero.newInstance(5.0);

  144.         // Layer amplitude coefficients
  145.         this.amplitudes = computeLayerAmplitudes(field, nmE, nmF1, foF1);

  146.         // Topside thickness parameter
  147.         this.h0 = computeH0(field, date.getMonth(), azr);
  148.     }

  149.     /**
  150.      * Get the F2 layer maximum density.
  151.      * @return nmF2
  152.      */
  153.     public T getNmF2() {
  154.         return nmF2;
  155.     }

  156.     /**
  157.      * Get the F2 layer maximum density height.
  158.      * @return hmF2 in km
  159.      */
  160.     public T getHmF2() {
  161.         return hmF2;
  162.     }

  163.     /**
  164.      * Get the F1 layer maximum density height.
  165.      * @return hmF1 in km
  166.      */
  167.     public T getHmF1() {
  168.         return hmF1;
  169.     }

  170.     /**
  171.      * Get the E layer maximum density height.
  172.      * @return hmE in km
  173.      */
  174.     public T getHmE() {
  175.         return hmE;
  176.     }

  177.     /**
  178.      * Get the F2 layer thickness parameter (bottom).
  179.      * @return B2Bot in km
  180.      */
  181.     public T getB2Bot() {
  182.         return b2Bot;
  183.     }

  184.     /**
  185.      * Get the F1 layer thickness parameter (top).
  186.      * @return B1Top in km
  187.      */
  188.     public T getB1Top() {
  189.         return b1Top;
  190.     }

  191.     /**
  192.      * Get the F1 layer thickness parameter (bottom).
  193.      * @return B1Bot in km
  194.      */
  195.     public T getB1Bot() {
  196.         return b1Bot;
  197.     }

  198.     /**
  199.      * Get the E layer thickness parameter (bottom).
  200.      * @return BeBot in km
  201.      */
  202.     public T getBEBot() {
  203.         return beBot;
  204.     }

  205.     /**
  206.      * Get the E layer thickness parameter (top).
  207.      * @return BeTop in km
  208.      */
  209.     public T getBETop() {
  210.         return beTop;
  211.     }

  212.     /**
  213.      * Get the F2, F1 and E layer amplitudes.
  214.      * <p>
  215.      * The resulting element is an array having the following form:
  216.      * <ul>
  217.      * <li>double[0] = A1 → F2 layer amplitude
  218.      * <li>double[1] = A2 → F1 layer amplitude
  219.      * <li>double[2] = A3 → E  layer amplitude
  220.      * </ul>
  221.      * @return layer amplitudes
  222.      */
  223.     public T[] getLayerAmplitudes() {
  224.         return amplitudes.clone();
  225.     }

  226.     /**
  227.      * Get the topside thickness parameter H0.
  228.      * @return H0 in km
  229.      */
  230.     public T getH0() {
  231.         return h0;
  232.     }

  233.     /**
  234.      * Computes the value of the modified dip latitude (MODIP) for the
  235.      * given latitude and longitude.
  236.      *
  237.      * @param lat receiver latitude, radians
  238.      * @param lon receiver longitude, radians
  239.      * @param stModip modip grid
  240.      * @return the MODIP in degrees
  241.      */
  242.     private T computeMODIP(final T lat, final T lon, final double[][] stModip) {

  243.         // Zero
  244.         final T zero = lat.getField().getZero();

  245.         // For the MODIP computation, the latitude and longitude have to be converted in degrees
  246.         final T latitude  = FastMath.toDegrees(lat);
  247.         final T longitude = FastMath.toDegrees(lon);

  248.         // Extreme cases
  249.         if (latitude.getReal() == 90.0 || latitude.getReal() == -90.0) {
  250.             return latitude;
  251.         }

  252.         // Auxiliary parameter l (Eq. 6 to 8)
  253.         final int lF = (int) ((longitude.getReal() + 180) * 0.1);
  254.         int l = lF - 2;
  255.         if (l < -2) {
  256.             l += 36;
  257.         } else if (l > 33) {
  258.             l -= 36;
  259.         }

  260.         // Auxiliary parameter a (Eq. 9 to 11)
  261.         final T a  = latitude.add(90).multiply(0.2).add(1.0);
  262.         final T aF = FastMath.floor(a);
  263.         // Eq. 10
  264.         final T x = a.subtract(aF);
  265.         // Eq. 11
  266.         final int i = (int) aF.getReal() - 2;

  267.         // zi coefficients (Eq. 12 and 13)
  268.         final T z1 = interpolate(zero.add(stModip[i + 1][l + 2]), zero.add(stModip[i + 2][l + 2]),
  269.                                       zero.add(stModip[i + 3][l + 2]), zero.add(stModip[i + 4][l + 2]), x);
  270.         final T z2 = interpolate(zero.add(stModip[i + 1][l + 3]), zero.add(stModip[i + 2][l + 3]),
  271.                                       zero.add(stModip[i + 3][l + 3]), zero.add(stModip[i + 4][l + 3]), x);
  272.         final T z3 = interpolate(zero.add(stModip[i + 1][l + 4]), zero.add(stModip[i + 2][l + 4]),
  273.                                       zero.add(stModip[i + 3][l + 4]), zero.add(stModip[i + 4][l + 4]), x);
  274.         final T z4 = interpolate(zero.add(stModip[i + 1][l + 5]), zero.add(stModip[i + 2][l + 5]),
  275.                                       zero.add(stModip[i + 3][l + 5]), zero.add(stModip[i + 4][l + 5]), x);

  276.         // Auxiliary parameter b (Eq. 14 and 15)
  277.         final T b  = longitude.add(180).multiply(0.1);
  278.         final T bF = FastMath.floor(b);
  279.         final T y  = b.subtract(bF);

  280.         // MODIP (Ref Eq. 16)
  281.         final T modip = interpolate(z1, z2, z3, z4, y);

  282.         return modip;
  283.     }

  284.     /**
  285.      * This method computes the effective ionisation level Az.
  286.      * <p>
  287.      * This parameter is used for the computation of the Total Electron Content (TEC).
  288.      * </p>
  289.      * @param modip modified dip latitude (MODIP) in degrees
  290.      * @param alpha effective ionisation level coefficients
  291.      * @return the ionisation level Az
  292.      */
  293.     private T computeAz(final T modip, final double[] alpha) {
  294.         // Field
  295.         final Field<T> field = modip.getField();
  296.         // Zero
  297.         final T zero = field.getZero();
  298.         // Particular condition (Eq. 17)
  299.         if (alpha[0] == 0.0 && alpha[1] == 0.0 && alpha[2] == 0.0) {
  300.             return zero.newInstance(63.7);
  301.         }
  302.         // Az = a0 + modip * a1 + modip^2 * a2 (Eq. 18)
  303.         T az = modip.multiply(alpha[2]).add(alpha[1]).multiply(modip).add(alpha[0]);
  304.         // If Az < 0 -> Az = 0
  305.         az = FastMath.max(zero, az);
  306.         // If Az > 400 -> Az = 400
  307.         az = FastMath.min(zero.newInstance(400.0), az);
  308.         return az;
  309.     }

  310.     /**
  311.      * This method computes the effective solar zenith angle.
  312.      * <p>
  313.      * The effective solar zenith angle is compute as a function of the
  314.      * solar zenith angle and the solar zenith angle at day night transition.
  315.      * </p>
  316.      * @param month current month of the year
  317.      * @param hours universal time (hours)
  318.      * @param latitude in radians
  319.      * @param longitude in radians
  320.      * @return the effective solar zenith angle, radians
  321.      */
  322.     private T computeEffectiveSolarAngle(final int month,
  323.                                          final double hours,
  324.                                          final T latitude,
  325.                                          final T longitude) {
  326.         // Zero
  327.         final T zero = latitude.getField().getZero();
  328.         // Local time (Eq.4)
  329.         final T lt = longitude.divide(FastMath.toRadians(15.0)).add(hours);
  330.         // Day of year at the middle of the month (Eq. 20)
  331.         final double dy = 30.5 * month - 15.0;
  332.         // Time (Eq. 21)
  333.         final double t = dy + (18 - hours) / 24;
  334.         // Arguments am and al (Eq. 22 and 23)
  335.         final double am = FastMath.toRadians(0.9856 * t - 3.289);
  336.         final double al = am + FastMath.toRadians(1.916 * FastMath.sin(am) + 0.020 * FastMath.sin(2.0 * am) + 282.634);
  337.         // Sine and cosine of solar declination (Eq. 24 and 25)
  338.         final double sDec = 0.39782 * FastMath.sin(al);
  339.         final double cDec = FastMath.sqrt(1. - sDec * sDec);
  340.         // Solar zenith angle, deg (Eq. 26 and 27)
  341.         final FieldSinCos<T> scLat   = FastMath.sinCos(latitude);
  342.         final T coef    = lt.negate().add(12.0).multiply(FastMath.PI / 12);
  343.         final T cZenith = scLat.sin().multiply(sDec).add(scLat.cos().multiply(cDec).multiply(FastMath.cos(coef)));
  344.         final T angle   = FastMath.atan2(FastMath.sqrt(cZenith.multiply(cZenith).negate().add(1.0)), cZenith);
  345.         final T x       = FastMath.toDegrees(angle);
  346.         // Effective solar zenith angle (Eq. 28)
  347.         final T xeff = join(clipExp(x.multiply(0.2).negate().add(20.0)).multiply(0.24).negate().add(90.0), x,
  348.                 zero.newInstance(12.0), x.subtract(X0));
  349.         return FastMath.toRadians(xeff);
  350.     }

  351.     /**
  352.      * This method computes the E layer critical frequency at a given location.
  353.      * @param month current month
  354.      * @param az ffective ionisation level
  355.      * @param xeff effective solar zenith angle in radians
  356.      * @param latitude latitude in radians
  357.      * @return the E layer critical frequency at a given location in MHz
  358.      */
  359.     private T computefoE(final int month, final T az,
  360.                          final T xeff, final T latitude) {
  361.         // The latitude has to be converted in degrees
  362.         final T lat = FastMath.toDegrees(latitude);
  363.         // Square root of the effective ionisation level
  364.         final T sqAz = FastMath.sqrt(az);
  365.         // seas parameter (Eq. 30 to 32)
  366.         final int seas;
  367.         if (month == 1 || month == 2 || month == 11 || month == 12) {
  368.             seas = -1;
  369.         } else if (month == 3 || month == 4 || month == 9 || month == 10) {
  370.             seas = 0;
  371.         } else {
  372.             seas = 1;
  373.         }
  374.         // Latitudinal dependence (Eq. 33 and 34)
  375.         final T ee = clipExp(lat.multiply(0.3));
  376.         final T seasp = ee.subtract(1.0).divide(ee.add(1.0)).multiply(seas);
  377.         // Critical frequency (Eq. 35)
  378.         final T coef = seasp.multiply(0.019).negate().add(1.112);
  379.         final T foE = FastMath.sqrt(coef .multiply(coef).multiply(sqAz).multiply(FastMath.cos(xeff).pow(0.6)).add(0.49));
  380.         return foE;
  381.     }

  382.     /**
  383.      * Computes the F2 layer height of maximum electron density.
  384.      * @param field field of the elements
  385.      * @param foE E layer layer critical frequency in MHz
  386.      * @param foF2 F2 layer layer critical frequency in MHz
  387.      * @param mF2 maximum usable frequency factor
  388.      * @return hmF2 in km
  389.      */
  390.     private T computehmF2(final Field<T> field, final T foE, final T foF2, final T mF2) {
  391.         // Zero
  392.         final T zero = field.getZero();
  393.         // Ratio
  394.         final T fo = foF2.divide(foE);
  395.         final T ratio = join(fo, zero.newInstance(1.75), zero.newInstance(20.0), fo.subtract(1.75));

  396.         // deltaM parameter
  397.         T deltaM = zero.subtract(0.012);
  398.         if (foE.getReal() >= 1e-30) {
  399.             deltaM = deltaM.add(ratio.subtract(1.215).divide(0.253).reciprocal());
  400.         }

  401.         // hmF2 Eq. 80
  402.         final T mF2Sq = mF2.square();
  403.         final T temp  = FastMath.sqrt(mF2Sq.multiply(0.0196).add(1.0).divide(mF2Sq.multiply(1.2967).subtract(1.0)));
  404.         final T height  = mF2.multiply(1490.0).multiply(temp).divide(mF2.add(deltaM)).subtract(176.0);
  405.         return height;
  406.     }

  407.     /**
  408.      * Computes cf2 coefficients.
  409.      * @param field field of the elements
  410.      * @param af2 interpolated coefficients for foF2
  411.      * @param t time argument
  412.      * @return the cf2 coefficients array
  413.      */
  414.     private T[] computeCF2(final Field<T> field, final T[][] af2, final double t) {
  415.         // Eq. 50
  416.         final T[] cf2 = MathArrays.buildArray(field, 76);
  417.         for (int i = 0; i < cf2.length; i++) {
  418.             T sum = field.getZero();
  419.             for (int k = 0; k < 6; k++) {
  420.                 final SinCos sc = FastMath.sinCos((k + 1) * t);
  421.                 sum = sum.add(af2[i][2 * k + 1].multiply(sc.sin()).add(af2[i][2 * (k + 1)].multiply(sc.cos())));
  422.             }
  423.             cf2[i] = af2[i][0].add(sum);
  424.         }
  425.         return cf2;
  426.     }

  427.     /**
  428.      * Computes Cm3 coefficients.
  429.      * @param field field of the elements
  430.      * @param am3 interpolated coefficients for foF2
  431.      * @param t time argument
  432.      * @return the Cm3 coefficients array
  433.      */
  434.     private T[] computeCm3(final Field<T> field, final T[][] am3, final double t) {
  435.         // Eq. 51
  436.         final T[] cm3 = MathArrays.buildArray(field, 49);
  437.         for (int i = 0; i < cm3.length; i++) {
  438.             T sum = field.getZero();
  439.             for (int k = 0; k < 4; k++) {
  440.                 final SinCos sc = FastMath.sinCos((k + 1) * t);
  441.                 sum = sum.add(am3[i][2 * k + 1].multiply(sc.sin()).add(am3[i][2 * (k + 1)].multiply(sc.cos())));
  442.             }
  443.             cm3[i] = am3[i][0].add(sum);
  444.         }
  445.         return cm3;
  446.     }

  447.     /**
  448.      * This method computes the F2 layer critical frequency.
  449.      * @param field field of the elements
  450.      * @param modip modified DIP latitude, in degrees
  451.      * @param cf2 Fourier time series for foF2
  452.      * @param latitude latitude in radians
  453.      * @param longitude longitude in radians
  454.      * @return the F2 layer critical frequency, MHz
  455.      */
  456.     private T computefoF2(final Field<T> field, final T modip, final T[] cf2,
  457.                           final T latitude, final T longitude) {

  458.         // One
  459.         final T one = field.getOne();

  460.         // Legendre grades (Eq. 63)
  461.         final int[] q = new int[] {
  462.             12, 12, 9, 5, 2, 1, 1, 1, 1
  463.         };

  464.         // Array for geographic terms
  465.         final T[] g = MathArrays.buildArray(field, cf2.length);
  466.         g[0] = one;

  467.         // MODIP coefficients Eq. 57
  468.         final T sinMODIP = FastMath.sin(FastMath.toRadians(modip));
  469.         final T[] m = MathArrays.buildArray(field, 12);
  470.         m[0] = one;
  471.         for (int i = 1; i < q[0]; i++) {
  472.             m[i] = sinMODIP.multiply(m[i - 1]);
  473.             g[i] = m[i];
  474.         }

  475.         // Latitude coefficients (Eq. 58)
  476.         final T cosLat = FastMath.cos(latitude);
  477.         final T[] p = MathArrays.buildArray(field, 8);
  478.         p[0] = cosLat;
  479.         for (int n = 2; n < 9; n++) {
  480.             p[n - 1] = cosLat.multiply(p[n - 2]);
  481.         }

  482.         // latitude and longitude terms
  483.         int index = 12;
  484.         for (int i = 1; i < q.length; i++) {
  485.             final FieldSinCos<T> sc = FastMath.sinCos(longitude.multiply(i));
  486.             for (int j = 0; j < q[i]; j++) {
  487.                 g[index++] = m[j].multiply(p[i - 1]).multiply(sc.cos());
  488.                 g[index++] = m[j].multiply(p[i - 1]).multiply(sc.sin());
  489.             }
  490.         }

  491.         // Compute foF2 by linear combination
  492.         final T frequency = one.linearCombination(g, cf2);
  493.         return frequency;
  494.     }

  495.     /**
  496.      * This method computes the Maximum Usable Frequency factor.
  497.      * @param field field of the elements
  498.      * @param modip modified DIP latitude, in degrees
  499.      * @param cm3 Fourier time series for M(3000)F2
  500.      * @param latitude latitude in radians
  501.      * @param longitude longitude in radians
  502.      * @return the Maximum Usable Frequency factor
  503.      */
  504.     private T computeMF2(final Field<T> field, final T modip, final T[] cm3,
  505.                          final T latitude, final T longitude) {

  506.         // One
  507.         final T one = field.getOne();
  508.         // Legendre grades (Eq. 71)
  509.         final int[] r = new int[] {
  510.             7, 8, 6, 3, 2, 1, 1
  511.         };

  512.         // Array for geographic terms
  513.         final T[] g = MathArrays.buildArray(field, cm3.length);
  514.         g[0] = one;

  515.         // MODIP coefficients Eq. 57
  516.         final T sinMODIP = FastMath.sin(FastMath.toRadians(modip));
  517.         final T[] m = MathArrays.buildArray(field, 12);
  518.         m[0] = one;
  519.         for (int i = 1; i < 12; i++) {
  520.             m[i] = sinMODIP.multiply(m[i - 1]);
  521.             if (i < 7) {
  522.                 g[i] = m[i];
  523.             }
  524.         }

  525.         // Latitude coefficients (Eq. 58)
  526.         final T cosLat = FastMath.cos(latitude);
  527.         final T[] p = MathArrays.buildArray(field, 8);
  528.         p[0] = cosLat;
  529.         for (int n = 2; n < 9; n++) {
  530.             p[n - 1] = cosLat.multiply(p[n - 2]);
  531.         }

  532.         // latitude and longitude terms
  533.         int index = 7;
  534.         for (int i = 1; i < r.length; i++) {
  535.             final FieldSinCos<T> sc = FastMath.sinCos(longitude.multiply(i));
  536.             for (int j = 0; j < r[i]; j++) {
  537.                 g[index++] = m[j].multiply(p[i - 1]).multiply(sc.cos());
  538.                 g[index++] = m[j].multiply(p[i - 1]).multiply(sc.sin());
  539.             }
  540.         }

  541.         // Compute m3000 by linear combination
  542.         final T m3000 = one.linearCombination(g, cm3);
  543.         return m3000;
  544.     }

  545.     /**
  546.      * This method computes the F1 layer critical frequency.
  547.      * <p>
  548.      * This computation performs the algorithm exposed in Annex F
  549.      * of the reference document.
  550.      * </p>
  551.      * @param field field of the elements
  552.      * @param foE the E layer critical frequency, MHz
  553.      * @return the F1 layer critical frequency, MHz
  554.      * @param foF2 the F2 layer critical frequency, MHz
  555.      */
  556.     private T computefoF1(final Field<T> field, final T foE, final T foF2) {
  557.         final T zero = field.getZero();
  558.         final T temp  = join(foE.multiply(1.4), zero, zero.newInstance(1000.0), foE.subtract(2.0));
  559.         final T temp2 = join(zero, temp, zero.newInstance(1000.0), foE.subtract(temp));
  560.         final T value = join(temp2, temp2.multiply(0.85), zero.newInstance(60.0), foF2.multiply(0.85).subtract(temp2));
  561.         if (value.getReal() < 1.0E-6) {
  562.             return zero;
  563.         } else {
  564.             return value;
  565.         }
  566.     }

  567.     /**
  568.      * This method allows the computation of the F2, F1 and E layer amplitudes.
  569.      * <p>
  570.      * The resulting element is an array having the following form:
  571.      * <ul>
  572.      * <li>double[0] = A1 → F2 layer amplitude
  573.      * <li>double[1] = A2 → F1 layer amplitude
  574.      * <li>double[2] = A3 → E  layer amplitude
  575.      * </ul>
  576.      * </p>
  577.      * @param field field of the elements
  578.      * @param nmE E layer maximum density in 10^11 m-3
  579.      * @param nmF1 F1 layer maximum density in 10^11 m-3
  580.      * @param foF1 F1 layer critical frequency in MHz
  581.      * @return a three components array containing the layer amplitudes
  582.      */
  583.     private T[] computeLayerAmplitudes(final Field<T> field, final T nmE, final T nmF1, final T foF1) {
  584.         // Zero
  585.         final T zero = field.getZero();

  586.         // Initialize array
  587.         final T[] amplitude = MathArrays.buildArray(field, 3);

  588.         // F2 layer amplitude (Eq. 90)
  589.         final T a1 = nmF2.multiply(4.0);
  590.         amplitude[0] = a1;

  591.         // F1 and E layer amplitudes (Eq. 91 to 98)
  592.         if (foF1.getReal() < 0.5) {
  593.             amplitude[1] = zero;
  594.             amplitude[2] = nmE.subtract(epst(a1, hmF2, b2Bot, hmE)).multiply(4.0);
  595.         } else {
  596.             T a2a = zero;
  597.             T a3a = nmE.multiply(4.0);
  598.             for (int i = 0; i < 5; i++) {
  599.                 a2a = nmF1.subtract(epst(a1, hmF2, b2Bot, hmF1)).subtract(epst(a3a, hmE, beTop, hmF1)).multiply(4.0);
  600.                 a2a = join(a2a, nmF1.multiply(0.8), field.getOne(), a2a.subtract(nmF1.multiply(0.8)));
  601.                 a3a = nmE.subtract(epst(a2a, hmF1, b1Bot, hmE)).subtract(epst(a1, hmF2, b2Bot, hmE)).multiply(4.0);
  602.             }
  603.             amplitude[1] = a2a;
  604.             amplitude[2] = join(a3a, zero.newInstance(0.05), zero.newInstance(60.0), a3a.subtract(0.005));
  605.         }

  606.         return amplitude;
  607.     }

  608.     /**
  609.      * This method computes the topside thickness parameter H0.
  610.      *
  611.      * @param field field of the elements
  612.      * @param month current month
  613.      * @param azr effective sunspot number
  614.      * @return H0 in km
  615.      */
  616.     private T computeH0(final Field<T> field, final int month, final T azr) {

  617.         // One
  618.         final T one = field.getOne();

  619.         // Auxiliary parameter ka (Eq. 99 and 100)
  620.         final T ka;
  621.         if (month > 3 && month < 10) {
  622.             // month = 4,5,6,7,8,9
  623.             ka = azr.multiply(0.014).add(hmF2.multiply(0.008)).negate().add(6.705);
  624.         } else {
  625.             // month = 1,2,3,10,11,12
  626.             final T ratio = hmF2.divide(b2Bot);
  627.             ka = ratio.multiply(ratio).multiply(0.097).add(nmF2.multiply(0.153)).add(-7.77);
  628.         }

  629.         // Auxiliary parameter kb (Eq. 101 and 102)
  630.         T kb = join(ka, one.newInstance(2.0), one, ka.subtract(2.0));
  631.         kb = join(one.newInstance(8.0), kb, one, kb.subtract(8.0));

  632.         // Auxiliary parameter Ha (Eq. 103)
  633.         final T hA = kb.multiply(b2Bot);

  634.         // Auxiliary parameters x and v (Eq. 104 and 105)
  635.         final T x = hA.subtract(150.0).multiply(0.01);
  636.         final T v = x.multiply(0.041163).subtract(0.183981).multiply(x).add(1.424472);

  637.         // Topside thickness parameter (Eq. 106)
  638.         final T h = hA.divide(v);
  639.         return h;
  640.     }

  641.     /**
  642.      * A clipped exponential function.
  643.      * <p>
  644.      * This function, describe in section F.2.12.2 of the reference document, is
  645.      * recommanded for the computation of exponential values.
  646.      * </p>
  647.      * @param power power for exponential function
  648.      * @return clipped exponential value
  649.      */
  650.     private T clipExp(final T power) {
  651.         final T zero = power.getField().getZero();
  652.         if (power.getReal() > 80.0) {
  653.             return zero.newInstance(5.5406E34);
  654.         } else if (power.getReal() < -80) {
  655.             return zero.newInstance(1.8049E-35);
  656.         } else {
  657.             return FastMath.exp(power);
  658.         }
  659.     }

  660.     /**
  661.      * This method provides a third order interpolation function
  662.      * as recommended in the reference document (Ref Eq. 128 to Eq. 138)
  663.      *
  664.      * @param z1 z1 coefficient
  665.      * @param z2 z2 coefficient
  666.      * @param z3 z3 coefficient
  667.      * @param z4 z4 coefficient
  668.      * @param x position
  669.      * @return a third order interpolation
  670.      */
  671.     private T interpolate(final T z1, final T z2,
  672.                           final T z3, final T z4,
  673.                           final T x) {

  674.         if (FastMath.abs(2.0 * x.getReal()) < 1e-10) {
  675.             return z2;
  676.         }

  677.         final T delta = x.multiply(2.0).subtract(1.0);
  678.         final T g1 = z3.add(z2);
  679.         final T g2 = z3.subtract(z2);
  680.         final T g3 = z4.add(z1);
  681.         final T g4 = z4.subtract(z1).divide(3.0);
  682.         final T a0 = g1.multiply(9.0).subtract(g3);
  683.         final T a1 = g2.multiply(9.0).subtract(g4);
  684.         final T a2 = g3.subtract(g1);
  685.         final T a3 = g4.subtract(g2);
  686.         final T zx = delta.multiply(a3).add(a2).multiply(delta).add(a1).multiply(delta).add(a0).multiply(0.0625);

  687.         return zx;
  688.     }

  689.     /**
  690.      * Allows smooth joining of functions f1 and f2
  691.      * (i.e. continuous first derivatives) at origin.
  692.      * <p>
  693.      * This function, describe in section F.2.12.1 of the reference document, is
  694.      * recommanded for computational efficiency.
  695.      * </p>
  696.      * @param dF1 first function
  697.      * @param dF2 second function
  698.      * @param dA width of transition region
  699.      * @param dX x value
  700.      * @return the computed value
  701.      */
  702.     private T join(final T dF1, final T dF2,
  703.                    final T dA, final T dX) {
  704.         final T ee = clipExp(dA.multiply(dX));
  705.         return dF1.multiply(ee).add(dF2).divide(ee.add(1.0));
  706.     }

  707.     /**
  708.      * The Epstein function.
  709.      * <p>
  710.      * This function, describe in section 2.5.1 of the reference document, is used
  711.      * as a basis analytical function in NeQuick for the construction of the ionospheric layers.
  712.      * </p>
  713.      * @param x x parameter
  714.      * @param y y parameter
  715.      * @param z z parameter
  716.      * @param w w parameter
  717.      * @return value of the epstein function
  718.      */
  719.     private T epst(final T x, final T y,
  720.                    final T z, final T w) {
  721.         final T ex  = clipExp(w.subtract(y).divide(z));
  722.         final T opex = ex.add(1.0);
  723.         final T epst = x.multiply(ex).divide(opex.multiply(opex));
  724.         return epst;
  725.     }

  726. }