Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize GRIB2 file #205

Open
Bap250 opened this issue Jul 20, 2022 · 1 comment
Open

Resize GRIB2 file #205

Bap250 opened this issue Jul 20, 2022 · 1 comment

Comments

@Bap250
Copy link

Bap250 commented Jul 20, 2022

Hello !

I am a novice in computer science applied to meteorology.
I want to extract data from a GRIB2 file. I need to resize the files because I am downloading raw data from a global weather forecast model (ICON) and I just need a small footprint of the model. So I need to create a new GRIB2 file less heavy in storage with just the span I am interested in, that is France.

I saw that it was possible to use the "pygrib" package.
But I tried to write the script but it doesn't work.

The output GRIB2 file can't be opened on any software that processes GRIB2 data. I even have the impression that it does not resize. It just separated the timelines from the original grib2 file.

Below is the script I wrote in python language. Sorry for the formatting ! I don't know how to add code with a nice layout 👍 😎 !

Can anyone help me?

I thank you in advance for your help.
Bap250

import pygrib

grbs = pygrib.open('originalfile.grib2')
grbs.seek(2)
grbs.tell()
grb = grbs.read(1)[0]
grbs.tell()

grb = grbs.select(name='Total Precipitation')[0].data(lat1 = 43, lat2 = 51, lon1 = -4, lon2 = 8)

print(grb)

grbs.close()

grbout = open('outfile.grib2', 'wb')
grbout.write(grb[0])
grbout.close()`

@jswhit
Copy link
Owner

jswhit commented Jul 24, 2022

you need a template file that is the same domain as the one you want to save, and then you can overwrite the data and change the metadata. Unfortunately, pygrib cannot create a grib2 file from scratch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants