Project

General

Profile

binding cdo python: open grib files?

Added by Juanma Sancho over 11 years ago

When trying to extract variables from a grib file using the following command I get an error (see below). If ifile is in netcdf format the output is correct. Any hints?

vals = cdo.timmean(input=ifile,returnCdf=True).variables['SID'][:]
TypeError: Error: /tmp/cdoPyyNpcD0 is not a valid NetCDF 3 file


Replies (4)

RE: binding cdo python: open grib files? - Added by Ralf Mueller over 11 years ago

Direct access to the data is done by numpy (python) and narray (ruby). I choosed these two interfaces, because they are available on the most systems and easy to install. That's why it's limited to netcdf input files (see here).

Gribdata records cannot be accessed by name, because grib uses integers for this, but you can copy your grb file on the fly and access the data:

>>> data  = cdo.timmean(input = '-copy mergetimeb.grb',options = '-f nc',returnCdf=True)
>>> data
<scipy.io.netcdf.netcdf_file object at 0x1b0dc50>
>>> data.variables
{'lat': <scipy.io.netcdf.netcdf_variable object at 0x1b0dbd0>, 'var1': <scipy.io.netcdf.netcdf_variable object at 0x1b0db10>, 'var2': <scipy.io.netcdf.netcdf_variable object at 0x1b0da90>, 'lon': <scipy.io.netcdf.netcdf_variable object at 0x1b0dc10>, 'time': <scipy.io.netcdf.netcdf_variable object at 0x1b0dad0>}
>>> data.variables['var2'][:]
array([[[ 3.52127862,  3.51023746,  3.51335645, ...,  3.92711973,
          3.92375684,  3.92830992],
        [ 3.55248594,  3.54958677,  3.5563252 , ...,  3.95879102,
          3.96648145,  3.97318912],
        [ 3.51718926,  3.5167315 ,  3.52176094, ...,  3.97872496,
          3.97777295,  3.9736774 ],
        ..., 
        [ 2.19552803,  2.20247984,  2.19984317, ...,  2.48209405,
          2.448946  ,  2.42951846],
        [ 2.15401793,  2.15449405,  2.14536309, ...,  2.44776177,
          2.41152525,  2.38703799],
        [ 2.171767  ,  2.17414737,  2.17752862, ...,  2.43432808,
          2.40647149,  2.38654971]]], dtype=float32)

hth
ralf

RE: binding cdo python: open grib files? - Added by Juanma Sancho over 11 years ago

Thank you Ralf. This is a good solution.
Please, Let me ask you also why the array's dimensions are 4. I have seen that the data are in the 2 last dimensions, and the values for the first and second are 0. What information is in the first 2 dimensions of the array?
Thanks

RE: binding cdo python: open grib files? - Added by Ralf Mueller over 11 years ago

after reading in the data with

data  = cdo.timmean(input = '-copy mergetimeb.grb',options = '-f nc',returnCdf=True)
v1 = data.variables['var1'][:]

I check the shape with (ipython2)
In [36]: v1.shape
Out[36]: (1, 323, 521)

I do not see any 4th dimension: the first one holds the time information and there is only one, because the result of timmean is of cause a single timestep only. You can read it's value just like with any other variable:

In [37]: time = data.variables['time'][:]

In [38]: time
Out[38]: array([ 19831001.])

You can check the dimensions of the input with
In [52]: data.dimensions
Out[52]: {'lat': 323, 'lon': 521, 'time': None}

To see, what methods are available, you could use the TAB-completion method when working interactively with ipython2.

RE: binding cdo python: open grib files? - Added by Juanma Sancho over 11 years ago

Thanks a lot for your answers!

    (1-4/4)