WSL/SLF GitLab Repository

Commit 19109ff6 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Improving error messages for file read/write/open errors.

parent 7dedf299
......@@ -60,7 +60,7 @@ class MeteoGrids {
V, ///< North component of wind
W, ///< Vertical component of wind
SWE, ///< Snow Water Equivalent
ROT, ///< Generated runoff
ROT, ///< Total generated runoff
ALB, ///< Albedo
DEM, ///< Digital Elevation Model
SLOPE, ///< DEM slope angle
......
......@@ -16,6 +16,8 @@
along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
*/
#include <meteoio/libsmet.h>
#include <errno.h>
#include <string.h>
using namespace std;
......@@ -418,8 +420,11 @@ bool SMETWriter::valid_header()
void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std::vector<double>& data)
{
fout.open(filename.c_str());
if (fout.fail())
throw SMETException("Could not open file '" + filename + "' for writing", SMET_AT);
if (fout.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for writing, possible reason: " << strerror(errno);
throw SMETException(ss.str(), SMET_AT);
}
write_header(); //Write the header info, always in ASCII format
......@@ -429,16 +434,19 @@ void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std:
}
if (!timestamp_present)
throw SMETException("No timestamp present when writing file '"+filename+"', use write(const vector<double>& data)", SMET_AT);
throw SMETException("No timestamp present when writing file \""+filename+"\", use write(const vector<double>& data)", SMET_AT);
const size_t nr_of_data_fields = nr_of_fields - 1;
size_t nr_of_lines = data.size() / (nr_of_fields-1);
if ((nr_of_lines != vec_timestamp.size()) || ((data.size() % (nr_of_fields-1)) != 0))
throw SMETException("Inconsistency between vec_timestamp and data detected in file '"+filename+"', recheck your data", SMET_AT);
throw SMETException("Inconsistency between vec_timestamp and data detected for file \""+filename+"\", recheck your data", SMET_AT);
std::vector<double> current_data;
current_data.resize(nr_of_fields-1); //HACK: check if nr_of_fields>0
if(nr_of_fields<1) {
throw SMETException("Attempting to write a dataset that contains no fields to file \"" + filename + "\"!", SMET_AT);
}
current_data.resize(nr_of_fields-1);
check_formatting();
if (smet_type == ASCII){
......@@ -449,7 +457,7 @@ void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std:
write_data_line_ascii(vec_timestamp[ii], current_data);
}
} else {
throw SMETException("Cannot write a binary file with a timestamp, use julian instead", SMET_AT);
throw SMETException("Cannot write binary file \""+filename+"\" with a timestamp, use julian instead", SMET_AT);
}
cleanup();
......@@ -458,8 +466,11 @@ void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std:
void SMETWriter::write(const std::vector<double>& data)
{
fout.open(filename.c_str());
if (fout.fail())
throw SMETException("Could not open file '" + filename + "' for writing", SMET_AT);
if (fout.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for writing, possible reason: " << strerror(errno);
throw SMETException(ss.str(), SMET_AT);
}
write_header(); //Write the header info, always in ASCII format
......@@ -470,7 +481,7 @@ void SMETWriter::write(const std::vector<double>& data)
size_t nr_of_lines = data.size() / nr_of_fields;
if ((data.size() % nr_of_fields) != 0)
throw SMETException("Inconsistency between data and header fields detected in file '"+filename+"', recheck your data", SMET_AT);
throw SMETException("Inconsistency between data and header fields detected in file \""+filename+"\", recheck your data", SMET_AT);
std::vector<double> current_data;
current_data.resize(nr_of_fields);
......@@ -500,7 +511,7 @@ void SMETWriter::write_header()
map<string,string>::iterator it;
if (!valid_header())
throw SMETException("The header data you supplied is not valid, file '"+filename+"' cannot be written", SMET_AT);
throw SMETException("The header data you supplied is not valid, file \""+filename+"\" cannot be written", SMET_AT);
write_signature();
......@@ -572,8 +583,11 @@ void SMETWriter::write_data_line_binary(const std::vector<double>& data)
if (!file_is_binary){ //open it as binary file
fout.close();
fout.open(filename.c_str(), ios::out | ios::app | ios::binary); //reopen as binary file
if (fout.fail())
throw SMETException("Could not access file " + filename, SMET_AT);
if (fout.fail()) {
stringstream ss;
ss << "Error writing to file \"" << filename << "\", possible reason: " << strerror(errno);
throw SMETException(ss.str(), SMET_AT);
}
file_is_binary = true;
}
......@@ -649,8 +663,12 @@ SMETReader::SMETReader(const std::string& in_fname) : filename(in_fname), nr_of_
std::ifstream fin; //Input file streams
fin.clear();
fin.open (filename.c_str(), ios::in);
if (fin.fail())
throw SMETException("Could not open file '" + filename + "' for reading. Please check file existence and permissions!", SMET_AT);
if (fin.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for reading, possible reason: " << strerror(errno);
ss << " Please check file existence and permissions!";
throw SMETException(ss.str(), SMET_AT);
}
try {
eoln = SMETCommon::getEoln(fin); //get the end of line character for the file
......@@ -685,7 +703,10 @@ std::string SMETReader::get_field_name(const size_t& nr_of_field)
if (nr_of_field < nr_of_fields){
return vec_fieldnames[nr_of_fields];
} else {
throw SMETException("The field you're trying to access in file '"+filename+"' is out of bounds", SMET_AT);
stringstream ss;
ss << "Trying to access field #" << nr_of_field << " (starting from 0) of " << nr_of_fields << " fields in file \"" << filename << "\". ";
ss << "This is out of bounds!";
throw SMETException(ss.str(), SMET_AT);
}
}
......@@ -949,8 +970,11 @@ void SMETReader::read(std::vector<std::string>& vec_timestamp, std::vector<doubl
ifstream fin;
fin.clear();
fin.open (filename.c_str(), ios::in);
if (fin.fail())
throw SMETException("Could not open file '" + filename + "' for reading", SMET_AT);
if (fin.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for reading, possible reason: " << strerror(errno);
throw SMETException(ss.str(), SMET_AT);
}
try {
fin.seekg(data_start_fpointer); //jump to data start position in the file
......@@ -992,8 +1016,11 @@ void SMETReader::read(std::vector<double>& vec_data)
ifstream fin;
fin.open (filename.c_str(), ios::in);
if (fin.fail())
throw SMETException("Could not open file '" + filename + "' for reading", SMET_AT);
if (fin.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for reading, possible reason: " << strerror(errno);
throw SMETException(ss.str(), SMET_AT);
}
try {
fin.seekg(data_start_fpointer); //jump to data start position in the file
......
......@@ -16,6 +16,8 @@
along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ARCIO.h"
#include <errno.h>
#include <string.h>
using namespace std;
......@@ -171,7 +173,9 @@ void ARCIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_
fin.clear();
fin.open (full_name.c_str(), ifstream::in);
if (fin.fail()) {
throw FileAccessException(full_name, AT);
stringstream ss;
ss << "Error openning file \"" << full_name << "\", possible reason: " << strerror(errno);
throw FileAccessException(ss.str(), AT);
}
char eoln = IOUtils::getEoln(fin); //get the end of line character for the file
......@@ -230,7 +234,7 @@ void ARCIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_
} catch(const std::exception& e) {
cleanup();
std::stringstream msg;
msg << "[E] Error while reading file " << full_name << ": " << e.what();
msg << "[E] Error when reading ARC grid \"" << full_name << "\" : " << e.what();
throw InvalidFormatException(msg.str(), AT);
}
cleanup();
......@@ -314,7 +318,9 @@ void ARCIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
std::string full_name = grid2dpath_out+"/"+name;
fout.open(full_name.c_str());
if (fout.fail()) {
throw FileAccessException(full_name, AT);
stringstream ss;
ss << "Error openning file \"" << full_name << "\", possible reason: " << strerror(errno);
throw FileAccessException(ss.str(), AT);
}
try {
......
/***********************************************************************************/
/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/***********************************************************************************/
/* This file is part of MeteoIO.
......@@ -16,6 +15,8 @@
along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PGMIO.h"
#include <errno.h>
#include <string.h>
using namespace std;
......@@ -111,7 +112,9 @@ void PGMIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_
fin.clear();
fin.open (full_name.c_str(), ifstream::in);
if (fin.fail()) {
throw FileAccessException(full_name, AT);
stringstream ss;
ss << "Error openning file \"" << full_name << "\", possible reason: " << strerror(errno);
throw FileAccessException(ss.str(), AT);
}
const char eoln = IOUtils::getEoln(fin); //get the end of line character for the file
......@@ -154,7 +157,10 @@ void PGMIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_
getline(fin, line, eoln); //read complete line
if (IOUtils::readLineToVec(line, tmpvec) != ncols) {
throw InvalidFormatException("Premature End " + full_name, AT);
stringstream ss;
ss << "Invalid number of columns at line " << nrows-kk << " in file \"" << full_name << "\". ";
ss << "Expecting " << ncols << " columns\n";
throw InvalidFormatException(ss.str(), AT);
}
for (unsigned int ll=0; ll < ncols; ll++){
......@@ -171,6 +177,7 @@ void PGMIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_
}
}
} catch(const std::exception&) {
cout << "[E] error when reading PGM grid \"" << full_name << "\" " << AT << ": "<< endl;
cleanup();
throw;
}
......@@ -238,7 +245,9 @@ void PGMIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
const unsigned int nr_colors = 256;
fout.open(full_name.c_str());
if (fout.fail()) {
throw FileAccessException(full_name, AT);
stringstream ss;
ss << "Error openning file \"" << full_name << "\", possible reason: " << strerror(errno);
throw FileAccessException(ss.str(), AT);
}
Coords llcorner=grid_in.llcorner;
......@@ -267,14 +276,14 @@ void PGMIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
for (unsigned int ll=0; ll < grid_in.ncols; ll++) {
const double value = grid_in.grid2D(ll, kk);
if(value!=IOUtils::nodata)
fout << (int)floor((grid_in.grid2D(ll, kk)-min_value)*scaling)+1 << " ";
fout << static_cast<unsigned int>( floor((grid_in.grid2D(ll, kk)-min_value)*scaling)+1 ) << " ";
else
fout << "0" << " ";
}
fout << "\n";
}
} catch(...) {
cout << "[E] " << AT << ": "<< endl;
cout << "[E] error when writing PGM grid \"" << full_name << "\" " << AT << ": "<< endl;
cleanup();
throw;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment