From 0f29c103d9992ca2ae9336c1fa18da98234e800c Mon Sep 17 00:00:00 2001 From: Chris Bradley Date: Tue, 26 Nov 2024 17:01:27 +0000 Subject: [PATCH] Improve scan command line arg support --- src/tools/gribjump-scan.cc | 66 +++++++++++++++++++----------- tests/tools/callback_vs_scan.sh.in | 2 +- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/tools/gribjump-scan.cc b/src/tools/gribjump-scan.cc index 0d601fe..ceb370d 100644 --- a/src/tools/gribjump-scan.cc +++ b/src/tools/gribjump-scan.cc @@ -15,6 +15,7 @@ #include "metkit/mars/MarsExpension.h" #include "fdb5/api/FDB.h" +#include "fdb5/api/helpers/FDBToolRequest.h" #include "gribjump/GribJump.h" #include "gribjump/tools/GribJumpTool.h" @@ -31,21 +32,21 @@ class Scan: public GribJumpTool { virtual void execute(const eckit::option::CmdArgs &args); virtual void usage(const std::string &tool) const; - virtual int numberOfPositionalArguments() const { return 1; } + virtual int numberOfPositionalArguments() const { return -1; } public: Scan(int argc, char **argv): GribJumpTool(argc, argv) { + options_.push_back(new eckit::option::SimpleOption("file", "Reads the mars requests from a file, rather than from the command line")); options_.push_back(new eckit::option::SimpleOption("raw", "Uses the raw request, without expansion")); - options_.push_back(new eckit::option::SimpleOption("files", "Scan entire files matching the request (default: true)")); + options_.push_back(new eckit::option::SimpleOption("byfiles", "Scan entire files matching the request (default: true)")); } }; void Scan::usage(const std::string &tool) const { eckit::Log::info() << std::endl - << "Usage: " << tool << " " << std::endl - << " " << tool << " --raw " << std::endl - << " " << tool << " --files " << std::endl + << "Usage: " << tool << " class=od,stream=oper,expver=xxxx" << std::endl + << " " << tool << " --file=" << std::endl ; GribJumpTool::usage(tool); @@ -54,29 +55,48 @@ void Scan::usage(const std::string &tool) const { void Scan::execute(const eckit::option::CmdArgs &args) { bool raw = args.getBool("raw", false); - bool files = args.getBool("files", false); - - // Build request(s) from input - std::ifstream in(args(0).c_str()); - if (in.bad()) { - throw eckit::ReadError(args(0)); - } + bool byfiles = args.getBool("byfiles", false); + std::string file = args.getString("file", ""); std::vector requests; - metkit::mars::MarsParser parser(in); - auto parsedRequests = parser.parse(); - if (raw) { - for (auto r : parsedRequests) - requests.push_back(r); + + if (args.count() > 0 && !file.empty()) { + throw eckit::UserError("Invalid arguments: Cannot specify both a file (--file) and a request (positional arguments)"); + } + + if (!file.empty()){ + // Build request(s) from + std::ifstream in(file); + if (in.bad()) { + throw eckit::ReadError(args(0)); + } + + metkit::mars::MarsParser parser(in); + auto parsedRequests = parser.parse(); + if (raw) { + for (auto r : parsedRequests) + requests.push_back(r); + } else { + bool inherit = false; + metkit::mars::MarsExpension expand(inherit); + requests = expand.expand(parsedRequests); + } } else { - bool inherit = false; - metkit::mars::MarsExpension expand(inherit); - requests = expand.expand(parsedRequests); + // Build request(s) from positional arguments + std::vector requests_in; + for (size_t i = 0; i < args.count(); ++i) { + requests_in.emplace_back(args(i)); + } + + for (const std::string& request_string : requests_in) { + auto parsed = fdb5::FDBToolRequest::requestsFromString(request_string, {}, raw, "retrieve"); + for (auto r : parsed) { + requests.push_back(r.request()); + } + } } - GribJump gj; - - size_t nfields = gj.scan(requests, files); + size_t nfields = gj.scan(requests, byfiles); eckit::Log::info() << "Scanned " << nfields << " field(s)" << std::endl; } diff --git a/tests/tools/callback_vs_scan.sh.in b/tests/tools/callback_vs_scan.sh.in index 51db402..46ade95 100755 --- a/tests/tools/callback_vs_scan.sh.in +++ b/tests/tools/callback_vs_scan.sh.in @@ -80,7 +80,7 @@ FDB5_CONFIG_FILE=${bindir}/config_root1.yaml $fdbwrite ${testdata} echo "Writing to FDB without gribjump plugin, then scanning" FDB5_CONFIG_FILE=${bindir}/config_root2.yaml $fdbwrite ${testdata} -FDB5_CONFIG_FILE=${bindir}/config_root2.yaml $gjscan $selectedrequests +FDB5_CONFIG_FILE=${bindir}/config_root2.yaml $gjscan --file=$selectedrequests # ------------------------------------- # 3: FDB with gribjump plugin