a
    iDf                     @   s   d Z ddlmZ ddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ h d
Zh dZdd Zdd Zdd Zdd Zdd Zdd Zdd ZdddZdd ZdS ) z4Functions for converting to and from xarray objects
    )CounterN)CFDatetimeCoderCFTimedeltaCoder)	decode_cf)duck_array_ops)	DataArray)get_fill_value)
array_type>   ZboundscalendarZ
_FillValueZmissing_valueZ	valid_maxZformula_termscell_methodsZ
leap_monthcompress	long_nameZgrid_mappingaxisZmonth_lengthsZclimatologyZvalid_rangeZscale_factorZcoordinatesZ	leap_yearZ
add_offsetZ	valid_minstandard_nameunits>
   ZminimumZ	mid_rangeZvariancemeanmaximumZmedianZstandard_deviationZpointsummodec                 C   s   t  t | jS N)r   encoder   variable)var r   [/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/xarray/convert.pyr   5   s    r   c                    s    fdd|   D S )z*Return attrs that are not in ignored_attrsc                    s   i | ]\}}| vr||qS r   r   .0kvignored_attrsr   r   
<dictcomp>;       z!_filter_attrs.<locals>.<dictcomp>items)attrsr    r   r   r   _filter_attrs9   s    r&   c                    s    fdd|   D S )z#Return attrs with keys in keys listc                    s   i | ]\}}| v r||qS r   r   r   keysr   r   r!   @   r"   z_pick_attrs.<locals>.<dictcomp>r#   )r%   r(   r   r'   r   _pick_attrs>   s    r)   c                 C   sV   ddl }dt| ti}|t| d t| d}d| v rR|j| d fi ||d< |S )z@Converts the xarray attrs into args that can be passed into Irisr   N
attributesr   r   )r
   r   )cf_unitsr&   iris_forbidden_keysupdater)   ZUnit)r%   r,   argsZ	unit_argsr   r   r   _get_iris_argsC   s    
r0   c              	   C   sT  ddl }ddlm} g }g }| jD ]}t| j| }t|j}||d< d}|jr\| |j}|| jv rz(|jj	|j
fi |}	||	|f W q ty   |jj|j
fi |}	||	|f Y q0 q"|jj|j
fi |}	||	|f q"t| j}
| j|
d< ||
d< ||
d< d| jv r0|| jd |
d< t| j}|jj|fi |
}|S )z$Convert a DataArray into a Iris Cuber   N)parse_cell_methodsvar_nameZdim_coords_and_dimsZaux_coords_and_dimsr   )irisZiris.fileformats.netcdfr1   coordsr   r0   r%   dimsZget_axis_numZDimCoordvaluesappend
ValueErrorZAuxCoordnamer   Zmasked_invaliddatacubeZCube)	dataarrayr3   r1   
dim_coordsZ
aux_coordsZ
coord_namecoordZ
coord_argsr   Z
iris_coordr/   Zmasked_datar;   r   r   r   to_irisQ   s:    




r?   c                 C   sb   | j | jd}| jjr"| jj|d< | jjdkrD| j sD| jj|d< || j dd | D S )z5Return a dictionary of attrs when given a Iris objectr+   r
   1r   c                 S   s   i | ]\}}|d ur||qS r   r   r   r   r   r   r!      r"   z&_iris_obj_to_attrs.<locals>.<dictcomp>)	r   r   r   r
   originZ
is_unknownr.   r*   r$   )objr%   r   r   r   _iris_obj_to_attrsy   s    rC   c                 C   s   g }| D ]|}d dd |jD }d dd |jD }d dd |jD }d ||g }|rpd| d}|||j |  qd |S )	z*Converts a Iris cell methods into a string c                 s   s   | ]}| d V  qdS )z: Nr   )r   nr   r   r   	<genexpr>   r"   z,_iris_cell_methods_to_str.<locals>.<genexpr> c                 s   s   | ]}d | V  qdS )z
interval: Nr   )r   intervalr   r   r   rF      s   c                 s   s   | ]}d | V  qdS )z	comment: Nr   )r   commentr   r   r   rF      r"   z ())joinZcoord_names	intervalscommentsstripr7   method)Zcell_methods_objr   Zcell_methodnamesrL   rM   extrar   r   r   _iris_cell_methods_to_str   s    
rR   unknownc                 C   s   | j p| jp| jp|S )zMimics `iris_obj.name()` but with different name resolution order.

    Similar to iris_obj.name() method, but using iris_obj.var_name first to
    enable roundtripping.
    )r2   r   r   )Ziris_objdefaultr   r   r   _name   s    rU   c              	      s  ddl }t| }|dkrd}g  t| jD ]N}z"| jd|fd} t| W q* |jjyv    d|  Y q*0 q*tt	 t krdd t
  D }td	| d
i }|  D ]V}t|} fdd| |D }	|	r|	|j|f|t|< qd|j |f|t|< qt| }
t| j}|r8||
d< t| drL|  n| j}td}t||rddlm} ||t| j}n*t|tjjrtj|t| j}n|}t ||||
 d}t!|" }|#|S )z%Convert a Iris cube into an DataArrayr   NrS   T)r=   Z
dimensionsZdim_c                 S   s   g | ]\}}|d kr|qS )   r   r   r   r   r   
<listcomp>   r"   zfrom_iris.<locals>.<listcomp>zDuplicate coordinate name .c                    s   g | ]} | qS r   r   )r   ir5   r   r   rW      r"   r   r   	core_dataZdask)ma)r4   r9   r%   r5   )$Ziris.exceptionsrU   rangendimr>   r7   
exceptionsZCoordinateNotFoundErrorlensetr   r$   r8   r4   rC   
coord_dimsZpointsitemrR   r   hasattrr[   r:   r	   
isinstanceZ
dask.arrayr\   Zfilledr   ZdtypenpZMaskedArrayr   r   Z_to_temp_datasetZ_from_temp_dataset)r;   r3   r9   rY   Z	dim_coord
duplicatesr4   r>   Zcoord_attrsrb   Zarray_attrsr   Z	cube_dataZdask_array_typeZdask_maZfilled_datar<   Z
decoded_dsr   rZ   r   	from_iris   sL    

rh   )rS   )__doc__collectionsr   numpyrf   Zxarray.coding.timesr   r   Zxarray.conventionsr   Zxarray.corer   Zxarray.core.dataarrayr   Zxarray.core.dtypesr   Zxarray.namedarray.pycompatr	   r-   Zcell_methods_stringsr   r&   r)   r0   r?   rC   rR   rU   rh   r   r   r   r   <module>   s&   (
	