added more features get time-specific values

This commit is contained in:
dario 2023-12-30 17:25:32 +01:00
parent 4bd50375d7
commit 4def93041e
3 changed files with 40 additions and 16 deletions

View File

@ -80,7 +80,7 @@ class Dataset(Advanceable):
int: The computed index. int: The computed index.
""" """
idx = (self.__df['time'] - t).abs().idxmin() idx = (self.__df['time'] - t).abs().idxmin()
idx = idx if self.__df['time'].loc[idx] < t else idx - 1 idx = idx if self.__df['time'].loc[idx] <= t else idx - 1
return idx return idx
@ -138,8 +138,15 @@ class Dataset(Advanceable):
float: The last time step in the dataset. float: The last time step in the dataset.
""" """
return max(self.__df['time']) return max(self.__df['time'])
def get_start_time(self) -> float:
"""
Returns:
float: Returns the starting time of the simulation.
"""
return self.fetch_start_value('time')
def fetch_init_value(self, name: str) -> float: def fetch_start_value(self, name: str) -> float:
"""Get the initial value for a given attribute from the dataframe. """Get the initial value for a given attribute from the dataframe.
Args: Args:
@ -148,7 +155,7 @@ class Dataset(Advanceable):
Returns: Returns:
float: Returns the requested value. float: Returns the requested value.
""" """
return self.__df[name].iloc[0] return self.__df.at[0, name]
def fetch_init_values(self, names: List[str]) -> ArrayLike: def fetch_init_values(self, names: List[str]) -> ArrayLike:
"""Get the initial value for given attributes from the dataframe. """Get the initial value for given attributes from the dataframe.
@ -159,7 +166,7 @@ class Dataset(Advanceable):
Returns: Returns:
np.array: Returns a numpy array containing the requested values in the same order as in the input list. np.array: Returns a numpy array containing the requested values in the same order as in the input list.
""" """
return np.asarray([self.fetch_init_value(name) for name in names]) return np.asarray([self.fetch_start_value(name) for name in names])
def fetch_value(self, name: str, t: float | None = None) -> float: def fetch_value(self, name: str, t: float | None = None) -> float:
"""Get a specific value from the dataframe. """Get a specific value from the dataframe.
@ -171,7 +178,7 @@ class Dataset(Advanceable):
Returns: Returns:
float: Returns the requested value. float: Returns the requested value.
""" """
idx = idx if t is None else self._get_closest_idx(t) idx = self.__idx if t is None else self._get_closest_idx(t)
if self.__interpolation == 'linear': if self.__interpolation == 'linear':
t_min = self.__df.at[idx, 'time'] t_min = self.__df.at[idx, 'time']
@ -211,7 +218,7 @@ class Dataset(Advanceable):
rots = self.fetch_values(['pitch_l', 'yaw_l', 'roll_l'], t) rots = self.fetch_values(['pitch_l', 'yaw_l', 'roll_l'], t)
pitch_l, yaw_l, roll_l = rots[0], rots[1], rots[2] pitch_l, yaw_l, roll_l = rots[0], rots[1], rots[2]
return self.T1(roll_l) @ self.T2(pitch_l - math.pi/2) @ self.T1(-yaw_l) return T1(roll_l) @ T2(pitch_l - math.pi/2) @ T1(-yaw_l)
def global_to_local(self, t: float | None = None) -> ArrayLike: def global_to_local(self, t: float | None = None) -> ArrayLike:
""" """
@ -223,11 +230,11 @@ class Dataset(Advanceable):
""" """
decl = self.fetch_value('declination', t) decl = self.fetch_value('declination', t)
long = self.fetch_value('longitude', t) long = self.fetch_value('longitude', t)
t0 = self.__df['time'].iloc[0] t0 = self.get_start_time()
omega_E = (2*math.pi) / (24*60*60) omega_E = (2*math.pi) / (24*60*60)
return self.T2(-decl) @ self.T3(long + omega_E * t0) return T2(-decl) @ T3(long + omega_E * t0)
def global_to_launch_rail(self, t: float | None = None) -> ArrayLike: def global_to_launch_rail(self, t: float | None = None) -> ArrayLike:
""" """
@ -238,10 +245,10 @@ class Dataset(Advanceable):
Returns: Returns:
ArrayLike: The current transformation matrix from global to launch rail coords. ArrayLike: The current transformation matrix from global to launch rail coords.
""" """
init_long = self.__df['longitude'].iloc[0] init_long = self.fetch_start_value('longitude')
init_lat = self.__df['latitude'].iloc[0] init_lat = self.fetch_start_value('latitude')
return self.T2(-math.pi/2 - init_lat) @ self.T3(init_long) return T2(-math.pi/2 - init_lat) @ T3(init_long)
def local_to_launch_rail(self, t: float | None = None) -> ArrayLike: def local_to_launch_rail(self, t: float | None = None) -> ArrayLike:
""" """
@ -348,6 +355,16 @@ class Dataset(Advanceable):
return vel return vel
def get_altitude(self, t: float | None = None) -> float:
"""
Args:
t (float | None, optional): Allows specification of a different time instead of the current time. None for current time.
Returns:
float: Returns the altitude in meter at the specified time.
"""
return self.fetch_value('altitude', t)
def get_speed_of_sound(self, t: float | None = None) -> float: def get_speed_of_sound(self, t: float | None = None) -> float:
""" """
Args: Args:

View File

@ -24,8 +24,15 @@ class Observer:
def _log(self, name: AnyStr, value: Any): def _log(self, name: AnyStr, value: Any):
self._logger.write(name, value, self._get_name()) self._logger.write(name, value, self._get_name())
def __call__(self) -> ArrayLike: def get_start_value(self) -> ArrayLike:
data = self._dataset.fetch_values(self.__attrs) """
Returns:
ArrayLike: Returns the values of the observed attributes at the start of the simulation.
"""
return self(t=self._dataset.get_start_time())
def __call__(self, t: float | None = None) -> ArrayLike:
data = self._dataset.fetch_values(self.__attrs, t)
for attrib, value in zip(self.__attrs, data): for attrib, value in zip(self.__attrs, data):
self._log(attrib, value) self._log(attrib, value)

View File

@ -37,14 +37,14 @@ class Sensor:
raise NotImplementedError() raise NotImplementedError()
@abstractmethod @abstractmethod
def _get_data(self, t: float = None) -> ArrayLike | float: def _get_data(self) -> ArrayLike | float:
raise NotImplementedError() raise NotImplementedError()
def get_init_data() -> ArrayLike: def get_init_data() -> ArrayLike:
pass pass
def __call__(self, t: float = None) -> ArrayLike | float: def __call__(self) -> ArrayLike | float:
out = self._get_data(t = t) out = self._get_data()
out = self._sensor_specific_effects(out) out = self._sensor_specific_effects(out)
for transform in self._transforms: for transform in self._transforms: