How do you select a particular geographic area for NetCDF files?

I am having some trouble trying to extract glacier runoff data for a geographical region and I am running into the same error in both R and Matlab (code and error pasted below). It looks like the values for latend and lonend exceed the number of values in the lat and lon dimensions (990 and 860). I am wondering whether it is because latend and lonend are so large? I thought that maybe the area from which I was trying to obtain runoff data was too large. Upon constraining the geographical area further, I still received the same error. I have included the header to the file below as well. I have also looked at other pages with remedies to this error, but they seem to suggest it's an issue of insufficient RAM. I have tried a couple troubleshooting solutions to this error, but still receive the same error in R and Matlab on two different devices (presumably because the array size is somehow so large), so I believe it's a problem with the code.
I have also used R and received a similar error, oddly when I do dim(lat) and dim(lon) in R to get the dimensions of lat and lon, I get two numbers as an output for each lat and lon (990 and 860) instead of one. Wondering whether this could be related to the issue? Any help would be appreciated, thank you!
ncfile='runoff.1958.BN_1958_2013_CAA_North_1km.MM.nc'
lon = ncread(ncfile,'LON')
lat = ncread(ncfile,'LAT')
t = ncread(ncfile,'time')
latstart = find(lat>76.4049,1, 'first')
latend = find(lat<76.8460,1, 'last')
lonstart = find(lon>-81.3612,1, 'first')
lonend = find(lon<-83.2651,1, 'last')
startloc = [205 759 1]
count = [851217 669241 Inf]
runoffdata = ncread('runoff.1958.BN_1958_2013_CAA_North_1km.MM.nc','runoff',startloc,count)
Error using matlab.internal.imagesci.netcdflib
Requested 851217x669241x12 (25466.2GB) array exceeds maximum array size preference (8.0GB). This
might cause MATLAB to become unresponsive.
Error in netcdf.getVar (line 205)
data = matlab.internal.imagesci.netcdflib(funcstr,ncid,varid,varargin{:});
Error in internal.matlab.imagesci.nc/read (line 674)
data = netcdf.getVar(gid, varid, ...
Error in ncread (line 74)
vardata = ncObj.read(varName, varargin{:});
Related documentation
File Header
File /Users/minolidias/Desktop/R/Original NC Data Files/runoff.1958.BN_1958_2013_CAA_North_1km.MM.nc (NC_FORMAT_CLASSIC):
7 variables (excluding dimension variables):
float x[lon]
long_name: lon
units: Km
axis: X
float y[lat]
long_name: lat
units: Km
axis: Y
float runoff[lon,lat,time]
standard_name: Downscaled_runoff
long_name: Downscaled runoff
units: mmWE
actual_range: 0
actual_range: 0
float LON[lon,lat]
standard_name: Longitude
long_name: Longitude
units: Degree
actual_range: -111.305130004883
actual_range: -54.9293098449707
float LAT[lon,lat]
standard_name: Latitude
long_name: Latitude
units: Degree
actual_range: 72.890022277832
actual_range: 84.5155563354492
float icemask[lon,lat]
standard_name: Ice_Mask__GrIS_+_ice_caps__1km:_Howat_et_al__2014
long_name: Ice Mask (GrIS + ice caps) 1km: Howat et al. 2014
units: -
actual_range: 0
actual_range: 1
float topography[lon,lat]
standard_name: Topography_1km:_Howat_et_al__2014
long_name: Topography 1km: Howat et al. 2014
units: -
actual_range: 0
actual_range: 2350
3 dimensions:
lon Size:990 (no dimvar)
lat Size:860 (no dimvar)
time Size:12 *** is unlimited ***
standard_name: time
long_name: time
units: months since 1958-1-15 00:00:00
calendar: standard
axis: T
8 global attributes:
CDI: Climate Data Interface version 1.7.1 (http://mpimet.mpg.de/cdi)
Conventions: CF-1.4
history: Fri Nov 10 16:28:55 2017: ncrename -d x,lon -d y,lat -O /Volumes/RAPID3/ZGRN11/CAA/Downscaling/Monthly_North//1958/runoff.1958.BN_1958_2013_CAA_North_1km.MM.nc
Mon Dec 12 07:02:13 2016: cdo cat /Users/brice/Desktop/DOWN_CAA_MM_YY//Monthly_North/1958/runoff.1958_JFM.BN_1958_2013_CAA_North_1km.MM.nc /Users/brice/Desktop/DOWN_CAA_MM_YY//Monthly_North/1958/runoff.1958_AMJ.BN_1958_2013_CAA_North_1km.MM.nc /Users/brice/Desktop/DOWN_CAA_MM_YY//Monthly_North/1958/runoff.1958_JAS.BN_1958_2013_CAA_North_1km.MM.nc /Users/brice/Desktop/DOWN_CAA_MM_YY//Monthly_North/1958/runoff.1958_OND.BN_1958_2013_CAA_North_1km.MM.nc /Users/brice/Desktop/DOWN_CAA_MM_YY//Monthly_North/1958/runoff.1958.BN_1958_2013_CAA_North_1km.MM.nc
libUN (2005.04.08) - Mon Dec 12 07:01:37 2016
institution: IMAU (Brice Noel)
title: Monthly cumulated field, B. Noel (IMAU)
netcdf: 4.4.1 of Jul 7 2016 23:08:32 $
CDO: Climate Data Operators version 1.7.1 (http://mpimet.mpg.de/cdo)
NCO: 4.6.6

Answers (1)

Both the variables of 'LAT' and 'LON' are 2-dim, but you treated them as 1-dim. Hence you get a very large number of count([851217 669241]).
You should check how the grid is created firstly. For example, it may be curvilinear orthogonal or a meshgrid or something else. In an simplest case (they are similar with a meshgrid), you only need to add some lines after ncreading lon, lat, and t. This is an example:
lon = ncread(ncfile,'LON')
lat = ncread(ncfile,'LAT')
t = ncread(ncfile,'time')
% lon and lat are 'meshgrid'.
lon=lon(1,:);
lat=lat(1,:);

7 Comments

I'm sorry, this is my first time using Matlab, so this may be a silly question, but how do I check the way in which the grid is created? I've been unable to figure it out. When I add the code you suggested for meshgrid, I get numbers that make sense for lat start and lat end, but for lon start I get the result below. lonend then gives me 860 which is the size of the lat dimension. Would this mean that it is not a meshgrid?
lonstart = find(lon>-81.3612,1, 'first')
lonstart =
1×0 empty double row vector
After getting the original lon and lat by ncread, you can open a variable (for example lat) and check wether it repeats a vector to create the matrix. If yes, you can only use a column or row.
PS: If the first row of lat worked, you may try a column of lon:
lon=lon(:, 1);
I included that after the lat code and ran the new code altering startloc and count, but I get the error below. I've been trying to try different approaches but none of my code is working. It looks like the lat and lon variables from the NetCDF file are 2D with lon and lat as their dimensions. Do I need to convert them from 2D to 1D before proceeding (unsure how to do this or if it is possible)? All of the lat and lon for similar examples that I have come across have 1D lat and lon variables. (Also thank you for your help and patience!!)
Error using matlab.internal.imagesci.netcdflib
The NetCDF library encountered an error during execution of 'getVarsFloat' function -
'Start+count exceeds dimension bound (NC_EEDGE)'.
Error in netcdf.getVar (line 205)
data = matlab.internal.imagesci.netcdflib(funcstr,ncid,varid,varargin{:});
Error in internal.matlab.imagesci.nc/read (line 674)
data = netcdf.getVar(gid, varid, ...
Error in ncread (line 74)
vardata = ncObj.read(varName, varargin{:});
How did you determine these numbers?
count = [851217 669241 Inf]
For lon and lat (the first two), I used the find function in the code above that is indicated by latstart/lonstart and latend/lone nd. For time, I have INF since I want the whole timeseries which is just 12 months.
Please post the final code you are using now. Especially, add all steps how to calculate "startloc" and "count".
For example, you should change "startloc = [205 759 1]" by something like this one:
% This is only an example, please post the real code!
startloc=[lonstart, latstart]
Apologies! Yes, I did end up changing startloc and count.
ncfile = 'runoff1958.nc'
ncdisp(ncfile)
run = ncread(ncfile,'runoff');
lon = ncread(ncfile,'LON');
lat = ncread(ncfile,'LAT');
t = ncread(ncfile,'time');
lon=lon(:, 1);
lat=lat(1,:);
latstart = find(lat>76.4049,1, 'first')
latend = find(lat<76.8460,1, 'last')
lonstart = find(lon>-83.2651,1, 'first')
lonend = find(lon<-81.3612,1, 'last')
startloc = [131 586 1]
count = [204 677 Inf]
runoffdata = ncread('runoff1958.nc','runoff',startloc,count)

Sign in to comment.

Asked:

on 23 May 2022

Commented:

on 27 May 2022

Community Treasure Hunt

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

Start Hunting!