Commit e2a42f93d78f8a8f95ccf45e01377a0dd612b69f

Authored by Jürgen Knödlseder
1 parent ec01b026

saveto() method handles also gzipped FITS files.

The saveto() method now also checks whether a file with the .gz suffix exists.
Showing 1 changed file with 18 additions and 5 deletions
src/fits/GFits.cpp
@@ -1213,19 +1213,32 @@ void GFits::saveto(const std::string& filename, const bool& clobber) @@ -1213,19 +1213,32 @@ void GFits::saveto(const std::string& filename, const bool& clobber)
1213 // Expand environment variables 1213 // Expand environment variables
1214 std::string fname = gammalib::expand_env(filename); 1214 std::string fname = gammalib::expand_env(filename);
1215 1215
  1216 + // Create gzipped file name version
  1217 + std::string gzfname = fname + ".gz";
  1218 +
1216 // Debug header 1219 // Debug header
1217 #if defined(G_DEBUG) 1220 #if defined(G_DEBUG)
1218 std::cout << "GFits::saveto(\"" << fname << "\", " << clobber << ")" 1221 std::cout << "GFits::saveto(\"" << fname << "\", " << clobber << ")"
1219 << " (size=" << size() << ") -->" << std::endl; 1222 << " (size=" << size() << ") -->" << std::endl;
1220 #endif 1223 #endif
1221 1224
1222 - // If overwriting has been specified then remove any existing file ... 1225 + // If overwriting has been specified then remove any existing file. We
  1226 + // consider here also the possibility that the file is gzipped, but
  1227 + // the file name does not contain the .gz extension.
1223 if (clobber) { 1228 if (clobber) {
1224 - std::remove(fname.c_str()); 1229 + if (gammalib::file_exists(fname)) {
  1230 + std::remove(fname.c_str());
  1231 + }
  1232 + else {
  1233 + if (gammalib::file_exists(gzfname)) {
  1234 + std::remove(gzfname.c_str());
  1235 + }
  1236 + }
1225 } 1237 }
1226 1238
1227 - // ... otherwise, if file exists then throw an exception  
1228 - else if (gammalib::file_exists(fname)) { 1239 + // Otherwise, if file exists then throw an exception
  1240 + else if (gammalib::file_exists(fname) ||
  1241 + gammalib::file_exists(gzfname)) {
1229 throw GException::fits_file_exist(G_SAVETO, fname); 1242 throw GException::fits_file_exist(G_SAVETO, fname);
1230 } 1243 }
1231 1244
@@ -1393,7 +1406,7 @@ void GFits::free_members(void) @@ -1393,7 +1406,7 @@ void GFits::free_members(void)
1393 // If FITS file has been opened then close it now 1406 // If FITS file has been opened then close it now
1394 if (m_fitsfile != NULL) { 1407 if (m_fitsfile != NULL) {
1395 1408
1396 - // If file has been created but not yet save then delete the file 1409 + // If file has been created but not yet saved then delete the file
1397 // now. We do not worry about the status in this case. 1410 // now. We do not worry about the status in this case.
1398 if (m_created) { 1411 if (m_created) {
1399 int status = 0; 1412 int status = 0;