Having a NetCDF with a 'time', 'lat' and 'lon' dimension, where time is hourly during a year and the variable to extract is temperature, how do I calculate mean temperature throughout the year using Python? (using Python and netcdf4 or xarray or numpy)
I would use xarray.Dataset.resample
. xarray
is very handy for time series and comes with several temporal dimension handling methods, some of which taken from the comprehensive pandas
. Check the docs here. If this does not solve the issue, please add some more context as Rowan asked in the comment.
import numpy as np
import pandas as pd
import xarray as xr
da = xr.DataArray(np.linspace(0, 11, num=12), coords=[pd.date_range('15/12/1999',
periods=12, freq=pd.DateOffset(months=1))], dims='time')
da.resample(time="A").mean()
Have a look at different frequency offsets you can input as parameters in the .resample
method, in the pandas
documentation here.