Average a Data Set while maintaining its variables?

67 Views Asked by At

I am currently trying to plot some data into cartopy, but I am having some issues.

I have a datasheet that has a shape of (180, 180, 360) time, lat, and lon respectively.

I would like to get an annual mean of this data. I had been using the code

def global_mean_3D(var, weights):
    # make sure masking is correct, otherwise we get nans
    var = np.ma.masked_invalid(var)
    # resulting variable should have dimensions of depth and time (x)
    ave = np.zeros([var.shape[0], var.shape[1]])
    # loop over time
    for t in np.arange(var.shape[0]):
    # loop over each depth slice
        for d in np.arange(var.shape[1]):
            ave[t,d] = np.ma.average(var[t,d,:], weights = weights)
    return ave

which I then use to plot

ax=plt.axes(projection=ccrs.Robinson())

ax.coastlines()

ax.contourf(x,y, ann_total_5tg)

But this code gives me a one dimension shape, over time, which I can't plot into cartopy using pcolor mesh.

I am left with the error

TypeError: Input z must be a 2D array.

Would it be possible to get an annual mean whilst maintaining variables within the datasheet?

1

There are 1 best solutions below

0
On

I suspect that you have to reshape your numpy array to use it with the contour method. Using your variable name it can be done like this :

ann_total_5tg = ann_total_5tg.reshape((180, 180))