B
    |d                 @   s   d dl Zd dlZd dlmZ d dlmZmZm	Z	m
Z
mZ d dlmZ d dlmZ dddd	gZdd
dZdddZdddZddd	ZdS )    N)fits)CompImageHDUHDUListHeaderImageHDU
PrimaryHDU)WCS)BaseHighLevelWCSparse_input_dataparse_input_shapeparse_input_weightsparse_output_projectionc             C   s   t | trtt| |dS t | trT|dkrHt| dkrDtdnd}t| | S t | tt	t
frt| jt| jfS t | trt | d tjrt | d tr| d t| d fS | S n"t | tjjr| j| jfS tddS )zB
    Parse input data to return a Numpy array and WCS object.
    )hdu_inN   zOMore than one HDU is present, please specify HDU to use with ``hdu_in=`` optionr   zWinput_data should either be an HDU object or a tuple of (array, WCS) or (array, Header))
isinstancestrr
   r   openr   len
ValueErrorr   r   r   datar   headertuplenpndarrayr   astropynddata
NDDataBasewcs	TypeError)Z
input_datar    r   \/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/reproject/utils.pyr
      s"    


c             C   s6  t | trtt| |dS t | trT|dkrHt| dkrDtdnd}t| | S t | tt	t
frt| jt| jfS t | trt | d tjrt | d tr| d jt| d fS | d j| d fS nlt | trt | d trt | d tr| d t| d fS | S n&t | tjjr*| jj| jfS tddS )zV
    Parse input shape information to return an array shape tuple and WCS object.
    )r   Nr   zOMore than one HDU is present, please specify HDU to use with ``hdu_in=`` optionr   zjinput_shape should either be an HDU object or a tuple of (array-or-shape, WCS) or (array-or-shape, Header))r   r   r   r   r   r   r   r   r   r   r   shaper   r   r   r   r   r   r   r   r   r   r   r   )Zinput_shaper   r   r   r    r   *   s*    


c             C   s   t | tr tt| |dd S t | tr\|dkrLt| dkrHtdnd}t| | d S t | tt	t
frr| jS t | tjr| S tddS )z6
    Parse input weights to return a Numpy array.
    )r   r   Nr   zTMore than one HDU is present, please specify HDU to use with ``hdu_weights=`` optionz=input_weights should either be an HDU object or a Numpy array)r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   )Zinput_weightsZhdu_weightsr   r   r    r   L   s    


c                s  |d kr|d k	r:|j }n"|d k	r:|d k	r:||j kr:tdt trt }y( fddt d D d d d }W n$ tk
r   |d krtdY nX ndt tr }|d krtdnDt trt	
 }|d jj }|d j}t|}|  ntd	t|dkrtd
||fS )Nz.shape_out does not match shape of output_arrayc                s   g | ]} d  |d  qS )zNAXIS{}r   )format).0i)output_projectionr   r    
<listcomp>o   s   z+parse_output_projection.<locals>.<listcomp>ZNAXISzUNeed to specify shape since output header does not contain complete shape informationzINeed to specify shape_out when specifying output_projection as WCS objectr   zHoutput_projection should either be a Header, a WCS object, or a filenamez:The shape of the output image should not be an empty tuple)r!   r   r   r   r   rangeKeyErrorr	   r   r   r   r   r   closer   r   )r%   Z	shape_outZoutput_arrayZwcs_outZhdu_listr   r   )r%   r    r   c   s8    








)N)N)N)NN)numpyr   Zastropy.nddatar   Z
astropy.ior   Zastropy.io.fitsr   r   r   r   r   Zastropy.wcsr   Zastropy.wcs.wcsapir	   __all__r
   r   r   r   r   r   r   r    <module>   s   

"
