extract netcdf pr based on one location for a given lat and long

634 views Asked by At

I am working with MATLAB to extract pr variable for the whole 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);

...and this returns the 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{:});
1

There are 1 answers

6
ClimateUnboxed On

from the command line in bash:

cdo remapnn,lon=9.15/lat=36 input.nc output.nc