NetCDF file grid box extraction

62 ビュー (過去 30 日間)
James Douglas
James Douglas 2015 年 1 月 23 日
コメント済み: Chad Greene 2017 年 3 月 5 日
I'm new to Matlab programming (usually use IDL) but I've been tasked with extracting meteorological data from netcdf files at specific gridsquares. I've looked at the structure of the file with ncread but my problem is that the netcdf is on a 193x by 130y grid that does not match any global grid. The gridpoint I'm looking for is at: lat 28.046, lon 86.902 in decimal degrees. How do I find where this is in the netcdf file?

回答 (4 件)

Mohamed Elshamy
Mohamed Elshamy 2017 年 3 月 3 日
Thanks for this information. Is there a way to reproject the whole field. I already found the lat and lon but they are matrices not vectors so interp2 did not accept them.
  2 件のコメント
Chad Greene
Chad Greene 2017 年 3 月 5 日
A common problem with interp2 and geo data is the order. It's the issue of whether the grid was created by
[latgrid,longrid] = meshgrid(lat,lon);
[longrid,latgrid] = meshgrid(lon,lat);
Different datasets build their grids in different ways, so you might need to do
zi = interp2(latgrid,longrid,z,lati,loni);
zi = interp2(longrid,latgrid,z,loni,lati);


Ashish Uthama
Ashish Uthama 2015 年 1 月 23 日
編集済み: Ashish Uthama 2015 年 1 月 23 日
I got this far.. then realized no exact match exists for that lat/lon. So, conceptually, what do you want to do? Pick a point closest to your required lat/lon? (closest in terms of euclidean distance? or is there another metric to measure the "distance" between two lats/lons?)
I used MAD below:
ncFile = '';
%%Read non-uniform (?) lat and lons
alllat = ncread(ncFile, 'lat');
alllon = ncread(ncFile, 'lon');
%%Find the index of the required lat/lon
lat = 28.046;
lon = 86.902;
%%Look for exact match
latInd = find(alllat==lat)
lonInd = find(alllon==lon)
%%Look for closest match (use Mean Absolute Difference)
madlatlon = abs(alllat-lat) + abs(alllon-lon);
[~, closestInd] = min(madlatlon(:)); % note - this is a linear index
closestLat = alllat(closestInd)
closestLon = alllon(closestInd)
%%Read that value:
[rlonInd, rlatInd] = ind2sub(size(alllat), closestInd); % convert to x,y index
% read all time for this lat/lon
tas = ncread(ncFile,'tas', [rlonInd, rlatInd, 1], [1 1 inf]);
tas = squeeze(tas); % make data single dimension
plot(tas); title(['tas at' num2str(closestLat) ' lat, ' num2str(closestLon) ' lon']);

Chad Greene
Chad Greene 2015 年 1 月 23 日
I think the issue is that the lat/lon grid is not regular--they're in rotated coordinates. If you have the Mapping Toolbox, here's the first "slice" of tas:
lati = 28.046;
loni = 86.902;
tas = ncread('','tas');
rlat = ncread('','rlat');
rlon = ncread('','rlon');
origin = newpole(80,-123);
[rlati,rloni] = rotatem(lati,loni,origin,'forward','degrees');
tas1 = squeeze(tas(:,:,1));
ans =
  4 件のコメント
Mohammad Abouali
Mohammad Abouali 2015 年 1 月 26 日
if you want to use rlon/rlat as Chad said here you need to change your destination point form 86.902,28.046 also to the rotated version, which seems you are doing.
But if you don't want to do that the not-rotated version of the coordinates are stored in lat/lon variable, and you can use the code I provided (interp2 doesn't work for non-rotated version).
It turns out to be faster to perform time-series interpolation using the ConstructPolyInterpolant2D, and non-rotated version. I designed that function particularly for this sort of application, that you have the same source and destination grid, but you want to interpolate a time series or you want to interpolate lot's of different fields. If you have just few interpolation that function might not be suitable but if you have 366 interpolation as it shows it works faster.


Chad Greene
Chad Greene 2015 年 1 月 26 日
t = datenum(1949,12,ncread('','time'));
to get time in Matlab's datenum format, which is days since year 0. A date vector contains year, month, and day, and is given by
dv = datevec(t);
The year would be
year = dv(:,1);
doy = datenum([dv(:,1:3), zeros(size(dv,1),3)]) - datenum([dv(:,1),...
ones(size(dv,1),1), zeros(size(dv,1), 4)]);

Community Treasure Hunt

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

Start Hunting!

Translated by