how to analyze matlab grib files?

35 views (last 30 days)
Hello,
Could you tell me how can i read grib files in matlab on my mac? I have to use matlab cdi or can’t do this to my mac? Also i have downloaded nctoolbox if you know. Do you know how can install one of them and run it? Except this i want to make another work. I have grib files from ECMWF, one with velocities V (Vx,Vy) and another one with potential temperature T and i want to calculate the product image001.png. So i have to import and analyze grib files first and then to calculate this product. Do you know how can i do this? Please help me if you can, i will appreciate it a lot. It is for my diploma thesis and i don't know what i have to do.
Best regards
Sotiris Tsiougkos

Accepted Answer

Jacob Halbrooks
Jacob Halbrooks on 25 Sep 2024
As of R2023b, you can read data from a GRIB file (.grb, .grib, .grib2) using the Mapping Toolbox readgeoraster function. You can also get information about a GRIB file by using the georasterinfo function. These functions were enhanced in R2024b to support GRIB files that use CCSDS compression.

More Answers (10)

Kelly Kearney
Kelly Kearney on 9 Apr 2015
You mean GRIB files? Matlab doesn't handle GRIB format natively, so I recommend converting the files to netCDF if you want to read them into Matlab without needing an outside toolbox. The ECMWF servers offer this option when downoading the data. Or you can use a converter like the NCL (NCAR Command Language) grib2netCDF.
I've never used the nctoolbox package, but it looks like it can read GRIB, so just follow the installation instructions and run the setup script to get that working.
  4 Comments
sotiris tsiougkos
sotiris tsiougkos on 10 Apr 2015
Rudeness?? No, never from me..really. I just told it about netcdf because i have the impression that nc files have less information from grib files. Maybe it's false. Also as i told you i don;t know how to import and analyze them. So after inserting nc files (Vx,Vy,T) i have to calculate -V*grad(T)= -(Vx,Vy)(θΤ/Θχ,ΘΤ/θy)=-(Vx*ΘΤ/Θχ + Vy*θΤ/Θy). So, i want to get the values from nc files (Vx,Vy,T) to calculate the last production. This is my problem
Kelly Kearney
Kelly Kearney on 10 Apr 2015
Edited: Kelly Kearney on 10 Apr 2015
Look at the help for ncread; that's the easiest way to read in netCDF files.
As for GRIB to netCDF conversion, you may lose some of the global metadata through the conversion, but the data values themselves will be preserved, as well as the important attributes (units, short name, standard name, etc.)

Sign in to comment.


sotiris tsiougkos
sotiris tsiougkos on 12 Apr 2015
Edited: per isakson on 14 Apr 2015
Hello again,
I found this in documentation for ncread.
vardata = ncread(source,varname)
vardata = ncread(source,varname,start,count,stride)
What ia have to puy into source, variance, start,count stride??
I have download my nc files from ECMWF to my HDD.

sotiris tsiougkos
sotiris tsiougkos on 12 Apr 2015
What i have to put into source,varname, start etc

sotiris tsiougkos
sotiris tsiougkos on 12 Apr 2015
Edited: per isakson on 14 Apr 2015
Ι wrote this
vardata = ncread(/Users/Sotiris_Tsiougkos/Downloads,netcdf_atls05_20150411184037_46649_0075.nc)
but...
|
Error: Unexpected MATLAB operator.
  1 Comment
Kelly Kearney
Kelly Kearney on 13 Apr 2015
I suggest you read through the Getting Started section of the documentation... it will teach you the basics of passing variables into functions. In this case, you need to include the full file name as the source (as a string), then the name of the variable. Use ncdisp to see the variables in your file. Here's an example with an ECMWF file I have on my computer (lines marked with >> are the ones I type at the command prompt):
>> file = ['/Volumes/MyPassportKak/LargeDatasets/ERA-Interim/' ...
'netcdf-atls05-20140820204544-48535-4662.nc'];
>> ncdisp(file)
Source:
/Volumes/MyPassportKak/LargeDatasets/ERA-Interim/netcdf-atls05-20140820204544-48535-4662.nc
Format:
classic
Global Attributes:
Conventions = 'CF-1.0'
history = '2014-08-20 20:46:19 GMT by grib_to_netcdf-1.13.0: grib_to_netcdf /data/data01/netcdf-atls05-20140820204435-48535-4661.target -o /data/data01/netcdf-atls05-20140820204544-48535-4662.nc'
Dimensions:
longitude = 2
latitude = 2
time = 14616
Variables:
longitude
Size: 2x1
Dimensions: longitude
Datatype: single
Attributes:
units = 'degrees_east'
long_name = 'longitude'
latitude
Size: 2x1
Dimensions: latitude
Datatype: single
Attributes:
units = 'degrees_north'
long_name = 'latitude'
time
Size: 14616x1
Dimensions: time
Datatype: int32
Attributes:
units = 'hours since 1900-01-01 00:00:0.0'
long_name = 'time'
ssrd
Size: 2x2x14616
Dimensions: longitude,latitude,time
Datatype: int16
Attributes:
scale_factor = 347.2613
add_offset = 11378362.3694
_FillValue = -32767
missing_value = -32767
units = 'J m**-2'
long_name = 'Surface solar radiation downwards'
standard_name = 'surface_downwelling_shortwave_flux_in_air'
>> v = ncread(file, 'ssrd');

Sign in to comment.


sotiris tsiougkos
sotiris tsiougkos on 14 Apr 2015
Edited: per isakson on 14 Apr 2015
I have this
>> file = ['/Users/Sotiris_Tsiougkos/Downloads/netcdf_atls05_20150411184037_46649_0075.nc'];
>> ncdisp(file)
Source:
/Users/Sotiris_Tsiougkos/Downloads/netcdf_atls05_20150411184037_46649_0075.nc
Format:
classic
Global Attributes:
Conventions = 'CF-1.0'
history = '2015-04-11 18:40:37 GMT by grib_to_netcdf-1.13.0: grib_to_netcdf /data/data01/netcdf-atls05-20150411184035-46649-0074.target -o /data/data01/netcdf-atls05-20150411184037-46649-0075.nc -utime'
Dimensions:
longitude = 480
latitude = 241
time = 28 (UNLIMITED)
Variables:
longitude
Size: 480x1
Dimensions: longitude
Datatype: single
Attributes:
units = 'degrees_east'
long_name = 'longitude'
latitude
Size: 241x1
Dimensions: latitude
Datatype: single
Attributes:
units = 'degrees_north'
long_name = 'latitude'
time
Size: 28x1
Dimensions: time
Datatype: int32
Attributes:
units = 'hours since 1900-01-01 00:00:0.0'
long_name = 'time'
calendar = 'gregorian'
u10
Size: 480x241x28
Dimensions: longitude,latitude,time
Datatype: int16
Attributes:
scale_factor = 0.0010234
add_offset = 1.6485
_FillValue = -32767
missing_value = -32767
units = 'm s**-1'
long_name = '10 metre U wind component'
>>
Which is the value of u (wind)? I have to make something more?
  1 Comment
mario rivera
mario rivera on 6 Apr 2016
The variable u10, is the component "u" of the wind, measured at 10mts. For obtain the values you have to write
UWIND=ncread(file,'u10')

Sign in to comment.


sotiris tsiougkos
sotiris tsiougkos on 14 Apr 2015
I have this:
* >> file = ['/Users/Sotiris_Tsiougkos/Downloads/netcdf_atls05_20150411184037_46649_0075.nc'];
* >> ncdisp(file)
* Source:
* /Users/Sotiris_Tsiougkos/Downloads/netcdf_atls05_20150411184037_46649_0075.nc
* Format:
* classic
* Global Attributes:
* Conventions = 'CF-1.0'
* history = '2015-04-11 18:40:37 GMT by grib_to_netcdf-1.13.0: grib_to_netcdf /data/data01/netcdf-atls05-20150411184035-46649-0074.target -o /data/data01/netcdf-atls05-20150411184037-46649-0075.nc -utime'
* Dimensions:
* longitude = 480
* latitude = 241
* time = 28 (UNLIMITED)
* Variables:
* longitude
* Size: 480x1
* Dimensions: longitude
* Datatype: single
* Attributes:
* units = 'degrees_east'
* long_name = 'longitude'
* latitude
* Size: 241x1
* Dimensions: latitude
* Datatype: single
* Attributes:
* units = 'degrees_north'
* long_name = 'latitude'
* time
* Size: 28x1
* Dimensions: time
* Datatype: int32
* Attributes:
* units = 'hours since 1900-01-01 00:00:0.0'
* long_name = 'time'
* calendar = 'gregorian'
* u10
* Size: 480x241x28
* Dimensions: longitude,latitude,time
* Datatype: int16
* Attributes:
* scale_factor = 0.0010234
* add_offset = 1.6485
* _FillValue = -32767
* missing_value = -32767
* units = 'm s**-1'
* long_name = '10 metre U wind component'
* >>
Which is the value of u (wind)? I have to make something more?
  2 Comments
Kostas
Kostas on 14 Apr 2015
u10 is the U wind component, as you can notice it has dimensions of lon x lat x time, you can read it in matlab and store in a matrix with
u10 = ncread('file','u10');
i guess you must have a similar file where you have stored the V wind component, which you need to read it also and store it in matlab, so as to calculate the wind component and the product with the potential temperature. I would suggest you to read for the beginning how you could work with ERA-Interim netcdf file in matlab. You could find a good start in this link
sotiris tsiougkos
sotiris tsiougkos on 23 Apr 2015
So i finally export my nc files to matrices. How can i make the function -V*gradT to calculate this product and then to extract it to a new matrix? I have to do this because i want finally to plot the matrix of -V*gradT.

Sign in to comment.


sotiris tsiougkos
sotiris tsiougkos on 15 Apr 2015
>> u10 = ncread('file','u10'); Error using internal.matlab.imagesci.nc/openToRead (line 1259)
Could not open file for reading.
Error in internal.matlab.imagesci.nc (line 121)
this.openToRead();
Error in ncread (line 53)
ncObj = internal.matlab.imagesci.nc(ncFile);
  1 Comment
sotiris tsiougkos
sotiris tsiougkos on 17 Apr 2015
Finally i wrote this >> u10 = ncread(file,'u10'); but when i opened the output matrix u10 i read this Cannot display summaries of variables with more than 524288 elements.

Sign in to comment.


sotiris tsiougkos
sotiris tsiougkos on 15 Apr 2015
>> u10 = ncread('file','u10');
>> u10 = ncread('file','u10');
Error using internal.matlab.imagesci.nc/openToRead (line 1259) Could not open file for reading.
Error in internal.matlab.imagesci.nc (line 121) this.openToRead();
Error in ncread (line 53)
ncObj = internal.matlab.imagesci.nc(ncFile);

sotiris tsiougkos
sotiris tsiougkos on 15 Apr 2015
Finally i wrote this
>> u10 = ncread(file,'u10');
but when i opened the output matrix u10 i read this
Cannot display summaries of variables with more than 524288 elements.

mario rivera
mario rivera on 6 Apr 2016
Hi, I have a question, but first I must to say that my english is not as good as I want, so I hope you can understand what I need.
I have a lot of files .nc into a folder and I need to read them all in just one order. Until now I have wrote this:
rout = ('/Users/aabb/Documents/MATLAB/EN3/');
# c=dir([rout,'*.nc']);
# cd = char(c.name);
#
# SERIETEMP=NaN(12,9,42,730);
# SERIESAL=NaN(12,9,42,730);
#
# for i=1:733
# capa=cd(i,:)
# files=netcdf.open(capa);
# temp= ncread(files,'temperature',[278, 92,1,1],[12,9,42,1],[1,1,1,1]);
# temp=temp-273.15;
# sal= ncread(files,'salinity',[278, 92,1,1],[12,9,42,1],[1,1,1,1]);
# SERIETEMP(:,:,:,i)=temp;
# SERIESAL(:,:,:,i)=sal;
# end
and it works good until the order temp=ncread(files,'temperature',[278, 92,1,1],[12,9,42,1],[1,1,1,1]);
and appear, those alerts:
Error using fopen
Invalid filename.
Error in internal.matlab.imagesci.nc/openToRead (line 1257)
fid = fopen(this.Filename,'r');
Error in internal.matlab.imagesci.nc (line 121)
this.openToRead();
Error in ncread (line 53)
ncObj = internal.matlab.imagesci.nc(ncFile);
I have not idea, about what does it means and how can I solve it. I appreciate a lot for your help.
my best regards,
Mario Rivera
  1 Comment
Kelly Kearney
Kelly Kearney on 6 Apr 2016
Side note:
In the future, creating a new question on the forum is a better way to get an answer, rather than posting a question as an answer to a year-old question. Here, you can't accept the answer, and may confuse future readers of the original question.
-------------
Your problem is that you're mixing up high-level and low-level netcdf functions. When using the high-level ncread, you don't need to worry about opening, closing, etc. And the ncread function takes a file name (as a string) as its input, not a netCDF file identifier.
A few other issues with the code:
  • Creating a character array of the filenames is unnecessary and messy; just stick with the structure output from dir.
  • Using i as a variable is usually discouraged, since it has an alternative value in Matlab, i=sqrt(-1).
With those changes, your code can be rewritten as
rout = ('/Users/aabb/Documents/MATLAB/EN3/');
c = dir([rout,'*.nc']);
SERIETEMP=NaN(12,9,42,730);
SERIESAL=NaN(12,9,42,730);
for ii=1:733
temp= ncread(c(ii).name,'temperature',[278, 92,1,1],[12,9,42,1],[1,1,1,1]);
temp=temp-273.15;
sal= ncread(c(ii).name,'salinity',[278, 92,1,1],[12,9,42,1],[1,1,1,1]);
SERIETEMP(:,:,:,ii)=temp;
SERIESAL(:,:,:,ii)=sal;
end

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!