-
Notifications
You must be signed in to change notification settings - Fork 2
/
reproject.c
448 lines (372 loc) · 11.9 KB
/
reproject.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* reproject.c
* Authors: Yizhao Gao <[email protected]>
* Date: {12/06/2017}
* Muqun Yang<[email protected]> (adapted for python wrapper)
* Date: {05/21/2019}
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <omp.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/**
* struct LonBlocks: data structure for grid-based spatial indexing. Each "struct LonBlocks" holds one row (latitude) of the spatial indexing
* ITEMS:
* double blockSizeR: the longtitude range of each block
* int nBlocks: the number of longtitude blocks in this row
* int * indexID: the starting and ending index (of the long arrays of data) of locations in each block
*/
struct LonBlocks {
double blockSizeR;
int nBlocks;
int * indexID;
};
struct LonBlocks * pointIndexOnLatLon(double ** plat, double ** plon, double*newLat, double*newLon,int * oriID, int count, int nBlockY, double maxradian) {
double *lat = *plat;
double *lon = *plon;
struct LonBlocks *blockIndex = NULL;
int ** pointsInB = NULL;
if(NULL == (blockIndex = (struct LonBlocks *)malloc(sizeof(struct LonBlocks) * nBlockY))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
if(NULL == (pointsInB = (int **)malloc(sizeof(int *) * nBlockY))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
double latBlockR = M_PI/nBlockY;
blockIndex[0].blockSizeR = 2 * M_PI;
blockIndex[0].nBlocks = 1;
if(NULL == (blockIndex[0].indexID = (int *) malloc (sizeof(int) * 2))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
if(NULL == (pointsInB[0] = (int *)malloc(sizeof(int)))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
pointsInB[0][0] = 0;
blockIndex[nBlockY - 1].blockSizeR = 2 * M_PI;
blockIndex[nBlockY - 1].nBlocks = 1;
if(NULL == (blockIndex[nBlockY - 1].indexID = (int *) malloc (sizeof(int) * 2))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
if(NULL == (pointsInB[nBlockY - 1] = (int *)malloc(sizeof(int)))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
pointsInB[nBlockY - 1][0] = 0;
int i, j;
double highestLat;
for(i = 1; i < nBlockY - 1; i++) {
if(i < (nBlockY + 1) / 2) {
highestLat = -M_PI/2 + latBlockR * i;
}
else {
highestLat = -M_PI/2 + latBlockR * (i + 1);
}
blockIndex[i].nBlocks = (int)(2 * M_PI * cos(highestLat)/ maxradian);
if(blockIndex[i].nBlocks < 4) {
blockIndex[i].nBlocks = 1;
}
blockIndex[i].blockSizeR = 2 * M_PI / blockIndex[i].nBlocks;
if(NULL == (blockIndex[i].indexID = (int *) malloc (sizeof(int) * (blockIndex[i].nBlocks + 1)))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
if(NULL == (pointsInB[i] = (int *) malloc (sizeof(int) * (blockIndex[i].nBlocks + 1)))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
for(j = 0; j < blockIndex[i].nBlocks; j++) {
pointsInB[i][j] = 0;
}
}
int rowID, colID;
for(i = 0; i < count; i++) {
rowID = (int)((lat[i] + M_PI/2) / latBlockR);
if(rowID >= 0 && rowID < nBlockY) {
colID = (int)((lon[i] + M_PI) / blockIndex[rowID].blockSizeR);
if(colID >= 0 && colID < blockIndex[rowID].nBlocks) {
pointsInB[rowID][colID] ++;
}
}
}
int newCount = 0;
for(i = 0; i < nBlockY; i++) {
blockIndex[i].indexID[0] = newCount;
for(j = 0; j < blockIndex[i].nBlocks; j++) {
newCount += pointsInB[i][j];
blockIndex[i].indexID[j+1] = newCount;
pointsInB[i][j] = blockIndex[i].indexID[j];
}
}
for(i = 0; i < count; i++) {
rowID = (int)((lat[i] + M_PI/2) / latBlockR);
if(rowID >= 0 && rowID < nBlockY) {
colID = (int)((lon[i] + M_PI) / blockIndex[rowID].blockSizeR);
if(colID >= 0 && colID < blockIndex[rowID].nBlocks) {
newLon[pointsInB[rowID][colID]] = lon[i];
newLat[pointsInB[rowID][colID]] = lat[i];
oriID[pointsInB[rowID][colID]] = i;
pointsInB[rowID][colID] ++;
}
}
}
for(i = 0; i < nBlockY; i++) {
if(pointsInB[i] != NULL)
free(pointsInB[i]);
}
if(pointsInB != NULL)
free(pointsInB);
return blockIndex;
}
/**
* NAME: nearestNeighborBlockIndex
* DESCRIPTION: Find the nearest neighboring source cell's ID for each target cell
* PARAMETERS:
* double ** psouLat: the pointer to the array of latitudes of source cells (the data are changed during in the function, so please do the output before this function)
* double ** psouLon: the pointer to the array of longitudes of source cells (the data are changed during in the function, so please do the output before this function)
* int nSou: the number of source cells
* double * tarLat: the latitudes of target cells
* double * tarLon: the longitudes of target cells
* int * tarNNSouID: the output IDs of nearest neighboring source cells
* double * tarNNDis the output nearest distance for each target cell (input NULL if you don't need this field)
* int nTar: the number of target cells
* double maxR: the maximum distance (in meters) to define neighboring cells
* Output:
* int * tarNNSouID: the output IDs of nearest neighboring source cells
* double * tarNNDis the output nearest distance for each target cell (input NULL if you don't need this field)
*/
void nearestNeighborBlockIndex(double ** psouLat, double ** psouLon, int nSou, double * tarLat, double * tarLon, int * tarNNSouID, double * tarNNDis, int nTar, double maxR) {
double * souLat = *psouLat;
double * souLon = *psouLon;
//const double earthRadius = 6367444;
const double earthRadius = 6371009;
double maxradian = maxR / earthRadius;
double blockSizeRadian = maxradian;
if(maxR < 1000) {
blockSizeRadian = 1000 / earthRadius;
}
int nBlockY = M_PI / blockSizeRadian;
double latBlockR = M_PI / nBlockY;
int i, j, k, kk, l;
#pragma omp parallel for
for(i = 0; i < nSou; i++) {
/* if(i == 0)
{
printf("%d threads\n", omp_get_num_threads());
}
*/
souLat[i] = souLat[i] * M_PI / 180;
souLon[i] = souLon[i] * M_PI / 180;
}
#pragma omp parallel for
for(i = 0; i < nTar; i++) {
tarLat[i] = tarLat[i] * M_PI / 180;
tarLon[i] = tarLon[i] * M_PI / 180;
}
int * souID;
if(NULL == (souID = (int *)malloc(sizeof(double) * nSou))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
double * newLat = NULL;
double * newLon = NULL;
if(NULL == (newLon = (double *)malloc(sizeof(double) * nSou))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
if(NULL == (newLat = (double *)malloc(sizeof(double) * nSou))) {
printf("ERROR: Out of memory at line %d in file %s\n", __LINE__, __FILE__);
exit(1);
}
struct LonBlocks * souIndex = pointIndexOnLatLon(psouLat, psouLon, newLat,newLon,souID, nSou, nBlockY, blockSizeRadian);
#pragma omp parallel for private(j, k, kk, l)
for(i = 0; i < nTar; i ++) {
/* if(i == 0)
{
printf("%d threads\n", omp_get_num_threads());
}
*/
double tLat = tarLat[i];
double tLon = tarLon[i];
double sLat, sLon;
int rowID, colID;
double pDis;
double nnDis;
int nnSouID= -1;
/*
if(i == 0) {
printf("%d\n", omp_get_num_threads());
}
*/
rowID = (tLat + M_PI / 2) / latBlockR;
nnDis = -1;
for(j = rowID - 1; j < rowID + 2; j ++) {
if(j < 0 || j >= nBlockY) {
continue;
}
colID = (tLon + M_PI) / souIndex[j].blockSizeR;
if(souIndex[j].nBlocks == 1) {
for(l = souIndex[j].indexID[0]; l < souIndex[j].indexID[1]; l++) {
sLat = newLat[l];
sLon = newLon[l];
pDis = acos(sin(tLat) * sin(sLat) + cos(tLat) * cos(sLat) * cos(tLon - sLon));
if((nnDis < 0 || nnDis > pDis) && pDis <= maxradian) {
nnDis = pDis;
nnSouID = souID[l];
}
}
}
else {
for(k = colID - 1; k < colID + 2; k ++) {
kk = k;
if(kk < 0) {
kk = souIndex[j].nBlocks-1;
}
if(kk >= souIndex[j].nBlocks) {
kk = 0;
}
for(l = souIndex[j].indexID[kk]; l < souIndex[j].indexID[kk+1]; l++) {
sLat = newLat[l];
sLon = newLon[l];
pDis = acos(sin(tLat) * sin(sLat) + cos(tLat) * cos(sLat) * cos(tLon - sLon));
if((nnDis < 0 || nnDis > pDis) && pDis <= maxradian) {
nnDis = pDis;
nnSouID = souID[l];
}
}
}
}
}
if(nnDis < 0) {
tarNNSouID[i] = -1;
if(tarNNDis != NULL) {
tarNNDis[i] = -1;
}
}
else {
tarNNSouID[i] = nnSouID;
if(tarNNDis != NULL) {
tarNNDis[i] = nnDis * earthRadius;
}
}
}
if (souID != NULL)
free(souID);
for(i = 0; i < nBlockY; i++) {
// printf("%d,\t%lf\n", souIndex[i].nBlocks, souIndex[i].blockSizeR);
if (souIndex[i].indexID != NULL)
free(souIndex[i].indexID);
}
if (souIndex != NULL)
free(souIndex);
free(newLat);
free(newLon);
return;
}
/**
* NAME: nnInterpolate
* DESCRIPTION: Nearest neighbor interpolation
* PARAMETERS:
* double * souVal: the input values at source cells
* double * tarVal: the output values at target cells
* int * tarNNSouID: the IDs of nearest neighboring source cells for each target cells (generated from "nearestNeighbor")
* int nTar: the number of target cells
* Output:
* double * tarVal: the output values at target cells
*/
void nnInterpolate(double * souVal, double * tarVal, int * tarNNSouID, int nTar) {
int nnSouID;
int i;
#pragma omp parallel for private(nnSouID)
for(i = 0; i < nTar; i++) {
nnSouID = tarNNSouID[i];
if(nnSouID < 0) {
tarVal[i] = -999;
}
else {
tarVal[i] = souVal[nnSouID];
}
}
}
/**
* NAME: summaryInterpolate
* DESCRIPTION: Interpolation (summary) from fine resolution to coarse resolution
* PARAMETERS:
* double * souVal: the input values at source cells
* int * souNNTarID: the IDs of nearest neighboring target cells for each source cells (generated from "nearestNeighbor")
* int nSou: the number of source cells
* double * tarVal: the output (average) values at target cells
* double * tarSD: the standard deviation (SD) value at target cells (can be NULL if no SD values need to be reported)
* int * nSouPixels: the output numbers of contributing source cells to each target cell
* int nTar: the number of target cells
* Output:
* double * tarVal: the output (average) values at target cells
* double * tarSD: the standard deviation (SD) value at target cells (can be NULL if no SD values need to be reported)
* int * nSouPixels: the output numbers of contributing source cells to each target cell
*/
void summaryInterpolate(double * souVal, int * souNNTarID, int nSou, double * tarVal, double * tarSD, int * nSouPixels, int nTar) {
int i;
for(i = 0; i < nTar; i++) {
tarVal[i] = 0;
if (tarSD != NULL) {
tarSD[i] = 0;
}
nSouPixels[i] = 0;
}
int nnTarID;
for(i = 0; i < nSou; i++) {
nnTarID = souNNTarID[i];
if(nnTarID >= 0 && souVal[i] >= 0) {
tarVal[nnTarID] += souVal[i];
if (tarSD != NULL) {
tarSD[nnTarID] += souVal[i] * souVal[i];
}
nSouPixels[nnTarID] ++;
}
}
for(i = 0; i < nTar; i++) {
if(nSouPixels[i] > 0) {
tarVal[i] = tarVal[i] / nSouPixels[i];
if (tarSD != NULL) {
if(tarSD[i] / nSouPixels[i] - tarVal[i] * tarVal[i] < 0) {
tarSD[i] = 0;
}
else {
tarSD[i] = sqrt(tarSD[i] / nSouPixels[i] - tarVal[i] * tarVal[i]);
}
}
}
else {
tarVal[i] = -999;
if (tarSD != NULL) {
tarSD[i] = -999;
}
}
}
}
/**
* NAME: clipping
* DESCRIPTION: Clip output radiance values based on mask
* PARAMETERS:
* double * val: the output radicance values to be clipped; radiance values will be set to -999 if the mask value is also -999
* double * mask: the mask for cliping, could be the radiance value after resampling
* int nPixels: the number of pixels for both val and mask
*/
void clipping(double * val, double * mask, int nPixels)
{
int i = 0;
for(i = 0; i < nPixels; i++)
{
if(mask[i] == -999)
{
val[i] = -999;
}
}
}