File: codes_export_resource.cc

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (125 lines) | stat: -rw-r--r-- 3,945 bytes parent folder | download | duplicates (2)
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
/*
 * (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.
 */

#include "grib_api_internal.h"

typedef enum ResourceType
{
    UNKNOWN,
    SAMPLE,
    DEFINITION
} ResourceType;

static void usage(const char* prog)
{
    printf("Usage: %s [-s | -d] resource_path out_file\n", prog);
    printf("       -s Extract a resource from the samples directory\n");
    printf("       -d Extract a resource from the definitions directory\n");
    printf("\n");
    printf("       E.g., %s -s GRIB2.tmpl sample.grib2\n", prog);
    printf("       E.g., %s -d grib2/boot.def grib2_boot.txt\n", prog);

    char v[32] = {0,};
    snprintf(v, sizeof(v), "%d.%d.%d", ECCODES_MAJOR_VERSION, ECCODES_MINOR_VERSION, ECCODES_REVISION_VERSION);
    printf("\nTo access all the definitions/samples for ecCodes version %s:\n", v);
    printf("\tgit clone --depth=1 git@github.com:ecmwf/eccodes -b %s eccodes_v%s\n", v, v);
    printf("\tls eccodes_v%s/definitions\n", v);
    printf("\tls eccodes_v%s/samples\n", v);
    exit(1);
}

#define SIZE (1024 * 1024)
int main(int argc, char* argv[])
{
    char* resource_path        = NULL;
    const char* resource_name  = NULL;
    ResourceType resource_type = UNKNOWN;
    char* full_path            = NULL;
    const char* out_file       = NULL;
    const char* option         = NULL;
    grib_context* c            = grib_context_get_default();
    FILE* fin                  = NULL;
    FILE* fout                 = NULL;
    char buffer[SIZE]          = {0,};
    size_t bytes = 0;

    if (argc != 4) usage(argv[0]);

    option        = argv[1];
    resource_path = argv[2];
    out_file      = argv[3];

    if (strcmp(option, "-s") == 0) {
        resource_type = SAMPLE;
        resource_name = "sample";
    }
    else if (strcmp(option, "-d") == 0) {
        resource_type = DEFINITION;
        resource_name = "definition";
    }
    else {
        fprintf(stderr, "Error: Invalid option: Specify either -s or -d\n");
        return 1;
    }

    if (resource_type == SAMPLE) {
        full_path = get_external_sample_path(c, resource_path);
    }
    else if (resource_type == DEFINITION) {
        full_path = grib_context_full_defs_path(c, resource_path);
    }
    if (!full_path) {
        fprintf(stderr, "Error: Failed to access %s file: '%s'\n", resource_name, resource_path);
        return 1;
    }

    fout = fopen(out_file, "wb");
    if (!fout) {
        fprintf(stderr, "Error: Failed to open output file '%s'\n", out_file);
        perror(out_file);
        return 1;
    }
    fin = codes_fopen(full_path, "r");
    if (!fin) {
        fprintf(stderr, "Error: Failed to open resource '%s'\n", full_path);
        fclose(fout);
        return 1;
    }

    /* write resource bytes to fout */
    int read_count = 0;
    while (0 < (bytes = fread(buffer, 1, sizeof(buffer), fin))) {
        if (fwrite(buffer, 1, bytes, fout) != bytes) {
            fprintf(stderr, "Error: Failed to write out bytes\n");
            return 1;
        }
        ++read_count;
    }

    if (fclose(fin) != 0) {
        fprintf(stderr, "Error: Call to fclose failed (input)\n");
        return 1;
    }
    if (fclose(fout) != 0) {
        fprintf(stderr, "Error: Call to fclose failed (output)\n");
        return 1;
    }

    grib_context_free(c, full_path);

    if (read_count == 0) {
        fprintf(stderr, "Error: Number of bytes read == 0. Please check the path refers to a regular %s file.\n",
                        (resource_type == SAMPLE) ? "sample" : "definition");
    } else {
        printf("Resource exported to file '%s'.\n", out_file);
    }

    return 0;
}