Realize satellite navigation GNSS time transformation by C#

GNSS time conversion is particularly important in the field of satellite navigation, mainly involving Gregorian date, Julian day, yearly days, days within a day and many other complicated concepts. This article will use C# to perform GNSS conversion. Since the author is learning C# language recently, so The C language is not used for programming here. Due to the relatively poor C# language foundation of the author, some codes may be too heavy and complicated. I hope readers will forgive me.

Transform method:

Gregorian date format is year (Y) + month (M + day (D) + hour (h) + minute (m) + second (s); Julian date format is Julian day (JD), reduced Julian Day (MJD): The conversion formula between the Gregorian calendar notation method and the Julian day notation method in different time systems is the same, and the specific conversion formula is as follows.

Next is Julian date conversion to Gregorian date

Then it is the conversion between the year + the accumulated day of the year + the second in the day and the Julian day

  1. Years + days in years + seconds in days to convert to Julian day
  • Calculates the Julian day JD1 of January 1 of the current year.
  • Annual cumulative day + JD1-1=Julian day JD2.
  • JD2 + seconds in days/86400=Julian day.
  1. Julian day to year + day in year + seconds in day
  • Calculates the Julian day of January 1 of the current year.
  • Add 1 to the difference between two Julian days to get the cumulative day of the year.
  • Calculates the second of the day from the hours, minutes, and seconds of the Gregorian calendar.

The time notation method of year + year-cum-day + day-second has the following constraints:

  1. Seconds within a day: less than 86400, greater than or equal to 0.
  2. The leap year is not greater than 366, and the normal year is not greater than 365, and the cumulative day starts from 1.

Finally, the conversion between Julian Day and GPS BDS Galileo Glonasst

Next is the code section:

using System;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;

namespace satellite navigation GNSS big job
{
    internal class Program
    {
        static public double ConvertGeligoriandate(double Y, double M, double D, double h, double m, double s) //Gregorian date to Julian date function
        {
            double jd;
            if (M <= 2)
            {
                Y = Y - 1;
                M = M + 12;
            }
            jd = Math.Floor(365.25 * Y) + Math.Floor(30.6001 * (M + 1)) + D + 1720981.5 + h / 24.0 + m / 1440 + s / 86400;
            return jd;
        }
        static public void Convertjulianday(double jd)//Convert Julian day to Gregorian date
        {
            double j = Math. Floor(jd + 0.5);
            double N = Math. Floor(4 * (j + 68549) / 146097);
            double L1 = j + 68569 - Math. Floor((N * 146097 + 3) / 4);
            double Y1 = Math. Floor(4000 * (L1 + 1) / 1461001);
            double L2 = L1 - Math. Floor((1461 * Y1) / 4) + 31;
            double M1 = Math. Floor(80 * L2 / 2447);
            double D = L2 - Math. Floor(2447 * M1 / 80);
            double L3 = Math. Floor(M1 / 11);
            double M = M1 + 2 - 12 * L3;
            double Y = Math. Floor(100 * (N - 49) + Y1 + L3);
            double T = (jd + 0.5 - Math.Floor(jd)) * 24 % 24;
            double h = Math. Floor(T);
            double T1 = (T - h) * 60;
            double m = Math. Floor(T1);
            double s = (T1 - M) * 60;
            Console.Write("The converted Gregorian date is: {0} year, {1} month, {2} day, {3} hour, {4} second", Y, M, D, h, m, s);
        }
        static public void ConvertDOYandseconds(double Y, double A, double B)//convert the accumulated days and seconds into Julian days
        {
            double jd1 = Math.Floor(365.25 * (Y - 1)) + Math.Floor(30.6001 * (1 + 12 + 1)) + 1 + 1720981.5 + 0 / 24.0 + 0 / 1440 + 0 / 86400;//calculation Julian Day on January 1st
            double jd2 = A + jd1 - 1;
            double jd = jd2 + B / 86400;
            Console.WriteLine("The converted Julian day is {0}", jd);
        }
        static public void Convertjulianday2(double jd, double Y)//Convert Julian day into seconds within days
        {
            double jd1 = Math.Floor(365.25 * (Y - 1)) + Math.Floor(30.6001 * (1 + 12 + 1)) + 1 + 1720981.5 + 0 / 24.0 + 0 / 1440 + 0 / 86400;
            double A = Math. Floor(jd - jd1 + 1);
            double B = Math. Ceiling(86400 * (jd - jd1 - A + 1));
            Console.WriteLine("Accumulated day is {0}, and second is {1}", A, B);
        }
        static public void ConvertGps(double WN, double TOW)//GPS conversion Julian day
        {
            if ((WN >= 0) & amp; & amp; (TOW >= 0) & amp; & amp; (TOW < 604800))
            {
                double jd = 2444244.5 + WN * 7 + TOW / 86400;
                if (jd >= 2444244.5)
                {
                    Console.WriteLine("The converted Julian day is {0}", jd);
                }
            }

        }
        static public void Convertjulianday3(double jd)//Julian day conversion GPS
        {
            double WN = Math. Floor((jd - 2444244.5) / 7);
            double TOW = Math. Ceiling(((jd - 2444244.5) / 7 - WN) * 604800) ;
            if ((WN >= 0) & amp; & amp; (TOW >= 0) & amp; & amp; (TOW < 604800) & amp; & amp; (jd >= 2444244.5))
            {
                Console.WriteLine("The converted week number is {0}, and the second in the week is {1}", WN, TOW);
            }
        }
        static public void ConvertBDS(double WN, double TOW)//BDS to Julian Day
        {
            if ((WN >= 0) & amp; & amp; (TOW >= 0) & amp; & amp; (TOW < 604800))
            {
                double jd = 2453736.5 + WN * 7 + TOW / 86400;
                if (jd >= 2453736.5)
                {
                    Console.WriteLine("The converted Julian day is {0}", jd);
                }
            }
        }
        static public void Convertjulianday4(double jd)//Julian day to BDS
        {
            double WN = Math. Floor((jd - 2453736.5) / 7);
            double TOW = Math. Ceiling(((jd - 2453736.5) / 7 - WN) * 604800);
            if ((WN >= 0) & amp; & amp; (TOW >= 0) & amp; & amp; (TOW < 604800) & amp; & amp; (jd >= 2453736.5))
            {
                Console.WriteLine("The converted week number is {0}, and the second in the week is {1}", WN, TOW);
            }
        }
        static public void ConvertGalileo(double WN, double TOW)//Galileo to Julian day
        {
            if ((WN >= 0) & amp; & amp; (TOW >= 0) & amp; & amp; (TOW < 604800))
            {
                double jd = 2451412.5 + WN * 7 + TOW / 86400;
                if (jd >= 2451412.5)
                {
                    Console.WriteLine("The converted Julian day is {0}", jd);
                }
            }
        }
        static public void Convertjulianday5(double jd)//Julian day to Galileo
        {
            double WN = Math. Floor((jd - 2451412.5) / 7);
            double TOW = Math. Ceiling(((jd - 2451412.5) / 7 - WN) * 604800);
            if ((WN >= 0) & amp; & amp; (TOW >= 0) & amp; & amp; (TOW < 604800) & amp; & amp; (jd >= 2451412.5))
            {
                Console.WriteLine("The converted week number is {0}, and the second in the week is {1}", WN, TOW);
            }

        }
        static public void ConvertGlonasst(double N4, double Nt, double h, double m, double s)//Glonasst to Julian day
        {
            double jdn = Math.Floor(365.25 * (1996 + 4 * (N4 - 1) - 1)) + Math.Floor(30.6001 * (1 + 12 + 1)) + 1 + 1720981.5 + 0 / 24.0 + 0 / 1440 +0/86400;
            double jd = Nt - 1 + jdn + h / 24 + m / 1440 + s / 86400;
            Console.WriteLine("The converted Julian day is {0}", jd);
        }
        static public void Convertjulianday6(double jd)//Julian day to Glonasst
        {
            double j = Math. Floor(jd + 0.5);
            double N = Math. Floor(4 * (j + 68549) / 146097);
            double L1 = j + 68569 - Math. Floor((N * 146097 + 3) / 4);
            double Y1 = Math. Floor(4000 * (L1 + 1) / 1461001);
            double L2 = L1 - Math. Floor((1461 * Y1) / 4) + 31;
            double M1 = Math. Floor(80 * L2 / 2447);
            double D = L2 - Math. Floor(2447 * M1 / 80);
            double L3 = Math. Floor(M1 / 11);
            double M = M1 + 2 - 12 * L3;
            double Y = Math. Floor(100 * (N - 49) + Y1 + L3);
            double T = (jd + 0.5 - Math.Floor(jd)) * 24 % 24;
            double h = Math. Floor(T);
            double T1 = (T - h) * 60;
            double m = Math. Floor(T1);
            double s = (T1 - M) * 60;
            Console.WriteLine("The converted Gregorian date is: {0} year, {1} month, {2} day, {3} hour, {4} second", Y, M, D, h, m, s);
            double N4 = Math. Floor((Y - 1996) / 4) + 1;
            double jdn = Math.Floor(365.25 * (1996 + 4 * (N4 - 1) - 1)) + Math.Floor(30.6001 * (1 + 12 + 1)) + 1 + 1720981.5 + 0 / 24.0 + 0 / 1440 +0/86400;
            double Nt = jd - jdn + 1;
            Console.WriteLine("The converted Julian day of 1996 + 4*(N4-1) is {0}", jdn);

        }

        static void Main(string[] args)
        {
            Console.WriteLine("Please enter any one of the numbers 1-12:");
            Console.WriteLine("1. Convert Gregorian date to Julian date:");
            Console.WriteLine("2. Convert Julian date to Gregorian date:");
            Console.WriteLine("3. Convert the seconds into Julian days:");
            Console.WriteLine("4. Convert the Julian day to the second in the year: ");
            Console.WriteLine("5.GPS converted to Julian day:");
            Console.WriteLine("6. Julian day converted to GPS:");
            Console.WriteLine("7.BDS converted to Julian Day:");
            Console.WriteLine("8. Convert Julian Day to BDS:");
            Console.WriteLine("9.Galileo converted to Julian Day:");
            Console.WriteLine("10. Julian day converted to Galileo:");
            Console.WriteLine("11.GLonasst into Julian Day:");
            Console.WriteLine("12. Julian day converted to GLonasst:");
            int a = Convert.ToInt32(Console.ReadLine());
            switch (a)
            {
                case 1:
                    Console.WriteLine("Please enter the Gregorian date:");
                    double Y = Convert.ToDouble(Console.ReadLine());
                    double M = Convert.ToDouble(Console.ReadLine());
                    double D = Convert.ToDouble(Console.ReadLine());
                    double h = Convert.ToDouble(Console.ReadLine());
                    double m = Convert.ToDouble(Console.ReadLine());
                    double s = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("The converted Julian date is {0}", ConvertGeligoriandate(Y, M, D, h, m, s));
                    break;
                case 2:
                    Console.WriteLine("Please enter the Julian day:");
                    double jd = Convert.ToDouble(Console.ReadLine());
                    Convertjulianday(jd);
                    break;
                case 3:
                    Console.WriteLine("Please enter the year, the cumulative day of the year, and the second in the day:");
                    double Y1 = Convert.ToDouble(Console.ReadLine());
                    double A = Convert.ToDouble(Console.ReadLine());
                    double B = Convert.ToDouble(Console.ReadLine());
                    ConvertDOYandseconds(Y1, A, B);
                    break;
                case 4:
                    Console.WriteLine("Please enter the Julian day and year:");
                    double jdA = Convert.ToDouble(Console.ReadLine());
                    double Y2= Convert.ToDouble(Console.ReadLine());
                    Convertjulianday2(jdA, Y2);
                    break;
                case 5:
                    Console.WriteLine("Please enter the GPS week and the second within the week:");
                    double WN = Convert.ToDouble(Console.ReadLine());
                    double TOW = Convert. ToDouble(Console. ReadLine());
                    ConvertGps(WN, TOW);
                    break;
                case 6:
                    Console.WriteLine("Please enter the Julian day:");
                    double jdB = Convert.ToDouble(Console.ReadLine());
                    Convertjulianday3(jdB);
                    break;
                case 7:
                    Console.WriteLine("Please enter the BDS week and the second in the week:");
                    double WN1= Convert.ToDouble(Console.ReadLine());
                    double TOW1= Convert.ToDouble(Console.ReadLine());
                    ConvertBDS(WN1, TOW1);
                    break;
                case 8:
                    Console.WriteLine("Please enter the Julian day:");
                    double jdC = Convert.ToDouble(Console.ReadLine());
                    Convertjulianday4(jdC);
                    break;
                case 9:
                    Console.WriteLine("Please enter Galileo week and week seconds:");
                    double WN2= Convert.ToDouble(Console.ReadLine());
                    double TOW2 = Convert.ToDouble(Console.ReadLine());
                    ConvertGalileo(WN2, TOW2);
                    break;
                case 10:
                    Console.WriteLine("Please enter the Julian day:");
                    double jdD = Convert.ToDouble(Console.ReadLine());
                    Convertjulianday5(jdD);
                    break;
                case 11:
                    Console.WriteLine("Please enter GLonnasst timing:");
                    double N4 = Convert.ToDouble(Console.ReadLine());
                    double Nt = Convert.ToDouble(Console.ReadLine());
                    double h1 = Convert.ToDouble(Console.ReadLine());
                    double m1 = Convert.ToDouble(Console.ReadLine());
                    double s1 = Convert.ToDouble(Console.ReadLine());
                    ConvertGlonasst(N4, Nt, h1, m1, s1);
                    break;
                case 12:
                    Console.WriteLine("Please enter the Julian day:");
                    double jdE = Convert.ToDouble(Console.ReadLine());
                    Convertjulianday6(jdE);
                    break;
            }
        }
    }
}

Since the author is a beginner in C#, part of the code has not been simplified, and I will continue to optimize it in the future learning process, please forgive me!