1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2023-05-15
 * Description : geolocation engine based on Marble.
 *               (c) 2007-2022 Marble Team
 *               https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
 *
 * SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * ============================================================ */

/**
 * Calculate Spacecraft around other planets
 */

#include "planetarySats.h"

// C++ includes

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>

using namespace std;

// Local includes

#include "astrolib.h"

namespace Marble
{

// ################ Planetary Sats Class ####################

PlanetarySats::PlanetarySats()
{
    plsatinit();
}

PlanetarySats::~PlanetarySats()
{

}

double PlanetarySats::atan23(double y, double x)
{
    // redefine atan2 so that it doesn't crash when both x and y are 0
    double result;

    if ((x == 0) && (y == 0))
    {
        result = 0;
    }

    else
    {
        result = atan2(y, x);
    }

    return result;
}

void PlanetarySats::plsatinit()
{
    // initialize planetary sat data
    pls_moonflg = false;
    pls_day = 1;
    pls_month = 1;
    pls_year = 2012;
    pls_hour = 0;
    pls_minute = 0;
    pls_second = 0;
    pls_del_auto = 1;
    pls_step = 60.0;
    pls_delta_rt = 0.0;
    getTime();
    getMars();

    strcpy(pls_satelmfl, "./planetarysats.txt");

}

void PlanetarySats::getTime()   // Get System Time and Date
{
    time_t tt;
    int hh, mm, ss;
    double jd, hr;

    tt = time(NULL);
    jd = 40587.0 + tt / 86400.0; // seconds since 1-JAN-1970

    jd = jd + pls_delta_rt / 24.0;
    caldat(jd, hh, mm, ss, hr);
    pls_year = ss;
    pls_month = mm;
    pls_day = hh;

    dms(hr, hh, mm, jd);
    pls_hour = hh;
    pls_minute = mm;
    pls_second = int(jd);

    if (pls_del_auto)
    {
        pls_del_tdut = DefTdUt(pls_year);
    }

    setMJD(pls_year, pls_month, pls_day, hh, mm, jd);
};

void PlanetarySats::setStepWidth(double s)
{
    // set the step width (in seconds) used for calculations
    if (s < 0.01)
    {
        pls_step = 0.01;
    }

    else
    {
        pls_step = s;
    }
}

void PlanetarySats::setDeltaRT(double drt)
{
    pls_delta_rt = drt;
}

void PlanetarySats::setDeltaTAI_UTC(double d)
{
    // c is the difference between TAI and UTC according to the IERS
    // we have to add 32.184 sec to get to the difference TT - UT
    // which is used in the calculations here

    pls_del_tdut = d + 32.184;
    pls_del_auto = 0;
}

void PlanetarySats::setAutoTAI_UTC()
{
    // set the difference between TAI and UTC according to the IERS
    pls_del_auto = true;
    pls_del_tdut = DefTdUt(pls_year);
}

void PlanetarySats::setMJD(int year, int month, int day, int hour, int min, double sec)
{
    // set the (MJD-) time currently used for calculations to year, month, day, hour, min, sec
    double jd;

    pls_year = year;
    pls_month = month;
    pls_day = day;
    pls_hour = hour;
    pls_minute = min;
    pls_second = sec;

    jd = ddd(hour, min, sec);
    jd = mjd(day, month, year, jd);

    pls_time = jd;

    if (pls_del_auto)
    {
        pls_del_tdut = DefTdUt(pls_year);
    }

}

void PlanetarySats::getDatefromMJD(double mjd, int& year, int& month, int& day, int& hour, int& min, double& sec)
{
    // convert times given in Modified Julian Date (MJD) into conventional date and time

    double magn;

    caldat((mjd), day, month, year, magn);
    dms(magn, hour, min, sec);

    if (sec > 30.0)
    {
        min++;
    }

    if (min > 59)
    {
        hour++;
        min = 0;
    };
}

void PlanetarySats::setSatFile(char* fname)<--- Parameter 'fname' can be declared as pointer to const
{
    strncpy(pls_satelmfl, fname, sizeof(pls_satelmfl));
}

void PlanetarySats::setStateVector(double mjd, double x, double y, double z, double vx, double vy, double vz)
{
    pls_rep[0] = x;
    pls_rep[1] = y;
    pls_rep[2] = z;
    pls_vep[0] = vx;
    pls_vep[1] = vy;
    pls_vep[2] = vz;

    int year = 0;
    int month = 0;
    int day = 0;
    int hour = 0;
    int min = 0;
    double sec = 0;
    getDatefromMJD(mjd, year, month, day, hour, min, sec);
    setMJD(year, month, day, hour, min, sec);
    pls_tepoch = pls_time;
    //pls_tepoch = pls_time + pls_del_tdut / 86400.0;  // epoch in TT
}

int PlanetarySats::getStateVector(int nsat)
{
    // read the state vector from the planetary sat file
    // nsat = number of eligible sat to select (1 if first or only sat)
    // RETURN number of eligible sat selected, 0 if no suitable sats of file problems

    int fsc, j, k, nst, utc;
    int yr, month, day, hour, min;
    double sec;
    bool searching;
    ifstream cfle;
    char satname[40];  // name of satellite
    char plntname[40]; // name of planet

    strcpy(satname, "");
    strcpy(plntname, "");
    nst = 0;

    searching = true;
    fsc = 0;

    cfle.open(pls_satelmfl, ios::in);

    if (!cfle)
    {
        searching = false;
        cfle.close();
    };

    if (searching)
    {
        while (searching)
        {
            fsc = 1;

            if (!cfle.getline(satname, 40))
            {
                fsc = 0;
            }

            else
            {
                k = strlen(satname);

                if ((k > 1) && (satname[0] == '#'))
                {
                    for (j = 1; j < k; ++j)
                    {
                        pls_satname[j - 1] = satname[j];

                        if (pls_satname[j - 1] == '\n')
                        {
                            pls_satname[j - 1] = '\0';
                        }
                    };

                    pls_satname[k - 1] = '\0';
                }

                else
                {
                    fsc = 0;
                }
            };

            if (cfle.eof())
            {
                fsc = 0;
                searching = false;
            };

            if (fsc)
            {
                if (!cfle.getline(plntname, 40))
                {
                    fsc = 0;
                }

                else
                {
                    k = strlen(plntname);

                    if (k > 0)
                    {
                        if (plntname[k - 1] == '\n')
                        {
                            plntname[k - 1] = '\0';
                        }

                        if ((k > 1) && (plntname[k - 2] == '\r'))
                        {
                            plntname[k - 2] = '\0';
                        }
                    };
                };
            };

            if (fsc)
            {
                cfle >> yr >> month >> day >> hour >> min >> sec >> utc;

                if (cfle.bad())
                {
                    fsc = 0;
                }

                if (cfle.eof())
                {
                    fsc = 0;
                    searching = false;
                };
            };

            if (fsc)
            {
                cfle >> pls_rep[0] >> pls_rep[1] >> pls_rep[2];

                if (cfle.bad())
                {
                    fsc = 0;
                }

                if (cfle.eof())
                {
                    fsc = 0;
                    searching = false;
                };
            };

            if (fsc)
            {
                cfle >> pls_vep[0] >> pls_vep[1] >> pls_vep[2];

                if (cfle.bad())
                {
                    fsc = 0;
                }
            };

            if (fsc)
            {
                if (strncmp(pls_plntname, plntname, 4) == 0)
                {
                    nst++;
                }

                if (nst == nsat)
                {
                    searching = false;

                    setMJD(yr, month, day, hour, min, sec);
                    pls_tepoch = pls_time;

                    if (utc)
                    {
                        pls_tepoch = pls_tepoch + pls_del_tdut / 86400.0;    // epoch in TT
                    }
                };
            };
        };

        cfle.close();
    };

    if (fsc == 0)
    {
        nst = 0;
    }

    return nst;
}

void PlanetarySats::setPlanet(char* pname)<--- Parameter 'pname' can be declared as pointer to const
{
    pls_moonflg = false;
    strncpy(pls_plntname, pname, sizeof(pls_plntname));

    if (strncmp("Mars", pname, 4) == 0)
    {
        getMars();
    }

    if (strncmp("Venus", pname, 4) == 0)
    {
        getVenus();
    }

    if (strncmp("Mercury", pname, 4) == 0)
    {
        getMercury();
    }

    if (strncmp("Moon", pname, 4) == 0)
    {
        getMoon();
    }
}

void PlanetarySats::stateToKepler()
{
    // convert state vector (mean equatorial J2000.0) into planetary Kepler elements

    double t, dt, ag, gm, re, j2;
    double n, c, w, a, ecc, inc;
    Vec3 r1, v1;
    Mat3 mx;

    dt = (pls_tepoch - 51544.5) / 36525.0;
    gm = pls_GM * 7.4649600000; // convert from m^3/s^2 into km^3/d^2
    re = pls_R0;
    j2 = pls_J2;

    // convert into planet equatorial reference frame
    if (pls_moonflg)
    {
        mx = mxidn();
        r1 = mxvct(mx, pls_rep);
        v1 = mxvct(mx, pls_vep);

    }

    else
    {
        ag = (pls_axl0 + pls_axl1 * dt) * M_PI / 180.0;
        mx = zrot(ag + M_PI / 2.0);
        r1 = mxvct(mx, pls_rep);
        v1 = mxvct(mx, pls_vep);

        ag = (pls_axb0 + pls_axb1 * dt) * M_PI / 180.0;
        mx = xrot(M_PI / 2.0 - ag);
        r1 = mxvct(mx, r1);
        v1 = mxvct(mx, v1);
    };

    v1 *= 86400.0;  // convert into km / day

    oscelm(gm, pls_tepoch, r1, v1, t, pls_m0, pls_a, pls_ecc, pls_ra, pls_per, pls_inc);

    // now the mean motion
    a = pls_a;

    ecc = pls_ecc;

    inc = pls_inc;

    //  preliminary n
    if (a == 0)
    {
        a = 1.0;    // just in case
    }

    if (a < 0)
    {
        a = -a;    // just in case
    }

    n = sqrt(gm / (a * a * a));

    // correct for J2 term
    w = 1.0 - ecc * ecc;

    if (w > 0)
    {
        w = pow(w, -1.5);
        c = sin(inc * M_PI / 180.0);
        n = n * (1.0 + 1.5 * j2 * re * re / (a * a) * w * (1.0 - 1.5 * c * c));
    }

    else
    {
        n = 1.0;    // do something to avoid a domain error
    }

    n = n / (2.0 * M_PI);

    if (n > 1000.0)
    {
        n = 1000.0;    // avoid possible errors
    }

    pls_n0 = n;

}

void PlanetarySats::getKeplerElements(double& perc, double& apoc, double& inc, double& ecc, double& ra, double& tano, double& m0, double& a, double& n0)
{
    // get Kepler elements of orbit with regard to the planetary equator and prime meridian.

    double t, gm;
    Vec3 r1, v1;
    Mat3 mx;


    if (pls_moonflg)  // for the Moon we normally work in J2000. Now get it into planetary
    {
        gm = pls_GM * 7.4649600000; // convert from m^3/s^2 into km^3/d^2

        mx = getSelenographic(pls_tepoch);
        r1 = mxvct(mx, pls_rep);
        v1 = mxvct(mx, pls_vep);
        v1 *= 86400.0;  // convert into km / day

        oscelm(gm, pls_tepoch, r1, v1, t, m0, a, ecc, ra, tano, inc);

        // now the mean motion
        if (a == 0)
        {
            a = 1.0;    // just in case
        }

        if (a < 0)
        {
            a = -a;    // just in case
        }

        n0 = sqrt(gm / (a * a * a));
        n0 = n0 / (2.0 * M_PI);
    }

    else
    {
        a = pls_a;
        n0 = pls_n0;
        m0 = pls_m0;
        tano = pls_per;
        ra = pls_ra;
        ecc = pls_ecc;
        inc = pls_inc;
    };

    perc = pls_a * (1.0 - pls_ecc) - pls_R0;

    apoc = pls_a * (1.0 + pls_ecc) - pls_R0;

}

int PlanetarySats::selectSat(char* sname)<--- Parameter 'sname' can be declared as pointer to const
{
    // select specified satellite
    // RETURN 1 if successful, 0 if no suitable satellite found

    int nst, res, sl;
    bool searching;

    searching = true;
    nst = 1;
    sl = strlen(sname);

    while (searching)
    {
        res = getStateVector(nst);

        if (res)
        {
            if (strncmp(pls_satname, sname, sl) == 0)
            {
                searching = false;
            }
        }

        else
        {
            searching = false;
        }

        nst++;
    };

    return res;
}

void PlanetarySats::getSatName(char* sname) const
{
    strcpy(sname, pls_satname);
}

void PlanetarySats::currentPos()
{
    getSatPos(pls_time);
}

void PlanetarySats::nextStep()
{
    pls_time = pls_time + pls_step / 86400.0;
    getSatPos(pls_time);
}

double PlanetarySats::getLastMJD() const
{
    return pls_time;
}

void PlanetarySats::getPlanetographic(double& lng, double& lat, double& height) const
{
    // planetographic coordinates from current state vector

    lng = pls_lng;
    lat = pls_lat;
    height = pls_height;

}

void PlanetarySats::getFixedFrame(double& x, double& y, double& z, double& vx, double& vy, double& vz)
{
    // last state vector coordinates in planetary fixed frame

    x = pls_r[0];
    y = pls_r[1];
    z = pls_r[2];
    vx = pls_v[0];
    vy = pls_v[1];
    vz = pls_v[2];
}

void PlanetarySats::getSatPos(double tutc)
{
    // Get Position of Satellite at MJD-time t (UTC)

    const double mp2 = 2.0 * M_PI;

    double t, dt, m0, ran, aper, inc, a, ecc, n0, re;
    double f, e, c, s, k, sh, j2, gm, fac, b1, b2, b3;
    int j;

    Vec3 r1, v1, rg1, s2;
    Mat3 m1, m2;

    // prepare orbit calculation

    t = tutc + pls_del_tdut / 86400.0;
    dt = t - pls_tepoch;

    ecc = pls_ecc;

    if (ecc >= 1.0)
    {
        ecc = 0.999;    // to avoid crashes
    }

    a = pls_a;
    n0 = mp2 * pls_n0;

    if (a < 1.0)
    {
        a = 1.0;    // avoid possible crashes later on
    }

    re = pls_R0;
    f = pls_flat;
    j2 = pls_J2;
    gm = pls_GM * 7.4649600000; // convert from m^3/s^2 into km^3/d^2

    // get current orbit elements ready
    aper = (re / a) / (1.0 - ecc * ecc);
    aper = 1.5 * j2 * aper * aper * n0;
    m0 = pls_inc * M_PI / 180.0;
    ran = -aper * cos(m0) * dt;
    m0 = sin(m0);
    aper = aper * (2.0 - 2.5 * m0 * m0) * dt;
    ran = pls_ra * M_PI / 180.0 + ran;
    aper = pls_per * M_PI / 180.0 + aper;
    m0 = pls_m0 * M_PI / 180.0 + n0 * dt;
    inc = pls_inc * M_PI / 180.0;

    // solve Kepler equation
    if (a < 1.0)
    {
        a = 1.0;    // avoid possible crashes later on
    }

    e = eccanom(m0, ecc);
    fac = sqrt(1.0 - ecc * ecc);
    c = cos(e);
    s = sin(e);
    r1.assign(a * (c - ecc), a * fac * s, 0.0);

    m0 = 1.0 - ecc * c;
    k = sqrt(gm / a);
    v1.assign(-k * s / m0, k * fac * c / m0, 0.0);

    // convert into reference plane
    m1 = zrot(-aper);
    m2 = xrot(-inc);
    m1 *= m2;
    m2 = zrot(-ran);
    m2 = m2 * m1;
    r1 = mxvct(m2, r1);
    v1 = mxvct(m2, v1);
    v1 /= 86400.0;

    // save state vector in planet-fixed frame

    if (pls_moonflg)
    {
        m1 = getSelenographic(t);
    }

    else
    {
        m1 = zrot((pls_W + pls_Wd * (t - 51544.5)) * (M_PI / 180.0));
    }

    pls_r = mxvct(m1, r1);
    pls_v = mxvct(m1, v1);
    pls_r *= 1000.0;
    pls_v *= 1000.0;

    // get the groundtrack coordinates

    rg1 = mxvct(m1, r1);

    s2 = carpol(rg1);
    pls_lat = s2[2];   // just preliminary
    pls_lng = s2[1];  // measured with the motion of rotation!

    if (pls_lng > mp2)
    {
        pls_lng -= mp2;
    }

    if (pls_lng < -M_PI)
    {
        pls_lng += mp2;
    }

    if (pls_lng > M_PI)
    {
        pls_lng -= mp2;
    }

    // get height above ellipsoid and geodetic latitude
    if (abs(r1) > 0.1)
    {
        if (f == 0)
        {
            pls_height = abs(r1) - re;
        }

        else
        {
            c = f * (2.0 - f);
            s = r1[0] * r1[0] + r1[1] * r1[1];
            sh = c * r1[2];

            for (j = 0; j < 4; ++j)
            {
                b1 = r1[2] + sh;
                b3 = sqrt(s + b1 * b1);

                if (b3 < 1e-5)
                {
                    b3 = sin(pls_lat);    // just in case
                }

                else
                {
                    b3 = b1 / b3;
                }

                b2 = re / sqrt(1.0 - c * b3 * b3);
                sh = b2 * c * b3;
            };

            sh = r1[2] + sh;

            pls_lat = atan20(sh, sqrt(s));

            sh = sqrt(s + sh * sh) - b2;

            pls_height = sh;
        }
    }

    else
    {
        pls_height = 0;    // this should never happen
    }

    pls_lat = pls_lat * 180.0 / M_PI;
    pls_lng = pls_lng * 180.0 / M_PI;

}


void PlanetarySats::getMars()  // Mars planetary constants
{
    pls_J2 = 1.964e-3;
    pls_R0 = 3397.2;
    pls_flat = 0.00647630;
    pls_axl0 = 317.681;
    pls_axl1 = -0.108;
    pls_axb0 = 52.886;
    pls_axb1 = -0.061;
    pls_W = 176.868;
    pls_Wd = 350.8919830;
    pls_GM = 4.282828596416e+13; // 4.282837405582e+13
}

void PlanetarySats::getVenus()  // Venus planetary constants
{
    pls_J2 = 0.027e-3;
    pls_R0 = 6051.9;
    pls_flat = 0.0;
    pls_axl0 = 272.72;
    pls_axl1 = 0.0;
    pls_axb0 = 67.15;
    pls_axb1 = 0.0;
    pls_W = 160.26;
    pls_Wd = -1.4813596;
    pls_GM = 3.24858761e+14;
}

void PlanetarySats::getMercury()  // Mercury planetary constants
{
    pls_J2 = 0.0;
    pls_R0 = 2439.7;
    pls_flat = 0.0;
    pls_axl0 = 281.01;
    pls_axl1 = -0.033;
    pls_axb0 = 61.45;
    pls_axb1 = -0.005;
    pls_W = 329.71;
    pls_Wd = 6.1385025;
    pls_GM = 2.20320802e+13;
}

void PlanetarySats::getMoon()  // Moon planetary constants
{
    pls_moonflg = true;
    pls_J2 = 0.2027e-3;
    pls_R0 = 1738.0;
    pls_flat = 0.0;
    pls_axl0 = 0.0;
    pls_axl1 = 0.0;
    pls_axb0 = 90.0;
    pls_axb1 = 0.0;
    pls_W = 0.0;
    pls_Wd = 13.17635898;
    pls_GM = 4.90279412e+12;
}

Mat3 PlanetarySats::getSelenographic(double jd)
{
    // Calculate the Matrix to transform from Mean of J2000 into selenographic
    // coordinates at MJD time jd.

    double t, gam, gmp, l, omg, mln;
    double a, b, c, ic, gn, gp, omp;
    double const degrad = M_PI / 180.0;
    Mat3 m1, m2;

    t = (jd - 15019.5) / 36525.0;
    gam = 281.2208333 + ((0.33333333e-5 * t + 0.45277778e-3) * t + 1.7191750) * t;
    gmp = 334.3295556 + ((-0.125e-4 * t - 0.10325e-1) * t + 4069.0340333) * t;
    l = 279.6966778 + (0.3025e-3 * t + 36000.768925) * t;
    omg = 259.1832750 + ((0.22222222e-5 * t + 0.20777778e-2) * t - 1934.1420083) * t;
    b = 23.45229444 + ((0.50277778e-6 * t - 0.16388889e-5) * t - 0.130125e-1) * t;
    mln = 270.4341639 + ((0.1888889e-5 * t - 0.11333e-2) * t + 481267.8831417) * t;
    ic = 1.535 * degrad;
    gn = (l - gam) * degrad;
    gp = (mln - gmp) * degrad;
    omp = (gmp - omg) * degrad;
    a = -107.0 * cos(gp) + 37.0 * cos(gp + 2.0 * omp) - 11.0 * cos(2.0 * (gp + omp));
    a = a * 0.000277778 * degrad + ic;
    c = (-109.0 * sin(gp) + 37.0 * sin(gp + 2.0 * omp) - 11.0 * sin(2.0 * (gp + omp))) / sin(ic);
    c = (c * 0.000277778 + omg) * degrad;
    gn = -12.0 * sin(gp) + 59.0 * sin(gn) + 18.0 * sin(2.0 * omp);
    gn = gn * 0.000277778 * degrad; // tau

    b *= degrad;
    gam = cos(a) * cos(b) + sin(a) * sin(b) * cos(c);
    gmp = gam * gam;

    if (gmp > 1.0)
    {
        gmp = 0;
    }

    else
    {
        gmp = sqrt(1.0 - gmp);
    }

    gam = atan23(gmp, gam); // theta
    t = cos(a) * sin(b) - sin(a) * sin(b) * cos(c);
    l = -sin(a) * sin(c);

    gmp = atan23(l, t); // phi
    t = sin(a) * cos(b) - cos(a) * sin(b) * cos(c);
    l = -sin(b) * sin(c);
    a = atan23(l, t); // delta
    c = a + mln * degrad + gn - c; // psi

    // libration rotation matrix from Mean equator to true selenographic
    m1 = zrot(gmp);
    m2 = xrot(gam);
    m1 = m2 * m1;
    m2 = zrot(c);
    m1 = m2 * m1;

    t = julcent(jd);
    m2 = pmatequ(0, t); // convert from mean of J2000 to mean of epoch
    m1 = m1 * m2;

    return m1;
}

} // namespace Marble