pygrib icon indicating copy to clipboard operation
pygrib copied to clipboard

GRIB values manipulation - can it work this way?

Open 60north opened this issue 7 years ago • 5 comments

I try this ...

data, lats, lons = grb.data(lat1=20,lat2=70,lon1=0,lon2=28)
grb['values'] = data
msg = grb.tostring() 
grb = pygrib.fromstring(msg)
data = grb.values()

Script dies with:

File "./get_latest.py", line 81, in open_grib_get_area data= grb.values() File "pygrib.pyx", line 850, in pygrib.gribmessage.getattr (pygrib.c:8490) File "pygrib.pyx", line 1270, in pygrib.gribmessage.getitem (pygrib.c:14140) File "pygrib.pyx", line 1390, in pygrib.gribmessage._reshape_mask (pygrib.c:15560) ValueError: total size of new array must be unchanged

What else I should update in GRIB2, if I attempt writing a subset of the grid in a separate file? Sorry if this not a pygrib question.

60north avatar Oct 01 '17 20:10 60north

I found that grb['Ni'], grb['Nj'] are not updated automatically when you assign new values field to the message. Can it be the problem? numberOfValues, numberOfPoints are automatically updated and recalculated

60north avatar Oct 02 '17 10:10 60north

You can't write a subset back to the same file, since the grid definitions are now invalid. It would have worked if you did not create a subset of the grid. You need a template file that has the same grid, or create a grib2 file from scratch using ncepgrib2.Grib2Encode.

jswhit avatar Oct 02 '17 14:10 jswhit

Can I modify the grid definitions manually?

grb['Nj'] = data.shape[0] grb['Ni'] = data.shape[1]

and I preseume I would have to modify

latitudeOfFirstGridPointInDegrees longitudeOfFirstGridPointInDegrees

latitudeOfLastGridPointInDegrees longitudeOfLastGridPointInDegrees

?

2017-10-02 17:51 GMT+03:00 Jeff Whitaker [email protected]:

You can't write a subset back to the same file, since the grid definitions are now invalid. You need a template file that has the same grid, or create a grib2 file from scratch using ncepgrib2.Grib2Encode https://jswhit.github.io/pygrib/ncepgrib2_docs/ncepgrib2.Grib2Encode-class.html .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jswhit/pygrib/issues/69#issuecomment-333558386, or mute the thread https://github.com/notifications/unsubscribe-auth/AMbusT2xvBIjGk6reO_uAeEdg3aFM18gks5soPiIgaJpZM4PqB2i .

-- С уважением, Федор Дружинин

Клуб Парусных Экспедиций 60ºN http://www.60north.ru

60north avatar Oct 02 '17 15:10 60north

Sorry if these are a GRIB newbie questions, I try to subset a huge HIRLAM GRIB, cnvgrib segfauls on it:) pygrib seems to be the only package that works so far

2017-10-02 18:00 GMT+03:00 Фёдор Дружинин [email protected]:

Can I modify the grid definitions manually?

grb['Nj'] = data.shape[0] grb['Ni'] = data.shape[1]

and I preseume I would have to modify

latitudeOfFirstGridPointInDegrees longitudeOfFirstGridPointInDegrees

latitudeOfLastGridPointInDegrees longitudeOfLastGridPointInDegrees

?

2017-10-02 17:51 GMT+03:00 Jeff Whitaker [email protected]:

You can't write a subset back to the same file, since the grid definitions are now invalid. You need a template file that has the same grid, or create a grib2 file from scratch using ncepgrib2.Grib2Encode https://jswhit.github.io/pygrib/ncepgrib2_docs/ncepgrib2.Grib2Encode-class.html .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jswhit/pygrib/issues/69#issuecomment-333558386, or mute the thread https://github.com/notifications/unsubscribe-auth/AMbusT2xvBIjGk6reO_uAeEdg3aFM18gks5soPiIgaJpZM4PqB2i .

-- С уважением, Федор Дружинин

Клуб Парусных Экспедиций 60ºN http://www.60north.ru

-- С уважением, Федор Дружинин

Клуб Парусных Экспедиций 60ºN http://www.60north.ru

60north avatar Oct 02 '17 15:10 60north

No you can't modify the grid definition section with pygrib. If you don't have a template file with the right GDS defined, you need to create the whole grib message from scratch.

The grib writing capability of pygrib is very limited - you basically have to have a template grib file that allows you to just over-write the data and some of the simpler metadata (like the times).

jswhit avatar Oct 02 '17 15:10 jswhit