Coverage for src/susi/atlantes/sss.py: 98%

47 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2025-06-13 14:15 +0000

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3""" 

4Module to access the second solar spectrum atlas 

5provided in electronic form by IRSOL as a compilation 

6by Stenflo (2014), based on the atlas of Gandorfer 

7(2000, 2002, 2005). 

8Per default located in `data/atlases/SSS-Atlas` 

9 

10@author: hoelken 

11""" 

12import os 

13import numpy as np 

14 

15from .atlas import Atlas 

16from ..base import ROOT_DIR, IllegalStateException 

17 

18 

19class SSSAtlas(Atlas): 

20 """ 

21 SSS Atlas data class 

22 """ 

23 

24 #: ATLAS_LOCATION 

25 ATLAS_LOCATION = os.path.join(ROOT_DIR, '..', 'data', 'atlantes', 'SSS-Atlas') 

26 #: ATLAS FILE NAME 

27 ATLAS_FILE = 'SSSatlas.txt' 

28 #: Number of rows to ignore ad the beginning (inclusive) 

29 HEADER_ROWS = 4 

30 

31 def __init__(self, start: float, stop: float, location: str = ATLAS_LOCATION): 

32 # list of file pathes 

33 self.file = os.path.join(location, SSSAtlas.ATLAS_FILE) 

34 # Containers for the loaded atlas 

35 #: Stokes Q/I 

36 self.stokes_q = [] 

37 #: Stokes Q/I smoothed 

38 self.stokes_q_smoothed = [] 

39 #: Stokes Q/I empirical continuum 

40 self.continuum = [] 

41 # Build class 

42 super().__init__(start, stop) 

43 

44 def load(self): 

45 """Read the content of the atlas to memory""" 

46 if len(self.wl) > 0: 

47 return self 

48 

49 if not os.path.exists(self.file): 

50 file = os.path.relpath(self.file) 

51 readme = os.path.abspath(os.path.join(SSSAtlas.ATLAS_LOCATION, '..', 'Readme.md')) 

52 raise IllegalStateException(f'{file} not found. Atlas not available.\nSee {readme} for details.') 

53 

54 self.__load_data() 

55 self.wl = np.array(self.wl) 

56 self.stokes_q = np.array(self.stokes_q) 

57 self.stokes_q_smoothed = np.array(self.stokes_q_smoothed) 

58 self.continuum = np.array(self.continuum) 

59 self.intensity = np.array(self.intensity) 

60 return self 

61 

62 def __load_data(self) -> None: 

63 with open(self.file) as f: 

64 line_nr = -1 

65 for line in f: 

66 line_nr += 1 

67 if line_nr < SSSAtlas.HEADER_ROWS: 

68 continue 

69 

70 content = line.split() 

71 wl = float(content[0]) 

72 if wl > self.stop: 

73 return 

74 

75 if wl < self.start: 

76 continue 

77 

78 self.wl.append(wl) 

79 self.intensity.append(float(content[1])) 

80 self.stokes_q.append(float(content[2])) 

81 self.stokes_q_smoothed.append(float(content[3])) 

82 self.continuum.append(float(content[4]))