Search code examples
pythondatetimenetcdfnetcdf4

How to find maximum daily temperature from hourly data netcdf file in Python?


I am trying to find the maximum daily temperature from hourly data (3 hour intervals). I am new to netcdf files and python so I'm not sure where to start. How would I find the maximum daily value? Can I use max() for that?

file='air.2m.2018.nc'
ncin = Dataset(file,'r')
#put data into numpy arrays
lons=ncin.variables['lon'][:]
lats=ncin.variables['lat'][:]
lats1=ncin.variables['lat'][:,0]
temp=ncin.variables['air'][:]

time_2018=ncin.variables['time']
dtime = netCDF4.num2date(time_2018[:],time_2018.units)

When I print(dtime) it looks like this:

 cftime.DatetimeGregorian(2018, 1, 1, 0, 0, 0, 0)
 cftime.DatetimeGregorian(2018, 1, 1, 3, 0, 0, 0)

Solution

  • This should be pretty easy to achieve with xarray:

    import xarray as xr
    nc = xr.open_dataset('air.2m.2018.nc')
    nc.resample(time='1D').max()