File: grib_dumper_class_wmo.c

package info (click to toggle)
grib-api 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,008 kB
  • ctags: 10,015
  • sloc: ansic: 63,157; sh: 9,355; f90: 2,545; makefile: 2,496; yacc: 519; perl: 240; lex: 217
file content (526 lines) | stat: -rw-r--r-- 14,856 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
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
/**
* Copyright 2005-2007 ECMWF
*
* Licensed under the GNU Lesser General Public License which
* incorporates the terms and conditions of version 3 of the GNU
* General Public License.
* See LICENSE and gpl-3.0.txt for details.
*/

/**************************************
 *  Enrico Fucile
 **************************************/


#include "grib_api_internal.h"
#include <ctype.h>
/*
   This is used by make_class.pl

   START_CLASS_DEF
   CLASS      = dumper
   IMPLEMENTS = dump_long;dump_bits
   IMPLEMENTS = dump_double;dump_string
   IMPLEMENTS = dump_bytes;dump_values
   IMPLEMENTS = dump_label;dump_section
   IMPLEMENTS = init;destroy
   MEMBERS = long section_offset
   MEMBERS = long begin
   MEMBERS = long end
   END_CLASS_DEF

 */


/* START_CLASS_IMP */

/*

Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
Instead edit values between START_CLASS_DEF and END_CLASS_DEF
or edit "dumper.class" and rerun ./make_class.pl

*/

static void init_class      (grib_dumper_class*);
static int init            (grib_dumper* d);
static int destroy         (grib_dumper*);
static void dump_long       (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_bits       (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_double     (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_string     (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_bytes      (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_values     (grib_dumper* d, grib_accessor* a);
static void dump_label      (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_section    (grib_dumper* d, grib_accessor* a,grib_block_of_accessors* block);

typedef struct grib_dumper_wmo {
    grib_dumper          dumper;  
/* Members defined in wmo */
	long section_offset;
	long begin;
	long end;
} grib_dumper_wmo;


static grib_dumper_class _grib_dumper_class_wmo = {
    0,                              /* super                     */
    "wmo",                              /* name                      */
    sizeof(grib_dumper_wmo),     /* size                      */
    0,                                   /* inited */
    &init_class,                         /* init_class */
    &init,                               /* init                      */
    &destroy,                            /* free mem                       */
    &dump_long,                          /* dump long         */
    &dump_double,                        /* dump double    */
    &dump_string,                        /* dump string    */
    &dump_label,                         /* dump labels  */
    &dump_bytes,                         /* dump bytes  */
    &dump_bits,                          /* dump bits   */
    &dump_section,                       /* dump section      */
    &dump_values,                        /* dump values   */
    0,                             /* header   */
    0,                             /* footer   */
};

grib_dumper_class* grib_dumper_class_wmo = &_grib_dumper_class_wmo;

/* END_CLASS_IMP */
static void set_begin_end(grib_dumper* d,grib_accessor* a);
static void print_offset(FILE* out,long begin,long end);
static void print_hexadecimal(FILE* out,unsigned long flags,grib_accessor* a);

static void init_class      (grib_dumper_class* c){}

static int  init(grib_dumper* d)
{
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  self->section_offset=0;

  return GRIB_SUCCESS;
}

static int  destroy  (grib_dumper* d){
  return GRIB_SUCCESS;
}


static void aliases(grib_dumper* d,grib_accessor* a)
{
int i;
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;

  if( (d->option_flags & GRIB_DUMP_FLAG_ALIASES) == 0)
    return;

  if(a->all_names[1])
  {
    char *sep = "";
    fprintf(self->dumper.out," [");

    for(i = 1; i < MAX_ACCESSOR_NAMES; i++)
    {
      if(a->all_names[i])
      {
        if(a->all_name_spaces[i])
          fprintf(self->dumper.out,"%s%s.%s", sep,a->all_name_spaces[i],a->all_names[i]);
        else
          fprintf(self->dumper.out,"%s%s", sep,a->all_names[i]);
      }
    sep = ", ";
    }
    fprintf(self->dumper.out,"]");
  }
}

static void dump_long(grib_dumper* d,grib_accessor* a,const char* comment)
{
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  long value; size_t size = 1;
  int err = grib_unpack_long(a,&value,&size);

  if( a->length == 0  &&
      (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
    return;

  if( (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) != 0 &&
      (d->option_flags & GRIB_DUMP_FLAG_READ_ONLY) == 0)
    return;

  set_begin_end(d,a);

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/

  print_offset(self->dumper.out,self->begin,self->end);

  if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0)
        fprintf(self->dumper.out,"%s ",a->creator->op);

  if( ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) && grib_is_missing_internal(a) )
    fprintf(self->dumper.out,"%s = MISSING",a->name);
  else
    fprintf(self->dumper.out,"%s = %ld",a->name,value);

  print_hexadecimal(self->dumper.out,d->option_flags,a);

  if(comment) fprintf(self->dumper.out," [%s]",comment);
  if(err)
    fprintf(self->dumper.out," *** ERR=%d (%s)",err,grib_get_error_message(err));

  aliases(d,a);


  fprintf(self->dumper.out,"\n");
}

static int test_bit(long a, long b) {return a&(1<<b);}

static void dump_bits(grib_dumper* d,grib_accessor* a,const char* comment)
{
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  int i;
  long value; size_t size = 1;
  int err = grib_unpack_long(a,&value,&size);


  if( a->length == 0  &&
      (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
    return;

  set_begin_end(d,a);

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
  print_offset(self->dumper.out,self->begin,self->end);
  if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0)
        fprintf(self->dumper.out,"%s ",a->creator->op);

  fprintf(self->dumper.out,"%s = %ld [",a->name,value);

  for(i=0;i<(a->length*8);i++) {
    if(test_bit(value,a->length*8-i-1))
      fprintf(self->dumper.out,"1");
    else
      fprintf(self->dumper.out,"0");
  }
/*
  if(comment)
    fprintf(self->dumper.out,":%s]",comment);
  else
*/
  fprintf(self->dumper.out,"]");

  if (err==0) print_hexadecimal(self->dumper.out,d->option_flags,a);

  if(err)
    fprintf(self->dumper.out," *** ERR=%d (%s)",err,grib_get_error_message(err));

  aliases(d,a);
  fprintf(self->dumper.out,"\n");
}

static void dump_double(grib_dumper* d,grib_accessor* a,const char* comment)
{
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  double value; size_t size = 1;
  int err = grib_unpack_double(a,&value,&size);


  if( a->length == 0  &&
      (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
    return;

  set_begin_end(d,a);

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/

  print_offset(self->dumper.out,self->begin,self->end);
  if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0)
        fprintf(self->dumper.out,"%s ",a->creator->op);

  if( ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) && grib_is_missing_internal(a))
    fprintf(self->dumper.out,"%s = MISSING",a->name);
  else
    fprintf(self->dumper.out,"%s = %g",a->name,value);
  /*if(comment) fprintf(self->dumper.out," [%s]",comment);*/

  if (err==0) print_hexadecimal(self->dumper.out,d->option_flags,a);

  if(err)
    fprintf(self->dumper.out," *** ERR=%d (%s)",err,grib_get_error_message(err));
  aliases(d,a);
  fprintf(self->dumper.out,"\n");
}

static void dump_string(grib_dumper* d,grib_accessor* a,const char* comment)
{
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  char value[1024]; size_t size = sizeof(value);
  int err = grib_unpack_string(a,value,&size);
  char *p = value;

  if( a->length == 0  &&
      (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
    return;

  set_begin_end(d,a);

  while(*p) { if(!isprint(*p)) *p = '.'; p++; }

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
  print_offset(self->dumper.out,self->begin,self->end);
  if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0)
        fprintf(self->dumper.out,"%s ",a->creator->op);

  fprintf(self->dumper.out,"%s = %s",a->name,value);

  if (err==0) print_hexadecimal(self->dumper.out,d->option_flags,a);

  /*if(comment) fprintf(self->dumper.out," [%s]",comment);*/
  if(err)
    fprintf(self->dumper.out," *** ERR=%d (%s)",err,grib_get_error_message(err));
  aliases(d,a);
  fprintf(self->dumper.out,"\n");
}

static void dump_bytes(grib_dumper* d,grib_accessor* a,const char* comment)
{
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  int i,k,err =0;
  int more = 0;
  size_t size = a->length;
  unsigned char* buf = grib_context_malloc(d->handle->context,size);

  if( a->length == 0  &&
      (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
    return;

  set_begin_end(d,a);

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
  print_offset(self->dumper.out,self->begin,self->end);
  if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0)
        fprintf(self->dumper.out,"%s ",a->creator->op);

  fprintf(self->dumper.out,"%s = %ld",a->name,a->length);
  aliases(d,a);
  fprintf(self->dumper.out," {");

  if(!buf)
  {
    if(size == 0)
      fprintf(self->dumper.out,"}\n");
    else
      fprintf(self->dumper.out," *** ERR cannot malloc(%ld) }\n",(long)size);
    return;
  }

  print_hexadecimal(self->dumper.out,d->option_flags,a);

  fprintf(self->dumper.out,"\n");

  err = grib_unpack_bytes(a,buf,&size);
  if(err){
    grib_context_free(d->handle->context,buf);
    fprintf(self->dumper.out," *** ERR=%d (%s) \n}",err,grib_get_error_message(err));
    return ;
  }

  if(size > 100) {
    more = size - 100;
    size = 100;
  }

  k = 0;
  /* if(size > 100) size = 100;  */
  while(k < size)
  {
    int j;
    for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");
    for(j = 0; j < 16 && k < size; j++, k++)
    {
      fprintf(self->dumper.out,"%02x",buf[k]);
      if(k != size-1)
        fprintf(self->dumper.out,", ");
    }
    fprintf(self->dumper.out,"\n");
  }

  if(more)
  {
    for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");
    fprintf(self->dumper.out,"... %d more values\n",more);
  }

  for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
  fprintf(self->dumper.out,"} # %s %s \n",a->creator->op, a->name);
  grib_context_free(d->handle->context,buf);
}

static void dump_values(grib_dumper* d,grib_accessor* a)
{
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  int k,err =0;
  int more = 0;
  double*  buf = NULL;
  size_t size=0;

  if( a->length == 0  &&
      (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
    return;

  size=grib_value_count(a);

  if(size == 1){
    dump_double(d,a,NULL);
    return ;
  }
  buf = grib_context_malloc(d->handle->context,size * sizeof(double));

  set_begin_end(d,a);

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
  print_offset(self->dumper.out,self->begin,self->end);
  if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0)
        fprintf(self->dumper.out,"%s ",a->creator->op);

  fprintf(self->dumper.out,"%s = (%ld,%ld)",a->name,(long)size,a->length);
  aliases(d,a);
  fprintf(self->dumper.out," {");

  if(!buf)
  {
    if(size == 0)
      fprintf(self->dumper.out,"}\n");
    else
      fprintf(self->dumper.out," *** ERR cannot malloc(%ld) }\n",(long)size);
    return;
  }

  fprintf(self->dumper.out,"\n");

  err =  grib_unpack_double(a,buf,&size);

  if(err){
    grib_context_free(d->handle->context,buf);
    fprintf(self->dumper.out," *** ERR=%d (%s) \n}",err,grib_get_error_message(err));
    return ;
  }

  if(size > 100) {
    more = size - 100;
    size = 100;
  }


  k = 0;
  while(k < size)
  {
#if 1
    int j;
    /*for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");*/
    for(j = 0; j < 8 && k < size; j++, k++)
    {
      fprintf(self->dumper.out,"%.10e",buf[k]);
      if(k != size-1)
        fprintf(self->dumper.out,", ");
    }
    fprintf(self->dumper.out,"\n");
#else

    fprintf(self->dumper.out,"%d %g\n",k,buf[k]);

#endif

  }
  if(more)
  {
    /*for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");*/
    fprintf(self->dumper.out,"... %d more values\n",more);
  }

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
  fprintf(self->dumper.out,"} # %s %s \n",a->creator->op, a->name);
  grib_context_free(d->handle->context,buf);
}

static void dump_label(grib_dumper* d,grib_accessor* a,const char* comment)
{
  /*grib_dumper_wmo *self = (grib_dumper_wmo*)d;

  for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
  fprintf(self->dumper.out,"----> %s %s %s\n",a->creator->op, a->name,comment?comment:"");*/
}

static void dump_section(grib_dumper* d,grib_accessor* a,grib_block_of_accessors* block)
{
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  grib_section* s = grib_get_sub_section(a);
  int is_wmo_section=0;
  char* upper=NULL;
  char tmp[512];
  char *p=NULL,*q=NULL;
  if (!strncmp(a->name,"section",7)) is_wmo_section=1;

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
  if (is_wmo_section) {
    upper=(char*)malloc(strlen(a->name)+1);
    p=(char*)a->name;
    q=upper;
    while (*p != '\0') {
      *q=toupper(*p);
      q++;
      p++;
    }
    *q='\0';
    sprintf(tmp,"%s ( length=%ld, padding=%ld )",upper,(long)s->length,(long)s->padding);
    fprintf(self->dumper.out,"======================   %-35s   ======================\n",tmp);
    free(upper);
    self->section_offset=a->offset;
  } else {

  }

  /*printf("------------- section_offset = %ld\n",self->section_offset);*/
  d->depth += 3;
  grib_dump_accessors_block(d,block);
  d->depth -= 3;

  /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
  /*fprintf(self->dumper.out,"<===== %s %s\n",a->creator->op, a->name);*/
}

static void set_begin_end(grib_dumper* d,grib_accessor* a) {
  grib_dumper_wmo *self = (grib_dumper_wmo*)d;
  if ((d->option_flags & GRIB_DUMP_FLAG_OCTECT) != 0) {

    self->begin=a->offset-self->section_offset+1;
    self->end=grib_get_next_position_offset(a)-self->section_offset;
  } else {
    self->begin=a->offset;
    self->end=grib_get_next_position_offset(a);
  }
}

static void print_offset(FILE* out,long begin,long end) {
  char tmp[50];
  if (begin == end)
    fprintf(out,"%-10ld" ,begin);
  else {
    sprintf(tmp,"%ld-%ld" ,begin,end);
    fprintf(out,"%-10s",tmp);
  }
}

static void print_hexadecimal(FILE* out,unsigned long flags,grib_accessor* a) {
  int i=0;
  unsigned long offset=0;
  if ((flags & GRIB_DUMP_FLAG_HEXADECIMAL) != 0 && a->length != 0) {
    fprintf(out," (");
    offset=a->offset;
    for (i=0;i<a->length;i++) {
      fprintf(out," 0x%.2X",a->parent->h->buffer->data[offset]);
      offset++;
    }
    fprintf(out," )");
  }
}