Skip to content

Commit

Permalink
Improve scan command line arg support
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisspyB committed Nov 26, 2024
1 parent 018cad3 commit 0f29c10
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
66 changes: 43 additions & 23 deletions src/tools/gribjump-scan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<std::string>("file", "Reads the mars requests from a file, rather than from the command line"));
options_.push_back(new eckit::option::SimpleOption<bool>("raw", "Uses the raw request, without expansion"));
options_.push_back(new eckit::option::SimpleOption<bool>("files", "Scan entire files matching the request (default: true)"));
options_.push_back(new eckit::option::SimpleOption<bool>("byfiles", "Scan entire files matching the request (default: true)"));
}

};

void Scan::usage(const std::string &tool) const {
eckit::Log::info() << std::endl
<< "Usage: " << tool << " <mars request file>" << std::endl
<< " " << tool << " --raw <mars request file>" << std::endl
<< " " << tool << " --files <mars request file>" << std::endl
<< "Usage: " << tool << " class=od,stream=oper,expver=xxxx" << std::endl
<< " " << tool << " --file=<mars request file>" << std::endl
;

GribJumpTool::usage(tool);
Expand All @@ -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<metkit::mars::MarsRequest> 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 <file>
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<std::string> 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;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/tools/callback_vs_scan.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f29c10

Please sign in to comment.