Commit 9157d2ef authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Replaced variable length arrays.

parent a89805a6
......@@ -290,18 +290,12 @@ void transpose2dArrayDP(size_t inWidth, size_t inHeight, double *data)
{
const size_t cacheBlockSize = 256; // Purely an optimization parameter. Current value of 32 means we are handling 8kB blocks,
// which should be a decent compromise on many architectures.
#ifdef __cplusplus
double *out[inHeight];
double *temp[inWidth];
temp[0] = (double *) Malloc(inHeight*inWidth*sizeof(double));
double **out = (double**) malloc(inHeight*sizeof(double*));
double **temp = (double**) malloc(inWidth*sizeof(double*));
temp[0] = (double *) malloc(inHeight*inWidth*sizeof(double));
memcpy(temp[0], data, inHeight*inWidth*sizeof(double));
for(int i = 0; i < inHeight; i++) out[i] = data + (inWidth*i);
for(int i = 1; i < inWidth; i++) temp[i] = temp[0] + (inHeight*i);
#else
double (*out)[inHeight] = (double (*)[inHeight])data;
double (*temp)[inWidth] = (double (*)[inWidth]) Malloc(inHeight*sizeof(*temp));
memcpy(temp, data, inHeight*sizeof(*temp));
#endif
for(size_t i = 0; i < inHeight; i++) out[i] = data + (inWidth*i);
for(size_t i = 1; i < inWidth; i++) temp[i] = temp[0] + (inHeight*i);
/*
for ( size_t y = 0; y < inHeight; ++y )
......@@ -317,7 +311,9 @@ void transpose2dArrayDP(size_t inWidth, size_t inHeight, double *data)
out[x][y] = temp[y][x];
}
Free(temp[0]);
free(temp[0]);
free(temp);
free(out);
}
static
......@@ -325,18 +321,12 @@ void transpose2dArraySP(size_t inWidth, size_t inHeight, float *data)
{
const size_t cacheBlockSize = 256; // Purely an optimization parameter. Current value of 32 means we are handling 8kB blocks,
// which should be a decent compromise on many architectures.
#ifdef __cplusplus
float *out[inHeight];
float *temp[inWidth];
temp[0] = (float *) Malloc(inHeight*inWidth*sizeof(float));
float **out = (float**) malloc(inHeight*sizeof(float*));
float **temp = (float**) malloc(inWidth*sizeof(float*));
temp[0] = (float *) malloc(inHeight*inWidth*sizeof(float));
memcpy(temp[0], data, inHeight*inWidth*sizeof(float));
for(int i = 0; i < inHeight; i++) out[i] = data + (inWidth*i);
for(int i = 1; i < inWidth; i++) temp[i] = temp[0] + (inHeight*i);
#else
float (*out)[inHeight] = (float (*)[inHeight])data;
float (*temp)[inWidth] = (float (*)[inWidth]) Malloc(inHeight*sizeof(*temp));
memcpy(temp, data, inHeight*sizeof(*temp));
#endif
for(size_t i = 0; i < inHeight; i++) out[i] = data + (inWidth*i);
for(size_t i = 1; i < inWidth; i++) temp[i] = temp[0] + (inHeight*i);
/*
for ( size_t y = 0; y < inHeight; ++y )
......@@ -352,7 +342,9 @@ void transpose2dArraySP(size_t inWidth, size_t inHeight, float *data)
out[x][y] = temp[y][x];
}
Free(temp);
free(temp[0]);
free(temp);
free(out);
}
static
......
......@@ -220,9 +220,11 @@ void cdfDefineAttributes(int cdiID, int varID, int fileID, int ncvarID)
len = (size_t)attlen;
if ( atttype == CDI_DATATYPE_FLT32 )
{
float attflt_sp[len];
for ( size_t i = 0; i < len; ++i ) attflt_sp[i] = (float)attflt[i];
cdf_put_att_float(fileID, ncvarID, attname, NC_FLOAT, len, attflt_sp);
float attflt_sp[8];
float *pattflt_sp = (len > 8) ? (float*) malloc(len*sizeof(float)) : attflt_sp;
for ( size_t i = 0; i < len; ++i ) pattflt_sp[i] = (float)attflt[i];
cdf_put_att_float(fileID, ncvarID, attname, NC_FLOAT, len, pattflt_sp);
if (len > 8) free(pattflt_sp);
}
else
cdf_put_att_double(fileID, ncvarID, attname, NC_DOUBLE, len, attflt);
......@@ -455,29 +457,23 @@ int cdfDefVar(stream_t *streamptr, int varID)
if ( name[0] )
{
sprintf(varname, "%s", name);
char *varname2 = varname+strlen(varname);
unsigned iz = 0;
bool checkname = true;
int iz = 0;
while ( checkname )
do
{
if ( iz ) sprintf(varname, "%s_%d", name, iz+1);
int status = nc_inq_varid(fileID, varname, &ncvarid);
if ( status != NC_NOERR ) checkname = false;
if ( iz ) sprintf(varname2, "_%u", iz+1);
if ( checkname ) iz++;
if ( nc_inq_varid(fileID, varname2, &ncvarid) != NC_NOERR ) break;
if ( iz >= CDI_MAX_NAME ) Error("Double entry of variable name '%s'!", name);
++iz;
}
while ( iz <= 99 );
if (iz > 99) Error("Variable name %s already exsist!", name);
if ( strcmp(name, varname) != 0 )
{
if ( iz == 1 )
Warning("Changed double entry of variable name '%s' to '%s'!", name, varname);
else
Warning("Changed multiple entry of variable name '%s' to '%s'!", name, varname);
}
Warning("Changed %s entry of variable name '%s' to '%s'!", (iz==1)?"double":"multiple", name, varname);
strcpy(name, varname);
}
......@@ -492,21 +488,19 @@ int cdfDefVar(stream_t *streamptr, int varID)
sprintf(varname, "param%d.%d.%d", pnum, pcat, pdis);
char *varname2 = varname+strlen(varname);
unsigned iz = 0;
bool checkname = true;
int iz = 0;
while ( checkname )
do
{
if ( iz ) sprintf(varname2, "_%d", iz+1);
if ( iz ) sprintf(varname2, "_%u", iz+1);
int status = nc_inq_varid(fileID, varname, &ncvarid);
if ( status != NC_NOERR ) checkname = false;
if ( nc_inq_varid(fileID, varname2, &ncvarid) != NC_NOERR ) break;
if ( checkname ) iz++;
if ( iz >= CDI_MAX_NAME ) break;
++iz;
}
while ( iz <= 99 );
if (iz > 99) Error("Variable name %s already exsist!", name);
strcpy(name, varname);
code = 0;
......
......@@ -140,7 +140,7 @@ struct idSearch
};
static inline struct idSearch
cdfSearchIDBySize(size_t startIdx, size_t numIDs, const ncgrid_t ncgrid[numIDs],
cdfSearchIDBySize(size_t startIdx, size_t numIDs, const ncgrid_t ncgrid[/*numIDs*/],
int ncIDType, int searchType, int searchSize,
int (*typeInq)(int id), size_t (*sizeInq)(int id))
{
......@@ -383,12 +383,12 @@ void checkGridName(char *axisname, int fileID)
size_t axisnameLen = strlen(axisname);
memcpy(axisname2, axisname, axisnameLen + 1);
do
{
if ( iz ) sprintf(axisname2 + axisnameLen, "_%u", iz+1);
int status = nc_inq_varid(fileID, axisname2, &ncdimid);
if ( status != NC_NOERR ) break;
if ( nc_inq_varid(fileID, axisname2, &ncdimid) != NC_NOERR ) break;
++iz;
}
......@@ -494,19 +494,15 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims,
int keyname = (axisLetter == 'X') ? CDI_KEY_XNAME : CDI_KEY_YNAME;
gridAxisInq->axisName(gridID, keyname, CDI_MAX_NAME, axisname);
if ( axisname[0] == 0 ) Error("axis name undefined!");
size_t axisnameLen = strlen(axisname);
/* enough to append _ plus up to 100 decimal and trailing \0 */
char extendedAxisname[axisnameLen + 4 + 1];
memcpy(extendedAxisname, axisname, axisnameLen + 1);
checkGridName(extendedAxisname, fileID);
size_t extendedAxisnameLen = axisnameLen + strlen(extendedAxisname + axisnameLen);
checkGridName(axisname, fileID);
size_t axisnameLen = strlen(axisname);
if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
if ( ndims )
{
if ( dimname[0] == 0 ) strcpy(dimname, extendedAxisname);
if ( dimname[0] == 0 ) strcpy(dimname, axisname);
dimID = checkDimName(fileID, dimlen, dimname);
if ( dimID == CDI_UNDEFID ) cdf_def_dim(fileID, dimname, dimlen, &dimID);
......@@ -517,7 +513,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims,
double *pbounds = NULL;
if ( pvals )
{
cdf_def_var(fileID, extendedAxisname, xtype, ndims, &dimID, &ncvarid);
cdf_def_var(fileID, axisname, xtype, ndims, &dimID, &ncvarid);
cdfPutGridStdAtts(fileID, ncvarid, gridID, axisLetter, gridAxisInq);
{
......@@ -547,13 +543,13 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims,
}
if ( pbounds && nvdimID != CDI_UNDEFID )
{
char boundsname[extendedAxisnameLen + 1 + sizeof(bndsName)];
memcpy(boundsname, axisname, extendedAxisnameLen);
boundsname[extendedAxisnameLen] = '_';
memcpy(boundsname + extendedAxisnameLen + 1, bndsName, sizeof bndsName);
char boundsname[CDI_MAX_NAME];
memcpy(boundsname, axisname, axisnameLen);
boundsname[axisnameLen] = '_';
memcpy(boundsname + axisnameLen + 1, bndsName, sizeof(bndsName));
int dimIDs[2] = { dimID, nvdimID };
cdf_def_var(fileID, boundsname, xtype, 2, dimIDs, &ncbvarid);
cdf_put_att_text(fileID, ncvarid, "bounds", extendedAxisnameLen + sizeof (bndsName), boundsname);
cdf_put_att_text(fileID, ncvarid, "bounds", axisnameLen + sizeof(bndsName), boundsname);
}
}
......@@ -1124,13 +1120,13 @@ void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID,
streamptr->ncmode = 2;
int vctsize = zaxisInqVctSize(zaxisID);
double vct[vctsize];
double *vct = (double*) malloc(vctsize*sizeof(double));;
zaxisInqVct(zaxisID, vct);
if ( p0status == 0 && IS_NOT_EQUAL(p0value,0) )
for ( int i = 0; i < vctsize/2; ++i ) vct[i] /= p0value;
double tarray[ilev*2];
double *tarray = (double*) malloc(ilev*2*sizeof(double));
if ( ncbndsID != -1 )
{
......@@ -1156,6 +1152,9 @@ void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID,
for ( int i = 0; i < mlev; ++i )
tarray[i] = (vct[ilev+i] + vct[ilev+i+1]) * 0.5;
cdf_put_var_double(fileID, hybmid, tarray);
free(tarray);
free(vct);
}
}
......@@ -1612,7 +1611,10 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
if ( ncbvarid != CDI_UNDEFID )
{
double lbounds[dimlen], ubounds[dimlen], zbounds[2*dimlen];
double *buffer = (double *) malloc(4*dimlen*sizeof(double));
double *zbounds = buffer;
double *lbounds = buffer + 2*dimlen;
double *ubounds = buffer + 3*dimlen;
zaxisInqLbounds(zaxisID, lbounds);
zaxisInqUbounds(zaxisID, ubounds);
for ( size_t i = 0; i < dimlen; ++i )
......@@ -1622,6 +1624,8 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
}
cdf_put_var_double(fileID, ncbvarid, zbounds);
free(buffer);
}
if ( ndims == 0 ) streamptr->nczvarID[zaxisindex] = ncvarid;
......
......@@ -2040,36 +2040,29 @@ void vlistVarPack(vlist_t *p, int varID, char * buf, int size, int *position,
dtempbuf[0] = var->missval;
dtempbuf[1] = var->scalefactor;
dtempbuf[2] = var->addoffset;
serializePack(tempbuf, vlistvarNint, CDI_DATATYPE_INT,
buf, size, position, context);
serializePack(dtempbuf, vlistvar_ndbls, CDI_DATATYPE_FLT64,
buf, size, position, context);
serializePack(tempbuf, vlistvarNint, CDI_DATATYPE_INT, buf, size, position, context);
serializePack(dtempbuf, vlistvar_ndbls, CDI_DATATYPE_FLT64,buf, size, position, context);
if (namesz)
serializePack(var->name, namesz, CDI_DATATYPE_TXT, buf, size, position, context);
if (longnamesz)
serializePack(var->longname, longnamesz, CDI_DATATYPE_TXT,
buf, size, position, context);
serializePack(var->longname, longnamesz, CDI_DATATYPE_TXT, buf, size, position, context);
if (stdnamesz)
serializePack(var->stdname, stdnamesz, CDI_DATATYPE_TXT,
buf, size, position, context);
serializePack(var->stdname, stdnamesz, CDI_DATATYPE_TXT, buf, size, position, context);
if (unitssz)
serializePack(var->units, unitssz, CDI_DATATYPE_TXT,
buf, size, position, context);
serializePack(var->units, unitssz, CDI_DATATYPE_TXT, buf, size, position, context);
if (extralen)
serializePack(var->extra, extralen, CDI_DATATYPE_TXT,
buf, size, position, context);
serializePack(var->extra, extralen, CDI_DATATYPE_TXT, buf, size, position, context);
if (nlevs)
{
int levbuf[nlevs][4];
for (int levID = 0; levID < nlevs; ++levID)
{
levbuf[levID][0] = var->levinfo[levID].flag;
levbuf[levID][1] = var->levinfo[levID].index;
levbuf[levID][2] = var->levinfo[levID].mlevelID;
levbuf[levID][3] = var->levinfo[levID].flevelID;
}
serializePack(levbuf, nlevs * 4, CDI_DATATYPE_INT,
buf, size, position, context);
int *levbuf = (int*) malloc(nlevs*sizeof(int));
for (int levID = 0; levID < nlevs; ++levID) levbuf[levID] = var->levinfo[levID].flag;
serializePack(levbuf, nlevs, CDI_DATATYPE_INT, buf, size, position, context);
for (int levID = 0; levID < nlevs; ++levID) levbuf[levID] = var->levinfo[levID].index;
serializePack(levbuf, nlevs, CDI_DATATYPE_INT, buf, size, position, context);
for (int levID = 0; levID < nlevs; ++levID) levbuf[levID] = var->levinfo[levID].mlevelID;
serializePack(levbuf, nlevs, CDI_DATATYPE_INT, buf, size, position, context);
for (int levID = 0; levID < nlevs; ++levID) levbuf[levID] = var->levinfo[levID].flevelID;
free(levbuf);
}
cdiAttsPack(p, varID, buf, size, position, context);
}
......@@ -2088,10 +2081,8 @@ void vlistVarUnpack(int vlistID, char * buf, int size, int *position,
int tempbuf[vlistvarNint];
char *varname = NULL;
vlist_t *vlistptr = vlist_to_pointer(vlistID);
serializeUnpack(buf, size, position,
tempbuf, vlistvarNint, CDI_DATATYPE_INT, context);
serializeUnpack(buf, size, position,
dtempbuf, vlistvar_ndbls, CDI_DATATYPE_FLT64, context);
serializeUnpack(buf, size, position, tempbuf, vlistvarNint, CDI_DATATYPE_INT, context);
serializeUnpack(buf, size, position, dtempbuf, vlistvar_ndbls, CDI_DATATYPE_FLT64, context);
/* ------------------------------------------- */
/* NOTE: Tile sets currently not supported!!! */
......@@ -2162,23 +2153,22 @@ void vlistVarUnpack(int vlistID, char * buf, int size, int *position,
int nlevs = tempbuf[VLISTVAR_PACK_INT_IDX_NLEVS];
if (nlevs)
{
int levbuf[nlevs][4];
var_t *var = vlistptr->vars + newvar;
int i, flagSetLev = 0;
cdiVlistCreateVarLevInfo(vlistptr, newvar);
serializeUnpack(buf, size, position,
levbuf, nlevs * 4, CDI_DATATYPE_INT, context);
for (i = 0; i < nlevs; ++i)
{
vlistDefFlag(vlistID, newvar, i, levbuf[i][0]);
vlistDefIndex(vlistID, newvar, i, levbuf[i][1]);
// FIXME: these lack an accessor function
var->levinfo[i].mlevelID = levbuf[i][2];
var->levinfo[i].flevelID = levbuf[i][3];
if (levbuf[i][0] == tempbuf[0])
flagSetLev = i;
}
vlistDefFlag(vlistID, newvar, flagSetLev, levbuf[flagSetLev][0]);
int *levbuf = (int*) malloc(nlevs*sizeof(int));
serializeUnpack(buf, size, position, levbuf, nlevs, CDI_DATATYPE_INT, context);
for (i = 0; i < nlevs; ++i) vlistDefFlag(vlistID, newvar, i, levbuf[i]);
for (i = 0; i < nlevs; ++i) if (levbuf[i] == tempbuf[0]) flagSetLev = i;
vlistDefFlag(vlistID, newvar, flagSetLev, levbuf[flagSetLev]);
serializeUnpack(buf, size, position, levbuf, nlevs, CDI_DATATYPE_INT, context);
for (i = 0; i < nlevs; ++i) vlistDefIndex(vlistID, newvar, i, levbuf[i]);
serializeUnpack(buf, size, position, levbuf, nlevs, CDI_DATATYPE_INT, context);
for (i = 0; i < nlevs; ++i) var->levinfo[i].mlevelID = levbuf[i];
serializeUnpack(buf, size, position, levbuf, nlevs, CDI_DATATYPE_INT, context);
for (i = 0; i < nlevs; ++i) var->levinfo[i].flevelID = levbuf[i];
free(levbuf);
}
vlistDefVarIOrank(vlistID, newvar, tempbuf[VLISTVAR_PACK_INT_IDX_IORANK]);
cdiAttsUnpack(vlistID, newvar, buf, size, position, context);
......
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