reproduced difference between means in python and c++ entirely in python still dont know why

This commit is contained in:
Robin 2024-12-20 12:56:54 +01:00
parent e1d11e84ce
commit 314082c160
3 changed files with 20 additions and 4 deletions

4
.gitignore vendored
View File

@ -1,3 +1,3 @@
build build/
.vscode .vscode
data data/

View File

@ -3,14 +3,16 @@
#include <cuda_runtime.h> #include <cuda_runtime.h>
#include <device_launch_parameters.h> #include <device_launch_parameters.h>
#include <iostream> #include <iostream>
#include <iomanip>
#include <cmath> #include <cmath>
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 = "T"; 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 // Calculate the mean of the data to see if it works.
float sum = 0; float sum = 0;
int n = 0; int n = 0;
int skipped = 0; int skipped = 0;

View File

@ -10,7 +10,7 @@ print(ncfile.variables.keys())
U = ncfile.variables['T'][:] U = ncfile.variables['T'][:]
# Check the shape of the variable (it should be 3D) # Check the shape of the variable
print("Shape of U:", U.shape) 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)
@ -28,5 +28,19 @@ print("Number of masked values in U:", masked_count)
nan_count = np.isnan(U).sum() nan_count = np.isnan(U).sum()
print("Number of NaN values in U:", nan_count) print("Number of NaN values in U:", nan_count)
print("Calculating mean manually (takes a bit cause python is slowww)")
count = 0
valsum = 0
for val in U.flat:
if not np.ma.is_masked(val):
# print(val)
valsum += val
count += 1
print(f"{valsum=} {valsum/count=} {count=}")
print(f"The problem is this: why does {valsum/count=} not equal {U_mean=}")
# Close the NetCDF file # Close the NetCDF file
ncfile.close() ncfile.close()