how to analyze matlab grib files?
27 views (last 30 days)
Show older comments
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
0 Comments
Answers (10)
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
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.)
sotiris tsiougkos
on 12 Apr 2015
Edited: per isakson
on 14 Apr 2015
1 Comment
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');
sotiris tsiougkos
on 14 Apr 2015
Edited: per isakson
on 14 Apr 2015
1 Comment
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')
sotiris tsiougkos
on 14 Apr 2015
2 Comments
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
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
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
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!