Open dasarkisov opened 1 week ago
grib2 compression is lossy, rewriting the same data multiple times will progressively lose information
grib2 compression is lossy, rewriting the same data multiple times will progressively lose information
I follow the procedure, where I open a gribfile containing temperature field, perform some calculations with the field and write it back down. Then later I take the result and do the same procedure. I cannot afford to loose any information. What could you suggest for these actions?
You can turn off compression by setting the dataRepresentationTemplateNumber key to 4 and then re-writing the file (there's a grib_repack
utility that does this)
grib_repack
Is it a command line program only? I wish I could do it in my main script. I tried to set the key in this way, but that wouldn't work...
grbs = pygrib.open('data/gfsT_2024-06-23_00')
for grb in grbs:
grb['dataRepresentationTemplateNumber'] = 4
print(grb[1]['dataRepresentationTemplateNumber'])
>> 0
take a look at the source code. Note that this only works for GRIB2 files.
take a look at the source code. Note that this only works for GRIB2 files.
Yep, I had already had, thank you. At the bottom of the source code, there is just what I did above:
for grb in grbs:
if grb['editionNumber'] != 2:
sys.stdout.write('not a GRIB2 message, skipping ..\n')
continue
if grb['dataRepresentationTemplateNumber'] == ipack:
sys.stdout.write('no repacking required, skipping ..\n')
grb['dataRepresentationTemplateNumber'] = ipack
But the 'dataRepresentationTemplateNumber' value (which should be 4 in my case) does not change. Should I ignore this and write the grib anyway?
1) Why does the pygrib writes the values 273.15 as 273.1499939? Here is what I have:
Here is what is written to a grib-message in the end:
This is such a pain later on.
2) After some computations, I have the following array and again write it to a grib-message:
Then I write grib-message to a grib-file, saving it as a new one, and then reopen the new grib-file only to behold the the following values:
Why on Earth the values are different from those I had been writing??
3) An now the most ridiculous thing. When I write an array like this:
save message, save grib-file, reopen again, here is what I get:
Value "273.15" against value "273.1045...". What the hell? I'm sure it's me missing something out...
4) What the hell is this??