Commit c4cca286 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

grib2 cleanup

parent fea2ee64
......@@ -36,7 +36,7 @@ const char *gribapiLibraryVersion(void)
}
void gribapiDefHandles(int streamID)
void gribContainersNew(int streamID)
{
stream_t *streamptr;
......@@ -50,12 +50,38 @@ void gribapiDefHandles(int streamID)
#endif
{
int i, editionNumber = 2;
gribContainer_t *gribContainers;
if ( streamptr->filetype == FILETYPE_GRB ) editionNumber = 1;
streamptr->gribHandle = (void **) malloc(streamptr->nvars*sizeof(void *));
gribContainers = (gribContainer_t *) malloc(streamptr->nvars*sizeof(gribContainer_t));
streamptr->gribContainers = (void *) gribContainers;
for ( i = 0; i < streamptr->nvars; ++i )
streamptr->gribHandle[i] = gribHandleNew(editionNumber);
{
gribContainers[i].gribHandle = gribHandleNew(editionNumber);
gribContainers[i].init = FALSE;
}
}
}
void gribContainersDelete(int streamID)
{
stream_t *streamptr;
streamptr = stream_to_pointer(streamID);
if ( streamptr->gribContainers )
{
int i;
gribContainer_t *gribContainers = (gribContainer_t *) streamptr->gribContainers;
for ( i = 0; i < streamptr->nvars; ++i )
{
gribHandleDelete(gribContainers[i].gribHandle);
}
free(streamptr->gribContainers);
streamptr->gribContainers = NULL;
}
}
......@@ -30,8 +30,15 @@
#define GRIB2_GTYPE_NUMBER 101 /* General Unstructured Grid */
const char *gribapiLibraryVersion(void);
void gribapiDefHandles(int streamID);
void gribContainersNew(int streamID);
void gribContainersDelete(int streamID);
void *gribHandleNew(int editionNumber);
void gribHandleDelete(void *gh);
typedef struct {
int init;
void *gribHandle;
}
gribContainer_t;
#endif /* _GRIBAPI_H */
......@@ -1107,17 +1107,7 @@ void streamClose(int streamID)
case FILETYPE_GRB2:
{
gribClose(fileID);
if ( streamptr->gribHandle )
{
int i;
for ( i = 0; i < streamptr->nvars; ++i )
{
gribHandleDelete(streamptr->gribHandle[i]);
streamptr->gribHandle[i] = NULL;
}
free(streamptr->gribHandle);
streamptr->gribHandle = NULL;
}
gribContainersDelete(streamID);
break;
}
#endif
......@@ -1999,7 +1989,7 @@ void streamDefVlist(int streamID, int vlistID)
else if ( streamptr->filetype == FILETYPE_GRB ||
streamptr->filetype == FILETYPE_GRB2 )
{
gribapiDefHandles(streamID);
gribContainersNew(streamID);
}
}
}
......
......@@ -438,7 +438,7 @@ static
size_t grbEncode(int filetype, int varID, int levelID, int vlistID, int gridID, int zaxisID,
int date, int time, int tsteptype, int numavg,
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize,
int ljpeg, void *gribHandle)
int ljpeg, void *gribContainer)
{
size_t nbytes;
......@@ -455,7 +455,7 @@ size_t grbEncode(int filetype, int varID, int levelID, int vlistID, int gridID,
nbytes = gribapiEncode(varID, levelID, vlistID, gridID, zaxisID,
date, time, tsteptype, numavg,
datasize, data, nmiss, gribbuffer, gribbuffersize,
ljpeg, gribHandle);
ljpeg, gribContainer);
}
return (nbytes);
......@@ -510,7 +510,7 @@ int grbWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
stream_t *streamptr;
int ljpeg = 0;
int ljpeg_warn = 1;
void *gh = NULL;
void *gc = NULL;
streamptr = stream_to_pointer(streamID);
......@@ -545,7 +545,8 @@ int grbWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
else
#endif
{
gh = streamptr->gribHandle[varID];
gribContainer_t *gribContainers = (gribContainer_t *) streamptr->gribContainers;
gc = (void *) &gribContainers[varID];
}
if ( streamptr->ztype == COMPRESS_JPEG )
......@@ -556,13 +557,13 @@ int grbWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
}
else
{
if ( ljpeg_warn ) Warning("Jpeg compression of GRIB1 records not available!");
if ( ljpeg_warn ) Warning("JPEG compression of GRIB1 records not available!");
ljpeg_warn = 0;
}
}
nbytes = grbEncode(filetype, varID, levelID, vlistID, gridID, zaxisID, date, time, tsteptype, numavg,
datasize, data, nmiss, gribbuffer, gribbuffersize, ljpeg, gh);
datasize, data, nmiss, gribbuffer, gribbuffersize, ljpeg, gc);
if ( streamptr->ztype == COMPRESS_SZIP )
nbytes = grbSzip(filetype, gribbuffer, nbytes);
......
......@@ -1858,7 +1858,7 @@ void gribapiDefParam(grib_handle *gh, int param, const char *name)
len = strlen(name);
status = grib_set_string(gh, "shortName", name, &len);
if ( status != 0 )
printf("status=%d; no match for shortName=%s\n", status, name);
Warning("grib_api: No match for shortName=%s\n", name);
}
else
{
......@@ -2021,8 +2021,6 @@ void gribapiDefGrid(grib_handle *gh, int gridID, int ljpeg)
static short lwarn = TRUE;
size_t len;
char *mesg;
// ISEC1_Sec2Or3Flag = 128;
gridtype = gridInqType(gridID);
......@@ -2031,8 +2029,6 @@ void gribapiDefGrid(grib_handle *gh, int gridID, int ljpeg)
if ( gridtype == GRID_GME || gridtype == GRID_REFERENCE )
gridtype = -1;
// ISEC1_GridDefinition = 255;
if ( gridtype == GRID_GENERIC )
{
int xsize, ysize, gridsize;
......@@ -2071,10 +2067,6 @@ void gribapiDefGrid(grib_handle *gh, int gridID, int ljpeg)
gridtype = GRID_LONLAT;
}
// ISEC2_Reduced = FALSE;
// ISEC2_ScanFlag = 0;
switch (gridtype)
{
case GRID_LONLAT:
......@@ -2263,8 +2255,8 @@ void gribapiDefGrid(grib_handle *gh, int gridID, int ljpeg)
GRIB_CHECK(grib_set_long(gh, "K", trunc), 0);
GRIB_CHECK(grib_set_long(gh, "M", trunc), 0);
GRIB_CHECK(grib_set_long(gh, "numberOfDataPoints", gridInqSize(gridID)), 0);
GRIB_CHECK(grib_set_long(gh, "numberOfValues", gridInqSize(gridID)), 0);
// GRIB_CHECK(grib_set_long(gh, "numberOfDataPoints", gridInqSize(gridID)), 0);
// GRIB_CHECK(grib_set_long(gh, "numberOfValues", gridInqSize(gridID)), 0);
if ( gridInqComplexPacking(gridID) )
{
......@@ -2581,7 +2573,7 @@ void gribHandleDelete(void *gh)
size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisID,
int vdate, int vtime, int tsteptype, int numavg,
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize,
int ljpeg, void *gribHandle)
int ljpeg, void *gribContainer)
{
size_t nbytes = 0;
#if defined (HAVE_LIBGRIB_API)
......@@ -2592,6 +2584,7 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
long bitsPerValue;
char name[256];
grib_handle *gh = NULL;
gribContainer_t *gc = (gribContainer_t *) gribContainer;
// extern unsigned char _grib_template_GRIB2[];
param = vlistInqVarParam(vlistID, varID);
......@@ -2601,20 +2594,20 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
#if defined(GRIBAPIENCODETEST)
gh = (grib_handle *) gribHandleNew();
#else
gh = gribHandle;
gh = gc->gribHandle;
#endif
gribapiDefInstitut(gh, vlistID, varID);
gribapiDefModel(gh, vlistID, varID);
if ( ! gc->init ) gribapiDefInstitut(gh, vlistID, varID);
if ( ! gc->init ) gribapiDefModel(gh, vlistID, varID);
gribapiDefParam(gh, param, name);
if ( ! gc->init ) gribapiDefParam(gh, param, name);
gribapiDefTime(gh, vdate, vtime, tsteptype, numavg, vlistInqTaxis(vlistID));
/* bitsPerValue have to be defined befor call to DefGrid (complex packing) */
bitsPerValue = grbBitsPerValue(datatype);
GRIB_CHECK(grib_set_long(gh, "bitsPerValue", bitsPerValue), 0);
gribapiDefGrid(gh, gridID, ljpeg);
if ( ! gc->init ) gribapiDefGrid(gh, gridID, ljpeg);
gribapiDefLevel(gh, param, zaxisID, levelID);
if ( nmiss > 0 )
......@@ -2639,6 +2632,8 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
gribHandleDelete(gh);
#endif
gc->init = TRUE;
nbytes = recsize;
#else
Error("GRIB_API support not compiled in!");
......
......@@ -11,6 +11,6 @@ int gribapiDecode(unsigned char *gribbuffer, int gribsize, double *data, int gri
size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisID,
int vdate, int vtime, int tsteptype, int numavg,
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize,
int ljpeg, void *gribHandle);
int ljpeg, void *gribContainer);
#endif /* _STREAM_GRIBAPI_H */
......@@ -425,7 +425,7 @@ void stream_init_entry(stream_t *streamptr)
streamptr->nfiles = 0;
streamptr->fnames = NULL;
streamptr->gribHandle = NULL;
streamptr->gribContainers = NULL;
}
......
......@@ -241,7 +241,7 @@ typedef struct {
int curfile;
int nfiles;
char **fnames;
void **gribHandle;
void *gribContainers;
}
stream_t;
......
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