how to clip a domain data from global data?

I am trying to clip South Asia domain (19.25°E-116.25°E,-15.75°S-45.75°N) from a global dataset with 0.5 degree resolution.
Here is the code i am using:
global_data = load('global_data.mat');
% Define the latitude and longitude boundaries
lat_start = -15.75;
lat_end = 45.75;
lon_start = 19.25;
lon_end = 116.25;
% Define the resolution
resolution = 0.5;
% Calculate the indices for cropping
lat_indices = floor((lat_start + 90) / resolution) + 1 : floor((lat_end + 90) / resolution) + 1;
lon_indices = floor((lon_start + 180) / resolution) + 1 : floor((lon_end + 180) / resolution) + 1;
% Crop the global data
cropped_data = global_data(lat_indices, lon_indices, :);
% Display the size of the cropped data
disp(size(cropped_data));
Can someone help me with this?
Thank you.

Answers (2)

Here are two ways to get it with two small corrections in your code:
Way - 1. Just use load() without variable name assigning, because the variable name is the same:
load('global_data.mat');
% Define the latitude and longitude boundaries
lat_start = -15.75;
lat_end = 45.75;
lon_start = 19.25;
lon_end = 116.25;
% Define the resolution
resolution = 0.5;
% Calculate the indices for cropping
lat_indices = floor((lat_start + 90) / resolution) + 1 : floor((lat_end + 90) / resolution) + 1;
lon_indices = floor((lon_start + 180) / resolution) + 1 : floor((lon_end + 180) / resolution) + 1;
% Crop the global data
cropped_data = global_data(lat_indices, lon_indices, :);
% Display the size of the cropped data
disp(size(cropped_data));
124 195 12
Way - 2. Read data from Stucture variable, e.g.: global_data.global_data
global_data = load('global_data.mat');
global_data = load('global_data.mat');
% Define the latitude and longitude boundaries
lat_start = -15.75;
lat_end = 45.75;
lon_start = 19.25;
lon_end = 116.25;
% Define the resolution
resolution = 0.5;
% Calculate the indices for cropping
lat_indices = floor((lat_start + 90) / resolution) + 1 : floor((lat_end + 90) / resolution) + 1;
lon_indices = floor((lon_start + 180) / resolution) + 1 : floor((lon_end + 180) / resolution) + 1;
% Crop the global data
cropped_data = global_data.global_data(lat_indices, lon_indices, :);
% Display the size of the cropped data
disp(size(cropped_data));
124 195 12

1 Comment

Actually, i need to compare two data. I am attaching two figures. One is data i want to compare with (ori_im) and the clipped figure(clip_im). Now, extent of the domain is same that i am giving as input in my code. But it is not extracting correct domain. Can you suggest what could be the possible reason?

Sign in to comment.

If understood your question correctly, what you want is to display your cropped data, right?
If so, here is how it can be done:
load('global_data.mat');
% Define the latitude and longitude boundaries
lat_start = -15.75;
lat_end = 45.75;
lon_start = 19.25;
lon_end = 116.25;
% Define the resolution
resolution = 0.5;
% Calculate the indices for cropping
lat_indices = floor((lat_start + 90) / resolution) + 1 : floor((lat_end + 90) / resolution) + 1;
lon_indices = floor((lon_start + 180) / resolution) + 1 : floor((lon_end + 180) / resolution) + 1;
% Crop the global data
cropped_data = global_data(lat_indices, lon_indices, :);
% Display the size of the cropped data
disp(size(cropped_data));
124 195 12
figure
imagesc(cropped_data(:,:,1)) % Cropped data set
figure
imagesc(global_data(:,:,1)) % Whole data set
In order to create the two image as you mentioned, you need to specify the corodinates (indices) as in this cropped one.

1 Comment

No. Actually the figure with "ori_im" name is the domain data that i need. And the coordinates are "South Asia domain (19.25°E-116.25°E,-15.75°S-45.75°N". But with these coordinates, i am getting the result (as you showed in your cropped_data figure) which is not correct domain data. So basically it is clipping a wrong domain.
Am I clear with my question? Sorry if i am not able to explain it correctly.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

on 28 Jun 2023

Commented:

on 28 Jun 2023

Community Treasure Hunt

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

Start Hunting!