Skip to content

Commit

Permalink
update new algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
sliu008 committed Mar 5, 2024
1 parent 12ecd66 commit f1d0a0b
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions podaac/tig/tig.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def generate_image_output(self,
fill_value,
rows,
cols,
nearest=False,
nearest
):
"""
Generates output that matches image extents using discrete global grids
Expand All @@ -724,8 +724,6 @@ def generate_image_output(self,
An array of latitude values
fill_value : float
The fill value used in the variable array
nearest : bool
Fill in values from the nearest grid cell to match pixel resolution
Returns
-------
numpy.ndarray
Expand All @@ -747,14 +745,58 @@ def generate_image_output(self,
# Generate an array for output values
output_vals = np.full(rows * cols, fill_value, dtype=np.float64)

"""
# Use values nearest to grid cells within max_dist
if nearest:
# Get grid points
gpis, gridlons, gridlats = image_grid.get_grid_points()
max_dist = DEG_M/self.ppd
for i, idx in enumerate(gpis):
ngpi, distance = data_grid.find_nearest_gpi(gridlons[idx],
gridlats[idx])
if distance <= max_dist:
value = var_array[ngpi]
if value != np.isnan:
if output_vals[i] == fill_value:
output_vals[i] = value
else:
# average two values if they fall in the same grid cell
output_vals[i] = (output_vals[i] + value) / 2
else:
output_vals[i] = fill_value
# Use values only within grid cells
else:
# remove nan values
lut = lut[~(np.isnan(var_array))]
var_array = var_array[~(np.isnan(var_array))]
for i, val in enumerate(var_array):
idx = lut[i]
try:
if output_vals[idx] == fill_value:
output_vals[idx] = val
else:
# average two values if they fall in the same grid cell
output_vals[idx] = (output_vals[idx] + val) / 2
# skip rare situations where we encounter nan location values
except IndexError:
continue
"""

# Iterate through valid values in var_array, remove nan values
valid_values = ~np.isnan(var_array)
lut = lut[valid_values]
var_array = var_array[valid_values]

# Find valid indices within the bounds of output_vals
valid_indices_lut = (0 <= lut) & (lut < len(output_vals))
# Filter lut to include only valid indices
lut = lut[valid_indices_lut]

# Replace the loop with NumPy indexing
valid_indices = np.where(output_vals[lut] == fill_value)[0]
output_vals[lut[valid_indices]] = var_array[valid_indices]


# Return output values
return output_vals
Expand Down

0 comments on commit f1d0a0b

Please sign in to comment.