-
Notifications
You must be signed in to change notification settings - Fork 0
/
griddingAndGranulation.c
893 lines (751 loc) · 30.2 KB
/
griddingAndGranulation.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/* file_Date = $Date$
* file_Revision = $Revision$
* file_Author = $Author$
* file_HeadURL = $HeadURL$
* file_Id = $Id$
*
* ________________________
* griddingAndGranulation.c
* ________________________
*
* A library for transforming datasets between regular and
* non-regular grids.
*
* Created by Geoff Cureton on 2011-06-27.
* Copyright (c) 2011 University of Wisconsin SSEC. All rights reserved.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
int test_ctypes(double *dblArr, long longVar, int intVar)
{
long idx;
printf("longVar = %ld\n",longVar);
printf("intVar = %d\n",intVar);
printf("dblArr = \n");
for (idx=0;idx<longVar;idx++){
printf("%f\n",dblArr[idx]);
}
return(0);
}
/*
* _________
* gran2grid
* _________
*
* This routine takes the data[nData] array, defined on the non-regular grid
* defined by lat[ndata] and lon[nData], and regrids to the regular grid
* gridData[nGridRows*nGridCols] defined by gridLat[nGridRows*nGridCols] and
* gridLon[nGridRows*nGridRows].
*
* Inputs:
*
* lat : Non-regular latitude grid (nData)
* lon : Non-regular longitude grid (nData)
* data : Data array on non-regular grid defined by lat and lon (nData)
* nData : Length of non-regular grid arrays
* gridLat : Regular latitude grid (nGridRows*nGridCols)
* gridLon : Regular longitude grid (nGridRows*nGridCols)
* nGridRows : Number of grid rows of the regular grid
* nGridCols : Number of grid columns of the regular grid
*
* Outputs:
*
* gridData : Data array on grid defined by gridLat and gridLon
* grid (nGridRows*nGridCols)
* gridDataIdx : Array of indices into data, defined by gridLat and gridLon
* grid (nGridRows*nGridCols)
*
*
*/
int gran2grid(double *lat,
double *lon,
double *data,
long nData,
double *gridLat,
double *gridLon,
double *gridData,
long *gridDataIdx,
int nGridRows,
int nGridCols
)
{
long idx;
int latGridIdxLo, latGridIdxHi, lonGridIdxLo, lonGridIdxHi;
double latVal,lonVal,dataVal;
double gridLatInc,gridLonInc;
int gridLatPt,gridLonPt;
int gridLatPoints[4], gridLonPoints[4];
double minDist,dist,latDist,lonDist;
double gridLatVal,gridLonVal;
int crnrPt,snapCrnrPt;
int rowInBounds,colInBounds;
printf("Shape of data is (%ld)\n",nData);
printf("Shape of gridData is (%d, %d)\n", nGridRows,nGridCols);
printf("TRUE = %d\n", TRUE);
printf("FALSE = %d\n", FALSE);
int numShow = 10;
int i;
printf("\nDisplaying lat,lon,data...\n");
for (i=0;i<numShow;i++){
printf("%6.1f %6.1f %6.1f\n",lat[i],lon[i],data[i]);
}
// Determine the lat and lon grid spacings
printf("nGridRows = %d\n", nGridRows);
printf("nGridCols = %d\n", nGridCols);
printf("gridLat[nGridCols] = %f\n", gridLat[nGridCols]);
printf("gridLat[0] = %f\n", gridLat[0]);
printf("gridLon[1] = %f\n", gridLon[1]);
printf("gridLon[0] = %f\n", gridLon[0]);
gridLatInc = fabs(gridLat[nGridCols]-gridLat[0]);
gridLonInc = fabs(gridLon[1]-gridLon[0]);
printf("gridLatInc,gridLonInc = (%6.1f %6.1f)\n",gridLatInc,gridLonInc);
// Loop through non-gridded data, find matching gridpoint and assign
// data value to that gridpoint
for (idx=0;idx<nData;idx++){
/*for (idx=0;idx<numShow;idx++){*/
latVal = lat[idx];
lonVal = lon[idx];
dataVal = data[idx];
// Determine lat/lon grid indices which bound the non-gridded point
latGridIdxLo = (int) floor((latVal-gridLat[0])/gridLatInc);
latGridIdxHi = latGridIdxLo + 1;
lonGridIdxLo = (int) floor((lonVal-gridLon[0])/gridLonInc);
lonGridIdxHi = lonGridIdxLo + 1;
rowInBounds = TRUE;
colInBounds = TRUE;
// If the grid indices bounding the non-gridded point are off the
// grid, mark this non-gridded point as out-of-bounds.
if ((latGridIdxLo<0) || (latGridIdxHi>=nGridRows)){
rowInBounds = FALSE;
/*printf("Row idx out of bounds...\n");*/
}
if ((lonGridIdxLo<0) || (lonGridIdxHi>=nGridCols)){
colInBounds = FALSE;
/*printf("Column idx out of bounds...\n");*/
}
if (rowInBounds==FALSE){
continue;
}else if (colInBounds==FALSE){
continue;
}else{
gridLatPoints[0] = latGridIdxLo;
gridLatPoints[1] = latGridIdxLo;
gridLatPoints[2] = latGridIdxHi;
gridLatPoints[3] = latGridIdxHi;
gridLonPoints[0] = lonGridIdxLo;
gridLonPoints[1] = lonGridIdxHi;
gridLonPoints[2] = lonGridIdxLo;
gridLonPoints[3] = lonGridIdxHi;
minDist = 1.e+30;
snapCrnrPt = 0;
// Loop through the corners bounding the non-gridded point
for (crnrPt=0;crnrPt<4;crnrPt++){
gridLatPt = (int) gridLatPoints[crnrPt];
gridLonPt = (int) gridLonPoints[crnrPt];
// Get the lat and lon values at this corner
gridLatVal = gridLat[nGridCols * gridLatPt + gridLonPt];
gridLonVal = gridLon[nGridCols * gridLatPt + gridLonPt];
// The Pythagorean distance of this corner from the non-gridded
// data point...
latDist = latVal-gridLatVal;
lonDist = lonVal-gridLonVal;
dist = sqrt(latDist*latDist + lonDist*lonDist);
// If this distance is the smallest so far, save the corner
// idx and distance...
if (dist < minDist){
snapCrnrPt = crnrPt;
minDist = dist;
}
}
// Get the grid indices of the corner closest to the non-gridded point
gridLatPt = (int) gridLatPoints[snapCrnrPt];
gridLonPt = (int) gridLonPoints[snapCrnrPt];
// Assign the value of the non-gridded data point to the grid point
// closest to it.
gridData[nGridCols * gridLatPt + gridLonPt] = dataVal;
/*printf("gridDataIdx indices are (%d, %d)\n",gridLatPt,gridLonPt);*/
// Save the index of the non-gridded data point to the nearest grid
// point.
gridDataIdx[nGridCols * gridLatPt + gridLonPt] = idx;
// TODO : Save subsequent data indices in the same gridcell
}
}
return(0);
}
/*
* _________
* gran2grid_moments
* _________
*
* This routine takes the data[nData] array, defined on the non-regular grid
* defined by lat[ndata] and lon[nData], and regrids to the regular grid
* gridData[nGridRows*nGridCols] defined by gridLat[nGridRows*nGridCols] and
* gridLon[nGridRows*nGridRows].
*
* In order to facilitate computation of the various statistical descriptors,
* we save on the defined grid the various sums (x, x^2, x^3, etc...), and the
* number of observations added to each grid cell. From these datasets the various
* gridded cumulants can be computed.
*
* Inputs:
*
* lat : Non-regular latitude grid (nData)
* lon : Non-regular longitude grid (nData)
* data : Data array on non-regular grid defined by lat and lon (nData)
* nData : Length of non-regular grid arrays
* gridLat : Regular latitude grid (nGridRows*nGridCols)
* gridLon : Regular longitude grid (nGridRows*nGridCols)
* nGridRows : Number of grid rows of the regular grid
* nGridCols : Number of grid columns of the regular grid
*
* Outputs:
*
* gridData : Data array on grid defined by gridLat and gridLon
* grid (nGridRows*nGridCols)
* gridDataIdx : Array of indices into data, defined by gridLat and gridLon
* grid (nGridRows*nGridCols)
*
*
*/
int gran2grid_moments(double *lat,
double *lon,
double *data,
long nData,
double *gridLat,
double *gridLon,
double *gridData,
long *gridDataIdx,
int nGridRows,
int nGridCols
)
{
long idx;
int latGridIdxLo, latGridIdxHi, lonGridIdxLo, lonGridIdxHi;
double latVal,lonVal,dataVal;
double gridLatInc,gridLonInc;
int gridLatPt,gridLonPt;
int gridLatPoints[4], gridLonPoints[4];
double minDist,dist,latDist,lonDist;
double gridLatVal,gridLonVal;
int crnrPt,snapCrnrPt;
int rowInBounds,colInBounds;
printf("Shape of data is (%ld)\n",nData);
printf("Shape of gridData is (%d, %d)\n", nGridRows,nGridCols);
printf("TRUE = %d\n", TRUE);
printf("FALSE = %d\n", FALSE);
int numShow = 10;
int i;
printf("\nDisplaying lat,lon,data...\n");
for (i=0;i<numShow;i++){
printf("%6.1f %6.1f %6.1f\n",lat[i],lon[i],data[i]);
}
// Determine the lat and lon grid spacings
printf("nGridRows = %d\n", nGridRows);
printf("nGridCols = %d\n", nGridCols);
printf("gridLat[nGridCols] = %f\n", gridLat[nGridCols]);
printf("gridLat[0] = %f\n", gridLat[0]);
printf("gridLon[1] = %f\n", gridLon[1]);
printf("gridLon[0] = %f\n", gridLon[0]);
gridLatInc = fabs(gridLat[nGridCols]-gridLat[0]);
gridLonInc = fabs(gridLon[1]-gridLon[0]);
printf("gridLatInc,gridLonInc = (%6.1f %6.1f)\n",gridLatInc,gridLonInc);
// Loop through non-gridded data, find matching gridpoint and assign
// data value to that gridpoint
for (idx=0;idx<nData;idx++){
latVal = lat[idx];
lonVal = lon[idx];
dataVal = data[idx];
// Determine lat/lon grid indices which bound the non-gridded point
latGridIdxLo = (int) floor((latVal-gridLat[0])/gridLatInc);
latGridIdxHi = latGridIdxLo + 1;
lonGridIdxLo = (int) floor((lonVal-gridLon[0])/gridLonInc);
lonGridIdxHi = lonGridIdxLo + 1;
rowInBounds = TRUE;
colInBounds = TRUE;
// If the grid indices bounding the non-gridded point are off the
// grid, mark this non-gridded point as out-of-bounds.
if ((latGridIdxLo<0) || (latGridIdxHi>=nGridRows)){
rowInBounds = FALSE;
}
if ((lonGridIdxLo<0) || (lonGridIdxHi>=nGridCols)){
colInBounds = FALSE;
}
if (rowInBounds==FALSE){
continue;
}else if (colInBounds==FALSE){
continue;
}else{
gridLatPoints[0] = latGridIdxLo;
gridLatPoints[1] = latGridIdxLo;
gridLatPoints[2] = latGridIdxHi;
gridLatPoints[3] = latGridIdxHi;
gridLonPoints[0] = lonGridIdxLo;
gridLonPoints[1] = lonGridIdxHi;
gridLonPoints[2] = lonGridIdxLo;
gridLonPoints[3] = lonGridIdxHi;
minDist = 1.e+30;
snapCrnrPt = 0;
// Loop through the corners bounding the non-gridded point
for (crnrPt=0;crnrPt<4;crnrPt++){
gridLatPt = (int) gridLatPoints[crnrPt];
gridLonPt = (int) gridLonPoints[crnrPt];
// Get the lat and lon values at this corner
gridLatVal = gridLat[nGridCols * gridLatPt + gridLonPt];
gridLonVal = gridLon[nGridCols * gridLatPt + gridLonPt];
// The Pythagorean distance of this corner from the non-gridded
// data point...
latDist = latVal-gridLatVal;
lonDist = lonVal-gridLonVal;
dist = sqrt(latDist*latDist + lonDist*lonDist);
// If this distance is the smallest so far, save the corner
// idx and distance...
if (dist < minDist){
snapCrnrPt = crnrPt;
minDist = dist;
}
}
// Get the grid indices of the corner closest to the non-gridded point
gridLatPt = (int) gridLatPoints[snapCrnrPt];
gridLonPt = (int) gridLonPoints[snapCrnrPt];
// Add the value of the non-gridded data point to the grid point
// closest to it.
gridData[nGridCols * gridLatPt + gridLonPt] += dataVal;
// Increment the number of obervations which have been attributed to this grid
// point.
gridDataIdx[nGridCols * gridLatPt + gridLonPt] += 1;
}
}
return(0);
}
/*
* _________
* grid2gran_nearest
* _________
*
* This routine takes the regular grid gridData[nGridRows*nGridCols],
* defined by gridLat[nGridRows*nGridCols] and gridLon[nGridRows*nGridRows],
* and regrids to the data[nData] array, defined on the non-regular grid
* defined by lat[ndata] and lon[nData].
*
* Inputs:
*
* gridLat : Regular latitude grid (nGridRows*nGridCols)
* gridLon : Regular longitude grid (nGridRows*nGridCols)
* gridData : Data array on grid defined by gridLat and gridLon
* grid (nGridRows*nGridCols)
* lat : Non-regular latitude grid (nData)
* lon : Non-regular longitude grid (nData)
* nData : Length of non-regular grid arrays
* nGridRows : Number of grid rows of the regular grid
* nGridCols : Number of grid columns of the regular grid
*
* Outputs:
*
* data : Data array on non-regular grid defined by lat and lon (nData)
* gridDataIdx : Array of indices into gridData, defined by lat and lon
* non-regular grid (nData)
*
*
*/
int grid2gran_nearest(double *lat,
double *lon,
double *data,
long nData,
double *gridLat,
double *gridLon,
double *gridData,
long *gridDataIdx,
int nGridRows,
int nGridCols
)
{
long idx;
int latGridIdxLo, latGridIdxHi, lonGridIdxLo, lonGridIdxHi;
double latVal,lonVal,dataVal;
double gridLatInc,gridLonInc;
int gridLatPt,gridLonPt;
int gridLatPoints[4], gridLonPoints[4];
double minDist,dist,latDist,lonDist;
double gridLatVal,gridLonVal;
int crnrPt,snapCrnrPt;
int rowInBounds,colInBounds;
/*printf("Shape of data is (%ld)\n",nData);*/
/*printf("Shape of gridData is (%d, %d)\n", nGridRows,nGridCols);*/
/*printf("TRUE = %d\n", TRUE);*/
/*printf("FALSE = %d\n", FALSE);*/
int numShow = 10;
int i;
/*printf("\nDisplaying lat,lon,data...\n");*/
/*for (i=0;i<numShow;i++){*/
/*printf("%6.1f %6.1f %6.1f\n",lat[i],lon[i],data[i]);*/
/*}*/
// Determine the lat and lon grid spacings
/*printf("nGridRows = %d\n", nGridRows);*/
/*printf("nGridCols = %d\n", nGridCols);*/
/*printf("gridLat[nGridCols] = %f\n", gridLat[nGridCols]);*/
/*printf("gridLat[0] = %f\n", gridLat[0]);*/
/*printf("gridLon[1] = %f\n", gridLon[1]);*/
/*printf("gridLon[0] = %f\n", gridLon[0]);*/
gridLatInc = fabs(gridLat[nGridCols]-gridLat[0]);
gridLonInc = fabs(gridLon[1]-gridLon[0]);
/*printf("gridLatInc,gridLonInc = (%8.3f %8.3f)\n",gridLatInc,gridLonInc);*/
// Loop through non-gridded data points, find matching gridpoint, and assign
// gridpoint data value to that non-gridded data point.
for (idx=0;idx<nData;idx++){
/*for (idx=0;idx<numShow;idx++){*/
latVal = lat[idx];
lonVal = lon[idx];
// Determine lat/lon grid indices which bound the non-gridded point
latGridIdxLo = (int) floor((latVal-gridLat[0])/gridLatInc);
latGridIdxHi = latGridIdxLo + 1;
lonGridIdxLo = (int) floor((lonVal-gridLon[0])/gridLonInc);
lonGridIdxHi = lonGridIdxLo + 1;
rowInBounds = TRUE;
colInBounds = TRUE;
// If the grid indices bounding the non-gridded point are off the
// grid, mark this non-gridded point as out-of-bounds.
if ((latGridIdxLo<0) || (latGridIdxHi>=nGridRows)){
rowInBounds = FALSE;
/*printf("Row idx out of bounds...\n");*/
}
if ((lonGridIdxLo<0) || (lonGridIdxHi>=nGridCols)){
colInBounds = FALSE;
/*printf("Column idx out of bounds...\n");*/
}
if (rowInBounds==FALSE){
continue;
}else if (colInBounds==FALSE){
continue;
}else{
gridLatPoints[0] = latGridIdxLo;
gridLatPoints[1] = latGridIdxLo;
gridLatPoints[2] = latGridIdxHi;
gridLatPoints[3] = latGridIdxHi;
gridLonPoints[0] = lonGridIdxLo;
gridLonPoints[1] = lonGridIdxHi;
gridLonPoints[2] = lonGridIdxLo;
gridLonPoints[3] = lonGridIdxHi;
minDist = 1.e+30;
snapCrnrPt = 0;
// Loop through the corners bounding the non-gridded point
for (crnrPt=0;crnrPt<4;crnrPt++){
gridLatPt = (int) gridLatPoints[crnrPt];
gridLonPt = (int) gridLonPoints[crnrPt];
// Get the lat and lon values at this corner
gridLatVal = gridLat[nGridCols * gridLatPt + gridLonPt];
gridLonVal = gridLon[nGridCols * gridLatPt + gridLonPt];
// The Pythagorean distance of this corner from the non-gridded
// data point...
latDist = latVal-gridLatVal;
lonDist = lonVal-gridLonVal;
dist = sqrt(latDist*latDist + lonDist*lonDist);
// If this distance is the smallest so far, save the corner
// idx and distance...
if (dist < minDist){
snapCrnrPt = crnrPt;
minDist = dist;
}
}
// Get the grid indices of the corner closest to the non-gridded point
gridLatPt = (int) gridLatPoints[snapCrnrPt];
gridLonPt = (int) gridLonPoints[snapCrnrPt];
// Assign the value of the grid point closest to the non-gridded data
// point to that non-gridded data point .
data[idx] = gridData[nGridCols * gridLatPt + gridLonPt];
/*printf("gridDataIdx indices are (%d, %d)\n",gridLatPt,gridLonPt);*/
// Save the index of the closest grid data point to the non-gridded
// data point.
gridDataIdx[idx] = nGridCols * gridLatPt + gridLonPt;
// TODO : Save subsequent data indices in the same gridcell
}
}
return(0);
}
/*
* _________
* grid2gran_weightedAvg
* _________
*
* This routine takes the regular grid gridData[nGridRows*nGridCols],
* defined by gridLat[nGridRows*nGridCols] and gridLon[nGridRows*nGridRows],
* and regrids to the data[nData] array, defined on the non-regular grid
* defined by lat[ndata] and lon[nData].
*
* Inputs:
*
* gridLat : Non-regular latitude grid (nGridRows*nGridCols)
* gridLon : Non-regular longitude grid (nGridRows*nGridCols)
* gridData : Data array on grid defined by gridLat and gridLon
* grid (nGridRows*nGridCols)
* lat : Non-regular latitude grid (nData)
* lon : Non-regular longitude grid (nData)
* nData : Length of non-regular grid arrays
* nGridRows : Number of grid rows of the regular grid
* nGridCols : Number of grid columns of the regular grid
*
* Outputs:
*
* data : Data array on non-regular grid defined by lat and lon (nData)
* gridDataIdx : Array of indices into gridData, defined by lat and lon
* non-regular grid (nData)
*
*
*/
int grid2gran_weightedAvg(double *lat,
double *lon,
double *data,
long nData,
double *gridLat,
double *gridLon,
double *gridData,
long *gridDataIdx,
int nGridRows,
int nGridCols
)
{
long idx;
int latGridIdxLo, latGridIdxHi, lonGridIdxLo, lonGridIdxHi;
double latVal,lonVal,dataVal;
double gridLatInc,gridLonInc;
int gridLatPt,gridLonPt;
int gridLatPoints[4], gridLonPoints[4];
double weight, weights[4], weightSum, weightedSum, eps;
double wAvgData;
double minDist,dist,latDist,lonDist;
double gridLatVal,gridLonVal;
int crnrPt,snapCrnrPt;
int rowInBounds,colInBounds;
/*printf("Shape of data is (%ld)\n",nData);*/
/*printf("Shape of gridData is (%d, %d)\n", nGridRows,nGridCols);*/
/*printf("TRUE = %d\n", TRUE);*/
/*printf("FALSE = %d\n", FALSE);*/
int numShow = 10;
int i;
/*printf("\nDisplaying lat,lon,data...\n");*/
/*for (i=0;i<numShow;i++){*/
/*printf("%6.1f %6.1f %6.1f\n",lat[i],lon[i],data[i]);*/
/*}*/
// Determine the lat and lon grid spacings
/*printf("nGridRows = %d\n", nGridRows);*/
/*printf("nGridCols = %d\n", nGridCols);*/
/*printf("gridLat[nGridCols] = %f\n", gridLat[nGridCols]);*/
/*printf("gridLat[0] = %f\n", gridLat[0]);*/
/*printf("gridLon[1] = %f\n", gridLon[1]);*/
/*printf("gridLon[0] = %f\n", gridLon[0]);*/
gridLatInc = fabs(gridLat[nGridCols]-gridLat[0]);
gridLonInc = fabs(gridLon[1]-gridLon[0]);
/*printf("gridLatInc,gridLonInc = (%6.1f %6.1f)\n",gridLatInc,gridLonInc);*/
// Loop through non-gridded data points, find matching gridpoint, and assign
// gridpoint data value to that non-gridded data point.
eps = 1.0e-12;
for (idx=0;idx<nData;idx++){
/*for (idx=0;idx<numShow;idx++){*/
latVal = lat[idx];
lonVal = lon[idx];
// Determine lat/lon grid indices which bound the non-gridded point
latGridIdxLo = (int) floor((latVal-gridLat[0])/gridLatInc);
latGridIdxHi = latGridIdxLo + 1;
lonGridIdxLo = (int) floor((lonVal-gridLon[0])/gridLonInc);
lonGridIdxHi = lonGridIdxLo + 1;
rowInBounds = TRUE;
colInBounds = TRUE;
// If the grid indices bounding the non-gridded point are off the
// grid, mark this non-gridded point as out-of-bounds.
if ((latGridIdxLo<0) || (latGridIdxHi>=nGridRows)){
rowInBounds = FALSE;
/*printf("Row idx out of bounds...\n");*/
}
if ((lonGridIdxLo<0) || (lonGridIdxHi>=nGridCols)){
colInBounds = FALSE;
/*printf("Column idx out of bounds...\n");*/
}
if (rowInBounds==FALSE){
continue;
}else if (colInBounds==FALSE){
continue;
}else{
gridLatPoints[0] = latGridIdxLo;
gridLatPoints[1] = latGridIdxLo;
gridLatPoints[2] = latGridIdxHi;
gridLatPoints[3] = latGridIdxHi;
gridLonPoints[0] = lonGridIdxLo;
gridLonPoints[1] = lonGridIdxHi;
gridLonPoints[2] = lonGridIdxLo;
gridLonPoints[3] = lonGridIdxHi;
minDist = 1.e+30;
snapCrnrPt = 0;
weightSum = 0.;
weightedSum = 0.;
wAvgData = 0.;
// Loop through the corners bounding the non-gridded point,
// to calculate the inverse-distance weights.
for (crnrPt=0;crnrPt<4;crnrPt++){
gridLatPt = (int) gridLatPoints[crnrPt];
gridLonPt = (int) gridLonPoints[crnrPt];
// Get the lat and lon values at this corner
gridLatVal = gridLat[nGridCols * gridLatPt + gridLonPt];
gridLonVal = gridLon[nGridCols * gridLatPt + gridLonPt];
// The Pythagorean distance of this corner from the non-gridded
// data point...
latDist = latVal-gridLatVal;
lonDist = lonVal-gridLonVal;
dist = sqrt(latDist*latDist + lonDist*lonDist);
weight = 1./(dist + eps);
weightedSum += weight * gridData[nGridCols * gridLatPt + gridLonPt];
weightSum += weight;
}
// Assign the value of the grid point closest to the non-gridded data
// point to that non-gridded data point .
data[idx] = weightedSum / weightSum;
// Save the index of the closest grid data point to the non-gridded
// data point.
gridDataIdx[idx] = nGridCols * gridLatPt + gridLonPt;
}
}
return(0);
}
/*
* _________
* grid2gran_bilinearInterp
* _________
*
* This routine takes the regular grid gridData[nGridRows*nGridCols],
* defined by gridLat[nGridRows*nGridCols] and gridLon[nGridRows*nGridRows],
* and regrids to the data[nData] array, defined on the non-regular grid
* defined by lat[ndata] and lon[nData].
*
* Inputs:
*
* gridLat : Non-regular latitude grid (nGridRows*nGridCols)
* gridLon : Non-regular longitude grid (nGridRows*nGridCols)
* gridData : Data array on grid defined by gridLat and gridLon
* grid (nGridRows*nGridCols)
* lat : Non-regular latitude grid (nData)
* lon : Non-regular longitude grid (nData)
* nData : Length of non-regular grid arrays
* nGridRows : Number of grid rows of the regular grid
* nGridCols : Number of grid columns of the regular grid
*
* Outputs:
*
* data : Data array on non-regular grid defined by lat and lon (nData)
* gridDataIdx : Array of indices into gridData, defined by lat and lon
* non-regular grid (nData)
*
*
* The notation below is based on the description at
* http:https://en.wikipedia.org/wiki/Bilinear_interpolation
*
* | | |
* |Q_12 |R_2 |Q_22
* lat_hi ------o----------o----------o----- \ \
* | | | | |
* | | | |-- dlat_plus |
* | | | | |
* latVal --|--------- P ---------|----- / |-- dlat
* | | | \ |
* | | | | |
* | | | |-- dlat_plus |
* |Q_11 |R_1 |Q_21 | |
* lat_lo ------o----------o----------o----- / /
* | | |
* | | |
* | lonVal |
* lon_lo lon_hi
*
* \__________/\_________/
* | |
* dlon_minus dlon_plus
*
* \_____________________/
* |
* dlon
*
*
*/
int grid2gran_bilinearInterp(double *lat,
double *lon,
double *data,
long nData,
double *gridLat,
double *gridLon,
double *gridData,
long *gridDataIdx,
int nGridRows,
int nGridCols
)
{
long idx;
int latGridIdxLo, latGridIdxHi, lonGridIdxLo, lonGridIdxHi;
double latVal,lonVal,dataVal;
double gridLatInc,gridLonInc;
double lat_lo, lat_hi, dlat, dlat_minus, dlat_plus;
double lon_lo, lon_hi, dlon, dlon_minus, dlon_plus;
double Q_11, Q_12, Q_21, Q_22;
double f_R1, f_R2, f_P;
int gridLatPt,gridLonPt;
double eps;
double gridLatVal,gridLonVal;
int rowInBounds,colInBounds;
int numShow = 10;
int i;
// Calculate the grid lat and lon increments
gridLatInc = fabs(gridLat[nGridCols]-gridLat[0]);
gridLonInc = fabs(gridLon[1]-gridLon[0]);
// Loop through non-gridded data points, find the bounding gridpoints,
// bilinearly interpolate.
eps = 1.0e-12;
for (idx=0;idx<nData;idx++){
latVal = lat[idx];
lonVal = lon[idx];
// Determine lat/lon grid indices which bound the non-gridded point
latGridIdxLo = (int) floor((latVal-gridLat[0])/gridLatInc);
latGridIdxHi = latGridIdxLo + 1;
lonGridIdxLo = (int) floor((lonVal-gridLon[0])/gridLonInc);
lonGridIdxHi = lonGridIdxLo + 1;
rowInBounds = TRUE;
colInBounds = TRUE;
// If the grid indices bounding the non-gridded point are off the
// grid, mark this non-gridded point as out-of-bounds.
if ((latGridIdxLo<0) || (latGridIdxHi>=nGridRows)){
rowInBounds = FALSE;
}
if ((lonGridIdxLo<0) || (lonGridIdxHi>=nGridCols)){
colInBounds = FALSE;
}
if (rowInBounds==FALSE){
continue;
}else if (colInBounds==FALSE){
continue;
}else{
lat_lo = gridLat[nGridCols * latGridIdxLo + lonGridIdxLo];
lat_hi = gridLat[nGridCols * latGridIdxHi + lonGridIdxLo];
dlat_minus = latVal - lat_lo;
dlat_plus = lat_hi - latVal;
dlat = lat_hi - lat_lo;
lon_lo = gridLon[nGridCols * latGridIdxLo + lonGridIdxLo];
lon_hi = gridLon[nGridCols * latGridIdxLo + lonGridIdxHi];
dlon_minus = lonVal - lon_lo;
dlon_plus = lon_hi - lonVal;
dlon = lon_hi - lon_lo;
Q_11 = gridData[nGridCols * latGridIdxLo + lonGridIdxLo];
Q_12 = gridData[nGridCols * latGridIdxHi + lonGridIdxLo];
Q_21 = gridData[nGridCols * latGridIdxLo + lonGridIdxHi];
Q_22 = gridData[nGridCols * latGridIdxHi + lonGridIdxHi];
f_R1 = (dlon_plus * Q_11 + dlon_minus * Q_21) / dlon;
f_R2 = (dlon_plus * Q_12 + dlon_minus * Q_22) / dlon;
f_P = (dlat_plus * f_R1 + dlat_minus * f_R2) / dlat;
// Assign the value of the grid point closest to the non-gridded data
// point to that non-gridded data point .
data[idx] = f_P;
// Save the index of the closest grid data point to the non-gridded
// data point.
gridDataIdx[idx] = nGridCols * gridLatPt + gridLonPt;
}
}
return(0);
}