Insert a read file in Atmosphere

Hi there,
I am prepared to add a read file class in Atmosphere, which includes a set of density values.
But I usually use python, very rookie in Java, which makes me big trouble.
I wrote the java class as follows,

package org.orekit.models.earth.atmosphere;

import java.io.*;

import org.hipparchus.CalculusFieldElement;
import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
import org.hipparchus.geometry.euclidean.threed.Vector3D;
import org.hipparchus.util.FastMath;
import org.orekit.bodies.BodyShape;
import org.orekit.bodies.FieldGeodeticPoint;
import org.orekit.bodies.GeodeticPoint;
import org.orekit.frames.Frame;
import org.orekit.time.AbsoluteDate;
import org.orekit.time.FieldAbsoluteDate;

public class SimpleAtmosphere implements Atmosphere {

        /** Serializable UID.*/
    private static final long serialVersionUID = 2772347498196369601L;

    /** Earth shape model. */
    private BodyShape    shape;

    /** Create an exponential atmosphere.
     * @param shape body shape model

     */
    public SimpleAtmosphere(final BodyShape shape) {
        this.shape  = shape;

    }

    /** {@inheritDoc} */
    public Frame getFrame() {
        return shape.getBodyFrame();
    }

    /** {@inheritDoc} */
    public double getDensity(final AbsoluteDate date, final Vector3D position, final Frame frame) {
        String path = "D:\\Desktop\\OrbitalPropagator\\density.txt";
        File file = new File(path);
        if (!file.exists()){
            System.exit(0);
        }

        BufferedReader reader = null;
        String temp = null;
        String rho0 = null;

        // define date
        try {
            reader = new BufferedReader(new FileReader((file)));
            while((temp = reader.readLine())!=null){
                String[] linenow = temp.split(" ");
                String year = linenow[0];
                String month = linenow[1];
                String day = linenow[2];
                String time = linenow[3];
                String density = linenow[10];

                String date1 = date.toString();
                String year1 = date1.substring(0, 4);
                String month1 = date1.substring(5, 7);
                String day1 = date1.substring(8, 10);
                String time1 = date1.substring(11, 19);

                if (year.equals(year1) ){

                    if (month.equals(month1) ) {

                        if (day.equals(day1) ) {
                            String rho3 = density;
                            if (time.equals(time1) ){
                                rho0 = density;
                                break;
                            }
                        }
                    }

                }
            }
        }
        catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }

        double rho = Double.parseDouble(rho0);

        return rho ;        
    }
}

I try to imitate a simple model in the Atmosphere class.
But I still could not let it work.
If you could teach me about the reason, I would be very grateful!!
Thank you!
Roint

Note that this implementation, will be extremely slow as it will open the file and parse it each time getDensity() is called, which is at each evaluation of the drag force model during propagation. Atmosphere models should rather parse the file once at construction, store the result in a fast data structure for later access (an array, a list, a map…) and use that data structure when getDensity is called.

Looking more precisely at your class, as you have a density that depends only on date and not on position (which is quite strange as density profile is often exponential-like with respect to altitude), I would suggest to store the parsed density in a TimeSpanMap<Double>.

You could add a field in the class:

  public class SimpleAtmosphere implements Atmosphere {
    private final TimeSpanMap<Double> densityHistory;
    private BodyShape    shape;

    public SimpleAtmosphere(final BodyShape shape) {
        this.shape  = shape;

       this.densityHistory = new TimeSpanMap<>(null);

       TimeScale utc = TimeScalesFactory.getUTC();
        String path = "D:\\Desktop\\OrbitalPropagator\\density.txt";
        File file = new File(path);
        if (!file.exists()){
            throw new RuntimeException(path + " does not exist");
        }

        try (Reader         reader = new FileReader(file);
             BufferedReader br = new bufferedReader(reader)) {
             for (String line= br.readLine() ; line !=null; line = br.readLine()){
                String[] linenow = line.split(" ");
                int year = Integer.parseInt(linenow[0]);
                int month = Integer.parseInt(linenow[1]);
                int day = Integer.parseInt(linenow[2]);
                int hour = Integer.parseInt(linenow[3].substring(0, 2));
                int minute = Integer.parseInt(linenow[3].substring(3, 5));
                double second = Double.parseDouble(linenow[3].substring(6));
                double density = Double.parseDouble(llinenow[10]);

                AbsoluteDate lineDate = new AbsoluteDate(year, month, date, hour, minute, second, utc);
                densitytHistory.addValidAfter(lineDate, density);
          }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
   }

    public double getDensity(final AbsoluteDate date, final Vector3D position, final Frame frame) {
       return densityHistory.get(date).doubleValue();
    }
}  

Beware! I have written this on the fly in the forum interface, it most probably contains some syntax errors. I have also assumed an arbitrary format for the time in the file, which may be different from your data. So take this example only as a rough sketch that need to be adapted to your needs.

1 Like

Thank you very much, Luc!
I have applied several days of orbit measurements density values, based on a specific satellite. Its format includes DateTime, position (lat, lon, alt), and total mass density.
So, I thought to propagate the orbit with the same time step, and replace the density with the measurement value, to validate whether the measured density is suitable and calculate the deviation from the density model.
As you say, every time getDensity() will open the file and read will slow the program, it do important to pre-read the data into a java data structure.