Commit 41928eb5 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

added grbUnzipRecord() to unzip grib records before decoding

parent 810edf92
......@@ -624,7 +624,7 @@ int cgribexScanTimestep1(stream_t * streamptr)
streamptr->ntsteps = 1;
break;
}
if ( recsize > buffersize )
if ( (size_t)recsize > buffersize )
{
buffersize = (size_t)recsize;
gribbuffer = (unsigned char *) realloc(gribbuffer, buffersize);
......@@ -1339,62 +1339,20 @@ int cgribexScanTimestep(stream_t * streamptr)
#endif
int cgribexDecode(unsigned char *gribbuffer, int gribsize, double *data, int gridsize,
int unreduced, int *nmiss, int *zip, double missval)
int unreduced, int *nmiss, double missval)
{
int status = 0;
#if defined (HAVE_LIBCGRIBEX)
int iret = 0, iword = 0;
int isec0[2], isec1[4096], isec2[4096], isec3[2], isec4[512];
int izip;
long unzipsize;
double fsec2[512], fsec3[2];
char hoper[2];
*zip = 0;
if ( unreduced ) strcpy(hoper, "R");
else strcpy(hoper, "D");
FSEC3_MissVal = missval;
if ( (izip = gribGetZip(gribsize, gribbuffer, &unzipsize)) > 0 )
{
*zip = izip;
if ( izip == 128 ) /* szip */
{
unsigned char *itmpbuffer = NULL;
size_t itmpbuffersize = 0;
if ( unzipsize < (long) gribsize )
{
fprintf(stderr, "Decompressed size smaller than compressed size (in %d; out %ld)!\n",
gribsize, unzipsize);
return (status);
}
if ( itmpbuffersize < (size_t) gribsize )
{
itmpbuffersize = (size_t)gribsize;
itmpbuffer = (unsigned char *) realloc(itmpbuffer, itmpbuffersize);
}
memcpy(itmpbuffer, gribbuffer, itmpbuffersize);
unzipsize += 100; /* need 0 to 1 bytes for rounding of bds */
gribsize = gribUnzip(gribbuffer, unzipsize, itmpbuffer, gribsize);
if ( gribsize <= 0 )
Error("Decompression problem!");
free(itmpbuffer);
}
else
{
Error("Decompression for %d not implemented!", izip);
}
}
gribExDP(isec0, isec1, isec2, fsec2, isec3, fsec3, isec4, data,
gridsize, (int *) gribbuffer, gribsize, &iword, hoper, &iret);
......@@ -2177,6 +2135,7 @@ void cgribexDefLevel(int *isec1, int *isec2, double *fsec2, int zaxisID, int lev
static
void cgribexDefMask(int *isec3)
{
UNUSED(isec3);
}
static
......
......@@ -6,7 +6,7 @@ int cgribexScanTimestep2(stream_t * streamptr);
int cgribexScanTimestep(stream_t * streamptr);
int cgribexDecode(unsigned char *gribbuffer, int gribsize, double *data, int gridsize,
int unreduced, int *nmiss, int *zip, double missval);
int unreduced, int *nmiss, double missval);
size_t cgribexEncode(int memtype, int varID, int levelID, int vlistID, int gridID, int zaxisID,
int vdate, int vtime, int tsteptype, int numavg,
......
......@@ -185,11 +185,12 @@ int grbInqRecord(stream_t * streamptr, int *varID, int *levelID)
void grbDefRecord(stream_t * streamptr)
{
UNUSED(streamptr);
}
static
int grbDecode(int filetype, unsigned char *gribbuffer, int gribsize, double *data, int gridsize,
int unreduced, int *nmiss, int *zip, double missval, int vlistID, int varID)
int unreduced, int *nmiss, double missval, int vlistID, int varID)
{
int status = 0;
......@@ -201,18 +202,71 @@ int grbDecode(int filetype, unsigned char *gribbuffer, int gribsize, double *dat
if ( cdiNAdditionalGRIBKeys > 0 )
Error("CGRIBEX decode does not support reading of additional GRIB keys!");
#endif
status = cgribexDecode(gribbuffer, gribsize, data, gridsize, unreduced, nmiss, zip, missval);
status = cgribexDecode(gribbuffer, gribsize, data, gridsize, unreduced, nmiss, missval);
}
else
#endif
{
status = gribapiDecode(gribbuffer, gribsize, data, gridsize, unreduced, nmiss, zip, missval, vlistID, varID);
status = gribapiDecode(gribbuffer, gribsize, data, gridsize, unreduced, nmiss, missval, vlistID, varID);
}
return (status);
}
int grbUnzipRecord(unsigned char *gribbuffer, size_t *gribsize)
{
int zip = 0;
int izip;
size_t igribsize;
size_t ogribsize;
long unzipsize;
igribsize = *gribsize;
ogribsize = *gribsize;
if ( (izip = gribGetZip(igribsize, gribbuffer, &unzipsize)) > 0 )
{
zip = izip;
if ( izip == 128 ) /* szip */
{
unsigned char *itmpbuffer = NULL;
size_t itmpbuffersize = 0;
if ( unzipsize < (long) igribsize )
{
fprintf(stderr, "Decompressed size smaller than compressed size (in %ld; out %ld)!\n", (long)igribsize, unzipsize);
return (0);
}
if ( itmpbuffersize < igribsize )
{
itmpbuffersize = igribsize;
itmpbuffer = (unsigned char *) realloc(itmpbuffer, itmpbuffersize);
}
memcpy(itmpbuffer, gribbuffer, itmpbuffersize);
unzipsize += 100; /* need 0 to 1 bytes for rounding of bds */
ogribsize = gribUnzip(gribbuffer, unzipsize, itmpbuffer, igribsize);
free(itmpbuffer);
if ( ogribsize <= 0 ) Error("Decompression problem!");
}
else
{
Error("Decompression for %d not implemented!", izip);
}
}
*gribsize = ogribsize;
return zip;
}
void grbReadRecord(stream_t * streamptr, double *data, int *nmiss)
{
int filetype = streamptr->filetype;
......@@ -240,10 +294,9 @@ void grbReadRecord(stream_t * streamptr, double *data, int *nmiss)
double missval = vlistInqVarMissval(vlistID, varID);
int zip;
grbDecode(filetype, gribbuffer, (int)recsize, data, gridsize, streamptr->unreduced, nmiss, &zip, missval, vlistID, varID);
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
streamptr->tsteps[tsID].records[recID].zip = zip;
grbDecode(filetype, gribbuffer, (int)recsize, data, gridsize, streamptr->unreduced, nmiss, missval, vlistID, varID);
}
static
......@@ -399,13 +452,14 @@ void grbReadVarDP(stream_t * streamptr, int varID, double *data, int *nmiss)
double missval = vlistInqVarMissval(vlistID, varID);
int imiss, zip;
int imiss;
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
grbDecode(filetype, gribbuffer, (int)recsize, &data[levelID*gridsize], gridsize,
streamptr->unreduced, &imiss, &zip, missval, vlistID, varID);
streamptr->unreduced, &imiss, missval, vlistID, varID);
*nmiss += imiss;
streamptr->tsteps[tsID].records[recID].zip = zip;
}
fileSetPos(fileID, currentfilepos, SEEK_SET);
......@@ -444,12 +498,11 @@ void grbReadVarSliceDP(stream_t * streamptr, int varID, int levelID, double *dat
double missval = vlistInqVarMissval(vlistID, varID);
int zip;
grbDecode(filetype, gribbuffer, (int)recsize, data, gridsize, streamptr->unreduced, nmiss, &zip, missval, vlistID, varID);
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
fileSetPos(fileID, currentfilepos, SEEK_SET);
grbDecode(filetype, gribbuffer, (int)recsize, data, gridsize, streamptr->unreduced, nmiss, missval, vlistID, varID);
streamptr->tsteps[tsID].records[recID].zip = zip;
fileSetPos(fileID, currentfilepos, SEEK_SET);
}
static
......
......@@ -2012,7 +2012,7 @@ int gribapiScanTimestep(stream_t * streamptr)
#endif
int gribapiDecode(unsigned char *gribbuffer, int gribsize, double *data, int gridsize,
int unreduced, int *nmiss, int *zip, double missval, int vlistID, int varID)
int unreduced, int *nmiss, double missval, int vlistID, int varID)
{
int status = 0;
#if defined (HAVE_LIBGRIB_API)
......@@ -2021,7 +2021,6 @@ int gribapiDecode(unsigned char *gribbuffer, int gribsize, double *data, int gri
size_t datasize, dummy, recsize;
grib_handle *gh = NULL;
UNUSED(zip);
UNUSED(vlistID);
UNUSED(varID);
......
......@@ -6,7 +6,7 @@ int gribapiScanTimestep2(stream_t * streamptr);
int gribapiScanTimestep(stream_t * streamptr);
int gribapiDecode(unsigned char *gribbuffer, int gribsize, double *data, int gridsize,
int unreduced, int *nmiss, int *zip, double missval, int vlistID, int varID);
int unreduced, int *nmiss, double missval, int vlistID, int varID);
size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisID,
int vdate, int vtime, int tsteptype, int numavg,
......
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