File: gribapi_swig.i

package info (click to toggle)
eccodes 2.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 480,184 kB
  • sloc: ansic: 163,815; makefile: 21,266; sh: 8,507; python: 6,026; f90: 5,762; perl: 2,891; yacc: 818; lex: 356; cpp: 305; fortran: 116; awk: 66
file content (266 lines) | stat: -rw-r--r-- 10,358 bytes parent folder | download
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
%module gribapi_swig

%include "cpointer.i"
%include "cstring.i"
%include "typemaps.i"
%include "cdata.i"
%include "carrays.i"
%include "grib_errors.h"

%{
#define SWIG_FILE_WITH_INIT
#include "grib_interface.h"
%}

%include "numpy.i"
%init %{
import_array();
%}

/* Converts a PyFile instance to a stdio FILE* for reading binary files */
%typemap(in) FILE* {
  int fileDescriptor = PyObject_AsFileDescriptor($input);
  /*printf("swig.i fileDescriptor=%d\n", fileDescriptor);*/
  if(fileDescriptor >= 0) {
    /* Convert file descriptor to a FILE pointer */
    $1 = fdopen(fileDescriptor,"rb"); // needs to be rb+ (or wb) for write
  }
  else {
    PyErr_SetString(PyExc_TypeError, "$1_name must be a file type.");
    return NULL;
  }
}

%pointer_class(int, intp);
%pointer_class(size_t, sizetp);
%pointer_class(long, longp);
%pointer_class(double, doublep);
%array_functions(double, doubleArray);
%array_functions(long, longArray);
%array_functions(int, intArray);
%array_functions(char*, stringArray);

// creation
int grib_c_new_from_file(FILE* f, int fd, char* fname, int* INOUT, int headers_only);
int grib_c_new_any_from_file(FILE* f, int fd, char* fname, int headers_only, int* INOUT);
int grib_c_new_bufr_from_file(FILE* f, int fd, char* fname, int headers_only, int* INOUT);
int grib_c_new_gts_from_file(FILE* f, int fd, char* fname, int headers_only, int* INOUT);
int grib_c_new_metar_from_file(FILE* f, int headers_only, int* INOUT);
int grib_c_iterator_new(int* INPUT, int* OUTPUT, int* INPUT);
int grib_c_keys_iterator_new(int* INPUT, int* OUTPUT, char* name_space);
int codes_c_bufr_keys_iterator_new(int* INPUT, int* OUTPUT);
int grib_c_grib_new_from_samples(int* INOUT, char* name);
int grib_c_bufr_new_from_samples(int* INOUT, char* name);
int grib_c_index_new_from_file(char* file, char* keys, int* OUTPUT);
int grib_c_index_add_file(int* INPUT, char* file);
int grib_c_new_from_index(int *INPUT, int *INOUT);
int grib_c_index_write(int* INPUT, char* file);
int grib_c_index_read(char* file, int* OUTPUT);
int grib_c_new_from_message(int *INOUT, char *binmsg, size_t *INPUT);
// ---

%apply int* INPUT { int* fid };
%apply int* INPUT { int* gid };
%apply int* INPUT { int* iterid };
%apply int* INPUT { int* iid };

// file operations
int codes_c_close_file(int fd, char* fname);
int grib_c_count_in_file(FILE* f,int* OUTPUT);
// ---

/* Converts a PyFile instance to a stdio FILE* for writing binary files */
%typemap(in) FILE* {
  int fileDescriptor = PyObject_AsFileDescriptor($input);
  if(fileDescriptor >= 0) {
    /* Convert file descriptor to a FILE pointer */
    $1 = fdopen(fileDescriptor,"wb");
  }
  else {
    PyErr_SetString(PyExc_TypeError, "$1_name must be a file type.");
    return NULL;
  }
}

// grib handle operations
int grib_c_release(int* gid);
int grib_c_write(int* gid, FILE* f);
int grib_c_get_size_long(int* gid, char* key, long* OUTPUT);
int grib_c_get_string_length(int* gid, char* key, size_t* OUTPUT);
int grib_c_clone(int* gid,int* INOUT);
int grib_c_copy_namespace(int* gid, char* name, int* INPUT);
int grib_c_get_message_size(int* gid, size_t* OUTPUT);
int grib_c_get_message_offset(int* gid, size_t* OUTPUT);
int grib_c_get_native_type(int* gid, char* key, int* OUTPUT);
// ---

// multi support
int grib_c_multi_new(int* OUTPUT);
int grib_c_multi_support_on(void);
int grib_c_multi_write(int* gid, FILE* f);
int grib_c_multi_support_off(void);
int grib_c_multi_release(int* gid);
int grib_c_multi_append(int* INPUT, int* INPUT,int* INPUT);
// ---

// gribex support
int grib_c_gribex_mode_on(void);
int grib_c_gribex_mode_off(void);
// ---

// keys iterator
int grib_c_keys_iterator_next(int* iterid);
int codes_c_bufr_keys_iterator_next(int* iterid);
int grib_c_keys_iterator_delete(int* iterid);
int codes_c_bufr_keys_iterator_delete(int* iterid);
int grib_c_skip_computed(int* iterid);
int grib_c_skip_coded(int* iterid);
int grib_c_skip_edition_specific(int* iterid);
int grib_c_skip_duplicates(int* iterid);
int grib_c_skip_read_only(int* iterid);
int grib_c_skip_function(int* iterid);
int grib_c_keys_iterator_rewind(int* iterid);
int codes_c_bufr_keys_iterator_rewind(int* iterid);
int grib_c_bufr_copy_data(int* gid, int* INOUT);


%cstring_bounded_output(char* name, 1024);
int grib_c_keys_iterator_get_name(int* iterid, char* name, int len);
int codes_c_bufr_keys_iterator_get_name(int* iterid, char* name, int len);
// ---

// indexing routines
int grib_c_index_get_size_long(int* iid, char* key, long* OUTPUT);
int grib_c_index_get_long(int* iid, char* key, long* val, int* size);
int grib_c_index_get_real8(int* iid, char* key, double* val, int* size);
%cstring_bounded_output(char* index_string_output, 1024*1024);
int grib_c_index_get_string(int* iid, char* key, char* index_string_output, int* INPUT, int* INOUT);
int grib_c_index_select_long(int *iid, char *key, long *INPUT);
int grib_c_index_select_real8(int *iid, char *key, double *INPUT);
int grib_c_index_select_string(int *iid, char *key, char *val);
int grib_c_index_release(int* iid);
// ---

// values iterator
int grib_c_iterator_delete(int* iterid);
int grib_c_iterator_next(int* iterid, double* OUTPUT, double* OUTPUT, double* OUTPUT);
// ---

// getting/setting key values
%cstring_output_withsize(char* string_val, size_t* string_size)
int grib_c_get_string(int* gid, char* key, char* string_val, size_t* string_size);
int grib_c_get_string_array(int* gid, char* key, char** array_string_val, size_t* size);
int grib_c_set_string(int* gid, char* key, char* sval, int len2);
int grib_c_get_long(int* gid, char* key, long* OUTPUT);
int grib_c_set_long(int* gid, char* key, long* INPUT);
int grib_c_get_double(int* gid, char* key, double* OUTPUT);
int grib_c_set_double(int* gid, char* key, double* INPUT);
int grib_c_set_real8_array(int* gid, char* key, double* val, int* size);
int grib_c_get_real8_array(int* gid, char* key, double* val, int* size);
int grib_c_get_long_array(int* gid, char* key, long* val, int* size);
int grib_c_set_long_array(int* gid, char* key, long* val, int* size);
int grib_c_get_real8_element(int* gid, char* key, int* INPUT, double* OUTPUT);
int grib_c_get_real8_elements(int* gid, char* key, int* index, double* val, int* size);
int grib_c_set_missing(int* gid, char* key);
int grib_c_set_key_vals(int* gid, char* keyvals);
int grib_c_is_missing(int* gid, char* key, int* OUTPUT);
int grib_c_is_defined(int* gid, char* key, int* OUTPUT);

// http://www.swig.org/Doc1.3/Python.html
// This tells SWIG to treat char ** as a special case
%typemap(in) char ** {
  /* Check if is a list */
  if (PyList_Check($input)) {
    int size = PyList_Size($input);
    int i = 0;
    $1 = (char **) malloc((size+1)*sizeof(char *));
    for (i = 0; i < size; i++) {
      PyObject *o = PyList_GetItem($input,i);
      if (PyString_Check(o))
        $1[i] = PyString_AsString(PyList_GetItem($input,i));
      else {
        PyErr_SetString(PyExc_TypeError,"list must contain strings");
        free($1);
        return NULL;
      }
    }
    $1[i] = 0;  /* Last entry set to NULL */
  } else {
    PyErr_SetString(PyExc_TypeError,"not a list");
    return NULL;
  }
}
// This cleans up the char ** array we malloc'd before the function call
%typemap(freearg) char ** {
  free((char *) $1);
}
int grib_c_set_string_array(int *gid, char *key, const char** val);

// Numpy Support
%apply (double* IN_ARRAY1, int DIM1) {(double* dpin_val, int dpin_val_dim1)};
%apply (long* IN_ARRAY1, int DIM1) {(long* lpin_val, int lpin_val_dim1)};
%apply (int* IN_ARRAY1, int DIM1) {(int* ipin_index, int ipin_index_dim1)};
%apply (double* ARGOUT_ARRAY1, int DIM1) {(double* dpout_val, int dpout_val_dim1)};
%apply (long* ARGOUT_ARRAY1, int DIM1) {(long* lpout_val, int lpout_val_dim1)};

%inline %{
//void with_numpy() {
//    return;
//}
int grib_set_double_ndarray(int* gid, char* key, double* dpin_val, int dpin_val_dim1) {
    return grib_c_set_real8_array(gid,key,dpin_val,&dpin_val_dim1);
}
int grib_set_long_ndarray(int* gid, char* key, long* lpin_val, int lpin_val_dim1) {
    return grib_c_set_long_array(gid,key,lpin_val,&lpin_val_dim1);
}
int grib_get_double_ndarray(int* gid, char* key, double* dpout_val, int dpout_val_dim1) {
    return grib_c_get_real8_array(gid,key,dpout_val,&dpout_val_dim1);
}
int grib_get_long_ndarray(int* gid, char* key, long* lpout_val, int lpout_val_dim1) {
    return grib_c_get_long_array(gid,key,lpout_val,&lpout_val_dim1);
}
int grib_get_double_ndelements(int* gid, char* key, int* ipin_index, int ipin_index_dim1, double* dpout_val, int dpout_val_dim1) {
    return grib_c_get_real8_elements(gid,key,ipin_index,dpout_val,&dpout_val_dim1);
}
%}
%clear double* dpin_val, int dpin_val_dim1;
%clear long* lpin_val, int lpin_val_dim1;
%clear int* ipout_val, int ipout_val_dim1;
%clear double* dpout_val, int dpout_val_dim1;
%clear long* lpout_val, int lpout_val_dim1;

// ---

// nearest
int grib_c_find_nearest_single(int* gid, int* INPUT, double* INPUT, double* INPUT, double* OUTPUT, double* OUTPUT, double* OUTPUT, double* OUTPUT, int* OUTPUT);
int grib_c_find_nearest_four_single(int* gid, int* INPUT, double* INPUT, double* INPUT, double* outlats, double* outlons, double* values, double* distances, int* indexes);
// ---

/*
* Get the binary string message for a grib.
*
* Set the 3rd argument to nothing in 'cstring_output_allocate_size'.
* This is kind of difficult to explain, but, *msg will point directly to
* the binary message data of the current grib (which is stored in
* handle->buffer->data if I remember correctly) so freeing it will cause
* the binary message data in the grib_handle structure to be freed. This
* is a problem as grib_api does not know that, so it tries to free it
* itself (grib_release does that) resulting in a 'Segmentation fault'.
*/
%cstring_output_allocate_size(const void **binmsg, size_t *binmsglen,);
int grib_c_get_message(int *gid, const void **binmsg, size_t *binmsglen);
%clear const void **binmsg, size_t *binmsglen;

%clear int* fid;
%clear int* gid;
%clear int* iterid;

%cstring_bounded_output(char* error_message, 1024);
int grib_c_get_error_string(int* INPUT, char* error_message,  int len);

void no_fail_on_wrong_length(int flag);
long grib_c_get_api_version();
void grib_c_gts_header_on();
void grib_c_gts_header_off();
void grib_c_set_definitions_path(const char* path);
void grib_c_set_samples_path(const char* path);