From 314082c1606fc1b141aa8d93e27a0dfbefa95d38 Mon Sep 17 00:00:00 2001 From: Robin Date: Fri, 20 Dec 2024 12:56:54 +0100 Subject: [PATCH] reproduced difference between means in python and c++ entirely in python still dont know why --- .gitignore | 4 ++-- src/main.cu | 4 +++- test_read.py | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 9b6e850..be20c38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -build +build/ .vscode -data \ No newline at end of file +data/ \ No newline at end of file diff --git a/src/main.cu b/src/main.cu index e360b0e..3df788e 100644 --- a/src/main.cu +++ b/src/main.cu @@ -3,14 +3,16 @@ #include #include #include +#include #include + int main() { std::string path = "data/MERRA2_400.inst6_3d_ana_Np.20120101.nc4"; std::string variable = "T"; 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; int n = 0; int skipped = 0; diff --git a/test_read.py b/test_read.py index 8cfcc9c..0ff56eb 100644 --- a/test_read.py +++ b/test_read.py @@ -10,7 +10,7 @@ print(ncfile.variables.keys()) 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) # 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() 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 ncfile.close()