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

streamWriteVarSliceF: added fall back to double precision writing

parent 2d2c8fcf
#if defined (HAVE_CONFIG_H)
# include "config.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cdi.h"
......@@ -133,7 +133,7 @@ void streamReadVarF(int streamID, int varID, float *data, int *nmiss)
size_t elementCount = (size_t) gridInqSize(vlistInqVarGrid(streamInqVlist(streamID), varID));
elementCount *= (size_t) zaxisInqSize(vlistInqVarZaxis(streamInqVlist(streamID), varID));
double *conversionBuffer = (double *) Malloc(elementCount*sizeof(*conversionBuffer));
cdiStreamReadVar(streamID, varID, MEMTYPE_DOUBLE, (void *) conversionBuffer, nmiss);
streamReadVar(streamID, varID, (void *) conversionBuffer, nmiss);
for ( size_t i = elementCount; i--; ) data[i] = (float) conversionBuffer[i];
Free(conversionBuffer);
}
......@@ -350,7 +350,7 @@ void streamReadRecordF(int streamID, float *data, int *nmiss)
int varID = streamptr->tsteps[tsID].records[recID].varID;
size_t elementCount = (size_t) gridInqSize(vlistInqVarGrid(streamInqVlist(streamID), varID));
double *conversionBuffer = (double *) Malloc(elementCount*sizeof(*conversionBuffer));
stream_read_record(streamID, MEMTYPE_DOUBLE, (void *) conversionBuffer, nmiss);
streamReadRecord(streamID, (void *) conversionBuffer, nmiss);
for ( size_t i = elementCount; i--; ) data[i] = (float) conversionBuffer[i];
Free(conversionBuffer);
}
......
......@@ -133,8 +133,12 @@ void streamWriteVarF(int streamID, int varID, const float *data, int nmiss)
}
static
void cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, const void *data, int nmiss)
int cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, const void *data, int nmiss)
{
// May fail if memtype == MEMTYPE_FLOAT and the file format does not support single precision writing.
// A value > 0 is returned in this case, otherwise it returns zero.
int status = 0;
if ( CDI_Debug ) Message("streamID = %d varID = %d", streamID, varID);
check_parg(data);
......@@ -161,7 +165,7 @@ void cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, c
#if defined (HAVE_LIBSERVICE)
case FILETYPE_SRV:
{
if ( memtype == MEMTYPE_FLOAT ) Error("srvWriteVarSlice not implemented for memtype float!");
if ( memtype == MEMTYPE_FLOAT ) return 1;
srvWriteVarSliceDP(streamptr, varID, levelID, (double *)data);
break;
}
......@@ -169,7 +173,7 @@ void cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, c
#if defined (HAVE_LIBEXTRA)
case FILETYPE_EXT:
{
if ( memtype == MEMTYPE_FLOAT ) Error("extWriteVarSlice not implemented for memtype float!");
if ( memtype == MEMTYPE_FLOAT ) return 1;
extWriteVarSliceDP(streamptr, varID, levelID, (double *)data);
break;
}
......@@ -177,7 +181,7 @@ void cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, c
#if defined (HAVE_LIBIEG)
case FILETYPE_IEG:
{
if ( memtype == MEMTYPE_FLOAT ) Error("iegWriteVarSlice not implemented for memtype float!");
if ( memtype == MEMTYPE_FLOAT ) return 1;
iegWriteVarSliceDP(streamptr, varID, levelID, (double *)data);
break;
}
......@@ -196,6 +200,8 @@ void cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, c
break;
}
}
return status;
}
/*
......@@ -240,7 +246,16 @@ 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, (const void *) data, nmiss);
if ( cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_FLOAT, (const void *) data, nmiss) )
{
// In case the file format does not support single precision reading,
// we fall back to double precision writing, converting the data on the fly.
size_t elementCount = (size_t) gridInqSize(vlistInqVarGrid(streamInqVlist(streamID), varID));
double *conversionBuffer = (double *) Malloc(elementCount*sizeof(*conversionBuffer));
for ( size_t i = elementCount; i--; ) conversionBuffer[i] = (double) data[i];
streamWriteVarSlice(streamID, varID, levelID, (const void *) conversionBuffer, nmiss);
Free(conversionBuffer);
}
}
......@@ -393,7 +408,7 @@ void streamWriteRecordF(int streamID, const float *data, int nmiss)
size_t elementCount = (size_t) gridInqSize(vlistInqVarGrid(streamInqVlist(streamID), varID));
double *conversionBuffer = (double *) Malloc(elementCount*sizeof(*conversionBuffer));
for ( size_t i = elementCount; i--; ) conversionBuffer[i] = (double) data[i];
stream_write_record(streamID, MEMTYPE_DOUBLE, (const void *) conversionBuffer, nmiss);
streamWriteRecord(streamID, (const void *) conversionBuffer, nmiss);
Free(conversionBuffer);
}
}
......
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