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  
19  import org.hipparchus.Field;
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.util.FastMath;
22  import org.hipparchus.util.FieldSinCos;
23  import org.hipparchus.util.MathArrays;
24  import org.hipparchus.util.SinCos;
25  import org.orekit.time.DateComponents;
26  import org.orekit.time.DateTimeComponents;
27  import org.orekit.time.TimeComponents;
28  
29  /**
30   * This class perfoms the computation of the parameters used by the NeQuick model.
31   *
32   * @author Bryan Cazabonne
33   *
34   * @see "European Union (2016). European GNSS (Galileo) Open Service-Ionospheric Correction
35   *       Algorithm for Galileo Single Frequency Users. 1.2."
36   *
37   * @since 10.1
38   */
39  class FieldNeQuickParameters <T extends CalculusFieldElement<T>> {
40  
41      /** Solar zenith angle at day night transition, degrees. */
42      private static final double X0 = 86.23292796211615;
43  
44      /** F2 layer maximum density. */
45      private final T nmF2;
46  
47      /** F2 layer maximum density height [km]. */
48      private final T hmF2;
49  
50      /** F1 layer maximum density height [km]. */
51      private final T hmF1;
52  
53      /** E layer maximum density height [km]. */
54      private final T hmE;
55  
56      /** F2 layer bottom thickness parameter [km]. */
57      private final T b2Bot;
58  
59      /** F1 layer top thickness parameter [km]. */
60      private final T b1Top;
61  
62      /** F1 layer bottom thickness parameter [km]. */
63      private final T b1Bot;
64  
65      /** E layer top thickness parameter [km]. */
66      private final T beTop;
67  
68      /** E layer bottom thickness parameter [km]. */
69      private final T beBot;
70  
71      /** topside thickness parameter [km]. */
72      private final T h0;
73  
74      /** Layer amplitudes. */
75      private final T[] amplitudes;
76  
77      /**
78       * Build a new instance.
79       * @param field field of the elements
80       * @param dateTime current date time components
81       * @param f2 F2 coefficients used by the F2 layer
82       * @param fm3 Fm3 coefficients used by the F2 layer
83       * @param latitude latitude of a point along the integration path, in radians
84       * @param longitude longitude of a point along the integration path, in radians
85       * @param alpha effective ionisation level coefficients
86       * @param modipGrip modip grid
87       */
88      FieldNeQuickParameters(final Field<T> field, final DateTimeComponents dateTime, final double[][][] f2,
89                             final double[][][] fm3, final T latitude, final T longitude,
90                             final double[] alpha, final double[][] modipGrip) {
91  
92          // Zero
93          final T zero = field.getZero();
94  
95          // MODIP in degrees
96          final T modip = computeMODIP(latitude, longitude, modipGrip);
97          // Effective ionisation level Az
98          final T az = computeAz(modip, alpha);
99          // Effective sunspot number (Eq. 19)
100         final T azr = FastMath.sqrt(az.subtract(63.7).multiply(1123.6).add(167273.0)).subtract(408.99);
101         // Date and Time components
102         final DateComponents date = dateTime.getDate();
103         final TimeComponents time = dateTime.getTime();
104         // Hours
105         final double hours  = time.getSecondsInUTCDay() / 3600.0;
106         // Effective solar zenith angle in radians
107         final T xeff = computeEffectiveSolarAngle(date.getMonth(), hours, latitude, longitude);
108 
109         // Coefficients for F2 layer parameters
110         // Compute the array of interpolated coefficients for foF2 (Eq. 44)
111         final T[][] af2 = MathArrays.buildArray(field, 76, 13);
112         for (int j = 0; j < 76; j++) {
113             for (int k = 0; k < 13; k++ ) {
114                 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]));
115             }
116         }
117 
118         // Compute the array of interpolated coefficients for M(3000)F2 (Eq. 46)
119         final T[][] am3 = MathArrays.buildArray(field, 49, 9);
120         for (int j = 0; j < 49; j++) {
121             for (int k = 0; k < 9; k++ ) {
122                 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]));
123             }
124         }
125 
126         // E layer maximum density height in km (Eq. 78)
127         this.hmE = field.getZero().newInstance(120.0);
128         // E layer critical frequency in MHz
129         final T foE = computefoE(date.getMonth(), az, xeff, latitude);
130         // E layer maximum density in 10^11 m-3 (Eq. 36)
131         final T nmE = foE.multiply(foE).multiply(0.124);
132 
133         // Time argument (Eq. 49)
134         final double t = FastMath.toRadians(15 * hours) - FastMath.PI;
135         // Compute Fourier time series for foF2 and M(3000)F2
136         final T[] cf2 = computeCF2(field, af2, t);
137         final T[] cm3 = computeCm3(field, am3, t);
138         // F2 layer critical frequency in MHz
139         final T foF2 = computefoF2(field, modip, cf2, latitude, longitude);
140         // Maximum Usable Frequency factor
141         final T mF2  = computeMF2(field, modip, cm3, latitude, longitude);
142         // F2 layer maximum density in 10^11 m-3
143         this.nmF2 = foF2.multiply(foF2).multiply(0.124);
144         // F2 layer maximum density height in km
145         this.hmF2 = computehmF2(field, foE, foF2, mF2);
146 
147         // F1 layer critical frequency in MHz
148         final T foF1 = computefoF1(field, foE, foF2);
149         // F1 layer maximum density in 10^11 m-3
150         final T nmF1;
151         if (foF1.getReal() <= 0.0 && foE.getReal() > 2.0) {
152             final T foEpopf = foE.add(0.5);
153             nmF1 = foEpopf.multiply(foEpopf).multiply(0.124);
154         } else {
155             nmF1 = foF1.multiply(foF1).multiply(0.124);
156         }
157         // F1 layer maximum density height in km
158         this.hmF1 = hmF2.add(hmE).multiply(0.5);
159 
160         // Thickness parameters (Eq. 85 to 89)
161         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);
162         this.b2Bot = nmF2.divide(a).multiply(0.385);
163         this.b1Top = hmF2.subtract(hmF1).multiply(0.3);
164         this.b1Bot = hmF1.subtract(hmE).multiply(0.5);
165         this.beTop = FastMath.max(b1Bot, zero.newInstance(7.0));
166         this.beBot = zero.newInstance(5.0);
167 
168         // Layer amplitude coefficients
169         this.amplitudes = computeLayerAmplitudes(field, nmE, nmF1, foF1);
170 
171         // Topside thickness parameter
172         this.h0 = computeH0(field, date.getMonth(), azr);
173     }
174 
175     /**
176      * Get the F2 layer maximum density.
177      * @return nmF2
178      */
179     public T getNmF2() {
180         return nmF2;
181     }
182 
183     /**
184      * Get the F2 layer maximum density height.
185      * @return hmF2 in km
186      */
187     public T getHmF2() {
188         return hmF2;
189     }
190 
191     /**
192      * Get the F1 layer maximum density height.
193      * @return hmF1 in km
194      */
195     public T getHmF1() {
196         return hmF1;
197     }
198 
199     /**
200      * Get the E layer maximum density height.
201      * @return hmE in km
202      */
203     public T getHmE() {
204         return hmE;
205     }
206 
207     /**
208      * Get the F2 layer thickness parameter (bottom).
209      * @return B2Bot in km
210      */
211     public T getB2Bot() {
212         return b2Bot;
213     }
214 
215     /**
216      * Get the F1 layer thickness parameter (top).
217      * @return B1Top in km
218      */
219     public T getB1Top() {
220         return b1Top;
221     }
222 
223     /**
224      * Get the F1 layer thickness parameter (bottom).
225      * @return B1Bot in km
226      */
227     public T getB1Bot() {
228         return b1Bot;
229     }
230 
231     /**
232      * Get the E layer thickness parameter (bottom).
233      * @return BeBot in km
234      */
235     public T getBEBot() {
236         return beBot;
237     }
238 
239     /**
240      * Get the E layer thickness parameter (top).
241      * @return BeTop in km
242      */
243     public T getBETop() {
244         return beTop;
245     }
246 
247     /**
248      * Get the F2, F1 and E layer amplitudes.
249      * <p>
250      * The resulting element is an array having the following form:
251      * <ul>
252      * <li>double[0] = A1 → F2 layer amplitude
253      * <li>double[1] = A2 → F1 layer amplitude
254      * <li>double[2] = A3 → E  layer amplitude
255      * </ul>
256      * @return layer amplitudes
257      */
258     public T[] getLayerAmplitudes() {
259         return amplitudes.clone();
260     }
261 
262     /**
263      * Get the topside thickness parameter H0.
264      * @return H0 in km
265      */
266     public T getH0() {
267         return h0;
268     }
269 
270     /**
271      * Computes the value of the modified dip latitude (MODIP) for the
272      * given latitude and longitude.
273      *
274      * @param lat receiver latitude, radians
275      * @param lon receiver longitude, radians
276      * @param stModip modip grid
277      * @return the MODIP in degrees
278      */
279     private T computeMODIP(final T lat, final T lon, final double[][] stModip) {
280 
281         // Zero
282         final T zero = lat.getField().getZero();
283 
284         // For the MODIP computation, the latitude and longitude have to be converted in degrees
285         final T latitude  = FastMath.toDegrees(lat);
286         final T longitude = FastMath.toDegrees(lon);
287 
288         // Extreme cases
289         if (latitude.getReal() == 90.0 || latitude.getReal() == -90.0) {
290             return latitude;
291         }
292 
293         // Auxiliary parameter l (Eq. 6 to 8)
294         final int lF = (int) ((longitude.getReal() + 180) * 0.1);
295         int l = lF - 2;
296         if (l < -2) {
297             l += 36;
298         } else if (l > 33) {
299             l -= 36;
300         }
301 
302         // Auxiliary parameter a (Eq. 9 to 11)
303         final T a  = latitude.add(90).multiply(0.2).add(1.0);
304         final T aF = FastMath.floor(a);
305         // Eq. 10
306         final T x = a.subtract(aF);
307         // Eq. 11
308         final int i = (int) aF.getReal() - 2;
309 
310         // zi coefficients (Eq. 12 and 13)
311         final T z1 = interpolate(zero.add(stModip[i + 1][l + 2]), zero.add(stModip[i + 2][l + 2]),
312                                       zero.add(stModip[i + 3][l + 2]), zero.add(stModip[i + 4][l + 2]), x);
313         final T z2 = interpolate(zero.add(stModip[i + 1][l + 3]), zero.add(stModip[i + 2][l + 3]),
314                                       zero.add(stModip[i + 3][l + 3]), zero.add(stModip[i + 4][l + 3]), x);
315         final T z3 = interpolate(zero.add(stModip[i + 1][l + 4]), zero.add(stModip[i + 2][l + 4]),
316                                       zero.add(stModip[i + 3][l + 4]), zero.add(stModip[i + 4][l + 4]), x);
317         final T z4 = interpolate(zero.add(stModip[i + 1][l + 5]), zero.add(stModip[i + 2][l + 5]),
318                                       zero.add(stModip[i + 3][l + 5]), zero.add(stModip[i + 4][l + 5]), x);
319 
320         // Auxiliary parameter b (Eq. 14 and 15)
321         final T b  = longitude.add(180).multiply(0.1);
322         final T bF = FastMath.floor(b);
323         final T y  = b.subtract(bF);
324 
325         // MODIP (Ref Eq. 16)
326         final T modip = interpolate(z1, z2, z3, z4, y);
327 
328         return modip;
329     }
330 
331     /**
332      * This method computes the effective ionisation level Az.
333      * <p>
334      * This parameter is used for the computation of the Total Electron Content (TEC).
335      * </p>
336      * @param modip modified dip latitude (MODIP) in degrees
337      * @param alpha effective ionisation level coefficients
338      * @return the ionisation level Az
339      */
340     private T computeAz(final T modip, final double[] alpha) {
341         // Field
342         final Field<T> field = modip.getField();
343         // Zero
344         final T zero = field.getZero();
345         // Particular condition (Eq. 17)
346         if (alpha[0] == 0.0 && alpha[1] == 0.0 && alpha[2] == 0.0) {
347             return zero.newInstance(63.7);
348         }
349         // Az = a0 + modip * a1 + modip^2 * a2 (Eq. 18)
350         T az = modip.multiply(alpha[2]).add(alpha[1]).multiply(modip).add(alpha[0]);
351         // If Az < 0 -> Az = 0
352         az = FastMath.max(zero, az);
353         // If Az > 400 -> Az = 400
354         az = FastMath.min(zero.newInstance(400.0), az);
355         return az;
356     }
357 
358     /**
359      * This method computes the effective solar zenith angle.
360      * <p>
361      * The effective solar zenith angle is compute as a function of the
362      * solar zenith angle and the solar zenith angle at day night transition.
363      * </p>
364      * @param month current month of the year
365      * @param hours universal time (hours)
366      * @param latitude in radians
367      * @param longitude in radians
368      * @return the effective solar zenith angle, radians
369      */
370     private T computeEffectiveSolarAngle(final int month,
371                                          final double hours,
372                                          final T latitude,
373                                          final T longitude) {
374         // Zero
375         final T zero = latitude.getField().getZero();
376         // Local time (Eq.4)
377         final T lt = longitude.divide(FastMath.toRadians(15.0)).add(hours);
378         // Day of year at the middle of the month (Eq. 20)
379         final double dy = 30.5 * month - 15.0;
380         // Time (Eq. 21)
381         final double t = dy + (18 - hours) / 24;
382         // Arguments am and al (Eq. 22 and 23)
383         final double am = FastMath.toRadians(0.9856 * t - 3.289);
384         final double al = am + FastMath.toRadians(1.916 * FastMath.sin(am) + 0.020 * FastMath.sin(2.0 * am) + 282.634);
385         // Sine and cosine of solar declination (Eq. 24 and 25)
386         final double sDec = 0.39782 * FastMath.sin(al);
387         final double cDec = FastMath.sqrt(1. - sDec * sDec);
388         // Solar zenith angle, deg (Eq. 26 and 27)
389         final FieldSinCos<T> scLat   = FastMath.sinCos(latitude);
390         final T coef    = lt.negate().add(12.0).multiply(FastMath.PI / 12);
391         final T cZenith = scLat.sin().multiply(sDec).add(scLat.cos().multiply(cDec).multiply(FastMath.cos(coef)));
392         final T angle   = FastMath.atan2(FastMath.sqrt(cZenith.multiply(cZenith).negate().add(1.0)), cZenith);
393         final T x       = FastMath.toDegrees(angle);
394         // Effective solar zenith angle (Eq. 28)
395         final T xeff = join(clipExp(x.multiply(0.2).negate().add(20.0)).multiply(0.24).negate().add(90.0), x,
396                 zero.newInstance(12.0), x.subtract(X0));
397         return FastMath.toRadians(xeff);
398     }
399 
400     /**
401      * This method computes the E layer critical frequency at a given location.
402      * @param month current month
403      * @param az ffective ionisation level
404      * @param xeff effective solar zenith angle in radians
405      * @param latitude latitude in radians
406      * @return the E layer critical frequency at a given location in MHz
407      */
408     private T computefoE(final int month, final T az,
409                          final T xeff, final T latitude) {
410         // The latitude has to be converted in degrees
411         final T lat = FastMath.toDegrees(latitude);
412         // Square root of the effective ionisation level
413         final T sqAz = FastMath.sqrt(az);
414         // seas parameter (Eq. 30 to 32)
415         final int seas;
416         if (month == 1 || month == 2 || month == 11 || month == 12) {
417             seas = -1;
418         } else if (month == 3 || month == 4 || month == 9 || month == 10) {
419             seas = 0;
420         } else {
421             seas = 1;
422         }
423         // Latitudinal dependence (Eq. 33 and 34)
424         final T ee = clipExp(lat.multiply(0.3));
425         final T seasp = ee.subtract(1.0).divide(ee.add(1.0)).multiply(seas);
426         // Critical frequency (Eq. 35)
427         final T coef = seasp.multiply(0.019).negate().add(1.112);
428         final T foE = FastMath.sqrt(coef .multiply(coef).multiply(sqAz).multiply(FastMath.cos(xeff).pow(0.6)).add(0.49));
429         return foE;
430     }
431 
432     /**
433      * Computes the F2 layer height of maximum electron density.
434      * @param field field of the elements
435      * @param foE E layer layer critical frequency in MHz
436      * @param foF2 F2 layer layer critical frequency in MHz
437      * @param mF2 maximum usable frequency factor
438      * @return hmF2 in km
439      */
440     private T computehmF2(final Field<T> field, final T foE, final T foF2, final T mF2) {
441         // Zero
442         final T zero = field.getZero();
443         // Ratio
444         final T fo = foF2.divide(foE);
445         final T ratio = join(fo, zero.newInstance(1.75), zero.newInstance(20.0), fo.subtract(1.75));
446 
447         // deltaM parameter
448         T deltaM = zero.subtract(0.012);
449         if (foE.getReal() >= 1e-30) {
450             deltaM = deltaM.add(ratio.subtract(1.215).divide(0.253).reciprocal());
451         }
452 
453         // hmF2 Eq. 80
454         final T mF2Sq = mF2.square();
455         final T temp  = FastMath.sqrt(mF2Sq.multiply(0.0196).add(1.0).divide(mF2Sq.multiply(1.2967).subtract(1.0)));
456         final T height  = mF2.multiply(1490.0).multiply(temp).divide(mF2.add(deltaM)).subtract(176.0);
457         return height;
458     }
459 
460     /**
461      * Computes cf2 coefficients.
462      * @param field field of the elements
463      * @param af2 interpolated coefficients for foF2
464      * @param t time argument
465      * @return the cf2 coefficients array
466      */
467     private T[] computeCF2(final Field<T> field, final T[][] af2, final double t) {
468         // Eq. 50
469         final T[] cf2 = MathArrays.buildArray(field, 76);
470         for (int i = 0; i < cf2.length; i++) {
471             T sum = field.getZero();
472             for (int k = 0; k < 6; k++) {
473                 final SinCos sc = FastMath.sinCos((k + 1) * t);
474                 sum = sum.add(af2[i][2 * k + 1].multiply(sc.sin()).add(af2[i][2 * (k + 1)].multiply(sc.cos())));
475             }
476             cf2[i] = af2[i][0].add(sum);
477         }
478         return cf2;
479     }
480 
481     /**
482      * Computes Cm3 coefficients.
483      * @param field field of the elements
484      * @param am3 interpolated coefficients for foF2
485      * @param t time argument
486      * @return the Cm3 coefficients array
487      */
488     private T[] computeCm3(final Field<T> field, final T[][] am3, final double t) {
489         // Eq. 51
490         final T[] cm3 = MathArrays.buildArray(field, 49);
491         for (int i = 0; i < cm3.length; i++) {
492             T sum = field.getZero();
493             for (int k = 0; k < 4; k++) {
494                 final SinCos sc = FastMath.sinCos((k + 1) * t);
495                 sum = sum.add(am3[i][2 * k + 1].multiply(sc.sin()).add(am3[i][2 * (k + 1)].multiply(sc.cos())));
496             }
497             cm3[i] = am3[i][0].add(sum);
498         }
499         return cm3;
500     }
501 
502     /**
503      * This method computes the F2 layer critical frequency.
504      * @param field field of the elements
505      * @param modip modified DIP latitude, in degrees
506      * @param cf2 Fourier time series for foF2
507      * @param latitude latitude in radians
508      * @param longitude longitude in radians
509      * @return the F2 layer critical frequency, MHz
510      */
511     private T computefoF2(final Field<T> field, final T modip, final T[] cf2,
512                           final T latitude, final T longitude) {
513 
514         // One
515         final T one = field.getOne();
516 
517         // Legendre grades (Eq. 63)
518         final int[] q = new int[] {
519             12, 12, 9, 5, 2, 1, 1, 1, 1
520         };
521 
522         // Array for geographic terms
523         final T[] g = MathArrays.buildArray(field, cf2.length);
524         g[0] = one;
525 
526         // MODIP coefficients Eq. 57
527         final T sinMODIP = FastMath.sin(FastMath.toRadians(modip));
528         final T[] m = MathArrays.buildArray(field, 12);
529         m[0] = one;
530         for (int i = 1; i < q[0]; i++) {
531             m[i] = sinMODIP.multiply(m[i - 1]);
532             g[i] = m[i];
533         }
534 
535         // Latitude coefficients (Eq. 58)
536         final T cosLat = FastMath.cos(latitude);
537         final T[] p = MathArrays.buildArray(field, 8);
538         p[0] = cosLat;
539         for (int n = 2; n < 9; n++) {
540             p[n - 1] = cosLat.multiply(p[n - 2]);
541         }
542 
543         // latitude and longitude terms
544         int index = 12;
545         for (int i = 1; i < q.length; i++) {
546             final FieldSinCos<T> sc = FastMath.sinCos(longitude.multiply(i));
547             for (int j = 0; j < q[i]; j++) {
548                 g[index++] = m[j].multiply(p[i - 1]).multiply(sc.cos());
549                 g[index++] = m[j].multiply(p[i - 1]).multiply(sc.sin());
550             }
551         }
552 
553         // Compute foF2 by linear combination
554         final T frequency = one.linearCombination(g, cf2);
555         return frequency;
556     }
557 
558     /**
559      * This method computes the Maximum Usable Frequency factor.
560      * @param field field of the elements
561      * @param modip modified DIP latitude, in degrees
562      * @param cm3 Fourier time series for M(3000)F2
563      * @param latitude latitude in radians
564      * @param longitude longitude in radians
565      * @return the Maximum Usable Frequency factor
566      */
567     private T computeMF2(final Field<T> field, final T modip, final T[] cm3,
568                          final T latitude, final T longitude) {
569 
570         // One
571         final T one = field.getOne();
572         // Legendre grades (Eq. 71)
573         final int[] r = new int[] {
574             7, 8, 6, 3, 2, 1, 1
575         };
576 
577         // Array for geographic terms
578         final T[] g = MathArrays.buildArray(field, cm3.length);
579         g[0] = one;
580 
581         // MODIP coefficients Eq. 57
582         final T sinMODIP = FastMath.sin(FastMath.toRadians(modip));
583         final T[] m = MathArrays.buildArray(field, 12);
584         m[0] = one;
585         for (int i = 1; i < 12; i++) {
586             m[i] = sinMODIP.multiply(m[i - 1]);
587             if (i < 7) {
588                 g[i] = m[i];
589             }
590         }
591 
592         // Latitude coefficients (Eq. 58)
593         final T cosLat = FastMath.cos(latitude);
594         final T[] p = MathArrays.buildArray(field, 8);
595         p[0] = cosLat;
596         for (int n = 2; n < 9; n++) {
597             p[n - 1] = cosLat.multiply(p[n - 2]);
598         }
599 
600         // latitude and longitude terms
601         int index = 7;
602         for (int i = 1; i < r.length; i++) {
603             final FieldSinCos<T> sc = FastMath.sinCos(longitude.multiply(i));
604             for (int j = 0; j < r[i]; j++) {
605                 g[index++] = m[j].multiply(p[i - 1]).multiply(sc.cos());
606                 g[index++] = m[j].multiply(p[i - 1]).multiply(sc.sin());
607             }
608         }
609 
610         // Compute m3000 by linear combination
611         final T m3000 = one.linearCombination(g, cm3);
612         return m3000;
613     }
614 
615     /**
616      * This method computes the F1 layer critical frequency.
617      * <p>
618      * This computation performs the algorithm exposed in Annex F
619      * of the reference document.
620      * </p>
621      * @param field field of the elements
622      * @param foE the E layer critical frequency, MHz
623      * @return the F1 layer critical frequency, MHz
624      * @param foF2 the F2 layer critical frequency, MHz
625      */
626     private T computefoF1(final Field<T> field, final T foE, final T foF2) {
627         final T zero = field.getZero();
628         final T temp  = join(foE.multiply(1.4), zero, zero.newInstance(1000.0), foE.subtract(2.0));
629         final T temp2 = join(zero, temp, zero.newInstance(1000.0), foE.subtract(temp));
630         final T value = join(temp2, temp2.multiply(0.85), zero.newInstance(60.0), foF2.multiply(0.85).subtract(temp2));
631         if (value.getReal() < 1.0E-6) {
632             return zero;
633         } else {
634             return value;
635         }
636     }
637 
638     /**
639      * This method allows the computation of the F2, F1 and E layer amplitudes.
640      * <p>
641      * The resulting element is an array having the following form:
642      * <ul>
643      * <li>double[0] = A1 → F2 layer amplitude
644      * <li>double[1] = A2 → F1 layer amplitude
645      * <li>double[2] = A3 → E  layer amplitude
646      * </ul>
647      * </p>
648      * @param field field of the elements
649      * @param nmE E layer maximum density in 10^11 m-3
650      * @param nmF1 F1 layer maximum density in 10^11 m-3
651      * @param foF1 F1 layer critical frequency in MHz
652      * @return a three components array containing the layer amplitudes
653      */
654     private T[] computeLayerAmplitudes(final Field<T> field, final T nmE, final T nmF1, final T foF1) {
655         // Zero
656         final T zero = field.getZero();
657 
658         // Initialize array
659         final T[] amplitude = MathArrays.buildArray(field, 3);
660 
661         // F2 layer amplitude (Eq. 90)
662         final T a1 = nmF2.multiply(4.0);
663         amplitude[0] = a1;
664 
665         // F1 and E layer amplitudes (Eq. 91 to 98)
666         if (foF1.getReal() < 0.5) {
667             amplitude[1] = zero;
668             amplitude[2] = nmE.subtract(epst(a1, hmF2, b2Bot, hmE)).multiply(4.0);
669         } else {
670             T a2a = zero;
671             T a3a = nmE.multiply(4.0);
672             for (int i = 0; i < 5; i++) {
673                 a2a = nmF1.subtract(epst(a1, hmF2, b2Bot, hmF1)).subtract(epst(a3a, hmE, beTop, hmF1)).multiply(4.0);
674                 a2a = join(a2a, nmF1.multiply(0.8), field.getOne(), a2a.subtract(nmF1.multiply(0.8)));
675                 a3a = nmE.subtract(epst(a2a, hmF1, b1Bot, hmE)).subtract(epst(a1, hmF2, b2Bot, hmE)).multiply(4.0);
676             }
677             amplitude[1] = a2a;
678             amplitude[2] = join(a3a, zero.newInstance(0.05), zero.newInstance(60.0), a3a.subtract(0.005));
679         }
680 
681         return amplitude;
682     }
683 
684     /**
685      * This method computes the topside thickness parameter H0.
686      *
687      * @param field field of the elements
688      * @param month current month
689      * @param azr effective sunspot number
690      * @return H0 in km
691      */
692     private T computeH0(final Field<T> field, final int month, final T azr) {
693 
694         // One
695         final T one = field.getOne();
696 
697         // Auxiliary parameter ka (Eq. 99 and 100)
698         final T ka;
699         if (month > 3 && month < 10) {
700             // month = 4,5,6,7,8,9
701             ka = azr.multiply(0.014).add(hmF2.multiply(0.008)).negate().add(6.705);
702         } else {
703             // month = 1,2,3,10,11,12
704             final T ratio = hmF2.divide(b2Bot);
705             ka = ratio.multiply(ratio).multiply(0.097).add(nmF2.multiply(0.153)).add(-7.77);
706         }
707 
708         // Auxiliary parameter kb (Eq. 101 and 102)
709         T kb = join(ka, one.newInstance(2.0), one, ka.subtract(2.0));
710         kb = join(one.newInstance(8.0), kb, one, kb.subtract(8.0));
711 
712         // Auxiliary parameter Ha (Eq. 103)
713         final T hA = kb.multiply(b2Bot);
714 
715         // Auxiliary parameters x and v (Eq. 104 and 105)
716         final T x = hA.subtract(150.0).multiply(0.01);
717         final T v = x.multiply(0.041163).subtract(0.183981).multiply(x).add(1.424472);
718 
719         // Topside thickness parameter (Eq. 106)
720         final T h = hA.divide(v);
721         return h;
722     }
723 
724     /**
725      * A clipped exponential function.
726      * <p>
727      * This function, describe in section F.2.12.2 of the reference document, is
728      * recommanded for the computation of exponential values.
729      * </p>
730      * @param power power for exponential function
731      * @return clipped exponential value
732      */
733     private T clipExp(final T power) {
734         final T zero = power.getField().getZero();
735         if (power.getReal() > 80.0) {
736             return zero.newInstance(5.5406E34);
737         } else if (power.getReal() < -80) {
738             return zero.newInstance(1.8049E-35);
739         } else {
740             return FastMath.exp(power);
741         }
742     }
743 
744     /**
745      * This method provides a third order interpolation function
746      * as recommended in the reference document (Ref Eq. 128 to Eq. 138)
747      *
748      * @param z1 z1 coefficient
749      * @param z2 z2 coefficient
750      * @param z3 z3 coefficient
751      * @param z4 z4 coefficient
752      * @param x position
753      * @return a third order interpolation
754      */
755     private T interpolate(final T z1, final T z2,
756                           final T z3, final T z4,
757                           final T x) {
758 
759         if (FastMath.abs(2.0 * x.getReal()) < 1e-10) {
760             return z2;
761         }
762 
763         final T delta = x.multiply(2.0).subtract(1.0);
764         final T g1 = z3.add(z2);
765         final T g2 = z3.subtract(z2);
766         final T g3 = z4.add(z1);
767         final T g4 = z4.subtract(z1).divide(3.0);
768         final T a0 = g1.multiply(9.0).subtract(g3);
769         final T a1 = g2.multiply(9.0).subtract(g4);
770         final T a2 = g3.subtract(g1);
771         final T a3 = g4.subtract(g2);
772         final T zx = delta.multiply(a3).add(a2).multiply(delta).add(a1).multiply(delta).add(a0).multiply(0.0625);
773 
774         return zx;
775     }
776 
777     /**
778      * Allows smooth joining of functions f1 and f2
779      * (i.e. continuous first derivatives) at origin.
780      * <p>
781      * This function, describe in section F.2.12.1 of the reference document, is
782      * recommanded for computational efficiency.
783      * </p>
784      * @param dF1 first function
785      * @param dF2 second function
786      * @param dA width of transition region
787      * @param dX x value
788      * @return the computed value
789      */
790     private T join(final T dF1, final T dF2,
791                    final T dA, final T dX) {
792         final T ee = clipExp(dA.multiply(dX));
793         return dF1.multiply(ee).add(dF2).divide(ee.add(1.0));
794     }
795 
796     /**
797      * The Epstein function.
798      * <p>
799      * This function, describe in section 2.5.1 of the reference document, is used
800      * as a basis analytical function in NeQuick for the construction of the ionospheric layers.
801      * </p>
802      * @param x x parameter
803      * @param y y parameter
804      * @param z z parameter
805      * @param w w parameter
806      * @return value of the epstein function
807      */
808     private T epst(final T x, final T y,
809                    final T z, final T w) {
810         final T ex  = clipExp(w.subtract(y).divide(z));
811         final T opex = ex.add(1.0);
812         final T epst = x.multiply(ex).divide(opex.multiply(opex));
813         return epst;
814     }
815 
816 }