WSL/SLF GitLab Repository

Commit bedd089e authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Following a few coverity scans, several inconsistencies / potential bugs have...

Following a few coverity scans, several inconsistencies / potential bugs have been fixed. Since my commit to picojson got merged, the newest version of picojson has been taken.
parent d4021fad
......@@ -207,7 +207,8 @@ void ResamplingAlgorithms2D::Bilinear(Array2D<double> &o_grid, const Array2D<dou
}
}
double ResamplingAlgorithms2D::BSpline_weight(const double &x) {
double ResamplingAlgorithms2D::BSpline_weight(const double &x)
{
double R = 0.;
if ((x+2.)>0.) R += Optim::pow3(x+2.);
if ((x+1.)>0.) R += -4.*Optim::pow3(x+1.);
......@@ -250,16 +251,14 @@ void ResamplingAlgorithms2D::cubicBSpline(Array2D<double> &o_grid, const Array2D
}
if (avg_count==16) { //normal bicubic
o_grid(ii,jj) = F*(16/avg_count);
o_grid(ii,jj) = F;
if (o_grid(ii,jj)>max) o_grid(ii,jj)=max; //try to limit overshoot
else if (o_grid(ii,jj)<min) o_grid(ii,jj)=min; //try to limit overshoot
} else if (avg_count==0) o_grid(ii,jj) = IOUtils::nodata; //nodata-> nodata
else //not enought data points -> bilinear for this pixel
o_grid(ii,jj) = bilinear_pixel(i_grid, org_ii, org_jj, org_nx, org_ny, dx, dy);
}
}
}
} //namespace
......
......@@ -136,15 +136,11 @@ void TimeSeriesManager::push_meteo_data(const IOUtils::ProcessingLevel& level, c
if (invalidate_cache) point_cache.clear(); //clear point cache, so that we don't return resampled values of deprecated data
}
//should we implement a cache for stationData?
size_t TimeSeriesManager::getStationData(const Date& date, STATIONS_SET& vecStation)
{
vecStation.clear();
if (processing_level == IOUtils::raw) {
iohandler.readStationData(date, vecStation);
} else {
iohandler.readStationData(date, vecStation); //HACK
}
iohandler.readStationData(date, vecStation);
return vecStation.size();
}
......@@ -244,11 +240,7 @@ size_t TimeSeriesManager::getMeteoData(const Date& i_date, METEO_SET& vecMeteo)
void TimeSeriesManager::writeMeteoData(const std::vector< METEO_SET >& vecMeteo, const std::string& name)
{
if (processing_level == IOUtils::raw) {
iohandler.writeMeteoData(vecMeteo, name);
} else {
iohandler.writeMeteoData(vecMeteo, name);
}
iohandler.writeMeteoData(vecMeteo, name);
}
double TimeSeriesManager::getAvgSamplingRate() const
......
......@@ -128,10 +128,10 @@ double ProcShade::getMaskElevation(const std::vector< std::pair<double,double> >
x2 = mask[ii].first;
y2 = mask[ii].second;
} else {
x1 = mask.end()->first - 360.;
y1 = mask.end()->second;
x2 = mask.begin()->first;
y2 = mask.begin()->second;
x1 = mask.back().first - 360.;
y1 = mask.back().second;
x2 = mask.front().first;
y2 = mask.front().second;
}
const double a = (y2 - y1) / (x2 - x1);
......@@ -190,6 +190,7 @@ void ProcShade::readMask(const std::string& filter, const std::string& filename,
throw;
}
if (o_mask.empty()) throw InvalidArgumentException("In filter 'SHADE', no valid mask found in file '"+filename+"'", AT);
std::sort(o_mask.begin(), o_mask.end(), sort_pred());
}
......@@ -203,6 +204,8 @@ void ProcShade::computeMask(const DEMObject& i_dem, const StationData& sd, std::
}
i_dem.getHorizon(position, 10., o_mask); //by 10deg increments
if (o_mask.empty()) throw InvalidArgumentException( "In filter 'SHADE', could not compute mask from DEM '"+i_dem.llcorner.toString(Coords::LATLON)+"'", AT);
if (dump_mask) {
std::cout << "Horizon mask for station '" << sd.stationID << "'\n";
for (size_t ii=0; ii<o_mask.size(); ii++)
......
......@@ -280,13 +280,13 @@ void ARPSIO::read2DGrid_internal(FILE* &fin, const std::string& filename, Grid2D
void ARPSIO::read3DGrid(Grid3DObject& grid_out, const std::string& i_name)
{
const size_t pos = i_name.find_last_of(":");//a specific parameter can be provided as {filename}:{parameter}
const std::string filename = (pos!=IOUtils::npos)? grid3dpath_in +"/" + i_name.substr(0, pos) : grid3dpath_in +"/" + i_name;
if (pos==IOUtils::npos) { //TODO: read by default the first data grid that is found?
const std::string filename = (pos!=std::string::npos)? grid3dpath_in +"/" + i_name.substr(0, pos) : grid3dpath_in +"/" + i_name;
if (pos==std::string::npos) { //TODO: read by default the first data grid that is found?
listFields(i_name);
throw InvalidArgumentException("Please provide the parameter that has to be read!", AT);
}
std::string parameter = (pos!=IOUtils::npos)? i_name.substr(pos+1) : "";
std::string parameter = (pos!=std::string::npos)? i_name.substr(pos+1) : "";
if (parameter=="DEM") { //this is called damage control... this is so ugly...
parameter = (is_true_arps)? "zp coordinat" : "zp_coordinat";
}
......@@ -420,7 +420,6 @@ void ARPSIO::openGridFile(FILE* &fin, const std::string& filename)
{
if (!FileUtils::fileExists(filename)) throw AccessException(filename, AT); //prevent invalid filenames
if ((fin=fopen(filename.c_str(),"r")) == NULL) {
fclose(fin);
throw AccessException("Can not open file "+filename, AT);
}
......
......@@ -26,6 +26,7 @@
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <errno.h>
using namespace std;
......@@ -211,7 +212,7 @@ void CNRMIO::readMetaData(const int& ncid, std::vector<StationData>& vecStation)
//Parse to StationData objects
Coords location(coordin, coordinparam);
ostringstream ss;
std::ostringstream ss;
for (size_t ii=0; ii<dimlen; ii++) {
location.setLatLon(lat[ii], lon[ii], alt[ii]);
......@@ -533,7 +534,14 @@ void CNRMIO::writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMete
int ncid, did_time, vid_time, did_points;
const bool exists = FileUtils::fileExists(file_and_path);
if (exists) remove(file_and_path.c_str()); // NOTE: file is deleted if it exists
if (exists) {// NOTE: file is deleted if it exists
errno = 0;
if ( remove(file_and_path.c_str())!=0 ) {
std::ostringstream ss;
ss << "Error deleting file \"" << file_and_path << "\", possible reason: " << strerror(errno);
throw AccessException(ss.str(), AT);
}
}
map<string, double*> map_data; // holds a pointer for every C array to be written
map_data[cnrm_latitude] = new double[number_of_stations];
......
......@@ -344,8 +344,8 @@ std::map<std::string, std::vector<DBO::tsMeta> > getTsProperties(picojson::value
for (size_t jj=0; jj<array.size(); jj++) {
if (! array[jj].is<picojson::null>()) {
std::string code, device_code, agg_type;
double id;
unsigned int interval;
double id = -1.;
unsigned int interval = 0;
Date since, until;
const picojson::value::object& obj = array[jj].get<picojson::object>();
......@@ -361,6 +361,7 @@ std::map<std::string, std::vector<DBO::tsMeta> > getTsProperties(picojson::value
if (device_code=="BATTERY" || device_code=="LOGGER") break;
if (agg_type=="SD") break; //we don't care about standard deviation anyway
if (id==-1.) break; //no id was provided
const std::string param_str( IOUtils::strToUpper( code.substr(0, code.find('_')) ) );
tsMap[param_str].push_back( DBO::tsMeta(since, until, agg_type, id, interval) );
......@@ -470,6 +471,8 @@ void DBO::fillStationMeta()
//read all data for the given station
void DBO::readData(const Date& dateStart, const Date& dateEnd, std::vector<MeteoData>& vecMeteo, const size_t& stationindex)
{
const Date Start(dateStart.getJulian(true), 0.);
const Date End(dateEnd.getJulian(true), 0.);
//debug info
/*for(size_t ii=0; ii<vecStationName.size(); ii++) {
for (std::map<std::string, std::vector<DBO::tsMeta> >::iterator it = vecTsMeta[ii].begin(); it != vecTsMeta[ii].end(); ++it) {
......@@ -478,31 +481,32 @@ void DBO::readData(const Date& dateStart, const Date& dateEnd, std::vector<Meteo
}
}*/
//TODO: for station stationindex, loop over the timeseries that cover [dateStart, dateEnd] for the current station
//TODO: for station stationindex, loop over the timeseries that cover [Start, End] for the current station
//vecTsMeta[ stationindex ]
/*for (std::map<std::string, std::vector<DBO::tsMeta> >::iterator it = vecTsMeta[stationindex].begin(); it != vecTsMeta[stationindex].end(); ++it) {
for (std::map<std::string, std::vector<DBO::tsMeta> >::iterator it = vecTsMeta[stationindex].begin(); it != vecTsMeta[stationindex].end(); ++it) {
for(size_t jj=0; jj<it->second.size(); jj++)
std::cout << it->first << " " << it->second[jj].toString() << "\n";
}*/
}
readTimeSerie(15, MeteoData::TA, dateStart, dateEnd, vecMeta[stationindex], vecMeteo);
readTimeSerie(23, MeteoData::RH, dateStart, dateEnd, vecMeta[stationindex], vecMeteo);
readTimeSerie(93, MeteoData::RSWR, dateStart, dateEnd, vecMeta[stationindex], vecMeteo);
readTimeSerie(31, MeteoData::HS, dateStart, dateEnd, vecMeta[stationindex], vecMeteo);
readTimeSerie(46, MeteoData::TSG, dateStart, dateEnd, vecMeta[stationindex], vecMeteo);
readTimeSerie(15, MeteoData::TA, Start, End, vecMeta[stationindex], vecMeteo);
readTimeSerie(23, MeteoData::RH, Start, End, vecMeta[stationindex], vecMeteo);
readTimeSerie(93, MeteoData::RSWR, Start, End, vecMeta[stationindex], vecMeteo);
readTimeSerie(31, MeteoData::HS, Start, End, vecMeta[stationindex], vecMeteo);
readTimeSerie(46, MeteoData::TSG, Start, End, vecMeta[stationindex], vecMeteo);
/*for (size_t param=MeteoData::firstparam; param<=MeteoData::lastparam; param++) {
}*/
}
//dateStart and dateEnd should already be GMT
void DBO::readTimeSerie(const unsigned int& ts_id, const MeteoData::Parameters& param, const Date& dateStart, const Date& dateEnd, const StationData& sd, std::vector<MeteoData>& vecMeteo)
{
std::ostringstream ss_ID; ss_ID << ts_id;
const std::string base_url( data_endpoint + ss_ID.str() );
//const std::string period( "?from=" + dateStart.toString(Date::ISO_TZ) + "&until=" + dateEnd.toString(Date::ISO_TZ) );
const std::string period( string("?from=2016-11-17T13:00Z") + "&until=" + "2017-01-05T13:00Z" );
const std::string period( "?from=" + dateStart.toString(Date::ISO) + "Z&until=" + dateEnd.toString(Date::ISO)+"Z" );
//const std::string period( string("?from=2016-11-17T13:00Z") + "&until=" + "2017-01-05T13:00Z" );
const std::string request( base_url + period );
std::stringstream ss;
......@@ -510,7 +514,10 @@ void DBO::readTimeSerie(const unsigned int& ts_id, const MeteoData::Parameters&
if (ss.str().empty()) throw UnknownValueException("Timeseries not found: '"+ss_ID.str()+"'", AT);
picojson::value v;
const std::string err( picojson::parse(v, ss.str()) );
if (!err.empty()) throw IOException("Error while parsing JSON: "+err, AT);
if (!err.empty()) {
std::cerr << ss.str() << "\n";
throw IOException("Error while parsing JSON: "+err, AT);
}
parseTimeSerie(ss_ID.str(), param, sd, v, vecMeteo);
} else {
......
......@@ -740,7 +740,7 @@ void NetCDFIO::write2DGrid_internal(Grid2DObject grid_in, const std::string& fil
}
if (is_record) {
size_t pos_start = ncpp::add_record(ncid, NetCDFIO::cf_time, vid_time, static_cast<double>( date.getUnixDate()/3600 ));
const size_t pos_start = ncpp::add_record(ncid, NetCDFIO::cf_time, vid_time, static_cast<double>(date.getUnixDate())/3600. );
ncpp::write_data(ncid, attr.var, vid_var, grid_in.getNy(), grid_in.getNx(), pos_start, data);
} else {
ncpp::write_data(ncid, attr.var, vid_var, data);
......
......@@ -341,7 +341,7 @@ void PNGIO::setPalette(const Gradient &gradient, png_structp& png_ptr, png_infop
std::vector<unsigned char> pal;
size_t nr_colors;
gradient.getPalette(pal, nr_colors);
palette = (png_color*)calloc(sizeof (png_color), nr_colors); //ie: three png_bytes, each being an unsigned char
palette = (png_color*)calloc(nr_colors, sizeof (png_color)); //ie: three png_bytes, each being an unsigned char
for (size_t ii=0; ii<nr_colors; ii++) {
const size_t interlace = ii*3; //colors from Gradient interlaced
palette[ii].red = static_cast<png_byte>(pal[interlace]);
......@@ -358,7 +358,7 @@ void PNGIO::writeDataSection(const Grid2DObject& grid, const Array2D<double>& le
// Allocate memory for one row (3 bytes per pixel - RGB)
const unsigned char channels = (indexed_png)? 1 : 3; //4 for rgba
png_bytep row = (png_bytep)calloc(channels*sizeof(png_byte), full_width);
png_bytep row = (png_bytep)calloc(full_width, channels*sizeof(png_byte));
if (row==NULL) {
fclose(fp); fp=NULL;
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
......@@ -707,13 +707,13 @@ void PNGIO::writeMetadata(png_structp &png_ptr, png_infop &info_ptr)
{
const size_t max_len = 79; //according to the official specs' recommendation
const size_t nr = metadata_key.size();
png_text *info_text = (png_text *)calloc(sizeof(png_text), nr);
char **key = (char**)calloc(sizeof(char)*max_len, nr);
char **text = (char**)calloc(sizeof(char)*max_len, nr);
png_text *info_text = (png_text *)calloc(nr, sizeof(png_text));
char **key = (char**)calloc(nr, sizeof(char)*max_len);
char **text = (char**)calloc(nr, sizeof(char)*max_len);
for (size_t ii=0; ii<nr; ii++) {
key[ii] = (char *)calloc(sizeof(char), max_len);
text[ii] = (char *)calloc(sizeof(char), max_len);
key[ii] = (char *)calloc(max_len, sizeof(char));
text[ii] = (char *)calloc(max_len, sizeof(char));
strncpy(key[ii], metadata_key[ii].c_str(), max_len-1); //in case the '\0' was not counted by maxlen
key[ii][max_len-1] = '\0'; //make sure it is null terminated
strncpy(text[ii], metadata_text[ii].c_str(), max_len-1);
......
......@@ -663,7 +663,8 @@ void calculate_dimensions(const mio::Grid2DObject& grid, double*& lat_array, dou
// corner to calculate the lat/lon intervals between cells
Coords urcorner(grid.llcorner);
urcorner.setGridIndex(static_cast<int>(grid.getNx() - 1), static_cast<int>(grid.getNy() - 1), IOUtils::nodata, true);
grid.gridify(urcorner); //no need to check the return value: we know it fits within the grid
if (grid.gridify(urcorner) )
throw IndexOutOfBoundsException("URcorner not within the grid... This should never happen!", AT);
const double lat_interval = (urcorner.getLat() - lat_array[0]) / static_cast<double>(grid.getNy()-1);
const double lon_interval = (urcorner.getLon() - lon_array[0]) / static_cast<double>(grid.getNx()-1);
......
......@@ -668,8 +668,13 @@ void SMETWriter::write(const std::vector<double>& data)
void SMETWriter::print_if_exists(const std::string& header_field, std::ofstream& fout) const
{
const std::map<string,string>::const_iterator it = header.find(header_field);
if (it != header.end())
fout << std::left << std::setw(16) << header_field << " = " << it->second << "\n";
if (it != header.end()) {
const std::ios_base::fmtflags flags = fout.setf(std::ios::left);
const std::streamsize width = fout.width(16);
fout << header_field << " = " << it->second << "\n";
fout.width(width);
fout.setf(flags);
}
}
void SMETWriter::write_header(std::ofstream& fout)
......@@ -765,7 +770,6 @@ void SMETWriter::write_data_line_ascii(const std::string& timestamp, const std::
for (size_t ii = 0; ii < data.size(); ii++){
if (ii > 0) fout << " ";
if (timestamp_present && (timestamp_field == ii)) fout << timestamp << " ";
fout << setw(ascii_width[ii]) << setprecision(ascii_precision[ii]);
if (data[ii] == nodata_value) fout << nodata_string; //to have a nicer representation
else fout << data[ii];
......@@ -1084,7 +1088,7 @@ void SMETReader::truncate_file(const std::string& date_stop) const
}
std::ofstream fout; //for the tmp file
const std::string filename_tmp = filename + ".tmp";
const std::string filename_tmp( filename + ".tmp" );
fout.open(filename_tmp.c_str(), ios::out|ios::binary);
if (fout.fail()) {
ostringstream ss;
......@@ -1100,7 +1104,13 @@ void SMETReader::truncate_file(const std::string& date_stop) const
fin.close();
SMETCommon::copy_file(filename_tmp, filename);
remove(filename_tmp.c_str()); //delete temporary file
errno = 0;
if (remove(filename_tmp.c_str())!=0) { //delete temporary file
ostringstream ss;
ss << "Error deleting file \"" << filename << "\", possible reason: " << strerror(errno);
throw SMETException(ss.str(), SMET_AT);
}
}
void SMETReader::copy_file_header(std::ifstream& fin, std::ofstream& fout) const
......
This diff is collapsed.
Markdown is supported
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