From 046a46c068ecb710c2f17c9b81710538395603c3 Mon Sep 17 00:00:00 2001 From: matilde Date: Fri, 21 Nov 2025 00:09:36 -0300 Subject: [PATCH 1/4] Add function to parse weather weights --- Cell2Fire/Cell2Fire.cpp | 13 +++++++++++++ Cell2Fire/ReadArgs.cpp | 14 ++++++++++++++ Cell2Fire/ReadArgs.h | 4 ++-- Cell2Fire/ReadCSV.cpp | 8 ++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cell2Fire/Cell2Fire.cpp b/Cell2Fire/Cell2Fire.cpp index e5391438..c6faa908 100644 --- a/Cell2Fire/Cell2Fire.cpp +++ b/Cell2Fire/Cell2Fire.cpp @@ -48,6 +48,7 @@ inputs* df; int currentSim = 0; std::unordered_map> BBOFactors; std::unordered_map> HarvestedCells; +std::unordered_map WeatherWeights; std::vector NFTypesCells; std::unordered_map IgnitionHistory; std::unordered_map WeatherHistory; @@ -218,6 +219,7 @@ Cell2Fire::Cell2Fire(arguments _args) : CSVForest(_args.InFolder + "fuels", " ") // DEBUG std::cout << "\n------------------Forest Data ----------------------\n" << std::endl; std::vector> FDF = this->CSVForest.getData(_args.InFolder + "fuels"); + // DEBUGthis->CSVForest.printData(FDF); this->CSVForest.parseForestDF(&frdf, FDF); @@ -327,6 +329,17 @@ Cell2Fire::Cell2Fire(arguments _args) : CSVForest(_args.InFolder + "fuels", " ") } } + if (this->args.UseWeatherWeights) + { + std::string sep = ","; + CSVReader CSVWeatherWeigths(this->args.WeatherWeightsFile, sep); + + std::vector> WeatherWeightsDF = CSVWeatherWeigths.getData(this->args.WeatherWeightsFile); + + CSVWeatherWeights.parseWeatherWeights(WeatherWeights, WeatherWeightDF, this->args.NWeatherFiles); + + } + // Relevant sets: Initialization this->availCells.clear(); this->nonBurnableCells.clear(); diff --git a/Cell2Fire/ReadArgs.cpp b/Cell2Fire/ReadArgs.cpp index 3d641918..5138400d 100644 --- a/Cell2Fire/ReadArgs.cpp +++ b/Cell2Fire/ReadArgs.cpp @@ -87,6 +87,20 @@ parseArgs(int argc, char* argv[], arguments* args_ptr) else input_hplan = ∅ + //--weather-weights + char* weather_weights_file = getCmdOption(argv, argv + argc, "--weather-weights"); + if (weather_weights_file) + { + printf("WeatherWeightsFile: %s \n", weather_weights_file); + args_ptr->WeatherWeightsFile = weather_weights_file; + args_ptr->UseWeatherWeights = true; + } + else { + args_ptr->WeatherWeightsFile = ∅ + args_ptr->UseWeatherWeights = false; + } + + // Booleans bool out_messages = false; bool out_trajectories = false; diff --git a/Cell2Fire/ReadArgs.h b/Cell2Fire/ReadArgs.h index aa92b410..00a824aa 100644 --- a/Cell2Fire/ReadArgs.h +++ b/Cell2Fire/ReadArgs.h @@ -14,9 +14,9 @@ */ typedef struct { - std::string InFolder, OutFolder, WeatherOpt, HarvestPlan, Simulator; + std::string InFolder, OutFolder, WeatherOpt, HarvestPlan, Simulator, WeatherWeightsFile; bool OutMessages, OutFl, OutIntensity, OutRos, OutCrown, OutCrownConsumption, OutSurfConsumption, Trajectories, - NoOutput, verbose, IgnitionsLog, Ignitions, OutputGrids, FinalGrid, PromTuned, Stats, BBOTuning, AllowCROS; + NoOutput, verbose, IgnitionsLog, Ignitions, OutputGrids, FinalGrid, PromTuned, Stats, BBOTuning, AllowCROS, UseWeatherWeights; float ROSCV, ROSThreshold, CROSThreshold, HFIThreshold, HFactor, FFactor, BFactor, EFactor, FirePeriodLen; float CBDFactor, CCFFactor, ROS10Factor, CROSActThreshold; int MinutesPerWP, MaxFirePeriods, TotalYears, TotalSims, NWeatherFiles, IgnitionRadius, seed, nthreads, FMC, diff --git a/Cell2Fire/ReadCSV.cpp b/Cell2Fire/ReadCSV.cpp index 8ede4cdf..06d981fe 100644 --- a/Cell2Fire/ReadCSV.cpp +++ b/Cell2Fire/ReadCSV.cpp @@ -584,6 +584,14 @@ CSVReader::parseIgnitionDF(std::vector& ig, std::vector& wf, std::vector>& DF, int NumberOfWeatherFiles){ + +} + /* * Populate HarvestedDF */ From 11bb3066390d9944f2d58a78c1328f396ce183d2 Mon Sep 17 00:00:00 2001 From: Mati Date: Fri, 28 Nov 2025 01:01:44 -0300 Subject: [PATCH 2/4] Chooses random number using weighted distribution --- Cell2Fire/Cell2Fire.cpp | 32 +++++++++++++++++++++++------ Cell2Fire/ReadCSV.cpp | 45 ++++++++++++++++++----------------------- Cell2Fire/ReadCSV.h | 2 ++ 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/Cell2Fire/Cell2Fire.cpp b/Cell2Fire/Cell2Fire.cpp index c6faa908..791474cc 100644 --- a/Cell2Fire/Cell2Fire.cpp +++ b/Cell2Fire/Cell2Fire.cpp @@ -48,7 +48,8 @@ inputs* df; int currentSim = 0; std::unordered_map> BBOFactors; std::unordered_map> HarvestedCells; -std::unordered_map WeatherWeights; +std::vector WeatherWeights; +std::vector WeatherWeightIDs; std::vector NFTypesCells; std::unordered_map IgnitionHistory; std::unordered_map WeatherHistory; @@ -329,16 +330,19 @@ Cell2Fire::Cell2Fire(arguments _args) : CSVForest(_args.InFolder + "fuels", " ") } } + /* if (this->args.UseWeatherWeights) { + cout << "use weather weights" << endl; std::string sep = ","; - CSVReader CSVWeatherWeigths(this->args.WeatherWeightsFile, sep); + CSVReader CSVWeatherWeights(this->args.WeatherWeightsFile, sep); - std::vector> WeatherWeightsDF = CSVWeatherWeigths.getData(this->args.WeatherWeightsFile); + std::vector> WeatherWeightsDF = CSVWeatherWeights.getData(this->args.WeatherWeightsFile); + cout << "ww df "<< WeatherWeightsDF[0][0] << endl; - CSVWeatherWeights.parseWeatherWeights(WeatherWeights, WeatherWeightDF, this->args.NWeatherFiles); + CSVWeatherWeights.parseWeatherWeights(WeatherWeights, WeatherWeightIDs, WeatherWeightsDF, this->args.NWeatherFiles); - } + }*/ // Relevant sets: Initialization this->availCells.clear(); @@ -2259,6 +2263,7 @@ Cell2Fire::chooseWeather(const string& weatherOpt, int rnumber, int simExt) { // Random Weather weatherFilename = this->args.InFolder + "Weathers" + separator() + "Weather" + std::to_string(rnumber) + ".csv"; + // this->CSVWeather.setFilename(weatherFilename); /* Weather DataFrame */ @@ -2349,6 +2354,16 @@ main(int argc, char* argv[]) { initialize_coeff_p(args.scenario); } + if (args.UseWeatherWeights) + { + std::string sep = ","; + CSVReader CSVWeatherWeights(args.WeatherWeightsFile, sep); + + std::vector> WeatherWeightsDF = CSVWeatherWeights.getData(args.WeatherWeightsFile); + + CSVWeatherWeights.parseWeatherWeights(WeatherWeights, WeatherWeightIDs, WeatherWeightsDF, args.NWeatherFiles); + + } cout << "\n-------Running simulations-------" << endl; // Parallel zone #pragma omp parallel num_threads(num_threads) @@ -2384,7 +2399,12 @@ main(int argc, char* argv[]) boost::random::uniform_int_distribution udistributionIgnition(1, Forest.nCells); TID = omp_get_thread_num(); - rnumber = udistribution(generator); + if (args.UseWeatherWeights) { + std::discrete_distribution dist(std::begin(WeatherWeights), std::end(WeatherWeights)); + int weatherWeightIndex = dist(generator); + rnumber = WeatherWeightIDs[weatherWeightIndex]; + } else { + rnumber = udistribution(generator);} rnumber2 = ndistribution(generator); // Reset Forest.reset(rnumber, rnumber2, ep); diff --git a/Cell2Fire/ReadCSV.cpp b/Cell2Fire/ReadCSV.cpp index 06d981fe..d997c205 100644 --- a/Cell2Fire/ReadCSV.cpp +++ b/Cell2Fire/ReadCSV.cpp @@ -14,7 +14,7 @@ #include #include #include - +#include /** * Creates an instance of CSVReader. * @param filename name of file to read @@ -588,8 +588,25 @@ CSVReader::parseIgnitionDF(std::vector& ig, std::vector& wf, std::vector>& DF, int NumberOfWeatherFiles){ - +CSVReader::parseWeatherWeights(std::vector& WeatherWeights, std::vector& WeatherWeightIDs, std::vector>& DF, int NumberOfWeatherFiles){ + int i; + std::string::size_type sz; // alias of size_t + if (DF[0].size() > NumberOfWeatherFiles) { + throw std::runtime_error("Error: number of rows in weather weights file longer than number of weather files"); + } + for (i = 0; i < DF.size(); i++) + { + int index = std::stoi(DF[i][0]); + WeatherWeights.push_back(std::stof(DF[i][1])); + WeatherWeightIDs.push_back(index); + if (index > NumberOfWeatherFiles) { + throw std::runtime_error("Error while parsing weather weights: ID not in Weathers folder"); + } + } + double sum = std::accumulate(WeatherWeights.begin(), WeatherWeights.end(), 0.0); + if (sum > 1.000001 || sum < 0.9999999) { + throw std::runtime_error("Error: weather weights must add up to 1.0"); + } } /* @@ -752,25 +769,3 @@ CSVReader::printWeatherDF(weatherDF wdf) std::cout << " " << wdf.waz; } -/* -int main() -{ - // Creating an object of CSVWriter - CSVReader reader("example.csv"); - - // Get the data from CSV File - std::vector > dataList = reader.getData(); - - // Print the content of row by row on screen - for(std::vector vec : dataList) - { - for(std::string data : vec) - { - std::cout<>& DF, int NFTypes); + void parseWeatherWeights(std::vector& WeatherWeights, std::vector& WeatherWeightIDs, std::vector>& DF, int NumberOfWeatherFiles); + // Prints individual cell info void printDF(inputs df); From f82e9c75970d070b2bdafa0d5681198c71611f76 Mon Sep 17 00:00:00 2001 From: Mati Date: Fri, 28 Nov 2025 10:30:40 -0300 Subject: [PATCH 3/4] Weather weights flag implies random weather selection --- Cell2Fire/ReadArgs.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Cell2Fire/ReadArgs.cpp b/Cell2Fire/ReadArgs.cpp index 5138400d..dd05c53b 100644 --- a/Cell2Fire/ReadArgs.cpp +++ b/Cell2Fire/ReadArgs.cpp @@ -94,6 +94,7 @@ parseArgs(int argc, char* argv[], arguments* args_ptr) printf("WeatherWeightsFile: %s \n", weather_weights_file); args_ptr->WeatherWeightsFile = weather_weights_file; args_ptr->UseWeatherWeights = true; + input_weather = "random" } else { args_ptr->WeatherWeightsFile = ∅ From 25c7b0c25a19ece3e2a7919a4b780a8a1dfe5e71 Mon Sep 17 00:00:00 2001 From: Mati Date: Tue, 2 Dec 2025 15:04:44 -0300 Subject: [PATCH 4/4] fix semicolon --- Cell2Fire/ReadArgs.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cell2Fire/ReadArgs.cpp b/Cell2Fire/ReadArgs.cpp index dd05c53b..184da890 100644 --- a/Cell2Fire/ReadArgs.cpp +++ b/Cell2Fire/ReadArgs.cpp @@ -94,7 +94,8 @@ parseArgs(int argc, char* argv[], arguments* args_ptr) printf("WeatherWeightsFile: %s \n", weather_weights_file); args_ptr->WeatherWeightsFile = weather_weights_file; args_ptr->UseWeatherWeights = true; - input_weather = "random" + input_weather = "random"; + args_ptr->WeatherOpt = "random"; } else { args_ptr->WeatherWeightsFile = ∅