o
    [Yb
                     @   sx   d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ dd Zd	d
 ZG dd deZdS )    )print_functiondivisionabsolute_import)range)Dataset)maNaN)remove)existsc           	      C   s<  t | ddd}||jvr0td|| f  td |jD ]}td|d|j| j|j| j qd S |j| }dt|v rWt|dd	krWt|d
}t|d}||d d   | }n*dt|v r{t|ddkr{t|d
}t|d}d||d d   |  }n|d d  }|  t|t	j
rt	|}|jdv rt||j< |S |S )NrNETCDF4formatz"Can not find variable "%s" in "%s"zAvailable variables:z ->-zscaling equationzvalue = slope*data + interceptslope	interceptz#value = 10^(slope*data + intercept)
   )float32float64)r   	variablesprintshapedtypedirgetattrclose
isinstancer   masked_arraygetdatar   mask)	filenamevariablencIdivarr   r   datarr r'   ]/Users/jing/Downloads/radiative_transfer_demo/ARTDECO/artdeco/pyartdeco/f2py_utils/ncutils.pyread_nc   s0   

"







r)   c                 C   s@   t | ddd}|jD ]}|j| }t||j|j q
|  d S )Nr   r   r   )r   r   r   r   r   r   )r    r"   r$   Vr'   r'   r(   nc_info0   s
   

r+   c                   @   s.   e Zd ZdddZdd Zdd Zd	d
 ZdS )	NCCreatorTFc                 C   sH   t |r|rtd| t| ntd|t|ddd| _|| _d S )NZremovingzFile {} existswr   r   )r
   r   r	   	Exceptionr   r   _NCCreator__rootgrp_NCCreator__compress)selfr    compress	overwriter'   r'   r(   __init__>   s   


zNCCreator.__init__c                 C   s   |j t|ks	J tt|D ] }|| }|j| }|| jjvr/td|| | j|| q| jj	||j
|| jd}|d d  |d d < d S )Nz creating dimension {} of size {})zlib)ndimlenr   r   r/   
dimensionsr   r   ZcreateDimensionZcreateVariabler   r0   )r1   namedatar8   idimdimsizer$   r'   r'   r(   writeH   s   
zNCCreator.writec                 C   s   |    d S N)r   r1   r'   r'   r(   __del__V   s   zNCCreator.__del__c                 C   s"   | j d kr| j   d | _ d S d S r?   )r/   r   r@   r'   r'   r(   r   Y   s   


zNCCreator.closeN)TF)__name__
__module____qualname__r4   r>   rA   r   r'   r'   r'   r(   r,   <   s
    

r,   N)
__future__r   r   r   builtinsr   ZnetCDF4r   numpyr   r   osr	   os.pathr
   r)   r+   objectr,   r'   r'   r'   r(   <module>   s   $