Commit 5d931021 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

patch from Nathanael Huebbe: 0012-Simplified-dimension-order-handling

parent ee8c7297
......@@ -32,6 +32,8 @@
#define UNDEFID CDI_UNDEFID
void vlistInqVarDimorder(int vlistID, int varID, int (*outDimorder)[3]);
void cdfDefGlobalAtts(stream_t *streamptr);
void cdfDefLocalAtts(stream_t *streamptr);
......@@ -2923,7 +2925,6 @@ int cdfDefVar(stream_t *streamptr, int varID)
int tablenum;
int vlistID;
int dimorder[3];
int ixyz;
int iax = 0;
char axis[5];
int ensID, ensCount, forecast_type;
......@@ -2947,8 +2948,7 @@ int cdfDefVar(stream_t *streamptr, int varID)
chunktype = vlistInqVarChunkType(vlistID, varID);
ixyz = vlistInqVarXYZ(vlistID, varID);
if ( ixyz == 0 ) ixyz = 321; // ZYX
vlistInqVarDimorder(vlistID, varID, &dimorder);
gridsize = gridInqSize(gridID);
if ( gridsize > 1 ) lchunk = TRUE;
......@@ -2965,14 +2965,11 @@ int cdfDefVar(stream_t *streamptr, int varID)
zaxisindex = vlistZaxisIndex(vlistID, zaxisID);
zid = streamptr->zaxisID[zaxisindex];
dimorder[0] = ixyz/100;
dimorder[1] = (ixyz-dimorder[0]*100)/10;
dimorder[2] = (ixyz-dimorder[0]*100-dimorder[1]*10);
if ( dimorder[0] != 3 ) lchunk = FALSE; /* ZYX and ZXY */
if ( ((dimorder[0]>0)+(dimorder[1]>0)+(dimorder[2]>0)) < ((xid!=UNDEFID)+(yid!=UNDEFID)+(zid!=UNDEFID)) )
{
printf("xyz=%d zid=%d yid=%d xid=%d\n", ixyz, zid, yid, xid);
printf("zid=%d yid=%d xid=%d\n", zid, yid, xid);
Error("Internal problem, dimension order missing!");
}
......@@ -4098,9 +4095,6 @@ void cdfGetSliceSlapDescription(stream_t *streamptr, int varId, int levelId, boo
int gridId = vlistInqVarGrid(vlistId, varId);
int zaxisID = vlistInqVarZaxis(vlistId, varId);
int tsteptype = vlistInqVarTsteptype(vlistId, varId);
int ixyz = vlistInqVarXYZ(vlistId, varId);
if ( ixyz == 0 ) ixyz = 321; // ZYX
int gridsize = gridInqSize(gridId);
streamptr->numvals += gridsize;
......@@ -4146,9 +4140,7 @@ void cdfGetSliceSlapDescription(stream_t *streamptr, int varId, int levelId, boo
printf("2 %p %d %d %s\n", streamptr, zaxisindex, streamptr->zaxisID[zaxisindex], vlistInqVarNamePtr(vlistId, varId));
*/
int dimorder[3];
dimorder[0] = ixyz/100;
dimorder[1] = (ixyz-dimorder[0]*100)/10;
dimorder[2] = (ixyz-dimorder[0]*100-dimorder[1]*10);
vlistInqVarDimorder(vlistId, varId, &dimorder);
*outSwapXY = (dimorder[2] == 2 || dimorder[0] == 1) && xid != UNDEFID && yid != UNDEFID ;
......@@ -4281,7 +4273,6 @@ void cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtyp
int tsteptype;
int gridindex, zaxisindex;
int dimorder[3];
int ixyz;
int swapxy = FALSE;
int dtype;
int vlistID;
......@@ -4302,8 +4293,8 @@ void cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtyp
gridID = vlistInqVarGrid(vlistID, varID);
zaxisID = vlistInqVarZaxis(vlistID, varID);
tsteptype = vlistInqVarTsteptype(vlistID, varID);
ixyz = vlistInqVarXYZ(vlistID, varID);
if ( ixyz == 0 ) ixyz = 321; // ZYX
vlistInqVarDimorder(vlistID, varID, &dimorder);
gridindex = vlistGridIndex(vlistID, gridID);
if ( gridInqType(gridID) == GRID_TRAJECTORY )
......@@ -4319,14 +4310,9 @@ void cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtyp
zaxisindex = vlistZaxisIndex(vlistID, zaxisID);
zid = streamptr->zaxisID[zaxisindex];
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) && xid != UNDEFID && yid != UNDEFID ) swapxy = TRUE;
/*
printf("swapxy %d\n", swapxy);
printf("ixyz %d\n", ixyz);
printf("dimorder: %d %d %d\n", dimorder[0], dimorder[1], dimorder[2]);
*/
......
......@@ -37,7 +37,7 @@ void vlistvarInitEntry(int vlistID, int varID)
vlistptr->vars[varID].typeOfGeneratingProcess = 0;
vlistptr->vars[varID].productDefinitionTemplate = -1;
vlistptr->vars[varID].chunktype = cdiChunkType;
vlistptr->vars[varID].xyz = 0;
vlistptr->vars[varID].xyz = 321;
vlistptr->vars[varID].gridID = CDI_UNDEFID;
vlistptr->vars[varID].zaxisID = CDI_UNDEFID;
vlistptr->vars[varID].instID = CDI_UNDEFID;
......@@ -1580,6 +1580,21 @@ int vlistInqVarChunkType(int vlistID, int varID)
return (vlistptr->vars[varID].chunktype);
}
static
int vlistEncodeXyz(int (*dimorder)[3])
{
return (*dimorder)[0]*100 + (*dimorder)[1]*10 + (*dimorder)[2];
}
static
void vlistDecodeXyz(int xyz, int (*outDimorder)[3])
{
(*outDimorder)[0] = xyz/100, xyz -= (*outDimorder)[0]*100;
(*outDimorder)[1] = xyz/10, xyz -= (*outDimorder)[1]*10;
(*outDimorder)[2] = xyz;
}
void vlistDefVarXYZ(int vlistID, int varID, int xyz)
{
......@@ -1592,32 +1607,47 @@ void vlistDefVarXYZ(int vlistID, int varID, int xyz)
/* check xyz dimension order */
{
int dimorder[3];
vlistDecodeXyz(xyz, &dimorder);
int dimx = 0, dimy = 0, dimz = 0;
dimorder[0] = xyz/100;
dimorder[1] = (xyz-dimorder[0]*100)/10;
dimorder[2] = (xyz-dimorder[0]*100-dimorder[1]*10);
for ( int id = 0; id < 3; ++id )
{
if ( dimorder[id] == 3 ) { dimz++; }
else if ( dimorder[id] == 2 ) { dimy++; }
else if ( dimorder[id] == 1 ) { dimx++; }
}
switch ( dimorder[id] )
{
case 1: dimx++; break;
case 2: dimy++; break;
case 3: dimz++; break;
default: dimorder[id] = 0; break; //Ensure that we assign a valid dimension to this position.
}
}
if ( dimz > 1 || dimy > 1 || dimx > 1 ) xyz = 321; // ZYX
else
{
int lchanged = 0;
if ( dimz == 0 ) for ( int id = 0; id < 3; ++id ) if ( dimorder[id] == 0 ) {dimorder[id] = 3; lchanged++; break;}
if ( dimy == 0 ) for ( int id = 0; id < 3; ++id ) if ( dimorder[id] == 0 ) {dimorder[id] = 2; lchanged++; break;}
if ( dimx == 0 ) for ( int id = 0; id < 3; ++id ) if ( dimorder[id] == 0 ) {dimorder[id] = 1; lchanged++; break;}
if ( lchanged ) xyz = dimorder[0]*100 + dimorder[1]*10 + dimorder[2];
if ( dimz == 0 ) for ( int id = 0; id < 3; ++id ) if ( dimorder[id] == 0 ) {dimorder[id] = 3; break;}
if ( dimy == 0 ) for ( int id = 0; id < 3; ++id ) if ( dimorder[id] == 0 ) {dimorder[id] = 2; break;}
if ( dimx == 0 ) for ( int id = 0; id < 3; ++id ) if ( dimorder[id] == 0 ) {dimorder[id] = 1; break;}
xyz = vlistEncodeXyz(&dimorder);
}
}
assert(xyz == 123 || xyz == 312 || xyz == 231 || xyz == 321 || xyz == 132 || xyz == 213);
vlistptr->vars[varID].xyz = xyz;
reshSetStatus(vlistID, &vlist_ops, RESH_DESYNC_IN_USE);
}
void vlistInqVarDimorder(int vlistID, int varID, int (*outDimorder)[3])
{
vlist_t *vlistptr;
vlistptr = vlist_to_pointer(vlistID);
vlistCheckVarID(__func__, vlistID, varID);
vlistDecodeXyz(vlistptr->vars[varID].xyz, outDimorder);
}
int vlistInqVarXYZ(int vlistID, int varID)
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
......
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