![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/682383/image.jpeg)
How to create a netcdf in matlab which can be read by grads without using descriptor file?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
UTKARSH VERMA
le 7 Jan 2021
Modifié(e) : ANKUR KUMAR
le 12 Juil 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.
0 commentaires
Réponse acceptée
ANKUR KUMAR
le 12 Juil 2021
Modifié(e) : ANKUR KUMAR
le 12 Juil 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/682383/image.jpeg)
0 commentaires
Plus de réponses (1)
KSSV
le 26 Fév 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
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!