Regrid netcdf file using weighted average method

14 views (last 30 days)
Qian
Qian on 14 May 2024
Commented: Qian on 20 May 2024
How can we regrid netcdf file in matlab? I found out in Python it is easy to regrid 3-dimensional data using xarray or xesmf but it seems not easy to regrid regrid 3-D netcdf in matlab? I want to resample a netcdf data from 0.25 degree to 1 degree. I want to use the the weighted average of all non-NODATA contributing pixels. How can I do this in Matlab? Is there a good function for resampling gridded datasets in matlab using different resampling methods?
  2 Comments
Qian
Qian on 14 May 2024
Thank you for your reply. resize() which is used to resize data by adding or removing elements. I feel it won't work on the weighted average. Thanks.

Sign in to comment.

Answers (1)

Rupesh
Rupesh on 20 May 2024
Hi Qian,
I understand that you're looking to resample 3D “NetCDF” data in MATLAB, specifically trying to change the resolution of your dataset from 0.25 degrees to 1 degree. You're seeking a way to achieve this by calculating a weighted average for all non-NODATA pixels within each new grid cell. Given the constraints one better solution will be to involve reading your “NetCDF” data into MATLAB, creating a new grid at the desired resolution, and then applying a weighted averaging method to resample the data onto this new grid. You can follow below steps to get the idea of workaround mentioned above.
  • This step involves reading the NetCDF data followed by defining the new grid.
filename = 'your_data.nc';
lat = ncread(filename, 'latitude'); % Adjust variable name as needed
lon = ncread(filename, 'longitude'); % Adjust variable name as needed
data = ncread(filename, 'data_variable'); % Adjust variable name as needed
lat_new = min(lat):1:max(lat);
lon_new = min(lon):1:max(lon);
  • After data import main objective is to resample the data One approach is to loop through the new grid cells, identify the contributing old grid cells, compute the weighted average, and assign it to the new grid. Since you're interested in a weighted average, you might consider using spatial averaging within each 1-degree grid cell. Here's a conceptual example of how you might approach it.
% Loop through each new grid cell
data_new = NaN(length(lat_new), length(lon_new), size(data, 3)); % Assuming data is 3-D with time as the third dimension
for i = 1:length(lat_new)
for j = 1:length(lon_new)
Find the indices of the old grid cells that fall within the current new grid cell
lat_indices = find(lat >= lat_new(i) - 0.5 & lat < lat_new(i) + 0.5);
lon_indices = find(lon >= lon_new(j) - 0.5 & lon < lon_new(j) + 0.5);
Extract the data for these indices
if ~isempty(lat_indices) && ~isempty(lon_indices)
sub_data = data(lon_indices, lat_indices, :); % Assuming lon x lat x time data organization
Compute the weighted average for the sub_data, excluding NODATA values
Here, you'll need to define how you handle weights and NODATA values
This is a placeholder for the averaging process
valid_data = sub_data(~isnan(sub_data)); % Example: Exclude NaNs which are used as NODATA
if ~isempty(valid_data)
data_new(i, j, :) = mean(valid_data, 'all');
end
end
end
end
Below are some matlab answers which you can refer for better understanding of how one can regrid the spatial data in “NetCDF” format.
Hope it helps!
  1 Comment
Qian
Qian on 20 May 2024
Thank you so much for your reply! Your code is really helpful. Still, obtaining the weights is really triky. The weight for each source cell relative to a target cell should be determined by the ratio of the overlap area to the total area of the target cell. Do you have any suggestion on getting the weights? Thank you so much!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!