~hrbrmstr/daybreak

ref: af8ff3a5efbe1173d1a188e6d8d884c2bc809e9c daybreak/src/daybreak-main.c -rw-r--r-- 3.7 KiB
af8ff3a5hrbrmstr plain C version 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <R.h>
#include <Rinternals.h>

#include "sunriset.h"

SEXP r_day_length(SEXP year, SEXP month, SEXP day, SEXP lon, SEXP lat) {

  SEXP out = PROTECT(allocVector(REALSXP, 1));
  REAL(out)[0] = day_length(
    asInteger(year), asInteger(month), asInteger(day), asReal(lon), asReal(lat)
  );
  UNPROTECT(1);

  return(out);

}

SEXP r_day_civil_twilight_length(SEXP year, SEXP month, SEXP day, SEXP lon, SEXP lat) {

  SEXP out = PROTECT(allocVector(REALSXP, 1));
  REAL(out)[0] = day_civil_twilight_length(
    asInteger(year), asInteger(month), asInteger(day), asReal(lon), asReal(lat)
  );
  UNPROTECT(1);

  return(out);

}

SEXP r_day_nautical_twilight_length(SEXP year, SEXP month, SEXP day, SEXP lon, SEXP lat) {

  SEXP out = PROTECT(allocVector(REALSXP, 1));
  REAL(out)[0] = day_nautical_twilight_length(
    asInteger(year), asInteger(month), asInteger(day), asReal(lon), asReal(lat)
  );
  UNPROTECT(1);

  return(out);

}

SEXP r_day_astronomical_twilight_length(SEXP year, SEXP month, SEXP day, SEXP lon, SEXP lat) {

  SEXP out = PROTECT(allocVector(REALSXP, 1));
  REAL(out)[0] = day_astronomical_twilight_length(
    asInteger(year), asInteger(month), asInteger(day), asReal(lon), asReal(lat)
  );
  UNPROTECT(1);

  return(out);

}

SEXP r_sun_rise_set(SEXP year, SEXP month, SEXP day, SEXP lon, SEXP lat) {

  double rise, set;

  int res = sun_rise_set(
    asInteger(year), asInteger(month), asInteger(day), asReal(lon), asReal(lat),
    &rise, &set
  );

  const char *names[] = {
    "rise",
    "set",
    ""
  };

  SEXP out = PROTECT(mkNamed(VECSXP, names));

  if (res == 0) {
    SET_VECTOR_ELT(out, 0, PROTECT(ScalarReal(rise)));
    SET_VECTOR_ELT(out, 1, PROTECT(ScalarReal(set)));
  } else {
    SET_VECTOR_ELT(out, 0, PROTECT(ScalarReal(NA_REAL)));
    SET_VECTOR_ELT(out, 1, PROTECT(ScalarReal(NA_REAL)));
  }

  UNPROTECT(3);

  return(out);

}

SEXP r_civil_twilight(SEXP year, SEXP month, SEXP day, SEXP lon, SEXP lat) {

  double start, end;

  int res = civil_twilight(
    asInteger(year), asInteger(month), asInteger(day), asReal(lon), asReal(lat),
    &start, &end
  );

  const char *names[] = {
    "start",
    "end",
    ""
  };

  SEXP out = PROTECT(mkNamed(VECSXP, names));

  if (res == 0) {
    SET_VECTOR_ELT(out, 0, PROTECT(ScalarReal(start)));
    SET_VECTOR_ELT(out, 1, PROTECT(ScalarReal(end)));
  } else {
    SET_VECTOR_ELT(out, 0, PROTECT(ScalarReal(NA_REAL)));
    SET_VECTOR_ELT(out, 1, PROTECT(ScalarReal(NA_REAL)));
  }

  UNPROTECT(3);

  return(out);

}

SEXP r_nautical_twilight(SEXP year, SEXP month, SEXP day, SEXP lon, SEXP lat) {

  double start, end;

  int res = nautical_twilight(
    asInteger(year), asInteger(month), asInteger(day), asReal(lon), asReal(lat),
    &start, &end
  );

  const char *names[] = {
    "start",
    "end",
    ""
  };

  SEXP out = PROTECT(mkNamed(VECSXP, names));

  if (res == 0) {
    SET_VECTOR_ELT(out, 0, PROTECT(ScalarReal(start)));
    SET_VECTOR_ELT(out, 1, PROTECT(ScalarReal(end)));
  } else {
    SET_VECTOR_ELT(out, 0, PROTECT(ScalarReal(NA_REAL)));
    SET_VECTOR_ELT(out, 1, PROTECT(ScalarReal(NA_REAL)));
  }

  UNPROTECT(3);

  return(out);

}

SEXP r_astronomical_twilight(SEXP year, SEXP month, SEXP day, SEXP lon, SEXP lat) {

  double start, end;

  int res = astronomical_twilight(
    asInteger(year), asInteger(month), asInteger(day), asReal(lon), asReal(lat),
    &start, &end
  );

  const char *names[] = {
    "start",
    "end",
    ""
  };

  SEXP out = PROTECT(mkNamed(VECSXP, names));

  if (res == 0) {
    SET_VECTOR_ELT(out, 0, PROTECT(ScalarReal(start)));
    SET_VECTOR_ELT(out, 1, PROTECT(ScalarReal(end)));
  } else {
    SET_VECTOR_ELT(out, 0, PROTECT(ScalarReal(NA_REAL)));
    SET_VECTOR_ELT(out, 1, PROTECT(ScalarReal(NA_REAL)));
  }

  UNPROTECT(3);

  return(out);

}