-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmisc_util.c
194 lines (141 loc) · 4.64 KB
/
misc_util.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
*
* Copyright (C) 2014-2018 Greg McGarragh <mcgarragh@atm.ox.ac.uk>
*
* This source code is licensed under the GNU General Public License (GPL),
* Version 3. See the file COPYING for more details.
*
******************************************************************************/
#include "external.h"
#include "internal.h"
#include "misc_util.h"
/*******************************************************************************
* Return non-zero if the current machine is Little-endian and zero if it is
* Big-endian.
******************************************************************************/
int su_is_little_endian()
{
ushort x = 1;
return ((char *) &x)[0];
}
/*******************************************************************************
* Round the argument to the nearest integer value with an upward rounding
* direction.
******************************************************************************/
double su_rint(double x)
{
return x >= 0. ? floor(x + .5) : ceil(x - .5);
}
/*******************************************************************************
* Fill an array with a constant value.
******************************************************************************/
void su_init_array_uc(uchar *a, uint n, uchar x)
{
uint i;
for (i = 0; i < n; ++i)
a[i] = x;
}
void su_init_array_us(ushort *a, uint n, ushort x)
{
uint i;
for (i = 0; i < n; ++i)
a[i] = x;
}
void su_init_array_f(float *a, uint n, float x)
{
uint i;
for (i = 0; i < n; ++i)
a[i] = x;
}
void su_init_array_d(double *a, uint n, double x)
{
uint i;
for (i = 0; i < n; ++i)
a[i] = x;
}
/*******************************************************************************
* Calculate and return the GSICS calibration offset from the values contained
* in the Level 1.5 header file
******************************************************************************/
double su_get_ar_val(double ac, double bc, double g0)
{
return (ac - (bc * g0));
}
/*******************************************************************************
* Calculate and return the GSICS calibration slope from the values contained
* in the Level 1.5 header file
******************************************************************************/
double su_get_br_val(double bc, double gs)
{
return (bc / gs);
}
/*******************************************************************************
* Compute calender date given Julian Day Number.
*
* Hatcher, D. A. 1984
******************************************************************************/
#ifdef IXMOD
#undef IXMOD
#endif
#define IXMOD(x, y) (((x % y) + y) % y)
#ifdef FXMOD
#undef FXMOD
#endif
#define FXMOD(x, y) (fmod((fmod(x, y) + y), y))
#define JUL_GREG 2299161
void su_jul_to_cal_date(long jul, int *y, int *m, int *d)
{
if (jul >= JUL_GREG)
jul += (long) (floor(floor((jul - 4479.5) / 36524.25) * 0.75 + 0.5) - 37.);
*y = (int) floor(jul / 365.25) - 4712;
*d = (int) floor(FXMOD((jul - 59.25), 365.25));
*m = (int) FXMOD((floor((*d + 0.5) / 30.6) + 2), 12) + 1;
*d = (int) floor(FXMOD((*d + 0.5), 30.6)) + 1;
if (*y <= 0)
--(*y);
}
/*******************************************************************************
* Compute Julian Day Number given calender date.
*
* Hatcher, D. A. 1984
******************************************************************************/
#define CAL_GREG (15+31L*(10+12L*1582))
long su_cal_to_jul_day(int y, int m, int d)
{
int yp;
int mp;
long jul;
if (y == 0) {
fprintf(stderr, "ERROR: There is no year zero\n");
return -1;
}
yp = y;
if (yp < 0)
++yp;
/*
yp = yp - floor((12 - m) / 10.);
*/
if (m < 3)
--yp;
mp = IXMOD((m - 3), 12);
jul = (long) (floor(365.25*(yp+4712)) + floor(30.6*mp+0.5) + d + 59);
if (d+31L*(m+12L*y) >= CAL_GREG)
jul -= (long) (floor(floor((yp * 0.01) + 49.) * 0.75) - 38.);
/*
jul -= (long) (floor(floor((yp * 0.01) + 1.) * 0.75) - 2.);
*/
return jul;
}
/*******************************************************************************
* Compute the annual solar distance factor given the day of year.
*
* Liou 2002, page 49
******************************************************************************/
double su_solar_distance_factor2(double jday)
{
double t;
t = (2. * PI * jday) / 365.;
return 1.000110 +
0.034221*cos(t) + 0.001280*sin(t) +
0.000719*cos(2.*t) + 0.000077*sin(2.*t);
}