Commit 8522d5ed authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Added CDI function cdiZaxisDefKeyFlt() and cdiZaxisInqKeyFlt().

parent dd50a8aa
......@@ -57,8 +57,7 @@ cdfLazyGridDefArea(grid_t *grid, const double *area)
{
struct cdfLazyGrid *cdfGrid = (struct cdfLazyGrid *)grid;
lock_lazy_load(cdfGrid);
if (grid->area == cdfPendingLoad)
grid->area = NULL;
if (grid->area == cdfPendingLoad) grid->area = NULL;
cdfGrid->cellAreaGet.datasetNCId = -1;
cdfGrid->cellAreaGet.varNCId = -1;
cdfGrid->baseVtable->defArea(grid, area);
......@@ -93,8 +92,7 @@ cdfLazyGridInqArea(grid_t *grid, double *area)
static void
cdfLazyLoadXYVals(struct xyValGet *valsGet, double **valsp)
{
double *grid_vals
= (double *)Malloc(valsGet->size * sizeof (double));
double *grid_vals = (double *)Malloc(valsGet->size * sizeof (double));
*valsp = grid_vals;
if ( valsGet->ndims == 3 )
cdf_get_vara_double(valsGet->datasetNCId, valsGet->varNCId,
......
......@@ -813,6 +813,8 @@ int gridInqYvals(int gridID, double yvals[]);
#define CDI_KEY_LONGNAME 943 // Z-axis longname
#define CDI_KEY_UNITS 944 // Z-axis units
#define CDI_KEY_PSNAME 950 // Z-axis surface pressure name
#define CDI_KEY_P0NAME 951 // Z-axis reference pressure name
#define CDI_KEY_P0VALUE 952 // Z-axis reference pressure in Pa
// cdiGridDefKeyStr: Define a CDI grid string value from a key
int cdiGridDefKeyStr(int gridID, int key, int size, const char *mesg);
......@@ -820,6 +822,12 @@ int cdiGridDefKeyStr(int gridID, int key, int size, const char *mesg);
// cdiGridInqKeyStr: Get a CDI grid string value from a key
int cdiGridInqKeyStr(int gridID, int key, int size, char *mesg);
// cdiZaxisDefKeyFlt: Define a CDI Z-axis floating point value from a key
int cdiZaxisDefKeyFlt(int zaxisID, int key, double value);
// cdiZaxisInqKeyFlt: Get a CDI Z-axis floating point value from a key
int cdiZaxisInqKeyFlt(int zaxisID, int key, double *value);
/* gridDefXname: Define the name of a X-axis */
void gridDefXname(int gridID, const char *xname);
......
......@@ -738,13 +738,12 @@ void scan_hybrid_formulaterms(int ncid, int ncfvarid, int *avarid, int *bvarid,
*bvarid = -1;
*psvarid = -1;
*p0varid = -1;
char attstring[1024];
bool lstop = false;
int dimvarid;
char attstring[1024];
cdfGetAttText(ncid, ncfvarid, "formula_terms", sizeof(attstring), attstring);
char *pstring = attstring;
bool lstop = false;
for ( int i = 0; i < 4; i++ )
{
while ( isspace((int) *pstring) ) pstring++;
......@@ -761,6 +760,7 @@ void scan_hybrid_formulaterms(int ncid, int ncfvarid, int *avarid, int *bvarid,
if ( *pstring == 0 ) lstop = true;
*pstring++ = 0;
int dimvarid;
int status_nc = nc_inq_varid(ncid, varname, &dimvarid);
if ( status_nc == NC_NOERR )
{
......@@ -2698,6 +2698,7 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
int zvarid = UNDEFID;
int zsize = 1;
int psvarid = -1;
int p0varid = -1;
double *lbounds = NULL;
double *ubounds = NULL;
......@@ -2753,6 +2754,7 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
vctsize = ncvars[zvarid].vctsize;
if ( ncvars[zvarid].psvarid != -1 ) psvarid = ncvars[zvarid].psvarid;
if ( ncvars[zvarid].p0varid != -1 ) p0varid = ncvars[zvarid].p0varid;
}
zvar = (double*) Malloc((size_t)zsize*sizeof(double));
......@@ -2805,8 +2807,18 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
if ( uuidOfVGrid[0] != 0 )
zaxisDefUUID(zaxisID, uuidOfVGrid);
if ( zaxisType == ZAXIS_HYBRID && psvarid != -1 )
cdiZaxisDefKeyStr(zaxisID, CDI_KEY_PSNAME, strlen(ncvars[psvarid].name)+1, ncvars[psvarid].name);
if ( zaxisType == ZAXIS_HYBRID )
{
if ( psvarid != -1 )
cdiZaxisDefKeyStr(zaxisID, CDI_KEY_PSNAME, strlen(ncvars[psvarid].name)+1, ncvars[psvarid].name);
if ( p0varid != -1 )
{
double px = 1;
cdf_get_var_double(ncvars[p0varid].ncid, p0varid, &px);
cdiZaxisDefKeyFlt(zaxisID, CDI_KEY_P0VALUE, px);
cdiZaxisDefKeyStr(zaxisID, CDI_KEY_P0NAME, strlen(ncvars[p0varid].name)+1, ncvars[p0varid].name);
}
}
if ( positive > 0 ) zaxisDefPositive(zaxisID, positive);
if ( is_scalar ) zaxisDefScalar(zaxisID);
......
......@@ -1405,7 +1405,7 @@ void cdf_def_vct_echam(stream_t *streamptr, int zaxisID)
}
static
void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID)
void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID, int p0status, double p0value)
{
int type = zaxisInqType(zaxisID);
......@@ -1437,7 +1437,10 @@ void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID)
streamptr->vct.mlevID = nclevID;
streamptr->vct.ilevID = nclevID;
cdf_def_var(fileID, "ap", NC_DOUBLE, 1, dimIDs, &hyamid);
if ( p0status == 0 )
cdf_def_var(fileID, "a", NC_DOUBLE, 1, dimIDs, &hyamid);
else
cdf_def_var(fileID, "ap", NC_DOUBLE, 1, dimIDs, &hyamid);
cdf_def_var(fileID, "b", NC_DOUBLE, 1, dimIDs, &hybmid);
{
......@@ -1459,7 +1462,10 @@ void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID)
if ( ncbndsID != -1 )
{
cdf_def_var(fileID, "ap_bnds", NC_DOUBLE, 2, dimIDs, &hyaiid);
if ( p0status == 0 )
cdf_def_var(fileID, "a_bnds", NC_DOUBLE, 2, dimIDs, &hyaiid);
else
cdf_def_var(fileID, "ap_bnds", NC_DOUBLE, 2, dimIDs, &hyaiid);
cdf_def_var(fileID, "b_bnds", NC_DOUBLE, 2, dimIDs, &hybiid);
{
static const char lname[] = "vertical coordinate formula term: ap(k+1/2)";
......@@ -1482,32 +1488,38 @@ void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID)
cdf_enddef(fileID);
streamptr->ncmode = 2;
const double *vctptr = zaxisInqVctPtr(zaxisID);
int vctsize = zaxisInqVctSize(zaxisID);
double vct[vctsize];
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];
if ( ncbndsID != -1 )
{
for ( int i = 0; i < mlev; ++i )
{
tarray[2*i ] = vctptr[i];
tarray[2*i+1] = vctptr[i+1];
tarray[2*i ] = vct[i];
tarray[2*i+1] = vct[i+1];
}
cdf_put_var_double(fileID, hyaiid, tarray);
for ( int i = 0; i < mlev; ++i )
{
tarray[2*i ] = vctptr[ilev+i];
tarray[2*i+1] = vctptr[ilev+i+1];
tarray[2*i ] = vct[ilev+i];
tarray[2*i+1] = vct[ilev+i+1];
}
cdf_put_var_double(fileID, hybiid, tarray);
}
for ( int i = 0; i < mlev; ++i )
tarray[i] = (vctptr[i] + vctptr[i+1]) * 0.5;
tarray[i] = (vct[i] + vct[i+1]) * 0.5;
cdf_put_var_double(fileID, hyamid, tarray);
for ( int i = 0; i < mlev; ++i )
tarray[i] = (vctptr[ilev+i] + vctptr[ilev+i+1]) * 0.5;
tarray[i] = (vct[ilev+i] + vct[ilev+i+1]) * 0.5;
cdf_put_var_double(fileID, hybmid, tarray);
}
}
......@@ -1590,12 +1602,27 @@ void cdf_def_zaxis_hybrid_echam(stream_t *streamptr, int type, int *ncvaridp, in
static
void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int zaxisID, int zaxisindex, int xtype, size_t dimlen, int *dimID, char *axisname)
{
int fileID = streamptr->fileID;
if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
char psname[CDI_MAX_NAME]; psname[0] = 0;
cdiZaxisInqKeyStr(zaxisID, CDI_KEY_PSNAME, CDI_MAX_NAME, psname);
if ( psname[0] == 0 ) strcpy(psname, "ps");
int fileID = streamptr->fileID;
if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
char p0name[CDI_MAX_NAME]; p0name[0] = 0;
double p0value = 1;
int p0varid = UNDEFID;
int p0status = cdiZaxisInqKeyFlt(zaxisID, CDI_KEY_P0VALUE, &p0value);
if ( p0status == 0 )
{
cdiZaxisInqKeyStr(zaxisID, CDI_KEY_P0NAME, CDI_MAX_NAME, p0name);
if ( p0name[0] == 0 ) strcpy(p0name, "p0");
cdf_def_var(fileID, p0name, NC_DOUBLE, 0, 0, &p0varid);
static const char longname[] = "reference pressure";
cdf_put_att_text(fileID, p0varid, "long_name", strlen(longname), longname);
static const char units[] = "Pa";
cdf_put_att_text(fileID, p0varid, "units", strlen(units), units);
}
char zname[CDI_MAX_NAME]; zname[0] = 0;
char zlongname[CDI_MAX_NAME]; zlongname[0] = 0;
......@@ -1631,11 +1658,14 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
cdf_put_att_text(fileID, ncvarid, "long_name", strlen(zlongname), zlongname);
cdf_put_att_text(fileID, ncvarid, "units", strlen(zunits), zunits);
}
{
char txt[CDI_MAX_NAME];
size_t len = (size_t)(sprintf(txt, "%s%s", "ap: ap b: b ps: ", psname));
cdf_put_att_text(fileID, ncvarid, "formula_terms", len, txt);
}
size_t len = 0;
char txt[CDI_MAX_NAME];
if ( p0status == 0 )
len = (size_t)(sprintf(txt, "%s%s %s%s", "a: a b: b p0: ", p0name, "ps: ", psname));
else
len = (size_t)(sprintf(txt, "%s%s", "ap: ap b: b ps: ", psname));
cdf_put_att_text(fileID, ncvarid, "formula_terms", len, txt);
int ncbvarid = UNDEFID;
int nvdimID = UNDEFID;
......@@ -1685,11 +1715,12 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
cdf_put_att_text(fileID, ncbvarid, tab[i].attName, tab[i].valLen, tab[i].attVal);
cdf_put_att_text(fileID, ncbvarid, "units", strlen(zunits), zunits);
}
{
char txt[CDI_MAX_NAME];
size_t len = (size_t)(sprintf(txt, "%s%s", "ap: ap_bnds b: b_bnds ps: ", psname));
cdf_put_att_text(fileID, ncbvarid, "formula_terms", len, txt);
}
if ( p0status == 0 )
len = (size_t)(sprintf(txt, "%s%s %s%s", "a: a_bnds b: b_bnds p0: ", p0name, "ps: ", psname));
else
len = (size_t)(sprintf(txt, "%s%s", "ap: ap_bnds b: b_bnds ps: ", psname));
cdf_put_att_text(fileID, ncbvarid, "formula_terms", len, txt);
}
}
......@@ -1698,6 +1729,8 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
cdf_put_var_double(fileID, ncvarid, levels);
if ( p0varid != UNDEFID ) cdf_put_var_double(fileID, p0varid, &p0value);
if ( ncbvarid != UNDEFID )
{
double zbounds[2*dimlen];
......@@ -1709,7 +1742,7 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
cdf_put_var_double(fileID, ncbvarid, zbounds);
}
cdf_def_vct_cf(streamptr, zaxisID, *dimID, nvdimID);
cdf_def_vct_cf(streamptr, zaxisID, *dimID, nvdimID, p0status, p0value);
if ( *dimID == UNDEFID )
{
......@@ -1752,13 +1785,13 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
size_t dimlen = (size_t)zaxisInqSize(zaxisID);
int type = zaxisInqType(zaxisID);
int is_scalar = FALSE;
bool is_scalar = false;
if ( dimlen == 1 )
{
is_scalar = zaxisInqScalar(zaxisID);
is_scalar = zaxisInqScalar(zaxisID) > 0;
if ( !is_scalar && CDI_cmor_mode )
{
is_scalar = TRUE;
is_scalar = true;
zaxisDefScalar(zaxisID);
}
}
......@@ -1791,8 +1824,7 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
{
checkZaxisName(axisname, fileID, vlistID, zaxisID, nzaxis);
char dimname[CDI_MAX_NAME+3];
dimname[0] = 0;
char dimname[CDI_MAX_NAME+3]; dimname[0] = 0;
//cdiZaxisInqKeyStr(zaxisID, CDI_KEY_DIMNAME, CDI_MAX_NAME, dimname);
if ( dimname[0] == 0 ) strcpy(dimname, axisname);
......@@ -1801,6 +1833,12 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
if ( type == ZAXIS_HYBRID || type == ZAXIS_HYBRID_HALF )
{
cdf_def_zaxis_hybrid(streamptr, type, &ncvarid, zaxisID, zaxisindex, xtype, dimlen, &dimID, axisname);
int natts;
cdiInqNatts(zaxisID, CDI_GLOBAL, &natts);
if ( natts > 0 && streamptr->ncmode == 2 ) cdf_redef(fileID);
cdfDefineAttributes(zaxisID, CDI_GLOBAL, fileID, ncvarid);
if ( natts > 0 && streamptr->ncmode == 2 ) cdf_enddef(fileID);
}
else
{
......@@ -1849,6 +1887,8 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
cdf_put_att_text(fileID, ncvarid, "bounds", strlen(axisname), axisname);
}
}
cdfDefineAttributes(zaxisID, CDI_GLOBAL, fileID, ncvarid);
}
cdf_enddef(fileID);
......@@ -1873,17 +1913,6 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
}
if ( ndims == 0 ) streamptr->nczvarID[zaxisindex] = ncvarid;
{
int natts;
cdiInqNatts(zaxisID, CDI_GLOBAL, &natts);
if ( natts > 0 && streamptr->ncmode == 2 ) cdf_redef(fileID);
cdfDefineAttributes(zaxisID, CDI_GLOBAL, fileID, ncvarid);
if ( natts > 0 && streamptr->ncmode == 2 ) cdf_enddef(fileID);
}
}
}
}
......
......@@ -124,6 +124,8 @@ void zaxis_init(zaxis_t *zaxisptr)
zaxisptr->vdimname[0] = 0;
zaxisptr->units[0] = 0;
zaxisptr->psname[0] = 0;
zaxisptr->p0name[0] = 0;
zaxisptr->p0value.defined = false;
zaxisptr->vals = NULL;
zaxisptr->ubounds = NULL;
zaxisptr->lbounds = NULL;
......@@ -348,6 +350,8 @@ void *zaxis_key_to_ptr(zaxis_t *zaxisptr, int key)
case CDI_KEY_DIMNAME: keyptr = (void*)zaxisptr->dimname; break;
case CDI_KEY_VDIMNAME: keyptr = (void*)zaxisptr->vdimname; break;
case CDI_KEY_PSNAME: keyptr = (void*)zaxisptr->psname; break;
case CDI_KEY_P0NAME: keyptr = (void*)zaxisptr->p0name; break;
case CDI_KEY_P0VALUE: keyptr = (void*)&zaxisptr->p0value; break;
}
return keyptr;
......@@ -430,6 +434,79 @@ int cdiZaxisInqKeyStr(int zaxisID, int key, int size, char *mesg)
return 0;
}
/*
@Function cdiZaxisDefKeyFlt
@Title Define a CDI Z-axis floating point value from a key
@Prototype int cdiZaxisDefKeyFlt(int zaxisID, int key, double value)
@Parameter
@Item zaxisID Z-axis ID, from a previous call to @fref{zaxisCreate}.
@Item key The key to be searched
@Item value A double where the data will be read
@Description
The function @func{cdiZaxisDefKeyFlt} defines a CDI Z-axis double value from a key.
@Result
@func{cdiZaxisDefKeyFlt} returns 0 if OK and integer value on error.
@EndFunction
*/
int cdiZaxisDefKeyFlt(int zaxisID, int key, double value)
{
zaxis_t *zaxisptr = zaxis_to_pointer(zaxisID);
zkey_double_t *keyptr = (zkey_double_t*)zaxis_key_to_ptr(zaxisptr, key);
if ( keyptr == NULL)
{
Warning("CDI zaxis double key %d not supported!", key);
return -1;
}
keyptr->value = value;
keyptr->defined = true;
reshSetStatus(zaxisID, &zaxisOps, RESH_DESYNC_IN_USE);
return 0;
}
/*
@Function cdiZaxisInqKeyFlt
@Title Get a CDI Z-axis floating point value from a key
@Prototype int cdiZaxisInqKeyFlt(int zaxisID, int key, double *value)
@Parameter
@Item zaxisID Z-axis ID, from a previous call to @fref{zaxisCreate}.
@Item key The key to be searched.
@Item value The address of a double where the data will be retrieved.
@Description
The function @func{cdiZaxisInqKeyFlt} return a CDI Z-axis double value from a key.
@Result
@func{cdiZaxisInqKeyFlt} returns 0 if OK and integer value on error.
@EndFunction
*/
int cdiZaxisInqKeyFlt(int zaxisID, int key, double *value)
{
zaxis_t *zaxisptr = zaxis_to_pointer(zaxisID);
zkey_double_t *keyptr = (zkey_double_t*)zaxis_key_to_ptr(zaxisptr, key);
if ( keyptr == NULL)
{
Warning("CDI zaxis double key %d not supported!", key);
return -1;
}
if ( !keyptr->defined ) return 1;
*value = keyptr->value;
return 0;
}
/*
@Function zaxisDefName
@Title Define the name of a Z-axis
......
......@@ -3,6 +3,12 @@
#include "cdi_att.h"
typedef struct {
double value;
bool defined;
}
zkey_double_t;
typedef struct {
char dimname[CDI_MAX_NAME];
char vdimname[CDI_MAX_NAME];
......@@ -11,6 +17,8 @@ typedef struct {
char stdname[CDI_MAX_NAME];
char units[CDI_MAX_NAME];
char psname[CDI_MAX_NAME];
char p0name[CDI_MAX_NAME];
zkey_double_t p0value;
double *vals;
double *lbounds;
double *ubounds;
......
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