Commit 17227484b0175e9a332ecac7ce9a73034820f398
1 parent
48c732ad
Rename GApplication::eCO2() method to GApplication::gCO2e() and make method inte…
…rface country-dependent (still need to implement country-code dependent emission factors)
Showing
7 changed files
with
78 additions
and
24 deletions
NEWS
| ... | ... | @@ -44,7 +44,7 @@ The following classes have been renamed: |
| 44 | 44 | - none |
| 45 | 45 | |
| 46 | 46 | The following methods have been added: |
| 47 | -- GApplication::eCO2() | |
| 47 | +- GApplication::gCO2e() | |
| 48 | 48 | - GApplication::stamp(GFitsHdu&) |
| 49 | 49 | - GApplication::stamp(GFits&) |
| 50 | 50 | - GApplication::stamp(GFilename&) |
| ... | ... | @@ -146,6 +146,8 @@ The following methods have been added: |
| 146 | 146 | - gammalib::compare() |
| 147 | 147 | - gammalib::xml_check_parnum() |
| 148 | 148 | - gammalib::xml_check_type() |
| 149 | +- gammalib::host_country() | |
| 150 | +- gammalib::http_query() | |
| 149 | 151 | - gammalib::check_energy_interval() |
| 150 | 152 | - gammalib::check_prj_x2s_status() |
| 151 | 153 | - gammalib::check_prj_s2x_status() |
| ... | ... | @@ -214,7 +216,7 @@ Add GApplication::stamp() methods to write provenance information into a FITS |
| 214 | 216 | HDU, a FITS object or a FITS file. So far the only provenance information that |
| 215 | 217 | is written is the CREATOR keyword (#3662). |
| 216 | 218 | |
| 217 | -Add GApplication::eCO2() method to estimate the carbon footprint if the elapsed | |
| 219 | +Add GApplication::gCO2e() method to estimate the carbon footprint if the elapsed | |
| 218 | 220 | CPU time. The carbon footprint estimation is also added to the trailer text of |
| 219 | 221 | an application to raise awareness. In addition, an ASCII file is maintained in |
| 220 | 222 | the $HOME/.gamma folder that contains statistics on application usage, including |
| ... | ... | @@ -410,6 +412,11 @@ were added to compute the total solid angle within specified sky region(s) (#178 |
| 410 | 412 | |
| 411 | 413 | 14. Support module |
| 412 | 414 | ------------------ |
| 415 | +Added gammalib::host_country() method that returns the host country on which the code | |
| 416 | +is executed. | |
| 417 | + | |
| 418 | +Added gammalib::http_query() method to get the response of a HTTP query. | |
| 419 | + | |
| 413 | 420 | Added gammalib::strdate() method that returns a string of the current date and |
| 414 | 421 | time (#3592). |
| 415 | 422 | ... | ... |
include/GApplication.hpp
| ... | ... | @@ -83,7 +83,7 @@ public: |
| 83 | 83 | const std::string& version(void) const; |
| 84 | 84 | double telapse(void) const; |
| 85 | 85 | double celapse(void) const; |
| 86 | - double eCO2(void) const; | |
| 86 | + double gCO2e(const std::string& country) const; | |
| 87 | 87 | void logFileOpen(const bool& clobber = true); |
| 88 | 88 | void logFileClose(void); |
| 89 | 89 | bool logTerse(void) const; | ... | ... |
include/GTools.hpp
pyext/GApplication.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GApplication.i - GammaLib application base class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2021 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2010-2022 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -60,7 +60,7 @@ public: |
| 60 | 60 | // Public methods |
| 61 | 61 | double telapse(void) const; |
| 62 | 62 | double celapse(void) const; |
| 63 | - double eCO2(void) const; | |
| 63 | + double gCO2e(const std::string& country) const; | |
| 64 | 64 | void logFileOpen(const bool& clobber = true); |
| 65 | 65 | void logFileClose(void); |
| 66 | 66 | ... | ... |
pyext/GTools.i
src/app/GApplication.cpp
| ... | ... | @@ -392,15 +392,16 @@ double GApplication::celapse(void) const |
| 392 | 392 | |
| 393 | 393 | |
| 394 | 394 | /***********************************************************************//** |
| 395 | - * @brief Return application equivalent CO2 imprint (units: g) | |
| 395 | + * @brief Return application equivalent CO2 footprint (units: g CO2e) | |
| 396 | 396 | * |
| 397 | - * @return Application equivalent CO2 imprint (g). | |
| 397 | + * @param[in] country Country where computation is performed. | |
| 398 | + * @return Application equivalent CO2 footprint (g CO2e). | |
| 398 | 399 | * |
| 399 | - * The method returns the equivalent CO2 imprint in grams after the currently | |
| 400 | - * elpased CPU time as returned by celapse(). The method assumes an imprint | |
| 401 | - * of 4.68 g eCO2 / CPU hour as estimated by Berthoud et al. (2020) | |
| 402 | - * (see page 10 of https://hal.archives-ouvertes.fr/hal-02549565v4/document). | |
| 403 | - * The imprint factor includes | |
| 400 | + * The method returns the equivalent CO2 footprint in grams after the | |
| 401 | + * currently elpased CPU time as returned by celapse(). The method assumes | |
| 402 | + * an emission factor of 4.68 g CO2e / CPU hour as estimated by Berthoud et | |
| 403 | + * al. (2020) (https://hal.archives-ouvertes.fr/hal-02549565v4/document). | |
| 404 | + * The emission factor includes | |
| 404 | 405 | * * server fabrication |
| 405 | 406 | * * server environment |
| 406 | 407 | * * server usage (electricity) |
| ... | ... | @@ -409,23 +410,25 @@ double GApplication::celapse(void) const |
| 409 | 410 | * * personnel equipment |
| 410 | 411 | * * personnel energy |
| 411 | 412 | * |
| 412 | - * The imprint factor is based on an estimate done for the DAHU cluster | |
| 413 | - * of the UMS GRICAD in 2019. Note that the impact of electricity was | |
| 413 | + * The emission factor is based on an estimate done for the DAHU cluster | |
| 414 | + * of the UMS GRICAD in 2019. Note that the footprint of electricity was | |
| 414 | 415 | * computed assuming 108 g eCO2 / kWh consumed, and about 50% of the total |
| 415 | - * imprint was due to electricity consumption. For countries with a more | |
| 416 | + * footprint was due to electricity consumption. For countries with a more | |
| 416 | 417 | * carbon intensive electricity production, the CO2 imprint will be |
| 417 | 418 | * accordingly larger. |
| 419 | + * | |
| 420 | + * @todo Implement carbon footprint for other countries than France | |
| 418 | 421 | ***************************************************************************/ |
| 419 | -double GApplication::eCO2(void) const | |
| 422 | +double GApplication::gCO2e(const std::string& country) const | |
| 420 | 423 | { |
| 421 | 424 | // Get elapsed time in CPU jours |
| 422 | 425 | double cpu_hours = celapse() / 3600.0; |
| 423 | 426 | |
| 424 | 427 | // Convert into g eCO2 |
| 425 | - double eCO2 = 4.68 * cpu_hours; | |
| 428 | + double gCO2e = 4.68 * cpu_hours; | |
| 426 | 429 | |
| 427 | - // Return equivalent CO2 imprint | |
| 428 | - return eCO2; | |
| 430 | + // Return equivalent CO2 footprint | |
| 431 | + return gCO2e; | |
| 429 | 432 | } |
| 430 | 433 | |
| 431 | 434 | |
| ... | ... | @@ -633,14 +636,14 @@ void GApplication::log_trailer(void) |
| 633 | 636 | // Get application statistics |
| 634 | 637 | double telapse = this->telapse(); |
| 635 | 638 | double celapse = this->celapse(); |
| 636 | - double eCO2 = this->eCO2(); | |
| 639 | + double gCO2e = this->gCO2e(gammalib::host_country()); | |
| 637 | 640 | |
| 638 | 641 | // Dump trailer |
| 639 | 642 | log << "Application \"" << m_name << "\" terminated after "; |
| 640 | 643 | log << telapse << " wall clock seconds, consuming "; |
| 641 | 644 | log << celapse << " seconds of CPU time and generating a carbon"; |
| 642 | - log << " footprint of " << eCO2 << " g eCO2."; | |
| 643 | - if (eCO2 >= 1000.0) { | |
| 645 | + log << " footprint of " << gCO2e << " g eCO2."; | |
| 646 | + if (gCO2e >= 1000.0) { | |
| 644 | 647 | log << " Please watch your carbon footprint."; |
| 645 | 648 | } |
| 646 | 649 | if (telapse < 0.1) { |
| ... | ... | @@ -1213,10 +1216,18 @@ void GApplication::write_statistics(void) |
| 1213 | 1216 | // statistics |
| 1214 | 1217 | FILE* fptr = fopen(filename.c_str(), "a"); |
| 1215 | 1218 | if (fptr != NULL) { |
| 1216 | - fprintf(fptr, "%s,%s,FR,%s,%s,%e,%e,%e\n", | |
| 1219 | + | |
| 1220 | + // Get host country | |
| 1221 | + std::string country = gammalib::host_country(); | |
| 1222 | + | |
| 1223 | + // Write statistics string | |
| 1224 | + fprintf(fptr, "%s,%s,%s,%s,%s,%e,%e,%e\n", | |
| 1217 | 1225 | gammalib::strdate().c_str(), VERSION, |
| 1226 | + country.c_str(), | |
| 1218 | 1227 | name().c_str(), version().c_str(), |
| 1219 | - telapse(), celapse(), eCO2()); | |
| 1228 | + telapse(), celapse(), gCO2e(country)); | |
| 1229 | + | |
| 1230 | + // Close file | |
| 1220 | 1231 | fclose(fptr); |
| 1221 | 1232 | } |
| 1222 | 1233 | ... | ... |
src/support/GTools.cpp
| ... | ... | @@ -2288,3 +2288,37 @@ std::string gammalib::http_query(const std::string& host, const std::string& que |
| 2288 | 2288 | // Return response |
| 2289 | 2289 | return std::string(response); |
| 2290 | 2290 | } |
| 2291 | + | |
| 2292 | + | |
| 2293 | +/***********************************************************************//** | |
| 2294 | + * @brief Return host country | |
| 2295 | + * | |
| 2296 | + * @return Host country. | |
| 2297 | + * | |
| 2298 | + * Returns host country using a curl query to http://api.hostip.info/. If | |
| 2299 | + * the country could not be determined an empty string is returned. | |
| 2300 | + ***************************************************************************/ | |
| 2301 | +std::string gammalib::host_country(void) | |
| 2302 | +{ | |
| 2303 | + // Initialise country | |
| 2304 | + std::string country; | |
| 2305 | + | |
| 2306 | + // Setup curl command | |
| 2307 | + char command[256]; | |
| 2308 | + sprintf(command, "curl --silent http://api.hostip.info/country.php?ip="); | |
| 2309 | + | |
| 2310 | + // Open the process with given 'command' for reading | |
| 2311 | + FILE* file = popen(command, "r"); | |
| 2312 | + | |
| 2313 | + // Retrieve curl output | |
| 2314 | + char output[1024]; | |
| 2315 | + if (fgets(output, sizeof(output)-1, file) != NULL) { | |
| 2316 | + country = std::string(output); | |
| 2317 | + } | |
| 2318 | + | |
| 2319 | + // Close process | |
| 2320 | + pclose(file); | |
| 2321 | + | |
| 2322 | + // Return country | |
| 2323 | + return country; | |
| 2324 | +} | ... | ... |