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

조회 수: 13 (최근 30일)
Jitesh Dadich
Jitesh Dadich 2019년 4월 4일
답변: ANKUR KUMAR 2019년 5월 9일
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

채택된 답변

KSSV
KSSV 2019년 4월 4일
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
KSSV
KSSV 2019년 4월 5일
Attach your xlsx file.
Jitesh Dadich
Jitesh Dadich 2019년 4월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

ANKUR KUMAR
ANKUR KUMAR 2019년 5월 9일
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).

카테고리

Help CenterFile Exchange에서 NetCDF에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by