Coverage for middlelayer / ngect / system.py: 100.00%

27 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-16 06:04 +0000

1# -*- coding: utf-8 -*- 

2# Copyright 2023 Associated Universities, Inc. 

3# 

4# This file is part of ngVLA Exposure Calculator Tool (ngECT). 

5# 

6# ngECT is free software: you can redistribute it and/or modify 

7# it under the terms of the GNU General Public License as published by 

8# the Free Software Foundation, either version 3 of the License, or 

9# any later version. 

10# 

11# ngECT is distributed in the hope that it will be useful, 

12# but WITHOUT ANY WARRANTY; without even the implied warranty of 

13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14# GNU General Public License for more details. 

15# 

16# You should have received a copy of the GNU General Public License 

17# along with ngECT. If not, see <https://www.gnu.org/licenses/>. 

18 

19"""System Specifications""" 

20import importlib 

21import json 

22from pathlib import Path 

23 

24 

25def _ect_resources_path() -> Path: 

26 with importlib.resources.path(__name__, "resources") as path: 

27 return path 

28 

29 

30def read_subarray_data(version: str | None = None) -> dict: 

31 """Read subarray configuration for a specific revision. 

32 

33 Parameters 

34 ---------- 

35 version : str, optional 

36 Subarray configuration revision (e.g., ``"RevA"``). If omitted, the 

37 repository's default revision is used. 

38 

39 Returns 

40 ------- 

41 dict 

42 Parsed JSON contents describing subarray parameters keyed by subarray 

43 name. 

44 """ 

45 version = version if version else default_subarray_revision() 

46 

47 filepath = _ect_resources_path() / "data" / "subarray_data" / f"{version}.json" 

48 

49 with filepath.open() as f: 

50 data = json.load(f) 

51 

52 return data 

53 

54 

55def read_receiver_data(date: str | None = None) -> dict: 

56 """Read receiver specification data. 

57 

58 Parameters 

59 ---------- 

60 date : str, optional 

61 Receiver revision date in ``YYYY.MM.DD`` format. If omitted or not 

62 found, the latest available revision is used. 

63 

64 Returns 

65 ------- 

66 dict 

67 Parsed JSON mapping of receiver band names to their specification 

68 tables (e.g., frequency grids, system temperatures, efficiencies). 

69 """ 

70 

71 # make sure we got a valid date, or default to the latest 

72 revisions = available_receiver_revisions() 

73 if not date or date not in revisions: 

74 date = revisions[-1] 

75 

76 # read the data 

77 with (receiver_data_path() / f"{date}.json").open() as f: 

78 data = json.load(f) 

79 

80 return data 

81 

82 

83def available_subarray_revisions() -> list[str]: 

84 """List available subarray configuration revisions. 

85 

86 Returns 

87 ------- 

88 list[str] 

89 Sorted list of revision names discovered under 

90 ``resources/data/subarray_data`` matching ``Rev*``. 

91 """ 

92 return [p.stem for p in (_ect_resources_path() / "data" / "subarray_data").glob("Rev*")] 

93 

94 

95def receiver_data_path() -> Path: 

96 """Return the filesystem path to receiver data JSON files. 

97 

98 Returns 

99 ------- 

100 pathlib.Path 

101 Directory that contains dated ``.json`` files with receiver 

102 specifications. 

103 """ 

104 return _ect_resources_path() / "data" / "receiver_data" 

105 

106 

107def available_receiver_revisions() -> list[str]: 

108 """List available receiver data revisions. 

109 

110 Returns 

111 ------- 

112 list[str] 

113 Sorted list of available receiver JSON basenames (dates) under 

114 :func:`receiver_data_path`. 

115 """ 

116 return sorted([p.stem for p in receiver_data_path().glob("*.json")]) 

117 

118 

119def default_subarray_revision() -> str: 

120 """Return the default subarray configuration revision. 

121 

122 Returns 

123 ------- 

124 str 

125 The name of the default revision pointed to by the ``default`` 

126 symlink under ``resources/data/subarray_data``. 

127 """ 

128 return (_ect_resources_path() / "data" / "subarray_data" / "default").readlink().stem