cropping netcdf files using geo-coordinates
이전 댓글 표시
I want to crop my study area using lat and long mask. the code is below
%Cropping the files
chlFileDirectory = "/Users/gulfcarbon/Desktop/Chlorophyll_MODIS/2021";
chlFilename = dir(fullfile(chlFileDirectory, '*_chl.nc'));
nChlFileDirectory = length(chlFilename);
outputFolder = "/Users/gulfcarbon/Desktop/Chlorophyll_MODIS/Cropped_2021";
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
% Define the latitude and longitude ranges for cropping
crop_latitude_range = [30, 24];
crop_longitude_range = [-94, -84];
%lat_mask = crop_latitude_range(2);
% ...
for i = 1:nChlFileDirectory
chl_nc_filename = chlFilename(i).name;
chl_nc_file_path = fullfile(chlFileDirectory, chl_nc_filename);
% Read chlorophyll data from the current file
chl_data = ncread(chl_nc_file_path, 'chl_image');
latitude = ncread(chl_nc_file_path, 'latitude');
longitude = ncread(chl_nc_file_path, 'longitude');
% Perform logical indexing to crop the data within the specified latitude and longitude ranges
lat_mask = (latitude >= crop_latitude_range(2)) & (latitude <= crop_latitude_range(1));
lon_mask = (longitude >= crop_longitude_range(1)) & (longitude <= crop_longitude_range(2));
% Apply the logical masks to obtain the cropped data
chl_data_cropped = chl_data(lat_mask, lon_mask);
latitude_cropped = latitude(lat_mask);
longitude_cropped = longitude(lon_mask);
% Create the output chlorophyll file name with "_cropped" suffix
[filepath, name, ext] = fileparts(chl_nc_filename);
chl_nc_filename_cropped = strcat([name, '_cropped', ext]);
chl_nc_file_path_cropped = fullfile(outputFolder, chl_nc_filename_cropped);
% Write cropped chlorophyll data to a new netCDF file
nccreate(chl_nc_file_path_cropped, 'chl_image', 'Dimensions', {'row', numel(latitude_cropped), 'col', numel(longitude_cropped)});
nccreate(chl_nc_file_path_cropped, 'longitude', 'Dimensions', {'row', numel(latitude_cropped), 'col', numel(longitude_cropped)});
nccreate(chl_nc_file_path_cropped, 'latitude', 'Dimensions', {'row', numel(latitude_cropped), 'col', numel(longitude_cropped)});
ncwrite(chl_nc_file_path_cropped, 'longitude', longitude_cropped);
ncwrite(chl_nc_file_path_cropped, 'latitude', latitude_cropped);
ncwrite(chl_nc_file_path_cropped, 'chl_image', chl_data_cropped);
end
THe error is
The logical indices in position 1 contain a true value outside of the array bounds.
I have added the a image of workspace with the question
채택된 답변
추가 답변 (1개)
KSSV
2023년 8월 1일
Let X, Y and Z be your original data. Xi, Yi is your coordinates for which you want to crop/ extract the data.
Zi = interp2(X,Y,Z,Xi,Yi) ;
댓글 수: 5
Arnab Paul
2023년 8월 2일
KSSV
2023년 8월 3일
It seems your longitude, latitude are already in meshgrid format. Check their dimensions.
Arnab Paul
2023년 8월 3일
KSSV
2023년 8월 3일
That means you need not to use mesh grid.
카테고리
도움말 센터 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!