Does ncwrite work for multi-dimensional matrix?
2 views (last 30 days)
Show older comments
I was trying to write a 3D matrix into the .nc file that I created with nccreate. The information dumped by ncdisp function indicated the file is properly created.
Here is the code I used to create and write the file.
nccreate('ERA5_2015_EVAm.nc','longitude','Dimensions',{'longitude',360},'Datatype','single','Format','64bit');
ncwriteatt('ERA5_2015_EVAm.nc','/','Conventions','CF-1.6');
ncwriteatt('ERA5_2015_EVAm.nc','longitude','units','degrees');
ncwriteatt('ERA5_2015_EVAm.nc','longitude','long_name','longitude');
nccreate('ERA5_2015_EVAm.nc','latitude','Dimensions',{'latitude',180},'Datatype','single');
ncwriteatt('ERA5_2015_EVAm.nc','latitude','units','degrees');
ncwriteatt('ERA5_2015_EVAm.nc','latitude','long_name','latitude');
nccreate('ERA5_2015_EVAm.nc','time','Dimensions',{'time',8760},'Datatype','int32');
ncwriteatt('ERA5_2015_EVAm.nc','time','units','hour since 1900-01-01 00:00:0.0');
ncwriteatt('ERA5_2015_EVAm.nc','time','long_name','time');
ncwriteatt('ERA5_2015_EVAm.nc','time','calendar','gregorian');
nccreate('ERA5_2015_EVAm.nc','e','Dimensions',{'longitude',360,'latitude',180,'time',8760},'Datatype','int16');
ncwriteatt('ERA5_2015_EVAm.nc','e','units','m of water equivalent');
ncwriteatt('ERA5_2015_EVAm.nc','e','long_name','Evaporation');
ncwriteatt('ERA5_2015_EVAm.nc','e','standard_name','lwe_thickness_of_water_evaporation_amount');
time = ncread('ERA5_2015_EVA.nc','time');
longitude = double(ncread('ERA5_2015_EVA.nc','longitude'));
lon(1:180,1) = longitude(181:360)-360;
lon(181:360,1) = longitude(1:180);
lon(361,1) = 180;
lat = double(ncread('ERA5_2015_EVA.nc','latitude'));
e = double(ncread('ERA5_2015_EVA.nc','e'));
eva(1:180,:,:) = e(181:360,:,:);eva(181:360,:,:) = e(1:180,:,:);eva(361,:,:) = e(181,:,:);
clear e;
[x0,y0] = meshgrid(lon,lat);
[x,y] = meshgrid(-179.5:179.5,-89.5:89.5);
for i = 1:8760
e(1:360,1:180,i) = interp2(x0,y0,eva(:,:,i)',x,y,'nearest')';
end
clear eva;
ncwrite('ERA5_2015_EVAm.nc','longitude',-179.5:179.5);
ncwrite('ERA5_2015_EVAm.nc','latitude',-89.5:89.5);
for Ilon = 1:360
for Ilat = 1:180
ncwrite('ERA5_2015_EVAm.nc','e',e(Ilon,Ilat,:),[Ilon Ilat 1]);
end
end
ncwrite('ERA5_2015_EVAm.nc','time',time);
The longitude, latitude and time has been properly input into the file while the 'e' is not. The 'e' I read from the .nc file after writing is zero at all grids as indicated below,
e = double(ncread('ERA5_2015_EVAm.nc','e'));
figure;pcolor(e(:,:,1));shading interp;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168934/image.jpeg)
while the actual distribution of 'e' should be like this,
figure;pcolor(e(:,:,1));shading interp;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168935/image.jpeg)
I am sure I did something stupidly wrong, hoping for suggestion from the community!
Many thanks!
Shenjie
2 Comments
Jan
on 15 Jan 2018
Please improve the readability of your code: https://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Answers (0)
See Also
Categories
Find more on Read, Write, and Modify Image 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!