-1

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)
  • There data operations are very common to do in pandas, is it ok to use that library in your solution? – Willem Hendriks Jul 15 '20 at 19:15
  • yes that is okay – megamart27 Jul 15 '20 at 19:19
  • This would be more easily solved using xarray's groupby operations. The example given on the website is a good starting point and can actually be modified to solve your problem: http://xarray.pydata.org/en/stable/generated/xarray.DataArray.groupby.html – Robert Wilson Jul 16 '20 at 13:34

1 Answers1

0

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()
DopplerShift
  • 5,472
  • 1
  • 21
  • 20
  • Thank you, this worked. How do I actually view the max temp data? I am new to xarray and can't figure out how to look at the numbers to confirm that they're correct – megamart27 Jul 19 '20 at 18:14