B
    q\x                 @   s   d Z ddlmZ ddlZdddddZd	d
 Zdd ZG dd dZG dd deZ	G dd deZ
G dd dZG dd deZdS )zH
This module provides functions to help with testing against iraf tasks
    )logNZ	ChebyshevZLegendreZSpline3ZSpline1)g      ?g       @g      @g      @c             C   s<   t | }| }|  |ddd }dd |D }|S )z
    Read the records of an IRAF database file into a python list

    Parameters
    ----------
    fname : str
           name of an IRAF database file

    Returns
    -------
        A list of records
    begin   Nc             S   s   g | ]}t |qS  )Record).0rr   r   >lib/python3.7/site-packages/astropy/modeling/tests/irafutil.py
<listcomp>"   s    zget_records.<locals>.<listcomp>)openreadclosesplit)fnamefdtbZrecsrecordsr   r   r	   get_records   s    r   c             C   s   t | }| }|  |S )z
    Read an IRAF database file

    Parameters
    ----------
    fname : str
          name of an IRAF database file

    Returns
    -------
        the database file as a string
    )r   r   r   )r   r   r   r   r   r	   get_database_string&   s    r   c               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )r   a,  
    A base class for all records - represents an IRAF database record

    Attributes
    ----------
    recstr: string
            the record as a string
    fields: dict
            the fields in the record
    taskname: string
            the name of the task which created the database file
    c             C   s   || _ |  | _|  | _d S )N)recstr
get_fieldsfieldsget_task_nameZtaskname)selfr   r   r   r	   __init__G   s    
zRecord.__init__c                s0   | j d dd  D   fdd D   S )N
c             S   s   g | ]}|  qS r   )strip)r   lr   r   r	   r
   N   s    z!Record.aslist.<locals>.<listcomp>c                s"   g | ]}t |d kr |qS )r   )lenremove)r   r   )reclistr   r	   r
   O   s    )r   r   )r   r   )r    r	   aslistL   s    zRecord.aslistc          	   C   s   i }|   }t|}xt|D ]}|| }|r|d  r| }|d |k r||d  d  s| |||t|d  d  ||d < qddd |dd  D ||d < qddd |dd  D ||d < qqqW |S )Nr   r    c             s   s   | ]
}|V  qd S )Nr   )r   sr   r   r	   	<genexpr>`   s    z$Record.get_fields.<locals>.<genexpr>c             s   s   | ]
}|V  qd S )Nr   )r   r#   r   r   r	   r$   b   s    )r!   r   rangeisalphar   read_array_fieldintjoin)r   r   ZflistZ	numfieldsilineZfieldr   r   r	   r   R   s    (&&zRecord.get_fieldsc             C   s$   y
| j d S  tk
r   d S X d S )NZtask)r   KeyError)r   r   r   r	   r   g   s    
zRecord.get_task_namec          	   C   sp   dd |dd  D }dd |D }yt |}W n0 tk
rb   td|d  d  Y nX |t jS )Nc             S   s   g | ]}|  qS r   )r   )r   r   r   r   r	   r
   o   s    z+Record.read_array_field.<locals>.<listcomp>r   c             S   s   g | ]}|d d qS )N   r   )r   r   r   r   r	   r
   r   s    zCould not read array field {}r   )	nparray	Exceptionr   debugformatr   ZastypeZfloat64)r   Z	fieldlistZ	fieldlineZxyzZfarrr   r   r	   r'   m   s    "zRecord.read_array_fieldN)	__name__
__module____qualname____doc__r   r!   r   r   r'   r   r   r   r	   r   9   s   r   c                   sH   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	  Z
S )IdentifyRecorda  
    Represents a database record for the onedspec.identify task

    Attributes
    ----------
    x: array
       the X values of the identified features
       this represents values on axis1 (image rows)
    y: int
       the Y values of the identified features
       (image columns)
    z: array
       the values which X maps into
    modelname: string
        the function used to fit the data
    nterms: int
        degree of the polynomial which was fit to the data
        in IRAF this is the number of coefficients, not the order
    mrange: list
        the range of the data
    coeff: array
        function (modelname) coefficients
    c                s   t  | | jd  | _| jd d d df | _|  | _| jd d d df | _| 	 | _
|  | _|  | _|  | _d S )NZcoefficientsZfeaturesr   r   )superr   r   flatten
_flatcoeffx	get_ydatayzget_model_name	modelname
get_ntermsZnterms	get_rangeZmrange	get_coeffcoeff)r   r   )	__class__r   r	   r      s    



zIdentifyRecord.__init__c             C   s   t | jd  S )Nr   )iraf_models_mapr:   )r   r   r   r	   r?      s    zIdentifyRecord.get_model_namec             C   s
   | j d S )Nr   )r:   )r   r   r   r	   rA      s    zIdentifyRecord.get_ntermsc             C   s   | j d }| j d }||gS )N   r-   )r:   )r   ZlowZhighr   r   r	   rB      s    

zIdentifyRecord.get_rangec             C   s   | j dd  S )N   )r:   )r   r   r   r	   rC      s    zIdentifyRecord.get_coeffc             C   s`   | j d }|dd }|d}||| }d|krT|dd }t||d S t|S d S )Nimage[r   ],)r   findr(   )r   rI   leftrightZsectionZyindr   r   r	   r<      s    

zIdentifyRecord.get_ydata)r3   r4   r5   r6   r   r?   rA   rB   rC   r<   __classcell__r   r   )rE   r	   r7   z   s   r7   c                   s(   e Zd ZdZ fddZdd Z  ZS )FitcoordsRecorda  
    Represents a database record for the longslit.fitccords task

    Attributes
    ----------
    modelname: string
        the function used to fit the data
    xorder: int
        number of terms in x
    yorder: int
        number of terms in y
    xbounds: list
        data range in x
    ybounds: list
        data range in y
    coeff: array
        function coefficients

    c                s~   t  | | jd  | _t| jd  | _| jd | _| jd | _| jd | jd g| _	| jd | jd g| _
|  | _d S )	NZsurfacer   r   rG   rH            )r8   r   r   r9   _surfacerF   r@   ZxorderZyorderZxboundsZyboundsrC   rD   )r   r   )rE   r   r	   r      s    zFitcoordsRecord.__init__c             C   s   | j dd  S )N   )rV   )r   r   r   r	   rC      s    zFitcoordsRecord.get_coeff)r3   r4   r5   r6   r   rC   rQ   r   r   )rE   r	   rR      s   
rR   c               @   s    e Zd ZdZdd Zdd ZdS )IDBz
    Base class for an IRAF identify database

    Attributes
    ----------
    records: list
             a list of all `IdentifyRecord` in the database
    numrecords: int
             number of records
    c             C   s&   dd |  |D | _t| j| _d S )Nc             S   s   g | ]}t |qS r   )r7   )r   Zrstrr   r   r	   r
      s    z IDB.__init__.<locals>.<listcomp>)r!   r   r   Z
numrecords)r   Zdtbstrr   r   r	   r      s    zIDB.__init__c             C   sl   | d}y|d  d}W n tk
r0   |S X t|dkrd|d drd|d  sd|dd  S |S d S )Nr   r   r   rG   #r   )r   r0   r   
startswithr   )r   r   ZrlZrl0r   r   r	   r!      s    
&z
IDB.aslistN)r3   r4   r5   r6   r   r!   r   r   r   r	   rX      s   rX   c                   s(   e Zd ZdZ fddZdd Z  ZS )ReidentifyRecordzG
    Represents a database record for the onedspec.reidentify task
    c                sJ   t  | tdd | jD | _|  | _tdd | jD | _d S )Nc             S   s   g | ]
}|j qS r   )r;   )r   r   r   r   r	   r
      s    z-ReidentifyRecord.__init__.<locals>.<listcomp>c             S   s   g | ]
}|j qS r   )r>   )r   r   r   r   r	   r
     s    )	r8   r   r.   r/   r   r;   r<   r=   r>   )r   Zdatabasestr)rE   r   r	   r      s    
zReidentifyRecord.__init__c             C   s:   t | jj}|t dd | jD d d t jf  }|S )Nc             S   s   g | ]
}|j qS r   )r=   )r   r   r   r   r	   r
     s    z.ReidentifyRecord.get_ydata.<locals>.<listcomp>)r.   Zonesr;   shaper/   r   Znewaxis)r   r=   r   r   r	   r<     s    (zReidentifyRecord.get_ydata)r3   r4   r5   r6   r   r<   rQ   r   r   )rE   r	   r[      s   r[   )r6   Zastropy.loggerr   Znumpyr.   rF   r   r   r   r7   rR   rX   r[   r   r   r   r	   <module>   s   A>#