MATLAB Answers

How to create a netcdf in matlab which can be read by grads without using descriptor file?

32 views (last 30 days)
UTKARSH VERMA on 7 Jan 2021
Commented: UTKARSH VERMA on 28 Feb 2021
I have data containing lat, lon, time and precipitation values:
lat: 128*1double
lon 256*1double
time 3652*1double
pr 256*128*3652double
I have masked my original data and now I want to create netcdf file which has all the information of original files and most importantly it can be read by grads without the use of descriptor file.

Answers (1)

KSSV on 26 Feb 2021
% nc filename to be written
file = '' ;
%% Write lon and lat variables
% Get data
lon = 1:10 ;
lat = 1:10 ;
nx = length(lon) ;
nccreate(file,'lon','Dimensions',{'lon',1,nx},'DeflateLevel',7) ;
ny = length(lat) ;
nccreate(file,'lat','Dimensions',{'lat',1,ny},'DeflateLevel',7) ;
nccreate(file,'time','Dimensions',{'time',1,Inf},'DeflateLevel',7) ;
nccreate(file,'z','Dimensions',{'lon','lat','time'},'DeflateLevel',7) ;
for i = 1:10
ncwrite(file,'time',i,i) % write time
data = rand(10) ;
ncwrite(file,'z',data,[1,1,i]) ; % write 3D data
  1 Comment
UTKARSH VERMA on 28 Feb 2021
Thanks for replying.
I ran the above code and I tried to open the same file in Grads but it's not working.
Grads shows while opening the above generated nc file:
ga-> sdfopen
Scanning self-describing file:
gadsdf: SDF file has no discernable X coordinate.
To open this file with GrADS, use a descriptor file with an XDEF entry.
Documentation is at
Can you suggest some changes or addition to above code?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by