bikuser
bikuser

Reputation: 2093

How to calculate average value from gridded datasets in python

Hello I have a gridded datasets in .nc format. I tried to read .nc file for different combination of latitude and longitude with the code given below, but i know this is not the ideal way to do that.

import netCDF4
from netCDF4 import Dataset
f= Dataset('data.nc')

data1 =  f.variables['asa'][:,3,23]
data2 =  f.variables['asa'][:,3,24]
data3 =  f.variables['asa'][:,3,25]
data4 =  f.variables['asa'][:,4,23]
data5 =  f.variables['asa'][:,4,24]
data6 =  f.variables['asa'][:,4,25]

and finally

data = np.vstack((data1,data2,data3,data4...))

Data = np.average(data, axis = 0)    

Is there any simple ways to do that instead of reading every single grid and to calculate the average value?? yours help will be highly appreciated.

Upvotes: 1

Views: 1595

Answers (1)

Mikea
Mikea

Reputation: 33

I don't know about nc formatting, but I would expect that you should be able to do

import netCDF4
from netCDF4 import Dataset

f= Dataset('data.nc')
Data = np.average(f.variables['asa'][:,3:5,23:26], axis = 0)

Upvotes: 1

Related Questions