#author("2023-09-22T12:52:14+09:00","default:Miyashita","Miyashita")
#author("2023-09-25T13:53:43+09:00","default:Miyashita","Miyashita")
*NetCDF形式データの読み書き [#q2b15111]
**読み [#n3cc4983]
#codeprettify(lang-matlab){{
hogehoge = ncinfo('filename.nc');
}}
で返す構造体で変数一覧などが確認できす. ncdisp は容量が大きい場合に出力が大変なので注意.~
で返す構造体で変数一覧などが確認できる. ncdisp は容量が大きい場合に出力が多くなって解読困難なので注意.~
変数名がわかれば
#codeprettify(lang-matlab){{
var1 = ncread('filename.nc', 'varname');
}}
でOK.


**書き [#fdbacda9]
下記は単制度浮動小数点数の気圧時空間データをそれぞれの時間ごとに1つのファイルにして出力する例.
#codeprettify(lang-matlab){{
%% output
ncdir = './slp_nc';
if exist(ncdir,'dir'); system(['rm -rf  ', ncdir]); end
mkdir(ncdir);

ncfile_base = 'slp_XXX.nc';

nt = length(t);

for k = 1:nt
    ncfile = fullfile(ncdir,strrep(ncfile_base,'XXX',sprintf('%04d',k)));
    disp([ncfile, '   ...']);

    % % lonlat
    nccreate(ncfile,'lon',"Dimensions",{"lon",nlon},"FillValue","disable","Datatype", "single");
    nccreate(ncfile,'lat',"Dimensions",{"lat",nlat},"FillValue","disable","Datatype", "single");
    ncwrite(ncfile,'lon',lon);
    ncwrite(ncfile,'lat',lat);
    % % pressure
    nccreate(ncfile,'slp',"Dimensions",{"lon",nlon,"lat",nlat},"FillValue","disable","Datatype", "single");
    ncwrite(ncfile,'slp',permute(flipud(pres(:,:,k)),[2,1]));
    % % time
    nccreate(ncfile,'time',"Datatype", "single");
    ncwrite(ncfile,'time',t(k));
end
}}
nccreate で変数を定義して ncwrite で MATLAB 上の変数を入れる.~
スカラーじゃない場合は nccreate で 'Dimensions' なるものを定義する.~
2次元空間分布の場合などは次元の順番と向きに注意.~


***参考 MathWorks 公式 [#k588f20f]
-[[ncinfo>https://jp.mathworks.com/help/matlab/ref/ncinfo.html]]
-[[ncread>https://jp.mathworks.com/help/matlab/ref/ncread.html]]
-[[nccreate>https://jp.mathworks.com/help/matlab/ref/nccreate.html]]
-[[ncwrite>https://jp.mathworks.com/help/matlab/ref/ncwrite.html]]

Front page   Edit Diff Attach Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes