How to create a netcdf in matlab which can be read by grads without using descriptor file?
17 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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)
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)
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)
lev_att.long_name='generic';
lev_att.units='level';
lev_att.axis='Z';
nccreatewrite(filename,'lev',{'lev'},[1:5],lev_att)
nccreatewrite(filename,'TC',{'lon','lat','lev'},random_data)
ncdisp(filename)
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.
0 Comments
More Answers (1)
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
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!