Skip to content

Commit

Permalink
src: code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
henrich14 committed Oct 29, 2024
1 parent b7411d2 commit 44a62ea
Show file tree
Hide file tree
Showing 7 changed files with 655 additions and 758 deletions.
15 changes: 6 additions & 9 deletions examples/BADAData.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,36 @@
Example of BADA parametes retrieval for specific aircraft
"""

from pyBADA.bada3 import Bada3Aircraft
from pyBADA.bada3 import Parser as Bada3Parser
from pyBADA.aircraft import Bada

badaVersion = "DUMMY"

# loading all the BADA data into a dataframe
allData = Bada3Parser.parseAll(badaVersion=badaVersion)
allData = Bada3Parser.parseAll(badaVersion="DUMMY")

# retrieve specific data from the whole database, including synonyms
params = Bada3Parser.getBADAParameters(
params = Bada.getBADAParameters(
df=allData,
acName=["B737", "A1", "P38", "AT45", "DA42"],
parameters=["VMO", "MMO", "MTOW", "engineType"],
)
print(params)
print("\n")

params = Bada3Parser.getBADAParameters(
params = Bada.getBADAParameters(
df=allData,
acName=["B737"],
parameters=["VMO", "MMO", "MTOW", "engineType"],
)
print(params)
print("\n")

params = Bada3Parser.getBADAParameters(
params = Bada.getBADAParameters(
df=allData, acName="DA42", parameters=["VMO", "MMO", "MTOW", "engineType"]
)
print(params)
print("\n")

params = Bada3Parser.getBADAParameters(
df=allData, acName="DA42", parameters="VMO"
)
params = Bada.getBADAParameters(df=allData, acName="DA42", parameters="VMO")
print(params)
print("\n")
48 changes: 32 additions & 16 deletions examples/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@
from pyBADA.bada3 import Bada3Aircraft
from pyBADA.bada4 import Bada4Aircraft
from pyBADA.badaH import BadaHAircraft
from pyBADA.bada3 import Parser as Bada3Parser
from pyBADA.bada4 import Parser as Bada4Parser
from pyBADA.badaH import Parser as BadaHParser

# initialization of BADAH
AC = BadaHAircraft(badaVersion="DUMMY", acName="DUMH")

# initialization of BADA3, BADA4 and BADAH
# uncomment for testing different BADA family if available
# BADAH
badaVersion = "DUMMY"
AC = BadaHAircraft(badaVersion=badaVersion, acName="DUMH")

# BADA4
# AC = Bada4Aircraft(badaVersion=badaVersion, acName='Dummy-TBP')

# BADA3
# AC = Bada3Aircraft(badaVersion=badaVersion, acName='BZJT')
if AC.BADAFamily.BADAH:
ICAO = AC.ICAO
WTC = AC.WTC
MTOW = AC.MTOW
print(
"BADA Family:",
AC.BADAFamilyName,
"| BADA Version:",
AC.BADAVersion,
"| ICAO:",
ICAO,
"| WTC:",
WTC,
"| MTOW =",
MTOW,
)

# initialization of BADA4
AC = Bada4Aircraft(badaVersion="DUMMY", acName="Dummy-TBP")

# BADA3 or BADA4
if AC.BADAFamily.BADA3 or AC.BADAFamily.BADA4:
Expand All @@ -50,10 +57,15 @@
MTOW,
)

# BADAH
if AC.BADAFamily.BADAH:
# initialization of BADA3
AC = Bada3Aircraft(badaVersion="DUMMY", acName="BZJT")

# BADA3 or BADA4
if AC.BADAFamily.BADA3 or AC.BADAFamily.BADA4:
ICAO = AC.ICAO
WTC = AC.WTC
VMO = AC.VMO
MMO = AC.MMO
MTOW = AC.MTOW
print(
"BADA Family:",
Expand All @@ -62,8 +74,12 @@
AC.BADAVersion,
"| ICAO:",
ICAO,
" |WTC:",
"| WTC:",
WTC,
"| VMO =",
VMO,
"| MMO =",
MMO,
"| MTOW =",
MTOW,
)
212 changes: 81 additions & 131 deletions src/pyBADA/aircraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,55 @@ def checkArgument(argument, **kwargs):
raise TypeError("Missing " + argument + " argument")


class BadaFamily(object):
"""This class sets the token for the respected BADA Family."""
class Bada(object):
"""This class implements the mechanisms applicable across all BADA families."""

def __init__(self, BADA3=False, BADA4=False, BADAH=False, BADAE=False):
self.BADA3 = BADA3
self.BADA4 = BADA4
self.BADAH = BADAH
self.BADAE = BADAE
def __init__(self):
pass

@staticmethod
def getBADAParameters(df, acName, parameters):
"""
Retrieves specified parameters for a given aircraft name from a DataFrame.
:param df: DataFrame containing BADA aircraft data.
:param acName: Name of the aircraft or list of aircraft names to search for.
:param parameters: List of column names (or a single column name) to retrieve.
:type df: pd.DataFrame
:type acName: list or str
:type parameters: list or str
:returns: A DataFrame containing the specified parameters for the given aircraft.
:rtype: pd.DataFrame
:raises ValueError: If any of the specified columns or aircraft names are not found.
"""

class Airplane(object):
"""This is a generic airplane class based on a three-degrees-of-freedom point mass model (where all the forces
are applied at the center of gravity).
# Ensure parameters is a list
if isinstance(parameters, str):
parameters = [parameters]

.. note::this generic class only implements basic aircraft dynamics
calculations, aircraft performance and optimisation can be obtained
from its inherited classes
# Ensure acName is a list
if isinstance(acName, str):
acName = [acName]

"""
# Ensure all requested parameters exist in the DataFrame
missing_cols = [col for col in parameters if col not in df.columns]
if missing_cols:
raise ValueError(
f"The following parameters are not in the DataFrame columns: {missing_cols}"
)

__metaclass__ = abc.ABCMeta
# Filter rows where 'acName' matches any of the specified aircraft names
filtered_df = df[df["acName"].isin(acName)]

def __init__(self):
pass
# Check if any rows were found
if filtered_df.empty:
raise ValueError(f"No entries found for aircraft(s): {acName}.")
else:
# Select the required columns
result_df = filtered_df[["acName"] + parameters].reset_index(
drop=True
)
return result_df

@staticmethod
def loadFactor(fi):
Expand Down Expand Up @@ -81,6 +106,21 @@ def bankAngle(rateOfTurn, v):
BA = atan((ROT * v) / const.g)
return conv.rad2deg(BA)

@staticmethod
def rateOfTurn(v, nz=1.0):
"""
Computes the rate of turn based on true airspeed (TAS) and load factor.
:param v: True airspeed (TAS) in meters per second (m/s).
:param nz: Load factor (default is 1.0), dimensionless.
:type v: float
:type nz: float
:returns: Rate of turn in degrees per second (deg/s).
:rtype: float
"""

return degrees((const.g / v) * sqrt(nz * nz - 1))

@staticmethod
def rateOfTurn_bankAngle(TAS, bankAngle):
"""
Expand All @@ -98,21 +138,6 @@ def rateOfTurn_bankAngle(TAS, bankAngle):

return degrees(ROT)

@staticmethod
def rateOfTurn(v, nz=1.0):
"""
Computes the rate of turn based on true airspeed (TAS) and load factor.
:param v: True airspeed (TAS) in meters per second (m/s).
:param nz: Load factor (default is 1.0), dimensionless.
:type v: float
:type nz: float
:returns: Rate of turn in degrees per second (deg/s).
:rtype: float
"""

return degrees((const.g / v) * sqrt(nz * nz - 1))

@staticmethod
def turnRadius(v, nz=1.0):
"""
Expand Down Expand Up @@ -160,6 +185,30 @@ def GS(tas, gamma, Ws):

return tas * cos(radians(gamma)) + Ws


class BadaFamily(object):
"""This class sets the token for the respected BADA Family."""

def __init__(self, BADA3=False, BADA4=False, BADAH=False, BADAE=False):
self.BADA3 = BADA3
self.BADA4 = BADA4
self.BADAH = BADAH
self.BADAE = BADAE


class Airplane(object):
"""This is a generic airplane class based on a three-degrees-of-freedom point mass model (where all the forces
are applied at the center of gravity).
.. note::this generic class only implements basic aircraft dynamics
calculations, aircraft performance and optimisation can be obtained
from its inherited classes
"""

def __init__(self):
pass

@staticmethod
def esf(**kwargs):
"""
Expand Down Expand Up @@ -278,90 +327,9 @@ class Helicopter(object):
"""

__metaclass__ = abc.ABCMeta

def __init__(self):
pass

@staticmethod
def loadFactor(fi):
"""
Computes the load factor from a given bank angle.
The load factor is calculated based on the cosine of the bank angle,
which is expressed in degrees. A small rounding operation is applied
to avoid precision issues with small decimal places.
:param fi: Bank angle in degrees.
:type fi: float
:returns: The load factor (dimensionless).
:rtype: float
"""

return 1 / round(cos(radians(fi)), 10)

@staticmethod
def rateOfTurn(v, nz=1.0):
"""
Computes the rate of turn based on true airspeed (TAS) and load factor.
:param v: True airspeed (TAS) in meters per second (m/s).
:param nz: Load factor (default is 1.0), dimensionless.
:type v: float
:type nz: float
:returns: Rate of turn in degrees per second (deg/s).
:rtype: float
"""

return degrees((const.g / v) * sqrt(nz * nz - 1))

@staticmethod
def rateOfTurn_bankAngle(TAS, bankAngle):
"""
Computes the rate of turn based on true airspeed (TAS) and bank angle.
:param TAS: True airspeed (TAS) in meters per second (m/s).
:param bankAngle: Bank angle in degrees.
:type TAS: float
:type bankAngle: float
:returns: Rate of turn in degrees per second (deg/s).
:rtype: float
"""

ROT = tan(radians(bankAngle)) * const.g / TAS

return degrees(ROT)

@staticmethod
def turnRadius(v, nz=1.0):
"""
Computes the turn radius based on true airspeed (TAS) and load factor.
:param v: True airspeed (TAS) in meters per second (m/s).
:param nz: Load factor (default is 1.0), dimensionless.
:type v: float
:type nz: float
:returns: Turn radius in meters.
:rtype: float
"""

return (v * v / const.g) * (1 / sqrt(nz * nz - 1))

@staticmethod
def turnRadius_bankAngle(v, ba):
"""
Computes the turn radius based on true airspeed (TAS) and bank angle.
:param v: True airspeed (TAS) in meters per second (m/s).
:param ba: Bank angle in degrees.
:type v: float
:type ba: float
:returns: Turn radius in meters.
:rtype: float
"""

return (v * v / const.g) * (1 / tan(conv.deg2rad(ba)))

@staticmethod
def esf(**kwargs):
"""
Expand Down Expand Up @@ -429,21 +397,3 @@ def esf(**kwargs):
ESF = float("Nan")

return ESF

@staticmethod
def bankAngle(rateOfTurn, v):
"""
Computes the bank angle based on true airspeed (TAS) and rate of turn.
:param v: True airspeed (TAS) in meters per second (m/s).
:param rateOfTurn: Rate of turn in degrees per second (deg/s).
:type v: float
:type rateOfTurn: float
:returns: Bank angle in degrees.
:rtype: float
"""

ROT = conv.deg2rad(rateOfTurn)

BA = atan((ROT * v) / const.g)
return conv.rad2deg(BA)
Loading

0 comments on commit 44a62ea

Please sign in to comment.