NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
g2c_index.c
Go to the documentation of this file.
1
7#include <ctype.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <grib2.h>
12
25int
26main(int argc, char **argv)
27{
28 char *filein = NULL;
29 char *fileout = NULL;
30 int verbose = 0;
31 int large_file_index = 0;
32 int index;
33 int write_index_flag = G2C_CLOBBER;
34 int c;
35 int p = 0;
36 int g2cid;
37 int ret;
38
39 opterr = 0;
40
41 /* Parse command line arguments. */
42 while ((c = getopt(argc, argv, "vlo:")) != -1)
43 {
44 switch (c)
45 {
46 case 'v':
47 verbose = 1;
48 break;
49 case 'l':
50 large_file_index = 1;
51 break;
52 case 'o':
53 if (!(fileout = malloc(sizeof(char) * strlen(optarg) + 1)))
54 return G2C_ENOMEM;
55 strcpy(fileout, optarg);
56 break;
57 case '?':
58 if (isprint(optopt))
59 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
60 else
61 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
62 return 1;
63 default:
64 return G2C_ERROR;
65 }
66 }
67
68 /* Get names of input file. */
69 for (index = optind; index < argc; index++)
70 {
71 if (!(filein = malloc(sizeof(char) * strlen(argv[index]) + 1)))
72 return G2C_ENOMEM;
73 strcpy(filein, argv[index]);
74 if (++p == 1)
75 break;
76 }
77
78 /* Yammer on and on. */
79 if (verbose)
80 {
81 printf("g2c_index %s reading index file %s summarizing into %s.\n",
82 G2C_VERSION, filein, fileout);
84 }
85
86 /* Open the GRIB2 file. */
87 if ((ret = g2c_open(filein, G2C_NOWRITE, &g2cid)))
88 return ret;
89
90 /* Write the index file. */
91 if (large_file_index)
92 write_index_flag |= G2C_LARGE_FILE_INDEX;
93 if ((ret = g2c_write_index(g2cid, write_index_flag, fileout)))
94 return ret;
95
96 /* Close the file. */
97 if ((ret = g2c_close(g2cid)))
98 return ret;
99
100 /* Free memory. */
101 if (filein)
102 free(filein);
103 if (fileout)
104 free(fileout);
105
106 return 0;
107}
int main(int argc, char **argv)
Write a GRIB2 index file.
Definition g2c_index.c:26
int g2c_open(const char *path, int mode, int *g2cid)
Open an existing GRIB2 file.
Definition g2cfile.c:1193
int g2c_close(int g2cid)
Close a GRIB2 file, freeing resources.
Definition g2cfile.c:1359
int g2c_write_index(int g2cid, int mode, const char *index_file)
Create an index file from a GRIB2 file, just like those created by the grb2index utility.
Definition g2cindex.c:433
Header file for NCEPLIBS-g2c library.
#define G2C_LARGE_FILE_INDEX
Create a large file index.
Definition grib2.h:294
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:500
#define G2C_ERROR
General error code, returned for some test errors.
Definition grib2.h:492
#define G2C_VERSION
Current version of NCEPLIBS-g2c library.
Definition grib2.h:25
#define G2C_CLOBBER
Destroy existing file.
Definition grib2.h:292
#define G2C_NOWRITE
Set read-only access for g2c_open().
Definition grib2.h:290
int g2c_set_log_level(int new_level)
Use this to set the global log level.
Definition util.c:129