diff --git a/src/Command.cpp b/src/Command.cpp index 7774e53..f6783ed 100644 --- a/src/Command.cpp +++ b/src/Command.cpp @@ -3785,7 +3785,7 @@ namespace libforefire } #endif /* FF_NO_NETCDF */ } - void Command::writeASCII(const char *filename, const std::vector> &matrix, double SWX, double SWY, double NEX, double /*NEY*/) + void Command::writeASCII(const char *filename, const std::vector> &matrix, double SWX, double SWY, double NEX, double NEY) { std::ofstream out(filename); if (!out) @@ -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(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(ncols) : 0.0; + double dy = (nrows > 0) ? (NEY - SWY) / static_cast(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"; diff --git a/tests/unit/test_commands.cpp b/tests/unit/test_commands.cpp index 86c11b7..54c744b 100644 --- a/tests/unit/test_commands.cpp +++ b/tests/unit/test_commands.cpp @@ -28,7 +28,9 @@ #include #include #include +#include #include +#include #include #include @@ -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.