<s> from __future__ import division <EOL> import datetime <EOL> import os <EOL> import sys <EOL> import warnings <EOL> import numpy as np <EOL> from . . extern . six import string_types , add_metaclass <EOL> from . . extern . six . moves import range <EOL> import pyfits <EOL> from . . file import _File <EOL> from . . header import Header <EOL> from . . py3compat import ignored , getargspec <EOL> from . . util import ( first , lazyproperty , _is_int , _is_pseudo_unsigned , <EOL> _unsigned_zero , _pad_length , itersubclasses , <EOL> decode_ascii , deprecated , _get_array_mmap ) <EOL> from . . verify import _Verify , _ErrList <EOL> class _Delayed ( object ) : <EOL> pass <EOL> DELAYED = _Delayed ( ) <EOL> BITPIX2DTYPE = { <NUM_LIT:8> : '<STR_LIT>' , <NUM_LIT:16> : '<STR_LIT>' , <NUM_LIT:32> : '<STR_LIT>' , <NUM_LIT:64> : '<STR_LIT>' , <EOL> - <NUM_LIT:32> : '<STR_LIT>' , - <NUM_LIT:64> : '<STR_LIT>' } <EOL> """<STR_LIT>""" <EOL> DTYPE2BITPIX = { '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:16> , '<STR_LIT>' : <NUM_LIT:16> , '<STR_LIT>' : <NUM_LIT:32> , <EOL> '<STR_LIT>' : <NUM_LIT:32> , '<STR_LIT>' : <NUM_LIT:64> , '<STR_LIT>' : <NUM_LIT:64> , '<STR_LIT>' : - <NUM_LIT:32> , <EOL> '<STR_LIT>' : - <NUM_LIT:64> } <EOL> """<STR_LIT>""" <EOL> class InvalidHDUException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def _hdu_class_from_header ( cls , header ) : <EOL> """<STR_LIT>""" <EOL> klass = cls <EOL> if header : <EOL> for c in reversed ( list ( itersubclasses ( cls ) ) ) : <EOL> try : <EOL> if not ( c . __module__ . startswith ( '<STR_LIT>' ) or <EOL> c in cls . _hdu_registry ) : <EOL> continue <EOL> if c . match_header ( header ) : <EOL> klass = c <EOL> break <EOL> except NotImplementedError : <EOL> continue <EOL> except Exception as exc : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % exc ) <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> klass = _CorruptedHDU <EOL> del exc <EOL> break <EOL> return klass <EOL> class _BaseHDUMeta ( type ) : <EOL> def __init__ ( cls , name , bases , members ) : <EOL> if '<STR_LIT:data>' in members : <EOL> data_prop = members [ '<STR_LIT:data>' ] <EOL> if ( isinstance ( data_prop , ( lazyproperty , property ) ) and <EOL> data_prop . _fdel is None ) : <EOL> def data ( self ) : <EOL> if self . _file is not None and self . _data_loaded : <EOL> data_refcount = sys . getrefcount ( self . data ) <EOL> del self . __dict__ [ '<STR_LIT:data>' ] <EOL> if data_refcount == <NUM_LIT:2> : <EOL> self . _file . _maybe_close_mmap ( ) <EOL> setattr ( cls , '<STR_LIT:data>' , data_prop . deleter ( data ) ) <EOL> @ add_metaclass ( _BaseHDUMeta ) <EOL> class _BaseHDU ( object ) : <EOL> """<STR_LIT>""" <EOL> _hdu_registry = set ( ) <EOL> _standard = True <EOL> _padding_byte = '<STR_LIT:\x00>' <EOL> _default_name = '<STR_LIT>' <EOL> def __new__ ( cls , data = None , header = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> klass = _hdu_class_from_header ( cls , header ) <EOL> return super ( _BaseHDU , cls ) . __new__ ( klass ) <EOL> def __init__ ( self , data = None , header = None , * args , ** kwargs ) : <EOL> if header is None : <EOL> header = Header ( ) <EOL> self . _header = header <EOL> self . _file = None <EOL> self . _buffer = None <EOL> self . _header_offset = None <EOL> self . _data_offset = None <EOL> self . _data_size = None <EOL> self . _data_replaced = False <EOL> self . _data_needs_rescale = False <EOL> self . _new = True <EOL> self . _output_checksum = False <EOL> if '<STR_LIT>' in self . _header and '<STR_LIT>' not in self . _header : <EOL> self . _output_checksum = '<STR_LIT>' <EOL> elif '<STR_LIT>' in self . _header : <EOL> self . _output_checksum = True <EOL> @ property <EOL> def header ( self ) : <EOL> return self . _header <EOL> @ header . setter <EOL> def header ( self , value ) : <EOL> self . _header = value <EOL> @ property <EOL> def name ( self ) : <EOL> return str ( self . _header . get ( '<STR_LIT>' , self . _default_name ) ) <EOL> @ name . setter <EOL> def name ( self , value ) : <EOL> if not isinstance ( value , string_types ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not pyfits . EXTENSION_NAME_CASE_SENSITIVE : <EOL> value = value . upper ( ) <EOL> if '<STR_LIT>' in self . _header : <EOL> self . _header [ '<STR_LIT>' ] = value <EOL> else : <EOL> self . _header [ '<STR_LIT>' ] = ( value , '<STR_LIT>' ) <EOL> @ property <EOL> def ver ( self ) : <EOL> return self . _header . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> @ ver . setter <EOL> def ver ( self , value ) : <EOL> if not _is_int ( value ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in self . _header : <EOL> self . _header [ '<STR_LIT>' ] = value <EOL> else : <EOL> self . _header [ '<STR_LIT>' ] = ( value , '<STR_LIT>' ) <EOL> @ property <EOL> def level ( self ) : <EOL> return self . _header . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> @ level . setter <EOL> def level ( self , value ) : <EOL> if not _is_int ( value ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in self . _header : <EOL> self . _header [ '<STR_LIT>' ] = value <EOL> else : <EOL> self . _header [ '<STR_LIT>' ] = ( value , '<STR_LIT>' ) <EOL> @ property <EOL> def is_image ( self ) : <EOL> return ( <EOL> self . name == '<STR_LIT>' or <EOL> ( '<STR_LIT>' in self . _header and <EOL> ( self . _header [ '<STR_LIT>' ] == '<STR_LIT>' or <EOL> ( self . _header [ '<STR_LIT>' ] == '<STR_LIT>' and <EOL> '<STR_LIT>' in self . _header and self . _header [ '<STR_LIT>' ] == True ) ) ) ) <EOL> @ property <EOL> def _data_loaded ( self ) : <EOL> return ( '<STR_LIT:data>' in self . __dict__ and self . data is not DELAYED ) <EOL> @ property <EOL> def _has_data ( self ) : <EOL> return self . _data_loaded and self . data is not None <EOL> @ property <EOL> @ deprecated ( '<STR_LIT>' , alternative = '<STR_LIT>' , <EOL> pending = True ) <EOL> def _hdrLoc ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _header_offset <EOL> @ _hdrLoc . setter <EOL> @ deprecated ( '<STR_LIT>' , alternative = '<STR_LIT>' , <EOL> pending = True ) <EOL> def _hdrLoc ( self , value ) : <EOL> self . _header_offset = value <EOL> @ property <EOL> @ deprecated ( '<STR_LIT>' , alternative = '<STR_LIT>' , <EOL> pending = True ) <EOL> def _datLoc ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _data_offset <EOL> @ _datLoc . setter <EOL> @ deprecated ( '<STR_LIT>' , alternative = '<STR_LIT>' , <EOL> pending = True ) <EOL> def _datLoc ( self , value ) : <EOL> self . _data_offset = value <EOL> @ property <EOL> @ deprecated ( '<STR_LIT>' , alternative = '<STR_LIT>' , <EOL> pending = True ) <EOL> def _datSpan ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _data_size <EOL> @ _datSpan . setter <EOL> @ deprecated ( '<STR_LIT>' , alternative = '<STR_LIT>' , <EOL> pending = True ) <EOL> def _datSpan ( self , value ) : <EOL> self . _data_size = value <EOL> @ classmethod <EOL> def register_hdu ( cls , hducls ) : <EOL> cls . _hdu_registry . add ( hducls ) <EOL> @ classmethod <EOL> def unregister_hdu ( cls , hducls ) : <EOL> if hducls in cls . _hdu_registry : <EOL> cls . _hdu_registry . remove ( hducls ) <EOL> @ classmethod <EOL> def match_header ( cls , header ) : <EOL> raise NotImplementedError <EOL> @ classmethod <EOL> def fromstring ( cls , data , checksum = False , ignore_missing_end = False , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return cls . _readfrom_internal ( data , checksum = checksum , <EOL> ignore_missing_end = ignore_missing_end , <EOL> ** kwargs ) <EOL> @ classmethod <EOL> def readfrom ( cls , fileobj , checksum = False , ignore_missing_end = False , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( fileobj , _File ) : <EOL> fileobj = _File ( fileobj ) <EOL> hdu = cls . _readfrom_internal ( fileobj , checksum = checksum , <EOL> ignore_missing_end = ignore_missing_end , <EOL> ** kwargs ) <EOL> fileobj . seek ( hdu . _data_offset + hdu . _data_size , os . SEEK_SET ) <EOL> return hdu <EOL> def writeto ( self , name , output_verify = '<STR_LIT>' , clobber = False , <EOL> checksum = False ) : <EOL> """<STR_LIT>""" <EOL> from pyfits . hdu . hdulist import HDUList <EOL> hdulist = HDUList ( [ self ] ) <EOL> hdulist . writeto ( name , output_verify , clobber = clobber , <EOL> checksum = checksum ) <EOL> @ classmethod <EOL> def _readfrom_internal ( cls , data , header = None , checksum = False , <EOL> ignore_missing_end = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> hdu_buffer = None <EOL> hdu_fileobj = None <EOL> header_offset = <NUM_LIT:0> <EOL> if isinstance ( data , _File ) : <EOL> from_file = True <EOL> if header is None : <EOL> header_offset = data . tell ( ) <EOL> header = Header . fromfile ( data , endcard = not ignore_missing_end ) <EOL> hdu_fileobj = data <EOL> data_offset = data . tell ( ) <EOL> else : <EOL> from_file = False <EOL> try : <EOL> np . ndarray ( ( ) , dtype = '<STR_LIT>' , buffer = data ) <EOL> except TypeError : <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % data ) <EOL> if header is None : <EOL> def block_iter ( nbytes ) : <EOL> idx = <NUM_LIT:0> <EOL> while idx < len ( data ) : <EOL> yield data [ idx : idx + nbytes ] <EOL> idx += nbytes <EOL> header_str , header = Header . _from_blocks ( <EOL> block_iter , True , '<STR_LIT>' , not ignore_missing_end , True ) <EOL> if len ( data ) > len ( header_str ) : <EOL> hdu_buffer = data <EOL> elif data : <EOL> hdu_buffer = data <EOL> header_offset = <NUM_LIT:0> <EOL> data_offset = len ( header_str ) <EOL> cls = _hdu_class_from_header ( cls , header ) <EOL> args , varargs , varkwargs , defaults = getargspec ( cls . __init__ ) <EOL> new_kwargs = kwargs . copy ( ) <EOL> if not varkwargs : <EOL> for key in kwargs : <EOL> if key not in args : <EOL> del new_kwargs [ key ] <EOL> hdu = cls ( data = DELAYED , header = header , ** new_kwargs ) <EOL> hdu . _file = hdu_fileobj <EOL> hdu . _buffer = hdu_buffer <EOL> hdu . _header_offset = header_offset <EOL> hdu . _data_offset = data_offset <EOL> size = hdu . size <EOL> hdu . _data_size = size + _pad_length ( size ) <EOL> if checksum and checksum != '<STR_LIT>' and isinstance ( hdu , _ValidHDU ) : <EOL> hdu . _verify_checksum_datasum ( checksum ) <EOL> return hdu <EOL> def _get_raw_data ( self , shape , code , offset ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( shape , int ) : <EOL> shape = ( shape , ) <EOL> if self . _buffer : <EOL> return np . ndarray ( shape , dtype = code , buffer = self . _buffer , <EOL> offset = offset ) <EOL> elif self . _file : <EOL> return self . _file . readarray ( offset = offset , dtype = code , shape = shape ) <EOL> else : <EOL> return None <EOL> def _prewriteto ( self , checksum = False , inplace = False ) : <EOL> self . _update_uint_scale_keywords ( ) <EOL> self . _update_checksum ( checksum ) <EOL> def _update_uint_scale_keywords ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _has_data and self . _standard and <EOL> _is_pseudo_unsigned ( self . data . dtype ) ) : <EOL> if '<STR_LIT>' in self . _header : <EOL> self . _header . set ( '<STR_LIT>' , <NUM_LIT:1> , after = '<STR_LIT>' ) <EOL> else : <EOL> self . _header . set ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . _header . set ( '<STR_LIT>' , _unsigned_zero ( self . data . dtype ) , <EOL> after = '<STR_LIT>' ) <EOL> def _update_checksum ( self , checksum , checksum_keyword = '<STR_LIT>' , <EOL> datasum_keyword = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> modified = self . _header . _modified or self . _data_loaded <EOL> if checksum == '<STR_LIT>' : <EOL> if checksum_keyword in self . _header : <EOL> del self . _header [ checksum_keyword ] <EOL> if datasum_keyword in self . _header : <EOL> del self . _header [ datasum_keyword ] <EOL> elif ( modified or self . _new or <EOL> ( checksum and ( '<STR_LIT>' not in self . _header or <EOL> '<STR_LIT>' not in self . _header ) ) ) : <EOL> if checksum == '<STR_LIT>' : <EOL> self . add_datasum ( datasum_keyword = datasum_keyword ) <EOL> elif checksum == '<STR_LIT>' : <EOL> self . add_datasum ( blocking = '<STR_LIT>' , <EOL> datasum_keyword = datasum_keyword ) <EOL> elif checksum == '<STR_LIT:test>' : <EOL> self . add_datasum ( self . _datasum_comment , <EOL> datasum_keyword = datasum_keyword ) <EOL> self . add_checksum ( self . _checksum_comment , True , <EOL> checksum_keyword = checksum_keyword , <EOL> datasum_keyword = datasum_keyword ) <EOL> elif checksum == '<STR_LIT>' : <EOL> self . add_checksum ( blocking = '<STR_LIT>' , <EOL> checksum_keyword = checksum_keyword , <EOL> datasum_keyword = datasum_keyword ) <EOL> elif checksum : <EOL> self . add_checksum ( blocking = '<STR_LIT>' , <EOL> checksum_keyword = checksum_keyword , <EOL> datasum_keyword = datasum_keyword ) <EOL> def _postwriteto ( self ) : <EOL> if ( self . _has_data and self . _standard and <EOL> _is_pseudo_unsigned ( self . data . dtype ) ) : <EOL> for keyword in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> with ignored ( KeyError ) : <EOL> del self . _header [ keyword ] <EOL> def _writeheader ( self , fileobj ) : <EOL> offset = <NUM_LIT:0> <EOL> if not fileobj . simulateonly : <EOL> with ignored ( AttributeError , IOError ) : <EOL> offset = fileobj . tell ( ) <EOL> self . _header . tofile ( fileobj ) <EOL> try : <EOL> size = fileobj . tell ( ) - offset <EOL> except ( AttributeError , IOError ) : <EOL> size = len ( str ( self . _header ) ) <EOL> else : <EOL> size = len ( str ( self . _header ) ) <EOL> return offset , size <EOL> def _writedata ( self , fileobj ) : <EOL> offset = <NUM_LIT:0> <EOL> size = <NUM_LIT:0> <EOL> if not fileobj . simulateonly : <EOL> fileobj . flush ( ) <EOL> try : <EOL> offset = fileobj . tell ( ) <EOL> except IOError : <EOL> offset = <NUM_LIT:0> <EOL> if self . _data_loaded or self . _data_needs_rescale : <EOL> if self . data is not None : <EOL> size += self . _writedata_internal ( fileobj ) <EOL> if size > <NUM_LIT:0> : <EOL> padding = _pad_length ( size ) * self . _padding_byte <EOL> fileobj . write ( padding . encode ( '<STR_LIT:ascii>' ) ) <EOL> size += len ( padding ) <EOL> else : <EOL> size += self . _writedata_direct_copy ( fileobj ) <EOL> if not fileobj . simulateonly : <EOL> fileobj . flush ( ) <EOL> return offset , size <EOL> def _writedata_internal ( self , fileobj ) : <EOL> """<STR_LIT>""" <EOL> if not fileobj . simulateonly : <EOL> fileobj . writearray ( self . data ) <EOL> return self . data . size * self . data . itemsize <EOL> def _writedata_direct_copy ( self , fileobj ) : <EOL> """<STR_LIT>""" <EOL> raw = self . _get_raw_data ( self . _data_size , '<STR_LIT>' , self . _data_offset ) <EOL> if raw is not None : <EOL> fileobj . writearray ( raw ) <EOL> return raw . nbytes <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def _writeto ( self , fileobj , inplace = False , copy = False ) : <EOL> if not inplace or self . _new : <EOL> header_offset , _ = self . _writeheader ( fileobj ) <EOL> data_offset , data_size = self . _writedata ( fileobj ) <EOL> if self . _new : <EOL> self . _header_offset = header_offset <EOL> self . _data_offset = data_offset <EOL> self . _data_size = data_size <EOL> return <EOL> hdrloc = self . _header_offset <EOL> hdrsize = self . _data_offset - self . _header_offset <EOL> datloc = self . _data_offset <EOL> datsize = self . _data_size <EOL> if self . _header . _modified : <EOL> self . _file . seek ( hdrloc ) <EOL> hdrloc , hdrsize = self . _writeheader ( fileobj ) <EOL> datloc = hdrloc + hdrsize <EOL> elif copy : <EOL> self . _file . seek ( hdrloc ) <EOL> hdrloc = fileobj . tell ( ) <EOL> fileobj . write ( self . _file . read ( hdrsize ) ) <EOL> datloc = fileobj . tell ( ) <EOL> if self . _data_loaded : <EOL> if self . data is not None : <EOL> if copy or self . _data_replaced : <EOL> array_mmap = None <EOL> else : <EOL> array_mmap = _get_array_mmap ( self . data ) <EOL> if array_mmap is not None : <EOL> array_mmap . flush ( ) <EOL> else : <EOL> self . _file . seek ( self . _data_offset ) <EOL> datloc , datsize = self . _writedata ( fileobj ) <EOL> elif copy : <EOL> datsize = self . _writedata_direct_copy ( fileobj ) <EOL> self . _header_offset = hdrloc <EOL> self . _data_offset = datloc <EOL> self . _data_size = datsize <EOL> self . _data_replaced = False <EOL> def _close ( self , closed = True ) : <EOL> if ( closed and self . _data_loaded and <EOL> _get_array_mmap ( self . data ) is not None ) : <EOL> del self . data <EOL> _AllHDU = _BaseHDU <EOL> register_hdu = _BaseHDU . register_hdu <EOL> unregister_hdu = _BaseHDU . unregister_hdu <EOL> class _CorruptedHDU ( _BaseHDU ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _buffer is not None : <EOL> return len ( self . _buffer ) - self . _data_offset <EOL> return self . _file . size - self . _data_offset <EOL> def _summary ( self ) : <EOL> return ( self . name , '<STR_LIT>' ) <EOL> def verify ( self ) : <EOL> pass <EOL> class _NonstandardHDU ( _BaseHDU , _Verify ) : <EOL> """<STR_LIT>""" <EOL> _standard = False <EOL> @ classmethod <EOL> def match_header ( cls , header ) : <EOL> """<STR_LIT>""" <EOL> card = header . cards [ <NUM_LIT:0> ] <EOL> if card . keyword == '<STR_LIT>' : <EOL> if '<STR_LIT>' not in header and card . value == False : <EOL> return True <EOL> else : <EOL> raise InvalidHDUException <EOL> else : <EOL> return False <EOL> @ property <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _buffer is not None : <EOL> return len ( self . _buffer ) - self . _data_offset <EOL> return self . _file . size - self . _data_offset <EOL> def _writedata ( self , fileobj ) : <EOL> """<STR_LIT>""" <EOL> offset = <NUM_LIT:0> <EOL> size = <NUM_LIT:0> <EOL> if not fileobj . simulateonly : <EOL> fileobj . flush ( ) <EOL> try : <EOL> offset = fileobj . tell ( ) <EOL> except IOError : <EOL> offset = <NUM_LIT:0> <EOL> if self . data is not None : <EOL> if not fileobj . simulateonly : <EOL> fileobj . write ( self . data ) <EOL> fileobj . flush ( ) <EOL> size = len ( self . data ) <EOL> return offset , size <EOL> def _summary ( self ) : <EOL> return ( self . name , '<STR_LIT>' , len ( self . _header ) ) <EOL> @ lazyproperty <EOL> def data ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_raw_data ( self . size , '<STR_LIT>' , self . _data_offset ) <EOL> def _verify ( self , option = '<STR_LIT>' ) : <EOL> errs = _ErrList ( [ ] , unit = '<STR_LIT>' ) <EOL> for card in self . _header . cards : <EOL> errs . append ( card . _verify ( option ) ) <EOL> return errs <EOL> class _ValidHDU ( _BaseHDU , _Verify ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = None , header = None , name = None , ** kwargs ) : <EOL> super ( _ValidHDU , self ) . __init__ ( data = data , header = header ) <EOL> if name is not None : <EOL> self . name = name <EOL> @ classmethod <EOL> def match_header ( cls , header ) : <EOL> """<STR_LIT>""" <EOL> return first ( header . keys ( ) ) not in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ property <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> size = <NUM_LIT:0> <EOL> naxis = self . _header . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if naxis > <NUM_LIT:0> : <EOL> size = <NUM_LIT:1> <EOL> for idx in range ( naxis ) : <EOL> size = size * self . _header [ '<STR_LIT>' + str ( idx + <NUM_LIT:1> ) ] <EOL> bitpix = self . _header [ '<STR_LIT>' ] <EOL> gcount = self . _header . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> pcount = self . _header . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> size = abs ( bitpix ) * gcount * ( pcount + size ) // <NUM_LIT:8> <EOL> return size <EOL> def filebytes ( self ) : <EOL> """<STR_LIT>""" <EOL> f = _File ( ) <EOL> return self . _writeheader ( f ) [ <NUM_LIT:1> ] + self . _writedata ( f ) [ <NUM_LIT:1> ] <EOL> def fileinfo ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) and self . _file : <EOL> return { '<STR_LIT:file>' : self . _file , '<STR_LIT>' : self . _file . mode , <EOL> '<STR_LIT>' : self . _header_offset , '<STR_LIT>' : self . _data_offset , <EOL> '<STR_LIT>' : self . _data_size } <EOL> else : <EOL> return None <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . data is not None : <EOL> data = self . data . copy ( ) <EOL> else : <EOL> data = None <EOL> return self . __class__ ( data = data , header = self . _header . copy ( ) ) <EOL> @ deprecated ( '<STR_LIT>' , alternative = '<STR_LIT>' , <EOL> pending = True ) <EOL> def update_ext_name ( self , value , comment = None , before = None , <EOL> after = None , savecomment = False ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . _header and savecomment : <EOL> comment = None <EOL> self . _header . set ( '<STR_LIT>' , value , comment , before , after ) <EOL> self . name = value <EOL> @ deprecated ( '<STR_LIT>' , alternative = '<STR_LIT>' , <EOL> pending = True ) <EOL> def update_ext_version ( self , value , comment = None , before = None , <EOL> after = None , savecomment = False ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . _header and savecomment : <EOL> comment = None <EOL> self . _header . set ( '<STR_LIT>' , value , comment , before , after ) <EOL> def _verify ( self , option = '<STR_LIT>' ) : <EOL> errs = _ErrList ( [ ] , unit = '<STR_LIT>' ) <EOL> is_valid = lambda v : v in [ <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:64> , - <NUM_LIT:32> , - <NUM_LIT:64> ] <EOL> if isinstance ( self , ExtensionHDU ) : <EOL> firstkey = '<STR_LIT>' <EOL> firstval = self . _extension <EOL> else : <EOL> firstkey = '<STR_LIT>' <EOL> firstval = True <EOL> self . req_cards ( firstkey , <NUM_LIT:0> , None , firstval , option , errs ) <EOL> self . req_cards ( '<STR_LIT>' , <NUM_LIT:1> , lambda v : ( _is_int ( v ) and is_valid ( v ) ) , <NUM_LIT:8> , <EOL> option , errs ) <EOL> self . req_cards ( '<STR_LIT>' , <NUM_LIT:2> , <EOL> lambda v : ( _is_int ( v ) and v >= <NUM_LIT:0> and v <= <NUM_LIT> ) , <NUM_LIT:0> , <EOL> option , errs ) <EOL> naxis = self . _header . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if naxis < <NUM_LIT:1000> : <EOL> for ax in range ( <NUM_LIT:3> , naxis + <NUM_LIT:3> ) : <EOL> self . req_cards ( '<STR_LIT>' + str ( ax - <NUM_LIT:2> ) , ax , <EOL> lambda v : ( _is_int ( v ) and v >= <NUM_LIT:0> ) , <NUM_LIT:1> , option , <EOL> errs ) <EOL> for keyword in self . _header : <EOL> if keyword . startswith ( '<STR_LIT>' ) and len ( keyword ) > <NUM_LIT:5> : <EOL> try : <EOL> number = int ( keyword [ <NUM_LIT:5> : ] ) <EOL> if number <= <NUM_LIT:0> or number > naxis : <EOL> raise ValueError <EOL> except ValueError : <EOL> err_text = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( keyword , naxis ) ) <EOL> def fix ( self = self , keyword = keyword ) : <EOL> del self . _header [ keyword ] <EOL> errs . append ( <EOL> self . run_option ( option = option , err_text = err_text , <EOL> fix = fix , fix_text = "<STR_LIT>" ) ) <EOL> if '<STR_LIT>' in self . _header : <EOL> if not isinstance ( self . _header [ '<STR_LIT>' ] , string_types ) : <EOL> err_text = '<STR_LIT>' <EOL> fix_text = '<STR_LIT>' <EOL> def fix ( header = self . _header ) : <EOL> header [ '<STR_LIT>' ] = str ( header [ '<STR_LIT>' ] ) <EOL> errs . append ( self . run_option ( option , err_text = err_text , <EOL> fix_text = fix_text , fix = fix ) ) <EOL> for card in self . _header . cards : <EOL> errs . append ( card . _verify ( option ) ) <EOL> return errs <EOL> def req_cards ( self , keyword , pos , test , fix_value , option , errlist ) : <EOL> """<STR_LIT>""" <EOL> errs = errlist <EOL> fix = None <EOL> try : <EOL> index = self . _header . index ( keyword ) <EOL> except ValueError : <EOL> index = None <EOL> fixable = fix_value is not None <EOL> insert_pos = len ( self . _header ) + <NUM_LIT:1> <EOL> if _is_int ( pos ) : <EOL> insert_pos = pos <EOL> pos = lambda x : x == insert_pos <EOL> if index is None : <EOL> err_text = "<STR_LIT>" % keyword <EOL> fix_text = "<STR_LIT>" % keyword <EOL> if fixable : <EOL> card = ( keyword , fix_value ) <EOL> def fix ( self = self , insert_pos = insert_pos , card = card ) : <EOL> self . _header . insert ( insert_pos , card ) <EOL> errs . append ( self . run_option ( option , err_text = err_text , <EOL> fix_text = fix_text , fix = fix , fixable = fixable ) ) <EOL> else : <EOL> if pos is not None : <EOL> if not pos ( index ) : <EOL> err_text = ( "<STR_LIT>" % <EOL> ( keyword , index ) ) <EOL> fix_text = ( "<STR_LIT>" <EOL> "<STR_LIT>" % insert_pos ) <EOL> def fix ( self = self , index = index , insert_pos = insert_pos ) : <EOL> card = self . _header . cards [ index ] <EOL> del self . _header [ index ] <EOL> self . _header . insert ( insert_pos , card ) <EOL> errs . append ( self . run_option ( option , err_text = err_text , <EOL> fix_text = fix_text , fix = fix ) ) <EOL> if test : <EOL> val = self . _header [ keyword ] <EOL> if not test ( val ) : <EOL> err_text = ( "<STR_LIT>" % <EOL> ( keyword , val ) ) <EOL> fix_text = "<STR_LIT>" % fix_value <EOL> if fixable : <EOL> def fix ( self = self , keyword = keyword , val = fix_value ) : <EOL> self . _header [ keyword ] = fix_value <EOL> errs . append ( self . run_option ( option , err_text = err_text , <EOL> fix_text = fix_text , fix = fix , fixable = fixable ) ) <EOL> return errs <EOL> def add_datasum ( self , when = None , blocking = '<STR_LIT>' , <EOL> datasum_keyword = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> cs = self . _calculate_datasum ( blocking ) <EOL> if when is None : <EOL> when = '<STR_LIT>' % self . _get_timestamp ( ) <EOL> self . _header [ datasum_keyword ] = ( str ( cs ) , when ) <EOL> return cs <EOL> def add_checksum ( self , when = None , override_datasum = False , <EOL> blocking = '<STR_LIT>' , checksum_keyword = '<STR_LIT>' , <EOL> datasum_keyword = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not override_datasum : <EOL> data_cs = self . add_datasum ( when , blocking , <EOL> datasum_keyword = datasum_keyword ) <EOL> else : <EOL> data_cs = self . _calculate_datasum ( blocking ) <EOL> if when is None : <EOL> when = '<STR_LIT>' % self . _get_timestamp ( ) <EOL> if datasum_keyword in self . _header : <EOL> self . _header . set ( checksum_keyword , '<STR_LIT:0>' * <NUM_LIT:16> , when , <EOL> before = datasum_keyword ) <EOL> else : <EOL> self . _header . set ( checksum_keyword , '<STR_LIT:0>' * <NUM_LIT:16> , when ) <EOL> csum = self . _calculate_checksum ( data_cs , blocking , <EOL> checksum_keyword = checksum_keyword ) <EOL> self . _header [ checksum_keyword ] = csum <EOL> def verify_datasum ( self , blocking = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . _header : <EOL> datasum = self . _calculate_datasum ( blocking ) <EOL> if datasum == int ( self . _header [ '<STR_LIT>' ] ) : <EOL> return <NUM_LIT:1> <EOL> elif blocking == '<STR_LIT>' : <EOL> return self . verify_datasum ( blocking = '<STR_LIT>' ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:2> <EOL> def verify_checksum ( self , blocking = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . _header : <EOL> if '<STR_LIT>' in self . _header : <EOL> datasum = self . _calculate_datasum ( blocking ) <EOL> else : <EOL> datasum = <NUM_LIT:0> <EOL> checksum = self . _calculate_checksum ( datasum , blocking ) <EOL> if checksum == self . _header [ '<STR_LIT>' ] : <EOL> return <NUM_LIT:1> <EOL> elif blocking == '<STR_LIT>' : <EOL> return self . verify_checksum ( blocking = '<STR_LIT>' ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:2> <EOL> def _verify_checksum_datasum ( self , blocking ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . _header : <EOL> self . _checksum = self . _header [ '<STR_LIT>' ] <EOL> self . _checksum_comment = self . _header . comments [ '<STR_LIT>' ] <EOL> if not self . verify_checksum ( blocking ) : <EOL> warnings . warn ( '<STR_LIT>' % <EOL> ( ( self . name , self . ver ) , ) ) <EOL> else : <EOL> self . _checksum = None <EOL> self . _checksum_comment = None <EOL> if '<STR_LIT>' in self . _header : <EOL> self . _datasum = self . _header [ '<STR_LIT>' ] <EOL> self . _datasum_comment = self . _header . comments [ '<STR_LIT>' ] <EOL> if not self . verify_datasum ( blocking ) : <EOL> warnings . warn ( '<STR_LIT>' % <EOL> ( ( self . name , self . ver ) , ) ) <EOL> else : <EOL> self . _checksum = None <EOL> self . _checksum_comment = None <EOL> self . _datasum = None <EOL> self . _datasum_comment = None <EOL> def _get_timestamp ( self ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime . now ( ) . isoformat ( ) [ : <NUM_LIT> ] <EOL> def _calculate_datasum ( self , blocking ) : <EOL> """<STR_LIT>""" <EOL> if not self . _data_loaded : <EOL> if self . size > <NUM_LIT:0> : <EOL> raw_data = self . _get_raw_data ( self . _data_size , '<STR_LIT>' , <EOL> self . _data_offset ) <EOL> return self . _compute_checksum ( raw_data , blocking = blocking ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> elif self . data is not None : <EOL> return self . _compute_checksum ( self . data . view ( '<STR_LIT>' ) , <EOL> blocking = blocking ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def _calculate_checksum ( self , datasum , blocking , <EOL> checksum_keyword = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> old_checksum = self . _header [ checksum_keyword ] <EOL> self . _header [ checksum_keyword ] = '<STR_LIT:0>' * <NUM_LIT:16> <EOL> s = str ( self . _header ) <EOL> cs = self . _compute_checksum ( np . fromstring ( s , dtype = '<STR_LIT>' ) , datasum , <EOL> blocking = blocking ) <EOL> s = self . _char_encode ( ~ cs ) <EOL> self . _header [ checksum_keyword ] = old_checksum <EOL> return s <EOL> def _compute_checksum ( self , data , sum32 = <NUM_LIT:0> , blocking = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> blocklen = { '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : len ( data ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> True : <NUM_LIT> } [ blocking ] <EOL> sum32 = np . uint32 ( sum32 ) <EOL> for i in range ( <NUM_LIT:0> , len ( data ) , blocklen ) : <EOL> length = min ( blocklen , len ( data ) - i ) <EOL> sum32 = self . _compute_hdu_checksum ( data [ i : i + length ] , sum32 ) <EOL> return sum32 <EOL> def _compute_hdu_checksum ( self , data , sum32 = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> u8 = np . uint32 ( <NUM_LIT:8> ) <EOL> u16 = np . uint32 ( <NUM_LIT:16> ) <EOL> uFFFF = np . uint32 ( <NUM_LIT> ) <EOL> if data . nbytes % <NUM_LIT:2> : <EOL> last = data [ - <NUM_LIT:1> ] <EOL> data = data [ : - <NUM_LIT:1> ] <EOL> else : <EOL> last = np . uint32 ( <NUM_LIT:0> ) <EOL> data = data . view ( '<STR_LIT>' ) <EOL> hi = sum32 >> u16 <EOL> lo = sum32 & uFFFF <EOL> hi += np . add . reduce ( data [ <NUM_LIT:0> : : <NUM_LIT:2> ] , dtype = np . uint64 ) <EOL> lo += np . add . reduce ( data [ <NUM_LIT:1> : : <NUM_LIT:2> ] , dtype = np . uint64 ) <EOL> if ( data . nbytes // <NUM_LIT:2> ) % <NUM_LIT:2> : <EOL> lo += last << u8 <EOL> else : <EOL> hi += last << u8 <EOL> hicarry = hi >> u16 <EOL> locarry = lo >> u16 <EOL> while hicarry or locarry : <EOL> hi = ( hi & uFFFF ) + locarry <EOL> lo = ( lo & uFFFF ) + hicarry <EOL> hicarry = hi >> u16 <EOL> locarry = lo >> u16 <EOL> return ( hi << u16 ) + lo <EOL> _MASK = [ <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> ] <EOL> _EXCLUDE = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> def _encode_byte ( self , byte ) : <EOL> """<STR_LIT>""" <EOL> quotient = byte // <NUM_LIT:4> + ord ( '<STR_LIT:0>' ) <EOL> remainder = byte % <NUM_LIT:4> <EOL> ch = np . array ( <EOL> [ ( quotient + remainder ) , quotient , quotient , quotient ] , <EOL> dtype = '<STR_LIT>' ) <EOL> check = True <EOL> while check : <EOL> check = False <EOL> for x in self . _EXCLUDE : <EOL> for j in [ <NUM_LIT:0> , <NUM_LIT:2> ] : <EOL> if ch [ j ] == x or ch [ j + <NUM_LIT:1> ] == x : <EOL> ch [ j ] += <NUM_LIT:1> <EOL> ch [ j + <NUM_LIT:1> ] -= <NUM_LIT:1> <EOL> check = True <EOL> return ch <EOL> def _char_encode ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = np . uint32 ( value ) <EOL> asc = np . zeros ( ( <NUM_LIT:16> , ) , dtype = '<STR_LIT>' ) <EOL> ascii = np . zeros ( ( <NUM_LIT:16> , ) , dtype = '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> byte = ( value & self . _MASK [ i ] ) >> ( ( <NUM_LIT:3> - i ) * <NUM_LIT:8> ) <EOL> ch = self . _encode_byte ( byte ) <EOL> for j in range ( <NUM_LIT:4> ) : <EOL> asc [ <NUM_LIT:4> * j + i ] = ch [ j ] <EOL> for i in range ( <NUM_LIT:16> ) : <EOL> ascii [ i ] = asc [ ( i + <NUM_LIT:15> ) % <NUM_LIT:16> ] <EOL> return decode_ascii ( ascii . tostring ( ) ) <EOL> class ExtensionHDU ( _ValidHDU ) : <EOL> """<STR_LIT>""" <EOL> _extension = '<STR_LIT>' <EOL> @ classmethod <EOL> def match_header ( cls , header ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def writeto ( self , name , output_verify = '<STR_LIT>' , clobber = False , <EOL> checksum = False ) : <EOL> """<STR_LIT>""" <EOL> from pyfits . hdu . hdulist import HDUList <EOL> from pyfits . hdu . image import PrimaryHDU <EOL> hdulist = HDUList ( [ PrimaryHDU ( ) , self ] ) <EOL> hdulist . writeto ( name , output_verify , clobber = clobber , <EOL> checksum = checksum ) <EOL> def _verify ( self , option = '<STR_LIT>' ) : <EOL> errs = super ( ExtensionHDU , self ) . _verify ( option = option ) <EOL> naxis = self . _header . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . req_cards ( '<STR_LIT>' , naxis + <NUM_LIT:3> , lambda v : ( _is_int ( v ) and v >= <NUM_LIT:0> ) , <EOL> <NUM_LIT:0> , option , errs ) <EOL> self . req_cards ( '<STR_LIT>' , naxis + <NUM_LIT:4> , lambda v : ( _is_int ( v ) and v == <NUM_LIT:1> ) , <EOL> <NUM_LIT:1> , option , errs ) <EOL> return errs <EOL> _ExtensionHDU = ExtensionHDU <EOL> class NonstandardExtHDU ( ExtensionHDU ) : <EOL> """<STR_LIT>""" <EOL> _standard = False <EOL> @ classmethod <EOL> def match_header ( cls , header ) : <EOL> """<STR_LIT>""" <EOL> card = header . cards [ <NUM_LIT:0> ] <EOL> xtension = card . value <EOL> if isinstance ( xtension , string_types ) : <EOL> xtension = xtension . rstrip ( ) <EOL> standard_xtensions = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return ( card . keyword == '<STR_LIT>' and <EOL> xtension not in standard_xtensions ) <EOL> def _summary ( self ) : <EOL> return ( self . name , '<STR_LIT>' , len ( self . _header ) ) <EOL> @ lazyproperty <EOL> def data ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_raw_data ( self . size , '<STR_LIT>' , self . _data_offset ) <EOL> _NonstandardExtHDU = NonstandardExtHDU </s>
