File: intf2.c

package info (click to toggle)
emoslib 2%3A4.4.5-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 359,232 kB
  • ctags: 13,125
  • sloc: fortran: 93,166; ansic: 27,958; sh: 7,500; f90: 5,209; perl: 604; cpp: 305; makefile: 78; python: 53
file content (331 lines) | stat: -rw-r--r-- 9,646 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
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
/**
* Copyright 1981-2016 ECMWF.
*
* This software is licensed under the terms of the Apache Licence
* Version 2.0 which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/


#include "grib_api.h"
#include "emos.h"


fortint int2_chkout();
fortint int2_estima();
fortint int2_intf(fortfloat in_array[], fortint in_array_length, fortfloat *out_array, fortint *out_array_length);
fortint int2_outrep();
fortint int2_setrep(fortint output_flag);
int copy_spec_from_ksec(grib_util_grid_spec* spec,grib_util_packing_spec* packing_spec);
long describe_input_field(grib_handle* handle, long outputRepresentation);


fortint intf2_(char* grib_in, fortint* length_in, char* grib_out, fortint* length_out)
{
    fortint isec1[ISECTION_1] = {0,};
    fortint isec2[ISECTION_2] = {0,};
    fortint isec3[ISECTION_3] = {0,};
    fortint isec4[ISECTION_4] = {0,};

    static double *values_in = NULL;
    static double *values_out = NULL;
    static size_t values_in_len = 0;
    static size_t values_out_len = 0;
    double *values =NULL;

    static float *values_in1 = NULL;
    static float *values_out1 = NULL;
    static size_t values_in_len1 = 0;
    static size_t values_out_len1 = 0;

    size_t ii = 0;
    size_t inlen;
    size_t len=0;
    size_t outlen = *length_out;
    size_t outlen_grib_message = 0;

    fortint out_length = 0;

    long long_length_in = *length_in;
    fortint fortint_inlen = *length_in;
    fortint fortint_outlen = outlen;

    grib_handle *handle = 0;
    grib_handle *outh   = 0;
    grib_handle *finalh = 0;

    fortint err1 = 0, mars_interpolation = 0;
    int err = 0 , what = 0;
    int  resetOutput = 0;
    long nins, niwe;
    long acc = 0;
    long bitmapPresent = 0;

    size_t size;

    const void* temp;
    char *intf2_debug = getenv("INTF2_DEBUG");
    char *intf2_write = getenv("INTF2_WRITE_TO_FILE");
    int jpeg = 0;
    int set_spec_flags=0;
    grib_util_grid_spec     spec={0,};
    grib_util_packing_spec packing_spec={0,};

    mars_interpolation = int2_chkout();

    if(!mars_interpolation)
    {
        *length_out = 0;
        if(intf2_debug){
            printf("INTF2: There is NOT users settings \n");
            printf("INTF2: No Interpolation carried out \n");
        }
        return err;
    }

    fortint outputRepresentation = int2_outrep();

    if(intf2_debug)
    {
        if(outputRepresentation)
            printf("INTF2:  Output Representation is NOT set by user\n");
    }

    handle = grib_handle_new_from_message(0,grib_in,long_length_in);
    if(handle == 0) {
        err = -1;
        fprintf(stdout,"INTF2: Cannot create Handle %d\n",err);
        /* intlog2("INTF2: Cannot create Handle"); */
        goto cleanup;
    }

    if ((err = grib_get_size(handle,"values",&inlen)))
    {
        fprintf(stdout,"INTF2: Cannot get size %s\n",grib_get_error_message(err));
        return err;
    }

    if(inlen > values_in_len)
    {
        if(values_in) free(values_in);
        values_in = (double*)malloc(sizeof(double)*inlen);
        values_in_len = inlen;

        if(!values_in)
        {
            err = -1;
            fprintf(stdout,"INTF2: Cannot allocate %ld\n",inlen);
            goto cleanup;
        }
    }

   /* If bitmap Present  */
    if((err = grib_get_long(handle,"bitmapPresent",&bitmapPresent))!= GRIB_SUCCESS)
     {
        fprintf(stderr,"Cannot get bitmapPresent  %s\n",grib_get_error_message(err));
        return err;
     }

    /* if bitmap present Set missing value which correspond WAVE in Emos  */
    if(bitmapPresent)
    {
        /* if(err = grib_set_double(handle,"missingValue",12345.0)) */
        if ((err = grib_set_double(handle,"missingValue",-9999999.0)))
        {
            fprintf(stdout,"INTF2: Cannot set Missing Value %s\n",grib_get_error_message(err));
            goto cleanup;
        }
    }

    /* Get decoded values */
    if ((err = grib_get_double_array(handle,"values",values_in,&inlen)))
    {
        fprintf(stdout,"INTF2: Cannot get decoded values %s\n",grib_get_error_message(err));
        goto cleanup;
    }


    /* Descibe input field and initial settings for output field */
    if ((err = describe_input_field(handle,outputRepresentation)))
    {
        fprintf(stdout,"INTF2 describe_input_field failed : %d\n",err);
        goto cleanup;
    }


    /*=============  INTF ====================================*/

    if(outputRepresentation){
        out_length = inlen;
    }
    else{
        out_length = int2_estima();
        if(!out_length){
            fprintf(stdout,"INTF2: Estimate for length of output array is 0 \n");
            err = -1;
            goto cleanup;
        }
    }
    if(out_length > values_out_len)
    {
        if(values_out) free(values_out);
        values_out = (double*)malloc(sizeof(double)*out_length);
        values_out_len = out_length;

        if(!values_out)
        {
            fprintf(stdout,"INTF2: Cannot allocate values_out %d\n",out_length);
            err = -1;
            goto cleanup;
        }
    }

    fortint_inlen = inlen;
    fortint_outlen = outlen;


#ifdef REAL_8
    if ((err = int2_intf(values_in,fortint_inlen,values_out,&fortint_outlen)))
    {
        fprintf(stdout,"INTF failed %d\n",err);
        goto cleanup;
    }
#else
    if(inlen > values_in_len1)
    {
        if(values_in1) free(values_in1);
        values_in1 = (float*)malloc(sizeof(float)*inlen);
        values_in_len1 = inlen;

        if(!values_in1)
        {
            err = -1;
            fprintf(stdout,"INTF2: Cannot allocate values_in1 %ld\n",inlen);
            goto cleanup;
        }
    }
    if(out_length > values_out_len1)
    {
        if(values_out1) free(values_out1);
        values_out1 = (float*)malloc(sizeof(float)*out_length);
        values_out_len1 = out_length;

        if(!values_out1)
        {
            fprintf(stdout,"INTF2: Cannot allocate values_out1 %ld\n",out_length);
            err = -1;
            goto cleanup;
        }
    }
    for( ii = 0 ; ii < fortint_inlen; ii++){
        values_in1[ii] = values_in[ii];
    }
    if ((err = int2_intf(values_in1,fortint_inlen,values_out1,&fortint_outlen)))
    {
        fprintf(stdout,"INTF failed %d\n",err);
        goto cleanup;
    }
    for( ii = 0 ; ii < fortint_outlen; ii++){
        values_out[ii] = values_out1[ii];
    }
#endif
    outlen = fortint_outlen;

    if(intf2_debug) {
        printf("INTF2: Outlen: %lu\n",outlen);
    }

    err=copy_spec_from_ksec(&spec,&packing_spec);
    if (err) {
        fprintf(stdout,"INTF2: ERROR - copy_grid_spec_from_ksec unable to copy spec: %d\n", err);
        goto cleanup;
    }

    packing_spec.accuracy=GRIB_UTIL_ACCURACY_USE_PROVIDED_BITS_PER_VALUES;
    packing_spec.packing=GRIB_UTIL_PACKING_USE_PROVIDED;

    if(outlen == 0) {
        set_spec_flags |= GRIB_UTIL_SET_SPEC_FLAGS_ONLY_PACKING;
        /* Sinisa and Enrico 7/3/11 comments this line */
        /* packing_spec.packing=0; */
        len=inlen;
        values=values_in;
        if(intf2_debug )
            printf("INTF2: Outlen = 0 No Interpolation carried out \n");
    } else {
        len=outlen;
        values=values_out;
    }
#ifdef DUMPING_GRIB_HANDLE
{
    const int dump_flags = GRIB_DUMP_FLAG_VALUES
                |  GRIB_DUMP_FLAG_READ_ONLY
                |  GRIB_DUMP_FLAG_ALIASES
                |  GRIB_DUMP_FLAG_TYPE;
    printf("intf2_ start dump...................\n");
    grib_dump_content(handle, stdout,"debug", dump_flags, NULL);
    printf("intf2_ end dump.....................\n");
}
#endif
    finalh = grib_util_set_spec(handle,&spec, &packing_spec, set_spec_flags,values, len, &err);
    if(!finalh)  {
        fprintf(stdout,"INTF2: ERROR - grib_util_set_spec: %d\n", err);
        if(!err) err = 1;
        goto cleanup;
    }

    /* deallocate pl array, if it has been passed into spec */
    if (spec.pl!=NULL) {
        free((long*) spec.pl);
        spec.pl = NULL;
    }

    if (outlen == 0){
        if(err == -1) {
            /* -1 coming from grib_util when there is not packing */
            err = 0;
            *length_out = 0;
            if(intf2_debug)
                printf("INTF2: No Interpolation carried out, nothing repacked \n");
            goto cleanup;
        }
    }

    err = grib_get_message(finalh,&temp,&outlen_grib_message);
    if(err!=0) {
        fprintf(stdout,"INTF2: ERROR - grib_get_message unable to generate message: %d\n", err);
        goto cleanup;
    }

    if(outlen_grib_message > *length_out) {
        fprintf(stdout,"INTF2: ERROR - INTF2  OUTLEN is too large: %ld > %d\n", outlen_grib_message, *length_out);
        err = 1;
        goto cleanup;
    }

    memcpy(grib_out,temp,outlen_grib_message);

    *length_out = outlen_grib_message;

    /* goto cleanup; */
    if(intf2_debug && (set_spec_flags & GRIB_UTIL_SET_SPEC_FLAGS_ONLY_PACKING)){
        printf("INTF2: No Interpolation carried out but data repacked \n");
    }


cleanup:
    if((resetOutput = int2_setrep(outputRepresentation))) printf("INTF2: Output Representation reset failed:  %d \n",resetOutput);
    if(handle)     {grib_handle_delete(handle);handle=NULL;}
    if(outh)       {grib_handle_delete(outh);outh=NULL;}
    if(outlen!=0 && finalh)     grib_handle_delete(finalh);

    return err;
}

fortint intf2(char* grib_in, fortint* length_in, char* grib_out, fortint* length_out)
{
    return intf2_(grib_in,length_in,grib_out,length_out);
}