I have NetCDF data which consists of dimensions as latitude, longitude, and time (as year), and one variable as TOMSEPL3_008_Aeroso. The time dimension is from 2001-2020. I am trying to perform a man-Kendall trend of the variable for 20 years and generate the corresponding Sen's slope and p values. I am not sure if I am doing it correctly. Can anyone help me with this? It seems I am getting the wrong values of the tau and slope values.
variable_name= 'TOMSEPL3_008_Aerosol'def calculate_mann_kendall_trend(data): time_values = np.arange(len(data.year)) trend = np.empty((len(data.lat), len(data.lon))) p_values = np.empty((len(data.lat), len(data.lon))) sen_slope = np.empty((len(data.lat), len(data.lon))) for i in range(len(data.lat)): for j in range(len(data.lon)): y = data[variable_name][:, i, j].values tau, p_value = kendalltau(time_values, y) trend[i, j] = tau # Calculate Sen's slope n = len(y) sen_slope[i, j] = np.median([((y[j] - y[i]) / (time_values[j] - time_values[i])) for i in range(n) for j in range(i+1, n)]) p_values[i, j] = p_value return trend, sen_slope, p_values# Calculate the Mann-Kendall trend and p-values for the merged datasetmann_kendall_trend_data, sen_slope, p_values = calculate_mann_kendall_trend(merged_data)# Plot Sen's Slopeplt.figure(figsize=(12, 4))plt.subplot(1, 3, 1)plt.imshow(sen_slope, cmap='coolwarm', interpolation='none')plt.colorbar(label="Sen's Slope")plt.title("Sen's Slope")# Plot Kendall Tau (Trend)plt.subplot(1, 3, 2)plt.imshow(mann_kendall_trend_data, cmap='coolwarm', interpolation='none')plt.colorbar(label='Kendall Tau')plt.title('Kendall Tau (Trend)')# Plot P-valuesplt.subplot(1, 3, 3)plt.imshow(p_values, cmap='coolwarm', interpolation='none')plt.colorbar(label='P-values')plt.title('P-values')plt.tight_layout()plt.show()