00001 /*------------------------------------------------------------------------- 00002 00003 BUFR encoding and decoding software and library 00004 Copyright (c) 2007, Institute of Broadband Communication, TU-Graz 00005 on behalf of EUMETNET OPERA, http://www.knmi.nl/opera 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; version 2.1 00010 of the License. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 ---------------------------------------------------------------------------- 00022 00023 FILE: ENCBUFR.C 00024 IDENT: $Id: encbufr.c,v 1.12 2007/12/18 14:40:13 fuxi Exp $ 00025 00026 AUTHOR: Juergen Fuchsberger, Helmut Paulitsch, Konrad Koeck 00027 Institute of Communication and Wave Propagation, 00028 Technical University Graz, Austria 00029 00030 VERSION NUMBER:3.0 00031 00032 DATE CREATED: 18-DEC-2001 00033 00034 STATUS: DEVELOPMENT FINISHED 00035 00036 AMENDMENT RECORD: 00037 00038 $Log: encbufr.c,v $ 00039 Revision 1.12 2007/12/18 14:40:13 fuxi 00040 added licence header 00041 00042 Revision 1.11 2007/12/07 08:16:41 fuxi 00043 update to version 3.0 00044 00045 Revision 1.10 2005/06/01 09:47:54 helmutp 00046 update version, use local tables V4 00047 00048 Revision 1.9 2005/04/06 09:08:10 helmutp 00049 local tables v5 00050 00051 Revision 1.8 2005/04/04 15:41:47 helmutp 00052 update to version 2.3 00053 use subcenter and generating center 00054 00055 Revision 1.7 2003/06/11 09:13:13 helmutp 00056 added version string 00057 00058 Revision 1.6 2003/06/06 11:52:34 helmutp 00059 select descriptor tables with different versions 00060 00061 Revision 1.5 2003/03/27 17:17:39 helmutp 00062 update to version 2.2 00063 00064 Revision 1.4 2003/03/13 17:22:24 helmutp 00065 allow tables to be specified on command line 00066 00067 Revision 1.3 2003/03/11 10:30:42 helmutp 00068 fixed memory leaks 00069 00070 Revision 1.2 2003/03/06 17:12:32 helmutp 00071 update to version 2.1 00072 00073 Revision 1.1 2003/02/28 13:41:12 helmutp 00074 Initial revision 00075 00076 --------------------------------------------------------------------------- */ 00077 00085 #include <stdlib.h> 00086 #include <stdio.h> 00087 #include <string.h> 00088 #include "bufrlib.h" 00089 #include "bufr_io.h" 00090 00091 #define BUFR_EDITION 3 00092 /*===========================================================================*/ 00093 /* internal functions */ 00094 /*===========================================================================*/ 00095 00096 static void free_all(bufr_t* bufr); 00097 00098 /*===========================================================================*/ 00099 /* internal data */ 00100 /*===========================================================================*/ 00101 00102 char *usage = "usage: encbufr [-v] [-d tabdir] input_file output_file\n"; 00103 char *version = "encbufr V3.0, 5-Dec-2007\n"; 00104 00105 /*===========================================================================*/ 00106 int main (int argc, char* argv[]) 00107 { 00108 sect_1_t s1; /* struct holding information from section 1 */ 00109 bufr_t bufr_dest; /* struct holding encoded bufr message */ 00110 char srcfile[200], buffile[200]; /* filenames for source and destination */ 00111 char *table_dir = NULL; 00112 long year, mon, day, hour, min; 00113 00114 /* initialize variables */ 00115 00116 memset (&bufr_dest, 0, sizeof (bufr_t)); 00117 memset (&s1, 0, sizeof (sect_1_t)); 00118 00119 /* set bufr edition */ 00120 00121 _bufr_edition = BUFR_EDITION; 00122 00123 /* check command line parameters */ 00124 00125 while (argc > 1 && *argv[1] == '-') 00126 { 00127 if (*(argv[1] + 1) == 'v') 00128 fprintf (stderr, "%s", version); 00129 else if (*(argv[1] + 1) == 'd') 00130 { 00131 if (argc < 2) 00132 { 00133 fprintf (stderr, "Missing parameter for -d\n\n%s", usage); 00134 exit (EXIT_FAILURE); 00135 } 00136 table_dir = argv[2]; 00137 argc--; 00138 argv++; 00139 } 00140 else 00141 { 00142 fprintf (stderr, "Invalid parameter %s\n\n%s", argv[1], usage); 00143 exit (EXIT_FAILURE); 00144 } 00145 argc--; 00146 argv++; 00147 } 00148 00149 /* Get input- and output-filenames from the command-line */ 00150 00151 if (argc < 3) 00152 { 00153 fprintf (stderr, "%s", usage); 00154 exit (EXIT_FAILURE); 00155 } 00156 strcpy (srcfile, argv[1]); 00157 strcpy (buffile, argv[2]); 00158 00159 /* Read section 1 from ASCII input file */ 00160 00161 bufr_sect_1_from_file (&s1, "section.1"); 00162 00163 /* read supported data descriptors from tables */ 00164 00165 if (read_tables (table_dir, s1.vmtab, s1.vltab, s1.subcent, 00166 s1.gencent) < 0) { 00167 free_all (&bufr_dest); 00168 exit (EXIT_FAILURE); 00169 } 00170 00171 /* code data in the source-file to a data-descriptor- and data-section */ 00172 00173 if (!bufr_data_from_file (srcfile, &bufr_dest)) { 00174 free_all (&bufr_dest); 00175 exit (EXIT_FAILURE); 00176 } 00177 00178 /* setup date and time if necessary */ 00179 00180 if (s1.year == 999) { 00181 bufr_get_date_time (&year, &mon, &day, &hour, &min); 00182 s1.year = (int) year; 00183 s1.mon = (int) mon; 00184 s1.day = (int) day; 00185 s1.hour = (int) hour; 00186 s1.min = (int) min; 00187 s1.sec = 0; 00188 } 00189 00190 /* encode section 0, 1, 2, 5 */ 00191 00192 if (!bufr_encode_sections0125 (&s1, &bufr_dest)) { 00193 fprintf (stderr, "Unable to create section 0, 1, 2 and/or 5\n"); 00194 free_all (&bufr_dest); 00195 exit (EXIT_FAILURE); 00196 } 00197 00198 /* Save coded data */ 00199 00200 if (!bufr_write_file (&bufr_dest, buffile)) { 00201 free_all (&bufr_dest); 00202 exit (EXIT_FAILURE); 00203 } 00204 00205 /* Free data */ 00206 00207 free_all (&bufr_dest); 00208 exit (EXIT_SUCCESS); 00209 } 00210 00211 /*===========================================================================*/ 00212 00213 static void free_all(bufr_t* bufr) { 00214 00215 free_descs(); 00216 bufr_free_data(bufr); 00217 } 00218 00219 00220 /* end of file */
1.5.4