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
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.
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?
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.
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.
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)