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

cgribexlib.c update.

parents b55877e2 c36738e9
......@@ -3,6 +3,14 @@
* using CGRIBEX library version 1.9.4
* Version 1.9.8 released
2019-09-08 Uwe Schulzweida
* time axis: added support for datatype integer
2019-09-06 Uwe Schulzweida
* gribapiDefTime: adjust reference time if necessary (bug fix) [report: Helmut Haak]
2019-08-28 Uwe Schulzweida
* find_leadtime() bug fix
......
......@@ -158,13 +158,11 @@ static
int setBaseTime(const char *timeunits, taxis_t *taxis)
{
int taxistype = TAXIS_ABSOLUTE;
int64_t rdate = -1;
int rtime = -1;
size_t len = strlen(timeunits);
while ( isspace(*timeunits) && len ) { timeunits++; len--; }
char *restrict tu = (char *)Malloc((len+1) * sizeof(char));
char *tu = (char *)Malloc((len+1) * sizeof(char));
for ( size_t i = 0; i < len; i++ ) tu[i] = (char)tolower((int)timeunits[i]);
tu[len] = 0;
......@@ -182,8 +180,7 @@ int setBaseTime(const char *timeunits, taxis_t *taxis)
{
while ( isspace(tu[pos]) ) ++pos;
if ( strStartsWith(tu+pos, "since") )
taxistype = TAXIS_RELATIVE;
if ( strStartsWith(tu+pos, "since") ) taxistype = TAXIS_RELATIVE;
while ( pos < len && !isspace(tu[pos]) ) ++pos;
if ( tu[pos] )
......@@ -211,13 +208,13 @@ int setBaseTime(const char *timeunits, taxis_t *taxis)
}
else if ( taxistype == TAXIS_RELATIVE )
{
int64_t rdate = -1;
int rtime = -1;
scanTimeString(tu+pos, &rdate, &rtime);
taxis->rdate = rdate;
taxis->rtime = rtime;
if ( CDI_Debug )
Message("rdate = %lld rtime = %d", rdate, rtime);
if ( CDI_Debug ) Message("rdate = %lld rtime = %d", rdate, rtime);
}
}
}
......@@ -227,8 +224,7 @@ int setBaseTime(const char *timeunits, taxis_t *taxis)
Free(tu);
if ( CDI_Debug )
Message("taxistype = %d unit = %d", taxistype, timeunit);
if ( CDI_Debug ) Message("taxistype = %d unit = %d", taxistype, timeunit);
return 0;
}
......@@ -4101,7 +4097,8 @@ int cdfInqContents(stream_t *streamptr)
if ( ncvars[nctimevarid].units[0] )
ptaxisDefUnits(taxis, ncvars[nctimevarid].units);
int datatype = (ncvars[nctimevarid].xtype == NC_FLOAT) ? CDI_DATATYPE_FLT32 : CDI_DATATYPE_FLT64;
int xtype = ncvars[nctimevarid].xtype;
int datatype = (xtype == NC_INT) ? CDI_DATATYPE_INT32 : ((xtype == NC_FLOAT) ? CDI_DATATYPE_FLT32 : CDI_DATATYPE_FLT64);
ptaxisDefDatatype(taxis, datatype);
}
......
#ifdef HAVE_CONFIG_H
# include "config.h"
#include "config.h"
#endif
#ifdef HAVE_LIBNETCDF
......@@ -12,10 +12,10 @@
#include "stream_cdf.h"
#include "cdf_int.h"
static
int cdfDefTimeBounds(int fileID, int nctimevarid, int nctimedimid, const char *taxis_name, taxis_t* taxis)
{
int time_bndsid = -1;
int dims[2];
dims[0] = nctimedimid;
......@@ -48,6 +48,8 @@ int cdfDefTimeBounds(int fileID, int nctimevarid, int nctimedimid, const char *t
bndsAttValLen = tmpstrLen;
bndsAttVal = tmpstr;
}
int time_bndsid = -1;
cdf_def_var(fileID, bndsAttVal, NC_DOUBLE, 2, dims, &time_bndsid);
cdf_put_att_text(fileID, nctimevarid, bndsAttName, bndsAttValLen, bndsAttVal);
......@@ -137,9 +139,6 @@ void cdfDefCalendar(int fileID, int ncvarid, int calendar)
void cdfDefTime(stream_t* streamptr)
{
int time_varid;
int time_dimid;
int time_bndsid = -1;
static const char default_name[] = "time";
if ( streamptr->basetime.ncvarid != CDI_UNDEFID ) return;
......@@ -152,15 +151,18 @@ void cdfDefTime(stream_t* streamptr)
taxis_t *taxis = &streamptr->tsteps[0].taxis;
const char *taxis_name = (taxis->name && taxis->name[0]) ? taxis->name : default_name ;
int time_dimid;
cdf_def_dim(fileID, taxis_name, NC_UNLIMITED, &time_dimid);
streamptr->basetime.ncdimid = time_dimid;
const nc_type xtype = (taxis->datatype == CDI_DATATYPE_FLT32) ? NC_FLOAT : NC_DOUBLE;
cdf_def_var(fileID, taxis_name, xtype, 1, &time_dimid, &time_varid);
const int datatype = taxis->datatype;
const nc_type xtype = (datatype == CDI_DATATYPE_INT32) ? NC_INT : ((datatype == CDI_DATATYPE_FLT32) ? NC_FLOAT : NC_DOUBLE);
int time_varid;
cdf_def_var(fileID, taxis_name, xtype, 1, &time_dimid, &time_varid);
streamptr->basetime.ncvarid = time_varid;
#if defined (HAVE_NETCDF4)
#ifdef HAVE_NETCDF4
if ( streamptr->filetype == CDI_FILETYPE_NC4 || streamptr->filetype == CDI_FILETYPE_NC4C )
{
const size_t chunk = 512;
......@@ -176,7 +178,7 @@ void cdfDefTime(stream_t* streamptr)
if ( taxis->has_bounds )
{
time_bndsid = cdfDefTimeBounds(fileID, time_varid, time_dimid, taxis_name, taxis);
int time_bndsid = cdfDefTimeBounds(fileID, time_varid, time_dimid, taxis_name, taxis);
streamptr->basetime.ncvarboundsid = time_bndsid;
}
......@@ -184,12 +186,10 @@ void cdfDefTime(stream_t* streamptr)
cdfDefTimeUnits(unitstr, taxis);
size_t len = strlen(unitstr);
if (len) cdf_put_att_text(fileID, time_varid, "units", len, unitstr);
// if ( len && taxis->has_bounds ) cdf_put_att_text(fileID, time_bndsid, "units", len, unitstr);
if ( taxis->calendar != -1 )
{
cdfDefCalendar(fileID, time_varid, taxis->calendar);
// ( taxis->has_bounds ) cdfDefCalendar(fileID, time_bndsid, taxis->calendar);
}
if ( taxis->type == TAXIS_FORECAST )
......@@ -214,7 +214,6 @@ void cdfDefTime(stream_t* streamptr)
if ( streamptr->ncmode == 2 ) cdf_enddef(fileID);
}
#endif
/*
* Local Variables:
......
......@@ -746,8 +746,8 @@ int cgribexScanTimestep1(stream_t *streamptr)
unsigned nrecs = 0;
while (true)
{
size_t recsize = gribGetSize(fileID);
recpos = fileGetPos(fileID);
const size_t recsize = gribGetSize(fileID);
recpos = fileGetPos(fileID);
if ( recsize == 0 )
{
......@@ -854,7 +854,7 @@ int cgribexScanTimestep1(stream_t *streamptr)
taxis->unit = tunit;
taxis->type = fcast ? TAXIS_RELATIVE : TAXIS_ABSOLUTE;
int taxisID = taxisCreate(taxis->type);
const int taxisID = taxisCreate(taxis->type);
taxis->rdate = rdate;
taxis->rtime = rtime;
taxis->vdate = (int64_t)datetime0.date;
......@@ -916,7 +916,7 @@ int cgribexScanTimestep2(stream_t * streamptr)
{
if ( rindex > nrecords ) break;
size_t recsize = gribGetSize(fileID);
const size_t recsize = gribGetSize(fileID);
recpos = fileGetPos(fileID);
if ( recsize == 0 )
{
......@@ -1112,7 +1112,7 @@ int cgribexScanTimestep(stream_t * streamptr)
if ( rindex > nrecs ) break;
const size_t recsize = gribGetSize(fileID);
recpos = fileGetPos(fileID);
recpos = fileGetPos(fileID);
if ( recsize == 0 )
{
streamptr->ntsteps = streamptr->rtsteps + 1;
......@@ -1148,7 +1148,7 @@ int cgribexScanTimestep(stream_t * streamptr)
if ( rindex == 0 )
{
const int vlistID = streamptr->vlistID;
int taxisID = vlistInqTaxis(vlistID);
const int taxisID = vlistInqTaxis(vlistID);
if ( taxisInqType(taxisID) == TAXIS_RELATIVE )
{
taxis->type = TAXIS_RELATIVE;
......@@ -1520,8 +1520,14 @@ void cgribexDefTime(int *isec1, int vdate, int vtime, int tsteptype, int numavg,
if ( timetype == TAXIS_RELATIVE )
{
const int calendar = taxisInqCalendar(taxisID);
const int rdate = taxisInqRdate(taxisID);
const int rtime = taxisInqRtime(taxisID);
int rdate = taxisInqRdate(taxisID);
int rtime = taxisInqRtime(taxisID);
if (vdate < rdate || (vdate == rdate && vtime < rtime))
{
rdate = vdate;
rtime = vtime;
}
int factor = cgribexDefDateTime(isec1, timeunit, rdate, rtime);
int ip1 = 0, ip2 = 0;
......
......@@ -178,10 +178,10 @@ int gribapiGetValidityDateTime(grib_handle *gh, int64_t *vdate, int *vtime)
if ( rday > 0 )
{
static bool lprint = true;
extern int grib_calendar;
extern int CGRIBEX_grib_calendar;
int64_t julday;
int secofday;
encode_caldaysec(grib_calendar, ryear, rmonth, rday, rhour, rminute, rsecond, &julday, &secofday);
encode_caldaysec(CGRIBEX_grib_calendar, ryear, rmonth, rday, rhour, rminute, rsecond, &julday, &secofday);
int64_t time_period = endStep;
int64_t addsec = 0;
......@@ -205,7 +205,7 @@ int gribapiGetValidityDateTime(grib_handle *gh, int64_t *vdate, int *vtime)
julday_add_seconds(addsec, &julday, &secofday);
decode_caldaysec(grib_calendar, julday, secofday, &ryear, &rmonth, &rday, &rhour, &rminute, &rsecond);
decode_caldaysec(CGRIBEX_grib_calendar, julday, secofday, &ryear, &rmonth, &rday, &rhour, &rminute, &rsecond);
}
*vdate = cdiEncodeDate(ryear, rmonth, rday);
......@@ -953,7 +953,7 @@ int gribapiScanTimestep1(stream_t * streamptr)
while ( true )
{
int level1 = 0, level2 = 0;
size_t recsize = gribGetSize(fileID);
const size_t recsize = gribGetSize(fileID);
recpos = fileGetPos(fileID);
if ( recsize == 0 )
......@@ -1111,7 +1111,7 @@ int gribapiScanTimestep2(stream_t * streamptr)
{
if ( rindex > nrecords ) break;
size_t recsize = gribGetSize(fileID);
const size_t recsize = gribGetSize(fileID);
recpos = fileGetPos(fileID);
if ( recsize == 0 )
{
......@@ -1337,7 +1337,7 @@ int gribapiScanTimestep(stream_t * streamptr)
if ( rindex == 0 )
{
int taxisID = vlistInqTaxis(vlistID);
const int taxisID = vlistInqTaxis(vlistID);
if ( taxisInqType(taxisID) == TAXIS_RELATIVE )
{
taxis->type = TAXIS_RELATIVE;
......@@ -1839,10 +1839,16 @@ void gribapiDefTime(int editionNumber, int productDefinitionTemplate, int typeOf
if ( taxistype == TAXIS_RELATIVE )
{
const int calendar = taxisInqCalendar(taxisID);
const int64_t rdate = taxisInqRdate(taxisID);
const int rtime = taxisInqRtime(taxisID);
const int timeunit = taxisInqTunit(taxisID);
const int calendar = taxisInqCalendar(taxisID);
int64_t rdate = taxisInqRdate(taxisID);
int rtime = taxisInqRtime(taxisID);
if (vdate < rdate || (vdate == rdate && vtime < rtime))
{
rdate = vdate;
rtime = vtime;
}
int status = gribapiDefDateTimeRel(editionNumber, gh, rdate, rtime, vdate, vtime, productDefinitionTemplate,
typeOfGeneratingProcess, tsteptype, timeunit, calendar, gcinit);
......@@ -2834,7 +2840,7 @@ convertDataScanningMode(int scanModeIN, int scanModeOUT, double *data,
if (scanModeIN==00) // Scanning Mode (00 dec) +i, -j; i direction consecutive (row-major order West->East & South->North )
{ // Scanning Mode (64 dec) +i, +j; i direction consecutive (row-major order West->East & North->South )
// Scanning Mode (96 dec) +i, +j; j direction consecutive (column-major order North->South & West->East )
// Scanning Mode (96 dec) +i, +j; j direction consecutive (column-major order North->South & West->East )
if (scanModeOUT==64)
{ // flip the data vertically ..
idxIN= 0; idxOUT= (jDim-1)*iDim;
......
......@@ -104,35 +104,6 @@ const char *tunitNamePtr(int unitID)
return (unitID > 0 && unitID < size) ? Timeunits[unitID] : Timeunits[0];
}
#if 0
static
void taxis_defaults(void)
{
char *timeunit = getenv("TIMEUNIT");
if ( timeunit )
{
if ( strcmp(timeunit, "minutes") == 0 )
DefaultTimeUnit = TUNIT_MINUTE;
else if ( strcmp(timeunit, "hours") == 0 )
DefaultTimeUnit = TUNIT_HOUR;
else if ( strcmp(timeunit, "3hours") == 0 )
DefaultTimeUnit = TUNIT_3HOURS;
else if ( strcmp(timeunit, "6hours") == 0 )
DefaultTimeUnit = TUNIT_6HOURS;
else if ( strcmp(timeunit, "12hours") == 0 )
DefaultTimeUnit = TUNIT_12HOURS;
else if ( strcmp(timeunit, "days") == 0 )
DefaultTimeUnit = TUNIT_DAY;
else if ( strcmp(timeunit, "months") == 0 )
DefaultTimeUnit = TUNIT_MONTH;
else if ( strcmp(timeunit, "years") == 0 )
DefaultTimeUnit = TUNIT_YEAR;
else
Warning("Unsupported TIMEUNIT %s!", timeunit);
}
}
#endif
static
void taxisDefaultValue(taxis_t* taxisptr)
{
......@@ -295,6 +266,7 @@ void taxisDefType(int taxisID, int taxistype)
if ( taxisptr->type != taxistype )
{
taxisptr->type = taxistype;
taxisptr->datatype = CDI_DATATYPE_FLT64;
if ( taxisptr->units )
{
delete_refcount_string(taxisptr->units);
......
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