How to create nc file having lat, long, time, and 2D matrix

Helo everyone,
I am trying to create a .nc file which will have lat, long, time and 2D matrix simultaneously.
I have loaded lat, long, and time parameters , but how will I put the corresponding 2D matrix for each lat long and time while creating .nc file.
The answer should come in a matrix which has time in 1st column, lat and long in 1st & 2nd rows, and parameter values corresponding to time and lat_long.
Thank you in advance

 Accepted Answer

Refer this demo example:
% 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

14 Comments

Hello KSSV
Thank you for your help.
Since
I get some problems while running above code. Since I have different dimensions of lat (1x48),lon (1x52), time(1x4270), and a matrix of (4270 x 785), which I have exracted from a .nc and modified it. Values are given for each la, long, and time.
Now again I want to create .nc file having same dimensions that had before modifying.
Your consideration will help me.
YOu can use the above code.......you might need to transpose before the writitng the matrix into .nc file.
I am bothering you but i want to know what is wrong with these codes
file = 'HW_05042019.nc';
HW=xlsread('HW.xlsx');
% here HW comes as a matrix of 4279 x 785
lon = 70:.5:95.5000 ;
lat = 8.5000:.5:32 ;
nx = length(lon) ;
nccreate(file,'lon','Dimensions',{'lon',1,nx},'DeflateLevel',7) ;
ny = length(lat) ;
nccreate(file,'lat','Dimensions',{'lat',1,ny},'DeflateLevel',7) ;
time=1:4270;
nt=length(time);
nccreate(file,'time','Dimensions',{'time',1,nt},'DeflateLevel',7) ;
nccreate(file,'HW','Dimensions',{'lon','lat','time'},'DeflateLevel',7) ;
for i = 1:4270
ncwrite(file,'time',i,i) % write time
data = HW ;
ncwrite(file,'HW',data,[1,1,i]) ; % write 3D data
end
% Can you help me with this
Thank you in advance @KSSV
Does it generate any error? What is the problem?
% This error comes when I run the loop
Error using netcdflib
The NetCDF library encountered an error during execution of 'putVaraDouble' function - 'Start+count exceeds dimension bound
(NC_EEDGE)'.
Error in netcdf.putVar (line 84)
netcdflib(funcstr,ncid,varid,varargin{:});
Error in internal.matlab.imagesci.nc/write (line 835)
netcdf.putVar(gid,varid, start, count, varData);
Error in ncwrite (line 75)
ncObj.write(varName, varData, start, stride);
This should also give a line number where error occurred. Mention the line number.
NC_Creation_04042019
Error using netcdflib
The NetCDF library encountered an error during execution of 'putVaraDouble' function - 'Start+count exceeds dimension bound
(NC_EEDGE)'.
Error in netcdf.putVar (line 84)
netcdflib(funcstr,ncid,varid,varargin{:});
Error in internal.matlab.imagesci.nc/write (line 835)
netcdf.putVar(gid,varid, start, count, varData);
Error in ncwrite (line 75)
ncObj.write(varName, varData, start, stride);
Error in NC_Creation_04042019 (line 18)
ncwrite(file,'HW',data,[1,1,i]) ; % write 3D data
What is the size of data/hw ?
But your lon and lat are of size 48, 52....obviously you will get error.
Try this:
file = 'HW_05042019.nc';
HW=xlsread('HW.xlsx');
% here HW comes as a matrix of 4279 x 785
lon = linspace(70,95.5,785) ;
lat = linspace(8.50,32,4270) ;
nx = length(lon) ;
nccreate(file,'lon','Dimensions',{'lon',1,nx},'DeflateLevel',7) ;
ny = length(lat) ;
nccreate(file,'lat','Dimensions',{'lat',1,ny},'DeflateLevel',7) ;
time=1:4270;
nt=length(time);
nccreate(file,'time','Dimensions',{'time',1,nt},'DeflateLevel',7) ;
nccreate(file,'HW','Dimensions',{'lon','lat','time'},'DeflateLevel',7) ;
for i = 1:4270
ncwrite(file,'time',i,i) % write time
data = HW ;
ncwrite(file,'HW',data,[1,1,i]) ; % write 3D data
end
This is 0.5 x 0.5 grid data.
we can't use linspace syntax.
The thing is lat and long are varying from 8.5 to 32 and 70 to 95.5, and total range of lat and lon is 785, respectively i.e. the data are irregular & they are in first 2 rows. Rest of the matrix have corresponding values (4270 x 785)
Is there any option I can create nc file.
thank you for helping me KSSV
Attach your xlsx file.
Here I have attached a small file of my excel file.
Time is varying from 3rd row to 52 row in 1 st column and first 2 row has lon and lat.

Sign in to comment.

More Answers (1)

I hope this helps.
[data]=xlsread('test.csv');
lat=data(2,2:end);
lon=data(1,2:end);
vardata=data(3:end,2:end);
vardata(:,size(vardata,2)+1)=nan; %dummy column just to put nan over the points where data is not available
longitude=unique(lon);
latitude=unique(lat);
[lon_m,lat_m]=meshgrid(longitude,latitude);
index=arrayfun(@(x,y) find(lon==x & lat==y),lon_m,lat_m,'uni',0);
index(cellfun('isempty',index))={-9999.99};
datavar=str2double(string(index)); datavar(datavar==-9999.99)=786;
out=arrayfun(@(x) reshape(vardata(x,datavar),[48 51]),1:50,'uni',0);
outputdata=cat(3,out{:});
nccreatewrite('test.nc','lat',{'lat','c'},latitude')
nccreatewrite('test.nc','lon',{'lon','c'},longitude')
nccreatewrite('test.nc','TC',{'lat','lon','days'},outputdata)
You can check the validity of the above data by typing
squeeze(outputdata(latitude==32,longitude==75.5,:))
The output of the above line is the same as the second column in the excel file (of course by skipping the first two rows).

Asked:

on 4 Apr 2019

Answered:

on 9 May 2019

Community Treasure Hunt

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

Start Hunting!