diff --git a/modules/util/util.py b/modules/util/util.py index 5f6314915de4cf76c5657fda4756872852b96b3d..2e8ec698f9282b465ef92982f6a4a63b328b1f08 100644 --- a/modules/util/util.py +++ b/modules/util/util.py @@ -411,6 +411,75 @@ def get_cartopy_crs(satellite, domain): return geos, xlen, xmin, xmax, ylen, ymin, ymax +def get_grid_values(h5f, grid_name, j_c, i_c, half_width, num_j=None, num_i=None, scale_factor_name='scale_factor', add_offset_name='add_offset', + fill_value_name='_FillValue', range_name='actual_range', fill_value=None): + hfds = h5f[grid_name] + attrs = hfds.attrs + if attrs is None: + raise GenericException('No attributes object for: '+grid_name) + + ylen, xlen = hfds.shape + + if half_width is not None: + j_l = j_c-half_width + i_l = i_c-half_width + if j_l < 0 or i_l < 0: + return None + + j_r = j_c+half_width+1 + i_r = i_c+half_width+1 + if j_r >= ylen or i_r >= xlen: + return None + else: + j_l = j_c + j_r = j_c + num_j + 1 + i_l = i_c + i_r = i_c + num_i + 1 + + grd_vals = hfds[j_l:j_r, i_l:i_r] + if fill_value is not None: + grd_vals = np.where(grd_vals == fill_value, np.nan, grd_vals) + + if scale_factor_name is not None: + attr = attrs.get(scale_factor_name) + if attr is None: + raise GenericException('Attribute: '+scale_factor_name+' not found for dataset: '+grid_name) + if np.isscalar(attr): + scale_factor = attr + else: + scale_factor = attr[0] + grd_vals = grd_vals * scale_factor + + if add_offset_name is not None: + attr = attrs.get(add_offset_name) + if attr is None: + raise GenericException('Attribute: '+add_offset_name+' not found for dataset: '+grid_name) + if np.isscalar(attr): + add_offset = attr + else: + add_offset = attr[0] + grd_vals = grd_vals + add_offset + + if range_name is not None: + attr = attrs.get(range_name) + if attr is None: + raise GenericException('Attribute: '+range_name+' not found for dataset: '+grid_name) + low = attr[0] + high = attr[1] + grd_vals = np.where(grd_vals < low, np.nan, grd_vals) + grd_vals = np.where(grd_vals > high, np.nan, grd_vals) + elif fill_value_name is not None: + attr = attrs.get(fill_value_name) + if attr is None: + raise GenericException('Attribute: '+fill_value_name+' not found for dataset: '+grid_name) + if np.isscalar(attr): + fill_value = attr + else: + fill_value = attr[0] + grd_vals = np.where(grd_vals == fill_value, np.nan, grd_vals) + + return grd_vals + # Example GOES file to retrieve GEOS parameters in MetPy form (CONUS) exmp_file_conus = '/Users/tomrink/data/OR_ABI-L1b-RadC-M6C14_G16_s20193140811215_e20193140813588_c20193140814070.nc' # Full Disk @@ -460,7 +529,7 @@ def make_for_full_domain_predict(h5f, name_list=None, satellite='GOES16', domain cnt_a = 0 for didx, ds_name in enumerate(name_list): - gvals = get_grid_values_all(h5f, ds_name, j_0, i_0, None, num_j=ylen, num_i=xlen) + gvals = get_grid_values(h5f, ds_name, j_0, i_0, None, num_j=ylen, num_i=xlen) if gvals is not None: grd_dct[ds_name] = gvals cnt_a += 1