U
    \Id                     @   sn   d dl Z d dlZd dlmZ d dlmZ d dlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ G d	d
 d
ZdS )    N)_File)_pad_length)fileobj_name   )BITPIX2DTYPE_BaseHDU)HDUList)
PrimaryHDUc                   @   sD   e Zd ZdZdd Zdd Zdd Zdd	 Zed
d Z	dd Z
dS )StreamingHDUa  
    A class that provides the capability to stream data to a FITS file
    instead of requiring data to all be written at once.

    The following pseudocode illustrates its use::

        header = astropy.io.fits.Header()

        for all the cards you need in the header:
            header[key] = (value, comment)

        shdu = astropy.io.fits.StreamingHDU('filename.fits', header)

        for each piece of data:
            shdu.write(data)

        shdu.close()
    c                 C   s  t |tjrtd| | _t|p(d}tj	|}d}|r`tj
|rZtj|dkrxd}nt|drx|jdkrxd}|rd| jkrtt g}||d nd| jkr,| jjd	d
ddd | jd= d| jkr| jd }|dkrd}nt|}| jjdddd| d d| jkr,| jjddddd t|d| _t }| j|_|| jd | _| j | _| j| _| jdkrd| _nd| _dS )a  
        Construct a `StreamingHDU` object given a file name and a header.

        Parameters
        ----------
        name : path-like or file-like
            The file to which the header and data will be streamed. If opened,
            the file object must be opened in a writeable binary mode such as
            'wb' or 'ab+'.

        header : `Header` instance
            The header object associated with the data to be written
            to the file.

        Notes
        -----
        The file will be opened and the header appended to the end of
        the file.  If the file does not already exist, it will be
        created, and if the header represents a Primary header, it
        will be written to the beginning of the file.  If the file
        does not exist and the provided header is not a Primary
        header, a default Primary HDU will be inserted at the
        beginning of the file and the provided header will be added as
        the first extension.  If the file does already exist, but the
        provided header represents a Primary header, the header will
        be modified to an image extension header and appended to the
        end of the file.
        z0StreamingHDU not supported for GzipFile objects. Fr   TlenSIMPLE	exceptionZXTENSIONZIMAGEzImage extension)afterPCOUNTNAXISznumber of parametersGCOUNTr   znumber of groupsappendN)
isinstancegzipGzipFile	TypeErrorcopy_headerr   ospath
expanduserexistsgetsizehasattrr   r   r	   Zwritetosetstrr   _ffor   Z_writeheaderZ_header_offsettell_data_offsetsize_sizewritecomplete)selfnameheaderfilenamenewfilehdulistZdimZtmp_hdu r.   A/tmp/pip-unpacked-wheel-1sxf5fhv/astropy/io/fits/hdu/streaming.py__init__#   sP    


   zStreamingHDU.__init__c                 C   s   | S Nr.   r(   r.   r.   r/   	__enter__   s    zStreamingHDU.__enter__c                 C   s   |    d S r1   )close)r(   typevalue	tracebackr.   r.   r/   __exit__   s    zStreamingHDU.__exit__c                 C   s   | j  | j }| js&||j | jkr.tdt| jd  |j	j
krLtd|j	jd dkrf| }n|}| j | | j  | j | jkr| j t| jd  d| _| j   | jS )a/  
        Write the given data to the stream.

        Parameters
        ----------
        data : ndarray
            Data to stream to the file.

        Returns
        -------
        writecomplete : int
            Flag that when `True` indicates that all of the required
            data has been written to the stream.

        Notes
        -----
        Only the amount of data specified in the header provided to the class
        constructor may be written to the stream.  If the provided data would
        cause the stream to overflow, an `OSError` exception is
        raised and the data is not written. Once sufficient data has been
        written to the stream to satisfy the amount specified in the header,
        the stream is padded to fill a complete FITS block and no more data
        will be accepted. An attempt to write more data after the stream has
        been filled will raise an `OSError` exception. If the
        dtype of the input data does not match what is expected by the header,
        a `TypeError` exception is raised.
        zCAttempt to write more data to the stream than the header specified.BITPIXz>Supplied data does not match the type specified in the header.r   > T)r"   r#   r$   r'   nbytesr&   OSErrorr   r   Zdtyper)   r   r!   ZbyteswapZ
writearraywriter   flush)r(   datar%   outputr.   r.   r/   r>      s$    

zStreamingHDU.writec           
      C   s   d}| j dd}|dkr| j dd}| j dd}|dkrL|dkrLd}nd}d}t||D ]}|| j dt|d    }q^| j d }| j d	d}| j d
d}	t|| |	|  d }|S )zL
        Return the size (in bytes) of the data portion of the HDU.
        r   r   r   FZGROUPSTr   r9   r   r      )r   getranger!   abs)
r(   r%   ZnaxissimpleZrandom_groupsgroupsidxZbitpixZgcountZpcountr.   r.   r/   r%      s     
zStreamingHDU.sizec                 C   s   | j   dS )z/
        Close the physical FITS file.
        N)r"   r4   r2   r.   r.   r/   r4      s    zStreamingHDU.closeN)__name__
__module____qualname____doc__r0   r3   r8   r>   propertyr%   r4   r.   r.   r.   r/   r
      s   c:
r
   )r   r   Zastropy.io.fits.filer   Zastropy.io.fits.headerr   Zastropy.io.fits.utilr   baser   r   r-   r   imager	   r
   r.   r.   r.   r/   <module>   s   