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

move grb_write functions to grb_write.c

parent 1180a65f
......@@ -253,6 +253,7 @@ src/file.h -text
src/gaussgrid.c -text
src/gaussgrid.h -text
src/getline.c -text
src/grb_write.c -text
src/gribapi.c -text
src/gribapi.h -text
src/gribapi_utilities.c -text
......
......@@ -92,6 +92,7 @@ libcdi_la_SOURCES = \
stream_srv.c \
stream_srv.h \
stream_var.c \
grb_write.c \
cdf_write.c \
cdf_read.c \
subtype.c \
......
......@@ -183,10 +183,10 @@ 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 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 \
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 \
......@@ -204,10 +204,10 @@ 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 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 \
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)
libcdi_la_OBJECTS = $(am_libcdi_la_OBJECTS)
......@@ -530,14 +530,14 @@ 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 \
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 $(am__append_2) \
$(am__append_3)
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 \
$(am__append_2) $(am__append_3)
# cfortran.h is an optional part of libcdi
libcdi_la_USE_FC_extra_sources = \
......@@ -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_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@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/grid.Plo@am__quote@
......
#if defined (HAVE_CONFIG_H)
# include "config.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_LIBNETCDF
......@@ -1262,8 +1262,6 @@ void cdf_write_record(stream_t *streamptr, int memtype, const void *data, int nm
int varID = streamptr->record->varID;
int levelID = streamptr->record->levelID;
if ( CDI_Debug ) Message("streamID = %d varID = %d", streamptr->self, varID);
cdf_write_var_slice(streamptr, varID, levelID, memtype, data, nmiss);
}
......
#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
size_t grbEncode(int filetype, int memtype, int varID, int levelID, int vlistID, int gridID, int zaxisID,
int date, int time, int tsteptype, int numavg,
size_t datasize, const double *data, int nmiss, unsigned char **gribbuffer,
int comptype, void *gribContainer)
{
size_t nbytes = 0;
#ifdef HAVE_LIBCGRIBEX
if ( filetype == FILETYPE_GRB )
{
size_t gribbuffersize = datasize*4+3000;
*gribbuffer = (unsigned char *) Malloc(gribbuffersize);
nbytes = cgribexEncode(memtype, varID, levelID, vlistID, gridID, zaxisID,
date, time, tsteptype, numavg,
(long)datasize, data, nmiss, *gribbuffer, gribbuffersize);
}
else
#endif
#ifdef HAVE_LIBGRIB_API
{
if ( memtype == MEMTYPE_FLOAT ) Error("gribapiEncode() not implemented for memtype float!");
size_t gribbuffersize;
nbytes = gribapiEncode(varID, levelID, vlistID, gridID, zaxisID,
date, time, tsteptype, numavg,
(long)datasize, data, nmiss, gribbuffer, &gribbuffersize,
comptype, gribContainer);
}
#else
Error("GRIB_API support not compiled in!");
(void)gribContainer;
(void)comptype;
#endif
return nbytes;
}
static
size_t grbSzip(int filetype, unsigned char *gribbuffer, size_t gribbuffersize)
{
size_t nbytes = 0;
size_t buffersize = gribbuffersize + 1000; /* compressed record can be greater than source record */
unsigned char *buffer = (unsigned char *) Malloc(buffersize);
/* memcpy(buffer, gribbuffer, gribbuffersize); */
if ( filetype == FILETYPE_GRB )
{
nbytes = (size_t)gribZip(gribbuffer, (long) gribbuffersize, buffer, (long) buffersize);
}
else
{
static int lszip_warn = 1;
if ( lszip_warn ) Warning("Szip compression of GRIB2 records not implemented!");
lszip_warn = 0;
nbytes = gribbuffersize;
}
Free(buffer);
return nbytes;
}
void grbCopyRecord(stream_t * streamptr2, stream_t * streamptr1)
{
int filetype = streamptr1->filetype;
int fileID1 = streamptr1->fileID;
int fileID2 = streamptr2->fileID;
int tsID = streamptr1->curTsID;
int vrecID = streamptr1->tsteps[tsID].curRecID;
int recID = streamptr1->tsteps[tsID].recIDs[vrecID];
off_t recpos = streamptr1->tsteps[tsID].records[recID].position;
size_t recsize = streamptr1->tsteps[tsID].records[recID].size;
fileSetPos(fileID1, recpos, SEEK_SET);
/* round up recsize to next multiple of 8 */
size_t gribbuffersize = ((recsize + 7U) & ~7U);
unsigned char *gribbuffer = (unsigned char *) Malloc(gribbuffersize);
if (fileRead(fileID1, gribbuffer, recsize) != recsize)
Error("Could not read GRIB record for copying!");
size_t nbytes = recsize;
if ( filetype == FILETYPE_GRB )
{
long unzipsize;
int izip = gribGetZip((long)recsize, gribbuffer, &unzipsize);
if ( izip == 0 && streamptr2->comptype == COMPRESS_SZIP )
nbytes = grbSzip(filetype, gribbuffer, nbytes);
}
while ( nbytes & 7 ) gribbuffer[nbytes++] = 0;
size_t nwrite = fileWrite(fileID2, gribbuffer, nbytes);
if ( nwrite != nbytes )
{
perror(__func__);
Error("Could not write record for copying!");
}
Free(gribbuffer);
}
void grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, int nmiss)
{
unsigned char *gribbuffer = NULL;
void *gc = NULL;
int filetype = streamptr->filetype;
int fileID = streamptr->fileID;
int vlistID = streamptr->vlistID;
int gridID = vlistInqVarGrid(vlistID, varID);
int zaxisID = vlistInqVarZaxis(vlistID, varID);
int tsteptype = vlistInqVarTsteptype(vlistID, varID);
int comptype = streamptr->comptype;
int tsID = streamptr->curTsID;
int date = streamptr->tsteps[tsID].taxis.vdate;
int time = streamptr->tsteps[tsID].taxis.vtime;
int numavg = 0;
if ( vlistInqVarTimave(vlistID, varID) )
numavg = streamptr->tsteps[tsID].taxis.numavg;
if ( CDI_Debug )
Message("gridID = %d zaxisID = %d", gridID, zaxisID);
size_t datasize = (size_t)gridInqSize(gridID);
/*
gribbuffersize = datasize*4+3000;
gribbuffer = (unsigned char *) Malloc(gribbuffersize);
*/
#ifdef HAVE_LIBCGRIBEX
if ( filetype == FILETYPE_GRB )
{
}
else
#endif
{
#ifdef GRIBCONTAINER2D
gribContainer_t **gribContainers = (gribContainer_t **) streamptr->gribContainers;
gc = (void *) &gribContainers[varID][levelID];
#else
gribContainer_t *gribContainers = (gribContainer_t *) streamptr->gribContainers;
gc = (void *) &gribContainers[varID];
#endif
}
if ( comptype != COMPRESS_JPEG && comptype != COMPRESS_SZIP ) comptype = COMPRESS_NONE;
if ( filetype == FILETYPE_GRB && comptype == COMPRESS_JPEG )
{
static int ljpeg_warn = 1;
if ( ljpeg_warn ) Warning("JPEG compression of GRIB1 records not available!");
ljpeg_warn = 0;
}
size_t nbytes = grbEncode(filetype, memtype, varID, levelID, vlistID, gridID, zaxisID, date, time, tsteptype, numavg,
datasize, (const double*) data, nmiss, &gribbuffer, comptype, gc);
if ( filetype == FILETYPE_GRB && streamptr->comptype == COMPRESS_SZIP )
nbytes = grbSzip(filetype, gribbuffer, nbytes);
size_t (*myFileWrite)(int fileID, const void *restrict buffer,
size_t len, int tsID)
= (size_t (*)(int, const void *restrict, size_t, int))
namespaceSwitchGet(NSSWITCH_FILE_WRITE).func;
size_t nwrite = myFileWrite(fileID, gribbuffer, nbytes, tsID);
if ( nwrite != nbytes )
{
perror(__func__);
Error("Failed to write GRIB slice!");
}
if ( gribbuffer ) Free(gribbuffer);
}
void grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data, int nmiss)
{
int vlistID = streamptr->vlistID,
gridID = vlistInqVarGrid(vlistID, varID),
gridsize = gridInqSize(gridID),
zaxisID = vlistInqVarZaxis(vlistID, varID),
nlevs = zaxisInqSize(zaxisID);
double missval = vlistInqVarMissval(vlistID, varID);
size_t chunkLen = (size_t)gridsize;
if ( memtype == MEMTYPE_FLOAT )
for ( int levelID = 0; levelID < nlevs; levelID++ )
{
int nmiss_slice = 0;
const float *restrict fdata = ((const float *)data)+levelID*gridsize;
for ( size_t i = 0; i < chunkLen; ++i )
nmiss_slice += DBL_IS_EQUAL(fdata[i], missval);
grb_write_var_slice(streamptr, varID, levelID, memtype, fdata, nmiss_slice);
}
else
for ( int levelID = 0; levelID < nlevs; levelID++ )
{
int nmiss_slice = 0;
const double *restrict ddata = ((const double *)data)+levelID*gridsize;
for ( size_t i = 0; i < chunkLen; ++i )
nmiss_slice += DBL_IS_EQUAL(ddata[i], missval);
grb_write_var_slice(streamptr, varID, levelID, memtype, ddata, nmiss_slice);
}
}
void grb_write_record(stream_t * streamptr, int memtype, const void *data, int nmiss)
{
int varID = streamptr->record->varID;
int levelID = streamptr->record->levelID;
grb_write_var_slice(streamptr, varID, levelID, memtype, data, nmiss);
}
#endif
#if defined (HAVE_CONFIG_H)
# include "config.h"
#include "config.h"
#endif
#include <stdio.h>
......@@ -518,250 +518,6 @@ void grbReadVarSliceDP(stream_t * streamptr, int varID, int levelID, double *dat
fileSetPos(fileID, currentfilepos, SEEK_SET);
}
static
size_t grbEncode(int filetype, int memtype, int varID, int levelID, int vlistID, int gridID, int zaxisID,
int date, int time, int tsteptype, int numavg,
size_t datasize, const double *data, int nmiss, unsigned char **gribbuffer,
int comptype, void *gribContainer)
{
size_t nbytes = 0;
#if defined (HAVE_LIBCGRIBEX)
if ( filetype == FILETYPE_GRB )
{
size_t gribbuffersize = datasize*4+3000;
*gribbuffer = (unsigned char *) Malloc(gribbuffersize);
nbytes = cgribexEncode(memtype, varID, levelID, vlistID, gridID, zaxisID,
date, time, tsteptype, numavg,
(long)datasize, data, nmiss, *gribbuffer, gribbuffersize);
}
else
#endif
#ifdef HAVE_LIBGRIB_API
{
if ( memtype == MEMTYPE_FLOAT ) Error("gribapiEncode() not implemented for memtype float!");
size_t gribbuffersize;
nbytes = gribapiEncode(varID, levelID, vlistID, gridID, zaxisID,
date, time, tsteptype, numavg,
(long)datasize, data, nmiss, gribbuffer, &gribbuffersize,
comptype, gribContainer);
}
#else
Error("GRIB_API support not compiled in!");
(void)gribContainer;
(void)comptype;
#endif
return (nbytes);
}
static
size_t grbSzip(int filetype, unsigned char *gribbuffer, size_t gribbuffersize)
{
size_t nbytes = 0;
unsigned char *buffer;
size_t buffersize;
static int lszip_warn = 1;
buffersize = gribbuffersize + 1000; /* compressed record can be greater than source record */
buffer = (unsigned char *) Malloc(buffersize);
/* memcpy(buffer, gribbuffer, gribbuffersize); */
if ( filetype == FILETYPE_GRB )
{
nbytes = (size_t)gribZip(gribbuffer, (long) gribbuffersize, buffer, (long) buffersize);
}
else
{
if ( lszip_warn ) Warning("Szip compression of GRIB2 records not implemented!");
lszip_warn = 0;
nbytes = gribbuffersize;
}
Free(buffer);
return (nbytes);
}
void grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, int nmiss)
{
size_t nwrite;
int fileID;
int gridID;
int zaxisID;
unsigned char *gribbuffer = NULL;
int tsID;
int vlistID;
int date, time;
int tsteptype;
int numavg = 0;
size_t nbytes;
int filetype;
void *gc = NULL;
filetype = streamptr->filetype;
fileID = streamptr->fileID;
vlistID = streamptr->vlistID;
gridID = vlistInqVarGrid(vlistID, varID);
zaxisID = vlistInqVarZaxis(vlistID, varID);
tsteptype = vlistInqVarTsteptype(vlistID, varID);
int comptype = streamptr->comptype;
tsID = streamptr->curTsID;
date = streamptr->tsteps[tsID].taxis.vdate;
time = streamptr->tsteps[tsID].taxis.vtime;
if ( vlistInqVarTimave(vlistID, varID) )
numavg = streamptr->tsteps[tsID].taxis.numavg;
if ( CDI_Debug )
Message("gridID = %d zaxisID = %d", gridID, zaxisID);
size_t datasize = (size_t)gridInqSize(gridID);
/*
gribbuffersize = datasize*4+3000;
gribbuffer = (unsigned char *) Malloc(gribbuffersize);
*/
#if defined (HAVE_LIBCGRIBEX)
if ( filetype == FILETYPE_GRB )
{
}
else
#endif
{
#if defined (GRIBCONTAINER2D)
gribContainer_t **gribContainers = (gribContainer_t **) streamptr->gribContainers;
gc = (void *) &gribContainers[varID][levelID];
#else
gribContainer_t *gribContainers = (gribContainer_t *) streamptr->gribContainers;
gc = (void *) &gribContainers[varID];
#endif
}
if ( comptype != COMPRESS_JPEG && comptype != COMPRESS_SZIP ) comptype = COMPRESS_NONE;
if ( filetype == FILETYPE_GRB && comptype == COMPRESS_JPEG )
{
static int ljpeg_warn = 1;
if ( ljpeg_warn ) Warning("JPEG compression of GRIB1 records not available!");
ljpeg_warn = 0;
}
nbytes = grbEncode(filetype, memtype, varID, levelID, vlistID, gridID, zaxisID, date, time, tsteptype, numavg,
datasize, (const double*) data, nmiss, &gribbuffer, comptype, gc);
if ( filetype == FILETYPE_GRB && streamptr->comptype == COMPRESS_SZIP )
nbytes = grbSzip(filetype, gribbuffer, nbytes);
{
size_t (*myFileWrite)(int fileID, const void *restrict buffer,
size_t len, int tsID)
= (size_t (*)(int, const void *restrict, size_t, int))
namespaceSwitchGet(NSSWITCH_FILE_WRITE).func;
nwrite = myFileWrite(fileID, gribbuffer, nbytes, tsID);
}
if ( nwrite != nbytes )
{
perror(__func__);
Error("Failed to write GRIB slice!");
}
if ( gribbuffer ) Free(gribbuffer);
}
void grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data, int nmiss)
{
int vlistID = streamptr->vlistID,
gridID = vlistInqVarGrid(vlistID, varID),
gridsize = gridInqSize(gridID),
zaxisID = vlistInqVarZaxis(vlistID, varID),
nlevs = zaxisInqSize(zaxisID);
double missval = vlistInqVarMissval(vlistID, varID);
size_t chunkLen = (size_t)gridsize;
if ( memtype == MEMTYPE_FLOAT )
for ( int levelID = 0; levelID < nlevs; levelID++ )
{
int nmiss_slice = 0;
const float *restrict fdata = ((const float *)data)+levelID*gridsize;
for ( size_t i = 0; i < chunkLen; ++i )
nmiss_slice += DBL_IS_EQUAL(fdata[i], missval);
grb_write_var_slice(streamptr, varID, levelID, memtype, fdata, nmiss_slice);
}
else
for ( int levelID = 0; levelID < nlevs; levelID++ )
{
int nmiss_slice = 0;
const double *restrict ddata = ((const double *)data)+levelID*gridsize;
for ( size_t i = 0; i < chunkLen; ++i )
nmiss_slice += DBL_IS_EQUAL(ddata[i], missval);
grb_write_var_slice(streamptr, varID, levelID, memtype, ddata, nmiss_slice);
}
}
void grbCopyRecord(stream_t * streamptr2, stream_t * streamptr1)
{
int filetype = streamptr1->filetype;
int fileID1 = streamptr1->fileID;
int fileID2 = streamptr2->fileID;
int tsID = streamptr1->curTsID;
int vrecID = streamptr1->tsteps[tsID].curRecID;
int recID = streamptr1->tsteps[tsID].recIDs[vrecID];
off_t recpos = streamptr1->tsteps[tsID].records[recID].position;
size_t recsize = streamptr1->tsteps[tsID].records[recID].size;
fileSetPos(fileID1, recpos, SEEK_SET);
/* round up recsize to next multiple of 8 */
size_t gribbuffersize = ((recsize + 7U) & ~7U);
unsigned char *gribbuffer = (unsigned char *) Malloc(gribbuffersize);
if (fileRead(fileID1, gribbuffer, recsize) != recsize)
Error("Could not read GRIB record for copying!");
size_t nbytes = recsize;
if ( filetype == FILETYPE_GRB )
{
long unzipsize;
int izip = gribGetZip((long)recsize, gribbuffer, &unzipsize);
if ( izip == 0 && streamptr2->comptype == COMPRESS_SZIP )
nbytes = grbSzip(filetype, gribbuffer, nbytes);
}
while ( nbytes & 7 ) gribbuffer[nbytes++] = 0;
size_t nwrite = fileWrite(fileID2, gribbuffer, nbytes);
if ( nwrite != nbytes )
{
perror(__func__);
Error("Could not write record for copying!");
}
Free(gribbuffer);
}
void grb_write_record(stream_t * streamptr, int memtype, const void *data, int nmiss)
{
int varID = streamptr->record->varID;
int levelID = streamptr->record->levelID;
grb_write_var_slice(streamptr, varID, levelID, memtype, data, nmiss);
}
void streamInqGRIBinfo(int streamID, int *intnum, float *fltnum, off_t *bignum)
{
......
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