Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3785,7 +3785,7 @@ namespace libforefire
}
#endif /* FF_NO_NETCDF */
}
void Command::writeASCII(const char *filename, const std::vector<std::vector<double>> &matrix, double SWX, double SWY, double NEX, double /*NEY*/)
void Command::writeASCII(const char *filename, const std::vector<std::vector<double>> &matrix, double SWX, double SWY, double NEX, double NEY)
{
std::ofstream out(filename);
if (!out)
Expand All @@ -3799,9 +3799,19 @@ namespace libforefire
size_t ncols = matrix.size();
size_t nrows = (ncols > 0) ? matrix[0].size() : 0;

// Compute the cell size.
// Here we assume that (NEX - SWX) corresponds to the total width of the matrix.
double cellsize = (ncols > 0) ? (NEX - SWX) / static_cast<double>(ncols) : 0.0;
// An ASCII grid carries a single cellsize, but the matrix can hold
// cells that are not square (plot[...;size=...]). Writing the x value
// alone would place every row at the wrong spacing without a word.
double dx = (ncols > 0) ? (NEX - SWX) / static_cast<double>(ncols) : 0.0;
double dy = (nrows > 0) ? (NEY - SWY) / static_cast<double>(nrows) : 0.0;
if ((ncols > 0) && (nrows > 0) &&
(std::abs(dx - dy) > 1e-6 * std::max(std::abs(dx), std::abs(dy))))
{
std::cout << "WARNING: writeASCII: the cells are not square, the file takes cellsize "
<< dx << " from the x extent, while the y extent gives " << dy
<< " for " << nrows << " rows" << std::endl;
}
double cellsize = dx;

// Write the ASCII header.
out << "ncols " << ncols << "\n";
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -179,6 +181,29 @@ TEST_CASE("plot writes arrival times as an ASCII grid, north row first") {
CHECK(inexact == 0);
}

TEST_CASE("plot warns when the ASCII cells are not square") {
// An ASCII grid carries one cellsize, but plot[...;size=] can ask for cells
// that are not square. writeASCII used to compute it from the x extent
// alone, so the y spacing the file declares was wrong with no sign of it.
Command executor;
burnOffCentre(executor);
const std::string path = "ff_test_anisotropic.asc";

std::ostringstream captured;
std::streambuf* previous = std::cout.rdbuf(captured.rdbuf());
run(executor, "plot[parameter=arrival_time;filename=" + path + ";size=20,20]");
std::cout.rdbuf(previous);

AsciiGrid grid = readAsciiGrid(path);
std::remove(path.c_str());

/* 2000 x 1000 m in 20 x 20 cells: 100 m in x, 50 m in y. */
CHECK(grid.ncols == 20);
CHECK(grid.nrows == 20);
CHECK(grid.cellsize == doctest::Approx(100.));
CHECK(captured.str().find("not square") != std::string::npos);
}

TEST_CASE("a domain built without a propagation layer spreads with the propagationModel parameter") {
// loadData picks the model from the propagationModel parameter, but a
// bare FireDomain[...] had no propagation layer and goTo segfaulted.
Expand Down
Loading