MATLAB Answers

Need help reading netcdf files

37 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.
ncdisp('January_June_1992_Hourly_sp.nc')
Source:
F:\All_ERA_5\1990s\January_June_1992_Hourly_sp.nc
Format:
64bit
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/adaptor.mars.internal-1552360053.8510885-11556-13-869f19cf-3b8a-4e0f-8265-af78ecffde0f.nc /cache/tmp/869f19cf-3b8a-4e0f-8265-af78ecffde0f-adaptor.mars.internal-1552360053.8514261-11556-3-tmp.grib'
Dimensions:
longitude = 1440
latitude = 721
time = 4368
Variables:
longitude
Size: 1440x1
Dimensions: longitude
Datatype: single
Attributes:
units = 'degrees_east'
long_name = 'longitude'
latitude
Size: 721x1
Dimensions: latitude
Datatype: single
Attributes:
units = 'degrees_north'
long_name = 'latitude'
time
Size: 4368x1
Dimensions: time
Datatype: int32
Attributes:
units = 'hours since 1900-01-01 00:00:00.0'
long_name = 'time'
calendar = 'gregorian'
sp
Size: 1440x721x4368
Dimensions: longitude,latitude,time
Datatype: int16
Attributes:
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('January_June_1992_Hourly_sp.nc','sp');
Out of memory. Type HELP MEMORY for your options.
Error in internal.matlab.imagesci.nc/read (line 651)
data = double(data);
Error in ncread (line 58)
vardata = ncObj.read(varName, varargin{:});
ABN_ERA5_1992_sp = ncread('January_June_1992_Hourly_sp','sp', startLoc,count);
Error using internal.matlab.imagesci.nc/openToRead (line 1272)
Could not open January_June_1992_Hourly_sp for reading.
Error in internal.matlab.imagesci.nc (line 121)
this.openToRead();
Error in ncread (line 53)
ncObj = internal.matlab.imagesci.nc(ncFile);

  4 Comments

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('January_June_1992_Hourly_sp.nc','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.
matA=p(:);
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.
plot(matA)
Capture2019.JPG

Sign in to comment.

Answers (1)

Cg Gc
Cg Gc on 12 Mar 2019
p = ncread('January_June_1992_Hourly_sp.nc','sp',[446 646 1 ],[1 1 Inf]);
matA=p(:);
plot(matA)

  0 Comments

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