Skip to content

Commit 72b5d48

Browse files
committed
[doc] Document data generator
1 parent d726489 commit 72b5d48

File tree

1 file changed

+122
-38
lines changed

1 file changed

+122
-38
lines changed

Cell2Fire/DataGenerator.cpp

Lines changed: 122 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// author = "Matias Vilches"
2+
/**
3+
*@file
4+
*/
25

36
#include "DataGenerator.h"
47

@@ -29,6 +32,11 @@ separator()
2932

3033
// Reads fbp_lookup_table.csv and creates dictionaries for the fuel types and
3134
// cells' ColorsDict
35+
/**
36+
*
37+
* @param filename Name of file containing the lookup table for the chosen simulation model
38+
* @return
39+
*/
3240
std::tuple<std::unordered_map<std::string, std::string>,
3341
std::unordered_map<std::string, std::tuple<float, float, float, float>>>
3442
Dictionary(const std::string& filename)
@@ -95,7 +103,10 @@ Dictionary(const std::string& filename)
95103
}
96104

97105
ColorsDict[tokens[0]] = std::make_tuple(
98-
std::stof(tokens[4]) / 255.0f, std::stof(tokens[5]) / 255.0f, std::stof(tokens[6]) / 255.0f, 1.0f);
106+
std::stof(tokens[4]) / 255.0f,
107+
std::stof(tokens[5]) / 255.0f,
108+
std::stof(tokens[6]) / 255.0f,
109+
1.0f);
99110
}
100111

101112
if (aux == 1)
@@ -159,8 +170,19 @@ ForestGrid(const std::string& filename, const std::unordered_map<std::string, st
159170
line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());
160171

161172
// Remove leading and trailing whitespaces
162-
line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](char c) { return !std::isspace(c); }));
163-
line.erase(std::find_if(line.rbegin(), line.rend(), [](char c) { return !std::isspace(c); }).base(),
173+
line.erase(line.begin(),
174+
std::find_if(line.begin(),
175+
line.end(),
176+
[](char c)
177+
{
178+
return !std::isspace(c);
179+
}));
180+
line.erase(std::find_if(line.rbegin(),
181+
line.rend(),
182+
[](char c)
183+
{
184+
return !std::isspace(c);
185+
}).base(),
164186
line.end());
165187

166188
std::istringstream iss(line);
@@ -206,7 +228,15 @@ fileExists(const std::string& filename)
206228
return file.good();
207229
}
208230

209-
// Function to read grid data from ASCII file
231+
/**
232+
* @brief Read grid data from ASCII file into a vector
233+
*
234+
* Transforms a raster layer into an array of information.
235+
*
236+
* @param filename name of file to read
237+
* @param data vector where information will be stored
238+
* @param nCells number of cells
239+
*/
210240
void
211241
DataGrids(const std::string& filename, std::vector<float>& data, int nCells)
212242
{
@@ -256,7 +286,7 @@ DataGrids(const std::string& filename, std::vector<float>& data, int nCells)
256286
data[aux++] = std::stof(token);
257287
if (aux == nCells)
258288
{
259-
return; // Stop reading if we've filled the data vector
289+
return; // Stop reading if we've filled the data vector
260290
}
261291
}
262292
}
@@ -403,8 +433,16 @@ ForestGridTif(const std::string& filename, const std::unordered_map<std::string,
403433
return std::make_tuple(gridcell3, gridcell4, grid.size(), tcols - 1, cellSizeX);
404434
}
405435

406-
// Function to read grid data from ASCII file
407-
436+
/**
437+
* @brief This functionality is not currently available
438+
*
439+
* Read grid data from Tif file into a vector.
440+
* Transforms a raster layer into an array of information.
441+
*
442+
* @param filename name of file to read
443+
* @param data vector where information will be stored
444+
* @param nCells number of cells
445+
*/
408446
void
409447
DataGridsTif(const std::string& filename, std::vector<float>& data, int nCells)
410448
{
@@ -508,12 +546,30 @@ DataGridsTif(const std::string& filename, std::vector<float>& data, int nCells)
508546
aux++;
509547
if (aux == nCells)
510548
{
511-
return; // Stop reading if we've filled the data vector
549+
return; // Stop reading if we've filled the data vector
512550
}
513551
}
514552
}
515553
}
516554

555+
/**
556+
* @brief Create input data matrix
557+
*
558+
* @param GFuelType Array of fuel model codes
559+
* @param GFuelTypeN Array of fuel model ids
560+
* @param Elevation Array of terrain elevations
561+
* @param PS Array of slope
562+
* @param SAZ Array of slope azimuth
563+
* @param Curing Array of curing levels
564+
* @param CBD Array of canopy bulk densities
565+
* @param CBH Array of canopy base heights
566+
* @param CCF Array of canopy cover fractions
567+
* @param PY Array of ignition probabilities
568+
* @param FMC Array of foliage moisture content
569+
* @param TreeHeight Array of tree heights
570+
* @param InFolder Input data directory
571+
* @return An array of arrays representing a cell.
572+
*/
517573
std::vector<std::vector<std::unique_ptr<std::string>>>
518574
GenerateDat(const std::vector<std::string>& GFuelType,
519575
const std::vector<int>& GFuelTypeN,
@@ -531,9 +587,9 @@ GenerateDat(const std::vector<std::string>& GFuelType,
531587
{
532588
// DF columns
533589
std::vector<std::string> Columns
534-
= { "fueltype", "lat", "lon", "elev", "ws", "waz", "ps", "saz", "cur",
535-
"cbd", "cbh", "ccf", "ftypeN", "fmc", "py", "jd", "jd_min", "pc",
536-
"pdf", "time", "ffmc", "bui", "gfl", "pattern", "tree_height" };
590+
= { "fueltype", "lat", "lon", "elev", "ws", "waz", "ps", "saz", "cur",
591+
"cbd", "cbh", "ccf", "ftypeN", "fmc", "py", "jd", "jd_min", "pc",
592+
"pdf", "time", "ffmc", "bui", "gfl", "pattern", "tree_height" };
537593

538594
// GFL dictionary (FBP)
539595
std::unordered_map<std::string, float> GFLD = { { "C1", 0.75f },
@@ -577,29 +633,29 @@ GenerateDat(const std::vector<std::string>& GFuelType,
577633

578634
// PDF dictionary (CANADA)
579635
std::unordered_map<std::string, int> PDFD
580-
= { { "M3_5", 5 }, { "M3_10", 10 }, { "M3_15", 15 }, { "M3_20", 20 }, { "M3_25", 25 },
581-
{ "M3_30", 30 }, { "M3_35", 35 }, { "M3_40", 40 }, { "M3_45", 45 }, { "M3_50", 50 },
582-
{ "M3_55", 55 }, { "M3_60", 60 }, { "M3_65", 65 }, { "M3_70", 70 }, { "M3_75", 75 },
583-
{ "M3_80", 80 }, { "M3_85", 85 }, { "M3_90", 90 }, { "M3_95", 95 }, { "M4_5", 5 },
584-
{ "M4_10", 10 }, { "M4_15", 15 }, { "M4_20", 20 }, { "M4_25", 25 }, { "M4_30", 30 },
585-
{ "M4_35", 35 }, { "M4_40", 40 }, { "M4_45", 45 }, { "M4_50", 50 }, { "M4_55", 55 },
586-
{ "M4_60", 60 }, { "M4_65", 65 }, { "M4_70", 70 }, { "M4_75", 75 }, { "M4_80", 80 },
587-
{ "M4_85", 85 }, { "M4_90", 90 }, { "M4_95", 95 }, { "M3M4_5", 5 }, { "M3M4_10", 10 },
636+
= { { "M3_5", 5 }, { "M3_10", 10 }, { "M3_15", 15 }, { "M3_20", 20 }, { "M3_25", 25 },
637+
{ "M3_30", 30 }, { "M3_35", 35 }, { "M3_40", 40 }, { "M3_45", 45 }, { "M3_50", 50 },
638+
{ "M3_55", 55 }, { "M3_60", 60 }, { "M3_65", 65 }, { "M3_70", 70 }, { "M3_75", 75 },
639+
{ "M3_80", 80 }, { "M3_85", 85 }, { "M3_90", 90 }, { "M3_95", 95 }, { "M4_5", 5 },
640+
{ "M4_10", 10 }, { "M4_15", 15 }, { "M4_20", 20 }, { "M4_25", 25 }, { "M4_30", 30 },
641+
{ "M4_35", 35 }, { "M4_40", 40 }, { "M4_45", 45 }, { "M4_50", 50 }, { "M4_55", 55 },
642+
{ "M4_60", 60 }, { "M4_65", 65 }, { "M4_70", 70 }, { "M4_75", 75 }, { "M4_80", 80 },
643+
{ "M4_85", 85 }, { "M4_90", 90 }, { "M4_95", 95 }, { "M3M4_5", 5 }, { "M3M4_10", 10 },
588644
{ "M3M4_15", 15 }, { "M3M4_20", 20 }, { "M3M4_25", 25 }, { "M3M4_30", 30 }, { "M3M4_35", 35 },
589645
{ "M3M4_40", 40 }, { "M3M4_45", 45 }, { "M3M4_50", 50 }, { "M3M4_55", 55 }, { "M3M4_60", 60 },
590646
{ "M3M4_65", 65 }, { "M3M4_70", 70 }, { "M3M4_75", 75 }, { "M3M4_80", 80 }, { "M3M4_85", 85 },
591647
{ "M3M4_90", 90 }, { "M3M4_95", 95 } };
592648

593649
// PCD dictionary (CANADA)
594650
std::unordered_map<std::string, int> PCD
595-
= { { "M3_5", 5 }, { "M3_10", 10 }, { "M3_15", 15 }, { "M3_20", 20 }, { "M3_25", 25 },
596-
{ "M3_30", 30 }, { "M3_35", 35 }, { "M3_40", 40 }, { "M3_45", 45 }, { "M3_50", 50 },
597-
{ "M3_55", 55 }, { "M3_60", 60 }, { "M3_65", 65 }, { "M3_70", 70 }, { "M3_75", 75 },
598-
{ "M3_80", 80 }, { "M3_85", 85 }, { "M3_90", 90 }, { "M3_95", 95 }, { "M4_5", 5 },
599-
{ "M4_10", 10 }, { "M4_15", 15 }, { "M4_20", 20 }, { "M4_25", 25 }, { "M4_30", 30 },
600-
{ "M4_35", 35 }, { "M4_40", 40 }, { "M4_45", 45 }, { "M4_50", 50 }, { "M4_55", 55 },
601-
{ "M4_60", 60 }, { "M4_65", 65 }, { "M4_70", 70 }, { "M4_75", 75 }, { "M4_80", 80 },
602-
{ "M4_85", 85 }, { "M4_90", 90 }, { "M4_95", 95 }, { "M3M4_5", 5 }, { "M3M4_10", 10 },
651+
= { { "M3_5", 5 }, { "M3_10", 10 }, { "M3_15", 15 }, { "M3_20", 20 }, { "M3_25", 25 },
652+
{ "M3_30", 30 }, { "M3_35", 35 }, { "M3_40", 40 }, { "M3_45", 45 }, { "M3_50", 50 },
653+
{ "M3_55", 55 }, { "M3_60", 60 }, { "M3_65", 65 }, { "M3_70", 70 }, { "M3_75", 75 },
654+
{ "M3_80", 80 }, { "M3_85", 85 }, { "M3_90", 90 }, { "M3_95", 95 }, { "M4_5", 5 },
655+
{ "M4_10", 10 }, { "M4_15", 15 }, { "M4_20", 20 }, { "M4_25", 25 }, { "M4_30", 30 },
656+
{ "M4_35", 35 }, { "M4_40", 40 }, { "M4_45", 45 }, { "M4_50", 50 }, { "M4_55", 55 },
657+
{ "M4_60", 60 }, { "M4_65", 65 }, { "M4_70", 70 }, { "M4_75", 75 }, { "M4_80", 80 },
658+
{ "M4_85", 85 }, { "M4_90", 90 }, { "M4_95", 95 }, { "M3M4_5", 5 }, { "M3M4_10", 10 },
603659
{ "M3M4_15", 15 }, { "M3M4_20", 20 }, { "M3M4_25", 25 }, { "M3M4_30", 30 }, { "M3M4_35", 35 },
604660
{ "M3M4_40", 40 }, { "M3M4_45", 45 }, { "M3M4_50", 50 }, { "M3M4_55", 55 }, { "M3M4_60", 60 },
605661
{ "M3M4_65", 65 }, { "M3M4_70", 70 }, { "M3M4_75", 75 }, { "M3M4_80", 80 }, { "M3M4_85", 85 },
@@ -665,7 +721,7 @@ GenerateDat(const std::vector<std::string>& GFuelType,
665721
// Handle special cases 8
666722
if (std::isnan(Curing[i]) && (GFuelType[i] == "O1a" || GFuelType[i] == "O1b"))
667723
{
668-
rowData.emplace_back(std::make_unique<std::string>("60")); // "cur"
724+
rowData.emplace_back(std::make_unique<std::string>("60")); // "cur"
669725
}
670726
else
671727
{
@@ -740,7 +796,7 @@ GenerateDat(const std::vector<std::string>& GFuelType,
740796
// Populate PC 17
741797
if (PCD.find(GFuelType[i]) != PCD.end())
742798
{
743-
rowData.emplace_back(std::make_unique<std::string>(std::to_string(PCD[GFuelType[i]]))); // "pc"
799+
rowData.emplace_back(std::make_unique<std::string>(std::to_string(PCD[GFuelType[i]]))); // "pc"
744800
}
745801
else
746802
{
@@ -751,7 +807,7 @@ GenerateDat(const std::vector<std::string>& GFuelType,
751807
// Populate PDF 18
752808
if (PDFD.find(GFuelType[i]) != PDFD.end())
753809
{
754-
rowData.emplace_back(std::make_unique<std::string>(std::to_string(PDFD[GFuelType[i]]))); // "pdf"
810+
rowData.emplace_back(std::make_unique<std::string>(std::to_string(PDFD[GFuelType[i]]))); // "pdf"
755811
}
756812
else
757813
{
@@ -768,7 +824,7 @@ GenerateDat(const std::vector<std::string>& GFuelType,
768824
// GFL 22
769825
if (GFLD.find(GFuelType[i]) != GFLD.end())
770826
{
771-
rowData.emplace_back(std::make_unique<std::string>(std::to_string(GFLD[GFuelType[i]]))); // "gfl"
827+
rowData.emplace_back(std::make_unique<std::string>(std::to_string(GFLD[GFuelType[i]]))); // "gfl"
772828
}
773829
else
774830
{
@@ -797,16 +853,20 @@ GenerateDat(const std::vector<std::string>& GFuelType,
797853
return dataGrids;
798854
}
799855

800-
// Function to write data to a CSV file
856+
/**
857+
* @brief Save data matrix into a CSV file called `Data.csv`.
858+
* @param dataGrids Array of arrays storing each cell's input data
859+
* @param InFolder Directory where the CSV file will be created.
860+
*/
801861
void
802862
writeDataToFile(const std::vector<std::vector<std::unique_ptr<std::string>>>& dataGrids, const std::string& InFolder)
803863
{
804864

805865
std::ofstream dataFile(InFolder + separator() + "Data.csv");
806866
std::vector<std::string> Columns
807-
= { "fueltype", "lat", "lon", "elev", "ws", "waz", "ps", "saz", "cur",
808-
"cbd", "cbh", "ccf", "ftypeN", "fmc", "py", "jd", "jd_min", "pc",
809-
"pdf", "time", "ffmc", "bui", "gfl", "pattern", "tree_height" };
867+
= { "fueltype", "lat", "lon", "elev", "ws", "waz", "ps", "saz", "cur",
868+
"cbd", "cbh", "ccf", "ftypeN", "fmc", "py", "jd", "jd_min", "pc",
869+
"pdf", "time", "ffmc", "bui", "gfl", "pattern", "tree_height" };
810870
if (dataFile.is_open())
811871
{
812872
// Write header
@@ -821,7 +881,7 @@ writeDataToFile(const std::vector<std::vector<std::unique_ptr<std::string>>>& da
821881
{
822882
for (const auto& item : rowData)
823883
{
824-
dataFile << *item << ","; // Dereference the unique_ptr before writing
884+
dataFile << *item << ","; // Dereference the unique_ptr before writing
825885
}
826886
dataFile << "\n";
827887
}
@@ -835,7 +895,31 @@ writeDataToFile(const std::vector<std::vector<std::unique_ptr<std::string>>>& da
835895
}
836896
}
837897

838-
// Main function
898+
/**
899+
* @brief Reads all available input raster files and generates a new file called 'Data.csv' in which each row contains
900+
* the input data for a cell.
901+
*
902+
* If the 'fuels' raster file is in `tif` format, then it assumes all other rasters are also in that format.
903+
* Otherwise, it assumes `ascii` format.
904+
*
905+
* The function looks for the following files in `InFolder`:
906+
* - elevation: terrain elevation [m]
907+
* - saz: slope azimuth
908+
* - slope: terrain slope
909+
* - cur: curing level
910+
* - cbd: canopy bulk density [km/m²]
911+
* - cbh: canopy base height [m]
912+
* - ccf: canopy cover fraction
913+
* - py: ignition probability map [%]
914+
* - fmc: foliage moisture content
915+
* - hm: tree height [m]
916+
*
917+
* The generated file contains the following columns: fuel type, latitude, longitude, elevation, wind speed (always blank),
918+
* wind direction (always blank), slope, slope azimuth, curing level, canopy bulk density, canopy base height, canopy
919+
* cover fraction, fuel type number ,foliage moisture content, ignition probability.
920+
* @param InFolder Input data directory
921+
* @param Simulator Simulation model code
922+
*/
839923
void
840924
GenDataFile(const std::string& InFolder, const std::string& Simulator)
841925
{
@@ -953,7 +1037,7 @@ GenDataFile(const std::string& InFolder, const std::string& Simulator)
9531037

9541038
std::vector<std::string> filenames
9551039
= { "elevation" + extension, "saz" + extension, "slope" + extension, "cur" + extension, "cbd" + extension,
956-
"cbh" + extension, "ccf" + extension, "py" + extension, "fmc" + extension, "hm" + extension };
1040+
"cbh" + extension, "ccf" + extension, "py" + extension, "fmc" + extension, "hm" + extension };
9571041

9581042
for (const auto& name : filenames)
9591043
{

0 commit comments

Comments
 (0)