how can i extract data from netcdf file for a given lat and long

28 views (last 30 days)
Hi I am working with Matlab to extract pr variable for the entire time period from a netcdf file for a specific lat and long coordinates. My location has lat=36 and long=9.15. The netcdf file has a Latitude matrix of 453 X 453 and a Longitude matrix of 453 X 453. Variables: lon Size: 453x453 Dimensions: x,y Datatype: double Attributes: standard_name = 'longitude' long_name = 'longitude' units = 'degrees_east' lat Size: 453x453 Dimensions: x,y Datatype: double Attributes: standard_name = 'latitude' long_name = 'latitude' units = 'degrees_north' time Size: 1826x1 Dimensions: time Datatype: double Attributes: standard_name = 'time' units = 'days since 1949-12-01' calendar = 'gregorian' bounds = 'time_bnds' axis = 'T' long_name = 'time' pr Size: 453x453x1826 Dimensions: x,y,time Datatype: single Attributes: _FillValue = 1.000000020040877e+20 grid_mapping = 'Lambert_Conformal' standard_name = 'precipitation_flux' long_name = 'precipitation' units = 'kg m-2 s-1' cell_methods = 'time: mean' coordinates = 'lat lon' Now i would like to find the closest point in the Latitude and longitude matrix of the netcdf file to my actual lat and long coordinates. I did that using
d = (mylat-Latnetcdf).^2+(mylong-Longnetcdf).^2;
[~, ind] = min(d(:));
resultlat = lat(ind); %// use that index to obtain the result resultlong = long(ind);
but this returns ind=50049.
then, when i try to extract the pr variable from file using
ncread(netcdfname,'pr', [50049,50049, 1], [1 1 inf]);
it returns error
Error using netcdflib The NetCDF library encountered an error during execution of 'getVarsFloat' function - 'Index exceeds dimension bound (NC_EINVALCOORDS)'.
Error in netcdf.getVar (line 136) data = netcdflib(funcstr,ncid,varid,varargin{:});
Error in internal.matlab.imagesci.nc/read (line 635) data = netcdf.getVar(gid, varid, ...
Error in ncread (line 58) vardata = ncObj.read(varName, varargin{:});
can any one help please Thanks
  1 Comment
Chem
Chem on 9 Jan 2021
Try this, although it can be very inefficient in terms of time, but it works
% Obtaining of points
% Latitude and Longitude
Puntos = single([-15, -70;...
-16, -71;]);
% Load netcdf data
Prec = ncread('Pisco_PP_Diario.nc','Prec');
lat = ncread('Pisco_PP_Diario.nc','Y');
lon = ncread('Pisco_PP_Diario.nc','X');
Dates = ncread('Pisco_PP_Diario.nc','T');
% Making the Grid
[GLat,GLon,GF] = meshgrid(lat,lon,Dates);
% Dimensioning
n = length(Puntos(:,1)); % number of rows or number of points
N = length(Dates); % número de fechas (en días, meses, etc.)
b = zeros(N,n); % Number of dates (in days, months, etc.)
for i = 1 : n
a = double(interp3(GLat,GLon,GF,Prec,Puntos(i,1),Puntos(i,2),(Dates)));
b(:,i) = reshape(a,length(a),1,[]);
end
for i = 1 : n
subplot(n,1,i)
plot(b(:,i),'LineWidth',1.5)
end

Sign in to comment.

Answers (1)

Haifa Ben Romdhane
Haifa Ben Romdhane on 5 Dec 2018
%% Display the contents
fileName = 'g4.timeAvgMap.MODISA_L3m_CHL_2014_chla.20071201-20090531.47E_23N_58E_31N.nc';
ncdisp(fileName);
%% Read Info
info=ncinfo(fileName);
%Display variables name
nVar=size(info.Variables, 2);
disp('Variable names are:')
for ii=1:nVar
disp(info.Variables(ii).Name);
end
%% Read variables
DATA = ncread(fileName,'MODISA_L3m_CHL_2014_chla');
LAT = ncread(fileName,'lat');
LON = ncread(fileName,'lon');
%%Example
data=getValue(11.47916, 45.14583, LAT, LON, DATA);
Hi,
Try this code and the attached function. The example here is for Chlorophyll netcdf data.
Hope it helps!
  3 Comments
Walter Roberson
Walter Roberson on 11 Jul 2020
Haifa Ben Romdhane attached the code for getValue in their Answer. It has nothing to do with those toolboxes.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!