U

������g�.����������������������@���sF���d�dl�Zd�dlZd�dlZd�dlmZ�G�dd��d�Zdd��Zdd��ZdS�)	�����N)�Datasetc�������������������@���s���e�Zd�Zdd��Zdd��ZdS�)�gribMetac�����������������C���s����d|�_�d|�_d|�_d|�_d|�_d|�_d|�_d|�_d|�_d|�_	d|�_
d|�_d|�_d|�_
d|�_d|�_d|�_d|�_d|�_d|�_d|�_d|�_d|�_d|�_d�S�)NFi�����MISSING)�metaInitialized�nx�ny�foundNdv�gribNdvZgribEdition�dx�dyZ
projection�	latsFound�gridCenterLats�	lonsFound�gridCenterLons�lon0�lat0Zlon1Zlat1ZlonVZeRadiusZsphericalRadiusZmajorRadiusZminorRadiusZlatDZ	lonOrigin)�self��r����@/lfs/h1/ops/prod/packages/nwm.v3.0.13/ush/mpe_refine/gribMod2.py�__init__���s0����zgribMeta.__init__c��������������	���C���s���|j�d�}tj�|�rBzt�|��W�n���d|�|_t���Y�nX�|jdkrfd|�d�|�d�|�}nd|�d�|�d�|�}zttj�	d�s�tj�
|j�d	�}t|d
��}|�d��W�5�Q�R�X�|tjd<�t
j|gt
jt
jdd
�}|���\}	}
|j}W�n���d|�|_t���Y�nX�|dk�r*d|�|_t���zt|�}W�n���d|�|_t���Y�nX�dt|j�k�rtd|�|_t���dt|j�k�r�d|�|_t���t|jd�j�dk�r�|jd�jd�|�_|jd�jd�|�_n$|jd�jd�|�_|jd�jd�|�_t�|�j|�jgtj�}
t�|�j|�jgtj�}t|jd�j�dk�r�td|�j�D�]$}|jd�dd��|
dd�|f<��qBtd|�j�D�]$}|jd�dd��||dd�f<��qtn�t|jd�j�dk�r�d|�|_t���t|jd�j�dk�r�d|�|_t���|jd�dd�dd�f�|
dd�dd�f<�|jd�dd�dd�f�|dd�dd�f<�z|
|�_d|�_W�n���d|�|_t���Y�nX�z||�_d|�_ W�n���d|�|_t���Y�nX�d|�_!z|�"���W�n���d|�|_t���Y�nX�zt�|��W�n���d|�|_t���Y�nX�dS�)a���
        Generic routine for opening GRIB files, and looking for some expected 
        metadata. This includes information on grid lat/lon values, number 
        of rows and columns, grid resolution, and projection information. 
        It's important to read this in every time to protect against unexpected
        grid changes that need to be accounted for on the forcing end. 
        �/tmpGRIB.nc�"Unable to remove old NetCDF file: �����$WGRIB2 �
 -match :"�": -netcdf �wgrib2 �
GRIB2TABLE�	grib2.tbl�wt��209:1:0:0:161:1:6:30:MultiSensorQPE01H:Multi-sensor estimated precipitation accumulation 1-hour:mm
209:1:0:0:161:1:6:37:MultiSensorQPE01H:Multi-sensor estimated precipitation accumulation 1-hour:mm
T��stdout�stderr�shell�-Unable to create temporary NetCDF file from: r����wgrib2 failed on: �&Unable to open temporary NetCDF file: Zlatitudez Expected latitude not found in: Z	longitudez!Expected longitude not found in: N����zUnexpected latitude size in: zUnexpected longitude size in: z?Unable to extract grid center point latitude values from file: z@Unable to extract grid center point longitude values from file: �Unable to close NetCDF file: �Unable to remove NetCDF file: )#�tmpDir�os�path�isfile�remove�	statusMsg�	Exception�	ncepWcoss�environ�get�join�open�write�
subprocess�Popen�PIPE�communicate�
returncoder����list�	variables�len�shaper���r����np�empty�float32�ranger
���r���r���r���r����close)r����
statusMeta�gribFile�id�	tmpNcPath�cmd�g2path�g2t�	cmdOutput�out�err�exitcode�idTmpZ	latCoordsZ	lonCoords�i�jr���r���r����readMeta+���s�����



�







"$

**





zgribMeta.readMetaN)�__name__�
__module__�__qualname__r���rT���r���r���r���r���r������s���r���c	��������������	���C���s"��|�j�d�}	tj�|	�rBzt�|	��W�n���d|	�|�_t���Y�nX�|�jdkrfd|�d�|�d�|	�}
nd|�d�|�d�|	�}
zttj�	d�s�tj�
|�j�d	�}t|d
��}|�d��W�5�Q�R�X�|tjd<�t
j|
gt
jt
jdd
�}
|
���\}}|
j}W�n���d|�|�_t���Y�nX�|dk�r*d|�|�_t���zt|	�}W�n���d|	�|�_t���Y�nX�|t|j�k�r|d|�d�|	�|�_t���|jd�|j|�jd�k�r�dt|jd���d�t|j|�jd���d�|�|�_|jd�|j|�jd�k�rdt|jd���d�t|j|�jd���d�|�|�_z0|j|�ddd�dd�f�|dd�dd�f<�W�n$���d|�d�|�|�_t���Y�nX�|j�s�|j|�j|_t�||j|�jk�}t|d��dk�r�d|�d�|�|�_t���z|����W�n���d|	�|�_t���Y�nX�zt�|	��W�n���d|	�|�_t���Y�nX�dS�)zi
    Function to read in GRIB data given a file, variable name, and number of 
    vertical levels. 
    r���r���r���r���r���r���r���r���r���r���r ���Tr!���r%���r���r&���r'���z$Unable to locate expected variable: z in: zExpected NY of: z Receivd NY of: z from file: r(���Nz"Unable to pull data for variable: z$No valid points found for variable: r)���r*���)r+���r,���r-���r.���r/���r0���r1���r2���r3���r4���r5���r6���r7���r8���r9���r:���r;���r<���r���r=���r>���r@����strr���Z
_FillValuer	���rA����wherer?���rE���)rF���rG���r���rH���ZvarNameZ	levelTypeZlevZ
expectedUnitsZ	dataArrayrI���rJ���rK���rL���rM���rN���rO���rP���rQ���ZvalidIndr���r���r����getGribVariable����s�����


�





��������0����

rZ���c�����������������C���s����t�j�|�s d|�d�|�_t���d|�d�|�}|�jdkrJd|�d�|�}ztj|dd��W�n���d	|�|�_t���Y�nX�t�j�|�s�d
|�d�|�_t���dS�)zj
    Generic function to utilize the cnvgrib command line utility to
    convert GRIB1 files to GRIB2
    zInput GRIB2 file: z not found.z
cnvgrib -g12 � r���z$CNVGRIB -g12 T)r$���zUnable to run cnvgrib on: zOutput GRIB2 file: N)r,���r-���r.���r0���r1���r2���r8����call)rF���Z
gribFileInZgribFileOutrJ���r���r���r����gribOneToGribTwo��s����

r]���)	�numpyrA���r,���r8����netCDF4r���r���rZ���r]���r���r���r���r����<module>���s����h