Skip to content

Commit

Permalink
Fix #2844 VAPOR fails to reliably identify file formats of files supp…
Browse files Browse the repository at this point in the history
…lied on the command line (#2970)

* Fix #2844

* clang-format pre-push hook

* Add BOV logic to reject header files > 1MB in size

* clang-format

* Add -ftype option

* clang-format pre-push hook

* fix typo

Co-authored-by: Scott Pearse <pearse@ucar.edu>
  • Loading branch information
StasJ and sgpearse committed Jan 19, 2022
1 parent f9d3615 commit dd7ac4e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
46 changes: 25 additions & 21 deletions apps/vaporgui/MainForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class ProgressStatusBar : public QWidget {

// Only the main program should call the constructor:
//
MainForm::MainForm(vector<QString> files, QApplication *app, bool interactive, QWidget *parent) : QMainWindow(parent)
MainForm::MainForm(vector<QString> files, QApplication *app, bool interactive, string filesType, QWidget *parent) : QMainWindow(parent)
{
_initMembers();

Expand Down Expand Up @@ -465,12 +465,18 @@ MainForm::MainForm(vector<QString> files, QApplication *app, bool interactive, Q
for (auto &f : files) paths.push_back(f.toStdString());

string fmt;
if (determineDatasetFormat(paths, &fmt)) {
loadDataHelper("", paths, "", "", fmt, true, ReplaceFirst);

if (filesType == "auto") {
if (!determineDatasetFormat(paths, &fmt)) {
fmt = "";
MSG_ERR("Could not determine dataset format for command line parameters");
}
} else {
MSG_ERR("Could not determine dataset format for command line parameters");
fmt = filesType;
}

if (!fmt.empty()) loadDataHelper("", paths, "", "", fmt, true, ReplaceFirst);

_stateChangeCB();
}

Expand Down Expand Up @@ -559,23 +565,21 @@ template<class T> bool MainForm::isDatasetValidFormat(const std::vector<std::str

bool MainForm::determineDatasetFormat(const std::vector<std::string> &paths, std::string *fmt) const
{
if (isDatasetValidFormat<VDCNetCDF>(paths))
*fmt = "vdc";
else if (isDatasetValidFormat<DCWRF>(paths))
*fmt = "wrf";
else if (isDatasetValidFormat<DCMPAS>(paths))
*fmt = "mpas";
else if (isDatasetValidFormat<DCP>(paths))
*fmt = "dcp";
else if (isDatasetValidFormat<DCUGRID>(paths))
*fmt = "ugrid";
else if (isDatasetValidFormat<DCCF>(paths))
*fmt = "cf";
else if (isDatasetValidFormat<DCBOV>(paths))
*fmt = "bov";
else
return false;
return true;
vector<pair<string, bool>> formats = {
{"vdc", isDatasetValidFormat<VDCNetCDF>(paths)}, {"wrf", isDatasetValidFormat<DCWRF>(paths)}, {"mpas", isDatasetValidFormat<DCMPAS>(paths)}, {"dcp", isDatasetValidFormat<DCP>(paths)},
{"ugrid", isDatasetValidFormat<DCUGRID>(paths)}, {"cf", isDatasetValidFormat<DCCF>(paths)}, {"bov", isDatasetValidFormat<DCBOV>(paths)},
};

int nOk = 0;
for (auto &f : formats) {
if (f.second) {
nOk++;
*fmt = f.first;
}
}
if (nOk == 1) return true;
MyBase::SetErrMsg("Unable to confidently determine the dataset format. Please load it manually in the GUI");
return false;
}

void MainForm::CheckForUpdates()
Expand Down
2 changes: 1 addition & 1 deletion apps/vaporgui/MainForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class MainForm : public QMainWindow {
Q_OBJECT

public:
MainForm(vector<QString> files, QApplication *app, bool interactive = true, QWidget *parent = 0);
MainForm(vector<QString> files, QApplication *app, bool interactive = true, string filesType = "auto", QWidget *parent = 0);
~MainForm();

int RenderAndExit(int start, int end, const std::string &baseFile, int width, int height);
Expand Down
5 changes: 4 additions & 1 deletion apps/vaporgui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ struct opt_t {
OptionParser::IntRange_ renderRange;
OptionParser::Dimension2D_ resolution;
char * outputPath;
char * datasetType;
} opt;
OptionParser::OptDescRec_T set_opts[] = {{"help", 0, "", "Print this message and exit"},
{"render", 0, "", "Render a given session file and exit"},
{"timesteps", 1, "0:0", "Timesteps to render when using -render. Defaults to all timesteps."},
{"resolution", 1, "1920x1080", "Output resolution when using -render"},
{"output", 1, "vapor.tiff", "Output image file when using -render. This will be suffixed with the timestep number. Supports tiff, jpg"},
{"ftype", 1, "auto", "Specify file format of datasets passed in command line. Valid options are: auto vdc wrf mpas dcp ugrid cf bov"},
{NULL}};
OptionParser::Option_T get_options[] = {{"help", Wasp::CvtToBoolean, &opt.help, sizeof(opt.help)},
{"render", Wasp::CvtToBoolean, &opt.render, sizeof(opt.render)},
{"timesteps", Wasp::CvtToIntRange, &opt.renderRange, sizeof(opt.renderRange)},
{"resolution", Wasp::CvtToDimension2D, &opt.resolution, sizeof(opt.resolution)},
{"output", Wasp::CvtToString, &opt.outputPath, sizeof(opt.outputPath)},
{"ftype", Wasp::CvtToString, &opt.datasetType, sizeof(opt.datasetType)},
{NULL}};
const char * ProgName;

Expand Down Expand Up @@ -175,7 +178,7 @@ int main(int argc, char **argv)

vector<QString> files;
for (int i = 1; i < argc; i++) { files.push_back(argv[i]); }
MainForm *mw = new MainForm(files, app, !opt.render);
MainForm *mw = new MainForm(files, app, !opt.render, opt.datasetType);

// StartupParams* sParams = new StartupParams(0);

Expand Down
17 changes: 17 additions & 0 deletions lib/vdc/BOVCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ const std::string BOVCollection::_intFormatString = "INT";
const std::string BOVCollection::_floatFormatString = "FLOAT";
const std::string BOVCollection::_doubleFormatString = "DOUBLE";

namespace {
size_t getFileSize(std::string filename) // path to file
{
FILE *p_file = NULL;
p_file = fopen(filename.c_str(), "rb");
fseek(p_file, 0, SEEK_END);
size_t size = ftell(p_file);
fclose(p_file);
return size;
}
} // namespace

BOVCollection::BOVCollection()
: _time(_defaultTime), _dataFile(_defaultFile), _dataFormat(_defaultFormat), _variable(_defaultVar), _byteOffset(_defaultByteOffset), _divideBrick(_defaultDivBrick), _dataEndian(_defaultEndian),
_centering(_defaultCentering), _dataComponents(_defaultComponents), _tmpDataFormat(_defaultFormat), _tmpByteOffset(_defaultByteOffset), _gridSizeAssigned(false), _formatAssigned(false),
Expand Down Expand Up @@ -103,6 +115,11 @@ int BOVCollection::Initialize(const std::vector<std::string> &paths)

header.open(paths[i]);
if (header.is_open()) {
if (getFileSize(paths[i]) > 1000000) {
SetErrMsg(("BOV header file larger than 1MB. This text file shouldn't need to be larger than a few KB." + paths[0]).c_str());
return -1;
}

rc = _parseHeader(header);
if (rc < 0) {
SetErrMsg(("Error parsing BOV file " + paths[0]).c_str());
Expand Down

0 comments on commit dd7ac4e

Please sign in to comment.