-1

I have hourly data from ECMWF ERA5 for each day in a specific year. I want to convert that data from hourly to daily. Copernicus has a Python code for this here https://confluence.ecmwf.int/display/CKB/ERA5%3A+How+to+calculate+daily+total+precipitation.
I want to know what is the matlab code to do this? I was upload the netcdf file in my google drive here: https://drive.google.com/open?id=1qm5AGj5zRC3ifD1_V-ne2nDT1ch_Khik time steps of each day are:

0:00
1:00
2:00
3:00
4:00
5:00
6:00
7:00
8:00
9:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00

Notice to cover total precipitation for 1st January 2017 for example, we need two days of data: 1st January 2017 time = 01 - 23 will give you total precipitation data to cover 00 - 23 UTC for 1st January 2017 2nd January 2017 time = 00 will give you total precipitation data to cover 23 - 24 UTC for 1st January 2017 here is ncdisp():

>> ncdisp(filename)
Source:
           C:\Users\Behzad\Desktop\download.nc
Format:
           64bit
Global Attributes:
           Conventions = 'CF-1.6'
           history     = '2019-11-01 07:36:15 GMT by grib_to_netcdf-2.14.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -o /cache/data6/adaptor.mars.internal-1572593007.3569295-19224-27-449cad76-bcd6-4cfa-9767-8a3c1219c0bb.nc /cache/tmp/449cad76-bcd6-4cfa-9767-8a3c1219c0bb-adaptor.mars.internal-1572593007.35751-19224-4-tmp.grib'
Dimensions:
           longitude = 49
           latitude  = 41
           time      = 8760
Variables:
    longitude
           Size:       49x1
           Dimensions: longitude
           Datatype:   single
           Attributes:
                       units     = 'degrees_east'
                       long_name = 'longitude'
    latitude 
           Size:       41x1
           Dimensions: latitude
           Datatype:   single
           Attributes:
                       units     = 'degrees_north'
                       long_name = 'latitude'
    time     
           Size:       8760x1
           Dimensions: time
           Datatype:   int32
           Attributes:
                       units     = 'hours since 1900-01-01 00:00:00.0'
                       long_name = 'time'
                       calendar  = 'gregorian'
    tp       
           Size:       49x41x8760
           Dimensions: longitude,latitude,time
           Datatype:   int16
           Attributes:
                       scale_factor  = 3.0792e-07
                       add_offset    = 0.010089
                       _FillValue    = -32767
                       missing_value = -32767
                       units         = 'm'
                       long_name     = 'Total precipitation'

tp is my variable which have 3 dimensions (lon*lat*time) = 49*41*8760 I want it in the 49*41*365 for a non-leap year.
The result should be the daily values for the whole year.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Behzad
  • 49
  • 8
  • You want to add every 24 values in the 3D dimension together then? – Ander Biguri Nov 01 '19 at 11:40
  • @AnderBiguri this NetCDF presents `tp` on an hourly basis for a year, I want to have it on a daily basis for a year. – Behzad Nov 01 '19 at 11:45
  • OK, but try to ask a programming question, rather than to make us understand your entire dataset and project. You seem to have a 3D matrix that contains slices of 2D information per hour. What is your expected outcome? You wan to add (or average?) every 24 slices in the third dimension? – Ander Biguri Nov 01 '19 at 12:00
  • @AnderBiguri yes you right. I want to sum the hourly data in order to produce daily data. ok, I delete this question here and ask as programming question, thanks – Behzad Nov 01 '19 at 12:03
  • No, no,leave it! I just wanted clarification. – Ander Biguri Nov 01 '19 at 12:10

1 Answers1

2

While some vectorized versions may exist that reshape your vector into 4 dimensions, a simple for loop will do the job.

tp_daily=zeros(size(tp,1),size(tp,2),365);
for ii=0:364
    day=tp(:,:,ii*24+1:(ii+1)*24); %grab an entire day 
    tp_daily(:,:,ii+1)=sum(day,3); % add the third dimension
end
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • @anderBigure this error leads to this error: (:,:,ii+1)=sum(tp,3); % add the third dimension ↑ Error: Invalid use of operator. – Behzad Nov 01 '19 at 12:38
  • @Behzad just minor typos.Please try to understand the code and not clindly copy paste it – Ander Biguri Nov 01 '19 at 12:57
  • sorry man I am really new to MATLAB, i don't know what to do – Behzad Nov 01 '19 at 13:40
  • @Behzad well, learn it then ;) this is a good start. I fixed the code btw, you shoudl try it again – Ander Biguri Nov 01 '19 at 13:43
  • 1
    @AnderBiguri, doesn't look like he's trying hard to learn: https://stackoverflow.com/questions/58662100/matlab-how-to-edit-this-code-in-order-to-consider-wider-range – Hoki Nov 01 '19 at 17:31
  • For someone inexperienced in coding, maybe CDO is easier, check out: https://stackoverflow.com/questions/59815665/calculating-era5-daily-total-precipitation-using-cdo/59946046#59946046 – ClimateUnboxed Mar 19 '20 at 11:40