multi raster files into netcdf4
6 次查看(过去 30 天)
显示 更早的评论
Hi, anyone has a m script handy to convert GIS multiple (240 files) raster files (same size) into 1 netcdf4 file?
0 个评论
采纳的回答
Niranjan Sundararajan
2023-7-12
Hey there,
Hope this helps.
directory = 'path_to_directory';
fileList = dir(fullfile(directory, '*.tif')); %assuming .tif files
info = geotiffinfo(fullfile(directory, fileList(1).name));
R = info.SpatialRef;
data = zeros(R.RasterSize(1), R.RasterSize(2), numel(fileList));
for i = 1:numel(fileList)
filename = fullfile(directory, fileList(i).name);
data(:, :, i) = geotiffread(filename);
end
ncid = netcdf.create('output.nc', 'NETCDF4');
dimid_x = netcdf.defDim(ncid, 'x', R.RasterSize(2));
dimid_y = netcdf.defDim(ncid, 'y', R.RasterSize(1));
dimid_time = netcdf.defDim(ncid, 'time', numel(fileList));
varid_data = netcdf.defVar(ncid, 'data', 'double', [dimid_x, dimid_y, dimid_time]);
varid_x = netcdf.defVar(ncid, 'x', 'double', dimid_x);
varid_y = netcdf.defVar(ncid, 'y', 'double', dimid_y);
varid_time = netcdf.defVar(ncid, 'time', 'double', dimid_time);
netcdf.putAtt(ncid, varid_data, 'units', 'your_units');
netcdf.putAtt(ncid, varid_x, 'units', 'your_units');
netcdf.putAtt(ncid, varid_y, 'units', 'your_units');
netcdf.putAtt(ncid, varid_time, 'units', 'your_units');
netcdf.endDef(ncid);
netcdf.putVar(ncid, varid_data, data);
netcdf.putVar(ncid, varid_x, R.XLimWorld);
netcdf.putVar(ncid, varid_y, R.YLimWorld);
netcdf.putVar(ncid, varid_time, 1:numel(fileList));
netcdf.close(ncid);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NetCDF 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!