multi raster files into netcdf4
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sri Adiyanti
am 28 Jun. 2023
Kommentiert: Sri Adiyanti
am 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 Kommentare
Akzeptierte Antwort
Niranjan Sundararajan
am 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);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu NetCDF finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!