oreCommon singleton class

Saluti to all
this is a singleton class for the initialization of orekit and the definition of some useful objects that requires some time so that will be defined once.
The Orekit, Rugged, Hipparchus and Java versions are also printed
an oreCommon object could be created everywhere in the code but only the first time is instantiated
I hope it will be useful

import os
from datetime import datetime, timezone
import orekit

from orekit.pyhelpers import setup_orekit_curdir, absolutedate_to_datetime, datetime_to_absolutedate

# *** imports here ***

#*************************FLS_SBG_OrekitTools *************************
# 
# NAME: oreCommon
#       
# PURPOSE: singleton class for Orekit initialization and 
#   definition of common variables
#       
# HISTORY:
# Written by:     Benedetto Michelozzi, Flysight, January, 2025
# VERSION   DATE            Name            Note
# 0.3.0     January, 2025   B.Michelozzi    first deploy
# 0.3.1     January, 2025   B.Michelozzi    comments and header
#  
#************************* ------------------------- *************************

 # ********************** UNDER DEVELOPMENT **********************

class oreCommon():
    """ singleton class for Orekit initialization and definition of common variables
    
    first constructor Args:
        doPrint:bool = enables prints on stdout 
    
    Init operations:
        The fist time a oreCommon object is instantiated, the input arguments are parsed and acquired.
        The following times the input arguments are discarded, and the first istance is returned.
        oreCommon initialises the Orekit library.
        Then defines the following objects:
            self.sun        : sun CelestialBodyFactory PVCoordinatesProvider object
            self.earthITRF  : earth OneAxisEllipsoid WHS84 - ITRF frame
            self.ITRF       : framework 
            self.ITRFe      : framework
            self.TIRF       : framework
            self.GTOD       : framework
            self.EME2000    : framework 
            self.TEME       : framework
            self.GCRF       : framework
            self.TOD        : framework
            self.MOD        : framework
            self.CIRF       : framework
            self.ICRF       : framework      
            self.utc        : TimeScalesFactory UTC 

    """
    def __new__(cls, doPrint = False):
        if not hasattr(cls, 'instance'):
            cls.instance = super(oreCommon, cls).__new__(cls)
            cls.instance._firstInit_(doPrint=doPrint)
        return cls.instance
    
    def __init__(self, doPrint = False):
        self.doPrint = doPrint
        self.nInst += 1

    def _firstInit_(self, doPrint=False):

        self.doPrint = doPrint
        self.nInst = 0

        vm = orekit.initVM()
        if self.doPrint:
            print(' Orekit version:', orekit.VERSION)
            print(' Rugged version: 3.0')
            print(' Hipparchus ver: 3.1')
            print('   Java version:',vm.java_version)

        setup_orekit_curdir()
        if not os.path.exists('orekit-data.zip'):
            orekit.pyhelpers.download_orekit_data_curdir()

        sun = CelestialBodyFactory.getSun()     # Sun as an CelestialBody
        self.sun = PVCoordinatesProvider.cast_(sun)  # cast to PVCoord interface

        self.ITRF = FramesFactory.getITRF(IERSConventions.IERS_2010, True) # International Terrestrial Reference Frame  
        self.ITRFe = FramesFactory.getITRFEquinox(IERSConventions.IERS_2010, True) # International Terrestrial Reference Frame  
        self.TIRF = FramesFactory.getTIRF(IERSConventions.IERS_2010) # True of date, rotating
        self.GTOD = FramesFactory.getGTOD(IERSConventions.IERS_2010, True) # Greenwich True of Date Coordinate System - a rotating, right-handed, Cartesian system with the origin at the center of the Earth.

        self.EME2000 = FramesFactory.getEME2000() # Earth's Mean Equator and Mean Equinox
        self.TEME = FramesFactory.getTEME() # true equator, mean equinox ( Norad TLE )
        self.GCRF = FramesFactory.getGCRF() # Geocentric Celestial Reference Frame.
        self.TOD = FramesFactory.getTOD(True) # True of Date 
        self.MOD = FramesFactory.getMOD(True) # Mean of Date 
        self.CIRF = FramesFactory.getCIRF(IERSConventions.IERS_2010, True)

        self.ICRF = FramesFactory.getICRF() # International Celestial Reference Frame.

        self.earthITRF = OneAxisEllipsoid(   Constants.WGS84_EARTH_EQUATORIAL_RADIUS, 
                                Constants.WGS84_EARTH_FLATTENING, 
                                self.ITRF)
        
        self.utc = TimeScalesFactory.getUTC()

        if self.doPrint:
            print('    Orekit init: Ok')

# for testing pourposes
if __name__ == '__main__':

    myOreCommon = oreCommon(True)
    anotherOreCommon = oreCommon(False)
    anotherOreCwmmon = oreCommon(True)
    anothwreCommon = oreCommon(False)
    anothereCommon = oreCommon(True)

3 Likes