bufr2synop 0.24.0
update_tableD.c
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2013-2017 by Guillermo Ballester Valor *
3 * gbv@ogimet.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20/*!
21 \file update_tableD.c
22 \brief This file includes the code for update_TableD binary
23
24 It try to fill some field definitions for TableD from ECMWF. It fills
25 the 'Title_en' for revisions prior 18_0_0 from this version.
26*/
27
28
29#include "bufrdeco.h"
30
31// To use package config.h
32#ifndef CONFIG_H
33# include "config.h"
34# define CONFIG_H
35#endif
36
37const char SELF[] = "update_tableD";
38char INPUT_FILE[256];
43FILE *F, *R;
44
45
46void print_usage ( void )
47{
48 printf ( "%s %s\n", SELF, PACKAGE_VERSION );
49 printf ( "Usage: \n" );
50 printf ( "%s -i input_file -r reference_file [-h]\n" , SELF );
51 printf ( " -h Print this help\n" );
52}
53
54int main ( int argc, char *argv[] )
55{
56 int iopt, i, j, k, nt;
57 char caux[CSV_MAXL], caux2[CSV_MAXL];
58 char *tk[16];
59
60 // read arguments
61 INPUT_FILE[0] = 0;
62 REFERENCE_FILE[0] = 0;
63
64 while ( ( iopt = getopt ( argc, argv, "hi:r:" ) ) !=-1 )
65 switch ( iopt )
66 {
67 case 'i':
68 if ( strlen ( optarg ) < 256 )
69 strcpy ( INPUT_FILE, optarg );
70 break;
71 case 'r':
72 if ( strlen ( optarg ) < 256 )
73 strcpy ( REFERENCE_FILE, optarg );
74 break;
75 case 'h':
76 default:
78 exit ( EXIT_SUCCESS );
79 }
80
81 // Open reference file
82 if ( ( R = fopen ( REFERENCE_FILE, "r" ) ) == NULL )
83 {
84 fprintf ( stderr, "%s: Error. Cannot open file '%s'\n", SELF, REFERENCE_FILE );
85 exit ( EXIT_FAILURE );
86 }
87
88 // read and proccess refence file
89 i = 0;
90 while ( fgets ( REF[i], CSV_MAXL, R ) != NULL )
91 {
92 // Parse line
93 if ( parse_csv_line ( &nt, TK[i], REF[i] ) < 0 )
94 {
95 fprintf ( stderr, "%s: Error parsing csv line '%s' from file '%s'\n", SELF, LIN, INPUT_FILE );
96 fclose ( R );
97 exit ( EXIT_FAILURE );
98 }
99 i++;
100 }
101 fclose ( R );
102
103 // Open source file
104 if ( ( F = fopen ( INPUT_FILE, "r" ) ) == NULL )
105 {
106 fprintf ( stderr, "%s: Error. Cannot open file '%s'\n", SELF, INPUT_FILE );
107 exit ( EXIT_FAILURE );
108 }
109
110 // Read and process file
111 j = 0;
112 while ( fgets ( LIN, CSV_MAXL, F ) != NULL )
113 {
114 // Parse line
115 if ( parse_csv_line ( &nt, tk, LIN ) < 0 )
116 {
117 fprintf ( stderr, "%s: Error parsing csv line '%s' from file '%s'\n", SELF, LIN, INPUT_FILE );
118 fclose ( F );
119 exit ( EXIT_FAILURE );
120 }
121
122 printf ( "%s,", csv_quoted_string ( caux, tk[0] ) );
123 printf ( "%s,", csv_quoted_string ( caux, tk[1] ) );
124
125
126 if ( tk[2][0] == 0 ) //case of void field, lets go
127 {
128 caux[0] = 0;
129 // Search the same key tk[0] in reference file
130 for ( k = j; k < i ; k++ )
131 {
132 if ( strcmp ( TK[k][0], tk[0] ) == 0 )
133 {
134 strcpy ( caux2, TK[k][2] );
135 j = k;
136 break;
137 }
138 }
139 printf ( "%s,", csv_quoted_string ( caux, caux2 ) );
140
141 }
142 else
143 printf ( "%s,", csv_quoted_string ( caux, tk[2] ) );
144
145
146 printf ( "%s\n", csv_quoted_string ( caux, tk[3] ) );
147 }
148 fclose ( F );
149
150 exit ( EXIT_SUCCESS );
151}
Include header file for bufrdeco library.
#define CSV_MAXL
Maximum length in a string to be parsed as csv.
Definition: bufrdeco.h:128
int parse_csv_line(int *nt, char *tk[], char *lin)
Parse a csv line.
Definition: bufrdeco_csv.c:258
char * csv_quoted_string(char *out, char *in)
Transform a string to a quoted string to be inserted in a csv file.
Definition: bufrdeco_csv.c:48
#define BUFR_MAXLINES_TABLED
The maximum expected lines in a Table D file.
Definition: bufrdeco.h:103
#define PACKAGE_VERSION
Definition: config.h:4
char LIN[CSV_MAXL]
Definition: update_tableD.c:42
char INPUT_FILE[256]
Definition: update_tableD.c:38
int main(int argc, char *argv[])
Definition: update_tableD.c:54
char REF[BUFR_MAXLINES_TABLED][CSV_MAXL]
Definition: update_tableD.c:41
const char SELF[]
Definition: update_tableD.c:37
void print_usage(void)
Definition: update_tableD.c:46
char * TK[BUFR_MAXLINES_TABLED][16]
Definition: update_tableD.c:40
FILE * F
Definition: update_tableD.c:43
FILE * R
Definition: update_tableD.c:43
char REFERENCE_FILE[256]
Definition: update_tableD.c:39