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

Added flexible XYZ dimension ordering for netCDF

parent 4cd050ef
2011-07-29 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de>
* Added flexible XYZ dimension ordering for netCDF [request: Andy Aschwanden]
2011-07-28 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de> 2011-07-28 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de>
* added interface function vlistDefVarXYZ to set the dimension order * added interface function vlistDefVarXYZ to set the dimension order
......
...@@ -3,8 +3,8 @@ CDI NEWS ...@@ -3,8 +3,8 @@ CDI NEWS
Version 1.5.2 (?? ??? 2011): Version 1.5.2 (?? ??? 2011):
New functions: New features:
* zaxisInqVct - read VCT * Added flexible XYZ dimension ordering for netCDF
Version 1.5.1 (12 July 2011): Version 1.5.1 (12 July 2011):
......
...@@ -2669,6 +2669,8 @@ int cdfDefVar(int streamID, int varID) ...@@ -2669,6 +2669,8 @@ int cdfDefVar(int streamID, int varID)
int gridindex, zaxisindex; int gridindex, zaxisindex;
int tablenum; int tablenum;
int vlistID; int vlistID;
int dimorder[3];
int ixyz;
int iax = 0; int iax = 0;
char axis[5]; char axis[5];
stream_t *streamptr; stream_t *streamptr;
...@@ -2692,6 +2694,16 @@ int cdfDefVar(int streamID, int varID) ...@@ -2692,6 +2694,16 @@ int cdfDefVar(int streamID, int varID)
param = vlistInqVarParam(vlistID, varID); param = vlistInqVarParam(vlistID, varID);
cdiDecodeParam(param, &pnum, &pcat, &pdis); cdiDecodeParam(param, &pnum, &pcat, &pdis);
ixyz = vlistInqVarXYZ(vlistID, varID);
if ( ixyz == 0 ) ixyz = 321; // ZYX
dimorder[0] = ixyz/100;
dimorder[1] = (ixyz-dimorder[0]*100)/10;
dimorder[2] = (ixyz-dimorder[0]*100-dimorder[1]*10);
if ( dimorder[2] == 2 || dimorder[0] == 1 ) lchunk = FALSE;
gridsize = gridInqSize(gridID); gridsize = gridInqSize(gridID);
if ( gridsize > 1 ) lchunk = TRUE; if ( gridsize > 1 ) lchunk = TRUE;
gridtype = gridInqType(gridID); gridtype = gridInqType(gridID);
...@@ -2716,7 +2728,7 @@ int cdfDefVar(int streamID, int varID) ...@@ -2716,7 +2728,7 @@ int cdfDefVar(int streamID, int varID)
dims[ndims++] = tid; dims[ndims++] = tid;
axis[iax++] = 'T'; axis[iax++] = 'T';
} }
/*
if ( zid != UNDEFID ) axis[iax++] = 'Z'; if ( zid != UNDEFID ) axis[iax++] = 'Z';
if ( zid != UNDEFID ) chunks[ndims] = 1; if ( zid != UNDEFID ) chunks[ndims] = 1;
if ( zid != UNDEFID ) dims[ndims++] = zid; if ( zid != UNDEFID ) dims[ndims++] = zid;
...@@ -2726,6 +2738,29 @@ int cdfDefVar(int streamID, int varID) ...@@ -2726,6 +2738,29 @@ int cdfDefVar(int streamID, int varID)
if ( xid != UNDEFID ) chunks[ndims] = xsize; if ( xid != UNDEFID ) chunks[ndims] = xsize;
if ( xid != UNDEFID ) dims[ndims++] = xid; if ( xid != UNDEFID ) dims[ndims++] = xid;
*/
for ( int id = 0; id < 3; ++id )
{
if ( dimorder[id] == 3 && zid != UNDEFID )
{
axis[iax++] = 'Z';
chunks[ndims] = 1;
dims[ndims] = zid;
ndims++;
}
else if ( dimorder[id] == 2 && yid != UNDEFID )
{
chunks[ndims] = ysize;
dims[ndims] = yid;
ndims++;
}
else if ( dimorder[id] == 1 && xid != UNDEFID )
{
chunks[ndims] = xsize;
dims[ndims] = xid;
ndims++;
}
}
if ( CDI_Debug ) if ( CDI_Debug )
fprintf(stderr, "chunks %d %d %d %d\n", fprintf(stderr, "chunks %d %d %d %d\n",
...@@ -3016,7 +3051,6 @@ int cdfDefVar(int streamID, int varID) ...@@ -3016,7 +3051,6 @@ int cdfDefVar(int streamID, int varID)
} }
#endif #endif
void cdfReadVarDP(int streamID, int varID, double *data, int *nmiss) void cdfReadVarDP(int streamID, int varID, double *data, int *nmiss)
{ {
#if defined (HAVE_LIBNETCDF) #if defined (HAVE_LIBNETCDF)
...@@ -3500,7 +3534,7 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data, ...@@ -3500,7 +3534,7 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
int xid = UNDEFID, yid = UNDEFID, zid = UNDEFID; int xid = UNDEFID, yid = UNDEFID, zid = UNDEFID;
int ncvarid; int ncvarid;
int ntsteps; int ntsteps;
size_t size; size_t size, xsize, ysize;
size_t start[4]; size_t start[4];
size_t count[4]; size_t count[4];
int ndims = 0; int ndims = 0;
...@@ -3508,9 +3542,14 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data, ...@@ -3508,9 +3542,14 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
int timeID; int timeID;
int gridindex, zaxisindex; int gridindex, zaxisindex;
int i; int i;
int dimorder[3];
int ixyz;
int swapxy = FALSE;
int dtype; int dtype;
int vlistID; int vlistID;
const double *pdata = data;
double *mdata = NULL; double *mdata = NULL;
double *sdata = NULL;
stream_t *streamptr; stream_t *streamptr;
extern int CDF_Debug; extern int CDF_Debug;
...@@ -3532,7 +3571,20 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data, ...@@ -3532,7 +3571,20 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
gridID = vlistInqVarGrid(vlistID, varID); gridID = vlistInqVarGrid(vlistID, varID);
zaxisID = vlistInqVarZaxis(vlistID, varID); zaxisID = vlistInqVarZaxis(vlistID, varID);
timeID = vlistInqVarTime(vlistID, varID); timeID = vlistInqVarTime(vlistID, varID);
ixyz = vlistInqVarXYZ(vlistID, varID);
if ( ixyz == 0 ) ixyz = 321; // ZYX
dimorder[0] = ixyz/100;
dimorder[1] = (ixyz-dimorder[0]*100)/10;
dimorder[2] = (ixyz-dimorder[0]*100-dimorder[1]*10);
if ( dimorder[2] == 2 || dimorder[0] == 1 ) swapxy = TRUE;
/*
printf("swapxy %d\n", swapxy);
printf("ixyz %d\n", ixyz);
printf("dimorder: %d %d %d\n", dimorder[0], dimorder[1], dimorder[2]);
*/
gridindex = vlistGridIndex(vlistID, gridID); gridindex = vlistGridIndex(vlistID, gridID);
if ( gridInqType(gridID) == GRID_TRAJECTORY ) if ( gridInqType(gridID) == GRID_TRAJECTORY )
{ {
...@@ -3553,26 +3605,30 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data, ...@@ -3553,26 +3605,30 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
count[ndims] = 1; count[ndims] = 1;
ndims++; ndims++;
} }
if ( zid != UNDEFID )
for ( int id = 0; id < 3; ++id )
{
if ( dimorder[id] == 3 && zid != UNDEFID )
{ {
start[ndims] = levelID; start[ndims] = levelID;
count[ndims] = 1; count[ndims] = 1;
ndims++; ndims++;
} }
if ( yid != UNDEFID ) else if ( dimorder[id] == 2 && yid != UNDEFID )
{ {
start[ndims] = 0; start[ndims] = 0;
cdf_inq_dimlen(fileID, yid, &size); cdf_inq_dimlen(fileID, yid, &ysize);
count[ndims] = size; count[ndims] = ysize;
ndims++; ndims++;
} }
if ( xid != UNDEFID ) else if ( dimorder[id] == 1 && xid != UNDEFID )
{ {
start[ndims] = 0; start[ndims] = 0;
cdf_inq_dimlen(fileID, xid, &size); cdf_inq_dimlen(fileID, xid, &xsize);
count[ndims] = size; count[ndims] = xsize;
ndims++; ndims++;
} }
}
if ( CDI_Debug ) if ( CDI_Debug )
for (idim = 0; idim < ndims; idim++) for (idim = 0; idim < ndims; idim++)
...@@ -3654,14 +3710,25 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data, ...@@ -3654,14 +3710,25 @@ int cdfWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
Message("nvals = %d, nmiss = %d, missval = %g, minval = %g, maxval = %g", Message("nvals = %d, nmiss = %d, missval = %g, minval = %g, maxval = %g",
nvals, nmiss, missval, fmin, fmax); nvals, nmiss, missval, fmin, fmax);
} }
if ( mdata ) pdata = mdata;
}
if ( swapxy )
{
int nvals;
nvals = gridInqSize(gridID);
sdata = (double *) malloc(nvals*sizeof(double));
for ( int j = 0; j < (int)ysize; ++j )
for ( int i = 0; i < (int)xsize; ++i )
sdata[j*xsize+i] = pdata[i*ysize+j];
pdata = sdata;
} }
if ( mdata )
cdf_put_vara_double(fileID, ncvarid, start, count, mdata);
else
cdf_put_vara_double(fileID, ncvarid, start, count, data); cdf_put_vara_double(fileID, ncvarid, start, count, data);
if ( mdata ) free(mdata); if ( mdata ) free(mdata);
if ( sdata ) free(sdata);
#endif #endif
return (0); return (0);
} }
......
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