TECA
The Toolkit for Extreme Climate Analysis
teca_calcalcs.h
Go to the documentation of this file.
1 /*
2 A threadsafe port of the calcalcs library
3 Burlen Loring Thu Apr 22 06:22:16 PM PDT 2021
4 
5 The CalCalcs routines, a set of C-language routines to perform
6 calendar calculations.
7 
8 Version 1.0, released 7 January 2010
9 
10 Copyright (C) 2010 David W. Pierce, dpierce@ucsd.edu
11 
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
25 #ifndef calcalcs_h
26 #define calcalcs_h
27 
28 /// @file
29 
30 #define CALCALCS_VERSION_NUMBER 1.0
31 
32 
33 /// A threadsafe port of the calcalcs library.
34 namespace teca_calcalcs
35 {
36 
37 /// @cond
38 
39 struct cccalendar {
40  int sig;
41  char *name;
42  int ndays_reg, ndays_leap;
43 
44  int (*c_isleap) ( int, int * );
45  int (*c_date2jday) ( int, int, int, int * );
46  int (*c_jday2date) ( int, int *, int *, int * );
47  int (*c_dpm) ( int, int, int * );
48 
49  /* The following implement "mixed" calendars, for example, our standard
50  * civil calendar, which converts from Julian to Gregorian, with the
51  * last day of the Julian calendar being 4 Oct 1582 and the first day
52  * of the Gregorian calendar being 15 Oct 1582
53  */
54  int mixed;
55  struct cccalendar *early_cal, *late_cal;
56  int year_x, month_x, day_x; /* These are the transition Y,M,D, i.e., the FIRST DATE the LATER CAL is used */
57  int year_px, month_px, day_px; /* These are the DAY BEFORE the transition Y,M,D, i.e., the last date the earlier cal is used */
58  int jday_x; /* Julian day of the transition date */
59 };
60 
61 /* A "country code", which holds the 2-letter code (abbreviation) for the country or region,
62  * its long name, and the Y/M/D date that it transitioned from the Julian to Gregorian calendar
63  */
64 struct ccs_ccode {
65  char *code, *longname;
66  int year, month, day;
67 };
68 
69 typedef struct cccalendar calcalcs_cal;
70 typedef struct ccs_ccode ccs_country_code;
71 
72 /* =====================================================================================
73  * Here are all the services this library supplies
74  *
75  * -------------------------------------------------------------------------
76  * ccs_init_calendar : initialize a calendar. Valid passed arguments are
77  * one of the following strings:
78  * Standard, proleptic_Gregorian, proleptic_Julian, noleap (aka 365_day and no_leap), 360_day
79  *
80  * The "Standard" calendar transitions from the Julian to the Gregorian calendar,
81  * with the last day of the Julian calender being 1582-10-04 and the next day being
82  * the first day of the Gregorian calendar, with the date 1582-10-15. This "transition date"
83  * can be set to be the value used by various countries, or set to any arbitrary
84  * date, using routine "set_cal_xition_date", below.
85  */
86 calcalcs_cal *ccs_init_calendar( const char *calname );
87 
88 /*------------------------------------------------------------------------------
89  * Frees the storage previously allocated by ccs_init_calendar()
90  */
91 void ccs_free_calendar( calcalcs_cal *calendar );
92 
93 /*--------------------------------------------------------------------------
94  * ccs_date2jday: turn a Y/M/D date into a (true) Julian day number. Note that
95  * a Julian day is not the day number of the year, but rather
96  * the integer day number starting from 1 on the day that would
97  * have been 1 Jan 4713 BC if the Julian calendar went back
98  * to that time.
99  */
100 int ccs_date2jday( calcalcs_cal *calendar, int year, int month, int day, int *jday );
101 
102 /*--------------------------------------------------------------------------
103  * ccs_jday2date: turn a (true) Julian day number into a calendar date.
104  */
105 int ccs_jday2date( calcalcs_cal *calendar, int jday, int *year, int *month, int *day );
106 
107 /*--------------------------------------------------------------------------
108  * ccs_isleap: determine if the specified year is a leap year in
109  * the specified calendar. return 0 if successful.
110  */
111 int ccs_isleap( calcalcs_cal *calendar, int year, int *leap );
112 
113 /*--------------------------------------------------------------------------
114  * ccs_dpm: returns the days per month for the given year/month.
115  * Note that during the month that transitions from a Julian to a
116  * Gregorian calendar, this might be a strange number of days.
117  */
118 int ccs_dpm( calcalcs_cal *calendar, int year, int month, int *dpm );
119 
120 /*--------------------------------------------------------------------------
121  * ccs_date2doy: given a Y/M/D date, calculates the day number of the year, starting at 1 for
122  * January 1st.
123  */
124 int ccs_date2doy( calcalcs_cal *calendar, int year, int month, int day, int *doy );
125 
126 /*--------------------------------------------------------------------------
127  * ccs_doy2date: given a year and a day number in that year (with counting starting at 1 for
128  * Jan 1st), this returns the month and day of the month that the doy refers to.
129  */
130 int ccs_doy2date( calcalcs_cal *calendar, int year, int doy, int *month, int *day );
131 
132 /*--------------------------------------------------------------------------
133  * ccs_dayssince: Given a Y/M/D date in a specified calendar, and the number of days since
134  * that date, this returns the new Y/M/D date in a (possibly different) calendar.
135  *
136  * Note that specifying "zero" days since, and giving different calendars as the original
137  * and new calendars, essentially converts dates between calendars.
138  */
139 int ccs_dayssince( calcalcs_cal *calendar_orig, int year_orig, int month_orig,
140  int day_orig, int ndays_since, calcalcs_cal *calendar_new,
141  int *year_new, int *month_new, int *day_new );
142 
143 /*--------------------------------------------------------------------------
144  * get/set_cal_xition_date: these routines set the transition date for a Standard
145  * calendar, which is the date that the Gregorian calendar was first used.
146  * Before that, it is assumed that the Julian calendar was used.
147  *
148  * Historically, this transition date varies by country and region. The
149  * variation can be extreme, and over the centuries country boundaries have
150  * changed, so this should be considered only the grossest approximation. For
151  * that matter, for several countries, different districts/regions converted
152  * at different times anyway, so actually doing a good job at this task is
153  * far beyond this library's capability. Nevertheless, this does give some
154  * basic simplified capabilities in this regard. And you can always set
155  * the routines to use an arbitrary transition date of your own calculation.
156  *
157  * How to use these routines:
158  *
159  * If you know the transition date you want to impose, simply call
160  * set_cal_xition_date with the specified transition date. The date
161  * specified is the FIRST date that the new (Gregorian) calendar was
162  * used. For exaple, in Italy, the Gregorian calendar was first used
163  * on 15 October 1582.
164  *
165  * If you don't know what transition date to use, there is a brief
166  * database with some APPROXIMATE dates of transition that can be accessed
167  * by calling get_cal_xition_date with a two-letter country code, corresponding
168  * to the internet suffix for the country. (As a special case, "US" is used
169  * for the United States of America.) If the routine returns 0, then the
170  * country code is recognized and the approximate transition date is returned
171  * in the passed parameters year, month, day. These should then be given to
172  * routine set_cal_xition_date to make that calendar use the specified
173  * transition date. If the get_cal_xition_date routine does not return 0,
174  * then there is no information on that country in the database.
175  *
176  * routine set_cal_xition_date returns 0 on success, and something other than
177  * 0 on an error. Errors include trying to set the transition date to an
178  * invalid date, or trying to set the transition date for any calendar
179  * other than the "Standard" calendar.
180  *
181  * The following country/region codes are recognized: AK (Alaska) 1867/10/18;
182  * AL (Albania) 1912/12/1; AT (Austria) 1583/10/16; BE (Belgium) 1582/12/25;
183  * BG (Bulgaria) 1916/4/1; CN (China) 1929/1/1; CZ (Czechoslovakia) 1584/1/17;
184  * DK (Denmark) 1700/3/1; NO (Norway) 1700/3/1; EG (Egypt) 1875/1/1;
185  * EE (Estonia) 1918/1/1; FI (Finland) 1753/3/1; FR (France) 1582/12/20;
186  * DE (Germany, note different states actually used different dates between
187  * 1583 and 1700!) 1583/11/22; UK (Great Britain and Dominions) 1752/9/14;
188  * GR (Greece) 1924/3/23; HU (Hungary) 1587/11/1; IT (Italy) 1582/10/15;
189  * JP (Japan) 1918/1/1; LV (Latvia) 1915/1/1; LT (Lithuania) 1915/1/1;
190  * LU (Luxemburg) 1582/12/15; NL (Netherlands, note Catholic regions
191  * transitioned in various dates of 1582/83, while Protestant regions
192  * transitioned in various dates of 1700/01) 1582/10/15; NO (Norway) 1700/3/1;
193  * PL (Poland) 1582/10/15; PT (Portugal) 1582/10/15; RO (Romania) 1919/4/14;
194  * ES (Spain) 1582/10/15; SE (Sweden) 1753/3/1; CH (Switzerland, note,
195  * varied bewteen 1584 and 1701 by Canton) 1584/1/22; TR (Turkey)
196  * 1927/1/1; YU (Yugoslavia) 1919/1/1; UK (Great Britain and Dominions) 1752/9/14;
197  * US (United States) 1752/9/14; SU (former Soviet Union) 1918/2/1;
198  * RU (Russia) 1918/2/1.
199  */
200 int ccs_set_xition_date( calcalcs_cal *calendar, int year, int month, int day );
201 int ccs_get_xition_date( const char *country_code, int *year, int *month, int *day );
202 
203 /*--------------------------------------------------------------------------
204  * calcalcs_err_str: return a static char * to an error string, given the error nmmber
205  */
206 char *ccs_err_str(int ccs_errno);
207 
208 #define CALCALCS_ERR_NO_YEAR_ZERO -10
209 #define CALCALCS_ERR_DATE_NOT_IN_CALENDAR -11
210 #define CALCALCS_ERR_INVALID_DAY_OF_YEAR -12
211 #define CALCALCS_ERR_NOT_A_MIXED_CALENDAR -13
212 #define CALCALCS_ERR_UNKNOWN_COUNTRY_CODE -14
213 #define CALCALCS_ERR_OUT_OF_RANGE -15
214 #define CALCALCS_ERR_NULL_CALENDAR -16
215 #define CALCALCS_ERR_INVALID_CALENDAR -17
216 
217 
218 /* The high level API is defined below. This API is thread safe and
219  * has implementations optimized for use with calendar and units strings.
220  */
221 #define UT_ENOINIT -10
222 #define UT_EINVALID -11
223 
224 /// @endcond
225 
226 /** high level thread safe initialize the library and select a calendar
227  * to use in subsequent calls.
228  * return 0 upon success
229  */
230 int set_current_calendar( const char *calendar, const char *units );
231 
232 /** Determine if the specified year is a leap year in the specified calendar.
233  * this wraps ccs_isleap such that initialization is automatically handled and
234  * optimizes for repeat calls. @returns 0 if successful.
235  */
236 int is_leap_year( const char *calendar, const char *units,
237  int year, int &leap );
238 
239 /** Returns the days per month for the given year/month.
240  * Note that during the month that transitions from a Julian to a
241  * Gregorian calendar, this might be a strange number of days. this
242  * wraps ccs_dpm such that initialization is automatically handled and
243  * optimizes for repeat calls. returns 0 on success.
244  */
245 int days_in_month( const char *calendar, const char *units,
246  int year, int month, int &dpm );
247 
248 /** Given a floating point offset in the given calendar return
249  * year, month, day, hour, minute, seconds. returns 0 upon success.
250  */
251 int date( double val, int *year, int *month, int *day, int *hour,
252  int *minute, double *second, const char *dataunits,
253  const char *calendar_name );
254 
255 
256 /** given a year, month, day, hour, minute, second and calendar find
257  * the floating point offset. returns 0 upon success.
258  */
259 int coordinate( int year, int month, int day, int hour, int minute,
260  double second, const char *user_unit, const char *calendar_name,
261  double *value );
262 
263 };
264 
265 #endif
teca_calcalcs::coordinate
int coordinate(int year, int month, int day, int hour, int minute, double second, const char *user_unit, const char *calendar_name, double *value)
teca_calcalcs::date
int date(double val, int *year, int *month, int *day, int *hour, int *minute, double *second, const char *dataunits, const char *calendar_name)
teca_calcalcs::set_current_calendar
int set_current_calendar(const char *calendar, const char *units)
teca_calcalcs::is_leap_year
int is_leap_year(const char *calendar, const char *units, int year, int &leap)
teca_calcalcs
A threadsafe port of the calcalcs library.
Definition: teca_calcalcs.h:34
teca_calcalcs::days_in_month
int days_in_month(const char *calendar, const char *units, int year, int month, int &dpm)