Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
netCDF

Subject: netCDF

From: Mel

Date: 1 Nov, 2010 18:07:04

Message: 1 of 5

I am trying to use MATLAB to open a netCDF file that contains multi dimensions.

dimension 0 = longitude
dimension 1 = latitude
dimension 2 = level
dimension 3 = time
dimension 4 = should contain 17 layers of atmospheric heights which correspond to dimension 2 (level)

When I run the following MATLAB code for dimensions 0-3, all works fine.

ncfilename = 'hgt.mon.mean.nc';
nc_id = netcdf.open(ncfilename,'NC_NOWRITE');
[dimname, dimlen] = netcdf.inqDim(nc_id,#);


But with dimension 4, I get the following error:

??? Error using ==> netcdflib
Dimension inquiry failed, library error message "NetCDF:
Invalid dimension ID or name"

Error in ==> inqDim at 22
[dimname,dimlen] = netcdflib('inq_dim', ncid,dimid);


Any help would be greatly appreciated.
Thanks,

Subject: netCDF

From: Ashish Uthama

Date: 1 Nov, 2010 18:36:10

Message: 2 of 5

Mel wrote:
> I am trying to use MATLAB to open a netCDF file that contains multi
> dimensions.
>
> dimension 0 = longitude
> dimension 1 = latitude
> dimension 2 = level
> dimension 3 = time
> dimension 4 = should contain 17 layers of atmospheric heights which
> correspond to dimension 2 (level)
>
> When I run the following MATLAB code for dimensions 0-3, all works fine.
>
> ncfilename = 'hgt.mon.mean.nc';
> nc_id = netcdf.open(ncfilename,'NC_NOWRITE');
> [dimname, dimlen] = netcdf.inqDim(nc_id,#);
>
>
> But with dimension 4, I get the following error:
>
> ??? Error using ==> netcdflib
> Dimension inquiry failed, library error message "NetCDF:
> Invalid dimension ID or name"
>
> Error in ==> inqDim at 22
> [dimname,dimlen] = netcdflib('inq_dim', ncid,dimid);
>
>
> Any help would be greatly appreciated.
> Thanks,

Mel,

There are only 4 dimensions defined, so there would only be four unique ids.

(i.e the information for the 'dimension 4' would be the same as the
level dimension, which has the id 1).

Ashish

Subject: netCDF

From: Mel

Date: 1 Nov, 2010 19:41:03

Message: 3 of 5

Ashish Uthama <first.last@mathworks.com> wrote in message <ian1aq$pbs$1@fred.mathworks.com>...
> Mel wrote:
> > I am trying to use MATLAB to open a netCDF file that contains multi
> > dimensions.
> >
> > dimension 0 = longitude
> > dimension 1 = latitude
> > dimension 2 = level
> > dimension 3 = time
> > dimension 4 = should contain 17 layers of atmospheric heights which
> > correspond to dimension 2 (level)
> >
> > When I run the following MATLAB code for dimensions 0-3, all works fine.
> >
> > ncfilename = 'hgt.mon.mean.nc';
> > nc_id = netcdf.open(ncfilename,'NC_NOWRITE');
> > [dimname, dimlen] = netcdf.inqDim(nc_id,#);
> >
> >
> > But with dimension 4, I get the following error:
> >
> > ??? Error using ==> netcdflib
> > Dimension inquiry failed, library error message "NetCDF:
> > Invalid dimension ID or name"
> >
> > Error in ==> inqDim at 22
> > [dimname,dimlen] = netcdflib('inq_dim', ncid,dimid);
> >
> >
> > Any help would be greatly appreciated.
> > Thanks,
>
> Mel,
>
> There are only 4 dimensions defined, so there would only be four unique ids.
>
> (i.e the information for the 'dimension 4' would be the same as the
> level dimension, which has the id 1).
>
> Ashish


Thank you for the reply, but I am still confused. If dimension 2 is “level”, how do I access the data from the 17 different levels (i.e. If I want to load the 850 hPa geopotential heights\ which should be the 3rd level in 17 pressure levels (hPa): 1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 10)? The data is from NOAA’s Earth System Research Laboratory located at: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.derived.pressure.html

Again, any help is greatly appreciated.
Mel

Subject: netCDF

From: Ashish Uthama

Date: 2 Nov, 2010 16:56:07

Message: 4 of 5

>
>
> Thank you for the reply, but I am still confused. If dimension 2 is
> “level”, how do I access the data from the 17 different
> levels (i.e. If I want to load the 850 hPa geopotential heights\ which
> should be the 3rd level in 17 pressure levels (hPa): 1000, 925, 850,
> 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 10)? The
> data is from NOAA’s Earth System Research Laboratory located at:
> http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.derived.pressure.html
>
>
> Again, any help is greatly appreciated.
> Mel


Maybe this helps clarify?

ncid = netcdf.open('hgt.mon.mean.nc','NOWRITE');
varid = netcdf.inqVarID(ncid,'hgt');

% Inspect the variable
[varname,xtype,dimids,natts] = netcdf.inqVar(ncid,varid);
for did = dimids
     [dname dlen] = netcdf.inqDim(ncid,did);
     disp(['Dimension: ' dname ' has length: ' num2str(dlen)]);
end

% Find out the levels.
levelVarId = netcdf.inqVarID(ncid,'level');
disp(netcdf.getVar(ncid, levelVarId)');

% Read 3rd index entry of the 'level' dimension. i.e resulting size of data
% should be 144x73x1x753.

% Use the start and count arguments:
help netcdf.getVar

% This is low-level access to the NetCDF library (which is written in C),
% so the start index is 0-based.
level3Data = netcdf.getVar(ncid, varid, [0 0 2 0],[144 73 1 753]);
disp(size(level3Data));



netcdf.close(ncid);

Subject: netCDF

From: Mel

Date: 2 Nov, 2010 18:50:05

Message: 5 of 5

Ashish Uthama <first.last@mathworks.com> wrote in message <iapfr8$m5h$1@fred.mathworks.com>...
> >
> >
> > Thank you for the reply, but I am still confused. If dimension 2 is
> > “level”, how do I access the data from the 17 different
> > levels (i.e. If I want to load the 850 hPa geopotential heights\ which
> > should be the 3rd level in 17 pressure levels (hPa): 1000, 925, 850,
> > 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 10)? The
> > data is from NOAA’s Earth System Research Laboratory located at:
> > http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.derived.pressure.html
> >
> >
> > Again, any help is greatly appreciated.
> > Mel
>
>
> Maybe this helps clarify?
>
> ncid = netcdf.open('hgt.mon.mean.nc','NOWRITE');
> varid = netcdf.inqVarID(ncid,'hgt');
>
> % Inspect the variable
> [varname,xtype,dimids,natts] = netcdf.inqVar(ncid,varid);
> for did = dimids
> [dname dlen] = netcdf.inqDim(ncid,did);
> disp(['Dimension: ' dname ' has length: ' num2str(dlen)]);
> end
>
> % Find out the levels.
> levelVarId = netcdf.inqVarID(ncid,'level');
> disp(netcdf.getVar(ncid, levelVarId)');
>
> % Read 3rd index entry of the 'level' dimension. i.e resulting size of data
> % should be 144x73x1x753.
>
> % Use the start and count arguments:
> help netcdf.getVar
>
> % This is low-level access to the NetCDF library (which is written in C),
> % so the start index is 0-based.
> level3Data = netcdf.getVar(ncid, varid, [0 0 2 0],[144 73 1 753]);
> disp(size(level3Data));
>
>
>
> netcdf.close(ncid);

Thank you,

That helps me to see where I need to go with the rest of the code.

Thanks again,
Mel

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us