I am trying to subset a .nc file in Matlab using ncread
. I am looking to subset the data for longitudes -74.6875 to -10.3125 (W) and latitudes 58.2500 to 84.7500 (N) to create a bounding box around Greenland. I want the subset for every day of data. I tried the following code, which works for the indices of longitude and latitude that I need, but produces a flipped (upside down) version of Greenland (see attached 1):
data = ncread('dust_ddep.nc','dust_ddep',[457 297 1], [104 54 Inf], [1 1 1]); %576x360x366 lon, lat, time
When I try to plot the data, I need to flip it to make sure that Iceland dust is located in the right place geographically (see attached). The latitude is plotted inversely (needs to be from 55 to 85 N, not the other way around).
Then, I plot using m_coast
function to produce the coastline of Greenland (see attached, 2):
figure;
data1 = data(:,:,160); %Grab one day of data
imagesc(long1_sub,lat,data1)
% m_pcolor(lon,lat,data(:,:,1)); %This comes up blank when I try to run m_pcolor
shading flat; hold on;
gland = m_coast('patch',[1 1 1],'edgecolor','k')
% flipud(gland) %doesn't fix the upside down Greenland
m_grid('box','fancy');
What am I doing wrong? Is it the order of my start and count? Or, is it in the way that I plot imagesc? I tried also using pcolor
(not m_pcolor), but I receive an error saying the dimensions are wrong (but they are not ...). I used this code with pcolor
: pcolor(long1_sub,lat,data1)
And receive the error:
Error using pcolor (line 59)
Matrix dimensions must agree.
The dimensions for these variables are:
Long1_sub is [104x1]
lat is [54x1]
data1 is [104x54]
The origin for imagesc is the top left corner instead of the bottom left, which therefore would 'flip' your y-axis. So you need to flip the y axis to make it like you would normally expect.
imagesc(long1_sub,flipup(lat),data1)
imagesc of very unintuitive because of this so I recommend using pcolor. As for why pcolor errors when using the same variables, the pcolor documentation says that, "If X and Y are vectors, X corresponds to the columns of C and Y corresponds to the rows." Since data1 is 104x54, you would want to transpose it to 54x104 so that it agrees with the lon/lat,
pcolor(long1_sub,lat,data1') % Notice the ' next to data1
I hope this helps!