Commit 0300740f authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Added function grbDecompress().

parent 0b68bba9
......@@ -764,7 +764,6 @@ int cgribexScanTimestep1(stream_t *streamptr)
int rdate = 0, rtime = 0, tunit = 0;
bool fcast = false;
int vlistID;
size_t unzipsize;
char paramstr[32];
streamptr->curTsID = 0;
......@@ -793,29 +792,15 @@ int cgribexScanTimestep1(stream_t *streamptr)
break;
}
if ( recsize > buffersize )
{
buffersize = recsize;
gribbuffer = Realloc(gribbuffer, buffersize);
}
ensureBufferSize(recsize, &buffersize, &gribbuffer);
readsize = recsize;
rstatus = gribRead(fileID, (unsigned char *)gribbuffer, &readsize);
if ( rstatus ) break;
int comptype = CDI_COMPRESS_NONE;
if ( gribGetZip(recsize, (unsigned char *)gribbuffer, &unzipsize) > 0 )
{
comptype = CDI_COMPRESS_SZIP;
unzipsize += 100; /* need 0 to 1 bytes for rounding of bds */
if ( buffersize < unzipsize )
{
buffersize = unzipsize;
gribbuffer = Realloc(gribbuffer, buffersize);
}
}
const int comptype = grbDecompress(recsize, &buffersize, &gribbuffer);
size_t sec2len = cgribexSection2Length(gribbuffer, buffersize);
const size_t sec2len = cgribexSection2Length(gribbuffer, buffersize);
if (sec2len > cgribexp->sec2len)
{
cgribexp->sec2len = sec2len;
......@@ -947,7 +932,6 @@ int cgribexScanTimestep2(stream_t * streamptr)
int nrecs, recID;
size_t recsize = 0;
bool warn_numavg = true;
size_t unzipsize;
char paramstr[32];
streamptr->curTsID = 1;
......@@ -998,25 +982,13 @@ int cgribexScanTimestep2(stream_t * streamptr)
break;
}
if ( recsize > buffersize )
{
buffersize = recsize;
gribbuffer = Realloc(gribbuffer, buffersize);
}
ensureBufferSize(recsize, &buffersize, &gribbuffer);
readsize = recsize;
rstatus = gribRead(fileID, (unsigned char *)gribbuffer, &readsize);
if ( rstatus ) break;
if ( gribGetZip(recsize, (unsigned char *)gribbuffer, &unzipsize) > 0 )
{
unzipsize += 100; /* need 0 to 1 bytes for rounding of bds */
if ( buffersize < unzipsize )
{
buffersize = unzipsize;
gribbuffer = Realloc(gribbuffer, buffersize);
}
}
grbDecompress(recsize, &buffersize, &gribbuffer);
nrecs_scanned++;
cgribexDecodeHeader(cgribexp, (int *) gribbuffer, (int)recsize, &lmv, &iret);
......@@ -1180,7 +1152,6 @@ int cgribexScanTimestep(stream_t * streamptr)
int taxisID = -1;
int rindex, nrecs = 0;
int nrecs_scanned;
size_t unzipsize;
char paramstr[32];
/*
......@@ -1230,11 +1201,7 @@ int cgribexScanTimestep(stream_t * streamptr)
break;
}
if ( recsize > buffersize )
{
buffersize = recsize;
gribbuffer = Realloc(gribbuffer, buffersize);
}
ensureBufferSize(recsize, &buffersize, &gribbuffer);
readsize = recsize;
rstatus = gribRead(fileID, (unsigned char *)gribbuffer, &readsize);
......@@ -1245,15 +1212,7 @@ int cgribexScanTimestep(stream_t * streamptr)
break;
}
if ( gribGetZip(recsize, (unsigned char *)gribbuffer, &unzipsize) > 0 )
{
unzipsize += 100; /* need 0 to 1 bytes for rounding of bds */
if ( buffersize < unzipsize )
{
buffersize = unzipsize;
gribbuffer = Realloc(gribbuffer, buffersize);
}
}
grbDecompress(recsize, &buffersize, &gribbuffer);
nrecs_scanned++;
cgribexDecodeHeader(cgribexp, (int *) gribbuffer, (int)recsize, &lmv, &iret);
......
......@@ -30,6 +30,21 @@ void ensureBufferSize(size_t requiredSize, size_t *curSize, void **buffer)
}
}
int grbDecompress(size_t recsize, size_t *buffersize, void **gribbuffer)
{
int comptype = CDI_COMPRESS_NONE;
size_t unzipsize;
if ( gribGetZip(recsize, (unsigned char *)*gribbuffer, &unzipsize) > 0 )
{
comptype = CDI_COMPRESS_SZIP;
unzipsize += 100; /* need 0 to 1 bytes for rounding of bds */
ensureBufferSize(unzipsize, buffersize, gribbuffer);
}
return comptype;
}
// Regarding operation to change parameter identification:
// change if cdiGribChangeParameterID.active
......
......@@ -2,6 +2,7 @@
#define STREAM_GRB_H
void ensureBufferSize(size_t requiredSize, size_t *curSize, void **buffer);
int grbDecompress(size_t recsize, size_t *buffersize, void **gribbuffer);
static inline bool gribbyte_get_bit(int number, int bit) { return (bool)((number >> (8-bit)) & 1); }
static inline void gribbyte_set_bit(int *number, int bit) { *number |= 1 << (8-bit); }
......
......@@ -817,22 +817,11 @@ int gribapiVarCompare(compvar2_t compVar, record_t record, int flag)
}
static
grib_handle *gribapiGetDiskRepresentation(size_t recsize, size_t *buffersize, void **gribbuffer, int *outDatatype, int *outCompressionType, size_t *outUnzipsize)
grib_handle *gribapiGetDiskRepresentation(size_t recsize, size_t *buffersize, void **gribbuffer, int *outDatatype, int *outCompressionType)
{
const int gribversion = (int)((char*)*gribbuffer)[7];
if ( gribversion <= 1 )
{
if ( gribGetZip(recsize, *gribbuffer, outUnzipsize) > 0 )
{
*outCompressionType = CDI_COMPRESS_SZIP;
ensureBufferSize(*outUnzipsize + 100, buffersize, gribbuffer);
}
else
{
*outCompressionType = CDI_COMPRESS_NONE;
}
}
if ( gribversion <= 1 ) *outCompressionType = grbDecompress(recsize, buffersize, gribbuffer);
grib_handle *gh = grib_handle_new_from_message(NULL, *gribbuffer, recsize);
......@@ -943,6 +932,7 @@ int gribapiScanTimestep1(stream_t * streamptr)
streamptr->ntsteps = 1;
break;
}
ensureBufferSize(recsize, &buffersize, &gribbuffer);
size_t readsize = recsize;
......@@ -950,8 +940,7 @@ int gribapiScanTimestep1(stream_t * streamptr)
if ( rstatus ) break;
int datatype, comptype = 0;
size_t unzipsize;
gh = gribapiGetDiskRepresentation(recsize, &buffersize, &gribbuffer, &datatype, &comptype, &unzipsize);
gh = gribapiGetDiskRepresentation(recsize, &buffersize, &gribbuffer, &datatype, &comptype);
nrecs_scanned++;
GRIB_CHECK(my_grib_set_double(gh, "missingValue", CDI_default_missval), 0);
......@@ -1157,15 +1146,14 @@ int gribapiScanTimestep2(stream_t * streamptr)
streamptr->ntsteps = 2;
break;
}
ensureBufferSize(recsize, &buffersize, &gribbuffer);
size_t readsize = recsize;
rstatus = gribRead(fileID, gribbuffer, &readsize);
if ( rstatus ) break;
size_t unzipsize;
if ( gribGetZip(recsize, gribbuffer, &unzipsize) > 0 )
ensureBufferSize(unzipsize + 100, &buffersize, &gribbuffer);
grbDecompress(recsize, &buffersize, &gribbuffer);
nrecs_scanned++;
gh = grib_handle_new_from_message(NULL, gribbuffer, recsize);
......@@ -1395,9 +1383,7 @@ int gribapiScanTimestep(stream_t * streamptr)
break;
}
size_t unzipsize;
if ( gribGetZip(recsize, gribbuffer, &unzipsize) > 0 )
ensureBufferSize(unzipsize + 100, &buffersize, &gribbuffer);
grbDecompress(recsize, &buffersize, &gribbuffer);
nrecs_scanned++;
gh = grib_handle_new_from_message(NULL, gribbuffer, recsize);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment