SXXXXXXX_ScenarioSimulator/scenario_simulator/utils/config_manager.py
2025-09-30 15:10:50 +02:00

82 lines
2.4 KiB
Python

"""
Handles loading and saving of radar configuration profiles and target scenarios
from JSON files.
"""
import json
import os
import numpy as np
# Use files in the user's home directory to persist data
# across different project locations or versions.
PROFILES_FILE = os.path.join(os.path.expanduser("~"), ".radar_simulator_profiles.json")
SCENARIOS_FILE = os.path.join(os.path.expanduser("~"), ".radar_simulator_scenarios.json")
# --- Profile Management (Radar Configuration) ---
def load_profiles():
"""
Loads radar configuration profiles from the JSON file.
If the file doesn't exist or is corrupted, returns an empty dictionary.
"""
if not os.path.exists(PROFILES_FILE):
return {}
try:
with open(PROFILES_FILE, 'r') as f:
profiles = json.load(f)
if isinstance(profiles, dict):
return profiles
return {}
except (IOError, json.JSONDecodeError):
return {}
def save_profiles(profiles):
"""
Saves the given profiles dictionary to the JSON file.
Returns True on success, False on failure.
"""
try:
with open(PROFILES_FILE, 'w') as f:
json.dump(profiles, f, indent=4)
return True
except IOError:
return False
# --- Scenario Management (Target Lists) - NUOVE FUNZIONI ---
def load_scenarios():
"""
Loads target scenarios (lists of targets) from the JSON file.
"""
if not os.path.exists(SCENARIOS_FILE):
return {}
try:
with open(SCENARIOS_FILE, 'r') as f:
scenarios = json.load(f)
# Basic validation to ensure it's a dictionary
if isinstance(scenarios, dict):
return scenarios
return {}
except (IOError, json.JSONDecodeError):
return {}
def save_scenarios(scenarios):
"""
Saves the given scenarios dictionary to the JSON file.
The data structure is:
{
"ScenarioName1": [
{"initial_position": [x,y,z], "velocity": [vx,vy,vz], "rcs": rcs_val},
...
],
...
}
"""
try:
with open(SCENARIOS_FILE, 'w') as f:
# np.ndarray is not JSON serializable, so convert to lists if needed
# This is handled in the GUI layer before calling this function
json.dump(scenarios, f, indent=4)
return True
except (IOError, TypeError):
return False