for some reason computing the mean in python and netcdfc++ gives two slightly different values

This commit is contained in:
Robin committed 2024-12-20 12:36:04 +01:00
1 parent 1c2ab4af76
commit e1d11e84ce
4 files changed
+20 -5

No files matched your search

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+11 -5
View File
@@ -7,16 +7,22 @@
int main() { int main() {
std::string path = "data/MERRA2_400.inst6_3d_ana_Np.20120101.nc4"; std::string path = "data/MERRA2_400.inst6_3d_ana_Np.20120101.nc4";
std::string variable = "U"; std::string variable = "T";
auto x = readData(path, variable); auto x = readData(path, variable);
// Print some values from the file to see that it worked // Print some values from the file to see that it worked
int num = 0; float sum = 0;
int n = 0;
int skipped = 0;
for(int i = 0; i < x.size(); i++) { for(int i = 0; i < x.size(); i++) {
if (x[i] < 1E14) std::cout << x[i] << "\n"; if (x[i] < 1E14) {
if(num > 10000) break; sum += x[i];
num++; n++;
} else {
skipped++;
} }
}
std::cout << "Mean = " << sum/n << " and sum = " << sum << " using " << n << " values in computation and skipped " << skipped << " values.\n";
return 0; return 0;
} }
+9
View File
@@ -15,9 +15,18 @@ print("Shape of U:", U.shape)
# Compute the mean of the variable across all axes (for all elements in U) # Compute the mean of the variable across all axes (for all elements in U)
U_mean = np.mean(U) U_mean = np.mean(U)
U_sum = np.sum(U)
# Print the mean # Print the mean
print("Mean of U:", U_mean) print("Mean of U:", U_mean)
print("Sum of U:", U_sum)
masked_count = np.ma.count_masked(U)
print("Number of masked values in U:", masked_count)
nan_count = np.isnan(U).sum()
print("Number of NaN values in U:", nan_count)
# Close the NetCDF file # Close the NetCDF file
ncfile.close() ncfile.close()
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.