guessnc.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef LPJGUESS_GUESSNC_H
  2. #define LPJGUESS_GUESSNC_H
  3. #ifdef HAVE_NETCDF
  4. #include "cftime.h"
  5. #include "cfvariable.h"
  6. #include <stdexcept>
  7. #include <string>
  8. namespace GuessNC {
  9. /// Exceptions thrown by this library are of this type (or sub-class)
  10. /** Like any exception inheriting from std::runtime_error, error
  11. * message can be retrieved by calling the what() member function.
  12. */
  13. class GuessNCError : public std::runtime_error {
  14. public:
  15. GuessNCError(const std::string& what) : std::runtime_error(what) {}
  16. };
  17. /// Help function to deal with status codes from NetCDF library
  18. /** Throws GuessNCError if status indicates an error */
  19. void handle_error(int status, const std::string& message);
  20. /// Opens a NetCDF file for reading and returns its id
  21. /** Throws GuessNCError if the file couldn't be opened */
  22. int open_ncdf(const char* fname);
  23. /// Closes a NetCDF file
  24. /** Throws a GuessNCError if NetCDF library reports an error */
  25. void close_ncdf(int ncid);
  26. /// Gets the value of a string attribute for a variable
  27. /** \returns false if the attribute didn't exist, or wasn't a string */
  28. bool get_attribute(int ncid_file, int ncid_var,
  29. const std::string& attr_name, std::string& value);
  30. /// Gets the value of a numeric attribute for a variable
  31. /** \returns false if the attribute didn't exist, or couldn't be interpreted as a double */
  32. bool get_attribute(int ncid_file, int ncid_var,
  33. const std::string& attr_name, double& value);
  34. /// Gets the dimension ids for a variable
  35. void get_dimensions(int ncid_file, int ncid_var, std::vector<int>& dimensions);
  36. /// Gets a coordinate variable corresponding to a dimension
  37. /** \returns false if there was no corresponding coordinate variable */
  38. bool get_coordinate_variable(int ncid_file, int ncid_dim, int& ncid_coord_var);
  39. /// Gets the name of a variable with a given id
  40. std::string get_variable_name(int ncid_file, int ncid_var);
  41. }
  42. #endif // HAVE_NETCDF
  43. #endif // LPJGUESS_GUESSNC_H