Python wrapper and interfaces/types/

Hi there :slight_smile:

I’m trying to adapt my code (previously only using Java) in python using the Orekit Python wrapper (thanks a lot to those who developed it), it works well but I have some trouble for adapting some specidif parts of the code.

For example, in Java, we can have the following method :

     /** Add a measurement to a list if it has non-zero weight.
     * @param measurement measurement to add
     * @param measurements measurements list
     */
    private void addIfNonZeroWeight(final ObservedMeasurement<?> measurement, final List<ObservedMeasurement<?>> measurements) {
        double sum = 0;
        for (double w : measurement.getBaseWeight()) {
            sum += FastMath.abs(w);
        }
        if (sum > Precision.SAFE_MIN) {
            // we only consider measurements with non-zero weight
            measurements.add(measurement);
        }
    }

and my problem here would be to translate the “ObservedMeasurements<?>" and "List>” in python.

Same thing for the following code :

// Initialize the table according to the number of stations

this.stations = new HashMap<String, StationData>(stationLatitudes.length * 2);

Here I don’t know how to specify that the hashmap has to be of String and StationData type.

edit : and one other problematic line of code is :

displacements = new StationDisplacement[] {
                    		universe.getTidalDisplacement(), oceanLoading
                    };

Would in this case “displacements = StationDisplacement[] { universe.tidalDisplacement, oceanLoading }” work or not ?

If you have any idea, it would be great !
Thanks a lot and have a good day :wink:

In the Python wrapper, you often need to cast the java classes to make certain operations/functions work. In your first example:

from java.util import ArrayList
measurement = ArrayList().of_(ObservedMeasurement)
# Here you would add your measurements to the list

measurements = ArrayList().of_(ObservedMeasurement)
sum_var = 0
for x in range(measurement.size()):
    sum_var += abs(measurement.get(x).getBaseWeight())
    if sum_var > Precision.SAFE_MIN:
        measurements.add(measurement.get(x))

I called your ‘sum’ ‘sum_var’ since you shouldn’t name variables the same as built-in Python functions.

For the second example, in Python you would use a dictionary, and there is no need to specify the types of data going in or the size. You could initialize the dictionary with self.stations = {} and then add to it later with dict[key] = value → self.stations[<your string>] = <your StationData type variable>. Note that the self is the same as this, though you likely wouldn’t need to subclass, so that probably isn’t needed.

I am not sure about the third one since StationDisplacements is an interface (which would need to get subclassed in Python), however the example you provided would not work.

1 Like

Hi,

In case you need to make a HashMap for input to some specific orekit interface the syntax for multiple types is

HashMap().of_(Type, Type)

And indeed as you are pointing out aerow, the StationDisplacement is a subclassing. The inline anonymous subclassing don’t really exist in the same way in Python (neatly at least, as I understand) and it would be recommended to create a new class by subclassing PythonStationDisplacement and then adding code to this. I don’t really understand the java code that is included in that method declaration “universe.getTidalDisplacement(), oceanLoading”, but haven’t looked into this in depth?

Regards

1 Like

And in case you have not seen it, a good place to look for somewhat advanced examples is the orekit-python-wrappers test suite, https://github.com/petrushy/orekit_python_artifacts/tree/version-10.1/test

1 Like

Thanks for your answers !
I’ll try to adapt my code and use a dictionary instead of the HashMap, it should be possible, I think.

For the ObservedMeasurement, do I need to specify the type of measurements that are added or will it be implied automatically ?

Concerning StationDisplacement, do you have nearby a simple example of subclassing ? I do not really know what is needed in such a new class for it to work properly.

N.B. : universe.getTidalDisplacement() and oceanLoading are just two possible displacements (in org.orekit.models.earth.displacement) and “displacements” is then used as the 3rd parameter in a new GroundStation.

Hi,

For subclassing in Python see for example the event handler test in:

In this example we’re subclassing EventHandler by using the special class PythonEventHandler (written in java) that contains code for callbacks as Python code.

I have not tested the StationDisplacement subclassing but should work same way.

2 Likes

Thanks for your answer, I’ll try that.

Another question regarding the casting of Java classes. I tried to cast the OutlierFilter class but it doesn’t seem to work :

In the mean time, it worked with the Bias class (in the same java package) so I’m lost.
Any idea why it does not work ?

You shouldn’t need to cast the outlier filter, just declare it and later add it to each measurement. However the reason for the InvalidArgsError is because the second arg in OutlierFilter needs to be a float.

Here’s how I normally do the outlier filter:

rangeOutliersManager = OutlierFilter(10, 5.0)

# Loop over measurements and ingest them to Orekit measurement classes, add OutlierFilter
meas_out = ArrayList().of_(ObservedMeasurement)
for meas in meas_list:
    obs_meas = Range(*args)
    obs_meas.addModifier(rangeOutliersManager)
    meas_out.add(obs_meas)
1 Like