
    HR-e{c                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlmZ d dlZd dl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lmZ dd	lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( erd dl)Z)d
d
dddd
dZ*dddddddZ+ ej,        d          Z-ej.        ej.        ej/        ej.        ej0        dZ1dZ2dZ3dZ4d Z5d Z6 G d d          Z7dS )    N)reduce)HAS_BZ2)_is_url_requires_fsspecdownload_fileget_readable_fileobj)classproperty)AstropyUserWarning)NOT_OVERWRITING_MSG   )
_array_from_file_array_to_file_write_stringfileobj_closedfileobj_modefileobj_nameisfile
isreadable
iswritable	path_likerbrb+ab+wb)readonlycopyonwriteupdateappendostream	denywriter   r   r   r   )r   r   r   zwb+abr   z^[rwa]((t?\+?)|(\+?t?))$)r   r   r   r   r    s   s   PKs   BZc                 H    t           rt          | t          j                  S dS NF)r   
isinstancebz2BZ2File)fileobjs    4lib/python3.11/site-packages/astropy/io/fits/file.py_is_bz2filer)   g   s!     '3;///u    c                     | n| t           vret                              |           rt          d|  d          t                              |           }|t           vrt          d|  d          |} | S )NzText mode 'z4' not supported: files must be opened in binary modeMode '' not recognized)IO_FITS_MODESTEXT_REmatch
ValueError
FILE_MODESget)modenew_modes     r(   _normalize_fits_moder6   n   s    D55== 	XdXXX   >>$''=((<d<<<===Kr*   c                       e Zd ZdZ	 	 	 	 	 d ddddZd Zd Zd	 Zd
 Zd!dZ	dde
j        dfdZd Zd Zd Zd Zd"dZd Zd!dZd Zd"dZd Zd#dZd Zd Zd Z ed          d             Zd ZdS )$_FilezM
    Represents a FITS file on disk (or in some other file-like object).
    NFT)
use_fsspecfsspec_kwargsc                   t          |          | _        |dn|}d | _        d| _        d| _        || _        || _        d | _        d| _        d| _	        d| _
        d | _        |	d| _        d S d| _        t          |t          j                  rt          j        |          }||t"          vrt%          d| d          t'          |          rIt)          t+          |                    }|)||k    r#t%          d                    ||                    |}|d}|st/          |          r,|dk    r&t1          |d||d	                                          }t          |t4          t6          f          r*|d
vr&t9          |          rt;          ||          | _        nt          |t>          j         j!                  r>|d
v rt%          d| d          tE          j#        |$                                          }nHt          |tJ                    rt          j&        '                    |          }tQ          |          | _        || _        d| _)        t'          |          r| *                    |||           nKt          |t4          t6          f          r| +                    |||           n| ,                    |||           t+          | j                  | _        t          |tZ          j.                  rd| _        n8t          |t^          j0                  rd| _        ntc          |          rd| _        |dv s| j        r|dk    rd| _        n|dk    s| j        r|dk    rd| _	        |dk    s| j        ste          | j        d          sd| _3        nl| j        4                                }	| j        5                    dd           | j        4                                | _3        | j        5                    |	           | j        r4t'          | j                  s	d| _        d S | j        s| j6        sd| _        d S d S d S d S )NTFr,   r-   zGRequested FITS mode '{}' not compatible with open file handle mode '{}'r   r   binary)encodingr9   r:   close_files)r   r   r   )cachezMode z not supported for HTTPResponsegzipzipbzip2)r   r   r    r   r   seekr      )7boolstrict_memmap_fileclosedr<   r4   memmapcompressionr   	writeonlyclose_on_error_mmapsimulateonlyr$   osPathLikefspathr.   r1   r   r6   r   formatr   r   	__enter__strbytesr   r   namehttpclientHTTPResponseioBytesIOreadr   path
expanduserr   	file_like_open_fileobj_open_filename_open_fileliker@   GzipFilezipfileZipFiler)   hasattrsizetellrC   _mmap_available)
selfr'   r4   rI   	overwriter?   r9   r:   objmodeposs
             r(   __init__z_File.__init__   s    "&\\V
	 $ 
? $DF %D'2;// -)G,,M 9 9<d<<<==='?? 	*<+@+@AAGDGOO ''-vdG'<'<   D<D  		*733 		9J9J +!%+!   ikk  we--	.;;;   < &gU;;;DII!9:: 	.666 !N!N!N!NOOOj00GG'9-- 6',,W55$W--DI	  '?? 	:wi8888#u.. 	:y9999y999(44gt}-- 	'%D11 	'$D!! 	'&D;;; <!%!1!1 DMMY4#38H8H!DN 9 0
F8S8S DII*//##CJOOAq!!!
))DIJOOC   ; 	$$*%% $#] $4+? $ $	$ 	$$ $ $ $r*   c                 B    d| j          d| j        j         d| j         dS )N<. >)
__module__	__class____name__rG   rj   s    r(   __repr__z_File.__repr__  s,    L4?LLT^%<LLtzLLLLr*   c                     | S N rw   s    r(   rS   z_File.__enter__  s    r*   c                 .    |                                   d S rz   )close)rj   typevalue	tracebacks       r(   __exit__z_File.__exit__	  s    

r*   c                 <    | j         rdS t          | j                  S r#   )rK   r   rG   rw   s    r(   readablez_File.readable  s!    > 	5$*%%%r*   c                     t          | j        d          st          	 | j                            |          S # t          $ r | j        dk    rY dS  w xY w)Nr\   r@    )rf   rG   EOFErrorr\   OSErrorrJ   rj   rg   s     r(   r\   z
_File.read  si    tz6** 	N	:??4((( 	 	 	 6))rr	s   8 AAr   c                 ~   t          | j        d          st          t          |t          j                  st	          j        |          }|r#||j        z  dk    rt          d| d|           t          |t                    r|f}|s|st          j
        dt                     d}|r|s||j        z  f}|rZ|rXt	          j        |          |j        z  }||k    rt          d| d| d|           ||k     rt          d| d	| d|           | j                                        }	 | j        rL| j        t           | j                 }| j                            dd           	 t'          j        | j                                        d|d          | _        n# t*          $ r}|j        t,          j        k    re| j        dk    rZt          j
        dt                     t'          j        | j                                        dt           d         d          | _        n Y d
}~nd
}~ww xY wt	          j        |||| j                  | j                                         | j                            |           S t3          t4          j        |          }	| j                            |           t9          | j        ||	          }
||
_        |
| j                                         | j                            |           S # | j                                         | j                            |           w xY w)a*  
        Similar to file.read(), but returns the contents of the underlying
        file as a numpy array (or mmap'd array if memmap=True) rather than a
        string.

        Usually it's best not to use the `size` argument with this method, but
        it's provided for compatibility.
        r\   r   zsize z not a multiple of z?No size or shape given to readarray(); assuming a shape of (1,))r   z is too few bytes for a z
 array of z is too many bytes for a N)accessoffsetr   zCould not memory map array with mode='readonly', falling back to mode='denywrite', which means that the array will be read-onlyr    )shapedtyper   buffer)rf   rG   r   r$   npr   itemsizer1   intwarningswarnr
   prodrh   rI   rM   MEMMAP_MODESr4   rC   mmapfilenor   errnoENOMEMndarrayr   operatormulr   r   )rj   rg   r   r   r   
actualsizefileposaccess_modeexccountdatas              r(   	readarrayz_File.readarray  s    tz6** 	N%** 	$HUOOE 	GD5>)Q..ETEEeEEFFFeS!! 	HE 	 	MQ"   E 	. 	.U^+-E 
	E 
	%.8JD   RDRR%RR5RR   d"" SDSS5SSESS   *//##?	%{ 6:% #/ty"9K JOOAq)))"%)Y J--//;q& & &

 # " " " 944j9P9P$M!> !3   *. $
 1 1 3 3 !'3K'@'(	* * *DJJ " 'JJJJ#"4 zuVDJ   JOOJOOG$$$$ x|U33
''''
E5AA"
 JOOJOOG$$$$ JOOJOOG$$$$s>   -<L *4F L 
H.)A;H)$L )H..L AL 5L<c                 <    | j         rdS t          | j                  S r#   )r   r   rG   rw   s    r(   writablez_File.writable  s!    = 	5$*%%%r*   c                 p    | j         rd S t          | j        d          rt          | j        |           d S d S )Nwrite)rN   rf   rG   r   )rj   strings     r(   r   z_File.write  sH     	F4:w'' 	.$*f-----	. 	.r*   c                 p    | j         rdS t          | j        d          rt          || j                   dS dS )z
        Similar to file.write(), but writes a numpy array instead of a string.

        Also like file.write(), a flush() or close() may be needed before
        the file on disk reflects the data written.
        Nr   )rN   rf   rG   r   )rj   arrays     r(   
writearrayz_File.writearray  sJ      	F4:w'' 	.5$*-----	. 	.r*   c                 x    | j         rd S t          | j        d          r| j                                         d S d S )Nflush)rN   rf   rG   r   rw   s    r(   r   z_File.flush  sJ     	F4:w'' 	J	 	r*   c                 .   t          | j        d          sd S | j                            ||           | j                                        }| j        r@|| j        k    r7t          j        d                    | j        |          t                     d S d S d S )NrC   z\File may have been truncated: actual file length ({}) is smaller than the expected size ({}))	rf   rG   rC   rh   rg   r   r   rR   r
   )rj   r   whencerm   s       r(   rC   z
_File.seek  s    tz6** 	F
'''joo9 	tyM>>DfTYPS>T>T"    	 	r*   c                     | j         rt          t          | j        d          st          | j                                        S )Nrh   )rN   r   rf   rG   r   rh   rw   s    r(   rh   z
_File.tell  s=     	Mtz6** 	Nz   r*   c                 h    t          | j        d          r| j                            |           d S d S )Ntruncate)rf   rG   r   r   s     r(   r   z_File.truncate  s<    4:z** 	&J%%%%%	& 	&r*   c                     t          | j        d          r| j                                         |                                  d| _        d| _        d| _        dS )z1
        Close the 'physical' FITS file.
        r}   NTF)rf   rG   r}   _maybe_close_mmaprM   rH   rL   rw   s    r(   r}   z_File.close  s\     4:w'' 	J    
#r*   c                     | j         Wt          t          d          rDt          j        | j                   d|z   k    r&| j                                          d| _         dS dS dS dS )z
        When mmap is in use these objects hold a reference to the mmap of the
        file (so there is only one, shared by all HDUs that reference this
        file).

        This will close the mmap if there are no arrays referencing it.
        NgetrefcountrD   )rM   rf   sysr   r}   )rj   refcount_deltas     r(   r   z_File._maybe_close_mmap  sm     J"]++ #
++q>/AAAJDJJJ #"""AAr*   c                    | j         rt          |d          r|j        dk    sLt          j                            | j                  rt          j                            | j                  dk    r|r_| j         r't          |d          r|                    d           dS |s|	                                 t          j
        | j                   dS t          t          j        | j                            dS dS )zOverwrite an existing file if ``overwrite`` is ``True``, otherwise
        raise an OSError.  The exact behavior of this method depends on the
        _File object state and is only meant for use within the ``_open_*``
        internal methods.
        lenr   r   N)r_   rf   r   rO   r]   existsrV   getsizer   r}   remover   r   rR   )rj   rk   r'   rH   s       r(   _overwrite_existingz_File._overwrite_existing  s     N 	Eww66 	E7;??GNN49%% <K*,'//$)*D*D*I*I E> )ggz&B&B )$$Q'''''! (Idi(((((18CCDDD <K?*I*Ir*   r   c                    |dk    }|r|dk    s|                     t                    rn|dk    rt          d          t          t          |                   }t          |t                    r||d<   n||d<   t          j        di || _	        d| _
        n|r|d	k    s|                     t                    r#|                     | j        |           d
| _
        ns|r|dk    s|                     t                    rQ|dv rt          d          t          st!          d          |rdnd}t#          j        ||          | _	        d| _
        | j
        duS )z5Attempt to determine if the given file is compressed.r   z.gzr   zH'append' mode is not supported with gzip files.Use 'update' mode instead)r4   filenamer'   r@   z.ziprA   z.bz2r   r   z:update and append modes are not supported with bzip2 filesz9This Python installation does not provide the bz2 module.wrrB   Nr{   )
startswith
GZIP_MAGICr   dictr.   r$   rT   r@   rc   rG   rJ   PKZIP_MAGIC_open_zipfilerV   BZIP2_MAGICr   ModuleNotFoundErrorr%   r&   )rj   obj_or_namemagicr4   ext
is_ostreamkwargs
bzip2_modes           r(   _try_read_compressedz_File._try_read_compressed  s   Y&
 	'3%<<E,<,<Z,H,H<x0  
 }T2333F+s++ 0%0z""$/y!0000DJ%D 	'SF]]u/?/?/L/L]ty$///$D 	'SF]]u/?/?/L/L]+++P    )O   !+3J[zBBBDJ&Dt++r*   c                    t          |          }|dk    r|                     |||           |s|| _        n4t          |          r%t	          | j        t          |                   | _        	 |dv r| j                            d           | j                            d          }| j                            d           n# t          $ r Y dS w xY w| 
                    |||           dS )zAOpen a FITS file from a file object (including compressed files).r   )r   r   r      N)r   r   rG   r   openrV   r.   rC   r\   r   r   )rj   r'   r4   rk   rH   r   s         r(   r`   z_File._open_fileobj  s    (( 9$$Y@@@ 	> DJJG__ 	>dit)<==DJ	 ,,,
"""JOOA&&E
 JOOA 	 	 	FF	 	!!'5$77777s   ,AB? ?
CCc                    d| _         || _        t          |          rt          d|d          t	          |t
          j                  r|                     ||           dS t          | j        d          rt          | j        d          s	dx| _	        }|dk    r| 
                    ||d           | j	        d	v r<t          | j        d
          s't          d                    | j	                            | j	        dk    r<t          | j        d          s)t          d                    | j	                            dS dS )zgOpen a FITS file from a file-like object, i.e. one that has
        read and/or write methods.
        Tz5Cannot read from/write to a closed file-like object (z).NrC   rh   r   F)r   r   r   r   zHFile-like object does not have a 'write' method, required for mode '{}'.r\   zGFile-like object does not have a 'read' method, required for mode {!r}.)r_   rG   r   r   r$   rd   re   r   rf   r4   r   rR   )rj   r'   r4   rk   s       r(   rb   z_File._open_filelike:  sq    
'"" 	UUUU   gw// 	w--- F tz6** 	)'$*f2M2M 	)((DI9$$Y??? 9777JA
 A
7 228&2C2C   9	!!'$*f*E*E!228&2C2C   "!!!r*   c                 f   |dk    r|                      |dd           t          j                            | j                  rCt          | j        d          5 }|                    d          }ddd           n# 1 swxY w Y   nd}t          j                            | j                  d         }|                     | j        |||          s,t          | j        t          |                   | _
        d| _        t          | j
                  r|dk    s| j
                            d	           dS dS )
z(Open a FITS file from a filename string.r   NTr   r   r*   r   )r   r   )r   rO   r]   r   rV   r   r\   splitextr   r.   rG   rL   r)   rC   )rj   r   r4   rk   fr   r   s          r(   ra   z_File._open_filenamed  sU   9$$Yd;;;7>>$)$$ 	di&& "!q		" " " " " " " " " " " " " " " Egty))!,((E4S(II 	'dit)<==DJ"&D
 DJ'' 	DI,=,=JOOA ->,=s   A99A= A=)lazyc                    t          j                    \  }}	 t          j        |d           t          j        |           	 t          j        |dt
          j                  }n_# t          $ rR}t          j	        d| dt                     ~Y d}~t          j        |           t          j        |           dS d}~ww xY w	 |                                 ng# t          $ rZ t          j	        dt                     Y |                                 t          j        |           t          j        |           dS w xY w	 |                                 n# |                                 w xY w	 t          j        |           t          j        |           n-# t          j        |           t          j        |           w xY wd	S )
a  Tests that mmap, and specifically mmap.flush works.  This may
        be the case on some uncommon platforms (see
        https://github.com/astropy/astropy/issues/968).

        If mmap.flush is found not to work, ``self.memmap = False`` is
        set and a warning is issued.
            r   )r   zFailed to create mmap: z; mmap use will be disabledNFzYmmap.flush is unavailable on this platform; using mmap in writeable mode will be disabledT)tempfilemkstemprO   r   fsyncr   ACCESS_WRITEr   r   r   r
   r}   r   r   )clstmpfdtmpnamemmr   s        r(   ri   z_File._mmap_available{  s    ")++w	HUD!!!HUOOOYua0ABBB   NcNNN&   uuu HUOOOIg)




   D&  
 


HUOOOIg  








HUOOOIg HUOOOIgtsq   )F !A$ #F $
C .B;F ;C  F C E $D==E >F <D==E F E,,F *Gc                    |dv rt          d          t          |t          j                  st          j        |          }d}n|}d}|                                }t          |          dk    rt          d          t          j        d          | _        | j        	                    |
                    |d	                              |r|                                 | j                            d	           d
S )zLimited support for zipfile.ZipFile objects containing a single
        a file.  Allows reading only for now by extracting the file to a
        tempfile.
        r   z7Writing to zipped fits files is not currently supportedTFr   z2Zip files with multiple members are not supported.z.fits)suffixr   N)r   r$   rd   re   namelistr   r   NamedTemporaryFilerG   r   r\   r}   rC   )rj   r'   r4   zfiler}   r   s         r(   r   z_File._open_zipfile  s    
 '''STTT'7?33 	OG,,EEEEE>>##x==ANOOO0@@@

HQK00111 	KKMMM 	
r*   )NNNFTrz   )r   )r   )rv   rt   __qualname____doc__rn   rx   rS   r   r   r\   r   uint8r   r   r   r   r   rC   rh   r   r}   r   r   r   r`   rb   ra   r	   ri   r   r{   r*   r(   r8   r8   {   s         @$ @$ @$ @$ @$ @$DM M M    & & &
    "!284 l% l% l% l%\& & &
. . .
. 
. 
.  
 
 
 
! ! !& & & &$ $ $   "E E E(#, #, #, #,J8 8 8B( ( (T  . ]% % %N    r*   r8   )8r   r@   http.clientrW   rZ   r   r   rO   rer   r   r   rd   	functoolsr   numpyr   "astropy.utils.compat.optional_depsr   astropy.utils.datar   r   r   r   astropy.utils.decoratorsr	   astropy.utils.exceptionsr
   astropy.utils.miscr   utilr   r   r   r   r   r   r   r   r   r   r%   r.   r2   compiler/   ACCESS_COPYr   ACCESS_READr   r   r   r   r)   r6   r8   r{   r*   r(   <module>r      s         				   				 				 



              7 6 6 6 6 6            3 2 2 2 2 2 7 7 7 7 7 7 2 2 2 2 2 2                         JJJ    

 
 "*0
1
1  #!  
!  
 
 
B	 B	 B	 B	 B	 B	 B	 B	 B	 B	r*   