1 from .fitsChan
import FitsChan, CardType
2 from .fitsChan
import *
5 def _calc_card_pos(self, index):
6 """Convert a python index into a FitsChan position. 11 The FitsChan to index. 13 0-based index into header. If negative, counts from end. 18 Raised if the index exceeds the size of the FitsChan. If the index 19 equals the size of the FitsChan (noting that in 0-based indexing the 20 final card is one less than the size) then this refers to the end of 26 index = nCards + index
27 elif abs(index) > nCards:
29 raise IndexError(f
"Index {index} exceeds size of FitsChan ({nCards})")
35 def _get_current_card_value(self):
36 """Retrieve the value of the current card along with the keyword name. 41 The name of the current card. 43 The value in the correct Python type. 46 typeLut = {CardType.INT: self.getFitsI,
47 CardType.FLOAT: self.getFitsF,
48 CardType.STRING: self.getFitsS,
49 CardType.COMPLEXF: self.getFitsCF,
50 CardType.LOGICAL: self.getFitsL
54 ctype = self.getCardType()
57 name = self.getCardName()
64 if ctype == CardType.UNDEF:
66 elif ctype
in typeLut:
67 found = typeLut[ctype](
"")
71 raise RuntimeError(f
"Unexpectedly failed to find card '{name}'")
72 elif ctype == CardType.COMMENT:
73 value = self.getCardComm()
75 raise RuntimeError(f
"Type, {ctype} of FITS card '{name}' not supported")
84 FitsChan.__len__ = length
88 """The FitsChan is its own iterator, incrementing the card position on 91 The position of the iterator is handled internally in the FitsChan and 92 is moved to the start of the FitsChan by this call. 93 Whilst iterating do not change the internal card position. 95 The iterator will return 80-character header cards. 101 FitsChan.__iter__ = iter
105 """Return each 80-character header card until we run out of cards. 107 card = self.findFits(
"%f",
True)
113 FitsChan.__next__ = next
117 """A FitsChan string representation is a FITS header with newlines 118 after each 80-character header card. 120 return "\n".join(c
for c
in self)
123 FitsChan.__str__ = to_string
127 """Returns True if either the supplied name is present in the FitsChan 128 or the supplied integer is acceptable to the FitsChan. 130 if isinstance(name, int):
132 if name >= 0
and name < self.nCard:
134 elif isinstance(name, str):
135 currentCard = self.getCard()
138 result = self.findFits(name,
False)
140 self.setCard(currentCard)
147 FitsChan.__contains__ = contains
151 """Return a value associated with the supplied name. 155 name : `str` or `int` 156 If the FitsChan is being accessed by integer index the returned value 157 will be the corresponding 80-character card. Index values are 0-based. 158 A negative index counts from the end of the FitsChan. 159 If the FitsChan is being accessed by string the returned value will 160 be the scalar value associated with the first card that matches the 165 value : `str`, `int`, `float`, `bool`, or `None` 166 The complete 80-character header card if an integer index is supplied, 167 else the first matching value of the named header. 172 Raised if an integer index is provided and the index is out of range. 174 Raised if a string is provided and that string is not present in 175 the FitsChan. Also raised if the supplied name is neither an integer 178 Raised if there is some problem accessing the value in the FitsChan. 182 currentCard = self.getCard()
184 if isinstance(name, int):
186 newpos = _calc_card_pos(self, name)
189 result = self.findFits(
"%f",
False)
191 self.setCard(currentCard)
193 raise IndexError(f
"No FITS card at index {name}")
196 elif isinstance(name, str):
203 result = self.findFits(name,
False)
205 raise KeyError(f
"{name}'")
207 this_name, value = _get_current_card_value(self)
208 if this_name != name:
209 raise RuntimeError(f
"Internal inconsistency in get: {this_name} != {name}")
213 self.setCard(currentCard)
217 raise KeyError(f
"Supplied key, '{name}' of unsupported type")
220 FitsChan.__getitem__ = getitem
228 name : `str` or `int` 229 If the name is an integer index this corresponds to a position within 230 the FitsChan. Index values are 0-based. A negative index counts 231 from the end of the FitsChan. If the index matches the number of 232 cards (e.g. the return value of `len()`) the new value will be 233 appended to the end of the FitsChan. 234 If the name is an empty string or `None`, the value will be inserted 235 at the current card position as a comment card. 236 If the name is a string corresponding to a header card that is already 237 present in the FitsChan, the new value will overwrite the existing 238 value leaving the header name and any comment unchanged. 239 Any other cards matching that name later in the header will 240 be removed. If there is no header with that name, a new card will 241 be inserted at the end of the FitsChan. 242 value : `str`, `int`, `float`, `bool`, `None` 243 The new value to be inserted. If an integer index is given it must be 244 a complete FITS header card. The string will be padded to 80 250 Raised if the supplied integer index is out of range. 252 Raised if the supplied name is neither a string or an integer. 254 Raised if an integer index is given but the supplied value is not 258 if isinstance(name, int):
260 newpos = _calc_card_pos(self, name)
267 self.putFits(value,
True)
273 self.setFitsCM(value,
False)
276 if not isinstance(name, str):
277 raise KeyError(f
"Supplied key, '{name}' of unsupported type")
280 currentCard = self.getCard()
287 self.findFits(name,
False)
292 self.setFitsU(name, overwrite=
True)
293 elif isinstance(value, int):
294 self.setFitsI(name, value, overwrite=
True)
295 elif isinstance(value, float):
296 self.setFitsF(name, value, overwrite=
True)
297 elif isinstance(value, bool):
298 self.setFitsL(name, value, overwrite=
True)
301 self.setFitsS(name, str(value), overwrite=
True)
305 found = self.findFits(name,
False)
311 self.setCard(currentCard)
314 FitsChan.__setitem__ = setitem
318 """Delete an item from the FitsChan either by index (0-based) or by name. 322 name : `str` or `int` 323 If the name is an integer index the card at that position will be 324 removed. The index is zero-based. A negative index counts from the 326 If the name is a string all cards matching that name will be removed. 331 Raised if the supplied index is out of range. 333 Raised if the supplied name is not found in the FitsChan. 335 if isinstance(name, int):
337 newpos = _calc_card_pos(self, name)
338 if newpos <= 0
or newpos > self.nCard:
340 raise IndexError(f
"No FITS card at index {name}")
345 if not isinstance(name, str):
346 raise KeyError(f
"Supplied key, '{name}' of unsupported type")
352 found = self.findFits(name,
False)
359 raise KeyError(f
"No FITS card named {name}")
362 FitsChan.__delitem__ = delitem
366 """Iterate over each card, returning the keyword and value in a tuple. 371 The key associated with the card. Can be an empty string for some 372 comment styles. The same key name can be returned multiple times 373 and be associated with different values. 374 value : `str`, `int`, `bool`, `float` 379 The position of the iterator is internal to the FitsChan. Do not 380 change the card position when iterating. 384 thisCard = self.getCard()
386 while thisCard <= nCards:
387 name, value = _get_current_card_value(self)
389 self.setCard(thisCard + 1)
390 thisCard = self.getCard()
393 FitsChan.items = items
Angle abs(Angle const &a)
def setitem(self, name, value)