
    HR-e9                         d Z ddlZddlZddlmZmZ ddlZddl	m
Z
mZ ddlmZmZmZ  G d de
j                  Z G d	 d
e
j                  Z G d de
j                  Z G d de
j                  ZdS )z
An extensible ASCII table reader and writer.

Classes to read DAOphot table format

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
    N)OrderedDictdefaultdict   )core
fixedwidth)first_false_indexfirst_true_index	groupmorec                       e Zd ZdZdZ ej        d          Z ej        dej                  Z	dZ
d Zd Zd Zd	 Zd
 ZdS )DaophotHeaderzK
    Read the header from a file produced by the IRAF DAOphot routine.
    z\s*#Kz%-?(\d+)\.?\d?[sdfg]z,[#]K\s+ (?P<name> \w+)\s* = (?P<stuff> .+) $ c                 D    t           j                            |            d S N)r   
BaseHeader__init__selfs    8lib/python3.11/site-packages/astropy/io/ascii/daophot.pyr   zDaophotHeader.__init__$   s      &&&&&    c                     d}t          t                    }d }t          t          |j        |           D ]S}t          |t          ||                    D ]2\  }}||                             |                                           3T j        j        rt          t          t          |j        |                     d         \  }}	}
t           j        j
                  }t          j        d|dz                                 d          D ]}t          t          dj        t          |t          j        |                                        }|dk    r||d         d<   V|d                             |           |d	                             |	           |d
                             |
            fdfd|d
         D             }t          j        t          t$          |          t&                    }t(          j        |z
  }t          ||          D ]\  }}|dxx         |z  cc<   | _        d |                                D             }|S )a  Parse a series of column definition lines.

        Examples
        --------
        When parsing, there may be several such blocks in a single file
        (where continuation characters have already been stripped).
        #N ID    XCENTER   YCENTER   MAG         MERR          MSKY           NITER
        #U ##    pixels    pixels    magnitudes  magnitudes    counts         ##
        #F %-9d  %-10.3f   %-10.3f   %-12.3f     %-14.3f       %-15.7g        %-6d
        #N#U#Fc                 <    | dd                               d          S )N   z \)stripss    r   <lambda>z.DaophotHeader.parse_col_defs.<locals>.<lambda>6   s    QqrrU[[// r   r   U2 1r   r   r   c                     t          j                            |                                           d                   S )Nr   )int	re_formatsearchgroups)r   r   s    r   r    z.DaophotHeader.parse_col_defs.<locals>.<lambda>O   s0    #dn&;&;A&>&>&E&E&G&G&J"K"K r   c                 ,    g | ]}fd |D             S )c                 &    g | ]} |          S r   r   ).0fget_col_widths     r   
<listcomp>z;DaophotHeader.parse_col_defs.<locals>.<listcomp>.<listcomp>Q   s#    ///!]]1///r   r   )r,   formatsr.   s     r   r/   z0DaophotHeader.parse_col_defs.<locals>.<listcomp>P   s;     
 
 
4;////w///
 
 
r   c                 6    i | ]\  }}|t          |g           S r   )sum)r,   kvs      r   
<dictcomp>z0DaophotHeader.parse_col_defs.<locals>.<dictcomp>]   s&    GGG!Qq#a**GGGr   )r   listzipmapgetappendsplitdatais_multilinelenfirst_blocknparangeastypejoinittrepeatfromiterr2   r&   Daophottable_width
col_widthsitems)r   grouped_lines_dictline_idscoldef_dictstripperdefblockkeyline
last_names
last_unitslast_formatsN_multilineiextended_namesrI   
row_widths	row_shortwrr.   s   `                  @r   parse_col_defszDaophotHeader.parse_col_defs'   sw    &!$'' 0/S!3!7BBC 	6 	6H 3x+B+BCC 6 6	TC ''

55556
 9! 	; 48S(3344 440J
L di344KYq+/2299$?? ; ;!%c"'3z3:a==3Q3Q&R&R!S!S88,:K%b))%,,^<<<%,,Z888%,,\:::: LKKK
 
 
 
?J4?P
 
 

 [S*!5!5s;;
'*4	
I.. 	 	DAqbEEEQJEEEE$ HG;3D3D3F3FGGGr   c           	         |d         }t          | j                  }|dk    rd }t          t          || j        t	          |                     \  }}t          | \  }}	t          t          ||                    }
d|
v r-t          t          | j        |
d                             }||d<   | 	                    |
          }d}t          t          |j
        |           D ]\  }}}||d|d         |<   || _        |d	         | _        d
S d
S )zy
        Extract table-level keywords for DAOphot table.  These are indicated by
        a leading '#K ' prefix.
        tabler   c                 :    |                      d d          d         S )Nr   r   )r;   r   s    r   r    z+DaophotHeader.update_meta.<locals>.<lambda>n   s    AGGD!$4$4Q$7 r   z#Kkeywordsr   unitformatcolsr   N)r>   linesr7   r
   rangedictr   r8   extract_keyword_liner\   r9   metanames)r   re   ri   
table_metaNlinesget_line_idgidr)   grouped_linesgixrK   r`   rM   rL   namerb   fmts                    r   update_metazDaophotHeader.update_metaa   s4   
 ']
 TZA:: 87K ydj%--PPQKC "%fM3 "&c#}&=&=!>!> )))&13Ed3KLL  *2
:&--.@AAK)H#&KOX(F(F#G C CdC.2c%B%BVT""DI$T*DJJJ= :r   c                 <   | j                             |          }|r|                    d                                                              dd          }|d         |d         t          |          dk    r|d         ndd}|                    d	          |fS dS )
z?
        Extract info from a header keyword line (#K).
        stuffNr   r!   r   r#   )unitsrc   valuerq   )re_header_keywordmatchgroupr   rsplitr>   )r   rQ   mvalskeyword_dicts        r   rh   z"DaophotHeader.extract_keyword_line   s     "((.. 	1777##))++224;;Dbr(%(YY]]$q'' L
 776??L00	1 	1r   c                   	 | j         st          j        d          |                                  | j        d         }| j        D ]?	t          |	j                 j        d          \  }}|dk    r|	_	        |dk    r|	_
        @t          | j        g           }t          j        |          }||z
  }t          | j                  D ]\  }	||         ||         c	_        	_        	j        	j        z
  	_        t'          	d          rbt)          	fddD                       rt          j        	_        qd	j
        v rt          j        	_        d		j
        v rt          j        	_        | j        j                            d
           dS )a  
        Initialize the header Column objects from the table ``lines`` for a DAOphot
        header.  The DAOphot header is specialized so that we just copy the entire BaseHeader
        get_cols routine and modify as needed.

        Parameters
        ----------
        lines : list
            List of table lines

        Returns
        -------
        col : list
            List of table Columns
        z'No column names found in DAOphot headerrd   ra   z##rc   c              3   *   K   | ]}|j         v V  d S r   )rc   )r,   xcols     r   	<genexpr>z)DaophotHeader.get_cols.<locals>.<genexpr>   s)      551qCJ555555r   fgdr   )INDEF0N)rj   r   InconsistentTableError_set_cols_from_namesri   rd   r8   rq   r9   rb   rc   r2   rI   r@   cumsum	enumeratestartendspanhasattrany	FloatTypetypeIntTypeStrTyper<   fill_valuesr:   )
r   re   coldefsrb   rr   	col_widthendsstartsrV   r   s
            @r   get_colszDaophotHeader.get_cols   s     z 	Y-.WXXX 	!!### )F#9 	! 	!CGCH-13EFFID#t||d{{ 
 ,,	y##	!	** 		, 		,FAs!'DGCIsww*CHsH%% ,555555555 ,#~CHHCJ&&#|CHHCJ&&#|CH 		$$^44444r   N)__name__
__module____qualname____doc__commentrecompiler'   VERBOSEry   aperture_valuesr   r\   rs   rh   r   r   r   r   r   r      s          G 
233I"
?  O' ' '8 8 8t'+ '+ '+R1 1 1/5 /5 /5 /5 /5r   r   c                   0    e Zd Zej        ZdZdZd Zd Z	dS )DaophotDatar   z\s*#c                 R    t           j                            |            d| _        d S )NF)r   BaseDatar   r=   r   s    r   r   zDaophotData.__init__   s&    t$$$!r   c                    | j         r_t          t          t          t          j        | j                             }t          t          t          |                    | j	        _
        t          j                            | |           d S r   )r=   nextr7   r8   strr;   r?   tuplefloatheaderr   r   r   get_data_lines)r   re   aplists      r   r   zDaophotData.get_data_lines   so      	D #s39d.>??@AAF*/E60B0B*C*CDK' 	$$T511111r   N)
r   r   r   r   FixedWidthSplittersplitter_class
start_liner   r   r   r   r   r   r   r      sD        2NJG" " "
2 
2 
2 
2 
2r   r   c                   H    e Zd ZdZdZdZ ej        d          Zd	dZ	d Z
dS )
DaophotInputter\* z(#?)[^\\*#]*(\*?)(\\*) ?$   c                 j    t           fd|d|         D              \  }}}t          |          }|dd|d|         fS |d|         }t          |||                   }|dd|fS t          |||z   |                   }	t          j        |||	g          }
||
d         |
d                  }|
||fS )z
        Search lines for special continuation character to determine number of
        continued rows in a datablock.  For efficiency, depth gives the upper
        limit of lines to search.
        c              3   p   K   | ]0}j                             |                                          V  1d S r   )re_multiliner(   r)   )r,   rQ   r   s     r   r   z3DaophotInputter.search_multiline.<locals>.<genexpr>   s@      PP$d&&t,,3355PPPPPPr   Nr   r!   )r7   r   r	   r@   r   )r   re   depthr   specialcont
data_startheader_linesfirst_speciallast_specialmarkersmultiline_blocks   `           r   search_multilinez DaophotInputter.search_multiline   s     "%PPPP%-PPP"
$
 'w//

 uVeV},,[j[) )E1A)BCC |++ )m1Ke1S)TUU )ZEFF
WR[ 8955r   c                    |                      |          \  }}}|d u| j        _        || j        _        || j        _        || j        j        _        |||d         d          }| j        }| j        }| j	        }g }g }	t          |          D ]\  }
}| j                            |          }|r|                                \  }}}|s|r|                    ||          }|r|                    ||          }|r|s|                    |           |s?|                    |           |	                    d                    |                     g }t#          j        d|
 d|           |	S )Nr   r#   z"multiline re could not match line z: )r   r<   r=   r   r?   r   re   continuation_charmultiline_charreplace_charr   r   r(   r)   replacer:   rC   r   r   )r   re   r   blockr   r   r   r   partsoutlinesrV   rQ   mor   r   r   s                   r   process_lineszDaophotInputter.process_lines  s   !%!6!6u!=!=!(!4	#	 %	!'	'!*,,'E 2,( '' 	 	GAt"))$//B )+&$ Id I<<(9<HHD F<<EED ' 'LL&&& LL&&&OOBGGENN333E1DDDdDD   r   N)r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sZ        NL2::;;L.6 .6 .6 .6`$ $ $ $ $r   r   c                   B    e Zd ZdZdZdgZdZdZeZ	e
ZeZdZd Zd	dZdS )
rG   ac	  
    DAOphot format table.

    Example::

      #K MERGERAD   = INDEF                   scaleunit  %-23.7g
      #K IRAF = NOAO/IRAFV2.10EXPORT version %-23s
      #K USER = davis name %-23s
      #K HOST = tucana computer %-23s
      #
      #N ID    XCENTER   YCENTER   MAG         MERR          MSKY           NITER    \
      #U ##    pixels    pixels    magnitudes  magnitudes    counts         ##       \
      #F %-9d  %-10.3f   %-10.3f   %-12.3f     %-14.3f       %-15.7g        %-6d
      #
      #N         SHARPNESS   CHI         PIER  PERROR                                \
      #U         ##          ##          ##    perrors                               \
      #F         %-23.3f     %-12.3f     %-6d  %-13s
      #
      14       138.538     INDEF   15.461      0.003         34.85955       4        \
                  -0.032      0.802       0     No_error

    The keywords defined in the #K records are available via the output table
    ``meta`` attribute::

      >>> import os
      >>> from astropy.io import ascii
      >>> filename = os.path.join(ascii.__path__[0], 'tests/data/daophot.dat')
      >>> data = ascii.read(filename)
      >>> for name, keyword in data.meta['keywords'].items():
      ...     print(name, keyword['value'], keyword['units'], keyword['format'])
      ...
      MERGERAD INDEF scaleunit %-23.7g
      IRAF NOAO/IRAFV2.10EXPORT version %-23s
      USER  name %-23s
      ...

    The unit and formats are available in the output table columns::

      >>> for colname in data.colnames:
      ...     col = data[colname]
      ...     print(colname, col.unit, col.format)
      ...
      ID None %-9d
      XCENTER pixels %-10.3f
      YCENTER pixels %-10.3f
      ...

    Any column values of INDEF are interpreted as a missing value and will be
    masked out in the resultant table.

    In case of multi-aperture daophot files containing repeated entries for the last
    row of fields, extra unique column names will be created by suffixing
    corresponding field names with numbers starting from 2 to N (where N is the
    total number of apertures).
    For example,
    first aperture radius will be RAPERT and corresponding magnitude will be MAG,
    second aperture radius will be RAPERT2 and corresponding magnitude will be MAG2,
    third aperture radius will be RAPERT3 and corresponding magnitude will be MAG3,
    and so on.

    daophotFzIRAF DAOphot format tableP   c                 f    t           j                            |            | j        | j        _        d S r   )r   
BaseReaderr   r<   inputterr   s    r   r   zDaophot.__init__  s*      &&&!Yr   Nc                     t           r   )NotImplementedError)r   r^   s     r   writezDaophot.write  s    !!r   r   )r   r   r   r   _format_name_io_registry_format_aliases_io_registry_can_write_descriptionr   header_classr   
data_classr   inputter_classrH   r   r   r   r   r   rG   rG   =  sp        < <| L#,+".L LJ$NK' ' '
" " " " " "r   rG   )r   	itertoolsrD   r   collectionsr   r   numpyr@   r#   r   r   miscr   r	   r
   r   r   r   r   ContinuationLinesInputterr   r   rG   r   r   r   <module>r      sW        				 0 0 0 0 0 0 0 0             @ @ @ @ @ @ @ @ @ @q5 q5 q5 q5 q5DO q5 q5 q5h2 2 2 2 2$- 2 2 2,Z Z Z Z Zd4 Z Z ZzP" P" P" P" P"do P" P" P" P" P"r   