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!