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
|
/* -*- mode: c++; c-basic-offset: 4 -*- */
#define NO_IMPORT_ARRAY
#include <math.h>
// utilities for irregular grids
void _bin_indices_middle(
unsigned int *irows, int nrows, const float *ys1, unsigned long ny, float dy, float y_min)
{
int i, j, j_last;
unsigned int *rowstart = irows;
const float *ys2 = ys1 + 1;
const float *yl = ys1 + ny;
float yo = y_min + dy / 2.0;
float ym = 0.5f * (*ys1 + *ys2);
// y/rows
j = 0;
j_last = j;
for (i = 0; i < nrows; i++, yo += dy, rowstart++) {
while (ys2 != yl && yo > ym) {
ys1 = ys2;
ys2 = ys1 + 1;
ym = 0.5f * (*ys1 + *ys2);
j++;
}
*rowstart = j - j_last;
j_last = j;
}
}
void _bin_indices_middle_linear(float *arows,
unsigned int *irows,
int nrows,
const float *y,
unsigned long ny,
float dy,
float y_min)
{
int i;
int ii = 0;
int iilast = (int)ny - 1;
float sc = 1 / dy;
int iy0 = (int)floor(sc * (y[ii] - y_min));
int iy1 = (int)floor(sc * (y[ii + 1] - y_min));
float invgap = 1.0f / (iy1 - iy0);
for (i = 0; i < nrows && i <= iy0; i++) {
irows[i] = 0;
arows[i] = 1.0;
}
for (; i < nrows; i++) {
while (i > iy1 && ii < iilast) {
ii++;
iy0 = iy1;
iy1 = (int)floor(sc * (y[ii + 1] - y_min));
invgap = 1.0f / (iy1 - iy0);
}
if (i >= iy0 && i <= iy1) {
irows[i] = ii;
arows[i] = (iy1 - i) * invgap;
} else
break;
}
for (; i < nrows; i++) {
irows[i] = iilast - 1;
arows[i] = 0.0;
}
}
void _bin_indices(int *irows, int nrows, const double *y, unsigned long ny, double sc, double offs)
{
int i;
if (sc * (y[ny - 1] - y[0]) > 0) {
int ii = 0;
int iilast = (int)ny - 1;
int iy0 = (int)floor(sc * (y[ii] - offs));
int iy1 = (int)floor(sc * (y[ii + 1] - offs));
for (i = 0; i < nrows && i < iy0; i++) {
irows[i] = -1;
}
for (; i < nrows; i++) {
while (i > iy1 && ii < iilast) {
ii++;
iy0 = iy1;
iy1 = (int)floor(sc * (y[ii + 1] - offs));
}
if (i >= iy0 && i <= iy1)
irows[i] = ii;
else
break;
}
for (; i < nrows; i++) {
irows[i] = -1;
}
} else {
int iilast = (int)ny - 1;
int ii = iilast;
int iy0 = (int)floor(sc * (y[ii] - offs));
int iy1 = (int)floor(sc * (y[ii - 1] - offs));
for (i = 0; i < nrows && i < iy0; i++) {
irows[i] = -1;
}
for (; i < nrows; i++) {
while (i > iy1 && ii > 1) {
ii--;
iy0 = iy1;
iy1 = (int)floor(sc * (y[ii - 1] - offs));
}
if (i >= iy0 && i <= iy1)
irows[i] = ii - 1;
else
break;
}
for (; i < nrows; i++) {
irows[i] = -1;
}
}
}
void _bin_indices_linear(
float *arows, int *irows, int nrows, double *y, unsigned long ny, double sc, double offs)
{
int i;
if (sc * (y[ny - 1] - y[0]) > 0) {
int ii = 0;
int iilast = (int)ny - 1;
int iy0 = (int)floor(sc * (y[ii] - offs));
int iy1 = (int)floor(sc * (y[ii + 1] - offs));
float invgap = 1.0 / (iy1 - iy0);
for (i = 0; i < nrows && i < iy0; i++) {
irows[i] = -1;
}
for (; i < nrows; i++) {
while (i > iy1 && ii < iilast) {
ii++;
iy0 = iy1;
iy1 = (int)floor(sc * (y[ii + 1] - offs));
invgap = 1.0 / (iy1 - iy0);
}
if (i >= iy0 && i <= iy1) {
irows[i] = ii;
arows[i] = (iy1 - i) * invgap;
} else
break;
}
for (; i < nrows; i++) {
irows[i] = -1;
}
} else {
int iilast = (int)ny - 1;
int ii = iilast;
int iy0 = (int)floor(sc * (y[ii] - offs));
int iy1 = (int)floor(sc * (y[ii - 1] - offs));
float invgap = 1.0 / (iy1 - iy0);
for (i = 0; i < nrows && i < iy0; i++) {
irows[i] = -1;
}
for (; i < nrows; i++) {
while (i > iy1 && ii > 1) {
ii--;
iy0 = iy1;
iy1 = (int)floor(sc * (y[ii - 1] - offs));
invgap = 1.0 / (iy1 - iy0);
}
if (i >= iy0 && i <= iy1) {
irows[i] = ii - 1;
arows[i] = (i - iy0) * invgap;
} else
break;
}
for (; i < nrows; i++) {
irows[i] = -1;
}
}
}
|