MATLAB Answers

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

9 views (last 30 days)
UTKARSH VERMA
UTKARSH VERMA on 7 Jan 2021
Edited: ANKUR KUMAR on 12 Jul 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.

Accepted Answer

ANKUR KUMAR
ANKUR KUMAR on 12 Jul 2021
Edited: ANKUR KUMAR on 12 Jul 2021
There are often times when MATLAB created NetCDF files are not readable in earth and atmospheric science visualization softwares like GrADS or NCL. Or one needs to write its seprate descriptor file to get it read in GrADS. Refer this function nccreatewrite on file exchagne which enables MATLAB created nc file readable in GrDAS. Here is an example for you.
clc
clear
random_data = normrnd(3,10,[20,30,5])+273;
filename='sample.nc';
delete(filename)
Warning: File 'sample.nc' not found.
lon_att.standard_name='longitude';
lon_att.long_name='longitude';
lon_att.units='degrees_east';
lon_att.axis='X';
nccreatewrite(filename,'lon',{'lon'},[65:84],lon_att)
att_fields = 4×2 cell array
{'standard_name'} {'longitude' } {'long_name' } {'longitude' } {'units' } {'degrees_east'} {'axis' } {'X' }
------------------------------------------- | Succesfully written lon in file sample.nc | --------------------------------------------
lat_att.standard_name='latitude';
lat_att.long_name='latitude';
lat_att.units='degrees_north';
lat_att.axis='Y';
nccreatewrite(filename,'lat',{'lat'},[1:30],lat_att)
att_fields = 4×2 cell array
{'standard_name'} {'latitude' } {'long_name' } {'latitude' } {'units' } {'degrees_north'} {'axis' } {'Y' }
------------------------------------------- | Succesfully written lat in file sample.nc | --------------------------------------------
lev_att.long_name='generic';
lev_att.units='level';
lev_att.axis='Z';
nccreatewrite(filename,'lev',{'lev'},[1:5],lev_att)
att_fields = 3×2 cell array
{'long_name'} {'generic'} {'units' } {'level' } {'axis' } {'Z' }
------------------------------------------- | Succesfully written lev in file sample.nc | --------------------------------------------
nccreatewrite(filename,'TC',{'lon','lat','lev'},random_data)
Warning: You are not writing any Attributes to TC variable
------------------------------------------- | Succesfully written TC in file sample.nc | --------------------------------------------
ncdisp(filename)
Source: /users/mss.system.TaLGpO/sample.nc Format: netcdf4 Dimensions: lon = 20 lat = 30 lev = 5 Variables: lon Size: 20x1 Dimensions: lon Datatype: double Attributes: standard_name = 'longitude' long_name = 'longitude' units = 'degrees_east' axis = 'X' lat Size: 30x1 Dimensions: lat Datatype: double Attributes: standard_name = 'latitude' long_name = 'latitude' units = 'degrees_north' axis = 'Y' lev Size: 5x1 Dimensions: lev Datatype: double Attributes: long_name = 'generic' units = 'level' axis = 'Z' TC Size: 20x30x5 Dimensions: lon,lat,lev Datatype: double
Lets plot the data from created nc file
filename='sample.txt'; % This is actually a netCDF file. I have just manually changed the
% %file extension in order to attach the file hee in the answers.
lon=ncread(filename,'lon');
lat=ncread(filename,'lat');
lev=ncread(filename,'lev');
tc=ncread(filename,'TC');
Let use contourf to plot how our data looks like
contourf(lon,lat,tc(:,:,3)')
hold on
daspect(ones(1,3))
load coastlines
plot(coastlon, coastlat, 'r')
axis([65 84 1 30])
colormap(parula)
colorbar
You can use the same .nc file to plot in GrDAS. Here is an example for you.
% sdfopen sample.nc
% q file
% set lev 3
% set gxout shaded
% d tc
You will get a warning like above, and you can ignore it for a moment. I am working on it to resolve this warning.
You will get an image like this in GrADS.

More Answers (1)

KSSV
KSSV on 26 Feb 2021
% nc filename to be written
file = 'myfile.nc' ;
%% 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
end
  1 Comment
UTKARSH VERMA
UTKARSH VERMA on 28 Feb 2021
Hi KSSV,
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 myfile.nc
Scanning self-describing file: myfile.nc
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 http://iges.org/grads/gadoc/SDFdescriptorfile.html
ga->
Can you suggest some changes or addition to above code?
Best,
Utkarsh

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