pygrib
pygrib copied to clipboard
Proper procedure for remapping data?
I have grib messages that use two different grids (one regular global grid and one gaussian global grid, and different numbers of lats and lons). I'd like to interpolate values on one grid to the other grid, replace the old grid in the grib message, and update the coordinates in the grib message accordingly. What's the proper procedure for doing that, or is that even allowed? I haven't found anything in the documentation about it yet. I can update the values to be a differently-shaped array (not sure if that's a bug), but pygrib won't allow me to touch latitudes and longitudes directly. Here's what I've tried:
- Updating the grid info (latitudeOfFirstGridPointInDegrees, longitudeOfFirstGridPointInDegrees, latitudeOfLastGridPointInDegrees, longitudeOfLastGridPointInDegrees, Ni, and Nj). This is the closest I've gotten, but the latitudes are still incorrect, I suspect because of the gridType. Changing gridType raises an exception in latlons() because of latitudes being a scalar when an array is expected.
- Keeping around a "template" grib message that has the correct latitudes and longitudes and changing the values and all the other read/write keys in the template. This seg faults for some reason after processing several grib messages.
Any help is appreciated!
You can either create a new grib2 file from scratch using ncepgrib2.Grib2Encode (which requires some detailed knowledge of grib2 encoding tables) or update an existing grib2 file with the correct grid definition section using pygrib. I don't think you can modify an existing grid with pygrib. This route is much easier, but you have to have an existing template file on the right grid.