MATLAB Answers

How to edit this code?

3 views (last 30 days)
Behzad Navidi
Behzad Navidi on 1 Nov 2019
Commented: Behzad Navidi on 2 Nov 2019
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

  4 Comments

Show 1 older comment
Behzad Navidi
Behzad Navidi on 1 Nov 2019
I was download it directly from ECWMF website. I haven't any other version of it :(
Daniel M
Daniel M on 1 Nov 2019
Note that length(0:23) is 24. So hourly data starting from 00:00 and going to 23:00 probably contains the full data for that day. I only think this because it would be of utmost nonsense to require a user to look at 2 days of data to capture data for 1 day.
Behzad Navidi
Behzad Navidi on 2 Nov 2019
If the variable was something like temperature that records every hour and independent from later hours, this was true, but here the variable is total precipitation. Each hour precipitation of one past hour will be recorded. So if we want to know how much rainfall was accrued in a total of the day, we need to analyze 00:00 of next day to understand the value for 23:00 to 00:00. Actually, this data is for the European Centre for Medium-Range Weather Forecasts (ECMWF) ERA5, which recently was released.
In the ERA5 page, the python code was presented for this purpose.
But unfortunately, still, now, the Matlab code doesn't exist. It could be beneficial for all students and researchers around the world if any Matlab code for this purpose exists.
Thank You :)

Sign in to comment.

Answers (0)

Sign in to answer this question.