multi raster files into netcdf4
5 views (last 30 days)
Show older comments
Sri Adiyanti
on 28 Jun 2023
Commented: Sri Adiyanti
on 4 Sep 2023
Hi, anyone has a m script handy to convert GIS multiple (240 files) raster files (same size) into 1 netcdf4 file?
0 Comments
Accepted Answer
Niranjan Sundararajan
on 12 Jul 2023
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);
More Answers (0)
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!