# ISC License
#
# Copyright (c) 2026, PIC4SeR & AVS Lab, Politecnico di Torino & Argotec S.R.L., University of Colorado Boulder
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""Test that the MSIS temporal inputs are evaluated at the epoch of the extrapolated spacecraft state."""
import datetime
import pytest
from Basilisk.architecture import messaging
from Basilisk.simulation import msisAtmosphere
from Basilisk.utilities import SimulationBaseClass, macros
EPOCH = datetime.datetime(2026, 1, 1) # UTC
R_SC = [6778137.0, 0.0, 0.0] # [m] spacecraft position on the equator, planet-fixed frame is the inertial frame
STEP = 3600.0 # [s] module update period, long enough for the local solar time to change visibly
def _density(epoch, extrapolate, stopSeconds, rewriteTimes=()):
"""Return the MSIS density of the last module update.
The spacecraft is at rest, so the extrapolation moves it nowhere and only the epoch of the temporal inputs differs.
Args:
epoch (datetime.datetime): UTC epoch given to the module.
extrapolate (bool): whether the extrapolation to the step midpoint is enabled.
stopSeconds (float): [s] simulation stop time.
rewriteTimes (tuple): [s] times at which the spacecraft state message is rewritten after the update, emulating
a spacecraft that runs after the module in the task with the same period.
"""
scSim = SimulationBaseClass.SimBaseClass()
proc = scSim.CreateNewProcess("p")
proc.addTask(scSim.CreateNewTask("t", macros.sec2nano(STEP)))
atmo = msisAtmosphere.MsisAtmosphere()
atmo.setExtrapolateScStateToStepMidpoint(extrapolate)
epochMsg = messaging.EpochMsg().write(messaging.EpochMsgPayload(
year=epoch.year, month=epoch.month, day=epoch.day, hours=epoch.hour, minutes=epoch.minute,
seconds=epoch.second))
atmo.epochInMsg.subscribeTo(epochMsg)
swMsgs = []
for c in range(23):
value = 150.0 if c >= 21 else 15.0 # f107 [sfu] and ap [-]
swMsgs.append(messaging.SwDataMsg().write(messaging.SwDataMsgPayload(dataValue=value)))
atmo.swDataInMsgs[c].subscribeTo(swMsgs[-1])
planetPayload = messaging.SpicePlanetStateMsgPayload()
planetPayload.J20002Pfix = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
planetMsg = messaging.SpicePlanetStateMsg().write(planetPayload)
atmo.planetPosInMsg.subscribeTo(planetMsg)
payload = messaging.SCStatesMsgPayload()
payload.r_BN_N = list(R_SC)
scMsg = messaging.SCStatesMsg().write(payload, 0)
atmo.addSpacecraftToModel(scMsg)
scSim.AddModelToTask("t", atmo)
recorder = atmo.envOutMsgs[0].recorder()
scSim.AddModelToTask("t", recorder)
scSim.InitializeSimulation()
for writeTime in rewriteTimes:
scSim.ConfigureStopTime(macros.sec2nano(writeTime))
scSim.ExecuteSimulation()
scMsg.write(payload, macros.sec2nano(writeTime))
scSim.ConfigureStopTime(macros.sec2nano(stopSeconds))
scSim.ExecuteSimulation()
return recorder.neutralDensity[-1]