Skip to content
Snippets Groups Projects
DopplerRangeRateConverter.java 6.13 KiB
Newer Older
package fr.cs.examples.estimation;

import org.orekit.data.DataProvidersManager;
import org.orekit.data.DirectoryCrawler;
import org.orekit.time.AbsoluteDate;
import org.orekit.time.TimeScale;
import org.orekit.time.TimeScalesFactory;
import org.orekit.utils.Constants;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;

/** Converter to convert the measured signal frequency to range-rate (in km/s) and to change MJD to the date in the ISO-8601 format.
 * @author Noel Janes
 */

public class DopplerRangeRateConverter {

    static double rRcalculator(String freq , double downlinkFreq, double c){
        double f2 = Double.parseDouble(freq);
        double dopplerFreq = f2 - downlinkFreq;
        double rR = (-c) * (dopplerFreq / downlinkFreq);
        rR = rR/1000;
        return rR;
    }

    static AbsoluteDate mjdDate(String dateIn, TimeScale utc, AbsoluteDate mjdRefUTC){
        double mjdDate = Double.parseDouble(dateIn);
        AbsoluteDate date = new AbsoluteDate(mjdRefUTC,mjdDate* Constants.JULIAN_DAY, utc);
        return date;
    }


    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();


        /* Configuring data loading */
        File home = new File(System.getProperty("user.home"));
        File orekitData = new File(home, "orekit-data");
        DataProvidersManager manager = DataProvidersManager.getInstance();
        manager.addProvider(new DirectoryCrawler(orekitData));


        /* Initial data path  */
        File pathDataIn = new File("C:\\Users\\nolja\\Documents\\ESA Socis\\Orbit Determination\\testing2\\42778");

        /* Initialising an array of filenames in the input data folder */
        String[] fileList = pathDataIn.list();


        /* Output data path */
        File pathDataOut = new File("C:\\Users\\nolja\\Documents\\ESA Socis\\Orbit Determination\\orbitdetermination\\satnogs-orbit-determination\\src\\main\\resources");

        //int secDay = 24*60*60 ; // Calculates the seconds in a day, used to calculate seconds between mesaurements (which are given in MJD)


        double c = 2.998*Math.pow(10,8); // Speed of light
        double downlinkFreq = 1.45961 * Math.pow(10, 8);  // The value of the downlink freq is taken from https://db.satnogs.org/satellite/42778/
        System.out.println("The downlink frequency is: " + downlinkFreq + "Hz");


        /* Setting up the initial parameters that are to be passed to the file */
        String measType = "   RANGE_RATE";
        //SString antennaID = "   39-CGBSAT-VHF         ";


        TimeScale utc = TimeScalesFactory.getUTC();
        AbsoluteDate mjdRefUTC = new AbsoluteDate(1858,11,17,utc);

        BufferedReader reader;

        for(String filename : fileList) {


            if(filename.endsWith(".dat")) {

                try {

                    long tIn = System.currentTimeMillis();

                    /* Initialising the filereader  */
                    reader = new BufferedReader(new FileReader(pathDataIn + "\\" + filename));

                    String line = reader.readLine();

                    /* Initialising the filewriter */

                    BufferedWriter bw = null;
                    File file = new File(pathDataOut + "\\" + filename);

                    /* This logic will make sure that the file
                     * gets created if it is not present at the
                     * specified location*/

                    if (!file.exists()) {
                        file.createNewFile();
                    }

                    FileWriter fw = new FileWriter(file);
                    bw = new BufferedWriter(fw);

                    System.out.println("Finished the initial setup for file: " + filename);

                    while (line != null) {

                        String[] columns = line.split(" ");

                        line = reader.readLine();   // Required to prevent the same line being run an infite number of times


                        if(filename.startsWith("2019-06-11T19")){
                            String antennaID = "   39-CGBSAT-VHF-P1         ";
                            String outputline = mjdDate(columns[0],utc,mjdRefUTC) + measType + antennaID + rRcalculator(columns[1],downlinkFreq,c) + "\n";
                        bw.write(outputline);
                        }
                        else if (filename.startsWith("2019-06-11T21")){
                            String antennaID = "   39-CGBSAT-VHF-P2         ";
                            String outputline = mjdDate(columns[0],utc,mjdRefUTC) + measType + antennaID + rRcalculator(columns[1],downlinkFreq,c) + "\n";
                        bw.write(outputline);
                        }
                        else {
                            String antennaID = "   39-CGBSAT-VHF         ";
                            String outputline = mjdDate(columns[0],utc,mjdRefUTC) + measType + antennaID + rRcalculator(columns[1],downlinkFreq,c) + "\n";
                        bw.write(outputline);
                        }


                    }


                    /* Closing the buffered writer */
                    try {
                        if (bw != null)
                            bw.close();
                    }
                    catch (Exception ex) {
                        System.out.println("Error in closing the BufferedWriter" + ex);
                        reader.close();
                    }



                    /* Calculating the time taken for this file to be converted */
                    long tOut = System.currentTimeMillis();
                    double tTaken = 0.001 * (tOut - tIn);
                    System.out.println("Conversion of file:" + filename + " completed in: " + tTaken + "s");

                }

                catch (IOException e) {
                    e.printStackTrace();

                }
            }

            else {
                break;
            }
        }

        long endTime = System.currentTimeMillis();

        System.out.println("All files have been converted \n" + "Total time taken is: " + 0.001*(endTime-startTime)+"s");
    }

}