Calculate daily, monthly, annual averages from hourly data

32 views (last 30 days)
I have hourly data, I want to be able to create daily, monthly and annual averages while taking into account the bisexile years.
These are hourly data from a netcdf file. So I have a temperature variable and a time variable
Data are available from January 1, 2003 to June 30, 2017
For temperature and time variables
Format:
classic
Dimensions:
time = 127080
Variables:
tas_sirta
Size: 127080x1
Dimensions: time
Datatype: single
Attributes:
units = '°C'
long_name = 'air_temperature'
standard_name = 'air_temperature'
name = 'tas'
dim = 'time'
Format:
classic
Dimensions:
time = 127080
Variables:
time
Size: 127080x1
Dimensions: time
Datatype: single
Attributes:
long_name = 'date_time'
units = 'hours since 1970-01-01'
actual_range = '289272.0, 416351.0'
standard_name = 'time'
name = 'time'
calendar = 'standard'
dim = 'time'
missing_value = NaN
I transform dates with datenum
timee = double(time)/24 + datenum('1970-01-01 00:00:00');
datestr(timee(1:7,1),'dd-mm-yyyy HH:MM:SS')
J'obtiens bien
ans =
01-01-2003 00:00:00
01-01-2003 01:00:00
01-01-2003 02:00:00
01-01-2003 03:00:00
01-01-2003 04:00:00
01-01-2003 05:00:00
01-01-2003 06:00:00
but I do not know then to make the daily, monthly and annual averages taking into account all the specificities of a year (February with 28-29 days, month with 30, 31 days ...)
thank you

Accepted Answer

Guillaume
Guillaume on 5 Dec 2017
The simplest way to do what you want is to convert your data into a timetable:
hourlytemp = timetable(hours(time) + datetime(1970, 1, 1), tas_sirta, 'VariableNames', {'air_temperature'});
Then retime that timetable to monthly, daily, yearly or whatever period you want:
monthlyaverage = retime(hourlytemp, 'monthly', 'mean')
Done!
  9 Comments
Polina S
Polina S on 14 Oct 2019
Thank you, Guillaume! I have an extra question..
I have a range of three-year data measured twice a day. I need to get mean monthly temperature averaged over all given years. I've used retime to get monthly average. But how can I average them over the years?
t = datetime (year, month, day, hour, minute, second);
hourlytemp = timetable(t, TempSurf);
monthlyaverage = retime (hourlytemp, 'monthly', 'mean')
monthlyaverage has a dimension 96x1 now. I need to get 12x1
Guillaume
Guillaume on 14 Oct 2019
retime can't do this but groupsummary can:
monthofyearaverage = groupsummary(hourlytemp, 'Time', 'monthofyear', 'mean')
groupsummary is probably more appropriate for the original question as well, as it lets you calculate multiple statistics at once. However, groupsummary did not exist when the question was asked. It requires R2018a or later.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!