Coverage for soxspipe/recipes/soxs_mdark.py : 89%

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 to generate a master dark frame*
6:Author:
7 David Young & Marco Landoni
9:Date Created:
10 January 27, 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
22import ccdproc
23from soxspipe.commonutils import keyword_lookup
26class soxs_mdark(_base_recipe_):
27 """
28 *The soxs_mdark recipe*
30 **Key Arguments**
32 - ``log`` -- logger
33 - ``settings`` -- the settings dictionary
34 - ``inputFrames`` -- input fits frames. Can be a directory, a set-of-files (SOF) file or a list of fits frame paths.
35 - ``verbose`` -- verbose. True or False. Default *False*
37 **Usage**
39 ```python
40 from soxspipe.recipes import soxs_mdark
41 mdarkFrame = soxs_mdark(
42 log=log,
43 settings=settings,
44 inputFrames=fileList
45 )..produce_product()
46 ```
48 ---
50 ```eval_rst
51 .. todo::
53 - add a tutorial about ``soxs_mdark`` to documentation
54 ```
55 """
57 def __init__(
58 self,
59 log,
60 settings=False,
61 inputFrames=[],
62 verbose=False
64 ):
65 # INHERIT INITIALISATION FROM _base_recipe_
66 super(soxs_mdark, self).__init__(log=log, settings=settings)
67 self.log = log
68 log.debug("instansiating a new 'soxs_mdark' object")
69 self.settings = settings
70 self.inputFrames = inputFrames
71 self.verbose = verbose
72 self.recipeName = "soxs-mdark"
73 self.recipeSettings = settings[self.recipeName]
74 # xt-self-arg-tmpx
76 # INITIAL ACTIONS
77 # CONVERT INPUT FILES TO A CCDPROC IMAGE COLLECTION (inputFrames >
78 # imagefilecollection)
79 sof = set_of_files(
80 log=self.log,
81 settings=self.settings,
82 inputFrames=self.inputFrames
83 )
84 self.inputFrames, self.supplementaryInput = sof.get()
86 # VERIFY THE FRAMES ARE THE ONES EXPECTED BY SOXS_MDARK - NO MORE, NO LESS.
87 # PRINT SUMMARY OF FILES.
88 print("# VERIFYING INPUT FRAMES")
89 self.verify_input_frames()
90 sys.stdout.write("\x1b[1A\x1b[2K")
91 print("# VERIFYING INPUT FRAMES - ALL GOOD")
93 # SORT IMAGE COLLECTION
94 self.inputFrames.sort(['mjd-obs'])
95 if self.verbose:
96 print("# RAW INPUT FRAMES - SUMMARY")
97 print(self.inputFrames.summary, "\n")
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"])
104 return None
106 def verify_input_frames(
107 self):
108 """*verify input frame match those required by the soxs_mdark recipe*
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')
114 kw = self.kw
116 # BASIC VERIFICATION COMMON TO ALL RECIPES
117 self._verify_input_frames_basics()
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] != 'DARK':
129 print(self.inputFrames.summary)
130 raise TypeError(
131 "Input frames not DARK frames" % locals())
133 exptimes = self.inputFrames.values(
134 keyword=kw("EXPTIME").lower(), unique=True)
135 # MIXED INPUT IMAGE TYPES ARE BAD
136 if len(exptimes) > 1:
137 exptimes = [str(e) for e in exptimes]
138 exptimes = " and ".join(exptimes)
139 print(self.inputFrames.summary)
140 raise TypeError(
141 "Input frames have differing exposure-times %(exptimes)s" % locals())
143 self.imageType = imageTypes[0]
144 self.log.debug('completed the ``verify_input_frames`` method')
145 return None
147 def produce_product(
148 self):
149 """*generate a master dark frame*
151 **Return:**
152 - ``productPath`` -- the path to master dark frame
153 """
154 self.log.debug('starting the ``produce_product`` method')
156 arm = self.arm
157 kw = self.kw
158 dp = self.detectorParams
160 combined_bias_mean = self.clip_and_stack(
161 frames=self.inputFrames, recipe="soxs_mdark")
163 # WRITE TO DISK
164 productPath = self._write(
165 frame=combined_bias_mean,
166 filedir=self.intermediateRootPath,
167 filename=False,
168 overwrite=True
169 )
170 self.clean_up()
172 self.log.debug('completed the ``produce_product`` method')
173 return productPath
175 # use the tab-trigger below for new method
176 # xt-class-method