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

15 次查看(过去 30 天)
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

回答(1 个)

MJFcoNaN
MJFcoNaN 2022-5-24
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 个评论
MJFcoNaN
MJFcoNaN 2022-5-27
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]
M_D8993
M_D8993 2022-5-27
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)

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by