MATLAB Answers

how to write attribute in netcdf file when creating it

142 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?

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 29 Aug 2017

  1 Comment

Tanmoyee Bhattacharya
Tanmoyee Bhattacharya on 30 Aug 2017
I have done.
end
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});
ncwriteatt('test_files.nc', 'lat', 'standard_name', 'latitude');
ncwriteatt('test_files.nc', 'lat', 'long_name', 'latitude');
ncwriteatt('test_files.nc', 'lat', 'units', 'degrees north');
ncwriteatt('test_files.nc', 'lat', '_CoordinateAxisType', 'Lat');
% ncwriteatt('test_files.nc','/','standard_name','latitude');
nccreate('test_files.nc','lon','Dimensions',{'lon' 4});
ncwriteatt('test_files.nc', 'lon', 'standard_name', 'longitude');
ncwriteatt('test_files.nc', 'lon', 'long_name', 'longitude');
ncwriteatt('test_files.nc', 'lon', 'units', 'degrees north');
ncwriteatt('test_files.nc', 'lon', '_CoordinateAxisType', 'Lon');
nccreate('test_files.nc','time','Dimensions',{'time' 9862});
ncwriteatt('test_files.nc', 'time', 'long_name', 'Time variable');
ncwriteatt('test_files.nc', 'time', 'units', 'days since 1979-01-01 00:00:00');
ncwriteatt('test_files.nc', 'time', '_CoordinateAxisType', 'Time');
nccreate('test_files.nc','lev','Dimensions',{'lev' 4});
ncwriteatt('test_files.nc', 'lev', 'standard_name', 'pressure');
ncwriteatt('test_files.nc', 'lev', 'long_name', 'pressure');
ncwriteatt('test_files.nc', 'lev', 'units', 'Pa');
ncwriteatt('test_files.nc', 'lev', '_CoordinateAxisType', 'Z');
nccreate('test_files.nc','Q','datatype','single','Dimensions',{'lon' 4 'lat' 3 'lev' 4 'time' 9862});
ncwriteatt('test_files.nc', 'Q', 'standard_name', 'specific humidity');
ncwriteatt('test_files.nc', 'Q', 'long_name', 'Specific humidity');
ncwriteatt('test_files.nc', 'Q', 'units', 'kg kg**-1');
ncwriteatt('test_files.nc', 'Q', 'missing_value', '-1e+04');
% 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');
Thank you.

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!