Plotting variable from NetCDF file

71 views (last 30 days)
Hello all,
I'm Student and work on my project but unfortunately, I faced a problem. I limit latitude and longitude of NetCDF using find<= but some part of it has been offside, I mean Some pixels are outside of the country. See the below figure: (the red line is country borders, and you'll see some pixel are outside)
I want to use inpolygon function in order to mask the country of interest. I want to eliminate all outside the country data and achive something like this picture below:
So I used this code below:
filename = 'tmin.1982.nc'
time = ncread(filename,'time'); %read time
lat = ncread(filename,'lat'); %reading latitude
lon = ncread(filename,'lon'); %reading longitude
tmin = ncread(filename,'tmin'); %reading the main variable precip=(lon*lat*time)
tmin_mean = mean(tmin, 3); %average of precip in all times
[x, y] = borders('Iran Islamic Republic of'); % study region
[lonG, latG] = ndgrid(lon, lat); %you might have to reverse these
in = inpolygon(lonG, latG, x, y);
inmask = repmat(in, 1, 1, size(tmin,3));
masked_tmin = tmin;
masked_tmin(~inmask) = nan;
mean_masked_tmin = mean(masked_tmin, 3, 'omitnan');
but after plot it using this code:
%plot
surf(lon, lat, mean_masked_tmin(:,:,:).'); view(2)
axis xy
%shading interp
cmocean 'rain' % rainy to dry colormap %from Climate data toolbox (Chad A. Greene)
xlabel longitude
ylabel latitude
hold on
borders('countries','color',rgb('dark gray'))
cmocean 'rain'
cb = colorbar;
cb.Label.String = 'Average Temperature';
xlabel longitude
ylabel latitude
I saw that it's far away from where it should be. I mean it must be in a yellow-highlighted region but it is far away (see figure below):
I don't know what the problem is.
I haven't any experience with this problem. here is size and other information that i'm screenshot them:

Accepted Answer

Jess Lovering
Jess Lovering on 5 Nov 2019
Edited: Jess Lovering on 5 Nov 2019
I noticed there is a comment note on this line about reversing the arrays:
[lonG, latG] = ndgrid(lon, lat); %you might have to reverse these
I think you may need to flip your lat and lon arrays before you grid them. You may want to try flipud. It's a little tough from the picture if that is the issue - but you may want to try this as a first step:
[lonG, latG] = ndgrid(flipud(lon), flipud(lat)); %you might have to reverse these
Also, maybe try switching the lat and lon - the picture almost look like these are reversed (maybe mixed up in the netcdf file?). So try:
[lonG, latG] = ndgrid(lat, lon); %you might have to reverse these
  2 Comments
BN
BN on 5 Nov 2019
Jessica Thank you for your answer, I tried what you mentioned above. some change was appear in plot. like:
pic.JPG
but the problem not fix :(
Jess Lovering
Jess Lovering on 5 Nov 2019
Sorry those didn't work. Maybe you can try to meshgrid the variables instead of ndgrid?
[lonG, latG] = meshgrid(lon, lat);

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!