
    HR-eS                         d dl Z d dlZd dlmZmZmZ d dlmZm	Z	 d dl
mZmZ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e	          Z G d de          Z G d dee          Zd Zd ZdS )    N)
FITS2NUMPYColDefsColumn)FITS_recFITS_record)_is_int_is_pseudo_integer_pseudo_zero)lazyproperty   )DELAYEDDTYPE2BITPIX)
PrimaryHDU)_TableLikeHDUc                   r     e Zd ZdZd
 fd	Zed             Zed             Zed             Z	d Z
d	 Z xZS )Groupz-
    One group of the random group data.
    r   Nc                 T    t                                          ||||||           d S N)super__init__)selfinputrowstartendstepbase	__class__s          :lib/python3.11/site-packages/astropy/io/fits/hdu/groups.pyr   zGroup.__init__   s+    UCt<<<<<    c                     | j         j        S r   )arrayparnamesr   s    r   r#   zGroup.parnames   s    z""r    c                 V    |                      | j        j        j        d                   S )N)fieldr"   _coldefsnamesr$   s    r   dataz
Group.data   s#     zz$*-3B7888r    c                 *    t          | j                  S r   _par_indicesr#   r$   s    r   _uniquezGroup._unique"       DM***r    c                    t          |          r| j        | j                 |         }n| j        |                                         }t          |          dk    r| j        | j                 |d                  }nY| j        | j                 |d                                      d          }|dd         D ]}|| j        | j                 |         z  }|S )z0
        Get the group parameter value.
        r   r   f8N)r   r"   r   r.   upperlenastyper   parnameresultindxis        r   parz	Group.par&   s     7 	6Z)'2FF<0D4yyA~~DH-d1g6 DH-d1g6==dCCabb 6 6Adj2155FFr    c                 F   t          |          r|| j        | j                 |<   dS | j        |                                         }t          |          dk    r|| j        | j                 |d         <   dS t          |t          t          f          rbt          |          t          |          k    rBt          t          |                    D ]#}||         | j        | j                 ||         <   $dS t          d                    t          |                              )z0
        Set the group parameter value.
        r   r   z:Parameter value must be a sequence with {} arrays/numbers.N)r   r"   r   r.   r2   r3   
isinstancelisttuplerange
ValueErrorformat)r   r6   valuer8   r9   s        r   setparzGroup.setpar9   s    7 	,1DJtx )))<0D4yyA~~05
48$T!W---
 edE]33 D		SZZ8O8O"3t99-- A A8=a
48,T!W55A A %--3VCII->->  r    )r   NNNN)__name__
__module____qualname____doc__r   propertyr#   r*   r   r.   r:   rC   __classcell__r   s   @r   r   r      s         = = = = = = # # X# 9 9 X9 + + \+  &      r    r   c                   z     e Zd ZdZeZdddg ddddfdZ fdZ fdZe	d             Z
ed             Zd Z xZS )		GroupDatazz
    Random groups data object.

    Allows structured access to FITS Group data in a manner analogous
    to tables.
    Nc	           	         t          |t                    s|d}	nt          |          }	dg|	z  dg|	z  |d t          |	          D             }t          |          |	k    rt	          d          t          |dgz             |t          |j        j                 }t          j
        |         t                   }
t          |j        dd                    |
 }d                    |
g|	z  |gz             }|j        d         }fdt          |	          D             }|                    t!          d	         ||
                     t#          |          }t          j        | t&          j                            d||j        |                    }d	         |_        ||_        ||_        t5          dd	                   D ]~\  }}||         }|                    |          dd         \  }}|s|r|                    |||                    N||         t&          j        j                            ||          dd<   ||j                 }|                    |          dd         \  }}|s|r|                    |j        |           nG|t&          j        j                            ||	          dd<   nt          j        | |          }d|_        |S )a  
        Parameters
        ----------
        input : array or FITS_rec instance
            input data, either the group data itself (a
            `numpy.ndarray`) or a record array (`FITS_rec`) which will
            contain both group parameter info and the data.  The rest
            of the arguments are used only for the first case.

        bitpix : int
            data type as expressed in FITS ``BITPIX`` value (8, 16, 32,
            64, -32, or -64)

        pardata : sequence of array
            parameter data, as a list of (numeric) arrays.

        parnames : sequence of str
            list of parameter names.

        bscale : int
            ``BSCALE`` of the data

        bzero : int
            ``BZERO`` of the data

        parbscales : sequence of int
            list of bscales for the parameters

        parbzeros : sequence of int
            list of bzeros for the parameters
        Nr   c                     g | ]
}d |dz    S )PARr    ).0idxs     r   
<listcomp>z%GroupData.__new__.<locals>.<listcomp>   s"    DDDO#'OODDDr    zLThe number of parameter data arrays does not match the number of parameters.DATAr   ,c           	      Z    g | ]'}t          |         |         |                    (S )namerA   bscalebzeror   )rQ   rR   fits_fmt
parbscales	parbzerosunique_parnamess     r   rS   z%GroupData.__new__.<locals>.<listcomp>   sS         (-#%c?#C.	    r    r&   rW   )formatsr)   shape      )r<   r   r3   r?   r@   _unique_parnamesr   dtyperX   	GroupsHDU_bitpix2tformr   strra   joinappendr   r   __new__nprecr"   r)   _data_fieldr(   r#   	enumerate_get_scale_factors_cache_fieldrecarrayr'   )clsr   bitpixpardatar#   rY   rZ   r]   r^   nparsrA   data_fmtr`   gcountcolscoldefsr   rR   rX   columnscalezeror\   r_   s          ``             @@r   rk   zGroupData.__new__`   s@   T %** S	!G!"Ve^
 !FUN	DDuU||DDD8}}%% :  
 /x6(/BCCO~%ek&67 .v6H)Fek!""o..888Hhh5 0XJ>??G[^F       !<<  D KK(,#!	     dmmG#T7'-vVV D  /r2D#DM$DM&ss';<< 
G 
G	T 
 #55f==acBt GD G%%dGCL9999:A#,BFO))$44QQQ77T-.F11&99!A#>KE4 > >!!$"2E::::8=%%dE2211155#C//D DMr    c                     t                                          |           t          |t                    r|j        | _        d S t          |t
                    r|j        j        | _        d S d S r   )r   __array_finalize__r<   rL   r#   r   r(   r)   )r   objr   s     r   r   zGroupData.__array_finalize__   sf    ""3'''c9%% 	/LDMMMX&& 	/L.DMMM	/ 	/r    c                     t                                          |          }t          |t                    r| j        |_        |S r   )r   __getitem__r<   rL   r#   )r   keyoutr   s      r   r   zGroupData.__getitem__   s:    gg!!#&&c9%% 	)=CL
r    c                 L    |                      | j        j        d                   S )zf
        The raw group data represented as a multi-dimensional `numpy.ndarray`
        array.
        r&   )r'   r(   r)   r$   s    r   r*   zGroupData.data   s      zz$--b1222r    c                 *    t          | j                  S r   r,   r$   s    r   r.   zGroupData._unique   r/   r    c                    t          |          r|                     |          }n| j        |                                         }t	          |          dk    r|                     |d                   }nS|                     |d                                       d          }|dd         D ]}||                     |          z  }|S )z1
        Get the group parameter values.
        r   r   r1   N)r   r'   r.   r2   r3   r4   r5   s        r   r:   zGroupData.par   s     7 	,ZZ((FF<0D4yyA~~DG,, DG,,33D99abb , ,Adjjmm+FFr    )rD   rE   rF   rG   r   _record_typerk   r   r   rH   r*   r   r.   r:   rI   rJ   s   @r   rL   rL   V   s          L ~ ~ ~ ~@/ / / / /     3 3 X3 + + \+      r    rL   c                       e Zd ZdZdddddddZeZd	Z	 d fd	Ze	d             Z
ed             Zed             Zed             Zed             Zed             Zed             Zed             Zd Zd Zd fd	Z fdZ fdZ xZS )rf   z
    FITS Random Groups HDU class.

    See the :ref:`astropy:random-groups` section in the Astropy documentation
    for more details on working with this type of HDU.
    BIJKED)          @   iirT   Nc                    t                                          ||           |t          ur|                                  t	          | j                  dk    r1dg| _        d| j        d<   | j                            ddd           d S d S )N)r*   headerr   r   NAXISNAXIS1after)r   r   r   update_headerr3   _axes_headerset)r   r*   r   r   s      r   r   zGroupsHDU.__init__  s    d6222w    tz??aDJ$%DL!LXq88888  r    c                 N    |j         d         j        }|dk    od|v o	|d         du S )Nr   SIMPLEGROUPST)cardskeyword)rs   r   r   s      r   match_headerzGroupsHDU.match_header*  s5    ,q/)("Vx6'9VfX>NRV>VVr    c                 ~    | j         dgk    rdS |                                 }| j        |_        | j        |_        | `|S )zb
        The data of a random group FITS file will be like a binary table's
        data.
        r   N)r   _get_tbdatacolumnsr(   r#   )r   r*   s     r   r*   zGroupsHDU.data/  sE     :!F!!Lr    c                 T      j         d         } fdt          |          D             S )z=The names of the group parameters as described by the header.PCOUNTc                 P    g | ]"}j         d t          |dz             z            #S )PTYPEr   )r   rh   )rQ   rR   r   s     r   rS   z&GroupsHDU.parnames.<locals>.<listcomp>D  s/    NNNWs37||34NNNr    )r   r?   )r   pcounts   ` r   r#   zGroupsHDU.parnames>  s4     h' ONNNfNNNNr    c           
         | j         r!t          | j        d          r| j        j        S | j        | j        d                  }| j        d         }g }g }g }t          |          D ]}|                    | j                            dt          |dz             z   d                      |                    | j                            dt          |dz             z   d                      |                    | j        dt          |dz             z                       |gt          |          z  }d gt          |          z  }|                    d           |                    | j                            d	                     |                    | j                            d
                     | j        d d         }	|                    t          t          t          j        |	                              |z              |                    |	           t          |          }|d         | _        d t#          |||||          D             }
t%          |
          }|S )Nr(   BITPIXr   PSCALr   PZEROr   rT   BSCALEBZEROSr&   c           	      B    g | ]\  }}}}}t          |||||           S ))rX   rA   rY   rZ   dimr[   )rQ   rX   fmtrY   rZ   r   s         r   rS   z%GroupsHDU.columns.<locals>.<listcomp>e  sE     
 
 
-c65# Su#NNN
 
 
r    )	_has_datahasattrr*   r(   rg   r   r?   rj   getrh   r3   ra   intrl   prodrd   rn   zipr   )r   rA   r   r#   bscalesbzerosrR   r`   r   
data_shapery   rz   s               r   r   zGroupsHDU.columnsF  s-   > 	&gdi<< 	&9%%#DL$:;h'== 	B 	BCNN4<++Gc#'ll,BDIIJJJMM$,**7Sq\\+A4HHIIIOODL3sQw<<)?@AAAA(S]]*fs8}}$ 	t|''11222dl&&x00111Z_
s3rwz223344v=>>>

:#H--#B<
 
14'7FC2 2
 
 
 $--r    c                 n    | j         s| j                            dd          S t          | j                  S )NGCOUNTr   )_data_loadedr   r   r3   r*   r$   s    r   _nrowszGroupsHDU._nrowso  s5      	" <##Ha000ty>>!r    c                     dS Nr   rP   r$   s    r   _theapzGroupsHDU._theapx  s	     qr    c                     dS )NFrP   r$   s    r   is_imagezGroupsHDU.is_image}  s    ur    c                 x   d}| j                             dd          }|dk    rd}t          d|          D ]%}|| j         dt          |dz             z            z  }&| j         d         }| j                             dd          }| j                             dd          }t	          |          |z  ||z   z  dz  }|S )zE
        Returns the size (in bytes) of the HDU's data part.
        r   r   r   r   r   r   r   )r   r   r?   rh   abs)r   sizenaxisrR   rt   rx   r   s          r   r   zGroupsHDU.size  s    
   !,, 199DQ C Cdl7Sq\\+ABB\(+F\%%h22F\%%h22Fv;;'6D=9Q>Dr    c           
         | j                             dd          }| j        rt          | j        t
                    rt          | j        j        j                  dd          | _        | j        	                                 dg| j        z   | _        | j        j
        j        d         }| j        j
        j        |         d         j        }n!| j        dg| _        d}nt          d          t          |         | j         d<   t!          | j                  | j         d<   t#          | j                  D ]P\  }}|dk    rd}ndt%          |          z   }| j                             dt%          |dz             z   ||           Qt)          t!          | j                  dz   |dz             D ]+}	 | j         dt%          |          z   = # t*          $ r Y (w xY w| j        r+t          | j        t
                    r| j                             dd	dt%          t!          | j                            z              | j                             d
t!          | j        j                  d           | j                             dt!          | j                  d
           | j        j        | j                 }| j                            |          dd         \  }}	|r | j                             d|j                   |	r | j                             d|j                   t#          | j        j                  D ]\  }}
| j                             dt%          |dz             z   |
           | j        j        |         }| j                            |          dd         \  }}	|r3| j                             dt%          |dz             z   |j                   |	r3| j                             dt%          |dz             z   |j                   d| j         v rYt!          | j                  r%dt%          t!          | j                            z   }nd}| j                             d|           d S d S )Nr   r   r   uint8zincorrect array typer   r   r   Tr   r   rb   rc   r   BZEROr   r   r   EXTEND)r   r   r   r<   r*   rL   r=   ra   r   reversere   r)   fieldsrX   r@   r   r3   ro   rh   r   r?   KeyErrorr   r#   r(   rn   rp   rY   rZ   )r   	old_naxisfield0field0_coderR   axisr   r{   r|   r}   rX   s              r   r   zGroupsHDU.update_header  s.   L$$Wa00	 	?$)Y// 
9!$)."677;

""$$$S4:-
.q1"io4V<Q?D"S
% !7888%1+%>DL" #DJW #4:.. 	H 	HICaxx#c((*LWs37||3TGGGG TZ1,i!m<< 	 	CL3s88!344    > 	KjI>> 	KLXt7STZ=Q=Q3QRRRLXs49+='>'>hOOOLXs49~~XFFFY'(89F)66v>>qsCKE4 :  6=999 8  &,777&ty'9:: K K	T  3sQw<<!7>>>+C0"i::6BB1Q3Gt LL$$Ws37||%;V]KKK KL$$Ws37||%;V\JJJ t|##4:  #c$*oo"6"66LXU33333 $#s   -G
GGc                 B   d}| j         | j                                          t          j        dk    rd}nd}t	          | j         j                  rJt          j        | j         t          | j         j                  z
  d| j         j        j	                   }d}nJ| j         }| j         j        j
        d         }| j         j        j        |         d         j        d         }||v }|r|j        j        r[|                    d	           	 |                    |           |                    d	           nX# |                    d	           w xY w|                    |                    d                     n|                    |           ||j        |j	        z  z  }|S )
a,  
        Basically copy/pasted from `_ImageBaseHDU._writedata_internal()`, but
        we have to get the data's byte order a different way...

        TODO: Might be nice to store some indication of the data's byte order
        as an attribute or function so that we don't have to do this.
        r   Nlittle)<=)r   z>i)re   FT)r*   _scale_backsys	byteorderr	   re   rl   r"   r
   itemsizer)   r   rh   flags	writeablebyteswap
writearrayr   )r   fileobjr   
swap_typesoutputshould_swapfnamer   s           r   _writedata_internalzGroupsHDU._writedata_internal  s    9 I!!### }(('

#
!$)/22 6ITY_ = ==9ty799   $	-a0 IO259!<@C	':5 +<) 	?OOD))).**6222-------- &&vu'='=>>>>""6***FK&/11Ds   
D5 5Ewarnc                    t                                          |          }|                     ddd d||           |                     ddd d	||           | j        d         dz   fd
}|                     d|t          d||           |                     d|t          d	||           |                     d|d d||           |S )N)optionr      c                 @    t          |           od| cxk    odk    nc S )Nr   i  r   vs    r   <lambda>z#GroupsHDU._verify.<locals>.<lambda>  s%    71::#?!q----C---- r    r   r   rb   c                 ,    t          |           o| dk    S r   r   r   s    r   r   z#GroupsHDU._verify.<locals>.<lambda>  s    wqzz/Da1f r    r   c                     | k    S r   rP   )xr   s    r   r   z#GroupsHDU._verify.<locals>.<lambda>  s    U
 r    r   r   r   c                 
    | du S )NTrP   r   s    r   r   z#GroupsHDU._verify.<locals>.<lambda>  s
    d r    T)r   _verify	req_cardsr   r   )r   r   errsposr   r   s       @r   r   zGroupsHDU._verify  s    wwf-- 	Q@@!VT	
 	
 	
 	x$E$Eq&RVWWWW%)""""xgq&$???xgq&$???x&;&;T64PPPr    c                    | j         rM| j        j        j        | j        j        j        d                  d         j        d         }|dk    r| j        j        j        r<d}| j                            d          }|j        	                    d          |_        nE| j                            d          }|j        	                    d          |_        d}n	d}| j        }|
                    t          j        t          j                  }|                     |          }|r4|                    d           |j        	                    d          |_        |S t                                                      S )zJ
        Calculate the value for the ``DATASUM`` card in the HDU.
        r   >TF)typere   r   )r   r*   re   r   r)   rh   r   r   r   newbyteorderviewrl   ndarrayubyte_compute_checksumr   _calculate_datasum)r   r   byteswappedd	byte_datacsr   s         r   r  zGroupsHDU._calculate_datasum  sF    > (	0 	.ty/DQ/GHKOPQRIC9?, 	("&K	**400Ag22377AGG 	**511Ag22377AG"'KK#IBJbh??I''	22B  4

4   '..s33I 77--///r    c                     t                                                      }|\  }}}}}}}|r2|dd          }|r&t          |          r| j        d         j        j        }| j         d| j         d}|||||||fS )Nr   r   z	 Groups  z Parameters)r   _summaryallr   re   rX   _gcount_pcount)
r   summaryrX   ver	classnamelengthra   rA   rx   r   s
            r   r  zGroupsHDU._summaryF  s    ''""$$>E;c9feVV  	4!""IE 4U 4a.3 LDD4<DDDc9feVVDDr    )NN)r   )rD   rE   rF   rG   rg   rL   
_data_typern   r   classmethodr   r   r*   r#   r   rH   r   r   r   r   r   r   r   r  r  rI   rJ   s   @r   rf   rf     s         #3SsKKMJK	9 	9 	9 	9 	9 	9 W W [W   \ O O \O & & \&P " " X"   \   X   X$?4 ?4 ?4B0 0 0d     ",0 ,0 ,0 ,0 ,0\E E E E E E E E Er    rf   c                     i }t          |           D ]?\  }}|                                }||v r||                             |           9|g||<   @|S )z
    Given a list of objects, returns a mapping of objects in that list to the
    index or indices at which that object was found in the list.
    )ro   r2   rj   )r)   uniquerR   rX   s       r   r-   r-   W  sg    
 Fu%% ! !	Tzz||6>>4L$$$$5F4LLMr    c                     t                      }g }| D ]R}|                                }||v rd|z   }d|z   }||v |                    |           |                    |           S|S )z
    Given a list of parnames, including possible duplicates, returns a new list
    of parnames with duplicates prepended by one or more underscores to make
    them unique.  This is also case insensitive.
    _)r   r2   rj   add)r)   upper_namesunique_namesrX   
name_uppers        r   rd   rd   g  s     %%KL $ $ZZ\\
K'':Dz)J K'' 	D!!!
####r    )r   numpyrl   astropy.io.fits.columnr   r   r   astropy.io.fits.fitsrecr   r   astropy.io.fits.utilr   r	   r
   astropy.utilsr   r   r   r   imager   tabler   r   rL   rf   r-   rd   rP   r    r   <module>r      s   


     > > > > > > > > > > 9 9 9 9 9 9 9 9 J J J J J J J J J J & & & & & & ' ' ' ' ' ' ' '                  B B B B BK B B BJu u u u u u u upFE FE FE FE FE
M FE FE FER
       r    