...
To copy only the pressure levels from a file
Code Block > grib_copy -w levtype=pl ../data/tigge_pf_ecmwf.grib2 out.grib
To copy only the fields that are not on pressure levels from a file
Code Block > grib_copy -w levtype!=pl ../data/tigge_pf_ecmwf.grib2 out.grib
To copy only the first three fields from a file
Code Block > grib_copy -w count=1/2/3 ../data/tigge_pf_ecmwf.grib2 out.grib
A grib_file with multi field messages can be converted in single field messages with a simple grib_copy.
Code Block > grib_copy multi.grib simple.grib
Use the square brackets to insert the value of a key in the name of the output file (This is a good way to split a large GRIB file)
Code Block > grib_copy in.grib 'out_[shortName].grib'
Note: we need to quote the name of the output so the shell does not interpret the square brackets
To copy fields whose typeOfLevel is either 'surface' or 'meanSea'
Code Block > grib_copy -w typeOfLevel=surface/meanSea orig.grib out.grib
To copy selected fields and apply sorting (sorted by level in ascending order)
Code Block > grib_copy -w typeOfLevel=heightAboveGround -B'level:i asc' tigge_af_ecmwf.grib2 out.grib
Note: we need to specify the ':i' to get a numerical sort. By default values are sorted as strings so a level of 100 would come before 20!
grib_dump examples
To dump in a WMO documentation style with hexadecimal octet values (-H).
Code Block > grib_dump -OH ../data/reduced_gaussian_model_level.grib1
To add key aliases and type information.
Code Block > grib_dump -OtaH ../data/reduced_gaussian_model_level.grib1
To obtain all the key names (computed keys included) available in a grib file.
Code Block > grib_dump -D ../data/regular_latlon_surface.grib1
grib_filter examples
The grib_filter processes sequentially all grib messages contained in the input files and applies the rules to each one of them. Input messages can be written to the output by using the "write" statement. The write statement can be parameterised so that output is sent to multiple files depending on key values used in the output file name. If we write a rules_file containing the only statement:
Code Block write "../data/split/[centre]_[date]_[dataType]_[levelType].grib[editionNumber]";
Applying this rules_file to the "../data/tigge_pf_ecmwf.grib2" grib file we obtain several files in the ../data/split directory containing fields split according to their key values
Code Block > grib_filter rules_file ../data/tigge_pf_ecmwf.grib2 > ls ../data/split ecmf_20060619_pf_sfc.grib2 ecmf_20060630_pf_sfc.grib2 ecmf_20070122_pf_pl.grib2 ecmf_20070122_pf_pt.grib2 ecmf_20070122_pf_pv.grib2 ecmf_20070122_pf_sfc.grib2
The key values in the file name can also be obtained in a different format by indicating explicitly the type required after a colon.
- :i for integer
- :d for double
- :s for string
The following statement works in a slightly different way from the previous example, including in the output file name the integer values for centre and dataType.
Code Block write "../data/split/[centre:i]_[date]_[dataType:i]_[levelType].grib[editionNumber]";
Running the same command again we obtain a different list of files.
Code Block > grib_filter rules_file ../data/tigge_pf_ecmwf.grib2 > ls ../data/split 98_20060619_4_sfc.grib2 98_20060630_4_sfc.grib2 98_20070122_4_pl.grib2 98_20070122_4_pt.grib2 98_20070122_4_pv.grib2 98_20070122_4_sfc.grib2
Other statements are allowed in the grib_filter syntax:
- if ( condition ) { block of rules } else { block of rules } The condition can be made using ==,!= and joining single block conditions with || and && The statement can be any valid statement also another nested condition
- set keyname = keyvalue;
- print "string to print also with key values like in the file name"
- transient keyname1 = keyname2;
- comments beginning with #
- defined(keyname) to check if a key is defined in a message
- missing(keyname) to check if the value of the key is set to MISSING (Note: This does not apply to codetable keys)
- To set a key value to MISSING, use 'set key=MISSING;' (note the case)
- You can also make an assertion with 'assert(condition)'. If condition is false, it will abort the filter.
A complex example of grib_filter rules is the following to change temperature in a grib edition 1 file.
Code Block # Temperature if ( level == 850 && indicatorOfParameter == 11 ) { print "found indicatorOfParameter=[indicatorOfParameter] level=[level] date=[date]"; transient oldtype = type ; set identificationOfOriginatingGeneratingSubCentre=98; set gribTablesVersionNo = 128; set indicatorOfParameter = 130; set localDefinitionNumber=1; set marsClass="od"; set marsStream="kwbc"; # Negatively/Positively Perturbed Forecast if ( oldtype == 2 || oldtype == 3 ) { set marsType="pf"; set experimentVersionNumber="4001"; } # Control Forecast if ( oldtype == 1 ) { set marsType="cf"; set experimentVersionNumber="0001"; } set numberOfForecastsInEnsemble=11; write; print "indicatorOfParameter=[indicatorOfParameter] level=[level] date=[date]"; print; }
Here is an example of an IF statement comparing a key with a string. Note you have to use the "is" keyword for strings and not "==", and to negate you add the "!" before the whole condition:
Code Block # Select Geopotential Height messages which are not on a Reduced Gaussian Grid if (shortName is "gh" && !(gridType is "reduced_gg" )) { set step = 72; }
The switch statement is an enhanced version of the if statement. Its syntax is the following:
Code Block switch (key1) { case val1: # statements case val2: # statements default: # statements }
The value of the key given as argument to the switch statement is matched against the values specified in the case statements. If there is a match, then the statements corresponding to the matching case are executed. Otherwise, the default case is executed. The default case is mandatory if the case statements do not cover all the possibilities. The "~" operator can be used to match "anything". The following is an example of the switch statement:
Code Block print 'Processing paramId=[paramId] [shortName] [stepType]'; switch (shortName) { case 'tp' : set stepType='accum'; print 'Message #[count]: Total Precipitation'; case '10u' : set typeOfLevel='surface'; print 'Message #[count]: 10m U-Wind'; default: }
grib_get examples
grib_get fails if a key is not found.
Code Block > grib_get -p nosuchkey ../data/tigge_pf_ecmwf.grib2 </p></li><br /></ol><h2>grib_get_data examples</h2><ol><li><p> To get a latitude, longitude, value list, skipping the missing values(=9999) <ac:structured-macro ac:name="code"><ac:plain-text-body><![CDATA[ > grib_get_data ../data/reduced_gaussian_model_level.grib2
If you want to define your missing value=1111 and to print the string 'missing' in place of it
Code Block > grib_get_data -m 1111:missing ../data/reduced_gaussian_model_level.grib2
If you want to print the value of other keys with the data value list
Code Block > grib_get_data -p centre,level,step ../data/reduced_gaussian_model_level.grib2
grib_index_build examples
By default grib_index_build will index on the MARS keys.
Code Block > grib_index_build ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2
To specify a custom list of keys to index on, use the -k option.
Code Block > grib_index_build -k paramId,dataDate ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2
grib_ls examples
Without options a default list of keys is printed. The default list is different depending on the type of grib message.
Code Block > grib_ls ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2
...