You might find it easier to handle data in a CSV file instead of a GRIB file. This article demonstrates how to extract data from a GRIB file using ECMWF's ecCodes and save the output as a CSV file. You are expected to have ecCodes installed on a Linux machine before you continue. You are also recommended to add ecCodes' "bin" directory to your PATH. Work below was tested with ecCodes 2.5.0.
Check the GRIB file by issuing command grib_ls -P time t2m_20000801.grib, you should see three GRIB messages in the file at three different times: 600, 1200, 1800 as shown below. Remember three time values were selected in step 1. Typing grib_ls will give you help.
t2m_20000801.grib time edition centre typeOfLevel level dataDate stepRange dataType shortName packingType gridType 600 1 ecmf surface 0 20000801 0 an 2t grid_simple regular_ll 1200 1 ecmf surface 0 20000801 0 an 2t grid_simple regular_ll 1800 1 ecmf surface 0 20000801 0 an 2t grid_simple regular_ll 3 of 3 messages in t2m_20000801.grib 3 of 3 total messages in 1 files |
Say I want to extract lat, lon, 2t (2m temperature) at time = 12:00 from the GRIB file. Run command grib_get_data -w time=1200 t2m_20000801.grib > temp.csv and you will get a file containing data like below. Typing grib_get_data will give you help.
Latitude, Longitude, Value 90.000 0.000 2.7346786499e+02 90.000 0.250 2.7346786499e+02 90.000 0.500 2.7346786499e+02 90.000 0.750 2.7346786499e+02 ... |
Form the CSV file. You may use script below.
#!/usr/bin/env python """ Save as format.py, then run "python format.py". Input file : temp.csv Output file: t2m_20000801.csv """ with open('temp.csv', 'r') as f_in, open('t2m_20000801.csv', 'w') as f_out: f_out.write(next(f_in)) [f_out.write(','.join(line.split()) + '\n') for line in f_in] |
You have a CSV file t2m_20000801.csv, which is ready to be imported in Excel. Notice there are over 1 million records in the file! You may now want to extract data for time=600 and 1800.
Latitude, Longitude, Value 90.000,0.000,2.7346786499e+02 90.000,0.250,2.7346786499e+02 90.000,0.500,2.7346786499e+02 90.000,0.750,2.7346786499e+02 ... |
If you have CDS API and ecCodes (with its Python interface) installed, you retrieve data, extract data and export data to CSV by writing up a Python script. |
Related articles appear here based on the labels you select. Click to edit the macro and add or change labels.
|