U † ‘gzWã@sXdZddlmZddlZddlZddlmZddlmZddl Z ddl Z dd„Z e Z dS)a@ESMF regridding module This module provides fast ESMF-based 2-D regridders for the National Water Model v2.0 forcing engine. Authors: Kent Sparrow and Greg Fall Date: May 10, 2018 United States Department of Commerce National Oceanic and Atmospheric Administration National Weather Service Office of Water Prediction é)Úprint_functionN)ÚDataset)Ú csr_matrixc/ Csxd}|rt ¡}d}t|jƒdkrTd|_zt |¡Wnt |¡YnXtƒ‚|j\}}d} d} d} d} d} d}t|t j j ƒrº|  |¡}d|_ zt |¡Wnt |¡YnXi} zNt|dƒ 8}|j ¡D]\}}t|ƒ||<qÚd|kr,d |_zt |¡Wnt |¡YnXtƒ‚d |krhd |_zt |¡Wnt |¡YnXtƒ‚d |kr¤d |_zt |¡Wnt |¡YnXtƒ‚zP|ddkròdt|dƒ|_zt |¡Wnt |¡YnXtƒ‚WnHtk r<d|_zt |¡Wnt |¡YnXtƒ‚YnXzP|ddkrŒdt|dƒ|_zt |¡Wnt |¡YnXtƒ‚WnHtk rÖd|_zt |¡Wnt |¡YnXtƒ‚YnX|j ¡D]Ž\}}|dkrx|j}t|ƒdkr|||_ zt |¡Wnt |¡YnX| rdt ¡}| r¦t ¡}d?|||_ zt |¡Wnt |¡YnX| rÎ| rºt ¡}t!| | | ff|d |dfd@} t  "||d¡}!|  #|!¡}"|  #|!|k¡}#t   |#d;k¡}$t|$dƒdk rb||"|$<dAt|$dƒ|_ zt |¡Wnt |¡YnXt   ||k¡}%t  "|"| |f¡}|||%<|rØt ¡}dB|||_ zt |¡Wnt |¡YnXn | |}&| |}'| |}(| |})| rüt ¡}t$t| ƒƒD]Œ}*||'|*|&|*f|kr*q||)|*|(|*f}+| |*},|+|krr|,d;krr|||'|*|&|*f<q||'|*|&|*f|,|+7<q|rØt ¡}dC|||_ zt |¡Wnt |¡YnX| ¡d0krt|rôt ¡}| "| |¡}-t   ||k|-d;k@¡}.||.|-|.||.<d}.d}-|rtt ¡}dD|||_ zt |¡Wnt |¡YnX|S)EaçESMF "offline" regridder. Perform sparse matrix regridding using a sparse matrix of regridding weights generated by ESMF_RegridWeightGen or its equivalent. Args: source_grid (float): A 2-D grid of data to regrid. weight_file_path (string): A NetCDF file of regridding weights. no_data_value (float): The missing or no-data value for input and output. Returns: The result of the regridding, a 2-D grid, or None if the regrid fails. Authors: Kent Sparrow and Greg Fall Date: May 10, 2018 United States Department of Commerce National Oceanic and Atmospheric Administration National Weather Service Office of Water Prediction FTézInput grid must be a 2-D array.Nz1Replaced mask in source grid with no-data values.ÚrZn_azMissing 'n_a' dimension.Zn_bzMissing 'n_b' dimensionZn_szMissing 'n_s' dimensionZ src_grid_rankzIncorrect source grid rank zmissing source grid rankZ dst_grid_rankz Incorrect destination grid rank zMissing destination grid rank.Z src_grid_dimséz"src_grid_dims must be a 1-D array.rz%src_grid_dims must have two elements.zsrc_grid_dims_lists z columns; z expectedz rows; z$n_a does not match source grid size.Z dst_grid_dimsz"dst_grid_dims must be a 1-D array.z%dst_grid_dims must have two elements.z#n_b does not match destination gridÚSzS Must be a 1-D array.zS must use the 'n_s' dimensionÚrowzrow must be a 1-D array.z!row must use the 'n_s' dimension.z*Non-positive values found in row variable.z-Out of bounds values found in 'row' variable.Úcolzcol must be a 1-D array.z!col must use the 'n_s' dimension.z*Non-positive values found in col variable.z'Out of bounds values in 'col' variable.z%Missing ESMF_regrid_method attribute.zfirst-order conservativeÚfrac_bzMissing frac_b variable.zfrac_b must be a 1-D array.z$frac_b must use the "n_b" dimension.Zbilinearz(Unrecognized ESMF_regrid_method of "%s".z Weight file z not found or wrong format.z7opened weight file and checked dimensions in %s secondsz-calculated unique values of row in %s secondsgz%initialized output grid in %s secondsz)Unexpected zero value in frac_b variable.z(checked frac_b consistency in %s secondszAcalculated source and destination row/column values in %s seconds)Úshapez9Adjusted %s destination cells to ndv based on source dataz7sparse matrix regrid using scipy.sparse took %s secondsz$sparse matrix regrid took %s secondsz8final adjustment for conservative regrid took %s seconds)%ÚtimeÚlenr ÚerrMsgÚerrModÚlogErrÚ errOutScreenÚ ExceptionÚ isinstanceÚnpÚmaÚ MaskedArrayÚfilledÚ statusMsgÚlogMsgrÚ dimensionsÚitemsÚstrÚKeyErrorÚ variablesÚminÚintÚmaxZESMF_regrid_methodÚ RuntimeErrorÚlowerÚIOErrorÚOSErrorÚfullÚuniqueÚastypeÚwhererÚreshapeÚdotÚrange)/Ú statusMetaZ source_gridZweight_file_pathZ no_data_valueZmeasure_wall_timesZ time_startZuse_scipy_sparseZsource_grid_rowsZsource_grid_colsr r Zsparse_matrix_weightZdest_grid_dimsZdest_grid_rowsZdest_grid_colsZ master_dictZ nc_weightÚnameÚdimZ nc_var_nameZnc_varZsrc_grid_dims_sizeZdest_grid_dims_sizeZ regrid_methodr Z dest_gridZ time_finishZrow_uniqZ uniq_dest_colZ uniq_dest_rowZ frac_b_tmpZcountTmpÚwgtZsource_grid_1dZ dest_grid_1dZdest_grid_1d_ndvZdest_grid_1d_ndv_indZdest_grid_init_ndvZdest_colZdest_rowZ source_colZ source_rowÚiZsource_grid_valZweightZ frac_b_gridZind_tmp©r3úC/lfs/h1/ops/prod/packages/nwm.v3.0.13/ush/mpe_refine/ESMF_regrid.pyÚ with_weightss*          ÿÿÿÿÿ               ÿ  $ r5)Ú__doc__Ú __future__rÚsysr ÚnetCDF4rZ scipy.sparserÚnumpyrrr5Zoffliner3r3r3r4Ús