What is the best way to open GRIB files in Matlab R2021a

Hello,
I need to use weather data from NOAA in my simulations. I am currently using the NetCDF data and they are very large (>6 G) for 1 hour worth of data. Thus making it computationally expensive to read the data, expecially when one need to run the simulation with 1 week worth of weather data.
The best option is to use the weather data with the GRIB format.
I have searched the internet with little success on how to read GRIB files in MATLAB. The viable option is https://github.com/nctoolbox/nctoolbox/wiki . However, nctoolbox does not support newer versions of MATLAB: https://github.com/nctoolbox/nctoolbox/issues/128.
Please, what is the best way to analyze GRIB data files in newer versions for MATLAB. i.e. R2021a.
Thank you for your help.

 Accepted Answer

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.

4 Comments

Is this toolbox able to decode GEFS or GFS Grib2 files (i.e., read in the numerical arrays)?
https://ftp.ncep.noaa.gov/data/nccf/com/gens/prod/
I usually code in Python as libraries are free, easy to install (via Conda) and usually work without any problems [& if there are, ChatGPT/Grok etc are able to help out]. I was asked recenly to convert one of my Python codes into Matlab but I am running into all sorts of issues when trying to decode GEFS/GFS grib2 files (with the nctoolbox as none of the methods are able to extract the numerical arrays; just cell arrays - not even ChatGPT has a solution). The above toolbox is behind a paywall (as if Matlab wasn't already expensive enough ...) so I was wondering if it was tested on GEFS/GFS forecast files? Thanks-
I use the toolbox to decode GFS grib2 files. It is works really well.
I downloaded the trial version and it is not feasible to retrieve arrays based on var names. One has to guess what they are based on max/min values or plots; so if you have a GRIB2 file with 30 variables, it is essentially not practically feasible to retrieve variables individually.
This function should help you out
function [wind_data] = read_grib_file(file,PLB,PUB)
% file = 'gdas.t00z.pgrb2.0p25.f000.grb2';
info = georasterinfo(file);
% md = info.Metadata;
% PLB = 100;
% PUB = 100000;
short_name = info.Metadata.ShortName;
p_bands_text = regexp(short_name,'(\d*)(?=-ISBL)','match');
p_idx = ~cellfun(@isempty,p_bands_text);
p_bands_text(~p_idx) = {'0'};
elems = info.Metadata.Element;
good_elems = (strcmp(elems,'TMP')| strcmp(elems,'UGRD')|strcmp(elems,'VGRD')|strcmp(elems,'HGT')|strcmp(elems,'DZDT') )& p_idx;
p_bands = cellfun(@str2num,p_bands_text);
good_p_bands = p_bands>=PLB & p_bands<=PUB;
good_bands = find(good_elems&good_p_bands);
good_metadata = info.Metadata(good_bands,:);
lon_lims = info.RasterReference.LongitudeLimits;
lat_lims = info.RasterReference.LatitudeLimits;
grid_res_lon = info.RasterReference.SampleSpacingInLongitude;
grid_res_lat = info.RasterReference.SampleSpacingInLatitude;
lon_array = lon_lims(1):grid_res_lon:lon_lims(2);
lon_array(lon_array<0) = 360+lon_array(lon_array<0);
[~,sort_i] = sort(lon_array);
lat_array = fliplr(lat_lims(1):grid_res_lat:lat_lims(2))';
is_temp = strcmp(good_metadata.Element,'TMP');
is_u = strcmp(good_metadata.Element,'UGRD');
is_v = strcmp(good_metadata.Element,'VGRD');
is_vz = strcmp(good_metadata.Element,'DZDT');
is_z = strcmp(good_metadata.Element,'HGT');
grib_file = readgeoraster(file,Bands = good_bands);
grib_file_test = flip(grib_file(:,sort_i,:),1);
grib_file = permute(grib_file_test,[2,1,3]);
wind_data.T = grib_file(:,:,is_temp)+273.15;
wind_data.U = grib_file(:,:,is_u);
wind_data.V = grib_file(:,:,is_v);
%Atm_data.Z_test = grib_file_test(:,:,is_z);
end

Sign in to comment.

More Answers (2)

I run nctoolbox just fine on the latest versions of MATLAB (R2020 onwards). In some cases, you might need to disable the version of protobuf3.jar shipped with matlab and therefore have matlab use the native version of this java library with the OS. To do this, I found merely renaming $MATLABROOT/java/jarext/protobuf3.jar to something like protobuf3.jar.save works with no secondary consequences that I found.
However, I think starting with R2021b, this may not even be necessary anymore and nctoolbox works straight out of the box, at least for me. I think this is because MATLAB stopped shipping a copy of protobuf3.jar with its distribution.

1 Comment

Peter, Thank you. Your rsuggestion worked. And the whole bunch of other java errors I was getting have disappeared.
However it still shows up this message "2022-08-05 10:01:30,513 [main] WARN ucar.nc2.grib.grib2.Grib2Index - Grib2Index bad size = -1 for test.grb2 index = test.grb2.gbx9"
Is this something to be concerned about?

Sign in to comment.

Please refer to the below File Exchange link for GRIB file reader

4 Comments

I found the instructions with the reader to be difficult to understand. Having not been successful in figure out how to get this package to install properly... Seems like that should be an easily way to get .grb2 files into matlab?
I also tried this package... https://github.com/nctoolbox/nctoolbox with no success.
hi i have the same issue. i download the toolbox and cannot find a way to open the data.
i am not sure to understand what it is about. If the data are downloaded already (without the API) from ECWMF copernicus. how do we open them?
i have no isse with .nc file.
Is there an existing function to open .grid file?
Matlab-CDI is older than dirt (it was last updated in 2014), and is unlikely to work with newer versions of Matlab. In addition, the installation is complicated, involving the compilation of MEX files and installation of NetCDF libraries. I don't know if it even works with the newer GRIB2 format.

Sign in to comment.

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!