Coverage for spectractor/logbook.py: 16%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

61 statements  

1from spectractor import parameters 

2from spectractor.config import set_logger 

3 

4import matplotlib.pyplot as plt 

5import pandas as pd 

6import os 

7import numpy as np 

8 

9 

10class LogBook: 

11 """Class to load_image and analyse observation logbook csv files.""" 

12 

13 def __init__(self, logbook="ctiofulllogbook_jun2017_v5.csv"): 

14 """Load and initialise the logbook 

15 

16 Parameters 

17 ---------- 

18 logbook: str 

19 Path to the logbook. Must be a CSV file. 

20 

21 Examples 

22 ---------- 

23 

24 >>> logbook = LogBook('./ctiofulllogbook_jun2017_v5.csv') 

25 >>> assert logbook.df is not None 

26 >>> print(logbook.logbook) 

27 ./ctiofulllogbook_jun2017_v5.csv 

28 >>> print(logbook.df['disperser'][:2]) 

29 0 Ron400 

30 1 Ron400 

31 Name: disperser, dtype: object 

32 >>> logbook = LogBook('./log.csv') 

33 

34 """ 

35 self.my_logger = set_logger(self.__class__.__name__) 

36 self.logbook = logbook 

37 if not os.path.isfile(logbook): 

38 self.my_logger.error('CSV logbook file {} not found.'.format(logbook)) 

39 return 

40 # self.csvfile = open(self.logbook, 'rU', encoding='latin-1') 

41 # self.reader = csv.DictReader(self.csvfile, delimiter=';', dialect=csv.excel_tab) 

42 self.df = pd.read_csv(self.logbook, sep=";", decimal=",", encoding='latin-1', header='infer') 

43 self.df['date'] = pd.to_datetime(self.df.date) 

44 

45 def search_for_image(self, filename): 

46 """ 

47 Look for an image file name in the logbook and load_image properties: 

48 

49 - Obj-posXpix and Obj-posYpix: the [x0,y0] guessed pixel position in the image 

50 - Dx and Dy: the x and y windows in pixel to search for the target; set XWINDOW and YWINDOW variables in parameters.py 

51 - object: the name of the target 

52 

53 

54 Parameters 

55 ---------- 

56 filename: str 

57 the fits image file name (not the path, only the file name.) 

58 

59 Returns 

60 ------- 

61 disperser_label: str 

62 the name of the disperser 

63 target: str 

64 the name of the target 

65 xpos: int 

66 the x position of the target (in pixel) 

67 ypos: int 

68 the y position of the target (in pixel) 

69 

70 Examples 

71 -------- 

72 

73 >>> logbook = LogBook('./ctiofulllogbook_jun2017_v5.csv') 

74 >>> disperser_label, target, xpos, ypos = logbook.search_for_image("unknown_file.fits") 

75 >>> print(disperser_label, target, xpos, ypos) 

76 None None None None 

77 >>> disperser_label, target, xpos, ypos = logbook.search_for_image("reduc_20170605_028.fits") 

78 >>> print(disperser_label, target, xpos, ypos) 

79 HoloPhAg PNG321.0+3.9 814 585 

80 >>> disperser_label, target, xpos, ypos = logbook.search_for_image("reduc_20170608_119.fits") 

81 >>> print(disperser_label, target, xpos, ypos) 

82 None HD205905 None None 

83 >>> disperser_label, target, xpos, ypos = logbook.search_for_image("reduc_20170630_001.fits") 

84 >>> print(disperser_label, target, xpos, ypos) 

85 None bias None None 

86 

87 """ 

88 disperser_label = None 

89 target = None 

90 xpos = None 

91 ypos = None 

92 skip = False 

93 try: 

94 row = self.df.loc[(self.df['file'] == filename)].iloc[0] 

95 target = row['object'] 

96 if row['object'] == 'bias' or row['object'] == 'flat' or row['object'] == 'zero': 

97 self.my_logger.error( 

98 'Fits file %s in logbook %s has flag %s. Skip file.' % (filename, self.logbook, target)) 

99 skip = True 

100 if row['skip'] == 'skip': 

101 self.my_logger.error('Fits file %s in logbook has flag "skip". Skip file.' % filename) 

102 skip = True 

103 if np.isnan(row['Obj-posXpix']): 

104 self.my_logger.error( 

105 'Fits file %s in logbook %s has no target x position. Skip file.' % (filename, self.logbook)) 

106 skip = True 

107 if np.isnan(row['Obj-posYpix']): 

108 self.my_logger.error( 

109 'Fits file %s in logbook %s has no target y position. Skip file.' % (filename, self.logbook)) 

110 skip = True 

111 if not np.isnan(row['Dx']): 

112 parameters.XWINDOW = int(row['Dx']) 

113 parameters.XWINDOW_ROT = int(row['Dx']) 

114 if not np.isnan(row['Dy']): 

115 parameters.YWINDOW = int(row['Dy']) 

116 parameters.YWINDOW_ROT = int(row['Dy']) 

117 if not skip: 

118 xpos = int(row['Obj-posXpix']) 

119 ypos = int(row['Obj-posYpix']) 

120 disperser_label = row['disperser'] 

121 except IndexError: 

122 if target is None and skip is False: 

123 self.my_logger.error('Fits file %s not found in logbook %s.' % (filename, self.logbook)) 

124 return disperser_label, target, xpos, ypos 

125 

126 def plot_columns_vs_date(self, column_names): 

127 """Plot of the column property with respect to the dates. 

128 

129 Parameters 

130 ---------- 

131 column_names: list, str 

132 List of column names to plot versus time from the log book. 

133 

134 Examples 

135 -------- 

136 >>> logbook = LogBook('./ctiofulllogbook_jun2017_v5.csv') 

137 >>> logbook.plot_columns_vs_date(['seeing']) 

138 >>> logbook.plot_columns_vs_date(['P', 'T']) 

139 """ 

140 if isinstance(column_names, str): 

141 column_names = [column_names] 

142 self.df.plot(x='date', y=column_names) 

143 if parameters.DISPLAY: 

144 plt.show() 

145 if parameters.PdfPages: 

146 parameters.PdfPages.savefig() 

147 

148 

149if __name__ == "__main__": 149 ↛ 150line 149 didn't jump to line 150, because the condition on line 149 was never true

150 import doctest 

151 

152 doctest.testmod()