I am a bit of a netCDF in python noob so please excuse this noob question.
I have a folder filled with circa 3650 netCDF4 files. One file per day for a decade. the niles are named yyyymmdd.nc (e.g. 20100101,20100102,20100103,etc.). Each .nc file contains latitude, longitude, and temperature at one-time point for the same area - a section of the Tonga EEZ.
What I am trying to do is compute the average temperature for each lat and lon from across all files, i.e. I want to end up with one .nc file that has all the same lats and lons and average temperature across 10 years.
I have tried different things/versions of code, usually, they end up looking something like this.....
files = glob('*.nc')
ds = xr.open_mfdataset(files,)
mean = np.mean(ds['temp'][:, 0].values)
...... This code would give me the average temperature within a .nc file for all .nc files and not the average temperature based on lat and lon across a decade worth of files.
All and any help is much appreciated.
Thank you.
Assuming you are working on linux/macOS, this can be done easily using my nctoolkit package(see details here).
The following will calculate the mean across all files and then plot the results:
import nctoolkit as nc
files = glob('*.nc')
ds = nc.open_data(file)
ds.ensemble_mean()
ds.plot()
nctoolkit uses CDO as a back-end by default, but can use NCO as well, which can result in a performance improvement. So the following might be faster:
import nctoolkit as nc
files = glob('*.nc')
ds = nc.open_data(file)
ds.ensemble_mean(nco=True)
ds.plot()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With