B
    îq\’¼  ã               @   sà   d dl Z d dlZd dlZddlmZmZmZmZ ddl	m
Z
mZmZmZ ddlmZ d dlmZ ddgZe d	d
¡Ze dd¡ZdZde ZdZdZdZG dd„ dƒZeƒ ZG dd„ de
ƒZdd„ Zdd„ Z dd„ Z!dd„ Z"dS )é    Né   )Ú_str_to_numÚ_is_intÚ	translateÚ_words_group)Ú_VerifyÚ_ErrListÚVerifyErrorÚVerifyWarning)Úconf)ÚAstropyUserWarningÚCardÚ	UndefinedZdeZDEZdDZeEéP   ú é   z= ú=c               @   s   e Zd ZdZdd„ ZdS )r   zUndefined value.c             C   s   d S )N© )Úselfr   r   ú3lib/python3.7/site-packages/astropy/io/fits/card.pyÚ__init__!   s    zUndefined.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   c               @   s²  e Zd ZeZe de ¡Ze dej	¡Z
dZdZde Zde Ze d e¡¡Ze d e¡¡Ze d	¡Ze d
e d e d e d ¡Ze de d e d e d ¡ZdZed Zd ee¡Zd ee¡Zd e¡Zd e¡Ze ed ¡Ze d ee¡¡Ze e¡ZddddhZeZ dedd„Z!dd „ Z"d!d"„ Z#d#d$„ Z$d%d&„ Z%e&d'd(„ ƒZ'e'j(d)d(„ ƒZ'e&d*d+„ ƒZ)e)j(d,d+„ ƒZ)e)j*d-d+„ ƒZ)e&d.d/„ ƒZ+e&d0d1„ ƒZ,e&d2d3„ ƒZ-e-j(d4d3„ ƒZ-e-j*d5d3„ ƒZ-e&d6d7„ ƒZ.e.j(d8d7„ ƒZ.e.j*d9d7„ ƒZ.e&d:d;„ ƒZ/e&d<d=„ ƒZ0e1d>d?„ ƒZ2e1d@dA„ ƒZ3dBdC„ Z4dDdE„ Z5dFdG„ Z6dHdI„ Z7dJdK„ Z8dLdM„ Z9dNdO„ Z:dPdQ„ Z;dRdS„ Z<dTdU„ Z=dVdW„ Z>dXdY„ Z?dZd[„ Z@d\d]„ ZAd^d_„ ZBdfdadb„ZCdcdd„ ZDdS )gr   z^[A-Z0-9_-]{0,%d}$z"^(?:HIERARCH +)?(?:^[ -<>-~]+ ?)+$z"(\.\d+|\d+(\.\d*)?)([DE][+-]?\d+)?z*(\.\d+|\d+(\.\d*)?) *([deDE] *[+-]? *\d+)?z[+-]?z[+-]? *z(?P<sign>[+-])?0*?(?P<digt>{})z (?P<sign>[+-])? *0*?(?P<digt>{})z[ -~]*\Zz\(?P<valu_field> *(?P<valu>\'(?P<strg>([ -~]+?|\'\'|)) *?\'(?=$|/| )|(?P<bool>[FT])|(?P<numr>z)|(?P<cplx>\( *(?P<real>z) *, *(?P<imag>z@) *\)))? *)(?P<comm_field>(?P<sepr>/ *)(?P<comm>[!-~][ -~]*)?)?$z\(?P<valu_field> *(?P<valu>\'(?P<strg>([ -~]+?|\'\'|) *?)\'(?=$|/| )|(?P<bool>[FT])|(?P<numr>z;) *\)))? *)(?P<comm_field>(?P<sepr>/ *)(?P<comm>(.|\n)*))?$z[a-zA-Z_]\w*z(\.\d+)?z	{}(\.{})*z(?P<keyword>{}): (?P<val>{})z\'(?P<rawval>{})\'z *{} *(/ *(?P<comm>[ -~]*))?$ú$z)(?P<keyword>{})\.(?P<field_specifier>{})$Ú ÚCOMMENTZHISTORYÚENDNc             K   s°   |d krd|kr|d }d | _ d | _d | _d | _d| _d| _d| _d | _d | _d | _	|d k	rp|d k	rp|  
||¡sŒ|d k	r~|| _|d k	rŒ|| _|d k	rš|| _d| _d | _d| _d S )NÚkeyTF)Ú_keywordÚ_valueÚ_commentÚ_imageÚ	_verifiedÚ	_hierarchÚ_invalidÚ_field_specifierÚ_rawkeywordÚ	_rawvalueÚ_check_if_rvkcÚkeywordÚvalueÚcommentÚ	_modifiedÚ_valuestringÚ_valuemodified)r   r+   r,   r-   Úkwargsr   r   r   r   š   s.    zCard.__init__c             C   s   t | j| j| jfƒS )N)Úreprr+   r,   r-   )r   r   r   r   Ú__repr__È   s    zCard.__repr__c             C   s   | j S )N)Úimage)r   r   r   r   Ú__str__Ë   s    zCard.__str__c             C   s   dS )Né   r   )r   r   r   r   Ú__len__Î   s    zCard.__len__c             C   s   | j | j| jf| S )N)r+   r,   r-   )r   Úindexr   r   r   Ú__getitem__Ñ   s    zCard.__getitem__c             C   s4   | j dk	r| j S | jr&|  ¡ | _ | j S d| _dS dS )z4Returns the keyword name parsed from the card image.Nr   )r    r#   Ú_parse_keywordr+   )r   r   r   r   r+   Ô   s    

zCard.keywordc             C   sØ   | j dk	rtdƒ‚nÀt|tƒrÆ| ¡ }| ¡ }t|ƒtkr\| j 	|¡r\|dkrVt
dƒ‚|}n\| j 	|¡rªd| _t| _|dd… dkr–|dd…  ¡ }q¸t d |¡t¡ nt
d	 |¡ƒ‚|| _ d| _nt
d
 |¡ƒ‚dS )z6Set the key attribute; once set it cannot be modified.Nz.Once set, the Card keyword may not be modifiedr   zKeyword 'END' not allowed.Té	   z	HIERARCH zˆKeyword name {!r} is greater than 8 characters or contains characters not allowed by the FITS standard; a HIERARCH card will be created.zIllegal keyword name: {!r}.z"Keyword name {!r} is not a string.)r    ÚAttributeErrorÚ
isinstanceÚstrÚrstripÚupperÚlenÚKEYWORD_LENGTHÚ_keywd_FSC_REÚmatchÚ
ValueErrorÚ_keywd_hierarch_REr%   ÚHIERARCH_VALUE_INDICATORÚ_value_indicatorÚstripÚwarningsÚwarnÚformatr
   r.   )r   r+   Úkeyword_upperr   r   r   r+   à   s.    

	c             C   sj   | j rt| jƒS | jdk	r"| j}n,| jdk	s2| jrD|  ¡ | _| j}n
d | _}tjrft|t	ƒrf| 
¡ }|S )z:The value associated with the keyword stored in this card.Nr   )Úfield_specifierÚfloatr!   r/   r#   Ú_parse_valuer   Ústrip_header_whitespacer=   r>   r?   )r   r,   r   r   r   r,     s    



z
Card.valuec             C   s  | j rtdƒ‚|d krd}| j}|d kr,d}t|tttttt	t
jt
jt
jt
jf
ƒs`td |¡ƒ‚t|tƒrŽt
 |¡s~t
 |¡rŽtd |¡ƒ‚n¢t|tƒr¸| j |¡}|s¶td |¡ƒ‚nxt|tƒrd}y| d¡}W n tk
rî   d}Y nX | j |¡}|d k	}|s0td	 |¡ƒ‚nt|t
jƒr0t|ƒ}tjrbt|tƒrbt|tƒrb| ¡ | ¡ k}n<t|tƒszt|tƒr„||k	}n||kpœt|t|ƒƒ }|r|| _d | _d| _d | _d| _| jryt | jƒ| _W n& tk
r    td
 | j¡ƒ‚Y nX d S )NzjThe value of invalid/unparseable cards cannot set.  Either delete this card from the header or replace it.r   zIllegal value: {!r}.z;Floating point {!r} values are not allowed in FITS headers.z•FITS header values must contain standard printable ASCII characters; {!r} contains characters not representable in ASCII or non-printable characters.TÚasciiFz˜FITS header values must contain standard printable ASCII characters; {!r} contains characters/bytes that do not represent printable characters in ASCII.zvalue {} is not a float)!r&   rE   r!   r=   r>   ÚintrO   ÚcomplexÚboolr   ÚnpÚfloatingZintegerÚcomplexfloatingÚbool_rL   ZisnanZisinfÚ_ascii_text_rerD   ÚbytesÚdecodeÚUnicodeDecodeErrorr   rQ   r?   Útyper)   r.   r/   r0   rN   Ú_int_or_float)r   r,   ZoldvalueÚmZvalidZ
text_valueZ	differentr   r   r   r,   &  sn    







c             C   s(   | j rtdƒ‚| jsd| _ntdƒ‚d S )NznThe value of invalid/unparseable cards cannot deleted.  Either delete this card from the header or replace it.r   z9Values cannot be deleted from record-valued keyword cards)r&   rE   rN   r,   r<   )r   r   r   r   r,   s  s    c             C   s>   | j dk	r| j S | jdk	r4| j dd¡d | _ | j S | jS dS )z½On record-valued keyword cards this is the name of the standard <= 8
        character FITS keyword that this RVKC is stored in.  Otherwise it is
        the card's normal keyword.
        NÚ.r   r   )r(   rN   r+   Úsplit)r   r   r   r   Ú
rawkeyword€  s    

zCard.rawkeywordc             C   s<   | j dk	r| j S | jdk	r2d | j| j¡| _ | j S | jS dS )zÜOn record-valued keyword cards this is the raw string value in
        the ``<field-specifier>: <value>`` format stored in the card in order
        to represent a RVKC.  Otherwise it is the card's normal value.
        Nz{}: {})r)   rN   rL   r,   )r   r   r   r   Úrawvalue  s    

zCard.rawvaluec             C   s4   | j dk	r| j S | jr&|  ¡ | _ | j S d| _dS dS )zAGet the comment attribute from the card image if not already set.Nr   )r"   r#   Ú_parse_commentr-   )r   r   r   r   r-   ž  s    

zCard.commentc             C   sl   | j rtdƒ‚|d krd}t|tƒrB| j |¡}|sBtd |¡ƒ‚| j}|d krTd}||krh|| _d| _d S )NzlThe comment of invalid/unparseable cards cannot set.  Either delete this card from the header or replace it.r   z—FITS header comments must contain standard printable ASCII characters; {!r} contains characters not representable in ASCII or non-printable characters.T)	r&   rE   r=   r>   rZ   rD   rL   r"   r.   )r   r-   r`   Z
oldcommentr   r   r   r-   «  s"    
c             C   s   | j rtdƒ‚d| _d S )NzpThe comment of invalid/unparseable cards cannot deleted.  Either delete this card from the header or replace it.r   )r&   rE   r-   )r   r   r   r   r-   Å  s    c             C   s   | j r| jS dS dS )zl
        The field-specifier of record-valued keyword cards; always `None` on
        normal cards.
        N)r+   r'   )r   r   r   r   rN   Î  s    	zCard.field_specifierc             C   sZ   |st dƒ‚nH| jstdƒ‚n8|| jkrV|| _| j dd¡d }d ||g¡| _d| _d S )NzDThe field-specifier may not be blank in record-valued keyword cards.z^Cannot coerce cards to be record-valued keyword cards by setting the field_specifier attributera   r   r   T)rE   rN   r<   r'   r    rb   Újoinr.   )r   rN   r+   r   r   r   rN   Ü  s    


c             C   s   t dƒ‚d S )NzRThe field_specifier attribute may not be deleted from record-valued keyword cards.)r<   )r   r   r   r   rN   ì  s    c             C   s6   | j r| js|  d¡ | j dks&| jr0|  ¡ | _ | j S )z…
        The card "image", that is, the 80 byte character string that represents
        this card in an actual FITS header.
        zfix+warnN)r#   r$   Úverifyr.   Ú_format_image)r   r   r   r   r4   ñ  s
    

z
Card.imagec             C   s4   | j s| jtkS | j o2t| jtƒo2| j o2| j S )z¼
        `True` if the card is completely blank--that is, it has no keyword,
        value, or comment.  It appears in the header as 80 spaces.

        Returns `False` otherwise.
        )r$   r#   Ú
BLANK_CARDr+   r=   r,   r>   r-   )r   r   r   r   Úis_blankþ  s
    	
zCard.is_blankc             C   s   | ƒ }t |ƒ|_d|_|S )zü
        Construct a `Card` object from a (raw) string. It will pad the string
        if it is not the length of a card image (80 columns).  If the card
        image is longer than 80 columns, assume it contains ``CONTINUE``
        card(s).
        F)Ú_padr#   r$   )Úclsr4   Úcardr   r   r   Ú
fromstring  s    	
zCard.fromstringc             C   s’   t |ƒtkr| j |¡r|S | j |¡}|rNd | d¡ ¡  ¡ | d¡f¡S t |ƒdkr‚|dd…  ¡ dkr‚|dd…  ¡  ¡ S | ¡  ¡ S dS )a_  
        `classmethod` to convert a keyword value that may contain a
        field-specifier to uppercase.  The effect is to raise the key to
        uppercase and leave the field specifier in its original case.

        Parameters
        ----------
        keyword : or str
            A keyword value or a ``keyword.field-specifier`` value
        ra   r+   rN   r;   Nz	HIERARCH )	rA   rB   rC   rD   Ú_rvkc_keyword_name_RErf   ÚgrouprI   r@   )rl   r+   rD   r   r   r   Únormalize_keyword!  s     zCard.normalize_keywordc             G   sè   t js
dS t|ƒdkr"| j|Ž  nÂt|ƒdkrä|\}}t|tƒsDdS || jkrRdS | j |¡}|rt|t	t
fƒr|  | d¡| d¡d|¡ dS t|tƒrä| d¡d	krä| j |¡}|rä| j |¡rä|  || d¡|| d
¡¡ dS dS )a!  
        Determine whether or not the card is a record-valued keyword card.

        If one argument is given, that argument is treated as a full card image
        and parsed as such.  If two arguments are given, the first is treated
        as the card keyword (including the field-specifier if the card is
        intended as a RVKC), and the second as the card value OR the first value
        can be the base keyword, and the second value the 'field-specifier:
        value' string.

        If the check passes the ._keyword, ._value, and .field_specifier
        keywords are set.

        Examples
        --------

        ::

            self._check_if_rvkc('DP1', 'AXIS.1: 2')
            self._check_if_rvkc('DP1.AXIS.1', 2)
            self._check_if_rvkc('DP1     = AXIS.1: 2')
        Fr   é   r+   rN   NTz: r   Úval)r   Z"enable_record_valued_keyword_cardsrA   Ú_check_if_rvkc_imager=   r>   Ú_commentary_keywordsro   rD   rS   rO   Ú
_init_rvkcrp   ÚfindÚ_rvkc_field_specifier_val_RErC   )r   Úargsr+   r,   rD   r   r   r   r*   C  s*    

zCard._check_if_rvkcc             G   sÀ   t |ƒdkrT|d }| t¡}|dk s.|dkr2dS |d|… }||t tƒ d… }n|\}}| ¡ }|r‚|d dks‚| d¡dk r†dS | j |¡}|r¼|  || d	¡| d
¡| d¡¡ dS dS )a(  
        Implements `Card._check_if_rvkc` for the case of an unparsed card
        image.  If given one argument this is the full intact image.  If given
        two arguments the card has already been split between keyword and
        value+comment at the standard value indicator '= '.
        r   r   r;   FNú'z: rr   r+   Zrawvalrs   T)rA   rw   ÚVALUE_INDICATORÚlstripÚ_rvkc_keyword_val_comm_RErD   rv   rp   )r   ry   r4   Zeq_idxr+   ÚrestrD   r   r   r   rt   u  s     
zCard._check_if_rvkc_imagec             C   s8   |  ¡ }d ||f¡| _|| _|| _t|ƒ| _|| _dS )z
        Sort of addendum to Card.__init__ to set the appropriate internal
        attributes if the card was determined to be a RVKC.
        ra   N)r@   rf   r    r(   r'   r_   r!   r)   )r   r+   rN   Zfieldr,   rM   r   r   r   rv   –  s    
zCard._init_rvkcc             C   s  | j d t…  ¡ }| ¡ }| j  t¡}| j}d|  kr@tksRn ||ksR|dkrž|dkrš|d |… }| j |ttƒ d … }|  ||¡rŽ| j	S |d |… }|S |dkrì| j d dkrìt
| j krìd| _t
| _| j  t
d¡d dd … }| ¡ S t d	 | j ¡t¡ d| _|S d S )
Nr   ÚCONTINUEÚHIERARCHr   r   Tr   r;   z^The following header keyword is invalid or follows an unrecognized non-standard convention:
{})r#   rB   rI   r@   rw   r{   ru   rA   rt   r    rG   r%   rH   rb   rJ   rK   rL   r   r&   )r   r+   rM   Zval_ind_idxZspecialr~   r   r   r   r:   £  s.    
zCard._parse_keywordc             C   sX  | j  ¡ | jks| jr(| jtd…  ¡ S |  | j¡r:| jS t	| jƒ| j
kr¬g }xH|  ¡ D ]<}|j ¡  dd¡}|rŠ|d dkrŠ|dd… }| |¡ qXW d |¡}|| _|S | j |  ¡ d ¡}|dkrØtd | j ¡ƒ‚| d	¡dk	rø| d	¡d
k}nH| d¡dk	r t dd| d¡¡}n | d¡dk	r‚| j | d¡¡}t| d¡tdƒ}| d¡dkrjd}n
| d¡}t|| ƒ}n¾| d¡dk	r<| j | d¡¡}t| d¡tdƒ}	| d¡dkrÌd}
n
| d¡}
t|
|	 ƒ}| j | d¡¡}t| d¡tdƒ}| d¡dkrd}n
| d¡}|t|| ƒd 7 }nt}| jsT| d¡| _|S )z.Extract the keyword value from the card image.Nz''rz   éÿÿÿÿú&r   r   z7Unparsable card ({}), fix it first with .verify('fix').rU   ÚTZstrgÚnumrÚdigtr   ÚsignÚcplxÚrealÚimagy              ð?Zvalu)r+   r@   ru   r&   r#   rB   r?   r*   r!   rA   ÚlengthÚ_itersubcardsr,   ÚreplaceÚappendrf   r/   Ú_value_NFSC_RErD   Ú_splitr	   rL   rp   ÚreÚsubÚ_number_NFSC_REr   ÚFIX_FP_TABLE2r   Ú	UNDEFINED)r   Úvaluesrm   r,   r`   r„   r…   r†   rˆ   ÚrdigtZrsignr‰   ÚidigtZisignr   r   r   rP   Ë  s\    




zCard._parse_valuec             C   s¤   | j tjks| jrdS t| jƒ| jkrng }x"|  ¡ D ]}|jr4| 	|j¡ q4W dd 
|¡ ¡  }| j |¡}n| j |  ¡ d ¡}|dk	r | d¡}|r | ¡ S dS )z.Extract the keyword value from the card image.r   z/ r   r   NÚcomm)r+   r   ru   r&   rA   r#   rŠ   r‹   r-   r   rf   r?   rŽ   rD   r   rp   )r   Zcommentsrm   r-   r`   r   r   r   re     s    
zCard._parse_commentc             C   sÞ   | j dk	r| j }n| j}| j| j dg¡kr>| dd¡\}}ny| | j¡}W n tk
rf   d}Y nX |dkrŠ|dt	… }|t	d… }nD|dkr¼|dd… dkr¼|dd… }|dd… }n| | jd¡\}}| 
¡ | 
¡ fS )	zT
        Split the card image between the keyword and the rest of the card.
        Nr   r   r   é
   r;   z	HIERARCH r   )r#   r4   r+   ru   Úunionrb   r8   rH   rE   rB   rI   )r   r4   r+   ÚvaluecommentZdelim_indexr   r   r   r   &  s"    

zCard._splitc             C   sD   | j r.| j dd¡\}}d | ¡ |g¡| _n| j ¡ | _d| _d S )Nra   r   T)rN   r    rb   rf   r@   r.   )r   r+   rN   r   r   r   Ú_fix_keywordF  s
    zCard._fix_keywordc          	   C   sx  d}|   ¡ \}}| j |¡}|dkrxy(| dd¡\}}| ¡ | _| ¡ | _W n ttfk
rj   || _Y nX | j	| _
dS | d¡dk	rÈ| j | d¡¡}t| d¡tdƒ}| d¡dk	rÆ| d¡| }n | d¡dk	rh| j | d	¡¡}t| d¡tdƒ}| d¡dk	r| d¡| }| j | d
¡¡}	t|	 d¡tdƒ}
|	 d¡dk	r\|	 d¡|
 }
d ||
¡}|| _
d| _dS )z7Fix the card image for fixable non-standard compliance.Nú/r   r„   r…   r   r†   r‡   rˆ   r‰   z({}, {})T)r   rŽ   rD   rb   rI   r,   r-   rE   Ú
IndexErrorr!   r/   rp   r’   r   ÚFIX_FP_TABLErL   r.   )r   r,   r+   r›   r`   r-   r„   rˆ   r–   r‰   r—   r   r   r   Ú
_fix_valueN  s:    
zCard._fix_valuec             C   sX   | j rL| jr(dj| j  dd¡d tdS | jr:d | j ¡S dj| j tdS ndt S d S )Nz{:{len}}ra   r   r   )rA   zHIERARCH {} r   )r+   rN   rL   rb   rB   r%   )r   r   r   r   Ú_format_keywordv  s    zCard._format_keywordc             C   s¤   t tjttjf}| j}| j}| j| jkr2t	|ƒ}nR| j
rX| jsXt| j|ƒrXd | j
¡}n,| jr|t| jƒ ¡ }d | j|¡}nt|ƒ}| js t| jƒtkr | ¡ }|S )Nz{:>20}z'{}: {}')rO   rV   rW   rT   rX   r,   r!   r+   ru   r>   r/   r0   r=   rL   rN   Ú_format_valuerI   rA   rB   )r   Zfloat_typesr,   r   r   r   r¢   ‚  s    
zCard._format_valuec             C   s   | j s
dS d | j¡S d S )Nr   z / {})r-   rL   r"   )r   r   r   r   Ú_format_comment   s    zCard._format_commentc             C   s$  |   ¡ }|  ¡ }| ¡ | jk}|r(d}n|  ¡ }t}|r<d}d ||||g¡}t|ƒt|ƒ t|ƒ }|| jkrÀ| 	d¡rÀ|| jd kr°|d dkr°d |d d… |||g¡}nt
d | j¡ƒ‚t|ƒ| jkrÚd |¡}nFt| jtƒrt|ƒ| jd kr|  ¡ }nt d	t¡ |d tj… }|S )
Nr   r€   r   r   r   z2The header keyword {!r} with its value is too longz{:80}r™   z,Card is too long, comment will be truncated.)r¡   r¢   rI   ru   r£   r{   rf   rA   rŠ   Ú
startswithrE   rL   r+   r=   r,   r>   Ú_format_long_imagerJ   rK   r
   r   )r   r+   r,   Zis_commentaryr-   Z	delimiterÚoutputZkeywordvalue_lengthr   r   r   rh   ¦  s4    



zCard._format_imagec             C   s  | j tjkr|  ¡ S d}d}g }| j dd¡}t||ƒ}xpt|ƒD ]d\}}|dkrddj| j t	d}nd}| j
s„|t|ƒd	 kr„d
}	nd}	|	 |¡}| d || ¡¡ qBW d}
| j
rt| j
|ƒ}xLt|ƒD ]@\}}|t|ƒd	 krêd}nd}||
 |¡ }| d |¡¡ qÌW d |¡S )a5  
        Break up long string value/comment into ``CONTINUE`` cards.
        This is a primitive implementation: it will put the value
        string in one block and the comment string in another.  Also,
        it does not break at the blank space between words.  So it may
        not look pretty.
        éC   é@   rz   z''r   z
{:{len}}= )rA   z
CONTINUE  r   z'{}'z'{}&'z{:80}z{}zCONTINUE  '' / zCONTINUE  '&' / r   )r+   r   ru   Ú_format_long_commentary_imager!   rŒ   r   Ú	enumeraterL   rB   r-   rA   r   rf   )r   Zvalue_lengthZcomment_lengthr¦   r,   ZwordsÚidxZwordZheadstrZvalue_formatZcomment_formatr-   r   r   r   r¥   ×  s4    	

zCard._format_long_imagec          	   C   s`   t jt }|  ¡ }g }d}x:|t|ƒk rT| tt | j|||| … ƒƒ¡ ||7 }qW d |¡S )z·
        If a commentary card's value is too long to fit on a single card, this
        will render the card as multiple consecutive commentary card of the
        same type.
        r   r   )	r   rŠ   rB   r¢   rA   r   r>   r+   rf   )r   Úmaxlenr,   r¦   r«   r   r   r   r©     s    
"z"Card._format_long_commentary_imagerK   c             C   sú  d| _ tg ƒ}d | j¡}| jr$|S | j| jkr~| jr~| jd d…  ¡ dkr~| j d¡dkr~| 	| j
|d | j¡|| jd¡ | jrš| jd d…  ¡ d	ks | jr¢n„| jrà|  ¡ d
 }|| ¡ krà| 	| j
|d |¡|| jd¡ | j}| jrü| dd¡d
 }| j |¡s&| 	| j
|d |¡dd¡ |  ¡ \}}| j| jkrl| j |¡s¢| 	| j
|d |¡dd¡ n6| j |¡}|s¢| 	| j
|d | j|¡|| jd¡ | j |¡}|d k	rö| d¡}|d k	rö| j |¡sö| 	| j
|d |¡dd¡ |S )NTz*Fixed {!r} card to meet the FITS standard.r;   z	HIERARCH r   r   z<Card {!r} is not FITS standard (equal sign not at column 8).)Úerr_textÚfix_textZfixr€   r   z$Card keyword {!r} is not upper case.ra   r   zIllegal keyword name {!r}F)r­   ZfixablezUUnprintable string {!r}; commentary cards may only contain printable ASCII charactersz<Card {!r} is not FITS standard (invalid value string: {!r}).r˜   zTUnprintable string {!r}; header comments may only contain printable ASCII characters)r$   r   rL   r+   r&   ru   r#   r@   rw   r   Z
run_optionr    r%   r   rœ   rN   rb   rC   rD   rZ   Ú_value_FSC_RErŽ   rp   )r   ZoptionZerrsr®   r+   r›   r`   r-   r   r   r   Ú_verify  s|    




zCard._verifyc             c   s„   t | jƒtj }xntdtj| tjƒD ]V}t | j||tj … ¡}|dkrb|j ¡ dkrbtdƒ‚t	|j
tƒsvtdƒ‚|V  q&W dS )zÞ
        If the card image is greater than 80 characters, it should consist of a
        normal card followed by one or more CONTINUE card.  This method returns
        the subcards that make up this logical card.
        r   r   z?Long card images must have CONTINUE cards after the first card.z'CONTINUE cards must have string values.N)rA   r#   r   rŠ   Úrangern   r+   r@   r	   r=   r,   r>   )r   Zncardsr«   rm   r   r   r   r‹   u  s    zCard._itersubcards)NNN)rK   )Er   r   r   ÚCARD_LENGTHrŠ   r   ÚcompilerB   rC   ÚIrF   Z_digits_FSCZ_digits_NFSCZ	_numr_FSCZ
_numr_NFSCrL   Z_number_FSC_REr’   rZ   r¯   rŽ   Z_rvkc_identifierZ_rvkc_fieldZ_rvkc_field_specifier_sZ_rvkc_field_specifier_valZ_rvkc_keyword_valZ_rvkc_keyword_val_commrx   ro   r}   ru   r{   rH   r   r3   r5   r7   r9   Úpropertyr+   Úsetterr,   Údeleterrc   rd   r-   rN   r4   rj   Úclassmethodrn   rq   r*   rt   rv   r:   rP   re   r   rœ   r    r¡   r¢   r£   rh   r¥   r©   r°   r‹   r   r   r   r   r   )   sŒ   
	



.2M	"2!(C (14
Zc             C   sp   t | tƒr| S yt| ƒS  ttfk
rj   yt| ƒS  ttfk
rd } ztt|ƒƒ‚W dd}~X Y nX Y nX dS )z
    Converts an a string to an int if possible, or to a float.

    If the string is neither a string or a float a value error is raised.
    N)r=   rO   rS   rE   Ú	TypeErrorr>   )ÚsÚer   r   r   r_   ‹  s    
r_   c             C   sÒ   t | tƒr8| dkrdS |  dd¡}d |¡}d |¡S n–t | ttjfƒrZd t| ƒd ¡S t| ƒrld | ¡S t | t	tj
fƒrŠd t| ƒ¡S t | ttjfƒr¼d	 t| jƒt| jƒ¡}d |¡S t | tƒrÊdS dS d
S )zk
    Converts a card value to its appropriate string representation as
    defined by the FITS format.
    r   z''rz   z'{:8}'z{:20}z{:>20}r   z{:>20d}z({}, {})N)r=   r>   rŒ   rL   rU   rV   rY   r2   r   rO   rW   Ú_format_floatrT   rX   rˆ   r‰   r   )r,   Zexp_val_strZval_strr   r   r   r¢   Ÿ  s&    




r¢   c             C   sÆ   d  | ¡}d|kr$d|kr$|d7 }nNd|krr| d¡\}}|d dkr\|d }|dd… }nd	}d
  ||t|ƒ¡}t|ƒ}|dkrÂ| d¡}|dk r¢|dd… }n |dd||  … ||d…  }|S )z@Format a floating number to make sure it gets the decimal point.z{:.16G}ra   ÚEz.0r   )ú+ú-r   Nr   z{}E{}{:02d}é   )rL   rb   rS   rA   rw   )r,   Z	value_strZsignificandZexponentr†   Zstr_lenr«   r   r   r   r¼   Ä  s"    


 r¼   c             C   sj   t | ƒ}|tjkr| S |tjkrJ|tj }|dkr6| S | dtj|   S n|tj }| dtj|   S dS )z9Pad blank space to the input string to be multiple of 80.r   r   N)rA   r   rŠ   )ÚinputÚ_lenZstrlenr   r   r   rk   ä  s    



rk   )#r   rJ   ZnumpyrV   Úutilr   r   r   r   rg   r   r   r	   r
   r   r   Zastropy.utils.exceptionsr   Ú__all__r>   Ú	maketransrŸ   r“   r²   ri   rB   r{   rG   r   r”   r   r_   r¢   r¼   rk   r   r   r   r   Ú<module>   s:           j% 