Coverage for soxspipe/commonutils/keyword_lookup.py : 93%

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*Given a keyword token and instrument name return the exact FITS Header keyword*
6:Author:
7 David Young & Marco Landoni
9:Date Created:
10 February 26, 2020
11"""
12################# GLOBAL IMPORTS ####################
13from builtins import object
14import sys
15import os
16os.environ['TERM'] = 'vt100'
17from fundamentals import tools
20class keyword_lookup(object):
21 """
22 *The worker class for the keyword_lookup module*
24 **Key Arguments:**
25 - ``log`` -- logger
26 - ``settings`` -- the settings dictionary
28 **Usage**
30 To initalise the keyword lookup object in your code add the following:
32 ```python
33 from soxspipe.commonutils import keyword_lookup
34 kw = keyword_lookup(
35 log=log,
36 settings=settings
37 ).get
38 ```
40 After this it's possible to either look up a single keyword using it's alias:
42 ```python
43 kw("DET_NDITSKIP")
44 > "ESO DET NDITSKIP"
45 ```
47 or return a list of keywords:
49 ```python
50 kw(["PROV", "DET_NDITSKIP"])
51 > ['PROV', 'ESO DET NDITSKIP']
52 ```
54 For those keywords that require an index it's possible to also pass the index to the `kw` function:
56 ```python
57 kw("PROV", 9)
58 > 'PROV09'
59 ```
61 If a tag is not in the list of FITS Header keyword aliases in the configuration file a `LookupError` will be raised.
62 """
63 # Initialisation
65 def __init__(
66 self,
67 log,
68 settings=False,
70 ):
71 self.log = log
72 log.debug("instansiating a new 'keyword_lookup' object")
73 self.settings = settings
74 # xt-self-arg-tmpx
76 # SELECT THE INSTRUMENT AND READ THE KEYWORD DICTIONARY IN RESOURCES
77 # FOLDER
78 if "instrument" in settings:
79 self.instrument = settings["instrument"]
80 else:
81 self.instrument = "soxs"
82 self.kwDict = self._select_dictionary()
84 return None
86 def get(self,
87 tag,
88 index=False):
89 """
90 *given a tag, and optional keyword index, return the FITS Header keyword for the selected instrument*
92 **Key Arguments:**
93 - ``tag`` -- the keyword tag as set in the yaml keyword dictionary (e.g. 'SDP_KEYWORD_TMID' returns 'TMID'). Can be string or list of sttings.
94 - ``index`` -- add an index to the keyword if not False (e.g. tag='PROV', index=3 returns 'PROV03') Default *False*
96 **Return:**
97 - ``keywords`` -- the FITS Header keywords. Can be string or list of sttings depending on format of tag argument
99 **Usage**
101 See docstring for the class
102 """
103 self.log.debug('starting the ``get`` method')
105 # CONVERT STRING TO LIST OF ONE ITEM
106 single = False
107 if not isinstance(tag, list):
108 single = True
109 tag = [tag]
111 # STRINGIFY INDEX
112 if index:
113 index = "%(index)0.2d" % locals()
114 else:
115 index = ""
117 # LOOKUP KEYWORDS
118 keywords = []
119 for t in tag:
120 if t not in self.kwDict:
121 raise LookupError(
122 "%(tag)s is not in the list of known FITS Header keyword aliases" % locals())
123 keywords.append(self.kwDict[t] + index)
125 # RETURNING A SINGLE KEYWORD?
126 if single:
127 keywords = keywords[0]
129 self.log.debug('completed the ``get`` method')
130 return keywords
132 def _select_dictionary(
133 self):
134 """*select the keyword dictionary based on the instrument passed via the settings*
136 **Return:**
137 - ``kwDict`` -- the python dictionary of keywords (key = tag, value = fits keyword)
139 **Usage**
141 ```python
142 from soxspipe.commonutils import keyword_lookup
143 this = keyword_lookup(
144 log=log,
145 settings=settings
146 )
147 kwDict = this._select_dictionary()
148 ```
149 """
150 self.log.debug('starting the ``_select_dictionary`` method')
152 # GENERATE PATH TO YAML DICTIONARY
153 yamlFilePath = os.path.dirname(os.path.dirname(
154 __file__)) + "/resources/" + self.instrument + "_keywords.yaml"
156 # YAML CONTENT TO DICTIONARY
157 import yaml
158 with open(yamlFilePath, 'r') as stream:
159 kwDict = yaml.load(stream)
161 self.log.debug('completed the ``_select_dictionary`` method')
162 return kwDict