1 #include "cdo_season.h"
2 #include "compare.h"
3 #include "cdo_output.h"
4 #include "cdo_options.h"
5
6 #include <stdlib.h>
7
8 const char *seas_name_dec[4] = { "DJF", "MAM", "JJA", "SON" };
9 const char *seas_name_jan[4] = { "JFM", "AMJ", "JAS", "OND" };
10
11 int
get_season_start(void)12 get_season_start(void)
13 {
14 static int season_start = START_DEC;
15 static bool lgetenv = true;
16
17 if (lgetenv)
18 {
19 lgetenv = false;
20
21 char *envstr = getenv("CDO_SEASON_START");
22 if (envstr)
23 {
24 if (cdo_cmpstr(envstr, "DEC"))
25 season_start = START_DEC;
26 else if (cdo_cmpstr(envstr, "JAN"))
27 season_start = START_JAN;
28
29 if (Options::cdoVerbose)
30 {
31 if (season_start == START_DEC)
32 cdo_print("Set SEASON_START to December");
33 else if (season_start == START_JAN)
34 cdo_print("Set SEASON_START to January");
35 }
36 }
37 }
38
39 return season_start;
40 }
41
42 void
get_season_name(const char * seas_name[])43 get_season_name(const char *seas_name[])
44 {
45 if (get_season_start() == START_DEC)
46 for (int i = 0; i < 4; ++i) seas_name[i] = seas_name_dec[i];
47 else
48 for (int i = 0; i < 4; ++i) seas_name[i] = seas_name_jan[i];
49 }
50
51 int
month_to_season(int month)52 month_to_season(int month)
53 {
54 const auto season_start = get_season_start();
55 int seas = -1;
56
57 if (month < 0 || month > 16) cdo_abort("Month %d out of range!", month);
58
59 if (season_start == START_DEC)
60 {
61 seas = (month <= 12) ? (month % 12) / 3 : month - 13;
62 }
63 else
64 {
65 seas = (month <= 12) ? (month - 1) / 3 : month - 13;
66 }
67
68 if (seas < 0 || seas > 3) cdo_abort("Season %d out of range!", seas + 1);
69
70 return seas;
71 }
72