""" Standards Loader - Loads NR standards from JSON config Network Rail Standards - Professional Edition """ import json from pathlib import Path from typing import Dict, Any, Optional _STANDARDS_CACHE = None def load_standards() -> Dict: """Load standards from JSON file with caching""" global _STANDARDS_CACHE if _STANDARDS_CACHE is not None: return _STANDARDS_CACHE # Try to load from JSON file standards_path = Path(__file__).parent.parent / "knowledge" / "standards" / "nr_standards.json" if standards_path.exists(): try: with open(standards_path, "r", encoding="utf-8") as f: _STANDARDS_CACHE = json.load(f) return _STANDARDS_CACHE except (OSError, json.JSONDecodeError): _STANDARDS_CACHE = None # Fallback to built-in standards _STANDARDS_CACHE = { "NR/L2/TRK/2500": { "version": "2024", "limits": { "disp_lift": {"value": 30.0, "unit": "mm", "rule": "Section 4.2.1"}, "disp_slue": {"value": 50.0, "unit": "mm", "rule": "Section 4.2.2"}, "disp_cant_diff": {"value": 15.0, "unit": "mm", "rule": "Section 4.2.3"}, "disp_sixfoot_diff": {"value": 10.0, "unit": "mm", "rule": "Section 4.2.4"}, "disp_por": {"value": 20.0, "unit": "mm", "rule": "Section 4.2.5"}, "geom_min_radius": {"value": 150.0, "unit": "m", "rule": "Section 5.1.1"}, "geom_max_cant": {"value": 150.0, "unit": "mm", "rule": "Section 5.2.1"}, "geom_max_cant_def": {"value": 110.0, "unit": "mm", "rule": "Section 5.2.2"}, "geom_max_roc": {"value": 55.0, "unit": "mm/m", "rule": "Section 5.2.3"}, "geom_min_transition": {"value": 20.0, "unit": "m", "rule": "Section 5.3.1"}, "geom_min_curve_length": {"value": 50.0, "unit": "m", "rule": "Section 5.3.2"}, "geom_max_gradient": {"value": 2.5, "unit": "%", "rule": "Section 6.1.1"}, "geom_min_vertical_length": {"value": 50.0, "unit": "m", "rule": "Section 6.1.2"}, "geom_min_k": {"value": 30.0, "unit": "m", "rule": "Section 6.1.3"} }, "track_categories": { "A": {"twist_3m": 2.0, "twist_5m": 2.5, "cant_def": 110.0, "cant_excess": 90.0}, "B": {"twist_3m": 2.0, "twist_5m": 2.5, "cant_def": 110.0, "cant_excess": 90.0}, "C": {"twist_3m": 2.5, "twist_5m": 2.86, "cant_def": 100.0, "cant_excess": 70.0}, "D": {"twist_3m": 2.86, "twist_5m": 3.33, "cant_def": 90.0, "cant_excess": 50.0} } } } return _STANDARDS_CACHE def get_limit(parameter: str, default: Optional[float] = None) -> Optional[float]: """Numeric limits always come from core.engineering.NR_LIMITS.""" from core.engineering import NR_LIMITS prefix_map = { "lift": "disp_lift", "slue": "disp_slue", "cant_diff": "disp_cant_diff", "sixfoot_diff": "disp_sixfoot_diff", "por": "disp_por", "min_radius": "geom_min_radius", "max_cant": "geom_max_cant", "max_cant_def": "geom_max_cant_def", "max_roc": "geom_max_roc", "min_transition": "geom_min_transition", "min_curve_length": "geom_min_curve_length", "max_gradient": "geom_max_gradient", "min_vertical_length": "geom_min_vertical_length", "min_k": "geom_min_k", } key = prefix_map.get(parameter, parameter) if key in NR_LIMITS: return NR_LIMITS[key] return default def get_rule_reference(parameter: str) -> str: """Get the rule reference for a parameter""" standards = load_standards() limits = standards.get("NR/L2/TRK/2500", {}).get("limits", {}) prefix_map = { "lift": "disp_lift", "slue": "disp_slue", "cant_diff": "disp_cant_diff", "min_radius": "geom_min_radius", "max_cant": "geom_max_cant", "max_cant_def": "geom_max_cant_def" } if parameter in prefix_map: key = prefix_map[parameter] if key in limits: return limits[key].get("rule", "Unknown") return "Unknown" def get_track_category_limits(category: str = "C") -> Dict: """Get limits for a specific track category""" standards = load_standards() categories = standards.get("NR/L2/TRK/2500", {}).get("track_categories", {}) return categories.get(category, {}) def reload_standards(): """Force reload standards from JSON""" global _STANDARDS_CACHE _STANDARDS_CACHE = None return load_standards()