How to define two dimension longitude, latitude and variable for saving data in netcdf format?

조회 수: 47 (최근 30일)
Hello everyone,
I have longitude,laltitude and var data. All are in two dimension. All the provided examples in matlab community are focused on saving 1D latitude,longitude for 2d variable. I have tried below codes so far..
lon=rand(406,271); lat=rand(406,271); var=rand(406,271);
ncid = netcdf.create('Output.nc','NETCDF4');
dim_lon = netcdf.defDim(ncid,'lon',length(lon));
dim_lat = netcdf.defDim(ncid,'lat',length(lat));
var1_lon = netcdf.defVar(ncid,'lon','double',dim_lon);
var1_lat = netcdf.defVar(ncid,'lat','double',dim_lat);
var_50 = netcdf.defVar(ncid,'var','float', [dim_lat dim_lon]);
netcdf.putAtt(ncid,var1_lon,lon);
netcdf.putAtt(ncid,var1_lat,lat);
netcdf.putAtt(ncid,var_50,var);
I think the above code is suitable for 1D lat-lon dimension saving in netcdf. I find it difficult (showing error) to use it for 2D lat-lon data.
Looking forward to any sort of help or directions. Thanks in advance.

채택된 답변

CHIRANJIT DAS
CHIRANJIT DAS 2022년 6월 24일
dimidlon = netcdf.defDim(ncid,'lon',size(lon,1));
dimidlat = netcdf.defDim(ncid,'lat',size(lon,2));
%variables
tim_ID=netcdf.defVar(ncid,'time','double',dimidtim);
lon_ID=netcdf.defVar(ncid,'lon','double',[dimidlon,dimidlat]);
lat_ID=netcdf.defVar(ncid,'lat','double',[dimidlon dimidlat]);
Try it, this should probably work in your problem scenario.
Good luck

추가 답변 (1개)

KSSV
KSSV 2022년 6월 23일
lon=rand(406,271); lat=rand(406,271); var=rand(406,271);
[nx,ny] = size(lon) ;
% nc filename to be written
file = 'myfile.nc' ;
nccreate(file,'x','Dimensions',{'x',1,nx},'DeflateLevel',7) ;
nccreate(file,'y','Dimensions',{'y',1,ny},'DeflateLevel',7) ;
nccreate(file,'lon','Dimensions',{'x','y'},'DeflateLevel',7) ;
ncwrite(file,'lon',lon) ;
nccreate(file,'lat','Dimensions',{'x','y'},'DeflateLevel',7) ;
ncwrite(file,'lat',lat) ;
nccreate(file,'var','Dimensions',{'x','y'},'DeflateLevel',7) ;
ncwrite(file,'var',var) ;
  댓글 수: 6
KSSV
KSSV 2022년 6월 24일
Then you can just write a vector of lon and lat right? Why you want to write a 2D matrix?

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

카테고리

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

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by