Reading 3-dimensional netcdf data

I downloaded precipitation data from SIM2RAIN. It is 3 dimensional and temporal resolution is 1 day. For example for year 2005, my file size is 365(days)*1440(long)*720(lat). I want to show precipitation data for each day in matrix (1440,720). So, It means ı will have 365 files. My file name is CCI_SM2RAIN_2005_BC.nc. I can do it by using Arcmap but it is very time consuming. How can I do this in matlab ? Thanks for help.

 Accepted Answer

KSSV
KSSV on 18 Jun 2020
Edited: KSSV on 18 Jun 2020
You can read your nc file like shown below....Change the variables in the below to your variables naming converntion in your file.
ncfile='CCI_SM2RAIN_2005_BC.nc'
lon = ncread(ncfile,'Longitude') ; nx = length(lon) ;
lat = ncread(ncfile,'Latitude') ; ny = length(lat) ;
time = ncread(ncfile,'Time') ;
for i = 1:length(time)
z = ncread(ncfile,'Rainfall',[i 1 1 ],[1, nx ny]) ;
z = squeeze(z) ;
pcolor(lon,lat,z') ;
title(sprintf("Time step = %d",i)) ;
shading interp
drawnow
end

9 Comments

Thanks for the comment KSSV. But the file does not accept longitude latitude and time as variables. It gives error when ı try to run it. My only variable is rainfall here.
I already mentioned that you have to change the variable names as according to your file variable names.
I run it again by changing variable names. My new code is like this:
ncfile='CCI_SM2RAIN_2005_BC.nc'
lon = ncread(ncfile,'Longitude') ; nx = length(lon) ;
lat = ncread(ncfile,'Latitude') ; ny = length(lat) ;
time = ncread(ncfile,'Time') ;
for i = 1:length(time)
z = ncread(ncfile,'z',[1 1 i],[nx ny 1]) ;
pcolor(lon,lat,z') ;
shading interp
drawnow
end
And it gives me this errors.
Error using internal.matlab.imagesci.nc/getGroupAndVarid (line 2096)
Could not find variable or group 'z' in file.
Error in internal.matlab.imagesci.nc/read (line 593)
[gid, varid] = getGroupAndVarid(this, location);
Error in ncread (line 66)
vardata = ncObj.read(varName, varargin{:});
Error in nc (line 6)
z = ncread(ncfile,'z',[1 1 i],[nx ny 1]) ;
What is 3D variable? You need to change it from 'z' to what it is in the file.
My 3D variable is 'Rainfall'. I think we should arrange this
z = ncread(ncfile,'Rainfall',[1 1 i],[nx ny 1]) ;
as
z = ncread(ncfile,'Rainfall',[i 1 1],[ 1 nx ny]) ;
because Time is first place for rainfall. 2nd one is longitude and 3rd variable is latitude.
Then I run code
ncfile='CCI_SM2RAIN_2005_BC.nc'
lon = ncread(ncfile,'Longitude') ; nx = length(lon) ;
lat = ncread(ncfile,'Latitude') ; ny = length(lat) ;
time = ncread(ncfile,'Time') ;
for i = 1:length(time)
z = ncread(ncfile,'Rainfall',[i 1 1],[ 1 nx ny]) ;
pcolor(lon,lat,'Rainfall') ;
shading interp
drawnow
end
Now it gives me this error
Error using pcolor (line 58)
Color data input must be a matrix.
Error in nc (line 7)
pcolor(lon,lat,'Rainfall') ;
ncfile='CCI_SM2RAIN_2005_BC.nc'
lon = ncread(ncfile,'Longitude') ; nx = length(lon) ;
lat = ncread(ncfile,'Latitude') ; ny = length(lat) ;
time = ncread(ncfile,'Time') ;
for i = 1:length(time)
z = ncread(ncfile,'Rainfall',[i 1 1],[ 1 nx ny]) ;
pcolor(lon,lat,z') ;
shading interp
drawnow
end
Edited the answer.
KSSV
KSSV on 18 Jun 2020
Edited: KSSV on 18 Jun 2020
Also you can read the entire data into MATLAB workspace using:
z = ncread(ncfile.'Rainfall') ;
for i = 1:length(time)
R = squeeze(z(i,:,:)) ;
pcolor(lon,lat,R')
shading interp
drawnow
end
I have edited the original answer again.....now it will work..check it.
Thanks. You are great, It worked perfectly :)

Sign in to comment.

More Answers (0)

Asked:

on 18 Jun 2020

Commented:

on 18 Jun 2020

Community Treasure Hunt

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

Start Hunting!