Clear Filters
Clear Filters

how to write attribute in netcdf file when creating it

47 views (last 30 days)
I have extracted information from one netcdf file and writting it to another netcdf file.I have created the netcdf file by
ncdisp('H:\era\ERA_Q.nc');
lev=ncread('H:\era\ERA_Q.nc','level');
lev1=lev*100;
lat=ncread('H:\era\ERA_Q.nc','latitude');
lon=ncread('H:\era\ERA_Q.nc','longitude');
q=ncread('H:\era\ERA_Q.nc','q');
Q1=single(q);
nccreate('test_files.nc','lat','Dimensions',{'lat' 3});
nccreate('test_files.nc','lon','Dimensions',{'lon' 4});
nccreate('test_files.nc','time','Dimensions',{'time' 9862});
nccreate('test_files.nc','lev','Dimensions',{'lev' 4});
nccreate('test_files.nc','q','datatype','single','Dimensions',{'lon' 4 'lat' 3 'lev' 4 'time' 9862});
% ncdisp('test_files.nc');
ncwrite('test_files.nc','lat',31.5000:.5:32.5000);
ncwrite('test_files.nc','lon',76.5000:.5:78);
ncwrite('test_files.nc','q',Q1);
ncdisp('test_files.nc');
I have to add attributes like
time
Size: 18262x1
Dimensions: time
Datatype: double
Attributes:
long_name = 'Time variable'
units = 'days since 1950-01-01 00:00:00'
_CoordinateAxisType = 'Time'
lat
Size: 6x1
Dimensions: rlat
Datatype: double
Attributes:
standard_name = 'latitude'
long_name = 'latitude'
units = 'degrees north'
_CoordinateAxisType = 'Lat'
lon
Size: 9x1
Dimensions: rlon
Datatype: double
Attributes:
standard_name = 'longitude'
long_name = 'longitude'
units = 'degrees east'
_CoordinateAxisType = 'Lon'
lev
Size: 4x1
Dimensions: lev
Datatype: double
Attributes:
standard_name = 'pressure'
long_name = 'pressure'
units = 'Pa'
_CoordinateAxisType = 'Z'
Q
Size: 6x9x4x18262
Dimensions: rlat,rlon,lev,time
Datatype: single
Attributes:
standard_name = 'specific humidity'
long_name = 'Specific humidity'
units = 'kg kg**-1'
missing_value = -1e+04
How can I do it?

Accepted Answer

KSSV
KSSV on 29 Aug 2017
  4 Comments
UTKARSH VERMA
UTKARSH VERMA on 8 Jan 2021
Edited: UTKARSH VERMA on 8 Jan 2021
Please guide me how to write global attributes to the same file?
Also, I want to know how to create nc file in Matlab so that it can be read by grads without using descriptor file.
GS Louw
GS Louw on 11 Mar 2021
When using the function ncwriteatt(filename,location,attname,attvalue)For global attributes you use '/' at location, so an example would look like the below:
ncwriteatt('test_files.nc', '/', 'geospatial_lat_units','degrees_north');

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!