1
1
// author = "Matias Vilches"
2
+ /* *
3
+ *@file
4
+ */
2
5
3
6
#include " DataGenerator.h"
4
7
@@ -29,6 +32,11 @@ separator()
29
32
30
33
// Reads fbp_lookup_table.csv and creates dictionaries for the fuel types and
31
34
// cells' ColorsDict
35
+ /* *
36
+ *
37
+ * @param filename Name of file containing the lookup table for the chosen simulation model
38
+ * @return
39
+ */
32
40
std::tuple<std::unordered_map<std::string, std::string>,
33
41
std::unordered_map<std::string, std::tuple<float , float , float , float >>>
34
42
Dictionary (const std::string& filename)
@@ -95,7 +103,10 @@ Dictionary(const std::string& filename)
95
103
}
96
104
97
105
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 );
99
110
}
100
111
101
112
if (aux == 1 )
@@ -159,8 +170,19 @@ ForestGrid(const std::string& filename, const std::unordered_map<std::string, st
159
170
line.erase (std::remove (line.begin (), line.end (), ' \n ' ), line.end ());
160
171
161
172
// 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 (),
164
186
line.end ());
165
187
166
188
std::istringstream iss (line);
@@ -206,7 +228,15 @@ fileExists(const std::string& filename)
206
228
return file.good ();
207
229
}
208
230
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
+ */
210
240
void
211
241
DataGrids (const std::string& filename, std::vector<float >& data, int nCells)
212
242
{
@@ -256,7 +286,7 @@ DataGrids(const std::string& filename, std::vector<float>& data, int nCells)
256
286
data[aux++] = std::stof (token);
257
287
if (aux == nCells)
258
288
{
259
- return ; // Stop reading if we've filled the data vector
289
+ return ; // Stop reading if we've filled the data vector
260
290
}
261
291
}
262
292
}
@@ -403,8 +433,16 @@ ForestGridTif(const std::string& filename, const std::unordered_map<std::string,
403
433
return std::make_tuple (gridcell3, gridcell4, grid.size (), tcols - 1 , cellSizeX);
404
434
}
405
435
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
+ */
408
446
void
409
447
DataGridsTif (const std::string& filename, std::vector<float >& data, int nCells)
410
448
{
@@ -508,12 +546,30 @@ DataGridsTif(const std::string& filename, std::vector<float>& data, int nCells)
508
546
aux++;
509
547
if (aux == nCells)
510
548
{
511
- return ; // Stop reading if we've filled the data vector
549
+ return ; // Stop reading if we've filled the data vector
512
550
}
513
551
}
514
552
}
515
553
}
516
554
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
+ */
517
573
std::vector<std::vector<std::unique_ptr<std::string>>>
518
574
GenerateDat (const std::vector<std::string>& GFuelType,
519
575
const std::vector<int >& GFuelTypeN,
@@ -531,9 +587,9 @@ GenerateDat(const std::vector<std::string>& GFuelType,
531
587
{
532
588
// DF columns
533
589
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" };
537
593
538
594
// GFL dictionary (FBP)
539
595
std::unordered_map<std::string, float > GFLD = { { " C1" , 0 .75f },
@@ -577,29 +633,29 @@ GenerateDat(const std::vector<std::string>& GFuelType,
577
633
578
634
// PDF dictionary (CANADA)
579
635
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 },
588
644
{ " M3M4_15" , 15 }, { " M3M4_20" , 20 }, { " M3M4_25" , 25 }, { " M3M4_30" , 30 }, { " M3M4_35" , 35 },
589
645
{ " M3M4_40" , 40 }, { " M3M4_45" , 45 }, { " M3M4_50" , 50 }, { " M3M4_55" , 55 }, { " M3M4_60" , 60 },
590
646
{ " M3M4_65" , 65 }, { " M3M4_70" , 70 }, { " M3M4_75" , 75 }, { " M3M4_80" , 80 }, { " M3M4_85" , 85 },
591
647
{ " M3M4_90" , 90 }, { " M3M4_95" , 95 } };
592
648
593
649
// PCD dictionary (CANADA)
594
650
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 },
603
659
{ " M3M4_15" , 15 }, { " M3M4_20" , 20 }, { " M3M4_25" , 25 }, { " M3M4_30" , 30 }, { " M3M4_35" , 35 },
604
660
{ " M3M4_40" , 40 }, { " M3M4_45" , 45 }, { " M3M4_50" , 50 }, { " M3M4_55" , 55 }, { " M3M4_60" , 60 },
605
661
{ " 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,
665
721
// Handle special cases 8
666
722
if (std::isnan (Curing[i]) && (GFuelType[i] == " O1a" || GFuelType[i] == " O1b" ))
667
723
{
668
- rowData.emplace_back (std::make_unique<std::string>(" 60" )); // "cur"
724
+ rowData.emplace_back (std::make_unique<std::string>(" 60" )); // "cur"
669
725
}
670
726
else
671
727
{
@@ -740,7 +796,7 @@ GenerateDat(const std::vector<std::string>& GFuelType,
740
796
// Populate PC 17
741
797
if (PCD.find (GFuelType[i]) != PCD.end ())
742
798
{
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"
744
800
}
745
801
else
746
802
{
@@ -751,7 +807,7 @@ GenerateDat(const std::vector<std::string>& GFuelType,
751
807
// Populate PDF 18
752
808
if (PDFD.find (GFuelType[i]) != PDFD.end ())
753
809
{
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"
755
811
}
756
812
else
757
813
{
@@ -768,7 +824,7 @@ GenerateDat(const std::vector<std::string>& GFuelType,
768
824
// GFL 22
769
825
if (GFLD.find (GFuelType[i]) != GFLD.end ())
770
826
{
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"
772
828
}
773
829
else
774
830
{
@@ -797,16 +853,20 @@ GenerateDat(const std::vector<std::string>& GFuelType,
797
853
return dataGrids;
798
854
}
799
855
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
+ */
801
861
void
802
862
writeDataToFile (const std::vector<std::vector<std::unique_ptr<std::string>>>& dataGrids, const std::string& InFolder)
803
863
{
804
864
805
865
std::ofstream dataFile (InFolder + separator () + " Data.csv" );
806
866
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" };
810
870
if (dataFile.is_open ())
811
871
{
812
872
// Write header
@@ -821,7 +881,7 @@ writeDataToFile(const std::vector<std::vector<std::unique_ptr<std::string>>>& da
821
881
{
822
882
for (const auto & item : rowData)
823
883
{
824
- dataFile << *item << " ," ; // Dereference the unique_ptr before writing
884
+ dataFile << *item << " ," ; // Dereference the unique_ptr before writing
825
885
}
826
886
dataFile << " \n " ;
827
887
}
@@ -835,7 +895,31 @@ writeDataToFile(const std::vector<std::vector<std::unique_ptr<std::string>>>& da
835
895
}
836
896
}
837
897
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
+ */
839
923
void
840
924
GenDataFile (const std::string& InFolder, const std::string& Simulator)
841
925
{
@@ -953,7 +1037,7 @@ GenDataFile(const std::string& InFolder, const std::string& Simulator)
953
1037
954
1038
std::vector<std::string> filenames
955
1039
= { " 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 };
957
1041
958
1042
for (const auto & name : filenames)
959
1043
{
0 commit comments