-
Notifications
You must be signed in to change notification settings - Fork 18
/
harp-binning.c
3287 lines (3022 loc) · 118 KB
/
harp-binning.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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015-2024 S[&]T, The Netherlands.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "harp-internal.h"
#include "harp-geometry.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LENGTH 128
#define LATLON_BLOCK_SIZE 1024
typedef enum binning_type_enum
{
binning_skip,
binning_remove,
binning_average,
binning_uncertainty,
binning_sum, /* only used for int32_t and float data */
binning_angle, /* will use averaging using 2D vectors */
binning_time_min,
binning_time_max,
binning_time_average
} binning_type;
static binning_type get_binning_type(harp_variable *variable, int force_correlated_uncertainty)
{
long variable_name_length = (long)strlen(variable->name);
int i;
/* any variable with a time dimension that is not the first dimension gets removed */
for (i = 1; i < variable->num_dimensions; i++)
{
if (variable->dimension[i] == harp_dimension_time)
{
return binning_remove;
}
}
/* only keep valid count variables */
if (variable_name_length >= 5 && strcmp(&variable->name[variable_name_length - 5], "count") == 0)
{
if (variable->num_dimensions < 1 || variable->dimension_type[0] != harp_dimension_time ||
variable->data_type != harp_type_int32 || variable->unit != NULL)
{
return binning_remove;
}
if (variable_name_length == 5 && variable->num_dimensions != 1)
{
return binning_remove;
}
return binning_sum;
}
/* only keep valid weight variables */
if (variable_name_length >= 6 && strcmp(&variable->name[variable_name_length - 6], "weight") == 0)
{
if (variable->num_dimensions < 1 || variable->dimension_type[0] != harp_dimension_time ||
variable->data_type != harp_type_float || variable->unit != NULL)
{
return binning_remove;
}
return binning_sum;
}
/* we only bin variables with a time dimension */
if (variable->num_dimensions == 0 || variable->dimension_type[0] != harp_dimension_time)
{
return binning_skip;
}
/* variables with enumeration values get removed */
if (variable->num_enum_values > 0)
{
return binning_remove;
}
/* we can't bin string values */
if (variable->data_type == harp_type_string)
{
return binning_remove;
}
/* we can't bin values that have no unit */
if (variable->unit == NULL)
{
return binning_remove;
}
if (strstr(variable->name, "_uncertainty") != NULL)
{
if (strstr(variable->name, "_uncertainty_systematic") != NULL)
{
/* always propagate uncertainty assuming full correlation for the systematic part */
return binning_average;
}
if (strstr(variable->name, "_uncertainty_random") != NULL)
{
/* always propagate uncertainty assuming no correlation for the random part */
return binning_uncertainty;
}
/* for the total uncertainty let it depend on the given parameter/option */
if (force_correlated_uncertainty || harp_get_option_propagate_uncertainty() == 1)
{
/* propagate uncertainty assuming full correlation */
return binning_average;
}
/* propagate uncertainty assuming no correlation */
return binning_uncertainty;
}
/* we can't bin averaging kernels */
if (strstr(variable->name, "_avk") != NULL)
{
return binning_remove;
}
/* we can't bin latitude/longitude bounds if they define an area */
if (strcmp(variable->name, "latitude_bounds") == 0 || strcmp(variable->name, "longitude_bounds") == 0)
{
if (variable->num_dimensions > 0 &&
variable->dimension_type[variable->num_dimensions - 1] == harp_dimension_independent &&
variable->dimension[variable->num_dimensions - 1] > 2)
{
return binning_remove;
}
}
if (strstr(variable->name, "latitude") != NULL || strstr(variable->name, "longitude") != NULL ||
strstr(variable->name, "angle") != NULL || strstr(variable->name, "direction") != NULL)
{
return binning_angle;
}
/* use minimum/maximum for datetime start/stop */
if (variable->num_dimensions == 1)
{
if (strcmp(variable->name, "datetime_start") == 0)
{
return binning_time_min;
}
if (strcmp(variable->name, "datetime_stop") == 0)
{
return binning_time_max;
}
}
/* use average by default */
return binning_average;
}
static binning_type get_spatial_binning_type(harp_variable *variable)
{
binning_type type = get_binning_type(variable, 1);
if (type != binning_remove && type != binning_skip)
{
/* remove all latitude/longitude variables */
if (strstr(variable->name, "latitude") != NULL || strstr(variable->name, "longitude") != NULL)
{
return binning_remove;
}
/* existing count and weight variables are removed if we perform a spatial bin */
if (type == binning_sum)
{
return binning_remove;
}
/* use a plain 'bin()' on datetime variables and check that they are one dimensional and dependent on time */
if (strcmp(variable->name, "datetime") == 0 || strcmp(variable->name, "datetime_length") == 0)
{
if (variable->num_dimensions != 1 || variable->dimension_type[0] != harp_dimension_time)
{
return binning_remove;
}
return binning_time_average;
}
}
return type;
}
/* find a <variable->name>_count variable.
* If the variable exists but is invalid its entry in the bintype array will be set to binning_remove.
*/
static int get_count_variable_for_variable(harp_product *product, harp_variable *variable, binning_type *bintype,
harp_variable **count_variable)
{
char variable_name[MAX_NAME_LENGTH];
int index;
int i;
*count_variable = NULL;
snprintf(variable_name, MAX_NAME_LENGTH, "%s_count", variable->name);
if (!harp_product_has_variable(product, variable_name))
{
return 0;
}
if (harp_product_get_variable_index_by_name(product, variable_name, &index) != 0)
{
return -1;
}
if (bintype[index] == binning_remove)
{
return 0;
}
/* make sure that the dimensions of the count variable match the dimensions of the given variable */
if (product->variable[index]->num_dimensions != variable->num_dimensions)
{
bintype[index] = binning_remove;
return 0;
}
for (i = 0; i < product->variable[index]->num_dimensions; i++)
{
if (product->variable[index]->dimension_type[i] != variable->dimension_type[i] ||
product->variable[index]->dimension[i] != variable->dimension[i])
{
bintype[index] = binning_remove;
return 0;
}
}
*count_variable = product->variable[index];
return 0;
}
/* find a <variable->name>_weight variable.
* If the variable exists but is invalid its entry in the bintype array will be set to binning_remove.
*/
static int get_weight_variable_for_variable(harp_product *product, harp_variable *variable, binning_type *bintype,
harp_variable **weight_variable)
{
char variable_name[MAX_NAME_LENGTH];
int index;
int i;
*weight_variable = NULL;
snprintf(variable_name, MAX_NAME_LENGTH, "%s_weight", variable->name);
if (!harp_product_has_variable(product, variable_name))
{
return 0;
}
if (harp_product_get_variable_index_by_name(product, variable_name, &index) != 0)
{
return -1;
}
if (bintype[index] == binning_remove)
{
return 0;
}
/* make sure that the dimensions of the weight variable match the dimensions of the given variable */
if (product->variable[index]->num_dimensions != variable->num_dimensions)
{
bintype[index] = binning_remove;
return 0;
}
for (i = 0; i < product->variable[index]->num_dimensions; i++)
{
if (product->variable[index]->dimension_type[i] != variable->dimension_type[i] ||
product->variable[index]->dimension[i] != variable->dimension[i])
{
bintype[index] = binning_remove;
return 0;
}
}
*weight_variable = product->variable[index];
return 0;
}
/* get count values for each element in the provided variable.
* if a '<variable>_count' or 'count' variable exists then 'count' will be populated and the return value will be 1.
* if no applicable count variable could be found then the return value will be 0.
* the return value is -1 when an error is encountered.
*/
static int get_count_for_variable(harp_product *product, harp_variable *variable, binning_type *bintype, int32_t *count)
{
harp_variable *count_variable = NULL;
long i, j;
if (variable->num_dimensions < 1 || variable->dimension_type[0] != harp_dimension_time)
{
return 0;
}
if (get_count_variable_for_variable(product, variable, bintype, &count_variable) != 0)
{
return -1;
}
if (count_variable == NULL)
{
int index;
if (!harp_product_has_variable(product, "count"))
{
return 0;
}
if (harp_product_get_variable_index_by_name(product, "count", &index) != 0)
{
return -1;
}
if (bintype[index] == binning_remove)
{
return 0;
}
count_variable = product->variable[index];
}
/* store data into count parameter */
if (variable->num_elements == count_variable->num_elements)
{
memcpy(count, count_variable->data.int32_data, count_variable->num_elements * sizeof(int32_t));
}
else
{
long num_sub_elements = variable->num_elements / count_variable->num_elements;
assert(count_variable->num_elements < variable->num_elements);
for (i = 0; i < count_variable->num_elements; i++)
{
for (j = 0; j < num_sub_elements; j++)
{
count[i * num_sub_elements + j] = count_variable->data.int32_data[i];
}
}
}
return 1;
}
/* get weight values for each element in the provided variable.
* if a '<variable>_weight' or 'weight' variable exists then 'weight' will be populated and the return value will be 1.
* if no applicable weight variable could be found then the return value will be 0.
* the return value is -1 when an error is encountered.
*/
static int get_weight_for_variable(harp_product *product, harp_variable *variable, binning_type *bintype, float *weight)
{
harp_variable *weight_variable = NULL;
long i, j;
if (variable->num_dimensions <= 1 || variable->dimension_type[0] != harp_dimension_time)
{
return 0;
}
if (get_weight_variable_for_variable(product, variable, bintype, &weight_variable) != 0)
{
return -1;
}
if (weight_variable == NULL)
{
int index;
if (!harp_product_has_variable(product, "weight"))
{
return 0;
}
if (harp_product_get_variable_index_by_name(product, "weight", &index) != 0)
{
return -1;
}
if (bintype[index] == binning_remove)
{
return 0;
}
weight_variable = product->variable[index];
/* initial dimensions should match */
if (weight_variable->num_dimensions > variable->num_dimensions)
{
return 0;
}
for (i = 0; i < weight_variable->num_dimensions; i++)
{
if (weight_variable->dimension_type[i] != variable->dimension_type[i] ||
weight_variable->dimension[i] != variable->dimension[i])
{
return 0;
}
}
}
/* store data into weight parameter */
if (variable->num_elements == weight_variable->num_elements)
{
memcpy(weight, weight_variable->data.float_data, weight_variable->num_elements * sizeof(float));
}
else
{
long num_sub_elements = variable->num_elements / weight_variable->num_elements;
for (i = 0; i < weight_variable->num_elements; i++)
{
for (j = 0; j < num_sub_elements; j++)
{
weight[i * num_sub_elements + j] = weight_variable->data.float_data[i];
}
}
}
return 1;
}
static int add_count_variable(harp_product *product, binning_type *bintype, binning_type target_bintype,
const char *variable_name, int num_dimensions, harp_dimension_type *dimension_type,
long *dimension, int32_t *count)
{
char count_variable_name[MAX_NAME_LENGTH];
harp_variable *variable;
int index = -1;
if (variable_name != NULL)
{
snprintf(count_variable_name, MAX_NAME_LENGTH, "%s_count", variable_name);
}
else
{
strcpy(count_variable_name, "count");
}
if (harp_product_has_variable(product, count_variable_name))
{
if (harp_product_get_variable_index_by_name(product, count_variable_name, &index) != 0)
{
return -1;
}
}
if (index != -1 && bintype[index] != binning_remove)
{
/* if the count variable already exists and does not get removed then we assume it is correct/consistent
* (i.e. existing count=0 <-> variable=NaN) */
/* update bintype anyway */
bintype[index] = target_bintype;
return 0;
}
if (harp_variable_new(count_variable_name, harp_type_int32, num_dimensions, dimension_type, dimension,
&variable) != 0)
{
return -1;
}
memcpy(variable->data.int32_data, count, variable->num_elements * sizeof(int32_t));
if (index == -1)
{
if (harp_product_add_variable(product, variable) != 0)
{
harp_variable_delete(variable);
return -1;
}
index = product->num_variables - 1;
}
else
{
if (harp_product_replace_variable(product, variable) != 0)
{
harp_variable_delete(variable);
return -1;
}
}
bintype[index] = target_bintype;
return 0;
}
static int add_weight_variable(harp_product *product, binning_type *bintype, binning_type target_bintype,
const char *variable_name, int num_dimensions, harp_dimension_type *dimension_type,
long *dimension, float *weight)
{
char weight_variable_name[MAX_NAME_LENGTH];
harp_variable *variable;
int index = -1;
if (variable_name != NULL)
{
snprintf(weight_variable_name, MAX_NAME_LENGTH, "%s_weight", variable_name);
}
else
{
strcpy(weight_variable_name, "weight");
}
if (harp_product_has_variable(product, weight_variable_name))
{
if (harp_product_get_variable_index_by_name(product, weight_variable_name, &index) != 0)
{
return -1;
}
}
if (harp_variable_new(weight_variable_name, harp_type_float, num_dimensions, dimension_type, dimension,
&variable) != 0)
{
return -1;
}
memcpy(variable->data.float_data, weight, variable->num_elements * sizeof(float));
if (index == -1)
{
if (harp_product_add_variable(product, variable) != 0)
{
harp_variable_delete(variable);
return -1;
}
index = product->num_variables - 1;
}
else
{
if (harp_product_replace_variable(product, variable) != 0)
{
harp_variable_delete(variable);
return -1;
}
}
bintype[index] = target_bintype;
return 0;
}
/* map polygon to right longitude range, close at the poles (if needed),
* replicate first point at the end, and calculate min/max lat/lon
*/
static void make_2d_polygon(long *num_elements, double *latitude, double *longitude, double reference_longitude,
double *latitude_min, double *latitude_max, double *longitude_min, double *longitude_max)
{
double min_lat, max_lat;
double min_lon, max_lon, lon;
long i;
if (longitude[0] < reference_longitude - 180)
{
longitude[0] += 360;
}
if (longitude[0] >= reference_longitude + 180)
{
longitude[0] -= 360;
}
min_lon = longitude[0];
max_lon = min_lon;
min_lat = latitude[0];
max_lat = min_lat;
for (i = 1; i < *num_elements; i++)
{
while (longitude[i] < longitude[i - 1] - 180)
{
longitude[i] += 360;
}
while (longitude[i] > longitude[i - 1] + 180)
{
longitude[i] -= 360;
}
if (latitude[i] < min_lat)
{
min_lat = latitude[i];
}
else if (latitude[i] > max_lat)
{
max_lat = latitude[i];
}
if (longitude[i] < min_lon)
{
min_lon = longitude[i];
}
else if (longitude[i] > max_lon)
{
max_lon = longitude[i];
}
}
/* close the polygon (this could have a different longitude, due to the ref_lon mapping) */
lon = longitude[0];
while (lon < longitude[(*num_elements) - 1] - 180)
{
lon += 360;
}
while (lon > longitude[(*num_elements) - 1] + 180)
{
lon -= 360;
}
if (lon < min_lon)
{
min_lon = lon;
}
else if (lon > max_lon)
{
max_lon = lon;
}
/* we are covering a pole if our longitude range equals 360 degrees */
if (fabs(max_lon - (min_lon + 360)) < 1e-4)
{
if (max_lat > 0)
{
if (min_lat < 0)
{
/* if we cross the equator then we don't know which pole is covered */
/* skip polygon by setting num_elements to 0 */
*num_elements = 0;
return;
}
max_lat = 90;
/* close the polygon via the North pole */
longitude[*num_elements] = longitude[(*num_elements) - 1];
latitude[*num_elements] = 90;
(*num_elements)++;
longitude[*num_elements] = longitude[0];
latitude[*num_elements] = 90;
(*num_elements)++;
}
else if (min_lat < 0)
{
min_lat = -90;
/* close the polygon via the South pole */
longitude[*num_elements] = longitude[(*num_elements) - 1];
latitude[*num_elements] = -90;
(*num_elements)++;
longitude[*num_elements] = longitude[0];
latitude[*num_elements] = -90;
(*num_elements)++;
}
}
/* wrap longitude range to [reference_longitude-180,reference_longitude+360] */
if (min_lon < reference_longitude - 360)
{
min_lon += 360;
max_lon += 360;
for (i = 0; i < *num_elements; i++)
{
longitude[i] += 360;
}
}
while (min_lon >= reference_longitude + 180)
{
min_lon -= 360;
max_lon -= 360;
for (i = 0; i < *num_elements; i++)
{
longitude[i] -= 360;
}
}
*latitude_min = min_lat;
*latitude_max = max_lat;
*longitude_min = min_lon;
*longitude_max = max_lon;
/* repeat first point at the end to make iterating over it more easy */
latitude[*num_elements] = latitude[0];
longitude[*num_elements] = longitude[0];
(*num_elements)++;
}
static int add_cell_index(long cell_index, long *cumsum_index, long **latlon_cell_index, double **latlon_weight)
{
if ((*cumsum_index) % LATLON_BLOCK_SIZE == 0)
{
long *new_latlon_cell_index;
double *new_latlon_weight;
new_latlon_cell_index = realloc(*latlon_cell_index, ((*cumsum_index) + LATLON_BLOCK_SIZE) * sizeof(long));
if (new_latlon_cell_index == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
((*cumsum_index) + LATLON_BLOCK_SIZE) * sizeof(long), __FILE__, __LINE__);
return -1;
}
*latlon_cell_index = new_latlon_cell_index;
new_latlon_weight = realloc(*latlon_weight, ((*cumsum_index) + LATLON_BLOCK_SIZE) * sizeof(double));
if (new_latlon_weight == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
((*cumsum_index) + LATLON_BLOCK_SIZE) * sizeof(double), __FILE__, __LINE__);
return -1;
}
*latlon_weight = new_latlon_weight;
}
(*latlon_cell_index)[(*cumsum_index)] = cell_index;
(*latlon_weight)[(*cumsum_index)] = 1.0; /* initialize with default weight */
(*cumsum_index)++;
return 0;
}
/* latitude_edges and longitude_edges should contain just 2 elements (bounds of the cell) */
static double find_weight_for_polygon_and_cell(long num_points, double *poly_latitude, double *poly_longitude,
double *temp_latitude, double *temp_longitude,
double *latitude_edges, double *longitude_edges)
{
double latitude, longitude, next_latitude, next_longitude;
double cell_area, poly_area;
long offset = num_points;
long num_temp = 0;
long i;
if (num_points < 3)
{
return 0.0;
}
/* we start with filling temp_latitude and temp_longitude at offset 'num_points' */
/* this allows us to use the same temp buffers in-place for the longitude clamping in the second step */
/* clamp to latitude range */
for (i = 0; i < num_points - 1; i++)
{
latitude = poly_latitude[i];
longitude = poly_longitude[i];
next_latitude = poly_latitude[i + 1];
next_longitude = poly_longitude[i + 1];
if (latitude < latitude_edges[0])
{
if (next_latitude > latitude_edges[0])
{
longitude += (latitude_edges[0] - latitude) * (next_longitude - longitude) / (next_latitude - latitude);
latitude = latitude_edges[0];
}
}
else if (latitude > latitude_edges[1])
{
if (next_latitude < latitude_edges[1])
{
longitude += (latitude_edges[1] - latitude) * (next_longitude - longitude) / (next_latitude - latitude);
latitude = latitude_edges[1];
}
}
if (latitude >= latitude_edges[0] && latitude <= latitude_edges[1])
{
temp_latitude[offset + num_temp] = latitude;
temp_longitude[offset + num_temp] = longitude;
num_temp++;
if (next_latitude < latitude_edges[0])
{
temp_longitude[offset + num_temp] = longitude + (latitude_edges[0] - latitude) *
(next_longitude - longitude) / (next_latitude - latitude);
temp_latitude[offset + num_temp] = latitude_edges[0];
num_temp++;
}
else if (next_latitude > latitude_edges[1])
{
temp_longitude[offset + num_temp] = longitude + (latitude_edges[1] - latitude) *
(next_longitude - longitude) / (next_latitude - latitude);
temp_latitude[offset + num_temp] = latitude_edges[1];
num_temp++;
}
}
}
if (num_temp < 3)
{
return 0.0;
}
if (temp_latitude[offset] != temp_latitude[offset + num_temp - 1] ||
temp_longitude[offset] != temp_longitude[offset + num_temp - 1])
{
temp_latitude[offset + num_temp] = temp_latitude[offset];
temp_longitude[offset + num_temp] = temp_longitude[offset];
num_temp++;
}
/* clamp to longitude range */
num_points = num_temp;
num_temp = 0;
for (i = 0; i < num_points - 1; i++)
{
latitude = temp_latitude[offset + i];
longitude = temp_longitude[offset + i];
next_latitude = temp_latitude[offset + i + 1];
next_longitude = temp_longitude[offset + i + 1];
if (longitude < longitude_edges[0])
{
if (next_longitude > longitude_edges[0])
{
latitude +=
(longitude_edges[0] - longitude) * (next_latitude - latitude) / (next_longitude - longitude);
longitude = longitude_edges[0];
}
}
else if (longitude > longitude_edges[1])
{
if (next_longitude < longitude_edges[1])
{
latitude +=
(longitude_edges[1] - longitude) * (next_latitude - latitude) / (next_longitude - longitude);
longitude = longitude_edges[1];
}
}
if (longitude >= longitude_edges[0] && longitude <= longitude_edges[1])
{
temp_latitude[num_temp] = latitude;
temp_longitude[num_temp] = longitude;
num_temp++;
if (next_longitude < longitude_edges[0])
{
temp_latitude[num_temp] = latitude + (longitude_edges[0] - longitude) *
(next_latitude - latitude) / (next_longitude - longitude);
temp_longitude[num_temp] = longitude_edges[0];
num_temp++;
}
else if (next_longitude > longitude_edges[1])
{
temp_latitude[num_temp] = latitude + (longitude_edges[1] - longitude) *
(next_latitude - latitude) / (next_longitude - longitude);
temp_longitude[num_temp] = longitude_edges[1];
num_temp++;
}
}
}
if (num_temp < 3)
{
return 0.0;
}
if (temp_latitude[0] != temp_latitude[num_temp - 1] || temp_longitude[0] != temp_longitude[num_temp - 1])
{
temp_latitude[num_temp] = temp_latitude[0];
temp_longitude[num_temp] = temp_longitude[0];
num_temp++;
}
/* calculate polygon area */
/* the area of a polygon is equal to 0.5 * sum(k=1..n-1, vec(0,k) X vec(0,k+1)) */
poly_area = 0;
for (i = 0; i < num_temp - 1; i++)
{
poly_area += (temp_longitude[i] + temp_longitude[i + 1]) * (temp_latitude[i] - temp_latitude[i + 1]);
}
poly_area /= 2.0;
if (poly_area < 0)
{
poly_area = -poly_area;
}
cell_area = (latitude_edges[1] - latitude_edges[0]) * (longitude_edges[1] - longitude_edges[0]);
return poly_area / cell_area;
}
static int find_matching_cells_and_weights_for_bounds(harp_variable *latitude_bounds, harp_variable *longitude_bounds,
long num_latitude_edges, double *latitude_edges,
long num_longitude_edges, double *longitude_edges,
long *num_latlon_index, long **latlon_cell_index,
double **latlon_weight)
{
double *temp_poly_latitude = NULL;
double *temp_poly_longitude = NULL;
double *poly_latitude = NULL;
double *poly_longitude = NULL;
long num_latitude_cells = num_latitude_edges - 1;
long num_longitude_cells = num_longitude_edges - 1;
long *min_lat_id = NULL, *max_lat_id = NULL; /* min/max grid latitude index for each longitude grid row */
long *min_lon_id = NULL, *max_lon_id = NULL; /* min/max grid longitude index for each latitude grid row */
long cumsum_index = 0;
long num_elements;
long max_num_vertices;
long i, j, k;
num_elements = latitude_bounds->dimension[0];
max_num_vertices = latitude_bounds->dimension[latitude_bounds->num_dimensions - 1];
if (longitude_bounds->dimension[latitude_bounds->num_dimensions - 1] != max_num_vertices)
{
harp_set_error(HARP_ERROR_INVALID_VARIABLE, "latitude_bounds and longitude_bounds variables should have the "
"same length for the inpendent dimension");
return -1;
}
/* add 1 point to allow closing the polygon (i.e. repeat first point at the end) */
/* and allow room for 2 more points to close polygons that cover a pole */
poly_latitude = malloc((max_num_vertices + 3) * sizeof(double));
if (poly_latitude == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(max_num_vertices + 3) * sizeof(double), __FILE__, __LINE__);
goto error;
}
poly_longitude = malloc((max_num_vertices + 3) * sizeof(double));
if (poly_longitude == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(max_num_vertices + 3) * sizeof(double), __FILE__, __LINE__);
goto error;
}
/* the temporary polygon is used for calculating the overlap fraction with a cell */
/* it needs to be able to hold three times the amount of points as the input polygon */
temp_poly_latitude = malloc(3 * (max_num_vertices + 3) * sizeof(double));
if (temp_poly_latitude == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
3 * (max_num_vertices + 3) * sizeof(double), __FILE__, __LINE__);
goto error;
}
temp_poly_longitude = malloc(3 * (max_num_vertices + 3) * sizeof(double));
if (temp_poly_longitude == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
3 * (max_num_vertices + 3) * sizeof(double), __FILE__, __LINE__);
goto error;
}
/* add two to the length to allow indexing just before and after the range */
min_lat_id = malloc((num_longitude_cells + 2) * sizeof(long));
if (min_lat_id == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(num_longitude_cells + 2) * sizeof(long), __FILE__, __LINE__);
goto error;
}
max_lat_id = malloc((num_longitude_cells + 2) * sizeof(long));
if (max_lat_id == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(num_longitude_cells + 2) * sizeof(long), __FILE__, __LINE__);
goto error;
}
min_lon_id = malloc((num_latitude_cells + 2) * sizeof(long));
if (min_lon_id == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(num_latitude_cells + 2) * sizeof(long), __FILE__, __LINE__);
goto error;
}
max_lon_id = malloc((num_latitude_cells + 2) * sizeof(long));
if (max_lon_id == NULL)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(num_latitude_cells + 2) * sizeof(long), __FILE__, __LINE__);
goto error;
}
for (i = 0; i < num_elements; i++)
{
double lat_min, lat_max, lon_min, lon_max;
long num_vertices = max_num_vertices;
int loop;
num_latlon_index[i] = 0;
memcpy(poly_latitude, &latitude_bounds->data.double_data[i * max_num_vertices],
max_num_vertices * sizeof(double));
memcpy(poly_longitude, &longitude_bounds->data.double_data[i * max_num_vertices],
max_num_vertices * sizeof(double));
while (num_vertices > 0 && harp_isnan(poly_latitude[num_vertices - 1]))
{
num_vertices--;
}
if (num_vertices > 2 && poly_latitude[0] == poly_latitude[num_vertices - 1] &&
poly_longitude[0] == poly_longitude[num_vertices - 1])
{
/* remove duplicate point (make_2d_polygon will introduce it again) */
num_vertices--;
}
if (num_vertices == 2)