Commit 49c0a5ca authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

netCDF4: added support for xtype NC_STRING

parent 88625071
......@@ -3,6 +3,10 @@
* Version 1.7.0 released
* using CGRIBEX library version 1.7.0
2014-07-31 Uwe Schulzweida
* netCDF4: added support for xtype NC_STRING
2014-07-25 Uwe Schulzweida
* vlistInqAtt: return -1 if attnum is out of range
......
......@@ -723,7 +723,7 @@ void cdf_put_att_double(int ncid, int varid, const char *name, nc_type xtype,
}
void cdf_get_att_text(int ncid, int varid, char *name, char *tp)
void cdf_get_att_text(int ncid, int varid, const char *name, char *tp)
{
int status;
......@@ -735,8 +735,22 @@ void cdf_get_att_text(int ncid, int varid, char *name, char *tp)
if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
}
void cdf_get_att_string(int ncid, int varid, const char *name, char **tp)
{
#if defined (HAVE_NETCDF4)
int status;
status = nc_get_att_string(ncid, varid, name, tp);
if ( CDF_Debug || status != NC_NOERR )
Message("ncid = %d varid = %d name = %s", ncid, varid, name);
if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
#endif
}
void cdf_get_att_int(int ncid, int varid, char *name, int *ip)
void cdf_get_att_int(int ncid, int varid, const char *name, int *ip)
{
int status;
......@@ -749,7 +763,7 @@ void cdf_get_att_int(int ncid, int varid, char *name, int *ip)
}
void cdf_get_att_double(int ncid, int varid, char *name, double *dp)
void cdf_get_att_double(int ncid, int varid, const char *name, double *dp)
{
int status;
......
......@@ -26,9 +26,9 @@ void cdf_def_var (int ncid, const char *name, nc_type xtype, int ndims,
const int dimids[], int *varidp);
void cdf_def_var_serial(int ncid, const char *name, nc_type xtype, int ndims,
const int dimids[], int *varidp);
void cdf_inq_varid (int ncid, const char *name, int *varidp);
void cdf_inq_nvars (int ncid, int *nvarsp);
void cdf_inq_var (int ncid, int varid, char *name, nc_type *xtypep,
void cdf_inq_varid(int ncid, const char *name, int *varidp);
void cdf_inq_nvars(int ncid, int *nvarsp);
void cdf_inq_var(int ncid, int varid, char *name, nc_type *xtypep,
int *ndimsp, int dimids[], int *nattsp);
void cdf_inq_varname (int ncid, int varid, char *name);
void cdf_inq_vartype (int ncid, int varid, nc_type *xtypep);
......@@ -73,23 +73,23 @@ void cdf_get_vara_float(int ncid, int varid, const size_t start[],
void cdf_put_vara_float(int ncid, int varid, const size_t start[],
const size_t count[], const float *fp);
void cdf_put_att_text (int ncid, int varid, const char *name, size_t len,
void cdf_put_att_text(int ncid, int varid, const char *name, size_t len,
const char *tp);
void cdf_put_att_int (int ncid, int varid, const char *name, nc_type xtype,
void cdf_put_att_int(int ncid, int varid, const char *name, nc_type xtype,
size_t len, const int *ip);
void cdf_put_att_double (int ncid, int varid, const char *name, nc_type xtype,
void cdf_put_att_double(int ncid, int varid, const char *name, nc_type xtype,
size_t len, const double *dp);
void cdf_get_att_text (int ncid, int varid, char *name, char *tp);
void cdf_get_att_int (int ncid, int varid, char *name, int *ip);
void cdf_get_att_double (int ncid, int varid, char *name, double *dp);
void cdf_get_att_string(int ncid, int varid, const char *name, char **tp);
void cdf_get_att_text (int ncid, int varid, const char *name, char *tp);
void cdf_get_att_int (int ncid, int varid, const char *name, int *ip);
void cdf_get_att_double(int ncid, int varid, const char *name, double *dp);
void cdf_inq_att (int ncid, int varid, const char *name, nc_type * xtypep,
size_t * lenp);
void cdf_inq_atttype (int ncid, int varid, const char *name, nc_type *xtypep);
void cdf_inq_att (int ncid, int varid, const char *name, nc_type * xtypep, size_t * lenp);
void cdf_inq_atttype(int ncid, int varid, const char *name, nc_type *xtypep);
void cdf_inq_attlen (int ncid, int varid, const char *name, size_t *lenp);
void cdf_inq_attname (int ncid, int varid, int attnum, char *name);
void cdf_inq_attid (int ncid, int varid, const char *name, int *attnump);
void cdf_inq_attname(int ncid, int varid, int attnum, char *name);
void cdf_inq_attid (int ncid, int varid, const char *name, int *attnump);
typedef int (*cdi_nc__create_funcp)(const char *path, int cmode,
size_t initialsz, size_t *chunksizehintp,
......
......@@ -414,31 +414,75 @@ void cdfGetAttDouble(int fileID, int ncvarid, char *attname, int attlen, double
static
void cdfGetAttText(int fileID, int ncvarid, char *attname, int attlen, char *atttext)
{
nc_type atttype;
size_t nc_attlen;
char attbuf[65636];
cdf_inq_atttype(fileID, ncvarid, attname, &atttype);
cdf_inq_attlen(fileID, ncvarid, attname, &nc_attlen);
if ( nc_attlen < sizeof(attbuf) )
if ( atttype == NC_CHAR )
{
cdf_get_att_text(fileID, ncvarid, attname, attbuf);
char attbuf[65636];
if ( nc_attlen < sizeof(attbuf) )
{
cdf_get_att_text(fileID, ncvarid, attname, attbuf);
attbuf[nc_attlen++] = 0;
attbuf[nc_attlen++] = 0;
if ( (int) nc_attlen > attlen ) nc_attlen = attlen;
memcpy(atttext, attbuf, nc_attlen);
if ( (int) nc_attlen > attlen ) nc_attlen = attlen;
memcpy(atttext, attbuf, nc_attlen);
}
else
{
atttext[0] = 0;
}
}
else
#if defined (HAVE_NETCDF4)
else if ( atttype == NC_STRING )
{
atttext[0] = 0;
if ( nc_attlen == 1 )
{
char *attbuf = NULL;
cdf_get_att_string(fileID, ncvarid, attname, &attbuf);
int slen = (int) strlen(attbuf);
slen++;
if ( slen > attlen ) slen = attlen;
memcpy(atttext, attbuf, slen);
free(attbuf);
}
else
{
atttext[0] = 0;
}
}
#endif
}
static
int xtypeIsText(int xtype)
{
int isText = FALSE;
if ( xtype == NC_CHAR )
isText = TRUE;
#if defined (HAVE_NETCDF4)
else if ( xtype == NC_STRING )
isText = TRUE;
#endif
return (isText);
}
static
int xtypeIsFloat(int xtype)
{
int isFloat = FALSE;
if ( xtype == NC_FLOAT || xtype == NC_DOUBLE ) isFloat = TRUE;
return isFloat;
}
......@@ -4928,15 +4972,15 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
cdf_inq_atttype(ncid, ncvarid, attname, &atttype);
cdf_inq_attlen(ncid, ncvarid, attname, &attlen);
if ( strcmp(attname, "long_name") == 0 && atttype == NC_CHAR )
if ( strcmp(attname, "long_name") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, CDI_MAX_NAME, ncvars[ncvarid].longname);
}
else if ( strcmp(attname, "standard_name") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "standard_name") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, CDI_MAX_NAME, ncvars[ncvarid].stdname);
}
else if ( strcmp(attname, "units") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "units") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, CDI_MAX_NAME, ncvars[ncvarid].units);
}
......@@ -4944,7 +4988,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
{
ncvars[ncvarid].calendar = TRUE;
}
else if ( strcmp(attname, "param") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "param") == 0 && xtypeIsText(atttype) )
{
char paramstr[32];
int pnum = 0, pcat = 255, pdis = 255;
......@@ -4953,12 +4997,12 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
ncvars[ncvarid].param = cdiEncodeParam(pnum, pcat, pdis);
cdfSetVar(ncvars, ncvarid, TRUE);
}
else if ( strcmp(attname, "code") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "code") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttInt(ncid, ncvarid, attname, 1, &ncvars[ncvarid].code);
cdfSetVar(ncvars, ncvarid, TRUE);
}
else if ( strcmp(attname, "table") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "table") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttInt(ncid, ncvarid, attname, 1, &tablenum);
if ( tablenum > 0 )
......@@ -4970,13 +5014,13 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
}
cdfSetVar(ncvars, ncvarid, TRUE);
}
else if ( strcmp(attname, "trunc_type") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "trunc_type") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, attstringlen-1, attstring);
if ( memcmp(attstring, "Triangular", attlen) == 0 )
ncvars[ncvarid].gridtype = GRID_SPECTRAL;
}
else if ( strcmp(attname, "grid_type") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "grid_type") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, attstringlen-1, attstring);
strtolower(attstring);
......@@ -5019,7 +5063,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
cdfSetVar(ncvars, ncvarid, TRUE);
}
else if ( strcmp(attname, "level_type") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "level_type") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, attstringlen-1, attstring);
strtolower(attstring);
......@@ -5058,19 +5102,19 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
cdfSetVar(ncvars, ncvarid, TRUE);
}
else if ( strcmp(attname, "trunc_count") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "trunc_count") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttInt(ncid, ncvarid, attname, 1, &ncvars[ncvarid].truncation);
}
else if ( strcmp(attname, "truncation") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "truncation") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttInt(ncid, ncvarid, attname, 1, &ncvars[ncvarid].truncation);
}
else if ( strcmp(attname, "number_of_grid_in_reference") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "number_of_grid_in_reference") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttInt(ncid, ncvarid, attname, 1, &ncvars[ncvarid].position);
}
else if ( strcmp(attname, "add_offset") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "add_offset") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttDouble(ncid, ncvarid, attname, 1, &ncvars[ncvarid].addoffset);
/*
......@@ -5080,7 +5124,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
*/
/* (also used for lon/lat) cdfSetVar(ncvars, ncvarid, TRUE); */
}
else if ( strcmp(attname, "scale_factor") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "scale_factor") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttDouble(ncid, ncvarid, attname, 1, &ncvars[ncvarid].scalefactor);
/*
......@@ -5090,7 +5134,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
*/
/* (also used for lon/lat) cdfSetVar(ncvars, ncvarid, TRUE); */
}
else if ( strcmp(attname, "climatology") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "climatology") == 0 && xtypeIsText(atttype) )
{
int status, ncboundsid;
......@@ -5108,7 +5152,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
else
Warning("%s - %s", nc_strerror(status), attstring);
}
else if ( strcmp(attname, "bounds") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "bounds") == 0 && xtypeIsText(atttype) )
{
int status, ncboundsid;
......@@ -5125,7 +5169,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
else
Warning("%s - %s", nc_strerror(status), attstring);
}
else if ( strcmp(attname, "cell_measures") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "cell_measures") == 0 && xtypeIsText(atttype) )
{
char *pstring, *cell_measures = NULL, *cell_var = NULL;
......@@ -5190,7 +5234,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
cdfSetVar(ncvars, ncvarid, TRUE);
}
*/
else if ( (strcmp(attname, "associate") == 0 || strcmp(attname, "coordinates") == 0) && atttype == NC_CHAR )
else if ( (strcmp(attname, "associate") == 0 || strcmp(attname, "coordinates") == 0) && xtypeIsText(atttype) )
{
int status;
char *pstring, *varname = NULL;
......@@ -5228,7 +5272,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
cdfSetVar(ncvars, ncvarid, TRUE);
}
else if ( (strcmp(attname, "auxiliary_variable") == 0) && atttype == NC_CHAR )
else if ( (strcmp(attname, "auxiliary_variable") == 0) && xtypeIsText(atttype) )
{
int status;
char *pstring, *varname = NULL;
......@@ -5266,7 +5310,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
cdfSetVar(ncvars, ncvarid, TRUE);
}
else if ( strcmp(attname, "grid_mapping") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "grid_mapping") == 0 && xtypeIsText(atttype) )
{
int status;
int nc_gmap_id;
......@@ -5284,7 +5328,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
cdfSetVar(ncvars, ncvarid, TRUE);
}
else if ( strcmp(attname, "positive") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "positive") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, attstringlen-1, attstring);
strtolower(attstring);
......@@ -5299,13 +5343,13 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
ncdims[ncvars[ncvarid].dimids[0]].dimtype = Z_AXIS;
}
}
else if ( strcmp(attname, "_FillValue") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "_FillValue") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttDouble(ncid, ncvarid, attname, 1, &ncvars[ncvarid].fillval);
ncvars[ncvarid].deffillval = TRUE;
/* cdfSetVar(ncvars, ncvarid, TRUE); */
}
else if ( strcmp(attname, "missing_value") == 0 && atttype != NC_CHAR )
else if ( strcmp(attname, "missing_value") == 0 && !xtypeIsText(atttype) )
{
cdfGetAttDouble(ncid, ncvarid, attname, 1, &ncvars[ncvarid].missval);
ncvars[ncvarid].defmissval = TRUE;
......@@ -5368,7 +5412,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
}
}
}
else if ( strcmp(attname, "_Unsigned") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "_Unsigned") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, attstringlen-1, attstring);
strtolower(attstring);
......@@ -5384,7 +5428,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
}
/* cdfSetVar(ncvars, ncvarid, TRUE); */
}
else if ( strcmp(attname, "cdi") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "cdi") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, attstringlen-1, attstring);
strtolower(attstring);
......@@ -5395,7 +5439,7 @@ void cdfScanVarAttributes(int nvars, ncvar_t *ncvars, ncdim_t *ncdims,
cdfSetVar(ncvars, ncvarid, FALSE);
}
}
else if ( strcmp(attname, "axis") == 0 && atttype == NC_CHAR )
else if ( strcmp(attname, "axis") == 0 && xtypeIsText(atttype) )
{
cdfGetAttText(ncid, ncvarid, attname, attstringlen-1, attstring);
attlen = strlen(attstring);
......@@ -6854,6 +6898,7 @@ void define_all_vars(stream_t *streamptr, int vlistID, int instID, int modelID,
cdf_inq_attname(ncid, ncvarid, attnum, attname);
cdf_inq_attlen(ncid, ncvarid, attname, &attlen);
cdf_inq_atttype(ncid, ncvarid, attname, &attrtype);
if ( attrtype == NC_SHORT || attrtype == NC_INT )
{
int *attint;
......@@ -6880,7 +6925,7 @@ void define_all_vars(stream_t *streamptr, int vlistID, int instID, int modelID,
printf("flt: %s.%s = %g\n", ncvars[ncvarid].name, attname, attflt[0]);
free(attflt);
}
else if ( attrtype == NC_CHAR )
else if ( xtypeIsText(attrtype) )
{
cdfGetAttText(ncid, ncvarid, attname, attstringlen-1, attstring);
vlistDefAttTxt(vlistID, varID, attname, (int)attlen, attstring);
......@@ -6994,7 +7039,7 @@ void scan_global_attributes(int fileID, int vlistID, stream_t *streamptr, int ng
cdf_inq_atttype(fileID, NC_GLOBAL, attname, &xtype);
cdf_inq_attlen(fileID, NC_GLOBAL, attname, &attlen);
if ( xtype == NC_CHAR )
if ( xtypeIsText(xtype) )
{
cdfGetAttText(fileID, NC_GLOBAL, attname, attstringlen-1, attstring);
......@@ -7157,7 +7202,7 @@ void find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid,
if ( ncvarid != streamptr->basetime.ncvarid &&
ncvars[ncvarid].ndims == 1 &&
timedimid == ncvars[ncvarid].dimids[0] &&
ncvars[ncvarid].xtype != NC_CHAR &&
!xtypeIsText(ncvars[ncvarid].xtype) &&
isTimeAxisUnits(ncvars[ncvarid].units) )
{
ncvars[ncvarid].isvar = FALSE;
......@@ -7181,7 +7226,7 @@ void find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid,
if ( ncvarid != streamptr->basetime.ncvarid &&
ncvars[ncvarid].ndims == 2 &&
timedimid == ncvars[ncvarid].dimids[0] &&
ncvars[ncvarid].xtype == NC_CHAR &&
!xtypeIsText(ncvars[ncvarid].xtype) &&
ncdims[ncvars[ncvarid].dimids[1]].len == 19 )
{
streamptr->basetime.ncvarid = ncvarid;
......@@ -7438,7 +7483,7 @@ int cdfInqContents(stream_t *streamptr)
continue;
}
if ( ncvars[ncvarid].xtype == NC_CHAR )
if ( xtypeIsText(ncvars[ncvarid].xtype) )
{
ncvars[ncvarid].isvar = 0;
continue;
......
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