multi raster files into netcdf4

5 visualizaciones (últimos 30 días)
Sri Adiyanti
Sri Adiyanti el 28 de Jun. de 2023
Comentada: Sri Adiyanti el 4 de Sept. de 2023
Hi, anyone has a m script handy to convert GIS multiple (240 files) raster files (same size) into 1 netcdf4 file?

Respuesta aceptada

Niranjan Sundararajan
Niranjan Sundararajan el 12 de Jul. de 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);

Más respuestas (0)

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by