How to edit this code?
2 views (last 30 days)
Show older comments
Hello all,
Recently I was trying to download ECMWF ERA5 precipitation data. the precipitation has 3 dimensions (lon x lat x time) the format of the file is NetCDF. data are in hourly time steps. each day begins at 00:00 and ends at 23:00. The NetCDF file includes hourly data for a year, so it has 365*24=8760 time steps. I would like to convert these hourly data daily but one main issue stands in front of me. I explain my issue with a very simple example:
to cover total precipitation for 1st January 2017 (for example), we need two days of data:
A) 1st January 2017 time = 01 - 23 will give you total precipitation data to cover 00 - 23 UTC for 1st January 2017
B) 2nd January 2017 time = 00 will give you total precipitation data to cover 23 - 24 UTC for 1st January 2017
So I need a code that Starts every day at 01:00 and ends at 00:00 next day and saves this value as a value of one day, rather than takes each day's time range between 00:00 to 23:00. And do it for each day in the year.
I have 2 types of code that unfortunately didn't consider this issue. if you kindly please help me edit this code I would be grateful. let me know if any other questions you have.
Code number 1:
filename='download.nc'; %Name of netcdf file
ncdisp(filename) %show summary of nc
lat = ncread(filename,'latitude'); %reading latitude
lon = ncread(filename,'longitude'); %reading longitude
precip = ncread(filename,'tp'); %reading the main variable
t = ncread(filename,'time'); %read time
daily_precip = squeeze(nansum(reshape(precip, size(precip, 1), size(precip, 2), 24, []), 3));
dt = datetime(double(t)*3600, 'ConvertFrom', 'epochtime', 'Epoch', '01-Jan-1900');
[group, actualday] = discretize(dt, 'day');
daily_precip = splitapply(@(pages) {nansum(precip(:, :, pages), 3)}, (1:size(precip, 3))', group);
daily_precip = cat(3, daily_precip{:});
And the code numebr two:
filename='download.nc'; %Name of netcdf file
ncdisp(filename) %show summary of nc
lat = ncread(filename,'latitude'); %reading latitude
lon = ncread(filename,'longitude'); %reading longitude
precip = ncread(filename,'tp'); %reading the main variable
t = ncread(filename,'time'); %read time
tp_daily=zeros(size(precip,1),size(precip,2),365);
for ii=0:364
day=precip(:,:,ii*24+1:(ii+1)*24); %grab an entire day
tp_daily(:,:,ii+1)=nansum(day,3); % add the third dimension
end
please download my netcdf file using this link below:
Thank you everyone
5 Comments
MarineM
on 19 May 2021
Hi, I have the same issue here. I am just wondering if 2 years later someone did discover a code for Matlab. Thank you :)
Answers (0)
See Also
Categories
Find more on Climate Science and Analysis 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!