NCEPLIBS-g2c 2.0.0
Loading...
Searching...
No Matches
g2c_degrib2.c
Go to the documentation of this file.
1
5#include <ctype.h>
6#include <grib2.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10
23int
24main(int argc, char **argv)
25{
26 int verbose = 0;
27 int index;
28 int g2cid;
29 int c;
30 int p = 0;
31 char *filein = NULL;
32 char *fileidx = NULL;
33 char *fileout = NULL;
34 int ret;
35
36 opterr = 0;
37
38 /* Parse command line arguments. */
39 while ((c = getopt(argc, argv, "vo:")) != -1)
40 {
41 switch (c)
42 {
43 case 'v':
44 verbose = 1;
45 break;
46 case 'o':
47 if (!(fileout = malloc(sizeof(char) * strlen(optarg) + 1)))
48 return G2C_ENOMEM;
49 strcpy(fileout, optarg);
50 break;
51 case '?':
52 if (isprint(optopt))
53 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
54 else
55 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
56 return 1;
57 default:
58 return G2C_ERROR;
59 }
60 }
61
62 /* Get names of input file(s). */
63 for (index = optind; index < argc; index++)
64 {
65 if (!p)
66 {
67 if (!(filein = malloc(sizeof(char) * strlen(argv[index]) + 1)))
68 return G2C_ENOMEM;
69 strcpy(filein, argv[index]);
70 }
71 else
72 {
73 if (!(fileidx = malloc(sizeof(char) * strlen(argv[index]) + 1)))
74 return G2C_ENOMEM;
75 strcpy(fileidx, argv[index]);
76 }
77 if (++p == 2)
78 break;
79 }
80
81 /* Turn on logging for verbose output. This only has effect if the
82 * library was built with LOGGING=ON. */
83 if (verbose)
85
86 /* If we got one input file, open it. If we got two input files,
87 * the second is an index file for the first. */
88 if (p == 1)
89 {
90 if (verbose)
91 printf("g2c_degrib2 %s summarizing %s into %s.\n", G2C_VERSION, filein, fileout);
92
93 /* Open the GRIB2 file. */
94 if ((ret = g2c_open(filein, G2C_NOWRITE, &g2cid)))
95 {
96 fprintf(stderr, "Could not read file %s.\n", filein);
97 return ret;
98 }
99 }
100 else if (p == 2)
101 {
102 if (verbose)
103 printf("g2c_degrib2 %s summarizing %s, with index %s into %s.\n", G2C_VERSION,
104 filein, fileidx, fileout);
105
106 /* Open the GRIB2 file with index. */
107 if ((ret = g2c_open_index(filein, fileidx, G2C_NOWRITE, &g2cid)))
108 {
109 fprintf(stderr, "Could not read file %s with index %s.\n", filein, fileidx);
110 return ret;
111 }
112 }
113 else
114 {
115 printf("One or two filenames must be provided, for input and (optionally) index.\n");
116 return G2C_ERROR;
117 }
118
119 /* Write the degrib2 summary. */
120 if ((ret = g2c_degrib2(g2cid, fileout)))
121 {
122 fprintf(stderr, "Could not write degrib2 summary to %s.\n", fileout);
123 return ret;
124 }
125
126 /* Close the file. */
127 if ((ret = g2c_close(g2cid)))
128 {
129 fprintf(stderr, "Error closing the file.\n");
130 return ret;
131 }
132
133 /* Free memory. */
134 if (filein)
135 free(filein);
136 if (fileidx)
137 free(fileidx);
138 if (fileout)
139 free(fileout);
140
141 return 0;
142}
int main(int argc, char **argv)
Compare two GRIB2 files.
Definition g2c_degrib2.c:24
int g2c_degrib2(int g2cid, const char *fileout)
Write a summary file like the degrib2 utility.
Definition g2cdegrib2.c:634
int g2c_open(const char *path, int mode, int *g2cid)
Open an existing GRIB2 file.
Definition g2cfile.c:1190
int g2c_close(int g2cid)
Close a GRIB2 file, freeing resources.
Definition g2cfile.c:1356
int g2c_open_index(const char *data_file, const char *index_file, int mode, int *g2cid)
Open a GRIB2 file with the help of an index file.
Definition g2cindex.c:902
Header file for NCEPLIBS-g2c library.
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:485
#define G2C_ERROR
General error code, returned for some test errors.
Definition grib2.h:477
#define G2C_VERSION
Current version of NCEPLIBS-g2c library.
Definition grib2.h:25
#define G2C_NOWRITE
Set read-only access for g2c_open().
Definition grib2.h:275
int g2c_set_log_level(int new_level)
Use this to set the global log level.
Definition util.c:129