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

44 ビュー (過去 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 件のコメント
ANIE
ANIE 2022 年 6 月 24 日
yes it is structured but changing 2D ways..
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 Center および File ExchangeNetCDF についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by