Hide keyboard shortcuts

Hot-keys 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

1#!/usr/bin/env python 

2# encoding: utf-8 

3""" 

4*The recipe for creating master-bias frames * 

5 

6:Author: 

7 David Young & Marco Landoni 

8 

9:Date Created: 

10 January 22, 2020 

11""" 

12################# GLOBAL IMPORTS #################### 

13from builtins import object 

14import sys 

15import os 

16os.environ['TERM'] = 'vt100' 

17from fundamentals import tools 

18from soxspipe.commonutils import set_of_files 

19from ._base_recipe_ import _base_recipe_ 

20import numpy as np 

21from astropy.nddata import CCDData 

22from astropy import units as u 

23import ccdproc 

24from soxspipe.commonutils import keyword_lookup 

25 

26 

27class soxs_mbias(_base_recipe_): 

28 """ 

29 *The* `soxs_mbias` *recipe is used to generate a master-bias frame from a set of input raw bias frames. The recipe is used only for the UV-VIS arm as NIR frames have bias (and dark current) removed by subtracting an off-frame of equal expsoure length.* 

30 

31 **Key Arguments** 

32 

33 - ``log`` -- logger 

34 - ``settings`` -- the settings dictionary 

35 - ``inputFrames`` -- input fits frames. Can be a directory, a set-of-files (SOF) file or a list of fits frame paths. 

36 - ``verbose`` -- verbose. True or False. Default *False* 

37 

38 **Usage** 

39 

40 ```python 

41 from soxspipe.recipes import soxs_mbias 

42 mbiasFrame = soxs_mbias( 

43 log=log, 

44 settings=settings, 

45 inputFrames=fileList 

46 ).produce_product() 

47 ``` 

48 

49 --- 

50 

51 ```eval_rst 

52 .. todo:: 

53 - add a tutorial about ``soxs_mbias`` to documentation 

54 ``` 

55 """ 

56 # Initialisation 

57 

58 def __init__( 

59 self, 

60 log, 

61 settings=False, 

62 inputFrames=[], 

63 verbose=False 

64 

65 ): 

66 # INHERIT INITIALISATION FROM _base_recipe_ 

67 super(soxs_mbias, self).__init__(log=log, settings=settings) 

68 self.log = log 

69 log.debug("instansiating a new 'soxs_mbias' object") 

70 self.settings = settings 

71 self.inputFrames = inputFrames 

72 self.verbose = verbose 

73 self.recipeName = "soxs-mbias" 

74 self.recipeSettings = settings[self.recipeName] 

75 # xt-self-arg-tmpx 

76 

77 # INITIAL ACTIONS 

78 # CONVERT INPUT FILES TO A CCDPROC IMAGE COLLECTION (inputFrames > 

79 # imagefilecollection) 

80 sof = set_of_files( 

81 log=self.log, 

82 settings=self.settings, 

83 inputFrames=self.inputFrames 

84 ) 

85 self.inputFrames, self.supplementaryInput = sof.get() 

86 

87 # VERIFY THE FRAMES ARE THE ONES EXPECTED BY SOXS_MBIAS - NO MORE, NO LESS. 

88 # PRINT SUMMARY OF FILES. 

89 print("# VERIFYING INPUT FRAMES") 

90 self.verify_input_frames() 

91 sys.stdout.write("\x1b[1A\x1b[2K") 

92 print("# VERIFYING INPUT FRAMES - ALL GOOD") 

93 

94 print("\n# RAW INPUT BIAS FRAMES - SUMMARY") 

95 # SORT IMAGE COLLECTION 

96 self.inputFrames.sort(['mjd-obs']) 

97 print(self.inputFrames.summary, "\n") 

98 

99 # PREPARE THE FRAMES - CONVERT TO ELECTRONS, ADD UNCERTAINTY AND MASK 

100 # EXTENSIONS 

101 self.inputFrames = self.prepare_frames( 

102 save=self.settings["save-intermediate-products"]) 

103 

104 return None 

105 

106 def verify_input_frames( 

107 self): 

108 """*verify the input frame match those required by the soxs_mbias recipe* 

109 

110 If the fits files conform to required input for the recipe everything will pass silently, otherwise an exception shall be raised. 

111 """ 

112 self.log.debug('starting the ``verify_input_frames`` method') 

113 

114 kw = self.kw 

115 

116 # BASIC VERIFICATION COMMON TO ALL RECIPES 

117 self._verify_input_frames_basics() 

118 

119 imageTypes = self.inputFrames.values( 

120 keyword=kw("DPR_TYPE").lower(), unique=True) 

121 # MIXED INPUT IMAGE TYPES ARE BAD 

122 if len(imageTypes) > 1: 

123 imageTypes = " and ".join(imageTypes) 

124 print(self.inputFrames.summary) 

125 raise TypeError( 

126 "Input frames are a mix of %(imageTypes)s" % locals()) 

127 # NON-BIAS INPUT IMAGE TYPES ARE BAD 

128 elif imageTypes[0] != 'BIAS': 

129 print(self.inputFrames.summary) 

130 raise TypeError( 

131 "Input frames not BIAS frames" % locals()) 

132 

133 self.imageType = imageTypes[0] 

134 

135 self.log.debug('completed the ``verify_input_frames`` method') 

136 return None 

137 

138 def produce_product( 

139 self): 

140 """*generate a master bias frame* 

141 

142 **Return:** 

143 - ``productPath`` -- the path to the master bias frame 

144 """ 

145 self.log.debug('starting the ``produce_product`` method') 

146 

147 arm = self.arm 

148 kw = self.kw 

149 dp = self.detectorParams 

150 

151 combined_bias_mean = self.clip_and_stack( 

152 frames=self.inputFrames, recipe="soxs_mbias") 

153 

154 # INSPECTING THE THE UNCERTAINTY MAPS 

155 # print("individual frame data") 

156 # for a in ccds: 

157 # print(a.data[0][0]) 

158 # print("\ncombined frame data") 

159 # print(combined_bias_mean.data[0][0]) 

160 # print("individual frame error") 

161 # for a in ccds: 

162 # print(a.uncertainty[0][0]) 

163 # print("combined frame error") 

164 # print(combined_bias_mean.uncertainty[0][0]) 

165 

166 # combined_bias_mean.data = combined_bias_mean.data.astype('float32') 

167 # combined_bias_mean.uncertainty = combined_bias_mean.uncertainty.astype( 

168 # 'float32') 

169 

170 # WRITE TO DISK 

171 productPath = self._write( 

172 frame=combined_bias_mean, 

173 filedir=self.intermediateRootPath, 

174 filename=False, 

175 overwrite=True 

176 ) 

177 

178 self.clean_up() 

179 

180 self.log.debug('completed the ``produce_product`` method') 

181 return productPath 

182 

183 # use the tab-trigger below for new method 

184 # xt-class-method