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

move grb_read functions to grb_read.c

parent f7bb44a0
......@@ -253,6 +253,7 @@ src/file.h -text
src/gaussgrid.c -text
src/gaussgrid.h -text
src/getline.c -text
src/grb_read.c -text
src/grb_write.c -text
src/gribapi.c -text
src/gribapi.h -text
......
......@@ -93,6 +93,7 @@ libcdi_la_SOURCES = \
stream_srv.h \
stream_var.c \
grb_write.c \
grb_read.c \
cdf_write.c \
cdf_read.c \
subtype.c \
......
......@@ -183,14 +183,14 @@ am__libcdi_la_SOURCES_DIST = basetime.c basetime.h binary.c binary.h \
stream_gribapi.c stream_gribapi.h stream_history.c \
stream_ieg.c stream_ieg.h stream_fcommon.c stream_fcommon.h \
cdi_int.c cdi_int.h stream_record.c stream_srv.c stream_srv.h \
stream_var.c grb_write.c cdf_write.c cdf_read.c subtype.c \
subtype.h swap.h table.c table.h tablepar.h taxis.c taxis.h \
timebase.c timebase.h tsteps.c util.c varscan.c varscan.h \
version.c vlist.c vlist.h vlist_att.c vlist_att.h vlist_var.c \
vlist_var.h zaxis.c zaxis.h stream.c stream_write.c \
stream_read.c swap.c iterator.c iterator.h iterator_fallback.c \
iterator_fallback.h iterator_grib.c iterator_grib.h cfortran.h \
cdiFortran.c gribapi_utilities.c
stream_var.c grb_write.c grb_read.c cdf_write.c cdf_read.c \
subtype.c subtype.h swap.h table.c table.h tablepar.h taxis.c \
taxis.h timebase.c timebase.h tsteps.c util.c varscan.c \
varscan.h version.c vlist.c vlist.h vlist_att.c vlist_att.h \
vlist_var.c vlist_var.h zaxis.c zaxis.h stream.c \
stream_write.c stream_read.c swap.c iterator.c iterator.h \
iterator_fallback.c iterator_fallback.h iterator_grib.c \
iterator_grib.h cfortran.h cdiFortran.c gribapi_utilities.c
am__objects_1 = cdiFortran.lo
@USE_FC_TRUE@am__objects_2 = $(am__objects_1)
am__objects_3 = gribapi_utilities.lo
......@@ -204,12 +204,12 @@ am_libcdi_la_OBJECTS = basetime.lo binary.lo calendar.lo cdf.lo \
stream_cdf.lo stream_cgribex.lo stream_ext.lo stream_grb.lo \
stream_gribapi.lo stream_history.lo stream_ieg.lo \
stream_fcommon.lo cdi_int.lo stream_record.lo stream_srv.lo \
stream_var.lo grb_write.lo cdf_write.lo cdf_read.lo subtype.lo \
table.lo taxis.lo timebase.lo tsteps.lo util.lo varscan.lo \
version.lo vlist.lo vlist_att.lo vlist_var.lo zaxis.lo \
stream.lo stream_write.lo stream_read.lo swap.lo iterator.lo \
iterator_fallback.lo iterator_grib.lo $(am__objects_2) \
$(am__objects_4)
stream_var.lo grb_write.lo grb_read.lo cdf_write.lo \
cdf_read.lo subtype.lo table.lo taxis.lo timebase.lo tsteps.lo \
util.lo varscan.lo version.lo vlist.lo vlist_att.lo \
vlist_var.lo zaxis.lo stream.lo stream_write.lo stream_read.lo \
swap.lo iterator.lo iterator_fallback.lo iterator_grib.lo \
$(am__objects_2) $(am__objects_4)
libcdi_la_OBJECTS = $(am_libcdi_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
......@@ -530,10 +530,10 @@ libcdi_la_SOURCES = basetime.c basetime.h binary.c binary.h calendar.c \
stream_gribapi.h stream_history.c stream_ieg.c stream_ieg.h \
stream_fcommon.c stream_fcommon.h cdi_int.c cdi_int.h \
stream_record.c stream_srv.c stream_srv.h stream_var.c \
grb_write.c cdf_write.c cdf_read.c subtype.c subtype.h swap.h \
table.c table.h tablepar.h taxis.c taxis.h timebase.c \
timebase.h tsteps.c util.c varscan.c varscan.h version.c \
vlist.c vlist.h vlist_att.c vlist_att.h vlist_var.c \
grb_write.c grb_read.c cdf_write.c cdf_read.c subtype.c \
subtype.h swap.h table.c table.h tablepar.h taxis.c taxis.h \
timebase.c timebase.h tsteps.c util.c varscan.c varscan.h \
version.c vlist.c vlist.h vlist_att.c vlist_att.h vlist_var.c \
vlist_var.h zaxis.c zaxis.h stream.c stream_write.c \
stream_read.c swap.c iterator.c iterator.h iterator_fallback.c \
iterator_fallback.h iterator_grib.c iterator_grib.h \
......@@ -711,6 +711,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/extralib.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/file.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gaussgrid.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/grb_read.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/grb_write.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gribapi.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gribapi_utilities.Plo@am__quote@
......
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_LIBGRIB
#include "dmemory.h"
#include "cdi.h"
#include "cdi_int.h"
#include "stream_cgribex.h"
#include "stream_grb.h"
#include "stream_gribapi.h"
#include "file.h"
#include "cgribex.h" /* gribZip gribGetZip gribGinfo */
#include "gribapi.h"
#include "namespace.h"
static
int grbDecode(int filetype, unsigned char *gribbuffer, int gribsize, double *data, int gridsize,
int unreduced, int *nmiss, double missval, int vlistID, int varID)
{
int status = 0;
#if defined (HAVE_LIBCGRIBEX)
if ( filetype == FILETYPE_GRB )
{
#if defined (HAVE_LIBGRIB_API)
extern int cdiNAdditionalGRIBKeys;
if ( cdiNAdditionalGRIBKeys > 0 )
Error("CGRIBEX decode does not support reading of additional GRIB keys!");
#endif
status = cgribexDecode(gribbuffer, gribsize, data, gridsize, unreduced, nmiss, missval);
}
else
#endif
#ifdef HAVE_LIBGRIB_API
status = gribapiDecode(gribbuffer, gribsize, data, gridsize, unreduced, nmiss, missval, vlistID, varID);
#else
{
(void)vlistID; (void)varID;
Error("GRIB_API support not compiled in!");
}
#endif
return (status);
}
static
int grbUnzipRecord(unsigned char *gribbuffer, size_t *gribsize)
{
int zip = 0;
int izip;
long unzipsize;
size_t igribsize = *gribsize;
size_t ogribsize = *gribsize;
if ( (izip = gribGetZip((long)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 = (size_t)gribUnzip(gribbuffer, unzipsize, itmpbuffer, (long)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;
unsigned char *gribbuffer = (unsigned char *) streamptr->record->buffer;
int vlistID = streamptr->vlistID;
int fileID = streamptr->fileID;
int tsID = streamptr->curTsID;
int vrecID = streamptr->tsteps[tsID].curRecID;
int recID = streamptr->tsteps[tsID].recIDs[vrecID];
off_t recpos = streamptr->tsteps[tsID].records[recID].position;
size_t recsize = streamptr->tsteps[tsID].records[recID].size;
int varID = streamptr->tsteps[tsID].records[recID].varID;
int gridID = vlistInqVarGrid(vlistID, varID);
int gridsize = gridInqSize(gridID);
streamptr->numvals += gridsize;
fileSetPos(fileID, recpos, SEEK_SET);
if (fileRead(fileID, gribbuffer, recsize) != recsize)
Error("Failed to read GRIB record");
double missval = vlistInqVarMissval(vlistID, varID);
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
grbDecode(filetype, gribbuffer, (int)recsize, data, gridsize, streamptr->unreduced, nmiss, missval, vlistID, varID);
}
void grbReadVarDP(stream_t * streamptr, int varID, double *data, int *nmiss)
{
int filetype = streamptr->filetype;
unsigned char *gribbuffer = (unsigned char *) streamptr->record->buffer;
int vlistID = streamptr->vlistID;
int fileID = streamptr->fileID;
int tsID = streamptr->curTsID;
int gridID = vlistInqVarGrid(vlistID, varID);
int gridsize = gridInqSize(gridID);
off_t currentfilepos = fileGetPos(fileID);
int isub = subtypeInqActiveIndex(streamptr->vars[varID].subtypeID);
int nlevs = streamptr->vars[varID].recordTable[0].nlevs;
if ( CDI_Debug )
Message("nlevs = %d gridID = %d gridsize = %d", nlevs, gridID, gridsize);
*nmiss = 0;
for (int levelID = 0; levelID < nlevs; levelID++ )
{
int recID = streamptr->vars[varID].recordTable[isub].recordID[levelID];
off_t recpos = streamptr->tsteps[tsID].records[recID].position;
size_t recsize = streamptr->tsteps[tsID].records[recID].size;
fileSetPos(fileID, recpos, SEEK_SET);
fileRead(fileID, gribbuffer, recsize);
double missval = vlistInqVarMissval(vlistID, varID);
int imiss;
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
grbDecode(filetype, gribbuffer, (int)recsize, &data[levelID*gridsize], gridsize,
streamptr->unreduced, &imiss, missval, vlistID, varID);
*nmiss += imiss;
}
fileSetPos(fileID, currentfilepos, SEEK_SET);
}
void grbReadVarSliceDP(stream_t * streamptr, int varID, int levelID, double *data, int *nmiss)
{
int filetype = streamptr->filetype;
unsigned char *gribbuffer = (unsigned char *) streamptr->record->buffer;
int vlistID = streamptr->vlistID;
int gridID = vlistInqVarGrid(vlistID, varID);
int gridsize = gridInqSize(gridID);
int tsID = streamptr->curTsID;
if ( CDI_Debug )
Message("gridID = %d gridsize = %d", gridID, gridsize);
int fileID = streamptr->fileID;
off_t currentfilepos = fileGetPos(fileID);
int isub = subtypeInqActiveIndex(streamptr->vars[varID].subtypeID);
int recID = streamptr->vars[varID].recordTable[isub].recordID[levelID];
off_t recpos = streamptr->tsteps[tsID].records[recID].position;
size_t recsize = streamptr->tsteps[tsID].records[recID].size;
if ( recsize == 0 )
Error("Internal problem! Recordsize is zero for record %d at timestep %d",
recID+1, tsID+1);
fileSetPos(fileID, recpos, SEEK_SET);
fileRead(fileID, gribbuffer, recsize);
double missval = vlistInqVarMissval(vlistID, varID);
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
grbDecode(filetype, gribbuffer, (int)recsize, data, gridsize, streamptr->unreduced, nmiss, missval, vlistID, varID);
fileSetPos(fileID, currentfilepos, SEEK_SET);
}
#endif
......@@ -134,6 +134,8 @@ files="basetime.c \
stream_record.c \
stream_srv.c \
stream_var.c \
grb_write.c \
grb_read.c \
cdf_write.c \
cdf_read.c \
subtype.c \
......
......@@ -114,6 +114,8 @@ files="basetime.c \
stream_history.c \
stream_record.c \
stream_var.c \
grb_write.c \
grb_read.c \
cdf_write.c \
cdf_read.c \
subtype.c \
......
......@@ -189,122 +189,6 @@ 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, double missval, int vlistID, int varID)
{
int status = 0;
#if defined (HAVE_LIBCGRIBEX)
if ( filetype == FILETYPE_GRB )
{
#if defined (HAVE_LIBGRIB_API)
extern int cdiNAdditionalGRIBKeys;
if ( cdiNAdditionalGRIBKeys > 0 )
Error("CGRIBEX decode does not support reading of additional GRIB keys!");
#endif
status = cgribexDecode(gribbuffer, gribsize, data, gridsize, unreduced, nmiss, missval);
}
else
#endif
#ifdef HAVE_LIBGRIB_API
status = gribapiDecode(gribbuffer, gribsize, data, gridsize, unreduced, nmiss, missval, vlistID, varID);
#else
{
(void)vlistID; (void)varID;
Error("GRIB_API support not compiled in!");
}
#endif
return (status);
}
static 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((long)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 = (size_t)gribUnzip(gribbuffer, unzipsize, itmpbuffer, (long)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;
unsigned char *gribbuffer = (unsigned char *) streamptr->record->buffer;
int vlistID = streamptr->vlistID;
int fileID = streamptr->fileID;
int tsID = streamptr->curTsID;
int vrecID = streamptr->tsteps[tsID].curRecID;
int recID = streamptr->tsteps[tsID].recIDs[vrecID];
off_t recpos = streamptr->tsteps[tsID].records[recID].position;
size_t recsize = streamptr->tsteps[tsID].records[recID].size;
int varID = streamptr->tsteps[tsID].records[recID].varID;
int gridID = vlistInqVarGrid(vlistID, varID);
int gridsize = gridInqSize(gridID);
streamptr->numvals += gridsize;
fileSetPos(fileID, recpos, SEEK_SET);
if (fileRead(fileID, gribbuffer, recsize) != recsize)
Error("Failed to read GRIB record");
double missval = vlistInqVarMissval(vlistID, varID);
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
grbDecode(filetype, gribbuffer, (int)recsize, data, gridsize, streamptr->unreduced, nmiss, missval, vlistID, varID);
}
static
int grbScanTimestep1(stream_t * streamptr)
{
......@@ -429,96 +313,6 @@ int grbInqTimestep(stream_t * streamptr, int tsID)
}
void grbReadVarDP(stream_t * streamptr, int varID, double *data, int *nmiss)
{
int filetype = streamptr->filetype;
unsigned char *gribbuffer = (unsigned char *) streamptr->record->buffer;
int vlistID = streamptr->vlistID;
int fileID = streamptr->fileID;
int tsID = streamptr->curTsID;
int gridID = vlistInqVarGrid(vlistID, varID);
int gridsize = gridInqSize(gridID);
off_t currentfilepos = fileGetPos(fileID);
int isub = subtypeInqActiveIndex(streamptr->vars[varID].subtypeID);
int nlevs = streamptr->vars[varID].recordTable[0].nlevs;
if ( CDI_Debug )
Message("nlevs = %d gridID = %d gridsize = %d", nlevs, gridID, gridsize);
*nmiss = 0;
for (int levelID = 0; levelID < nlevs; levelID++ )
{
int recID = streamptr->vars[varID].recordTable[isub].recordID[levelID];
off_t recpos = streamptr->tsteps[tsID].records[recID].position;
size_t recsize = streamptr->tsteps[tsID].records[recID].size;
fileSetPos(fileID, recpos, SEEK_SET);
fileRead(fileID, gribbuffer, recsize);
double missval = vlistInqVarMissval(vlistID, varID);
int imiss;
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
grbDecode(filetype, gribbuffer, (int)recsize, &data[levelID*gridsize], gridsize,
streamptr->unreduced, &imiss, missval, vlistID, varID);
*nmiss += imiss;
}
fileSetPos(fileID, currentfilepos, SEEK_SET);
}
void grbReadVarSliceDP(stream_t * streamptr, int varID, int levelID, double *data, int *nmiss)
{
int filetype = streamptr->filetype;
unsigned char *gribbuffer = (unsigned char *) streamptr->record->buffer;
int vlistID = streamptr->vlistID;
int gridID = vlistInqVarGrid(vlistID, varID);
int gridsize = gridInqSize(gridID);
int tsID = streamptr->curTsID;
if ( CDI_Debug )
Message("gridID = %d gridsize = %d", gridID, gridsize);
int fileID = streamptr->fileID;
off_t currentfilepos = fileGetPos(fileID);
int isub = subtypeInqActiveIndex(streamptr->vars[varID].subtypeID);
int recID = streamptr->vars[varID].recordTable[isub].recordID[levelID];
off_t recpos = streamptr->tsteps[tsID].records[recID].position;
size_t recsize = streamptr->tsteps[tsID].records[recID].size;
if ( recsize == 0 )
Error("Internal problem! Recordsize is zero for record %d at timestep %d",
recID+1, tsID+1);
fileSetPos(fileID, recpos, SEEK_SET);
fileRead(fileID, gribbuffer, recsize);
double missval = vlistInqVarMissval(vlistID, varID);
streamptr->tsteps[tsID].records[recID].zip = grbUnzipRecord(gribbuffer, &recsize);
grbDecode(filetype, gribbuffer, (int)recsize, data, gridsize, streamptr->unreduced, nmiss, missval, vlistID, varID);
fileSetPos(fileID, currentfilepos, SEEK_SET);
}
void streamInqGRIBinfo(int streamID, int *intnum, float *fltnum, off_t *bignum)
{
stream_t *streamptr = stream_to_pointer(streamID);
......
......@@ -69,7 +69,7 @@ void cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data,
case FILETYPE_NC4C:
{
if ( streamptr->accessmode == 0 ) cdfEndDef(streamptr);
cdf_write_var(streamptr, varID, memtype, (double *)data, nmiss);
cdf_write_var(streamptr, varID, memtype, data, nmiss);
break;
}
#endif
......@@ -103,7 +103,7 @@ void streamWriteVar(int streamID, int varID, const double *data, int nmiss)
const void *data, int nmiss)
= (void (*)(int, int, int, const void *, int))
namespaceSwitchGet(NSSWITCH_STREAM_WRITE_VAR_).func;
myCdiStreamWriteVar_(streamID, varID, MEMTYPE_DOUBLE, data, nmiss);
myCdiStreamWriteVar_(streamID, varID, MEMTYPE_DOUBLE, (const void *) data, nmiss);
}
/*
......@@ -129,7 +129,7 @@ void streamWriteVarF(int streamID, int varID, const float *data, int nmiss)
const void *data, int nmiss)
= (void (*)(int, int, int, const void *, int))
namespaceSwitchGet(NSSWITCH_STREAM_WRITE_VAR_).func;
myCdiStreamWriteVar_(streamID, varID, MEMTYPE_FLOAT, data, nmiss);
myCdiStreamWriteVar_(streamID, varID, MEMTYPE_FLOAT, (const void *) data, nmiss);
}
static
......@@ -218,7 +218,7 @@ The values are converted to the external data type of the variable, if necessary
*/
void streamWriteVarSlice(int streamID, int varID, int levelID, const double *data, int nmiss)
{
cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_DOUBLE, data, nmiss);
cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_DOUBLE, (const void *) data, nmiss);
}
/*
......@@ -241,7 +241,7 @@ Only support for netCDF was implemented in this function.
*/
void streamWriteVarSliceF(int streamID, int varID, int levelID, const float *data, int nmiss)
{
cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_FLOAT, data, nmiss);
cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_FLOAT, (const void *) data, nmiss);
}
......
Supports Markdown
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