File: grib_convert.c

package info (click to toggle)
eccodes 2.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 154,404 kB
  • sloc: cpp: 162,953; ansic: 26,308; sh: 21,742; f90: 6,854; perl: 6,361; python: 5,172; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 283; xml: 183; awk: 66
file content (210 lines) | stat: -rw-r--r-- 5,539 bytes parent folder | download | duplicates (4)
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
/*
 * (C) Copyright 2005- 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.
 */

/*
 * C Implementation: grib_convert
 *
 */

#include "grib_tools.h"


grib_option grib_options[]={
  /*  {id, args, help}, on, command_line, value*/
  {"f",0,0,0,1,0},
  {"F",0,0,1,0,0},
  {"q",0,0,1,0,0},
  {"M",0,0,0,1,0},
  {"U",0,0,1,0,0},
  {"I",0,0,1,0,0},
  {"g",0,0,0,1,0},
  {"G",0,0,0,1,0},
  {"T",0,0,0,1,0},
  {"V",0,0,0,1,0},
  {"7",0,0,0,1,0},
  {"v",0,0,0,1,0}
};

char* tool_description=
"It converts grib messages applying the rules from a conversion_rules file."
"\n\tThe rules are of the type \"keyname = value;\" and if blocks are allowed as"
"\n\tif ( keyname1 == value1 || keyname2 != value2 && keyname3 == value3 ) {"
"\n\t keyname4 = value4;"
"\n\t}";
char* tool_name="grib_convert";
char* tool_usage="[options] conversion_rules "
"grib_file grib_file ... output_grib_file";

int grib_options_count=sizeof(grib_options)/sizeof(grib_option);

int main(int argc, char *argv[]) { return grib_tool(argc,argv);}

int grib_tool_before_getopt(grib_runtime_options* options) {
  return 0;
}

int grib_tool_init(grib_runtime_options* options) {
  int set=0;
  grib_rule* r=NULL;

  options->rules = grib_parse_rules_file(NULL,options->infile_extra->name);

  if(!options->rules) {
    printf("Unable to build rules from %s\n",options->infile_extra->name);
    exit(1);
  }
  r = options->rules;
  set = 0;
  while(r)  {
    if(!r->condition) {
      grib_rule_entry* e = r->entries;
      while(e)  {
        Assert(set < (sizeof(options->set_values)/sizeof(options->set_values[0])));
        options->set_values[set].name = e->name;
        GRIB_CHECK_NOLINE(grib_expression_set_value(NULL,e->value,&(options->set_values[set])),NULL);
        set++;
        e = e->next;
      }
    }
    r = r->next;
  }
  options->set_values_count=set;

  if(options->verbose)
  {
    printf("Fixed rules:\n");
    printf("------------\n");
    grib_print_values(options->set_values,
        options->set_values_count);
    printf("\n");
  }
/*
  if (options->outfile && options->outfile->name) {
    options->outfile->file = fopen(options->outfile->name,"w");
    if(!options->outfile->file) {
      perror(options->outfile->name);
      exit(1);
    }
  }
*/
  return 0;
}

int grib_tool_new_filename_action(grib_runtime_options* options,const char* file) {
   return 0;
}

int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file) {
  return 0;
}

int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
  grib_rule* r = options->rules;
  int i;
  int err=0;
  double scale = 1;
  double offset = 0;

  int set = options->set_values_count;

  while(r) {
    if(r->condition)  {
      long ok = 0;
      GRIB_CHECK_NOLINE(grib_expression_evaluate_long(h,r->condition,&ok),NULL);
      if(ok) {
        grib_rule_entry* e = r->entries;
        while(e) {
          if (!strcmp(e->name,"skip")) return 0;
          Assert(set < (sizeof(options->set_values)/sizeof(options->set_values[0])));
          options->set_values[set].name = e->name;
          options->set_values[set].error=grib_expression_set_value(h,
                                              e->value,&(options->set_values[set]));
          set++;
          e = e->next;
        }

      }
    }
    r = r->next;
  }

  if(options->verbose)
  {
    printf("Applying %d:\n",options->infile->handle_count);
    grib_print_values(options->set_values, set);
    printf("\n");
  }

  err=grib_set_values(h,options->set_values, set);

  if( err != GRIB_SUCCESS) {
      for(i = 0; i < options->set_values_count ; i++) {
        if(options->set_values[i].error != GRIB_SUCCESS && options->fail)
           grib_context_log(h->context,GRIB_LOG_ERROR,"unable to set key \"%s\" (%s)",
              options->set_values[i].name,grib_get_error_message( options->set_values[i].error));
      }
	  grib_context_log(h->context,GRIB_LOG_ERROR,"error processing message %d\n",options->infile->handle_count);
	  if (options->fail) exit(err);
  }

  grib_get_double(h,"scaleValuesBy",&scale);
  grib_get_double(h,"offsetValuesBy",&offset);

  if(scale != 1 || offset != 0)
  {
    double *v;
    size_t size;
    int i;

    GRIB_CHECK_NOLINE(grib_get_size(h,"values",&size),0);

    v    = (double*)calloc(size,sizeof(double));
    if(!v) {
      fprintf(stderr,"failed to allocate %ld bytes\n",(long)(size*sizeof(double)));
      exit(1);
    }

    GRIB_CHECK_NOLINE(grib_get_double_array(h,"values",v,&size),0);

    if(options->verbose)
    {
      printf("Applying scale=%g/offset=%g\n",scale,offset);
    }

    for(i = 0; i< size; i++)
      v[i] =  v[i] * scale + offset;

    GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
    free(v);
  }

  grib_tools_write_message(options,h);

  return 0;
}

int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h) {
  grib_handle_delete(h);
  return 0;
}

void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h) {
  grib_print_key_values(options,h);
}

int grib_tool_finalise_action(grib_runtime_options* options) {
  return 0;
}

int grib_no_handle_action(int err) {
  fprintf(dump_file,"\t\t\"ERROR: unreadable message\"\n");
  return 0;
}