diff --git a/ChangeLog b/ChangeLog index b1a317e4e78f367c2a26655e77ce438b56c3f2b0..75ab17783cc8650f725f8d5e2eef44e3b4ad488f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,10 @@ * Version 2.1.0 released +2022-10-04 Uwe Schulzweida + + * cdi_encode_timeval: added support for TUNIT_SECOND + 2022-09-30 Uwe Schulzweida * install cmake files in <install>/lib/cmake/cdi diff --git a/src/cdf_util.c b/src/cdf_util.c index 14cdf0641bd160de50ddeaee1f57596497d11412..7735fbf1b51614374aedd7a0cdf8a12241747b6e 100644 --- a/src/cdf_util.c +++ b/src/cdf_util.c @@ -30,7 +30,7 @@ strStartsWith(const char *vstr, const char *cstr) } int -get_timeunit(size_t len, const char *ptu) +get_time_units(size_t len, const char *ptu) { int timeunit = -1; @@ -51,7 +51,7 @@ get_timeunit(size_t len, const char *ptu) else if (strStartsWith(ptu, "calendar_month")) timeunit = TUNIT_MONTH; else if (strStartsWith(ptu, "year")) timeunit = TUNIT_YEAR; } - else if (len == 1 && ptu[0] == 's') timeunit = TUNIT_SECOND; + else if (len == 1 && ptu[0] == 's') timeunit = TUNIT_SECOND; // clang-format on return timeunit; @@ -87,17 +87,15 @@ is_timeaxis_units(const char *timeunits) for (size_t i = 0; i < len; i++) ptu[i] = (char) tolower((int) ptu[i]); - int timeunit = get_timeunit(len, ptu); + int timeunit = get_time_units(len, ptu); if (timeunit != -1) { while (!isspace(*ptu) && *ptu != 0) ptu++; if (*ptu) { while (isspace(*ptu)) ptu++; - int timetype = strStartsWith(ptu, "as") ? TAXIS_ABSOLUTE : strStartsWith(ptu, "since") ? TAXIS_RELATIVE : -1; - - status = timetype != -1; + status = (timetype != -1); } } diff --git a/src/cdf_util.h b/src/cdf_util.h index 974e7dd619aac3751dac377ea4fac72c83eb1a49..22f5061a409255bdc6d012f98c34391d2322ca32 100644 --- a/src/cdf_util.h +++ b/src/cdf_util.h @@ -3,7 +3,7 @@ #include <stdbool.h> -int get_timeunit(size_t len, const char *ptu); +int get_time_units(size_t len, const char *ptu); bool is_time_units(const char *timeunits); bool is_timeaxis_units(const char *timeunits); diff --git a/src/stream_cdf_i.c b/src/stream_cdf_i.c index bd8c2bfedb29448a3eb85fd6470d6fc97191e020..9fefc3062c764969132f88fb0c4056aa53d733a0 100644 --- a/src/stream_cdf_i.c +++ b/src/stream_cdf_i.c @@ -171,13 +171,13 @@ static int scan_time_units(const char *unitstr) { const size_t len = strlen(unitstr); - const int timeunit = get_timeunit(len, unitstr); + const int timeunit = get_time_units(len, unitstr); if (timeunit == -1) Message("Unsupported TIMEUNIT: %s!", unitstr); return timeunit; } static void -setForecastTime(const char *timestr, taxis_t *taxis) +set_forecast_time(const char *timestr, taxis_t *taxis) { const size_t len = strlen(timestr); if (len != 0) @@ -191,26 +191,26 @@ setForecastTime(const char *timestr, taxis_t *taxis) } static int -setBaseTime(const char *timeunits, taxis_t *taxis) +set_base_time(const char *timeUnitsStr, taxis_t *taxis) { - int taxistype = TAXIS_ABSOLUTE; + int taxisType = TAXIS_ABSOLUTE; - size_t len = strlen(timeunits); - while (isspace(*timeunits) && len) + size_t len = strlen(timeUnitsStr); + while (isspace(*timeUnitsStr) && len) { - timeunits++; + timeUnitsStr++; len--; } char *tu = (char *) Malloc((len + 1) * sizeof(char)); - for (size_t i = 0; i < len; i++) tu[i] = (char) tolower((int) timeunits[i]); + for (size_t i = 0; i < len; i++) tu[i] = (char) tolower((int) timeUnitsStr[i]); tu[len] = 0; - int timeunit = get_timeunit(len, tu); - if (timeunit == -1) + int timeUnits = get_time_units(len, tu); + if (timeUnits == -1) { - Message("Unsupported TIMEUNIT: %s!", timeunits); + Message("Unsupported TIMEUNIT: %s!", timeUnitsStr); return 1; } @@ -220,37 +220,37 @@ 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]) { while (isspace(tu[pos])) ++pos; - if (taxistype == TAXIS_ABSOLUTE) + if (taxisType == TAXIS_ABSOLUTE) { - if (timeunit == TUNIT_DAY) + if (timeUnits == TUNIT_DAY) { if (!strStartsWith(tu + pos, "%y%m%d.%f")) { Warning("Unsupported format %s for TIMEUNIT day!", tu + pos); - timeunit = -1; + timeUnits = -1; } } - else if (timeunit == TUNIT_MONTH) + else if (timeUnits == TUNIT_MONTH) { if (!strStartsWith(tu + pos, "%y%m.%f")) { Warning("Unsupported format %s for TIMEUNIT month!", tu + pos); - timeunit = -1; + timeUnits = -1; } } - else if (timeunit == TUNIT_YEAR) + else if (timeUnits == TUNIT_YEAR) { if (!strStartsWith(tu + pos, "%y.%f")) { Warning("Unsupported format %s for TIMEUNIT year!", tu + pos); - timeunit = -1; + timeUnits = -1; } } else @@ -258,7 +258,7 @@ setBaseTime(const char *timeunits, taxis_t *taxis) Warning("Unsupported format for time units: %s!", tu); } } - else if (taxistype == TAXIS_RELATIVE) + else if (taxisType == TAXIS_RELATIVE) { taxis->rdatetime = scan_time_string(tu + pos); if (CDI_Debug) @@ -267,12 +267,12 @@ setBaseTime(const char *timeunits, taxis_t *taxis) } } - taxis->type = taxistype; - taxis->unit = timeunit; + taxis->type = taxisType; + taxis->unit = timeUnits; Free(tu); - if (CDI_Debug) Message("taxistype = %d unit = %d", taxistype, timeunit); + if (CDI_Debug) Message("taxisType = %d timeUnits = %d", taxisType, timeUnits); return 0; } @@ -3774,7 +3774,7 @@ find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid, strea if (timedimid == CDI_UNDEFID) { - char timeunits[CDI_MAX_NAME]; + char timeUnitsStr[CDI_MAX_NAME]; for (ncvarid = 0; ncvarid < nvars; ncvarid++) { @@ -3783,10 +3783,10 @@ find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid, strea { if (ncvar->units[0]) { - strcpy(timeunits, ncvar->units); - strToLower(timeunits); + strcpy(timeUnitsStr, ncvar->units); + strToLower(timeUnitsStr); - if (is_time_units(timeunits)) + if (is_time_units(timeUnitsStr)) { streamptr->basetime.ncvarid = ncvarid; break; @@ -4372,7 +4372,7 @@ cdfInqContents(stream_t *streamptr) { taxis_t *taxis = &streamptr->tsteps[0].taxis; - if (setBaseTime(ncvars[nctimevarid].units, taxis) == 1) + if (set_base_time(ncvars[nctimevarid].units, taxis) == 1) { nctimevarid = CDI_UNDEFID; streamptr->basetime.ncvarid = CDI_UNDEFID; @@ -4389,7 +4389,7 @@ cdfInqContents(stream_t *streamptr) if (timeunit == -1) timeunit = taxis->unit; taxis->fc_unit = timeunit; - setForecastTime(fcreftime, taxis); + set_forecast_time(fcreftime, taxis); } } diff --git a/src/stream_cdf_o.c b/src/stream_cdf_o.c index 0ffe3cad07ff2039df8bdf1034b1a7530f109460..fd9f977658edbe9bcbc28a110de508a4589bfeb9 100644 --- a/src/stream_cdf_o.c +++ b/src/stream_cdf_o.c @@ -62,35 +62,35 @@ cdfDefTimestep(stream_t *streamptr, int tsID) cdfEndDef(streamptr); } - double timevalue = cdi_encode_timeval(taxis->vdatetime, &streamptr->tsteps[0].taxis); - if (CDI_Debug) Message("tsID = %d timevalue = %f", tsID, timevalue); + double timeValue = cdi_encode_timeval(taxis->vdatetime, &streamptr->tsteps[0].taxis); + if (CDI_Debug) Message("tsID = %d timeValue = %f", tsID, timeValue); int ncvarid = streamptr->basetime.ncvarid; size_t index = (size_t) tsID; - cdf_put_var1_double(fileID, ncvarid, &index, &timevalue); + cdf_put_var1_double(fileID, ncvarid, &index, &timeValue); if (taxis->has_bounds) { ncvarid = streamptr->basetime.ncvarboundsid; if (ncvarid == CDI_UNDEFID) Error("Call to taxisWithBounds() missing!"); - timevalue = cdi_encode_timeval(taxis->vdatetime_lb, &streamptr->tsteps[0].taxis); + timeValue = cdi_encode_timeval(taxis->vdatetime_lb, &streamptr->tsteps[0].taxis); size_t start[2] = { tsID, 0 }, count[2] = { 1, 1 }; - cdf_put_vara_double(fileID, ncvarid, start, count, &timevalue); + cdf_put_vara_double(fileID, ncvarid, start, count, &timeValue); - timevalue = cdi_encode_timeval(taxis->vdatetime_ub, &streamptr->tsteps[0].taxis); + timeValue = cdi_encode_timeval(taxis->vdatetime_ub, &streamptr->tsteps[0].taxis); start[0] = (size_t) tsID; count[0] = 1; start[1] = 1; count[1] = 1; - cdf_put_vara_double(fileID, ncvarid, start, count, &timevalue); + cdf_put_vara_double(fileID, ncvarid, start, count, &timeValue); } ncvarid = streamptr->basetime.leadtimeid; if (taxis->type == TAXIS_FORECAST && ncvarid != CDI_UNDEFID) { - timevalue = taxis->fc_period; - cdf_put_var1_double(fileID, ncvarid, &index, &timevalue); + timeValue = taxis->fc_period; + cdf_put_var1_double(fileID, ncvarid, &index, &timeValue); } } @@ -454,7 +454,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims, bool if (IS_EQUAL(inqVal(gridID0, 0), inqVal(gridID, 0)) && IS_EQUAL(inqVal(gridID0, dimlen - 1), inqVal(gridID, dimlen - 1))) { - dimID = ncgrid[index].ncIDs[axisLetter == 'X' ? CDF_DIMID_X : CDF_DIMID_Y]; + dimID = ncgrid[index].ncIDs[(axisLetter == 'X') ? CDF_DIMID_X : CDF_DIMID_Y]; break; } } @@ -535,11 +535,11 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims, bool if (ncbvarid != CDI_UNDEFID) cdf_put_var_double(fileID, ncbvarid, pbounds); if (gen_bounds) Free(pbounds); - if (ndims == 0 || addVarToGrid) ncgrid[gridindex].ncIDs[axisLetter == 'X' ? CDF_VARID_X : CDF_VARID_Y] = ncvarid; + if (ndims == 0 || addVarToGrid) ncgrid[gridindex].ncIDs[(axisLetter == 'X') ? CDF_VARID_X : CDF_VARID_Y] = ncvarid; } ncgrid[gridindex].gridID = gridID; - ncgrid[gridindex].ncIDs[axisLetter == 'X' ? CDF_DIMID_X : CDF_DIMID_Y] = dimID; + ncgrid[gridindex].ncIDs[(axisLetter == 'X') ? CDF_DIMID_X : CDF_DIMID_Y] = dimID; } static void diff --git a/src/taxis.c b/src/taxis.c index aed811d545fe9f9db7051974432730cc58058bd5..811e48b367a8ab2252bb76c4e653be20f5d3d070 100644 --- a/src/taxis.c +++ b/src/taxis.c @@ -1372,31 +1372,37 @@ cdi_decode_timeval(double timevalue, taxis_t *taxis) double cdi_encode_timeval(CdiDateTime datetime, taxis_t *taxis) { - double timevalue; + double timeValue = 0.0; if (taxis->type == TAXIS_ABSOLUTE) { if (taxis->unit == TUNIT_YEAR) { - timevalue = datetime.date.year; + timeValue = datetime.date.year; } else if (taxis->unit == TUNIT_MONTH) { - int64_t xdate = cdiDate_get(datetime.date); - timevalue = xdate / 100 + copysign((double) (datetime.date.day != 0) * 0.5, (double) xdate); + const int64_t xdate = cdiDate_get(datetime.date); + timeValue = xdate / 100 + copysign((double) (datetime.date.day != 0) * 0.5, (double) xdate); + } + else if (taxis->unit == TUNIT_SECOND) + { + int hour, minute, second, ms; + cdiTime_decode(datetime.time, &hour, &minute, &second, &ms); + timeValue = hour * 3600 + minute * 60 + second; } else { int hour, minute, second, ms; cdiTime_decode(datetime.time, &hour, &minute, &second, &ms); - int64_t xdate = cdiDate_get(datetime.date); - timevalue = copysign(1.0, (double) xdate) * (fabs((double) xdate) + (hour * 3600 + minute * 60 + second) / 86400.0); + const int64_t xdate = cdiDate_get(datetime.date); + timeValue = copysign(1.0, (double) xdate) * (fabs((double) xdate) + (hour * 3600 + minute * 60 + second) / 86400.0); } } else - timevalue = datetime2rtimeval(datetime, taxis); + timeValue = datetime2rtimeval(datetime, taxis); - return timevalue; + return timeValue; } void