First step with matlab

Hi,

There used to be a small example on the previous orekit wiki but cannot find that page now. I do not have a matlab license anymore but this below is the example that at some point was on the wiki. At least is shows how the library is interfaced.

Regards
/Petrus

%
% /* Copyright 2002-2018 CS Systèmes d'Information
%  * Licensed to CS Systèmes d'Information (CS) under one or more
%  * contributor license agreements.  See the NOTICE file distributed with
%  * this work for additional information regarding copyright ownership.
%  * CS licenses this file to You under the Apache License, Version 2.0
%  * (the "License"); you may not use this file except in compliance with
%  * the License.  You may obtain a copy of the License at
%  *
%  *   http://www.apache.org/licenses/LICENSE-2.0
%  *
%  * Unless required by applicable law or agreed to in writing, software
%  * distributed under the License is distributed on an "AS IS" BASIS,
%  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%  * See the License for the specific language governing permissions and
%  * limitations under the License.
%  */
%
% Translated from SlaveMode.java to Matlab by Petrus Hyvönen 2011 as an
% example of how to access orekit from matlab
% the jars orekit-8.0.jar, hipparchus-core-1.0.jar, hipparchus-geometry-1.0.jar,
% hipparchus-ode-1.0.jar, hipparchus-fitting-1.0.jar, hipparchus-optim-1.0.jar
% and orekit-data.zip is in current matlab dir.


______
% These seems to work if pasted to prompt.
javaaddpath 'C:\ ... enter your path here ...\MATLAB'
javaaddpath 'C:\.. enter your path here ...\MATLAB\orekit-8.0.jar'
javaaddpath 'C:\.. enter your path here ...\\MATLAB\hipparchus-core-1.0.jar
javaaddpath 'C:\.. enter your path here ...\\MATLAB\hipparchus-geometry-1.0.jar
javaaddpath 'C:\.. enter your path here ...\\MATLAB\hipparchus-ode-1.0.jar
javaaddpath 'C:\.. enter your path here ...\\MATLAB\hipparchus-fitting-1.0.jar
javaaddpath 'C:\.. enter your path here ...\\MATLAB\hipparchus-optim-1.0.jar


%% do the imports
import org.orekit.errors.OrekitException
import org.orekit.frames.Frame
import org.orekit.frames.FramesFactory
import org.orekit.orbits.KeplerianOrbit
import org.orekit.orbits.Orbit
import org.orekit.orbits.PositionAngle
import org.orekit.propagation.SpacecraftState
import org.orekit.propagation.analytical.KeplerianPropagator
import org.orekit.data.DataProvidersManager
import org.orekit.data.ZipJarCrawler
import org.orekit.time.AbsoluteDate
import org.orekit.time.TimeScalesFactory

%% Configure Orekit. The file orekit-data.zip must be in current dir
DM=org.orekit.data.DataProvidersManager.getInstance()
crawler=org.orekit.data.ZipJarCrawler('orekit-data.zip')
DM.clearProviders()
DM.addProvider(crawler)

%% Initial orbit parameters
a = 24396159;    % semi major axis in meters
e = 0.72831215;  % eccentricity
i = (7.0)/180*pi; % inclination
omega = (180)/180*pi; % perigee argument
raan = (261)/180*pi; %right ascension of ascending node
lM = 0.0; % mean anomaly

%% Set inertial frame
inertialFrame = FramesFactory.getEME2000()

%% Initial date in UTC time scale
utc = TimeScalesFactory.getUTC();
initialDate = AbsoluteDate(2004, 01, 01, 23, 30, 00.000, utc)

%% Setup orbit propagator
%gravitation coefficient
mu =  3.986004415e+14

%Orbit construction as Keplerian
initialOrbit = KeplerianOrbit(a, e, i, omega, raan, lM, PositionAngle.MEAN, inertialFrame, initialDate, mu)

%Simple extrapolation with Keplerian motion
kepler = KeplerianPropagator(initialOrbit);

%Set the propagator to slave mode (could be omitted as it is the default mode)
kepler.setSlaveMode()

%% Setup propagation time
%Overall duration in seconds for extrapolation
duration = 180*60.0

%Stop date
finalDate =  AbsoluteDate(initialDate, duration, utc)

%Step duration in seconds
stepT = 30.0

%% Perform propagation
%Extrapolation loop
cpt = 1;
extrapDate = initialDate
while extrapDate.compareTo(finalDate) <= 0
    currentState = kepler.propagate(extrapDate);
    fprintf('step %d: time %s %s\n', cpt, char(currentState.getDate()), char(currentState.getOrbit()))
    coord=currentState.getPVCoordinates.getPosition;
    P(:,cpt)=[coord.getX coord.getY coord.getZ]';
    extrapDate = AbsoluteDate(extrapDate, stepT, utc);
    cpt=cpt+1;
end
%%
figure;
plot3(P(1,:),P(2,:),P(3,:));

Hi Petrus,

I don’t know why this example has not been migrated with the others. I temporarily disabled the redirection in Apache server to allow access to the wiki of our old forge. The example you are looking for is here:

https://www.orekit.org/forge/projects/orekit/wiki/Integration_in_Matlab

Regards,

Sébastien

Thanks a lot for this example.

I still have a problem to run this script, the command window return me :

Undefined variable “org” or class “org.orekit.data.DataProvidersManager.getInstance”.
Error in PropagationDistribution (line 19)
DM=org.orekit.data.DataProvidersManager.getInstance()

Have got any idea to solve this issue ?

Thanks

Hi @Tintin

What version of Orekit do you use ? To my mind, your problem is that you don’t use a correct version of Java on your Matlab interface.

You shall use, at least, Java 8 to run correctly Orekit on your Matlab interface (if your version of Orekit is greater than 8.0).

To check your version of Java, on your Matlab terminal write : version -java

In the case of your Java version is lower than 8, download the correct jdk version and follow the recommendations on the Mathworks website depending if you are using Linux or Windows.

Best regards,

Bryan

Hi @bcazabonne,

I come back to this topic. I have tried the versin -java on matlab R2017b and the output is:

version -java

ans =

'Java 1.8.0_121-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode'

Is this java version compatible with the orekit-10.1.jar file?

Thank you very much,

Rubén

Hi,

Orekit 10.1 needs, at least, Java 8 to work. Therefore, you have a compatible Java version.

Bryan

Hi @Ruben ,

I am using Orekit 10.1 with Matlab R2018b and R2020a on both Linux and Windows without issues.

Here is my Java version:

'Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode'

Did you add all Hipparchus dependencies to the Java class path too?

To load Orekit+Rugged+all Hipparchus dependencies, regardless of the version, this is what my startup.m script looks like:

orekit_path = <MY PATH TO THE OREKIT&HIPPARCHUS FOLDER>;
addpath(genpath(orekit_path));
javaaddpath( orekit_path );

hipparchusModules = {'clustering', 'core',  'fft', 'filtering', 'fitting',...
    'geometry', 'migration', 'ode', 'optim', 'samples', 'stat'};

% Looking for Hipparchus JARs, independently of the version
for i = 1:length(hipparchusModules)
    hipparchusJarName = dir(fullfile( orekit_path, strcat('hipparchus-', ...
        hipparchusModules{i}, '-*.jar')));
    javaaddpath( fullfile(orekit_path, hipparchusJarName.name ));
end

% Looking for Orekit JAR, independently of the version
orekitJarName = dir(fullfile( orekit_path, 'orekit-*.jar'));
javaaddpath( fullfile(orekit_path, orekitJarName.name ));

% Looking for Rugged JAR, independently of the version
ruggedJarName = dir(fullfile( orekit_path, 'rugged-*.jar'));
javaaddpath( fullfile(orekit_path, ruggedJarName.name ));

clear orekit_path hipparchusModules hipparchusJarName i orekitJarName ruggedJarName;

The javaclasspath command then gives the following results:

[...]
DYNAMIC JAVA PATH

<MY PATH TO THE OREKIT FOLDER>/rugged-2.1.jar               
<MY PATH TO THE OREKIT FOLDER>/orekit-10.1.jar              
<MY PATH TO THE OREKIT FOLDER>/hipparchus-stat-1.6.jar      
<MY PATH TO THE OREKIT FOLDER>/hipparchus-samples-1.6.jar   
<MY PATH TO THE OREKIT FOLDER>/hipparchus-optim-1.6.jar     
<MY PATH TO THE OREKIT FOLDER>/hipparchus-ode-1.6.jar       
<MY PATH TO THE OREKIT FOLDER>/hipparchus-migration-1.6.jar 
<MY PATH TO THE OREKIT FOLDER>/hipparchus-geometry-1.6.jar  
<MY PATH TO THE OREKIT FOLDER>/hipparchus-fitting-1.6.jar   
<MY PATH TO THE OREKIT FOLDER>/hipparchus-filtering-1.6.jar 
<MY PATH TO THE OREKIT FOLDER>/hipparchus-fft-1.6.jar       
<MY PATH TO THE OREKIT FOLDER>/hipparchus-core-1.6.jar      
<MY PATH TO THE OREKIT FOLDER>/hipparchus-clustering-1.6.jar
<MY PATH TO THE OREKIT FOLDER>

Hi, I have added the jar of orekit 11.2 to matlab R2021a’s java path, as well as the hipparchus kit.
However, there isn’t “getInstance()” and “crawler=org.orekit.data.ZipJarCrawler(‘orekit-data-master.zip’)” didn’t work as intended. By the way, the file named orekit-data-master.zip is in the current dir.

The warning shows as following:

Java exception occurred:
java.lang.NullPointerException

at org.orekit.data.ZipJarCrawler.<init>(ZipJarCrawler.java:112)

at org.orekit.data.ZipJarCrawler.<init>(ZipJarCrawler.java:99)

Are there more information or introduction for work with matlab?

Hi @AccLxc

Did you look at our introduction to Matlab tutorial? It could be useful to help you using Orekit in a Matlab environment.

The tutorial is available here: https://www.orekit.org/site-orekit-tutorials-11.2/tutorials/integration-in-other-languages.html

Just be careful to update the different path and folder names with yours.

Best regards,
Bryan

Hi @AccLxc and @bcazabonne,

I looked into the “Matlab” part of the “Integration in other languages” tutorial and we still have a reference to

DM=org.orekit.data.DataProvidersManager.getInstance()

Which I think is wrong since 10.1 and the introduction of DataContext.

It looks like this recent post in Python.
I think the line above should be replaced with:

DM=org.orekit.data.DataContext.getDefault().getDataProvidersManager()

Right!

We must open an issue in the Orekit tutorial project and fix it.

Bryan

@AccLxc, do you want to open it ? Since you found the bug.

The tutorials issues are located on the dedicated forge.

Note that the Jython part of this page is wrong for the same reason… But I don’t know if the Jython integration is still necessary since there is the Python wrapper.

Thanks a lot, the first question is done!

Well, I will read it carefully, thanks.

The second question is done too, with the assistance in Can't get Orekit to work in Matlab. What I improved in my code was to import some basic java file.

% From java
import java.lang.Math;
import java.lang.System;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.io.File;

Maybe it’s useful for others, thanks for your help.