Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/build_conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: build_conda
on:
pull_request:
branches:
- main

# cancel running jobs if theres a newer push
concurrency:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/create_test_conda_env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: create_test_conda_env
on:
pull_request:
branches:
- main

push:
branches:
- main
Expand Down
12 changes: 9 additions & 3 deletions fre/app/generate_time_averages/cdoTimeAverager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ def generate_timavg(self, infile = None, outfile = None):
nc_fin = Dataset(infile, 'r')

time_bnds = nc_fin['time_bnds'][:].copy()
wgts = ( np.moveaxis(time_bnds, 0, -1)[1][:].copy() - \
np.moveaxis(time_bnds, 0, -1)[0][:].copy() )
wgts_sum = sum(wgts)
# Ensure float64 precision for consistent results across numpy versions
# NumPy 2.0 changed type promotion rules (NEP 50), so explicit casting
# is needed to avoid precision differences
time_bnds = np.asarray(time_bnds, dtype=np.float64)
# Transpose once to avoid redundant operations
time_bnds_transposed = np.moveaxis(time_bnds, 0, -1)
wgts = time_bnds_transposed[1] - time_bnds_transposed[0]
# Use numpy.sum for consistent dtype handling across numpy versions
wgts_sum = np.sum(wgts, dtype=np.float64)

fre_logger.debug('wgts_sum = %s', wgts_sum)

Expand Down
20 changes: 11 additions & 9 deletions fre/app/generate_time_averages/frepytoolsTimeAverager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ def generate_timavg(self, infile = None, outfile = None):
fin_dims = nc_fin.dimensions
num_time_bnds = fin_dims['time'].size
if not self.unwgt: #compute sum of weights
wgts = ( numpy.moveaxis( time_bnds, 0, -1 )[1][:].copy() - \
numpy.moveaxis( time_bnds, 0, -1 )[0][:].copy() )
# Cast to float64 for consistent results across numpy versions (NEP 50 type promotion changes)
time_bnds = numpy.asarray(time_bnds, dtype=numpy.float64)
# Transpose once to avoid redundant operations
time_bnds_transposed = numpy.moveaxis(time_bnds, 0, -1)
wgts = time_bnds_transposed[1] - time_bnds_transposed[0]
# Use numpy.ma.sum only if there are actually masked values in time_bnds
if has_masked_time_bnds:
wgts_sum = numpy.ma.sum(wgts)
wgts_sum = numpy.ma.sum(wgts, dtype=numpy.float64)
else:
wgts_sum = sum(wgts)
wgts_sum = numpy.sum(wgts, dtype=numpy.float64)

fre_logger.debug('wgts_sum = %s', wgts_sum)

Expand Down Expand Up @@ -123,8 +126,8 @@ def generate_timavg(self, infile = None, outfile = None):
if has_masked_data:
avgvals[0][lat][lon] = numpy.ma.sum(tim_val_array * wgts) / wgts_sum
else:
avgvals[0][lat][lon] = sum( (tim_val_array[tim] * wgts[tim] )
for tim in range(num_time_bnds) ) / wgts_sum
# Use numpy.sum for consistent dtype handling across numpy versions
avgvals[0][lat][lon] = numpy.sum(tim_val_array * wgts, dtype=numpy.float64) / wgts_sum

del tim_val_array
del lon_val_array
Expand All @@ -139,9 +142,8 @@ def generate_timavg(self, infile = None, outfile = None):
if has_masked_data:
avgvals[0][lat][lon] = numpy.ma.sum(tim_val_array) / num_time_bnds
else:
avgvals[0][lat][lon] = sum( # no time sum needed here, b.c. unweighted, so sum
tim_val_array[tim] for tim in range(num_time_bnds)
) / num_time_bnds
# Use numpy.sum for consistent dtype handling across numpy versions
avgvals[0][lat][lon] = numpy.sum(tim_val_array, dtype=numpy.float64) / num_time_bnds

del tim_val_array
del lon_val_array
Expand Down