Casting np.array into JArray_double2D

Hello everyone,

I am trying to use the second constructor in this class to feed my PV measurements to the BatchLeastSquare method:

PV(AbsoluteDate date, Vector3D position, Vector3D velocity, double[][] covarianceMatrix, double baseWeight, ObservableSatellite satellite)

Well, I believe everything is correct except the covarianceMatrix. I have a numpy array, while the constuctor accepts JArray_double2D. So I need to convert my covarianceMatrix from numpy array to 2 dimensional (6x6) JArray. I am not hundred percent sure but some casting must be done in order to accomplish this.

from orekit.pyhelpers import datetime_to_absolutedate, JArray, JArray_double2D
from org.orekit.estimation.measurements import PV, ObservableSatellite
from org.hipparchus.geometry.euclidean.threed import Vector3D
from orekit import JArray_double
from org.hipparchus.linear import Array2DRowRealMatrix

observableSatellite = ObservableSatellite(0) # Propagator index = 0

covarianceMatrix  = np.zeros((6, 6))

for i in range(1000): #4321
    pos3D = Vector3D(float(truedata_df['pos_x'][i])*1000,
                     float(truedata_df['pos_y'][i])*1000,
                     float(truedata_df['pos_z'][i])*1000)                  # in [m]

   vel3D = Vector3D(float(truedata_df['vel_x'][i])*1000,
                     float(truedata_df['vel_y'][i])*1000,
                     float(truedata_df['vel_z'][i])*1000)                  # in [m/s]
    
    date = AbsoluteDate(truedata_df['date'][i].year,
                        truedata_df['date'][i].month,
                        truedata_df['date'][i].day,
                        truedata_df['date'][i].hour,
                        truedata_df['date'][i].minute,
                        float(truedata_df['date'][i].second)+0.566,
                        UTC)
    
    covarianceMatrix = np.array([[float(df_cov[i][0]), float(df_cov[i][1]), float(df_cov[i][3]), float(df_cov[i][6]), float(df_cov[i][10]), float(df_cov[i][15])],
                                 [float(df_cov[i][1]), float(df_cov[i][2]), float(df_cov[i][4]), float(df_cov[i][7]), float(df_cov[i][11]), float(df_cov[i][16])],
                                 [float(df_cov[i][3]), float(df_cov[i][4]), float(df_cov[i][5]), float(df_cov[i][8]), float(df_cov[i][12]), float(df_cov[i][17])],
                                 [float(df_cov[i][6]), float(df_cov[i][7]), float(df_cov[i][8]), float(df_cov[i][9]), float(df_cov[i][13]), float(df_cov[i][18])],
                                 [float(df_cov[i][10]), float(df_cov[i][11]), float(df_cov[i][12]), float(df_cov[i][13]), float(df_cov[i][14]), float(df_cov[i][19])],
                                 [float(df_cov[i][15]), float(df_cov[i][16]), float(df_cov[i][17]), float(df_cov[i][18]), float(df_cov[i][19]), float(df_cov[i][20])]])

    covarianceMatrix_1 = JArray_double.cast_(JArray('double')(covarianceMatrix[0]))
    covarianceMatrix_2 = JArray_double.cast_(JArray('double')(covarianceMatrix[1]))
    covarianceMatrix_3 = JArray_double.cast_(JArray('double')(covarianceMatrix[2]))
    covarianceMatrix_4 = JArray_double.cast_(JArray('double')(covarianceMatrix[3]))
    covarianceMatrix_5 = JArray_double.cast_(JArray('double')(covarianceMatrix[4]))
    covarianceMatrix_6 = JArray_double.cast_(JArray('double')(covarianceMatrix[5]))
    
    orekitPV_est = PV(date, pos3D, vel3D, covarianceMatrix, baseweight, observableSatellite)
    estimator.addMeasurement(orekitPV_est)

Individual elements in numpy covariance matrix can be cast into JArray_double object, but how can I get a 2D JArray using these elements? Below line returns an empty 2D JArray, but how can I assign above casted elements into this 2D JArray?

    covarianceMatrix = JArray_double2D(6,6)

Also, after creating JArray_double2D method we again need another .cast_ right? Do you have any idea how can it be done @petrus.hyvonen?

Basically, I couldn’t add my covariance matrix (np.array) to my estimations and this is my problem in a nutshell :sweat_smile: Anyone else faced a similar issue before? Any advice is appreciated.

Thanks,
Baris

Hi there,

I have never used Jarray to create a matrix for Orekit, but instead you can instantiate an Array2DRowRealMatrix and fill its elements with setEntry.

Best,
Romain.

Hi,

Hard to test without an working example (truedata_df missing), but I think you should be able to similar as in the JArray_double2D routine:

  arr = JArray('object')(x)

    for i in range(x):
        arr[i] = JArray('double')(y)

There arr[i] is your casted arrays and merging these into a JArray object and arr would be your final 2D matrix.

Hello @petrus.hyvonen , thanks for the reply. I’ve tried your suggestion as following:

arr = JArray('object')(6)

for i in range(6):        
   arr[i] = JArray('double')(covarianceMatrix[i])
            
orekitPV_est = PV(date, pos3D, vel3D, arr, baseweight, observableSatellite)    
estimator.addMeasurement(orekitPV_est)    

and recieved an error that looks like this:


JavaError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_19536\1758518618.py in <module>     
85     
86     covarianceMatrix = Array2DRowRealMatrix(arr)
---> 87     orekitPV_est = PV(date, pos3D, vel3D, arr, baseweight, observableSatellite)     
88     estimator.addMeasurement(orekitPV_est)     
89
JavaError: <super: <class 'JavaError'>, <JavaError object>>
    Java stacktrace:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[D
	at org.orekit.estimation.measurements.PV.<init>(PV.java:173)

so it seems like after casting elements we are supposed to cast them once again but it does not let me. Could not proceed with this way.

On the other hand, hello @Serrof, I will try your suggestion and keep you updated.

@Echulion I use @Serrof 's method for setting the covariance matrix element by element and haven’t had any problems - though I am setting the initial covariance matrix for the Kalman Filter, not in a PV measurement like you.

from org.hipparchus.linear import Array2DRowRealMatrix

covariance_matrix = [<here I have 6 rows of 6-element lists (should be similar to your numpy approach)>]
num_params = len(covariance_matrix)
covariance_matrix_java = Array2DRowRealMatrix(num_params, num_params)
for row in range(num_params):
    for col in range(num_params):
        covariance_matrix_java.setEntry(row, col, covariance_matrix[row][col])