File: mir-count.cc

package info (click to toggle)
metview 5.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 242,296 kB
  • sloc: cpp: 437,117; ansic: 41,433; xml: 19,944; f90: 13,059; sh: 6,562; python: 3,953; yacc: 1,774; lex: 1,121; perl: 701; makefile: 92
file content (165 lines) | stat: -rw-r--r-- 5,161 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * (C) Copyright 1996- 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 <memory>

#include "eckit/log/JSON.h"
#include "eckit/option/CmdArgs.h"
#include "eckit/option/SimpleOption.h"
#include "eckit/option/VectorOption.h"

#include "mir/data/MIRField.h"
#include "mir/input/GribFileInput.h"
#include "mir/key/grid/Grid.h"
#include "mir/repres/Iterator.h"
#include "mir/repres/Representation.h"
#include "mir/repres/latlon/RegularLL.h"
#include "mir/tools/Count.h"
#include "mir/tools/MIRTool.h"
#include "mir/util/Increments.h"


//using prec_t = decltype(std::cout.precision());
using prec_t = size_t; 


class MIRCount : public mir::tools::MIRTool {
private:
    void execute(const eckit::option::CmdArgs&);

    int minimumPositionalArguments() const { return 0; }

    void usage(const std::string& tool) const {
        eckit::Log::info() << "\nCount MIR representation number of values, compared to the GRIB numberOfValues."
                              "\n"
                              "\nUsage:"
                              "\n\t"
                           << tool
                           << " [--area=N/W/S/E] file.grib [file.grib [...]]"
                              "\n\t"
                           << tool
                           << " [--area=N/W/S/E] --grid=WE/SN"
                              "\n\t"
                           << tool
                           << " [--area=N/W/S/E] --grid=WE/SN --ni-nj"
                              "\n\t"
                           << tool
                           << " [--area=N/W/S/E] --gridname=[FNOfno][1-9][0-9]*  # ..."
                              "\n"
                              "\n"
                              "Examples:"
                              "\n\t"
                              "% "
                           << tool
                           << " 1.grib"
                              "\n\t"
                              "% "
                           << tool
                           << " --area=6/0/0/6 1.grib 2.grib"
                              "\n\t"
                              "% "
                           << tool
                           << " --area=6/0/0/6 --gridname=O1280"
                              "\n\t"
                              "% "
                           << tool << " --area=6/0/0/6 --grid=1/1 --ni-nj" << std::endl;
    }

public:
    MIRCount(int argc, char** argv) : MIRTool(argc, argv) {
        using eckit::option::SimpleOption;
        using eckit::option::VectorOption;

        // options_.push_back(new SimpleOption< bool >("sizes", "compare sizes of coordinates and values vectors,
        // default false"));
        options_.push_back(new VectorOption<double>("area", "cropping area (North/West/South/East)", 4));
        options_.push_back(new SimpleOption<std::string>("gridname", "grid name: [FNOfno][1-9][0-9]*"));
        options_.push_back(new VectorOption<double>("grid", "regular grid increments (West-East/South-North)", 2));
        options_.push_back(new SimpleOption<prec_t>("precision", "Output precision"));
    }
};


void MIRCount::execute(const eckit::option::CmdArgs& args) {
    using mir::util::BoundingBox;

    auto& log = eckit::Log::info();
    eckit::JSON j(log);

    prec_t precision;
    args.get("precision", precision) ? log.precision(precision) : log.precision();

    std::vector<double> area;
    args.get("area", area);


    // setup a regular lat/lon representation and perfom count
    std::vector<double> grid;
    if (args.get("grid", grid)) {
        ASSERT(!args.has("gridname"));

        mir::tools::Count counter(area);
        counter.countOnGridIncrements(grid);

        counter.json(j);
        return;
    }


    // setup a representation from gridname and perfom count
    std::string gridname;
    if (args.get("gridname", gridname)) {
        ASSERT(!args.has("grid"));

        mir::tools::Count counter(area);
        counter.countOnNamedGrid(gridname);

        counter.json(j);
        return;
    }


    // count each file(s) message(s)
    mir::tools::Count counter(area);

    j.startObject();
    j << "files";
    j.startList();

    for (size_t i = 0, k = 0; i < args.count(); ++i, k = 0) {

        mir::input::GribFileInput grib(args(i));
        while (grib.next()) {
            ++k;

            auto field = static_cast<const mir::input::MIRInput&>(grib).field();
            mir::repres::RepresentationHandle rep(field.representation());

            counter.countOnRepresentation(*rep);

            j.startObject();
            j << "file" << args(i);
            j << "fileMessage" << k;
            counter.json(j, false);
            j.endObject();
        }
    }

    j.endList();
    j.endObject();
}


int main(int argc, char** argv) {
    MIRCount tool(argc, argv);
    return tool.start();
}