Skip to content

Commit

Permalink
add exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hugopendlebury committed Jan 31, 2024
1 parent 427a155 commit 1996ef1
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 33 deletions.
13 changes: 11 additions & 2 deletions pythonApi/grib_to_arrow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "../src/gribreader.hpp"
#include "../src/gribmessage.hpp"
#include "../src/exceptions/notsuchfileexception.hpp"
#include "../src/exceptions/nosuchgribfileexception.hpp"
#include "../src/exceptions/nosuchlocationsfileexception.hpp"
#include "../src/exceptions/arrowtablereadercreationexception.hpp"
#include "../src/exceptions/arrowgenericexception.hpp"
#include "../src/exceptions/invalidcsvexception.hpp"

//#define USE_CMAKE

Expand All @@ -26,7 +30,12 @@ using namespace std;
PYBIND11_MODULE(gribtoarrow, m)
{

py::register_exception<NoSuchFileException>(m, "NoSuchFileException");
py::register_exception<InvalidCSVException>(m, "InvalidCSVException");
py::register_exception<NoSuchGribFileException>(m, "NoSuchGribFileException");
py::register_exception<NoSuchLocationsFileException>(m, "NoSuchLocationsFileException");
py::register_exception<UnableToCreateArrowTableReaderException>(m, "UnableToCreateArrowTableReaderException");
py::register_exception<ArrowGenericException>(m, "ArrowGenericException");

arrow::py::import_pyarrow();
py::class_<GribReader>(m, "GribReader")
.def(py::init<string>(), pybind11::call_guard<pybind11::gil_scoped_release>(), R"EOL(
Expand Down
9 changes: 9 additions & 0 deletions src/exceptions/arrowgenericexception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

class ArrowGenericException : public std::runtime_error
{
public:

ArrowGenericException(std::string errorDetails) : std::runtime_error("Exception " + errorDetails) { }

};
9 changes: 9 additions & 0 deletions src/exceptions/arrowtablereadercreationexception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

class UnableToCreateArrowTableReaderException : public std::runtime_error
{
public:

UnableToCreateArrowTableReaderException(std::string errorDetails) : std::runtime_error("Unable to create arrow table reader " + errorDetails) { }

};
9 changes: 9 additions & 0 deletions src/exceptions/invalidcsvexception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

class InvalidCSVException : public std::runtime_error
{
public:

InvalidCSVException(std::string filepath) : std::runtime_error("Invalid CSV file - is there a formatting error with file " + filepath) { }

};
9 changes: 9 additions & 0 deletions src/exceptions/nosuchgribfileexception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

class NoSuchGribFileException : public std::runtime_error
{
public:

NoSuchGribFileException(std::string filepath) : std::runtime_error("Unable to find file " + filepath) { }

};
9 changes: 9 additions & 0 deletions src/exceptions/nosuchlocationsfileexception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

class NoSuchLocationsFileException : public std::runtime_error
{
public:

NoSuchLocationsFileException(std::string filepath) : std::runtime_error("Unable to find Locations file " + filepath) { }

};
22 changes: 0 additions & 22 deletions src/exceptions/notsuchfileexception.hpp

This file was deleted.

43 changes: 34 additions & 9 deletions src/gribreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
#include "gribmessageiterator.hpp"
#include "caster.hpp"
#include "converter.hpp"
#include "exceptions/notsuchfileexception.hpp"
#include "exceptions/nosuchgribfileexception.hpp"
#include "exceptions/nosuchlocationsfileexception.hpp"
#include "exceptions/arrowtablereadercreationexception.hpp"
#include "exceptions/arrowgenericexception.hpp"
#include "exceptions/invalidcsvexception.hpp"

using namespace std;
namespace cp = arrow::compute;
Expand All @@ -38,7 +42,7 @@ GribReader::GribReader(string filepath) : filepath(filepath) {
-999l);
fin = fopen(filepath.c_str(), "rb");
if (!fin) {
throw NoSuchFileException(filepath);
throw NoSuchGribFileException(filepath);
cout << "Error: unable to open input file" << filepath << endl;
} else {
cout << "I'm ready file is " << fin << endl;
Expand Down Expand Up @@ -277,18 +281,39 @@ bool GribReader::hasLocations() {
}

std::shared_ptr<arrow::Table> GribReader::getTableFromCsv(std::string path, arrow::csv::ConvertOptions convertOptions){
std::shared_ptr<arrow::io::ReadableFile> infile = arrow::io::ReadableFile::Open(path).ValueOrDie();
auto infile = arrow::io::ReadableFile::Open(path);

if (infile.ok()) {

auto csv_reader =
arrow::csv::TableReader::Make(
arrow::io::default_io_context(), infile.ValueOrDie(), arrow::csv::ReadOptions::Defaults(),
arrow::csv::ParseOptions::Defaults(), convertOptions);

if(csv_reader.ok()) {
auto table = csv_reader.ValueOrDie()->Read();

if (table.ok()) {
return table.ValueOrDie();
} else {
std::string errDetails = "Error reading results into arrow table is this a valid CSV ? ";
throw InvalidCSVException(errDetails );
}

} else {
std::string errDetails = "Unable to create arrow CSV table reader for file " + path;
throw UnableToCreateArrowTableReaderException(errDetails);
}



auto csv_reader =
arrow::csv::TableReader::Make(
arrow::io::default_io_context(), infile, arrow::csv::ReadOptions::Defaults(),
arrow::csv::ParseOptions::Defaults(), convertOptions).ValueOrDie();

} else {
throw NoSuchLocationsFileException(path);
}

std::shared_ptr<arrow::Table> table = csv_reader->Read().ValueOrDie();

return table;

}

arrow::Result<std::shared_ptr<arrow::Array>> GribReader::createSurrogateKeyCol(long numberOfRows){
Expand Down

0 comments on commit 1996ef1

Please sign in to comment.