Coverage for soxspipe/commonutils/sof_util.py: 91%

# Coverage for soxspipe/commonutils/sof_util.py : 91%

## 47 statements 43 run 4 missing 0 excluded

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*Tools for working with 'set-of-files' (sof) files*

5

6:Author:

7 David Young

8

9:Date Created:

10 January 22, 2020

11"""

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

13 from builtins import object

14 import sys

15 import os

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

17 from fundamentals import tools

18 from astropy.io import fits

19

20

21 class sof_util(object):

22 """

23 *The worker class for the sof module*

24

25 **Key Arguments:**

26 - ``log`` -- logger

27 - ``settings`` -- the settings dictionary

28

29 **Usage:**

30

31 To setup your logger, settings and database connections, please use the ``fundamentals`` package (`see tutorial here <http://fundamentals.readthedocs.io/en/latest/#tutorial>`_).

32

33 To initiate a sof object, use the following:

34

35 ```python

36 usage code

37 ```

38

39 ---

40

41 ```eval_rst

42 .. todo::

43

44 - add usage info

45 - create a sublime snippet for usage

46 - create cl-util for this class

47 - add a tutorial about ``sof`` to documentation

48 - create a blog post about what ``sof`` does

49 ```

50 """

51 # Initialisation

52

53 def __init__(

54 self,

55 log,

56 settings=False,

57

58 ):

59 self.log = log

60 log.debug("instansiating a new 'sof' object")

61 self.settings = settings

62 # xt-self-arg-tmpx

63

64 # Initial Actions

65

66 return None

67

68 def generate_sof_file_from_directory(

69 self,

70 directory,

71 sofPath):

72 """*generate an sof file from a directory of FITS frames*

73

74 **Key Arguments:**

75 - ``directory`` -- the path to the directory to containing the FITS files.

76 - ``sofPath`` -- the path to generate the sof file to

77

78 **Return:**

79 - ``sofPath`` -- the path to the sof file

80

81 **Usage:**

82

83 ```python

84 from soxspipe.commonutils import sof_util

85 sof = sof_util(

86 log=log,

87 settings=settings

88 )

89 sofFile = sof.generate_sof_file_from_directory(

90 directory="path/to/directory", sofPath="/path/to/myFile.sof")

91 ```

92

93 ---

94

95 ```eval_rst

96 .. todo::

97

98 - write a command-line tool for this method

99 ```

100

101 """

102 self.log.debug(

103 'starting the ``generate_sof_file_from_directory`` method')

104

105 # MAKE RELATIVE HOME PATH ABSOLUTE

106 from os.path import expanduser

107 home = expanduser("~")

108 if directory[0] == "~":

109 directory = directory.replace("~", home)

110 if sofPath[0] == "~":

111 sofPath = sofPath.replace("~", home)

112

113 content = ""

114 for d in sorted(os.listdir(directory)):

115 if os.path.isfile(os.path.join(directory, d)) and (os.path.splitext(d)[-1].lower() == ".fits"):

116 fitsPath = os.path.abspath(os.path.join(directory, d))

117 # OPEN FITS FILE AT HDULIST - HDU (HEADER DATA UNIT) CONTAINS A HEADER AND A DATA ARRAY (IMAGE) OR

118 # TABLE.

119 with fits.open(fitsPath) as hdul:

120 # READ HEADER INTO MEMORY

121 hdr = hdul[0].header

122 # PRINT FULL FITS HEADER TO STDOUT

123 # print(repr(hdr).strip())

124 dpr_type = hdr['HIERARCH ESO DPR TYPE'].strip()

125 # CHECK ARM

126 arm = hdr['HIERARCH ESO SEQ ARM']

127 # CHECK BINNING

128 if 'CDELT1' in hdr:

129 xbin = str(int(hdr['CDELT1']))

130 ybin = str(int(hdr['CDELT2']))

131 catagory = dpr_type + "_" + arm.strip()

132 if 'CDELT1' in hdr:

133 catagory += "_" + \

134 xbin.strip() + "x" + ybin.strip()

135

136 content += "%(fitsPath)s %(catagory)s\n" % locals()

137

138 # Recursively create missing directories

139 moduleDirectory = os.path.dirname(sofPath)

140 if not os.path.exists(moduleDirectory):

141 os.makedirs(moduleDirectory)

142

143 # WRITE TO FILE

144 with open(sofPath, 'w') as myFile:

145 myFile.write(content)

146

147 self.log.debug(

148 'completed the ``generate_sof_file_from_directory`` method')

149 return sofPath

150

151 def validate_sof_frames(

152 self):

153 """*given various filtering criteria, set by the recipe, validate that the frames in the sof file are compatible with the recipe*

154

155 **Key Arguments:**

156 - ``sofPath`` -- path to the SOF file

157

158 **Return:**

159 - ``valid`` -- are the frames in the sof file valid or not. True or False.

160

161 **Usage:**

162

163 ```python

164 usage code

165 ```

166

167 ---

168

169 ```eval_rst

170 .. todo::

171

172 - add usage info

173 - create a sublime snippet for usage

174 - write a command-line tool for this method

175 - update package tutorial with command-line tool info if needed

176 ```

177 """

178 self.log.debug('starting the ``validate_sof_frames`` method')

179

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

181 return valid

182

183 # use the tab-trigger below for new method

184 # xt-class-method