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.

 Akzeptierte Antwort

KSSV
KSSV am 18 Jun. 2020
Bearbeitet: KSSV am 18 Jun. 2020

3 Stimmen

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 Kommentare

Gokhan Kayan
Gokhan Kayan am 18 Jun. 2020
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.
KSSV
KSSV am 18 Jun. 2020
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]) ;
KSSV
KSSV am 18 Jun. 2020
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') ;
KSSV
KSSV am 18 Jun. 2020
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
KSSV
KSSV am 18 Jun. 2020
Edited the answer.
KSSV
KSSV am 18 Jun. 2020
Bearbeitet: KSSV am 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.
Gokhan Kayan
Gokhan Kayan am 18 Jun. 2020
Thanks. You are great, It worked perfectly :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by