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

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*
6:Author:
7 David Young
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 astropy.io import fits
21class sof_util(object):
22 """
23 *The worker class for the sof module*
25 **Key Arguments:**
26 - ``log`` -- logger
27 - ``settings`` -- the settings dictionary
29 **Usage:**
31 To setup your logger, settings and database connections, please use the ``fundamentals`` package (`see tutorial here <http://fundamentals.readthedocs.io/en/latest/#tutorial>`_).
33 To initiate a sof object, use the following:
35 ```python
36 usage code
37 ```
39 ---
41 ```eval_rst
42 .. todo::
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
53 def __init__(
54 self,
55 log,
56 settings=False,
58 ):
59 self.log = log
60 log.debug("instansiating a new 'sof' object")
61 self.settings = settings
62 # xt-self-arg-tmpx
64 # Initial Actions
66 return None
68 def generate_sof_file_from_directory(
69 self,
70 directory,
71 sofPath):
72 """*generate an sof file from a directory of FITS frames*
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
78 **Return:**
79 - ``sofPath`` -- the path to the sof file
81 **Usage:**
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 ```
93 ---
95 ```eval_rst
96 .. todo::
98 - write a command-line tool for this method
99 ```
101 """
102 self.log.debug(
103 'starting the ``generate_sof_file_from_directory`` method')
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)
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()
136 content += "%(fitsPath)s %(catagory)s\n" % locals()
138 # Recursively create missing directories
139 moduleDirectory = os.path.dirname(sofPath)
140 if not os.path.exists(moduleDirectory):
141 os.makedirs(moduleDirectory)
143 # WRITE TO FILE
144 with open(sofPath, 'w') as myFile:
145 myFile.write(content)
147 self.log.debug(
148 'completed the ``generate_sof_file_from_directory`` method')
149 return sofPath
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*
155 **Key Arguments:**
156 - ``sofPath`` -- path to the SOF file
158 **Return:**
159 - ``valid`` -- are the frames in the sof file valid or not. True or False.
161 **Usage:**
163 ```python
164 usage code
165 ```
167 ---
169 ```eval_rst
170 .. todo::
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')
180 self.log.debug('completed the ``validate_sof_frames`` method')
181 return valid
183 # use the tab-trigger below for new method
184 # xt-class-method