1 /*
2  * (C) Copyright 2005- ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <assert.h>
13 #include <pthread.h>
14 
15 #include "eccodes.h"
16 #define NUM_THREADS 3
17 
18 /*static int DBL_EQUAL(double d1, double d2, double tolerance)
19 {
20     return fabs(d1-d2) <= tolerance;
21 }*/
22 
process_bufr(void * arg)23 static void* process_bufr(void* arg)
24 {
25     FILE* fin       = (FILE*)arg;
26     int err         = 0;
27     codes_handle* h = NULL;
28     long numSubsets = 0, lVal = 0;
29     size_t size = 0, i = 0;
30     double* dValues = NULL;
31     /* Each thread gets a different message handle */
32     h = codes_handle_new_from_file(NULL, fin, PRODUCT_BUFR, &err);
33     assert(h);
34 
35     /* Check expected values for this BUFR file */
36     CODES_CHECK(codes_get_long(h, "numberOfSubsets", &numSubsets), 0);
37     assert(numSubsets == 1);
38     CODES_CHECK(codes_get_long(h, "rectimeSecond", &lVal), 0);
39     assert(lVal == 27);
40 
41     CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
42 
43     dValues = (double*)malloc(numSubsets * sizeof(double));
44     assert(dValues);
45     size = numSubsets;
46     CODES_CHECK(codes_get_double_array(h, "latitude", dValues, &size), 0);
47     for (i = 0; i < size; ++i) {
48         /* Specific test for latitudes in this BUFR file */
49         assert(dValues[0] < 79 && dValues[0] > 70);
50     }
51     free(dValues);
52 
53     /* Some encoding too */
54     CODES_CHECK(codes_set_long(h, "bufrHeaderCentre", 88), 0);
55     CODES_CHECK(codes_set_long(h, "blockNumber", 2), 0);
56     CODES_CHECK(codes_set_long(h, "#3#verticalSignificanceSurfaceObservations", 8), 0);
57     CODES_CHECK(codes_set_long(h, "pack", 1), 0);
58 
59     codes_handle_delete(h);
60     pthread_exit(NULL);
61 }
62 
main(int argc,char ** argv)63 int main(int argc, char** argv)
64 {
65     pthread_t thread1, thread2, thread3;
66     int err          = 0;
67     FILE* fin        = 0;
68     codes_handle* h1 = 0;
69     codes_handle* h2 = 0;
70     fin              = fopen("../../data/bufr/syno_multi.bufr", "rb");
71     assert(fin);
72 
73     err = pthread_create(&thread1, NULL, process_bufr, (void*)fin);
74     if (err) return 1;
75 
76     err = pthread_create(&thread2, NULL, process_bufr, (void*)fin);
77     if (err) return 1;
78 
79     err = pthread_create(&thread3, NULL, process_bufr, (void*)fin);
80     if (err) return 1;
81 
82     pthread_join(thread1, NULL);
83     pthread_join(thread2, NULL);
84     pthread_join(thread3, NULL);
85 
86     fclose(fin);
87     codes_handle_delete(h1);
88     codes_handle_delete(h2);
89     return 0;
90 }
91