frank
frank

Reputation: 91

python average of multidimensional array netcdf plot

I read a multidimensional array from netCDF file. The variable that I need to plot is named "em", and it has 4 dimensions ´em (years, group, lat, lon)´

The "group" variable has 2 values, I am interested only of the first one. So the only variable that I need to manage is the "years" variable. The variable "years" has 17 values. For the first plot I need to average the first 5 years, and for the second plot I have to aveage from 6th years to the last years.

data = Dataset (r'D:\\Users\\file.nc') 
lat = data.variables['lat'][:]
lon = data.variables['lon'][:]
year = data.variables['label'][:]
group  = data.variables['group'][:] 
em= data.variables['em'][:]

How can I create a 2 dimensional array avareging for this array ? First one :

`em= data.variables['em'][0:4][0][:][:]`

Second one :

em= data.variables['em'][5:16][0][:][:]

I create a simple loop

nyear=(2005-2000)+1
for i in range (nyear):
    em_1= data.variables['em'][i][0][:][:]
    em_1+=em_1
em_2000_2005=em_1/nyear

but I think there could be more elegant easy way to this on python

Upvotes: 1

Views: 1517

Answers (2)

wingtorres
wingtorres

Reputation: 11

I would highly recommend using xarray for working with NetCDF files. Rather than keeping track of indices positionally, you can operate on them by name which greatly improves code readability. In your example all you would need to do is

import xarray as xr

ds = xr.open_dataset(r'D:\\Users\\file.nc')
em_mean1 = ds.em.isel(label = range(6,18)).mean()
em_mean2 = ds.em.isel(label = range(6)).mean()

the .isel method selects the indices of the specified dimension (label in this case), and the .mean() method computes the average over the selection.

Upvotes: 1

msi_gerva
msi_gerva

Reputation: 2078

You can use NumPy:

em = data.variables['em'][:];
em_mean = np.mean(em,axis=0) # if you want to average all over first dimension

If data contains NaN's, just use NumPY's nanmean.

As you wanted to average first 3 values, for the first case, use:

em_mean1 = np.squeeze(np.mean(em[0:4,:],axis=0)) 

and take for the plot:

em_mean1 = np.squeeze(em_mean1[0,:]);

You can do similar for the second case.

Upvotes: 0

Related Questions