diff --git a/src/Makefile.am b/src/Makefile.am index 54faa81d6043b507eb8a0c0af443109ac5cc0a25..8ea10e3fcf9e375ce55e3b58d19349dd3cd4a592 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -161,6 +161,7 @@ libcdi_la_SOURCES = \ model.h \ namespace.c \ namespace.h \ + mormalize_month.h \ referenceCounting.c \ referenceCounting.h \ resource_handle.c \ diff --git a/src/calendar.h b/src/calendar.h index 3d0ef4bc8a92da4f1f67194fea1b5378dfac5372..6e2a6757abacb5a1185a49c8ddf7d69b6d0de80a 100644 --- a/src/calendar.h +++ b/src/calendar.h @@ -6,8 +6,6 @@ // clang-format off -#include <stdlib.h> - #include "cdi.h" #ifdef __cplusplus @@ -32,24 +30,10 @@ calendar_dpy(int calendar) int days_per_year(int calendar, int year); int days_per_month(int calendar, int year, int month); -struct yearMonth -{ - int year, month; -}; - -/* normalizes month to range [1,12] and adjusts year accordingly */ -static inline struct yearMonth -normalize_month(int year, int month) -{ - div_t modres = div(month - 1, 12); - year += modres.quot - ((month < 1) & (modres.rem != 0)); - return (struct yearMonth){ .year = year, .month = (modres.rem + 12) % 12 + 1 }; -} - #ifdef __cplusplus } #endif // clang-format on -#endif /* CALENDAR_H */ +#endif diff --git a/src/normalize_month.h b/src/normalize_month.h new file mode 100644 index 0000000000000000000000000000000000000000..d727de7bdf024c065cb0606a4206a283e4a8eb71 --- /dev/null +++ b/src/normalize_month.h @@ -0,0 +1,20 @@ +#ifndef NORMALIZE_MONTH_H +#define NORMALIZE_MONTH_H + +#include <stdlib.h> + +struct YearMonth +{ + int year, month; +}; + +/* normalizes month to range [1,12] and adjusts year accordingly */ +static inline struct YearMonth +normalize_month(int year, int month) +{ + div_t modres = div(month - 1, 12); + year += modres.quot - ((month < 1) & (modres.rem != 0)); + return (struct YearMonth){ .year = year, .month = (modres.rem + 12) % 12 + 1 }; +} + +#endif diff --git a/src/taxis.c b/src/taxis.c index 84d262c0503c11ca1ff02e45c3141624f5b74d99..576c1cb6e3f41b195adc666e7b65458a466bea8a 100644 --- a/src/taxis.c +++ b/src/taxis.c @@ -11,6 +11,7 @@ #include "serialize.h" #include "resource_handle.h" #include "resource_unpack.h" +#include "normalize_month.h" static int DefaultTimeType = TAXIS_ABSOLUTE; static int DefaultTimeUnit = TUNIT_HOUR; @@ -500,14 +501,14 @@ taxisDefNumavg(int taxisID, int numavg) int taxisInqType(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->type; } int taxisHasBounds(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->has_bounds; } @@ -564,7 +565,7 @@ taxisCopyTimestep(int taxisID2, int taxisID1) CdiDateTime taxisInqVdatetime(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->vDateTime; } @@ -601,21 +602,21 @@ The function @func{taxisInqVdate} returns the verification date of a Time axis. int taxisInqVdate(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return (int) cdiDate_get(taxisptr->vDateTime.date); } CdiDateTime taxisInqSdatetime(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->sDateTime; } void taxisInqVdateBounds(int taxisID, int *vdate_lb, int *vdate_ub) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); *vdate_lb = (int) cdiDate_get(taxisptr->vDateTime_lb.date); *vdate_ub = (int) cdiDate_get(taxisptr->vDateTime_ub.date); } @@ -623,7 +624,7 @@ taxisInqVdateBounds(int taxisID, int *vdate_lb, int *vdate_ub) void taxisInqVdatetimeBounds(int taxisID, CdiDateTime *vDateTime_lb, CdiDateTime *vDateTime_ub) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); *vDateTime_lb = taxisptr->vDateTime_lb; *vDateTime_ub = taxisptr->vDateTime_ub; } @@ -677,14 +678,14 @@ The function @func{taxisInqVtime} returns the verification time of a Time axis. int taxisInqVtime(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return cdiTime_get(taxisptr->vDateTime.time); } void taxisInqVtimeBounds(int taxisID, int *vtime_lb, int *vtime_ub) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); *vtime_lb = cdiTime_get(taxisptr->vDateTime_lb.time); *vtime_ub = cdiTime_get(taxisptr->vDateTime_ub.time); } @@ -817,35 +818,35 @@ The valid CDI calendar types are @func{CALENDAR_STANDARD}, @func{CALENDAR_PROLEP int taxisInqCalendar(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->calendar; } int taxisInqTunit(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->unit; } int taxisInqForecastTunit(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->fc_unit; } double taxisInqForecastPeriod(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->fc_period; } int taxisInqNumavg(int taxisID) { - const taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); + taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps); return taxisptr->numavg; } @@ -867,7 +868,7 @@ ptaxisDefName(taxis_t *taxisptr, const char *name) { if (name) { - const size_t len = strlen(name); + size_t len = strlen(name); delete_refcount_string(taxisptr->name); char *taxisname = taxisptr->name = new_refcount_string(len); strcpy(taxisname, name); @@ -879,7 +880,7 @@ ptaxisDefLongname(taxis_t *taxisptr, const char *longname) { if (longname) { - const size_t len = strlen(longname); + size_t len = strlen(longname); delete_refcount_string(taxisptr->longname); char *taxislongname = taxisptr->longname = new_refcount_string(len); strcpy(taxislongname, longname); @@ -898,7 +899,7 @@ ptaxisDefUnits(taxis_t *taxisptr, const char *units) { if (units) { - const size_t len = strlen(units); + size_t len = strlen(units); char *taxisunits = ptaxisAllocUnits(taxisptr, len); strcpy(taxisunits, units); } @@ -925,7 +926,7 @@ timevalue_decode(int timeunits, double timevalue) if (timeunits == TUNIT_SECOND) { julianDate.julianDay = (int64_t) (timevalue / 86400.0); - const double seconds = timevalue - julianDate.julianDay * 86400.0; + double seconds = timevalue - julianDate.julianDay * 86400.0; julianDate.secondOfDay = round(seconds * 1000.0) / 1000.0; if (julianDate.secondOfDay < 0) { @@ -943,7 +944,7 @@ timevalue_decode(int timeunits, double timevalue) else if (timeunits == TUNIT_DAY) { julianDate.julianDay = (int64_t) timevalue; - const double seconds = (timevalue - julianDate.julianDay) * 86400.0; + double seconds = (timevalue - julianDate.julianDay) * 86400.0; julianDate.secondOfDay = (int) lround(seconds); if (julianDate.secondOfDay < 0) { @@ -1012,7 +1013,7 @@ rtimeval2datetime(double timevalue, const taxis_t *taxis) if (IS_EQUAL(timevalue, 0.0)) return taxis->rDateTime; int timeunits = taxis->unit; - const int calendar = taxis->calendar; + int calendar = taxis->calendar; if (timeunits == TUNIT_MONTH && calendar == CALENDAR_360DAYS) { @@ -1029,12 +1030,12 @@ rtimeval2datetime(double timevalue, const taxis_t *taxis) if (timeunits == TUNIT_YEAR) timevalue *= 12; - const int nmon = (int) timevalue; - const double fmon = timevalue - nmon; + int nmon = (int) timevalue; + double fmon = timevalue - nmon; month += nmon; - struct yearMonth ym = normalize_month(year, month); + struct YearMonth ym = normalize_month(year, month); year = ym.year; month = ym.month; @@ -1045,8 +1046,8 @@ rtimeval2datetime(double timevalue, const taxis_t *taxis) rDateTime.date.month = month; } - const JulianDate julianDate = julianDate_encode(calendar, rDateTime); - const JulianDate julianDate2 = timevalue_decode(timeunits, timevalue); + JulianDate julianDate = julianDate_encode(calendar, rDateTime); + JulianDate julianDate2 = timevalue_decode(timeunits, timevalue); return julianDate_decode(calendar, julianDate_add(julianDate2, julianDate)); } @@ -1057,9 +1058,9 @@ datetime2rtimeval(CdiDateTime vDateTime, const taxis_t *taxis) { double value = 0.0; - const int calendar = taxis->calendar; + int calendar = taxis->calendar; int timeunits = taxis->unit; - const int timeunits0 = timeunits; + int timeunits0 = timeunits; CdiDateTime rDateTime = taxis->rDateTime; @@ -1067,14 +1068,14 @@ datetime2rtimeval(CdiDateTime vDateTime, const taxis_t *taxis) if (cdiDateTime_isNull(rDateTime) && cdiDateTime_isNull(vDateTime)) return value; - const JulianDate julianDate1 = julianDate_encode(calendar, rDateTime); + JulianDate julianDate1 = julianDate_encode(calendar, rDateTime); if (timeunits == TUNIT_MONTH && calendar == CALENDAR_360DAYS) timeunits = TUNIT_DAY; if (timeunits == TUNIT_MONTH || timeunits == TUNIT_YEAR) { - const int ryear = rDateTime.date.year; - const int rmonth = rDateTime.date.month; + int ryear = rDateTime.date.year; + int rmonth = rDateTime.date.month; int year = vDateTime.date.year; int month = vDateTime.date.month; value = (year - ryear) * 12 - rmonth + month; @@ -1082,24 +1083,24 @@ datetime2rtimeval(CdiDateTime vDateTime, const taxis_t *taxis) int nmonth = (int) value; month -= nmonth; - struct yearMonth ym = normalize_month(year, month); + struct YearMonth ym = normalize_month(year, month); year = ym.year; month = ym.month; - const int dpm = days_per_month(calendar, year, month); + int dpm = days_per_month(calendar, year, month); vDateTime.date.year = year; vDateTime.date.month = month; - const JulianDate julianDate2 = julianDate_encode(calendar, vDateTime); - const JulianDate dateDifference = julianDate_sub(julianDate2, julianDate1); + JulianDate julianDate2 = julianDate_encode(calendar, vDateTime); + JulianDate dateDifference = julianDate_sub(julianDate2, julianDate1); value += (dateDifference.julianDay + dateDifference.secondOfDay / 86400.0) / dpm; if (timeunits == TUNIT_YEAR) value = value / 12; } else { - const JulianDate julianDate2 = julianDate_encode(calendar, vDateTime); - const JulianDate dateDifference = julianDate_sub(julianDate2, julianDate1); + JulianDate julianDate2 = julianDate_encode(calendar, vDateTime); + JulianDate dateDifference = julianDate_sub(julianDate2, julianDate1); value = cdi_encode_timevalue(dateDifference.julianDay, dateDifference.secondOfDay, timeunits); } @@ -1113,17 +1114,17 @@ datetime2rtimeval(CdiDateTime vDateTime, const taxis_t *taxis) static CdiDateTime atimeval2datetime(double timevalue) { - const int64_t vdate = (int64_t) timevalue; - const double tmpval = (timevalue - vdate) * 86400.0; - const int daysec = (vdate < 0) ? (int) (-tmpval + 0.01) : (int) (tmpval + 0.01); + int64_t vdate = (int64_t) timevalue; + double tmpval = (timevalue - vdate) * 86400.0; + int daysec = (vdate < 0) ? (int) (-tmpval + 0.01) : (int) (tmpval + 0.01); int year, month, day; cdiDecodeDate(vdate, &year, &month, &day); - const int hour = daysec / 3600; - const int minute = (daysec - hour * 3600) / 60; - const int second = daysec - hour * 3600 - minute * 60; - const int ms = 0; + int hour = daysec / 3600; + int minute = (daysec - hour * 3600) / 60; + int second = daysec - hour * 3600 - minute * 60; + int ms = 0; CdiDateTime datetime; datetime.date = cdiDate_encode(year, month, day); @@ -1240,7 +1241,7 @@ cdiSetForecastPeriod(double timevalue, taxis_t *taxis) taxis->fc_period = timevalue; int timeunits = taxis->fc_unit; - const int calendar = taxis->calendar; + int calendar = taxis->calendar; if (cdiDateTime_isNull(taxis->vDateTime) && DBL_IS_EQUAL(timevalue, 0.0)) return; @@ -1259,12 +1260,12 @@ cdiSetForecastPeriod(double timevalue, taxis_t *taxis) if (timeunits == TUNIT_YEAR) timevalue *= 12; - const int nmon = (int) timevalue; - const double fmon = timevalue - nmon; + int nmon = (int) timevalue; + double fmon = timevalue - nmon; month -= nmon; - struct yearMonth ym = normalize_month(year, month); + struct YearMonth ym = normalize_month(year, month); year = ym.year; month = ym.month; @@ -1275,8 +1276,8 @@ cdiSetForecastPeriod(double timevalue, taxis_t *taxis) vDateTime.date.month = month; } - const JulianDate julianDate = julianDate_encode(calendar, vDateTime); - const JulianDate julianDate2 = timevalue_decode(timeunits, timevalue); + JulianDate julianDate = julianDate_encode(calendar, vDateTime); + JulianDate julianDate2 = timevalue_decode(timeunits, timevalue); taxis->fDateTime = julianDate_decode(calendar, julianDate_sub(julianDate, julianDate2)); } @@ -1300,7 +1301,7 @@ cdi_encode_timeval(CdiDateTime datetime, taxis_t *taxis) } else if (taxis->unit == TUNIT_MONTH) { - const int64_t xdate = cdiDate_get(datetime.date); + 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) @@ -1313,7 +1314,7 @@ cdi_encode_timeval(CdiDateTime datetime, taxis_t *taxis) { int hour, minute, second, ms; cdiTime_decode(datetime.time, &hour, &minute, &second, &ms); - const int64_t xdate = cdiDate_get(datetime.date); + int64_t xdate = cdiDate_get(datetime.date); timeValue = copysign(1.0, (double) xdate) * (fabs((double) xdate) + (hour * 3600 + minute * 60 + second) / 86400.0); } } diff --git a/tests/test_month_adjust.c b/tests/test_month_adjust.c index 3d097188138b0216138dcf920c320bec1c64ddc4..739e8106090c217166fd1c4f0928cd820227eb82 100644 --- a/tests/test_month_adjust.c +++ b/tests/test_month_adjust.c @@ -2,7 +2,7 @@ #include <stdlib.h> #include <sys/time.h> -#include "calendar.h" +#include "normalize_month.h" int main(void) @@ -19,7 +19,7 @@ main(void) startYear = 1900 }; int month = vals[i]; - struct yearMonth ym = normalize_month(startYear, month); + struct YearMonth ym = normalize_month(startYear, month); if ((long) ym.year * 12 + ym.month != (long) startYear * 12 + month) { fprintf(stderr, @@ -42,7 +42,7 @@ main(void) for (size_t j = 0; j < 1000000; ++j) { int year = (int) (random() - RAND_MAX / 2), month = (int) (random() - RAND_MAX / 2); - struct yearMonth ym = normalize_month(year, month); + struct YearMonth ym = normalize_month(year, month); if ((long) ym.year * 12 + ym.month != (long) year * 12 + month) { fprintf(stderr,