MATLAB Answers

Need help reading netcdf files

38 views (last 30 days)
Cg Gc
Cg Gc on 12 Mar 2019
Answered: Cg Gc on 12 Mar 2019
I am trying to read a subset of a netcdf file. I have a bunch of these files to read, but I can't even get one to work.
The subset is a time series of surface pressure ('sp') at one single latitude and longitude. My latitude is at 646 and the longitude is at 446. I only need this one point.
Here is what I have so far. When I try to read the entire file, it runs out of memory, but I also can't read a subset of the file either.
Can anyone point me in the right direction on how to read a subset of the file and also assign it to a variable? I eventually need to plot the time series.
Thank you much.
Global Attributes:
Conventions = 'CF-1.6'
history = '2019-03-12 03:17:31 GMT by grib_to_netcdf-2.10.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -o /cache/data3/ /cache/tmp/869f19cf-3b8a-4e0f-8265-af78ecffde0f-adaptor.mars.internal-1552360053.8514261-11556-3-tmp.grib'
longitude = 1440
latitude = 721
time = 4368
Size: 1440x1
Dimensions: longitude
Datatype: single
units = 'degrees_east'
long_name = 'longitude'
Size: 721x1
Dimensions: latitude
Datatype: single
units = 'degrees_north'
long_name = 'latitude'
Size: 4368x1
Dimensions: time
Datatype: int32
units = 'hours since 1900-01-01 00:00:00.0'
long_name = 'time'
calendar = 'gregorian'
Size: 1440x721x4368
Dimensions: longitude,latitude,time
Datatype: int16
scale_factor = 0.88881
add_offset = 76906.7529
_FillValue = -32767
missing_value = -32767
units = 'Pa'
long_name = 'Surface pressure'
standard_name = 'surface_air_pressure'
startLoc = [446 646 1]; % Start location along each coordinate
count = [0 0 Inf]; %I only need it to read at this one point, but at all times.
ABN_sp_Data = ncread('','sp');
Out of memory. Type HELP MEMORY for your options.
Error in (line 651)
data = double(data);
Error in ncread (line 58)
vardata =, varargin{:});
ABN_ERA5_1992_sp = ncread('January_June_1992_Hourly_sp','sp', startLoc,count);
Error using (line 1272)
Could not open January_June_1992_Hourly_sp for reading.
Error in (line 121)
Error in ncread (line 53)
ncObj =;


Show 1 older comment
Cg Gc
Cg Gc on 12 Mar 2019
The file is over 8GB in size. It is too big to attach. Also netcdf files are not an approved file.
It looks like this... This is only one time and contains all of the latitudes and longitudes.
I need latitude cell number 646, longitude cell number 446, for times 1-4368.
Cg Gc
Cg Gc on 12 Mar 2019
I am still very new to this and have tried to use the examples, but they don't seem to work for my file. Any help in reading the data would be appreciated.
Cg Gc
Cg Gc on 12 Mar 2019
This is in case anyone else out there has a similar problem.
Stackoverflow to the rescue.
My code to assign all of my pressure values to a single variable.
p = ncread('','sp',[446 646 1 ],[1 1 Inf]);
The dimensions are longitude, latitude, and time. I am reading the 446th longitude and the 646th latitude, for all times.
But now, the data is in matrix form, which is kind of annoying since there is only one value per line.
To change the matrix to a column of vectors.
Now, the data is really easy to visualize with a line plot. No need to generate a separate x axis. In this case, the values are hourly, so the x axis could just be thought of as hourly timesteps.

Sign in to comment.

Answers (1)

Cg Gc
Cg Gc on 12 Mar 2019
p = ncread('','sp',[446 646 1 ],[1 1 Inf]);


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by